//=====================================================//
var LoadReadyLocal = {

    registerForm: function(){
        $(function() {
            Member.setupRegisterForm();
        });
    },

    smartApplication: function(){
        $(function() {
            Member.setupSmartApplication();
        });
    },

    softphoneApplication: function(){
        $(function() {
            Member.setupSoftphoneApplication();
        });
    },

    applicationForm: function(){
        $(function() {
            Member.setupApplicationForm();
        });
    },

    editProfileForm: function(){
        $(function() {
            Member.setupEditProfileForm();
        });
    },

    enquiryForm: function(){
        $(function() {
            Content.setupEnquiryForm();
        });
    },
    
    indiaDidForm: function(){
        $(function() {
            Content.setupIndiaDidForm();
        });
    },

    newsLetterForm: function(){
        $(function() {
            Content.setupNewsletterForm();
        });
    },

    emailToFriend: function(){
        $(function() {
            Dialog.setUpForm('emailToFriendForm');
            $('#btnEmailToFriend').click(function(e){
                e.preventDefault();
                Dialog.openDialog.call(this, 'emailToFriendForm', 'Email To A Friend');
            });
        });
    },
    
    product: function(){
    	$('#btnClearAll').click(function(e){
        	e.preventDefault();
            Util.openDialogForLink.call(this, "Calculate for Total");
      	});

        $('#clearAllValues').livequery('click', function(e){
        	e.preventDefault();
        	var url1 = '/index.php?_room=product&_spAction=clearAllValues&showHTML=0';
        		  
        	$.get(url1,  function(data){
        		window.location.reload();
       		});
        	$('#productForm select').val('');
      	});
    },

    forgotPassword: function(){
        $(function() {
            Dialog.setUpForm('forgotPasswordForm');
            $('#btnForgotPassword').click(function(e){
                e.preventDefault();
                Dialog.openDialog.call(this, 'forgotPasswordForm', 'Password Retrieval Form');
            });
        });
    },

    login: function(){
        $(function() {

            var extraPar = {
                callbackOnError: function() {
                    alert('Invalid email or password. Please try again.');
                }
            }

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

            $('#loginForm').ajaxForm(options);

            $('#resetLogin').click(function(e){
                e.preventDefault();
                $('#loginForm').resetForm();
            });

        });
    },
    
    agentLogin: function(){
        $(function() {

            var extraPar = {
                callbackOnError: function() {
                    alert('Invalid email or password. Please try again.');
                }
            }

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

            $('#agentLoginForm').ajaxForm(options);

            $('#resetAgentLogin').click(function(e){
                e.preventDefault();
                $('#agentLoginForm').resetForm();
            });

        });
    },

    countryByPackage: function(){
        $(function() {
            var options = {
                success: function(data) {
                    $('#searchResults').css('display', 'none');
                    $('#searchResults').html(data);
                    $('#searchResults').slideDown();
                    $('.progressSpan').removeClass('progress').css('display', 'none');
                },
                beforeSubmit: function() {
                    $('.progressSpan').addClass('progress').css('display', 'block');
                }
            };
            $('#frmSearch').ajaxForm(options);

            $('#submitSearch').click(function(e){
                e.preventDefault();
                $('#frmSearch').submit();
            });
        });
	},

    rateForm: function(){
        $(function() {
            var options = {
                success: function(data) {
                	/*var msg = "<div>Search Result</div>";
                	$('#searchDisplay').html(msg);*/
                	
                    $('#rateDisplay').css('display', 'none');
                    $('#rateDisplay').html(data);
                    $('#searchDisplay').slideDown();
                    $('#rateDisplay').slideDown();
                    $('.progressSpan').removeClass('progress').css('display', 'none');
                },
                beforeSubmit: function() {
                    $('.progressSpan').addClass('progress').css('display', 'block');
                }
            };
            $('#rateForm').ajaxForm(options);

        });
    }
};

LoadReady = $.extend(LoadReady, LoadReadyLocal);

//** extending the master Util class
var Util = $.extend(Util, {

});

$(function() {


    // set the border right of last item to 0 //
    $('#list li:nth-child(even)').css({'background-color': '#eff3ef'});
    $('table.generic tr:nth-child(even)').css({'background-color': '#f3f3f3'});
    $('#keyword').focus(function(){
        var value = $(this).val();
        if ($(this).val() == 'keyword search'){
            $(this).val("");
        }
    });

    $('a#loginClick').click(function(e){
        //alert(123);
        $('.productLoginForm').css('display', 'block');
        $('a#registerClick').click(function(e){
        $('.productLoginForm').css('display', 'none');
    });
    });

    $('a#registerClick').click(function(e){
        $('.productRegisterForm').css('display', 'block');
        $('a#loginClick').click(function(e){
        $('.productRegisterForm').css('display', 'none');
        });
    });

    $('a#agentLoginClick').click(function(e){
        e.preventDefault();
        $('.productAgentLoginForm').css('display', 'block');
    });

    $('#fld_packageId').change(function(){
        $('#totalPriceContainer').css('display', 'none');
        var value = $(this).val();
          var url1 = '/index.php?_room=product&_spAction=updateInCartPackage&showHTML=0';
          
          $.get(url1, {package : value}, function(data){
             $('td #packagePrice').html(data);
             //window.location.reload();
          });
            if(value == 1){
             $('tr#package').hide();
            }else {
             $('tr#package').show();
            }
    });

    $('#fld_packageId').change(function(){
        var value = $(this).val();
          var url1 = '/index.php?_room=product&_spAction=updateInCartRegistrationFee&showHTML=0';

          $.get(url1, {package : value}, function(data){
            if(value == 1){
             $('#activation').hide();
            }else {
             $('#activation').show();
            }
             $('#activationFee').html(data);
             //window.location.reload();
          });
    });

    $('#fld_Adapter').change(function(){
        $('#totalPriceContainer').css('display', 'none');
        var value = $(this).val();
        if(value != ""){
            if(value == 1){
             $('#adapter').hide();
            }else {
             $('#adapter').show();   
            }
          $('#adaptorPrice').load('/index.php?_room=product&_spAction=updateInCart&showHTML=0', {id : value});
          //$('#totalPriceContainer').load('/index.php?_room=product&_spAction=totalAmount&showHTML=0');
        }
    });

    $('#fld_countryDID1').change(function(){
        $('#totalPriceContainer').css('display', 'none');
        var value = $(this).val();
        if(value != ""){
            if(value == 59){
             $('#did1').hide();
            }else {
             $('#did1').show();   
            }
          $('#didPrice1').load('/index.php?_room=product&_spAction=updateInCartDIDRate1&showHTML=0', {did_id : value});
          //$('#totalPriceContainer').load('/index.php?_room=product&_spAction=totalAmount&showHTML=0');
        }
    });

    $('#fld_countryDID2').change(function(){
        $('#totalPriceContainer').css('display', 'none');
        var value = $(this).val();
        if(value != ""){
            if(value == 59){
             $('#did2').hide();
            }else {
             $('#did2').show();   
            }
          $('#didPrice2').load('/index.php?_room=product&_spAction=updateInCartDIDRate2&showHTML=0', {did_id1 : value});
          //$('#totalPriceContainer').load('/index.php?_room=product&_spAction=totalAmount&showHTML=0');
        }
    });

    $('#fld_countryDID3').change(function(){
        $('#totalPriceContainer').css('display', 'none');
        var value = $(this).val();
        if(value != ""){
            if(value == 59){
             $('#did3').hide();
            }else {
             $('#did3').show();   
            }
          $('#didPrice3').load('/index.php?_room=product&_spAction=updateInCartDIDRate3&showHTML=0', {did_id2 : value});
          //$('#totalPriceContainer').load('/index.php?_room=product&_spAction=totalAmount&showHTML=0');
        }
    });

    $('#fld_countryDID1').change(function(){
        var value = $(this).val();
        if(value != ""){
            if(value == 59){
             $('#setup1').hide();
            }else {
             $('#setup1').show();   
            }
          $('#setupFee1').load('/index.php?_room=product&_spAction=showDidSetupFee&showHTML=0', {did_id : value,sequence_code :1});
          //$('#totalPriceContainer').load('/index.php?_room=product&_spAction=totalAmount&showHTML=0');
        }
    });

    $('#fld_countryDID2').change(function(){
        var value = $(this).val();
        if(value != ""){
            if(value == 59){
             $('#setup2').hide();
            }else {
             $('#setup2').show();   
            }
          $('#setupFee2').load('/index.php?_room=product&_spAction=showDidSetupFee&showHTML=0', {did_id : value,sequence_code :2});
          //$('#totalPriceContainer').load('/index.php?_room=product&_spAction=totalAmount&showHTML=0');
        }
    });

    $('#fld_countryDID3').change(function(){
        var value = $(this).val();
        if(value != ""){
            if(value == 59){
             $('#setup3').hide();
            }else {
             $('#setup3').show();   
            }
          $('#setupFee3').load('/index.php?_room=product&_spAction=showDidSetupFee&showHTML=0', {did_id : value,sequence_code :3});
          //$('#totalPriceContainer').load('/index.php?_room=product&_spAction=totalAmount&showHTML=0');
        }
    });

    $('input[name=installation]').click(function(){
        $('#totalPriceContainer').css('display', 'none');
        var value = $(this).val();
        if(value != ""){
            if(value == 0.00){
             $('#installation').hide();
            }else {
             $('#installation').show();   
            }
          $('#installationPrice').load('/index.php?_room=product&_spAction=updateInCart&showHTML=0', {installation : value});
          //$('#totalPriceContainer').load('/index.php?_room=product&_spAction=totalAmount&showHTML=0');
          //window.location.reload();
        }
    });


    $('#recalculate').click(function(e){
    	e.preventDefault();
        $('#totalPriceContainer').css('display', 'block');
        value = $('#fld_agent_code').val();
        //if(value != ""){
          $('#agentCodeResult').load('/index.php?_room=product&_spAction=agentCodeValidationMessage&showHTML=0', {agent_code : value});
          //$('#agentPrice').load('/index.php?_room=product&_spAction=agentCodeValidation&showHTML=0', {agent_code //: value});
          //$('#totalPriceContainer').load('/index.php?_room=product&_spAction=totalAmount&showHTML=0');
        //}
    });

    $('#add_did').click(function(e){
        $('.did1').css('display', 'block');
    });

    $('#add_did1').click(function(e){
        $('.did2').css('display', 'block');
    });

    $('#add_did2').click(function(e){
        alert ('Please contact CNG telecom Admin for more DID numbers ');
        $('.did3').css('display', 'block');
    });

    $('#remove_did').click(function(e){ 
          value = 'removeDid';
          $('#didPrice1').load('/index.php?_room=product&_spAction=updateInCartDIDRate1&showHTML=0', {did_id : value});
          $('#totalPriceContainer').load('/index.php?_room=product&_spAction=totalAmount&showHTML=0');
          window.location.reload();
    });

    $('#remove_did1').click(function(e){
          value = 'removeDid';
          $('#didPrice2').load('/index.php?_room=product&_spAction=updateInCartDIDRate2&showHTML=0', {did_id1 : value});
          window.location.reload();
    }); 

    $('#remove_did2').click(function(e){
          value = 'removeDid';
          $('#didPrice3').load('/index.php?_room=product&_spAction=updateInCartDIDRate3&showHTML=0', {did_id2 : value});
          window.location.reload();
    });

    $('#fld_Voucher').change(function(){
        $('#totalPriceContainer').css('display', 'none');
        var value = $(this).val();
        if(value != ""){
            if(value == 6){
             $('#voucher').hide();
            }else {
             $('#voucher').show();   
            }
          $('#voucherPrice').load('/index.php?_room=product&_spAction=updateInCart&showHTML=0', {voucher : value});
          //$('#totalPriceContainer').load('/index.php?_room=product&_spAction=totalAmount&showHTML=0');
        }
    });


    $('input[name=paymentMethod]').click(function(){
        var value = $(this).val();
        if(value != ""){
            $('.confirmOrder').css('display', 'block');
            var url = '/index.php?_room=product&_spAction=updatePaymentType&showHTML=0';
            $.get(url, {payment_method : value}, function (data) {
            })
        }
    });

    $('#paymentForm').submit(function(e){
        e.preventDefault();
        var valNumeric   = $('#fld_payment_amount').val();
        var valAlphabet  = valNumeric.replace(/[A-Za-z\s]/g ,"");

        if (valNumeric < 10 ){
            alert ("Amount should be atleast greater than 10");
        } else if (valAlphabet == ""){
            alert ("Value should be numeric");
        } else if ($("select[name='paymentFormPackage']):selected").val() == ''){
            alert('Please select your package');
        } else {
            $(this).unbind('submit');
            $(this).submit();
        }     
    });

    $('#fld_package').change(function(e){
        $('#rateForm').submit();
    });
	
    $('#fld_package').attr('disabled','disabled');

    $('#fld_country').change(function(e){
        e.preventDefault();
        $('#fld_package').val('');
        $('#rateDisplay').css('display','none');

        var valueCountry = $("select[id='fld_country']):selected").val();
        if (valueCountry != ''){
            $('#fld_package').removeAttr('disabled').attr('enabled','enabled');
        } else {
            $('#fld_package').removeAttr('enabled').attr('disabled','disabled');
            $('#fld_package').val('');
            $('#rateDisplay').css('display','none');
        }
    });

    $('#refreshSite').click(function(e){
        //window.location.reload();
    });

    $('#clearAll').click(function(){
          var url1 = '/index.php?_room=product&_spAction=clearAllValues&showHTML=0';
          
          $.get(url1,  function(data){
             window.location.reload();
          });
        
        $('#productForm select').val('');
    });

    $('#clear').click(function(e){
        e.preventDefault();
        $('#fld_agent_code').val('');
    });

    $('#edit').livequery('click', function(e){
        e.preventDefault();
		var d = $(window.parent.document).find('#dialog');
        d.dialog('close');             	
    });

    $('input[name=paymentMethod]').click(function(){
        var value = $(this).val();
        if(value == "payByCreditCard"){
            $('.totalCharge').css('display', 'block');
        }else {
            $('.totalCharge').css('display', 'none');
        }
    });

});

