/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
if($("#paymentSettingsGroupID").val()>0) {
    $(".paymentgatwaytab").trigger('click');
    
}

$(document).on("click",".paymentgatwaytab", function () {
    var text = $(this).text();
   
    $("#paymentSettingsGroupID").val($(this).attr("id"));
    $("#hidRel").val($(this).attr("rel"));
    $("#paymentSettingsGroupID").trigger('change');
    $('.payment-gateways .paymentgatwaytab').removeClass('active');
    $("#payment-tab-sel").text(text);
    $(this).addClass('active'); 
});

$(document).ready(function () {
    $("#netPayGroupCheck").val(0);
    $("#AjaxdivBillingValues").hide();
    $("#AjaxdivBillingFields").show();
    $("#AjaxAvailbalance").hide();
    $("#AjaxdivPaymentProcess").hide();
    $('#Ajaxcommon_message_buy_limit').hide();
    $('#AjaxpaymentErrorMessage').hide();
    $("#divPaymentButton").hide();
    $("#divCompleteOrdertButton").hide();
    $("#divAccountCredit").hide();
    $(document).on('change',"#billingCountry", function () {
        var billingCountry = '';
        billingCountry = $(this).val();
        //listCities(billingCountry, '');
        listStates(billingCountry, '');
    });
    /*$(document).on('change',"#billingCity", function () {
        var billingCity = '';
        billingCity = $(this).val();
        if (billingCity > 0) {
            getCityHiddenValue(billingCity, '');
        } else {
            $("#billingCity").val('');
            $("#billingCityID").val('');
        }
    });*/
    $(document).on('change',"#netpayPaymentMethod", function () {
        setNetpayHiddenValue($(this).val());
    });
    checkPaymentNeeded();
    updatehiddenpaymentGroupId();
    getValidateCardTypeHiddenValue();
    showPaymentForm('L');
    $(document).on('change',"#paymentSettingsGroupID", function () { 
        if ($(this).val() == '') {
            $("#AjaxAgreeTerms").hide();
        }
        $("#errorMsg").val('');
        //Hide Offline Payment Desc
        $(".AjaxOfflinePaymentDesc").html('');
        updatehiddenpaymentGroupId();
        showPaymentForm('L');
    })
     $(document).on('change',"#paymentSettingsGroupIDRes", function () { 
        if ($(this).val() == '') {
            $("#AjaxAgreeTerms").hide();
        } 
		
		$("#hidRel").val($("#paymentSettingsGroupIDRes option:selected").attr('data'));
        $("#paymentSettingsGroupID").val($(this).val());
        
        $("#errorMsg").val('');
        //Hide Offline Payment Desc
        $(".AjaxOfflinePaymentDesc").html('');
        updatehiddenpaymentGroupId();
        showPaymentForm('L');
    })
	
    $(document).on("click","#agree", function () {    
        showAgreeError();
    })
    showPaymentForm('C');
   
    $(document).on('blur',"#billingProvince", function () {
        getCityHiddenValue($("#billingProvince").val());
    })
    $(document).on('click','#submitPayment', function () {
       
        var processOrder = 1;
        $('#append_hidden').html($('#div_qnty_update').html());
        afterSubmitChange(processOrder);
    });
   // $(document).on('click','#submitOrder', function () {
        $(document).on('submit','#formProductPayment',function()  { 
       
            var processOrder = 1;
            
            $('#append_hidden').html($('#div_qnty_update').html());
            $("#AjaxAgreeTerms").hide();
            $("#AjaxdivPaymentProcess").show();
            $("#AjaxdivPaymentBlock").hide();
            $("#divPaymentButton").hide();
            $("#divCompleteOrdertButton").hide();
            $("#AjaxdivPaymentSection").hide();
            $("#AjaxdivBillingSection").hide();
            return true;
    });
    $(document).on('click','#submitCompleteOrder', function () {
        
        var processOrder = 1;
        $('#append_hidden').html($('#div_qnty_update').html());
        afterSubmitChange(processOrder);
    });
    getCityHiddenValue($("#billingCityID").val(), 'success');
});
function updatehiddenpaymentGroupId() {
    if ($("#paymentSettingsGroupID").val() <= 0) {
        $("#hidPaymentSettingsGroupIDTemp").val(0);
        $("#hidPaymentSettingsGroupIDcheck").val(0);
    } else {
        if ($("#paymentSettingsGroupID").val() == 28 || $("#paymentSettingsGroupID").val() == 29 || $("#paymentSettingsGroupID").val() == 30 || $("#paymentSettingsGroupID").val() == 4 || $("#paymentSettingsGroupID").val() == 5 || $("#paymentSettingsGroupID").val() == 7 || $("#paymentSettingsGroupID").val() == 10 || $("#paymentSettingsGroupID").val() == 11 || $("#paymentSettingsGroupID").val() == 12 || $("#paymentSettingsGroupID").val() == 17 || $("#paymentSettingsGroupID").val() == 18 || $("#paymentSettingsGroupID").val() == 21 || $("#paymentSettingsGroupID").val() == 20 || $("#paymentSettingsGroupID").val() == 23) {
            $("#hidPaymentSettingsGroupIDTemp").val($("#paymentSettingsGroupID").val());
            $("#hidPaymentSettingsGroupIDcheck").val(0);
        } else
            $("#hidPaymentSettingsGroupIDcheck").val(1);
        $("#AjaxAgreeTerms").show();
    }
}
function checkPaymentNeeded() {
    
    var paymentgroup = $("#paymentSettingsGroupID").val();
    
    var showAvailBalance = 0;
	var val = $('#couponAmount').val();
	showAvailBalance = 1;
	if (parseInt(val) > parseInt($("#availableAmt").val())) {
		var totalAmount = 1;
	} else {
		var totalAmount = 0;
	}
	$("#AjaxShowBalance").hide();
	if ((parseInt($("#availableAmt").val()) > 0 && totalAmount > 0)) {
		$("#AjaxShowBalance").show();
	}

    $("#divShowPaymentSection").show();
    $("#divPaymentButton").hide();
    $("#AjaxAgreeTerms").hide();
    $("#AjaxdivPaymentSection").show();
    $(".payment-block").removeClass('account-credit');
    if (totalAmount > 0) {
        $('.pay-img').show();
        $(".payhead").show();
        $(".payment-block-new").show();
        $("#AjaxAvailbalance").hide(); 
        if (showAvailBalance > 0 && $("#availableAmt").val() > 0) {
            $("#AjaxAvailbalance").show();
        }
        
        if (paymentgroup <= 0 && ($("#hidPaymentSettingsGroupID").val() != paymentgroup)) {
            
            $("#hidPaymentSettingsGroupID").val($('.payment-gateways a.active').attr('id'));            
            $("#paymentSettingsGroupID").val($("#hidPaymentSettingsGroupID").val());
            
            updatehiddenpaymentGroupId();
            showPaymentForm('L');
        }
        //$("#AjaxdivPaymentSection").show();
        $("#divCompleteOrdertButton").hide();
        $("#divAccountCredit").hide();
        if ($("#paymentSettingsGroupID").val() > 0) {
            if ($("#paymentSettingsGroupID").val() == 28 || $("#paymentSettingsGroupID").val() == 29 || $("#paymentSettingsGroupID").val() == 30 || $("#paymentSettingsGroupID").val() == 4 || $("#paymentSettingsGroupID").val() == 5 || $("#paymentSettingsGroupID").val() == 7 || $("#paymentSettingsGroupID").val() == 10 || $("#paymentSettingsGroupID").val() == 13 || $("#paymentSettingsGroupID").val() == 15 || $("#paymentSettingsGroupID").val() == 17 || $("#paymentSettingsGroupID").val() == 18 || $("#paymentSettingsGroupID").val() == 19 || $("#paymentSettingsGroupID").val() == 21 || $("#paymentSettingsGroupID").val() == 20 || $("#paymentSettingsGroupID").val() == 23) {
                $("#divPaymentButton").hide();
                $("#divCompleteOrdertButton").show();
            } else {
                $("#divCompleteOrdertButton").hide();
                $("#divPaymentButton").show();
            }
            //$("#divPaymentButton").show();	
            $("#AjaxAgreeTerms").show();
        } else {
            $("#AjaxAgreeTerms").hide();
        }
    } else {
        if (showAvailBalance > 0 && $("#availableAmt").val() > 0) {
            $("#AjaxAvailbalance").show();           
        } else {
            $("#AjaxAvailbalance").hide();
        }
        $('.pay-img').hide();
        $(".payment-block").addClass('account-credit');
        //$("#AjaxdivPaymentSection").show();
        $(".payhead").hide();
        $(".payment-block-new").hide();
        $("#divCompleteOrdertButton").show();
        $("#divAccountCredit").show();
        
        $("#paymentSettingsGroupID").val(0);
        $("#netPayGroupCheck").val(0);
        updatehiddenpaymentGroupId();
        showPaymentForm('L');
        $("#AjaxdivPaymentElements").hide();
        $("#AjaxdivPaymentCardTypes").hide();
        $("#AjaxAgreeTerms").show();
    }
    if ($("#paymentSettingsGroupID").val() <= 0) {
        $("#AjaxdivBillingValues").css("display", $("#AjaxdivBillingValues").css("display"));
        $("#AjaxdivBillingFields").css("display", $("#AjaxdivBillingFields").css("display"));
    }
}
function showAgreeError() {
    if ($("#enableTermsPrivacy").val() == 'Yes') {
        if ($('#agree').prop('checked')) {
            
            $("#agreeTerms").val(1);
        } else {
            $("#agreeTerms").val('');
        }
       
    }
}
function showPaymentForm(arg) {
    $('#netpayPayContainer').hide();
    var appendCardImages = ''; //
    var processPay = 1;
    var fromFacebook = 'No';
    if (processPay == 1) {
        if ($("#billingAddress").val() == '' || $("#billingCity").val() == '' || $("#billingCountryID").val() == '') {
            fromFacebook = 'Yes';
        }
        $("#editLinkID").show();
        $('#billingCountry').val($('#billingCountryID').val());
        if (arg == 'C' || fromFacebook == 'Yes') {
            $("#editBillingInfo").val(2);
            $("#AjaxdivBillingValues").hide();
            $("#AjaxdivBillingFields").show();
            var countryID = $('#billingCountryID').val();
            $.ajax({
                type: "POST",
                url: GLOBAL_BASE_PATH + "/giftcoupon/ajaxCountry/",
                data: 'countryID=' + countryID,
                success: function (data) {
                    $('#billingCountry').empty();
                    $("#billingCountry").append(data);
                }
            });
            var city = $('#cityhidValue').val();
            //listCities(countryID, city);
			listStates($('#billingCountryID').val(), $('#billingStateID').val());
        }
    } else {
        $("#editLinkID").hide();
        if ($("#paymentSettingsGroupID").val() > 0) {
            //alert('temp');
            $("#paymentSettingsGroupID").val(0);
        }
    }
    if ($("#paymentSettingsGroupID").val() > 0) {
        $("#divAccountCredit").hide();
        if (processPay == 1) {
            if (arg != 'C') {
                // alert('her1'+$("#AjaxdivBillingValues").css("display"));
                $("#AjaxdivBillingValues").css("display", $("#AjaxdivBillingValues").css("display"));
                $("#AjaxdivBillingFields").css("display", $("#AjaxdivBillingFields").css("display"));
            }
            $("#editLinkID").show();
            $("#netPayGroupCheck").val(0);
            if ($("#paymentSettingsGroupID").val() != 28 && $("#paymentSettingsGroupID").val() != 29 && $("#paymentSettingsGroupID").val() != 30 && $("#paymentSettingsGroupID").val() != 4 && $("#paymentSettingsGroupID").val() != 5 && $("#paymentSettingsGroupID").val() != 7 && $("#paymentSettingsGroupID").val() != 10 && $("#paymentSettingsGroupID").val() != 11 && $("#paymentSettingsGroupID").val() != 12 && $("#paymentSettingsGroupID").val() != 17 && $("#paymentSettingsGroupID").val() != 18 && $("#paymentSettingsGroupID").val() != 21 && $("#paymentSettingsGroupID").val() != 20 && $("#paymentSettingsGroupID").val() != 23) {
                //alert('her2');
                $(".AjaxOfflinePaymentDesc").html('');
                $("#divPaymentButton").show();
                $("#divCompleteOrdertButton").hide();
                $("#AjaxdivPaymentCardTypes").show();
                $("#AjaxloadProcessImagePay").append('<img src="' + GLOBAL_ROOT_PATH + '/public/default/manage/standard/images/ajax-loader.gif" id="loading_gif2" width="16" height="16" border="0" alt="Loading"/>');
                $("#AjaxdivPaymentElements").show();
                $("#AjaxdivPayCardholderFirstName").show();
                $("#AjaxdivPayCardholderLastName").show();
                $("#AjaxdivPayCardNumber").show();
                $("#AjaxdivPayCardType").show();
                $("#AjaxdivPayExpiryDate").show();
                $("#AjaxloadProcessImagePay").empty();
                if (arg != 'C') {
                    $.ajax({
                        type: "POST",
                        url: GLOBAL_BASE_PATH + "/giftcoupon/ajaxCardType/",
                        data: 'cardType=' + $('#creditCardTypeId').val() + '&paymentId=' + $("#paymentSettingsGroupID").val(),
                        success: function (data) {
                            $('#creditCardType').empty();
                            if (data != '') {
                                $('#creditCardType').show();
                                $("#creditCardType").append(data);
                                $("#creditCardType option").each(function () {
                                    if ($(this).text() != selecttext) {
                                        var cardImage = $("#imgFullPath").val() + '/' + $(this).val() + '.png';
                                        appendCardImages = appendCardImages + '<li> <img src="' + cardImage + '" alt="' + $(this).text() + '" title="' + $(this).text() + '" /> </li>';
                                    }
                                });
                            } else {
                                $('#creditCardType').hide();
                            }
                            $("#AjaxdivPaymentCardTypeImages").empty();
                            $("#AjaxdivPaymentCardTypeImages").append(appendCardImages);
                        }
                    });
                }
            } else if ($("#paymentSettingsGroupID").val() == 17 || $("#paymentSettingsGroupID").val() == 18) {
                $("#divPaymentButton").hide();
                hidePaymentFields(1);
                $("#editLinkID").show();
                $(".AjaxOfflinePaymentDesc").html('<img src="' + GLOBAL_ROOT_PATH + '/public/default/manage/standard/images/ajax-loader.gif" id="loading_gif2"  />');
                $.ajax({
                    type: "POST",
                    url: GLOBAL_BASE_PATH + "/cart/ajaxOfflinePayments/",
                    data: 'paymentSettingsGroupID=' + $("#paymentSettingsGroupID").val(),
                    success: function (data) {
                        $(".AjaxOfflinePaymentDesc").html(data);
                        $("#divPaymentButton").hide();
                        $("#divCompleteOrdertButton").show();
                    }
                });
            }
            //Netpay
            else if ($("#paymentSettingsGroupID").val() == 21) {
                $("#divPaymentButton").hide();
                hidePaymentFields(1);
                $("#netPayGroupCheck").val(1);
                $("#AjaxloadProcessNetpay").html('<img src="' + GLOBAL_ROOT_PATH + '/public/default/manage/standard/images/ajax-loader.gif" />');
                $.ajax({
                    type: "POST",
                    url: GLOBAL_BASE_PATH + "/cart/ajaxNetpayMethods/",
                    data: 'paymentSettingsGroupID=' + $("#paymentSettingsGroupID").val() + '&netpayPaymentMethod=' + $("#hidNetpayPaymentMethod").val(),
                    success: function (data) {
                        $('#netpayPaymentMethod').empty();
                        $('#netpayPaymentMethod').append(data);
                        $('#netpayPayContainer').show();
                        $("#AjaxloadProcessNetpay").html('');
                        $("#divPaymentButton").hide();
                        $("#divCompleteOrdertButton").show();
                    }
                });
            } else {
                //Hide Offline Payment Desc
                $(".AjaxOfflinePaymentDesc").html('');
                hidePaymentFields(1);
                $("#editLinkID").show();
                $("#divCompleteOrdertButton").show();
            }
        } else {
            hidePaymentFields(0);
        }
        
        $('.pay-img').html('<img src="'+GLOBAL_IMAGES_PATH+'/'+$("#hidRel").val()+'_'+languageLCIDStr+'.png">');
    } else {
        hidePaymentFields(1);
        if (arg != 'C' && fromFacebook == 'No') {
            $("#AjaxdivBillingValues").hide();
            $("#AjaxdivBillingFields").show();
            $("#editBillingInfo").val(1);
        }
    }
    
}
function hidePaymentFields(arg) {
    $("#AjaxloadProcessImagePay").empty();
    $("#AjaxdivPaymentElements").hide();
    $("#AjaxdivPaymentCardTypes").hide();
    $("#divPaymentButton").hide();
    $("#divCompleteOrdertButton").hide();
    if (arg == 1) {
        //if ($("#AjaxdivPaymentSection").css("display") == 'none') {
            $("#divCompleteOrdertButton").show();
        //}
    }
}
//ajax state populating
function listCities(countryID, cityID) {
 $('#cityhidValue').val(cityID);
    /*$('#billingCity').empty();
	$("#cityProvinceInput").hide();
	$('#billingCountryID').val(countryID);*/
    /*if (countryID) {
        if ($("#hid_err_formProductPayment_billingProvince").val() == 1) {
            $("#hid_err_formProductPayment_billingProvince").val(0);
        }
    } else {
        cityID = '';
        $("#billingProvince").val('');
    }*/;
    if(countryID!=''){
		var data={
			"countryID": countryID , 
			"userCity" : cityID
		};
		$.ajax({
			type: "POST",
			url: GLOBAL_BASE_PATH + "/ajax/ajaxCity/",
			data: data,
			beforeSend:function(){
				 $("#loadProcessImage").append('<img src="' + GLOBAL_ROOT_PATH + '/public/default/manage/standard/images/ajax-loader.gif" id="loading_gif2"  />');
			},
			success: function (msg) {
				$("#loadProcessImage").empty();
				$(".choosen").chosen();
				if (msg != '') {
					$("#cityInput").show();
					$('#billingCity').empty();
					$("#cityProvinceInput").hide();
					$("#billingCity").append('<option value="">' + selecttext + '</option>' + msg);
					$('#billingCity').trigger("liszt:updated");
					$("#citySelectedType").val("city");
					$("#billingProvince").val('');
				} else {
					$('#billingCity').empty();
					$("#cityInput").hide();
					$("#cityProvinceInput").show();
					$("#billingCity").val('');
					$("#billingProvince").val(cityID);
					$("#citySelectedType").val("province");
				}
			}
		});
	}
	
}
function afterSubmitChange(processOrder) {
    if (processOrder == 1) {
       
        if ($("#paymentSettingsGroupID").val() == '') {
            $("#paymentSettingsGroupID").val(0);
            updatehiddenpaymentGroupId();
            showPaymentForm('L');
        }
    }
    $("#AjaxAgreeTerms").hide();
    $("#AjaxdivPaymentProcess").show();
    $("#AjaxdivPaymentBlock").hide();
    $("#divPaymentButton").hide();
    $("#divCompleteOrdertButton").hide();
    $("#AjaxdivPaymentSection").hide();
    $("#AjaxdivBillingSection").hide();
    var paymentgroup = $("#paymentSettingsGroupID").val();
    if (paymentgroup > 0 && (paymentgroup == 28 || paymentgroup == 29 || paymentgroup == 30 || paymentgroup == 4 || paymentgroup == 5 || paymentgroup == 7 || paymentgroup == 10 || paymentgroup == 11 || paymentgroup == 12 || paymentgroup == 17 || paymentgroup == 18 || paymentgroup == 20)) {
        $("#hid_err_formProductPayment_creditCardFirstName").val(0);
        $("#hid_err_formProductPayment_creditCardLastName").val(0);
        $("#hid_err_formProductPayment_validateCreditCardType").val(0);
        $("#hid_err_formProductPayment_creditCardNumber").val(0);
        $("#hid_err_formProductPayment_securityCode").val(0);
    }
}
function getCityHiddenValue(val) {
    //Hide Offline Payment Desc
    if ($("#paymentSettingsGroupID").val() == '') {
        $(".AjaxOfflinePaymentDesc").html('');
    }
    $('#billingCityID').val(val);
}
function getCardTypeHiddenValue(val) {
    $('#creditCardTypeId').val(val);
    getValidateCardTypeHiddenValue();
}
function getValidateCardTypeHiddenValue() {
    var val = $('#creditCardTypeId').val();
    if (val == '') {
        $('#validateCreditCardTypeId').val(1);
    } else {
        $('#validateCreditCardTypeId').val(2);
    }
}
function setNetpayHiddenValue(methodKey) {
    $("#hidNetpayPaymentMethod").val(methodKey);
}
//ajax state populating
function listStates(countryID,stateID){
	
	if(countryID!=''){
		var data=new Object();
		data.countryID=countryID;
		data.stateID=stateID;
		$.ajax({
			type: "POST",
			url: GLOBAL_BASE_PATH+"/ajax/ajaxState/",
			data: data,
			beforeSend:function(){
				$("#loadProcessImage").append('<img src="' + GLOBAL_ROOT_PATH + '/public/default/manage/standard/images/ajax-loader.gif" id="loading_gif2"  />');
			},
			success: function(msg){  
				if(msg!=''){
					$("#stateInput").show();
					$('#billingState').empty();
					$("#stateProvinceInput").hide();
					$("#billingState").append('<option value="">'+selecttext+'</option>'+msg);
					$("#stateSelectedType").val("state");	
					$("#billingProvince").val('');
				} else {
					$("#stateInput").hide();
					$("#stateProvinceInput").show();
					if(stateID==0) { 
						stateID	=	'';
					}
					if(!(stateID>=0 || stateID==''))
					$("#billingProvince").val(stateID);
					$("#stateSelectedType").val("province");
						$("#billingState").empty()
						$("#billingStateID").val('');
				}
				$("#loadProcessImage").empty();
			}
		});
	}
}
