﻿
function BillingInfoValidate() {
    //debug
    //return true;
		if (location.host=='www.rejuv-cosmetics.com') {
			if (document.getElementById('Iaccept').checked!=true) {
			alert('You must accept our terms and conditions.');
			return false;
			}
		}
	
    clientID = "customerInfoDv";

    shippingFirstName = GetValue(clientID + '_shippingFirstName');
    shippingLastName = GetValue(clientID + '_shippingLastName');
    shippingAddress = GetValue(clientID + '_shippingAddress');
    shippingCity = GetValue(clientID + '_shippingCity');
    shippingZip = GetValue(clientID + '_shippingZip');
    shippingState = GetValue(clientID + '_shippingState');
    shippingCountry = GetValue(clientID + '_shippingCountry');
    shippingEmail = GetValue(clientID + '_shippingEmail');
    shippingPhone = GetValue(clientID + '_shippingPhone');

    ccNumber = GetValue(clientID + '_ccNumber');
    ccCvv = GetValue(clientID + '_ccCvv');
    ccType = GetValue(clientID + '_ccType');
    ccExpYear = GetValue(clientID + '_ccExpYear');
    ccExpMonth = GetValue(clientID + '_ccExpMonth');

    routingNumber = GetValue(clientID + '_routingNumber');
    accountNumber = GetValue(clientID + '_accountNumber');
    checkNumber = GetValue(clientID + '_checkNumber');

    CardOwnerFirstName = GetValue(clientID + '_CardOwnerFirstName');
    CardOwnerLastName = GetValue(clientID + '_CardOwnerLastName');
    CardOwnerAddress = GetValue(clientID + '_CardOwnerAddress');
    CardOwnerState = GetValue(clientID + '_CardOwnerState');
    CardOwnerCountry = GetValue(clientID + '_CardOwnerCountry');

    if (CardOwnerCountry == -1) {
        alert("Card Owner Country is not selected");
        return false;
    }

    if (CardOwnerState == 0 || CardOwnerState == "") {
        alert("Card Owner State is not selected");
        return false;
    }

    CardOwnerCity = GetValue(clientID + '_CardOwnerCity');
    CardOwnerZip = GetValue(clientID + '_CardOwnerZip');
    CardOwnerEmail = GetValue(clientID + '_CardOwnerEmail');
    CardOwnerVerifyEmail = GetValue(clientID + '_CardOwnerVerifyEmail');
    CardOwnerPhone = GetValue(clientID + '_CardOwnerPhone');



    var ccCheckResult = CheckCreditCard(ccType, ccNumber, ccCvv, ccExpMonth, ccExpYear, routingNumber, accountNumber, checkNumber);
    if (ccCheckResult == false) return false;
    

    if (CardOwnerFirstName.replace(/^ */, "") == "") {
        alert("Card Owner First name field is empty");
        return false;
    }

    if (CardOwnerLastName.replace(/^ */, "") == "") {
        alert("Card Owner Last name field is empty");
        return false;
    }
    if (CardOwnerEmail.replace(/^ */, "") == "") {
        alert("E-mail field is empty");
        return false;
    }
    if (CardOwnerVerifyEmail.replace(/^ */, "") == "") {
        alert("Verify E-mail field is empty");
        return false;
    }
    if (CardOwnerEmail.indexOf("@") < 0) {
        alert("Please enter a valid E-mail address");
        return false;
    }
    if ((CardOwnerVerifyEmail) != (CardOwnerEmail)) {
        alert("E-mail field is diffrent then verify E-mail field");
        return false;
    }
    if (CardOwnerAddress.replace(/^ */, "") == "") {
        alert("Card Owner Address field is empty");
        return false;
    }
    //if  (document.form.b_address.value.toLowerCase().indexOf("box") >=0 ) {
    //         alert("we do not ship to p.o boxes");
    //        return false;
    //}

    CardOwnerPhone = CardOwnerPhone.replace(/[^0-9]+/g, '');

    if (CardOwnerPhone.replace(/^ */, "") == "") {
        alert("Billing phone field is empty");
        return false;
    }
    if (CardOwnerZip.replace(/^ */, "") == "") {
        alert("Card Owner Zip field is empty");
        return false;
    }
    if (CardOwnerCity.replace(/^ */, "") == "") {
        alert("Card Owner City field is empty");
        return false;
    }

    return true;

}


function CheckShippingFields() {


    if (shippingFirstName.replace(/^ */, "") == "") {
        alert("Shipping First name field is empty");
        return false;
    }
    if (shippingLastName.replace(/^ */, "") == "") {
        alert("Shipping Last name field is empty");
        return false;
    }
    if (shippingEmail.replace(/^ */, "") == "") {
        alert("E-mail field is empty");
        return false;
    }
    if (shippingEmail.indexOf("@") < 0) {
        alert("Please enter a valid E-mail address");
        return false;
    }
    //document.form.s_phone.value=document.form.b_phone.value.replace(/[^0-9]+/g,'');

    if (shippingPhone.replace(/^ */, "") == "") {
        alert("Shipping phone field is empty");
        return false;
    }
    if (shippingAddress.replace(/^ */, "") == "") {
        alert("Shipping Address field is empty");
        return false;
    }


    //if  (document.form.s_address.value.toLowerCase().indexOf("box") >= 0) {
    //        alert("we do not ship to p.o boxes");
    //        return false;
    //}

    if (shippingCity.value.replace(/^ */, "") == "") {
        alert("Shipping City field is empty");
        return false;
    }
    if (shippingZip.value.replace(/^ */, "") == "") {
        alert("Shipping Zip field is empty");
        return false;
    }
    if (shippingState.selectedIndex == 0) {
        alert("Shipping State is not selected");
        return false;
    }
    if (shippingCountry.selectedIndex == 0) {
        alert("Shipping Country is not selected");
        return false;
    }
    if ((shippingState.selectedIndex > 1 && document.form.s_country.selectedIndex != 1 && document.form.s_country.selectedIndex != 37 && document.form.s_country.selectedIndex != 224) || (document.form.s_state.selectedIndex == 1 && (document.form.s_country.selectedIndex == 1 || document.form.s_country.selectedIndex == 37 || document.form.s_country.selectedIndex == 224))) {
        alert("Please check country/state.");
        return false;
    }
}


//credit card checks
function CheckCreditCard(ccType, ccNumber, ccCvv, ccExpMonth, ccExpYear, routingNumber, accountNumber, checkNumber) {


    var isDMSTransaction = IsDMSTransaction(ccType);
    if (isDMSTransaction) return true;
 
 //check if the first char of the credit card number is 4 when Visa card selected
    var first_number = ccNumber.charAt(0);
   
    if (ccType != 'ECheck' && ccNumber.replace(/^ */, "") == "") {
        alert("Card number field is empty");
        return false;
    }


    if (ccType == 'Visa' && ccNumber.length != 16) {
        alert("Card number must be 16 characters");
        return false;
    }

    if (ccType == 'AMEX' && ccNumber.length != 15) {
        alert("Card number must be 15 characters");
        return false;
    }


    if ((ccType == 'VISA') && (first_number != 4)) {
        alert("Please enter a valid VISA card number.");
        return false;
    }

    if ((ccType == 'AMEX') && (first_number != 3)) {
        alert("Please enter a valid AMEX card number.");
        return false;
    }


    if (ccType != 'ECheck' && ccCvv.replace(/^ */, "") == "") {
        alert("CVV2/CVC field is empty");
        return false;
    }


    if (ccType == 'Visa' && ccCvv.length != 3) {
        alert("CVV2/CVC must be 3 characters");
        return false;
    }

    if (ccType == 'AMEX' && ccCvv.length != 4) {
        alert("CVV2/CVC must be 4 characters");
        return false;
    }


    //     if((CardOwnerFirstName)&&((ccNumber.substring(ccNumber.length-3,ccNumber.length)==ccCvv) || !(ccCvv>0 && ccCvv<9999))){
    //	     alert("CVC2/CVV field is not correct. Please click 'What is this' button to verify what CVV/CVC2 is.");
    //         return false;
    //     }
    if (ccType == -1) {
        alert("Credit card type is not selected");
        return false;
    }
    if (ccExpMonth == 0) {
        alert("Expiration month is not selected");
        return false;
    }


    if (ccExpYear == 0) {
        alert("Expiration year is not selected");
        return false;
    }

   
    if (ccType == 'ECheck' && routingNumber.length != 9) {
        alert("Routing Number must be 9 characters");
        return false;
    }

    if (ccType == 'ECheck' && accountNumber.length < 6) {
        alert("Account Number must be at least 6 characters");
        return false;
    }

    if (ccType == 'ECheck' && checkNumber.length < 1) {
        alert("Check Number must be at least 1 character");
        return false;
    }


    //проверяем номер с помощью валидатора кредиток
    fileName = 'CreditCardServerValidate.ashx';
    paramValues = 'ccType=' + ccType + '&ccNumber=' + ccNumber + '&CardOwnerEmail=' + CardOwnerEmail + '&ccExpMonth=' + ccExpMonth + '&ccExpYear=' + ccExpYear;

    result = CallbackRequest(fileName, paramValues);

    if (result != 'ok') {
        alert(result);
        return false;
    }


}



function IsDMSTransaction(ccType) {
    if (location.host.indexOf('deluxepharm')>-1 && ccType == 'VISA') {
        return true;
    }
    else {
        return false;
    }
} 
