/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$(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();
    $(document).on('change',"#billingCountry", function () {
        var billingCountry = '';
        billingCountry = $(this).val();
        listStates(billingCountry, '');
    });
    $(document).on('change',"#billingState", function () {
        var billingState = '';
        billingState = $(this).val();
        if (billingState > 0) {
            getStateHiddenValue(billingState, '');
        } else {
            $("#billingState").val('');
            $("#billingStateID").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');
    })
    $("#agree").click(function () {
        showAgreeError();
    })
    showPaymentForm('C');
    $(document).on('blur',"#billingProvince", function () {
        getStateHiddenValue($("#billingProvince").val());
    })
    $(document).on('click','#submitPayment', function () {
        var processOrder = 1;
        $('#append_hidden').html($('#div_qnty_update').html());
        afterSubmitChange(processOrder);
    });
    $(document).on('click','#submitCompleteOrder', function () {
        var processOrder = 1;
        $('#append_hidden').html($('#div_qnty_update').html());
        afterSubmitChange(processOrder);
    });
    getStateHiddenValue($("#billingStateID").val(), 'success');
});
function updatehiddenpaymentGroupId() {
    if ($("#paymentSettingsGroupID").val() <= 0) {
        $("#hidPaymentSettingsGroupIDTemp").val(0);
        $("#hidPaymentSettingsGroupIDcheck").val(0);
    } else {
        if ($("#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();
    if (totalAmount > 0) {
        $("#AjaxAvailbalance").hide();
        if (showAvailBalance > 0 && $("#availableAmt").val() > 0) {
            $("#AjaxAvailbalance").show();
        }
        if (paymentgroup <= 0 && ($("#hidPaymentSettingsGroupID").val() != paymentgroup)) {
            $("#paymentSettingsGroupID").val($("#hidPaymentSettingsGroupID").val());
            updatehiddenpaymentGroupId();
            showPaymentForm('L');
        }
        $("#AjaxdivPaymentSection").show();
        $("#divCompleteOrdertButton").hide();
        if ($("#paymentSettingsGroupID").val() > 0) {
            if ($("#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();
        }
        $("#AjaxdivPaymentSection").hide();
        $("#divCompleteOrdertButton").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 state = $('#billingStateID').val();
            listStates(countryID, state);
        }
    } else {
        $("#editLinkID").hide();
        if ($("#paymentSettingsGroupID").val() > 0) {
            //alert('temp');
            $("#paymentSettingsGroupID").val(0);
        }
    }
    if ($("#paymentSettingsGroupID").val() > 0) {
        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() != 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);
        }
    } 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 listStates(countryID, stateID) {
    $('#billingState').empty();
	$("#stateProvinceInput").hide();
	$('#billingCountryID').val(countryID);
    if (countryID) {
        if ($("#hid_err_formProductPayment_billingProvince").val() == 1) {
            $("#hid_err_formProductPayment_billingProvince").val(0);
        }
    } else {
        stateID = '';
        $("#billingProvince").val('');
    }
    $('#billingStateID').val(stateID);
    if(countryID!=''){
		var data={
			"countryID": countryID , 
			"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) {
				$("#loadProcessImage").empty();
				if (msg != '') {
					$("#stateInput").show();
					$('#billingState').empty();
					$("#stateProvinceInput").hide();
					$("#billingState").append('<option value="">' + selecttext + '</option>' + msg);
					$("#stateSelectedType").val("state");
					$("#billingProvince").val('');
				} else {
					$('#billingState').empty();
					$("#stateInput").hide();
					$("#stateProvinceInput").show();
					if (stateID == 0) {
						stateID = '';
					}
					$("#billingProvince").val(stateID);
					$("#stateSelectedType").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 == 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 getStateHiddenValue(val) {
    //Hide Offline Payment Desc
    if ($("#paymentSettingsGroupID").val() == '') {
        $(".AjaxOfflinePaymentDesc").html('');
    }
    $('#billingStateID').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);
}