var PostNewItem   = new Object();
//=====================================================//
//** extending the master Util class
var Util = $.extend(Util, {
    showSimpleMessageInDialog: function(msg) {

        Util.initDialog();
        $('#dialog').html(msg);
        
        var xButtons = {};
        
        xButtons.close = function() {
            $(this).dialog('close');
            $(this).dialog('destroy');
        };
        
        var x_dialog = $('#dialog').dialog(
            $.extend(Util.dialogDefaults, {
                buttons: xButtons
            })
        );
    },

    openDialogForLink: function(dialogTitle, w, h) {
        if (!w){
           w = 350;
        }
        
        if (!h){
           h = 230;
        }

        url = $(this).attr('href');

        Util.showProgressInd();

        $.get(url, function(data){
            Util.initDialog();
            $('#dialog').html(data);

            var x_dialog = $('#dialog').dialog(
                $.extend(Util.dialogDefaults, {
                    width: w,
                    height: h,
                    title: dialogTitle
                })
            );
            Util.hideProgressInd();
        });
    }

});

//=====================================================//
var Validate = $.extend(Validate, {

});

var Member = {
    setupRegisterForm: function(e) {
        
        var dateObj = new Date();
        var currentYear = dateObj.getFullYear();
	    $("#fld_issued_date , #fld_exp_date, #fld_date_of_birth")
	    .datepicker(
	        {
	            showOn: 'button',
	            buttonImage: '/images/icons/calendar_icon.gif',
	            dateFormat: 'yy-mm-dd',
	            buttonImageOnly: true,
	            changeYear: true,
	            yearRange: '1940:' + currentYear
             }
         )

	    .focus(function(){
            $(this).blur();
	    });

	    $('#ui-datepicker-div').draggable();

        var extraPar = {
            callback: function() {
                var msg = "<div class='sysMessage'>Thanks for registering.</div>";
                $('#registerForm').html(msg);
            }
        }

        var options = {
            success: function(json, statusText, jqFormObj) {
                Validate.validateFormData(json, statusText, jqFormObj, extraPar);
            },
            beforeSubmit: function() {},
            dataType: 'json'
        };
        $('#registerForm').ajaxForm(options);
    },

    setupSmartApplication: function(e) {
        var extraPar = {
            callback: function() {
                var url = '/index.php?_room=member&_spAction=smartApplicationThanks&showHTML=0';
                $.get(url, function (data) {
                    $('#smartApplication').html(data);
                }) 
            }
        }

        var options = {
            success: function(json, statusText, jqFormObj) {
                Validate.validateFormData(json, statusText, jqFormObj, extraPar);
            },
            beforeSubmit: function() {},
            dataType: 'json'
        };
        $('#smartApplication').ajaxForm(options);
    },

    setupSoftphoneApplication: function(e) {
        var extraPar = {
            callback: function() {
                var url = '/index.php?_room=member&_spAction=softphoneApplicationThanks&showHTML=0';
                $.get(url, function (data) {
                    $('#softphoneApplication').html(data);
                }) 
            }
        }

        var options = {
            success: function(json, statusText, jqFormObj) {
                Validate.validateFormData(json, statusText, jqFormObj, extraPar);
            },
            beforeSubmit: function() {},
            dataType: 'json'
        };
        $('#softphoneApplication').ajaxForm(options);
    },

    setupApplicationForm: function(e) {
        var extraPar = {
            callback: function() {
                var url = '/index.php?_room=member&_spAction=applicationThanks&showHTML=0';
                $.get(url, function (data) {
                    $('#applicationForm').html(data);
                }) 
            }
        }

        var options = {
            success: function(json, statusText, jqFormObj) {
                Validate.validateFormData(json, statusText, jqFormObj, extraPar);
            },
            beforeSubmit: function() {},
            dataType: 'json'
        };
        $('#applicationForm').ajaxForm(options);
    },
    
    setupEditProfileForm: function(e) {
        var extraPar = {
            callback: function() {
                var msg = "<div class='sysMessage'>Your profile has been updated.</div>";
                $('#editProfileForm').html(msg);
            }
        }

        var options = {
            success: function(json, statusText, jqFormObj) {
                Validate.validateFormData(json, statusText, jqFormObj, extraPar);
            },
            beforeSubmit: function() {},
            dataType: 'json'
        };
        $('#editProfileForm').ajaxForm(options);
    }

}

//------------------------------------------------//
var Dialog = {

    setUpForm: function(formName) {
        $('#' + formName).livequery(function() {

            /****************************************************/
            var extraPar = {
                callback: function(json) {
                    if (json.returnText != ''){
                        Util.showSimpleMessageInDialog(json.returnText);
                    }
                }
            }

            var options = {
                success: function(json, statusText, jqFormObj) {
                    Validate.validateFormData(json, statusText, jqFormObj, extraPar);
                    Util.hideProgressInd();
                },
                beforeSubmit: function(frmData) {
                    Util.showProgressInd();
                },
                dataType: 'json'
            };

            $('#' + formName).ajaxForm(options);

        });
    },

    openDialog: function(formName, dialogTitle) {

        url = $(this).attr('href');

        Util.showProgressInd();

        $.get(url, function(data){
            Util.initDialog();
            $('#dialog').html(data);

            var xButtons = {};

            xButtons.submit = function() {
                $('#' + formName).submit();
            };

            xButtons.cancel = function() {
                $(this).dialog('close');
                $(this).dialog('destroy');
            };

            var x_dialog = $('#dialog').dialog(
                $.extend(Util.dialogDefaults, {
                    width: 450,
                    height: 410,
                    title: dialogTitle,
                    buttons: xButtons
                })
            );
            Util.hideProgressInd();
        });
    }
}

var EmailFriend = {
    emailToFriendForm: function(e) {
        var extraPar = {
            callback: function() {
                var msg = "<div class='sysMessage'>Your message has been sent successfully.</div>";
                $('#emailToFriendForm').html(msg);
            }
        }

        var options = {
            success: function(json, statusText, jqFormObj) {
                Validate.validateFormData(json, statusText, jqFormObj, extraPar);
            },
            beforeSubmit: function() {},
            dataType: 'json'
        };
        $('#emailToFriendForm').ajaxForm(options);
    }
}

//------------------------------------------------//
var Content = {
    setupEnquiryForm: function(e) {
        var extraPar = {
            callback: function() {
                var msg = "<div class='sysMessage'>Thanks for your enquiry.</div>";
                $('#enquiryForm').html(msg);
            }
        }

        var options = {
            success: function(json, statusText, jqFormObj) {
                Validate.validateFormData(json, statusText, jqFormObj, extraPar);
            },	
            beforeSubmit: function() {},
            dataType: 'json'
        };
        $('#enquiryForm').ajaxForm(options);
    },


    setupNewsletterForm: function(e) {
        var extraPar = {
            callback: function() {
                var url = '/index.php?_spAction=newsLetterThanks&showHTML=0';
                $.get(url, function (data) {
                    $('#newsLetterForm').html(data);
                }) 
            }
        }

        var options = {
            success: function(json, statusText, jqFormObj) {
                Validate.validateFormData(json, statusText, jqFormObj, extraPar);
            },
            beforeSubmit: function() {},
            dataType: 'json'
        };
        $('#newsLetterForm').ajaxForm(options);
    },

    setupIndiaDidForm: function(e) {
        var options = {
            success: function(json, statusText, jqFormObj) {
                Validate.validateFormData(json, statusText, jqFormObj);
                Util.hideProgressInd();
            },
            beforeSubmit: function() {
                Util.showProgressInd();
            },
            dataType: 'json'
        };
        $('#indiaDidForm').ajaxForm(options);
    }

}

//------------------------------------------------//
var CPCalendar = {

    displayCPCalendar: function(m,y, ajaxPath) {
    	var ran_no=(Math.round((Math.random()*9999)));
        var url = ajaxPath + '&m='+m+'&y='+y+'&ran='+ran_no

        $.get(url, function(data){
            $('#cpCalender').html(data);
        });
    }
}

//------------------------------------------------//
