function verifyPaymentForm() {
    /*  Disables Submit and Previous Buttons While Processing */
    document.paymentForm.submit.disabled=true;

    if(document.forms['paymentForm'].elements['payNow']) {

        for(i=0; i<document.paymentForm.payNow.length; i++) {
            if(document.paymentForm.payNow[i].checked) {
                if(document.paymentForm.payNow[i].value=="Y") {
                    if(!verifyCreditCardForm()) {
                        return false;
                    } else {
                        return true;
                    }
                } else {
                    return true;
                }
            }
        }

    } else {

        if(!verifyCreditCardForm()) {
            return false;
        } else {
            return true;
        }
    }
}

function validateBillingAddress() {

    if(document.paymentForm.BillingCountryName.value.length < 1) {
        alert("Please Select Your Country");
        return false;
    }
    if(document.paymentForm.BillingAddress1.value.length < 1) {
        alert("Please Enter Your Billing Address");
        return false;
    }
    if(document.paymentForm.BillingCity.value.length < 1) {
        alert("Please Enter Your Billing City");
        return false;
    }
    if(document.paymentForm.BillingCountryName.value == "USA" || document.paymentForm.BillingCountryName.value == "CAN") {
        if(document.paymentForm.BillingStateProvince.value.length < 1) {
            alert("Please Enter Your Billing State / Province");
            return false;
        }
    }

    if(document.paymentForm.BillingPostalCode.value.length < 1) {
        alert("Please Provide Your Billing Postal Code");
        return false;
    }

    return true;
}

function hideBilling() {

    document.paymentForm.BillingCountryName.selectedIndex=0;
    changeBillingCountry();

    document.paymentForm.BillingCountryName.value="";
    document.paymentForm.BillingAddress1.value="";
    document.paymentForm.BillingAddress2.value="";
    document.paymentForm.BillingAddress3.value="";
    document.paymentForm.BillingCity.value="";
    document.paymentForm.BillingPostalCode.value="";

    document.paymentForm.BillingCountryName.disabled=true;
    document.paymentForm.BillingAddress1.disabled=true;
    document.paymentForm.BillingAddress2.disabled=true;
    document.paymentForm.BillingAddress3.disabled=true;
    document.paymentForm.BillingCity.disabled=true;
    document.paymentForm.BillingPostalCode.disabled=true;

    var thisDiv = getObjectById("billingAddressBlock");
    thisDiv.style.display="none";
}


function showBilling() {

    document.paymentForm.BillingCountryName.disabled=false;
    document.paymentForm.BillingAddress1.disabled=false;
    document.paymentForm.BillingAddress2.disabled=false;
    document.paymentForm.BillingCity.disabled=false;
    document.paymentForm.BillingPostalCode.disabled=false;


    var thisDiv = getObjectById("billingAddressBlock");
    thisDiv.style.display="block";

}

function hideCCBox() {

    document.paymentForm.CC_Type.disabled=true;
    document.paymentForm.CC_Number.disabled=true;
    document.paymentForm.CC_Expire_Month.disabled=true;
    document.paymentForm.CC_Expire_Year.disabled=true;
    document.paymentForm.CC_Name.disabled=true;
    document.paymentForm.CC_Authorization_Number.disabled=true;
    document.paymentForm.CC_Type.style.backgroundColor="#CCCCCC";
    document.paymentForm.CC_Number.style.backgroundColor="#CCCCCC";
    document.paymentForm.CC_Expire_Month.style.backgroundColor="#CCCCCC";
    document.paymentForm.CC_Expire_Year.style.backgroundColor="#CCCCCC";
    document.paymentForm.CC_Name.style.backgroundColor="#CCCCCC";
    document.paymentForm.CC_Authorization_Number.style.backgroundColor="#CCCCCC";

    for(var j=0;j<document.paymentForm.SameBilling.length;j++) {
        if(document.paymentForm.SameBilling[j].value=='Yes') {
            document.paymentForm.SameBilling[j].checked=true;
            hideBilling();
        }
        document.paymentForm.SameBilling[j].disabled = true;
    }

}

function showCCBox() {
    document.paymentForm.CC_Type.disabled=false;
    document.paymentForm.CC_Number.disabled=false;
    document.paymentForm.CC_Expire_Month.disabled=false;
    document.paymentForm.CC_Expire_Year.disabled=false;
    document.paymentForm.CC_Name.disabled=false;
    document.paymentForm.CC_Authorization_Number.disabled=false;
    document.paymentForm.CC_Type.style.backgroundColor="#FFFFFF";
    document.paymentForm.CC_Number.style.backgroundColor="#FFFFFF";
    document.paymentForm.CC_Expire_Month.style.backgroundColor="#FFFFFF";
    document.paymentForm.CC_Expire_Year.style.backgroundColor="#FFFFFF";
    document.paymentForm.CC_Name.style.backgroundColor="#FFFFFF";
    document.paymentForm.CC_Authorization_Number.style.backgroundColor="#FFFFFF";

    for(var j=0;j<document.paymentForm.SameBilling.length;j++) {
        document.paymentForm.SameBilling[j].disabled = false;
    }
}

function changeBillingCountry() {

    if(document.paymentForm.BillingCountryName.value == "USA" || document.paymentForm.BillingCountryName.value == "CAN") {

        var a=document.getElementById('billingAddressTable').rows
        rowExists = false;
        for(i=0; i<a.length;i++) {
            if(a[i].id == 'StateRow') {
                rowExists = true;
                break;
            }
        }
        if(!rowExists) {
            for(i=0; i<a.length;i++) {
                if(a[i].id == 'CityRow') {
                    var x=document.getElementById('billingAddressTable').insertRow(i+1)
                    x.id = "StateRow"
                    var y=x.insertCell(0)
                    var z=x.insertCell(1)
                    y.className="alignRight"
                    y.innerHTML="<b>Billing State/Province</b>"
                    z.innerHTML=billingStateBox
                }
            }
        }
        for(i=0; i<a.length;i++) {
            if(a[i].id == 'Address3Row') {
                document.getElementById('billingAddressTable').deleteRow(i);
                break;
            }
        }

    } else {

        var a=document.getElementById('billingAddressTable').rows
        rowExists = false;
        for(i=0; i<a.length;i++) {
            if(a[i].id == 'Address3Row') {
                rowExists = true;
                break;
            }
        }
        if(!rowExists) {
            for(i=0; i<a.length;i++) {
                if(a[i].id == 'Address2Row') {
                    var x=document.getElementById('billingAddressTable').insertRow(i+1)
                    x.id = "Address3Row"
                    var y=x.insertCell(0)
                    var z=x.insertCell(1)
                    y.align="left"
                    y.innerHTML=""
                    z.align="left"
                    z.innerHTML="<input type=text name=BillingAddress3 size=30 maxlength=30>"
                }
            }
        }
        for(i=0; i<a.length;i++) {
            if(a[i].id == 'StateRow') {
                document.getElementById('billingAddressTable').deleteRow(i);
                break;
            }
        }
    }
}


function verifyCreditCardForm() {

    /*  Disables Submit and Previous Buttons While Processing */
    document.paymentForm.CC_Number.value = document.paymentForm.CC_Number.value.split("-").join("");
    document.paymentForm.CC_Number.value = document.paymentForm.CC_Number.value.split(" ").join("");

    if (Validate(document.paymentForm.CC_Number.value) == false) {
        document.paymentForm.submit.disabled=false;
        return false;
    }

    if(document.paymentForm.CC_Name.value.length < 1) {
        alert("Please Provide The Name On The Credit Card");
        document.paymentForm.submit.disabled=false;
        return false;
    }

    if(!checkDate(document.paymentForm.CC_Expire_Month[document.paymentForm.CC_Expire_Month.selectedIndex].value,document.paymentForm.CC_Expire_Year[document.paymentForm.CC_Expire_Year.selectedIndex].value)) {
        alert("The Expiration Date Has Already Passed");
        document.paymentForm.submit.disabled=false;
        return false;
    }

    if(document.paymentForm.CC_Authorization_Number.value.length < 1) {
        alert("Please Provide the Authorization Number from the back of your Card");
        document.paymentForm.submit.disabled=false;
        return false;
    }

    var sameAddress = true;
    for (i=0;i<document.paymentForm.SameBilling.length;i++){
        if (document.paymentForm.SameBilling[i].checked) {
            if(document.paymentForm.SameBilling[i].value=="No") {
                sameAddress = false;
            }
        }
    }

    if(!sameAddress) {
        if(!validateBillingAddress()) {
            document.paymentForm.submit.disabled=false;
            return false;
        }
    }
    return true;

}

function getVerText(cc) {
    if(cc.value=="AMEX") {
        document.getElementById('verText').innerHTML = "<i>(front of card, 4 digits)</i>";
        document.getElementById('CC_Authorization_Number').setAttribute('maxLength','4');
    } else {
        document.getElementById('verText').innerHTML = "<i>(back of card, last 3 digits)</i>";
        document.getElementById('CC_Authorization_Number').setAttribute('maxLength','3');
    }
}

function displayCCExample() {
    if(document.paymentForm.CC_Type.value == 'AMEX') {
        imagePath = "images/cc_authcode_amex.gif";
    } else {
        imagePath = "images/cc_authcode_mc_visa.gif";
    }
    window.open(imagePath,'ccimagewindow','width=350,height=250,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no');
}


function addCommas(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function sumPayment(cur) {
    var totalPayment = 0.0;
    if(isArray(document.paymentForm.applyPaymentRegId)) {
        for(i = 0; i < document.paymentForm.applyPaymentRegId.length; i++) {
            for(n = 0; n < regBalances.length; n++) {
                if(document.paymentForm.applyPaymentRegId[i].value == regBalances[n][0] && document.paymentForm.applyPaymentRegId[i].checked == true) {
                    totalPayment += regBalances[n][1];
                }
            }
        }
    } else {
       if(document.paymentForm.applyPaymentRegId.value == regBalances[0][0] && document.paymentForm.applyPaymentRegId.checked == true) {
            totalPayment += regBalances[0][1];
        }
    }

    document.getElementById("totalBalanceToPay").innerHTML= cur + addCommas(totalPayment.toFixed(2));
    document.paymentForm.paymentAmount.value=totalPayment;
}

function getVerText(cc) {
    if(cc.value=="AMEX") {
        document.getElementById('verText').innerHTML = "<i>(front of card, 4 digits)</i>";
        document.paymentForm.CC_Authorization_Number.setAttribute('maxLength','4');
    } else {
        document.getElementById('verText').innerHTML = "<i>(back of card, last 3 digits)</i>";
        document.paymentForm.CC_Authorization_Number.setAttribute('maxLength','3');
    }

}

