// General function for pop-up window
var newWindow = null
	function makeNewWindow(x,y) {
		newWindow =  window.open(x,y,"status=1,menubar=1,toolbar=0,scrollbars=1,resizable=0,width=600,height=600")
		newWindow.focus()
}

// General function that returns false if "fieldname" is empty.
function checkfield(fieldname) {
	var fieldval = (fieldname.value.length == 0) ? false: true;
	return fieldval;
}

// General function that returns false if "Phone" contains "x".
function checkphonefield(fieldname) {
	var phonefieldval = (fieldname.value.indexOf("x",0) > -1) ? false: true;
	return phonefieldval;
}

// General function that returns false if "Fax" contains "x".
function checkfaxfield(fieldname) {
	var faxfieldval = (fieldname.value.indexOf("x",0) > -1) ? false: true;
	return faxfieldval;
}

// Function that checks for valid E-mail. Returns false if not valid.
function checkemail(fieldname) {
	if (!checkfield(fieldname)) {
		return false;
	}

	invalidchars = " /:,;"
	for (count = 0; count < invalidchars.length; count++) {
		badchar = invalidchars.charAt(count);
		if (fieldname.value.indexOf(badchar,0) > -1) {
			return false;
		}
	}

	atpos = fieldname.value.indexOf("@", 1);
	if (atpos == -1) {
		return false;
	}
	if (fieldname.value.indexOf("@", atpos + 1) > -1) {
		return false;
	}

	periodpos = fieldname.value.indexOf(".", atpos);
	if (periodpos == -1) {
		return false;
	}
	if (periodpos + 3 > fieldname.value.length) {
		return false;
	}
	return true;
}

// Function that checks for checked item from basket. Returns false if not valid.
function checkProductID(ProductID) { 
	var ckbx_arr=document.getElementsByName('ProductID');
	var ckbx_arr_ln=ckbx_arr.length;
	for(var i=0;i<ckbx_arr_ln;i++) 
	{
	if(ckbx_arr[i].checked)
		return true;
	}
	return false; 
}

// This checks the basket for checked item for removal from basket by Product ID. It calls the various functions to check for validity.
function checkbasket(f) {
	var ProductIDval = checkProductID(f.ProductID);
	
	var formvalid = true;
	var focusfield = "";
	var errormsg = "The following errors were found when attempting to remove an item from your shopping cart:\n\n";
	
	if (!ProductIDval) {
		formvalid = false;
		errormsg = errormsg + '  - You need to check the box that corresponds to the item that you wish to remove.\n';
		focusfield = f.remove;
	}
	
	if ( formvalid ) {
		return true;
	} else {
		alert(errormsg);
		focusfield.focus();
		return false;
	}
}

// Function that checks if other billing province or state. Returns false if not valid.
function checkbillstateother(BillState, BillStateOther) {
	if ( BillState.options[BillState.selectedIndex].value == "Other") {
		if ( checkfield(BillStateOther) == false ) {
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
	return true;
}

// Function that checks for valid billing province or state. Returns false if not valid.
function checkbillstate(BillState) {
	if ( BillState.options[BillState.selectedIndex].value == "") {
	return false;
    } else {
        return true;
    }
}

// Function that checks if other shipping province or state. Returns false if not valid.
function checkshipstateother(ShipState, ShipStateOther) {
	if ( ShipState.options[ShipState.selectedIndex].value == "Other") {
		if ( checkfield(ShipStateOther) == false ) {
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
	return true;
}

// Function that checks for valid shipping province or state. Returns false if not valid.
function checkshipstate(ShipState) {
	if ( ShipState.options[ShipState.selectedIndex].value == "") {
	return false;
    } else {
        return true;
    }
}

// Function that checks for valid credit card type. Returns false if not valid.
function checkCardTypeList(CardTypeList) {
	if ( CardTypeList.options[CardTypeList.selectedIndex].value == "") {
	return false;
    } else {
        return true;
    }
}

function checkMod10(CCNumber) {  // v2.0
var valid = "0123456789"  // Valid digits in a credit card number
var len = CCNumber.value.length;  // The length of the submitted cc number
var iCCN = parseInt(CCNumber);  // integer of CCNumber
var sCCN = CCNumber.value.toString();  // string of CCNumber
sCCN = sCCN.replace (/^\s+|\s+$/g,'');  // strip spaces
var iTotal = 0;  // integer total set at zero
var bNum = true;  // by default assume it is a number
var bResult = false;  // by default assume it is NOT a valid cc
var temp;  // temp variable for parsing string
var calc;  // used for calculation of each digit

// Determine if the CCNumber is in fact all numbers
for (var j=0; j<len; j++) {
  temp = "" + sCCN.substring(j, j+1);
  if (valid.indexOf(temp) == "-1"){bNum = false;}
}

// if it is NOT a number, you can either alert to the fact, or just pass a failure
if(!bNum){
  /*alert("Not a Number");*/bResult = false;
}

// Determine if it is the proper length 
if((len == 0)&&(bResult)){  // nothing, field is blank AND passed above # check
  bResult = false;
} else{  // CCNumber is a number and the proper length - let's see if it is a valid card number
  if(len >= 15){  // 15 or 16 for Amex or V/MC
    for(var i=len;i>0;i--){  // LOOP throught the digits of the card
      calc = parseInt(iCCN) % 10;  // right most digit
      calc = parseInt(calc);  // assure it is an integer
      iTotal += calc;  // running total of the card number as we loop - Do Nothing to first digit
      i--;  // decrement the count - move to the next digit in the card
      iCCN = iCCN / 10;                               // subtracts right most digit from CCNumber
      calc = parseInt(iCCN) % 10 ;    // NEXT right most digit
      calc = calc *2;                                 // multiply the digit by two
      // Instead of some screwy method of converting 16 to a string and then parsing 1 and 6 and then adding them to make 7,
      // I use a simple switch statement to change the value of calc2 to 7 if 16 is the multiple.
      switch(calc){
        case 10: calc = 1; break;       //5*2=10 & 1+0 = 1
        case 12: calc = 3; break;       //6*2=12 & 1+2 = 3
        case 14: calc = 5; break;       //7*2=14 & 1+4 = 5
        case 16: calc = 7; break;       //8*2=16 & 1+6 = 7
        case 18: calc = 9; break;       //9*2=18 & 1+8 = 9
        default: calc = calc;           //4*2= 8 &   8 = 8  -same for all lower numbers
      }                                               
    iCCN = iCCN / 10;  // subtracts right most digit from ccNum
    iTotal += calc;  // running total of the card number as we loop
  }  // END OF LOOP
  if ((iTotal%10)==0){  // check to see if the sum Mod 10 is zero
    bResult = true;  // This IS (or could be) a valid credit card number.
  } else {
    bResult = false;  // This could NOT be a valid credit card number
    }
  }
}
// change alert to on-page display or other indication as needed.
if (bResult = false)
	{
        return false;
    } else {
        return true;
    }
}

// Function that checks for valid credit card expiration month. Returns false if not valid.
function checkCCExpMonth(CCExpMonth) {
	if ( CCExpMonth.options[CCExpMonth.selectedIndex].value == "") {
	return false;
    } else {
        return true;
    }
}

// Function that checks for valid credit card expiration year. Returns false if not valid.
function checkCCExpYear(CCExpYear) {
	if ( CCExpYear.options[CCExpYear.selectedIndex].value == "") {
	return false;
    } else {
        return true;
    }
}

// Function that checks for checked order agreement. Returns false if not valid.
function checkOrderAgreement(ConfirmOrderAgreement) { 
  if (ConfirmOrderAgreement.checked != true)    {
	return false;
    } else {
        return true;
    }
}

// This checks the order checkout form. It calls the various functions to check for validity.
function checkorderform(f) {
	var BillFirstval = checkfield(f.BillFirst);
	var BillLastval = checkfield(f.BillLast);
	var BillAddress1val = checkfield(f.BillAddress1);
	var BillCityval = checkfield(f.BillCity);
	var BillStateOtherval = checkbillstateother(f.BillState, f.BillStateOther);
	var BillStateval = checkbillstate(f.BillState);
	var BillZipval = checkfield(f.BillZip);
	var BillPhoneval = checkphonefield(f.BillPhone);
	var BillEmailval = checkemail(f.BillEmail);
	var ShipFirstval = checkfield(f.ShipFirst);
	var ShipLastval = checkfield(f.ShipLast);
	var ShipAddress1val = checkfield(f.ShipAddress1);
	var ShipCityval = checkfield(f.ShipCity);
	var ShipStateOtherval = checkshipstateother(f.ShipState, f.ShipStateOther);
	var ShipStateval = checkshipstate(f.ShipState);
	var ShipZipval = checkfield(f.ShipZip);
	var ShipPhoneval = checkphonefield(f.ShipPhone);
	var ShipEmailval = checkemail(f.ShipEmail);
	var CardTypeListval = checkCardTypeList(f.CardTypeList);
	var CCNumberval = checkfield(f.CCNumber);
	var Mod10val = checkMod10(f.CCNumber);
	var CCExpMonthval = checkCCExpMonth(f.CCExpMonth);
	var CCExpYearval = checkCCExpYear(f.CCExpYear);
	var OrderAgreementval = checkOrderAgreement(f.ConfirmOrderAgreement);
	
	var formvalid = true;
	var focusfield = "";
	var errormsg = "The following errors were found when attempting to submit your payment form:\n\n";
	
	if (!OrderAgreementval) {
		formvalid = false;
		errormsg = errormsg + '  - You must check the box to confirm that you have read and agreed to the Order Agreement.\n';
		focusfield = f.ConfirmOrderAgreement;
	}

	if (!CCExpYearval) {
		formvalid = false;
		errormsg = errormsg + '  - Credit Card Number Expiration Year must be selected.\n';
		focusfield = f.CCExpYear;
	}

	if (!CCExpMonthval) {
		formvalid = false;
		errormsg = errormsg + '  - Credit Card Number Expiration Month must be selected.\n';
		focusfield = f.CCExpMonth;
	}

	if (!Mod10val) {
		formvalid = false;
		errormsg = errormsg + '  - Credit Card Number must be valid.\n';
		focusfield = f.CCNumber;
	}

	if (!CCNumberval) {
		formvalid = false;
		errormsg = errormsg + '  - Credit Card Number must be filled in.\n';
		focusfield = f.CCNumber;
	}

	if (!CardTypeListval) {
		formvalid = false;
		errormsg = errormsg + '  - Credit Card Type must be selected.\n';
		focusfield = f.CardTypeList;
	}
	
	if (!ShipEmailval) {
		formvalid = false;
		errormsg = errormsg + '  - Shipping E-mail Address must be filled in.\n';
		focusfield = f.ShipEmail;
	}

	if (!ShipPhoneval) {
		formvalid = false;
		errormsg = errormsg + '  - Shipping Phone Number must be filled in.\n';
		focusfield = f.ShipPhone;
	}

	if (!ShipZipval) {
		formvalid = false;
		errormsg = errormsg + '  - Shipping Postal or Zip Code must be filled in.\n';
		focusfield = f.ShipZip;
	}

	if (!ShipStateval) {
		formvalid = false;
		errormsg = errormsg + '  - Shipping Province or State must be selected.\n';
		focusfield = f.ShipState;
	}
	
	if (!ShipStateOtherval) {
		formvalid = false;
		errormsg = errormsg + '  - Other Shipping Province or State must be filled in if other.\n';
		focusfield = f.ShipStateOther;
	}
	
	if (!ShipCityval) {
		formvalid = false;
		errormsg = errormsg + '  - Shipping City must be filled in.\n';
		focusfield = f.ShipCity;
	}

	if (!ShipAddress1val) {
		formvalid = false;
		errormsg = errormsg + '  - Shipping Street Address must be filled in.\n';
		focusfield = f.ShipAddress1;
	}

	if (!ShipLastval) {
		formvalid = false;
		errormsg = errormsg + '  - Shipping Last Name must be filled in.\n';
		focusfield = f.ShipLast;
	}

	if (!ShipFirstval) {
		formvalid = false;
		errormsg = errormsg + '  - Shipping First Name must be filled in.\n';
		focusfield = f.ShipFirst;
	}
	
	if (!BillEmailval) {
		formvalid = false;
		errormsg = errormsg + '  - Billing E-mail Address must be filled in.\n';
		focusfield = f.BillEmail;
	}
	
	if (!BillPhoneval) {
		formvalid = false;
		errormsg = errormsg + '  - Billing Phone Number must be filled in.\n';
		focusfield = f.BillPhone;
	}

	if (!BillZipval) {
		formvalid = false;
		errormsg = errormsg + '  - Billing Postal or Zip Code must be filled in.\n';
		focusfield = f.BillZip;
	}

	if (!BillStateval) {
		formvalid = false;
		errormsg = errormsg + '  - Billing Province or State must be selected.\n';
		focusfield = f.BillState;
	}
	
	if (!BillStateOtherval) {
		formvalid = false;
		errormsg = errormsg + '  - Other Billing Province or State must be filled in if other.\n';
		focusfield = f.BillStateOther;
	}
	
	if (!BillCityval) {
		formvalid = false;
		errormsg = errormsg + '  - Billing City must be filled in.\n';
		focusfield = f.BillCity;
	}

	if (!BillAddress1val) {
		formvalid = false;
		errormsg = errormsg + '  - Billing Street Address must be filled in.\n';
		focusfield = f.BillAddress1;
	}

	if (!BillLastval) {
		formvalid = false;
		errormsg = errormsg + '  - Billing Last Name must be filled in.\n';
		focusfield = f.BillLast;
	}

	if (!BillFirstval) {
		formvalid = false;
		errormsg = errormsg + '  - Billing First Name must be filled in.\n';
		focusfield = f.BillFirst;
	}
	
	if ( formvalid ) {
		return true;
	} else {
		alert(errormsg);
		focusfield.focus();
		return false;
	}
}

var ShipFirst = "";
var ShipLast = "";
var ShipAddress1 = "";
var ShipAddress2 = "";
var ShipCity = "";
var ShipState = "";
var ShipStateIndex = 0;
var ShipStateOther = "";
var ShipCountryIndex = 0;
var ShipZip = "";
var ShipPhone = "";
var ShipEmail = "";
var ShipConfirm = 0;

function InitSaveVariables(form) {
ShipFirst = form.ShipFirst.value;
ShipLast = form.ShipLast.value;
ShipAddress1 = form.ShipAddress1.value;
ShipAddress2 = form.ShipAddress2.value;
ShipCity = form.ShipCity.value;
ShipZip = form.ShipZip.value;
ShipStateIndex = form.ShipState.selectedIndex;
ShipState = form.ShipState[ShipStateIndex].value;
ShipStateOther = form.ShipStateOther.value;
ShipCountry = form.ShipState[ShipCountryIndex].value;
ShipPhone = form.ShipPhone.value;
ShipEmail = form.ShipEmail.value;
ShipConfirm = form.ShipConfirm.checked;
}

function ShipToBillPerson(form) {
if (form.copy.checked) {
InitSaveVariables(form);
form.ShipFirst.value = form.BillFirst.value;
form.ShipLast.value = form.BillLast.value;
form.ShipAddress1.value = form.BillAddress1.value;
form.ShipAddress2.value = form.BillAddress2.value;
form.ShipCity.value = form.BillCity.value;
form.ShipZip.value = form.BillZip.value;
form.ShipState.selectedIndex = form.BillState.selectedIndex;
form.ShipStateOther.value = form.BillStateOther.value;
form.ShipCountry.selectedIndex = form.BillCountry.selectedIndex;
form.ShipPhone.value = form.BillPhone.value;
form.ShipEmail.value = form.BillEmail.value;
form.ShipConfirm.checked = form.BillConfirm.checked;
}
else {
form.ShipFirst.value = ShipFirst;
form.ShipLast.value = ShipLast;
form.ShipAddress1.value = ShipAddress1;
form.ShipAddress2.value = ShipAddress2;
form.ShipCity.value = ShipCity;
form.ShipZip.value = ShipZip;
form.ShipState.selectedIndex = ShipStateIndex;
form.ShipStateOther.value = ShipStateOther;
form.ShipCountry.selectedIndex = ShipCountryIndex;
form.ShipPhone.value = ShipPhone;
form.ShipEmail.value = ShipEmail;
form.ShipConfirm.checked = ShipConfirm;
   }
}
