// 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 student province or state. Returns false if not valid.
function checkstudentstateother(StudentState, StudentStateOther) {
	if ( StudentState.options[StudentState.selectedIndex].value == "Other") {
		if ( checkfield(StudentStateOther) == false ) {
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
	return true;
}

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

//  General function that returns false if postal code is invalid.
function checkPostalCode(UserPostalCode) {
	if (UserPostalCode.value.length == 6 && UserPostalCode.value.search(/^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$/) != -1) {
		return true;
	} else if (UserPostalCode.value.length == 7 && UserPostalCode.value.search(/^[a-zA-Z]\d[a-zA-Z](-|\s)\d[a-zA-Z]\d$/) != -1) {	
		return true;
	} else {
		return true;
	}
}

// Function that checks for valid student country. Returns false if not valid.
function checkstudentcountry(StudentCountry) {
	if ( StudentCountry.options[StudentCountry.selectedIndex].value == "") {
	return false;
    } else {
        return true;
    }
}

// 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 compares confirmed student e-mail addresses. Returns false if not valid.
function checkstudentemailaddress(StudentEmail1, StudentEmail2) {
	if (StudentEmail1.value != StudentEmail2.value)
	{
        return false;
    } else {
        return true;
    }
}

// Function that checks for course to register. Returns false if not valid.
function checkcoursetoregister(CACourseID) {
	if ( CACourseID.options[CACourseID.selectedIndex].value == "") {
	return false;
    } else {
        return true;
    }
}

// Function that checks for course time. Returns false if not valid.
function checkcoursetime(CACourseTime) {
	if ( CACourseTime.options[CACourseTime.selectedIndex].value == "") {
	return false;
    } else {
        return true;
    }
}

// Function that checks for course start date. Returns false if not valid.
function checkcoursestartdate(CACourseStartDate) {
	if ( CACourseStartDate.options[CACourseStartDate.selectedIndex].value == "") {
	return false;
    } else {
        return true;
    }
}

// Function that checks if evening course time selected for rsd. Returns false if not valid.
function checkcoursetimersd(CACourseID, CACourseTime) {
	if ( CACourseID.options[CACourseID.selectedIndex].value == "2" ) {
		if ( CACourseTime.options[CACourseTime.selectedIndex].value == "Evening (6pm - 10pm)" ) {
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
	return true;
}

// Function that checks if evening course time selected for vfp. Returns false if not valid.
function checkcoursetimevfp(CACourseID, CACourseTime) {
	if ( CACourseID.options[CACourseID.selectedIndex].value == "3" ) {
		if ( CACourseTime.options[CACourseTime.selectedIndex].value == "Evening (6pm - 10pm)" ) {
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
	return true;
}

// Function that checks for valid date in yyyy-mm-dd format. Returns false if not valid.
function checkdate(input){
var validformat=/^\d{4}\-\d{2}\-\d{2}$/ //Basic check for format validity
var returnval=false
	if (!validformat.test(input.value))
		return returnval
	else { //Detailed check for valid date ranges
	var yearfield=input.value.split("-")[0]
	var monthfield=input.value.split("-")[1]
	var dayfield=input.value.split("-")[2]
	var dayobj = new Date(yearfield, monthfield-1, dayfield)
		if ((dayobj.getFullYear()!=yearfield)||(dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield))
			return returnval
		else
		returnval=true
		}
		if (returnval==false) input.select()
	return returnval
}

// Function that checks for course payment option selection. Returns false if not valid.
function checkcoursepaymentoption(CACoursePaymentOption) {
	myOption = -1;
	for (i=0; i<CACoursePaymentOption.length; i++) {
	if (CACoursePaymentOption[i].checked) {
		myOption = i;
		}
	}
	if (myOption == -1) {
	return false;
    } else {
        return true;
    }
}

// Function that checks for registration payment method selection. Returns false if not valid.
function checkregistrationpaymentmethod(PaymentMethod) {
	myOption = -1;
	for (i=0; i<PaymentMethod.length; i++) {
	if (PaymentMethod[i].checked) {
		myOption = i;
		}
	}
	if (myOption == -1) {
	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 domestic student enrolment agreement and contract form. It calls the various functions to check for validity.
function checkenrolmentdomesticform(f) {
	var StudentFirstval = checkfield(f.StudentFirst);
	var StudentLastval = checkfield(f.StudentLast);
	var StudentAddress1val = checkfield(f.StudentAddress1);
	var StudentCityval = checkfield(f.StudentCity);
	var StudentStateval = checkstudentstate(f.StudentState);
	var StudentZipval = checkPostalCode(f.StudentZip);
	var StudentEmail1val = checkemail(f.StudentEmail1);
	var StudentEmail2val = checkemail(f.StudentEmail2);
	var StudentEmailval = checkstudentemailaddress(f.StudentEmail1, f.StudentEmail2);
	var StudentPhoneval = checkphonefield(f.StudentPhone);
	var StudentBirthDateval = checkdate(f.StudentBirthDate);
	var CACourseIDval = checkcoursetoregister(f.CACourseID);
	var CACourseTimeval = checkcoursetime(f.CACourseTime);
	var CACourseStartDateval = checkcoursestartdate(f.CACourseStartDate);
	var CACourseTimeRSDval = checkcoursetimersd(f.CACourseID, f.CACourseTime);
	var CACourseTimeVFPval = checkcoursetimevfp(f.CACourseID, f.CACourseTime);
	var CACoursePaymentval = checkcoursepaymentoption(f.CACoursePaymentOption);
	var PaymentMethodval = checkregistrationpaymentmethod(f.PaymentMethod);
	var OrderAgreementval = checkOrderAgreement(f.ConfirmOrderAgreement);
	
	var formvalid = true;
	var focusfield = "";
	var errormsg = "The following errors were found when attempting to submit your student enrolment agreement and contract form:\n\n";
	
	if (!OrderAgreementval) {
		formvalid = false;
		errormsg = errormsg + '  - You must check the box to confirm that you have read and agreed to the Policies and Conditions.\n';
		focusfield = f.ConfirmOrderAgreement;
	}
	
	if (!PaymentMethodval) {
		formvalid = false;
		errormsg = errormsg + '  - Registration Payment Method must be selected.\n';
		focusfield = f.PaymentMethod[0];
	}
	
	if (!CACoursePaymentval) {
		formvalid = false;
		errormsg = errormsg + '  - Course Payment Option must be selected.\n';
		focusfield = f.CACoursePaymentOption[0];
	}
	
	if (!CACourseTimeVFPval) {
		formvalid = false;
		errormsg = errormsg + '  - Evening time slot is not available for Video & Film Production course.\n';
		focusfield = f.CACourseTime;
	}
	
	if (!CACourseTimeRSDval) {
		formvalid = false;
		errormsg = errormsg + '  - Evening time slot is not available for Recording & Sound Design course.\n';
		focusfield = f.CACourseTime;
	}
	
	if (!CACourseStartDateval) {
		formvalid = false;
		errormsg = errormsg + '  - Course Start Date must be selected.\n';
		focusfield = f.CACourseStartDate;
	}
	
	if (!CACourseTimeval) {
		formvalid = false;
		errormsg = errormsg + '  - Course Time must be selected.\n';
		focusfield = f.CACourseTime;
	}
	
	if (!CACourseIDval) {
		formvalid = false;
		errormsg = errormsg + '  - Course To Register must be selected.\n';
		focusfield = f.CACourseID;
	}
	
	if (!StudentBirthDateval) {
		formvalid = false;
		errormsg = errormsg + '  - Student Date of Birth must be filled in yyyy-mm-dd format.\n';
		focusfield = f.StudentBirthDate;
	}
	
	if (!StudentPhoneval) {
		formvalid = false;
		errormsg = errormsg + '  - Student Phone Number must be filled in.\n';
		focusfield = f.StudentPhone;
	}

	if (!StudentEmailval) {
		formvalid = false;
		errormsg = errormsg + '  - Student E-mail Address must be confirmed properly and match.\n';
		focusfield = f.StudentEmail1;
	}
	
	if (!StudentEmail2val) {
		formvalid = false;
		errormsg = errormsg + '  - Confirmed Student E-mail Address must be filled in.\n';
		focusfield = f.StudentEmail2;
	}
	
	if (!StudentEmail1val) {
		formvalid = false;
		errormsg = errormsg + '  - Student E-mail Address must be filled in.\n';
		focusfield = f.StudentEmail1;
	}

	if (!StudentZipval) {
		formvalid = false;
		errormsg = errormsg + '  - Student Postal Code must be filled in.\n';
		focusfield = f.StudentZip;
	}

	if (!StudentStateval) {
		formvalid = false;
		errormsg = errormsg + '  - Student Province must be selected.\n';
		focusfield = f.StudentState;
	}
	
	if (!StudentCityval) {
		formvalid = false;
		errormsg = errormsg + '  - Student City must be filled in.\n';
		focusfield = f.StudentCity;
	}

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

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

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

// This checks the international student enrolment agreement and contract form. It calls the various functions to check for validity.
function checkenrolmentinternationalform(f) {
	var StudentFirstval = checkfield(f.StudentFirst);
	var StudentLastval = checkfield(f.StudentLast);
	var StudentAddress1val = checkfield(f.StudentAddress1);
	var StudentCityval = checkfield(f.StudentCity);
	var StudentStateval = checkstudentstate(f.StudentState);
	var StudentStateOtherval = checkstudentstateother(f.StudentState, f.StudentStateOther)
	var StudentZipval = checkfield(f.StudentZip);
	var StudentCountryval = checkstudentcountry(f.StudentCountry);
	var StudentEmail1val = checkemail(f.StudentEmail1);
	var StudentEmail2val = checkemail(f.StudentEmail2);
	var StudentEmailval = checkstudentemailaddress(f.StudentEmail1, f.StudentEmail2);
	var StudentPhoneval = checkphonefield(f.StudentPhone);
	var StudentBirthDateval = checkdate(f.StudentBirthDate);
	var CACourseIDval = checkcoursetoregister(f.CACourseID);
	var CACourseTimeval = checkcoursetime(f.CACourseTime);
	var CACourseStartDateval = checkcoursestartdate(f.CACourseStartDate);
	var CACourseTimeRSDval = checkcoursetimersd(f.CACourseID, f.CACourseTime);
	var CACourseTimeVFPval = checkcoursetimevfp(f.CACourseID, f.CACourseTime);
	var EstimatedArrivalDateval = checkdate(f.EstimatedArrivalDate);
	var CACoursePaymentval = checkcoursepaymentoption(f.CACoursePaymentOption);
	var PaymentMethodval = checkregistrationpaymentmethod(f.PaymentMethod);
	var OrderAgreementval = checkOrderAgreement(f.ConfirmOrderAgreement);
	
	var formvalid = true;
	var focusfield = "";
	var errormsg = "The following errors were found when attempting to submit your international student enrolment agreement and contract form:\n\n";
	
	if (!OrderAgreementval) {
		formvalid = false;
		errormsg = errormsg + '  - You must check the box to confirm that you have read and agreed to the Policies and Conditions.\n';
		focusfield = f.ConfirmOrderAgreement;
	}
	
	if (!PaymentMethodval) {
		formvalid = false;
		errormsg = errormsg + '  - Registration Payment Method must be selected.\n';
		focusfield = f.PaymentMethod[0];
	}
	
	if (!CACoursePaymentval) {
		formvalid = false;
		errormsg = errormsg + '  - Course Payment Option must be selected.\n';
		focusfield = f.CACoursePaymentOption[0];
	}
	
	if (!EstimatedArrivalDateval) {
		formvalid = false;
		errormsg = errormsg + '  - Estimated Date of Arrival must be filled in yyyy-mm-dd format.\n';
		focusfield = f.EstimatedArrivalDate;
	}
	
	if (!CACourseTimeVFPval) {
		formvalid = false;
		errormsg = errormsg + '  - Evening time slot is not available for Video & Film Production course.\n';
		focusfield = f.CACourseTime;
	}
	
	if (!CACourseTimeRSDval) {
		formvalid = false;
		errormsg = errormsg + '  - Evening time slot is not available for Recording & Sound Design course.\n';
		focusfield = f.CACourseTime;
	}
	
	if (!CACourseStartDateval) {
		formvalid = false;
		errormsg = errormsg + '  - Course Start Date must be selected.\n';
		focusfield = f.CACourseStartDate;
	}
	
	if (!CACourseTimeval) {
		formvalid = false;
		errormsg = errormsg + '  - Course Time must be selected.\n';
		focusfield = f.CACourseTime;
	}
	
	if (!CACourseIDval) {
		formvalid = false;
		errormsg = errormsg + '  - Course To Register must be selected.\n';
		focusfield = f.CACourseID;
	}
	
	if (!StudentBirthDateval) {
		formvalid = false;
		errormsg = errormsg + '  - Student Date of Birth must be filled in yyyy-mm-dd format.\n';
		focusfield = f.StudentBirthDate;
	}
	
	if (!StudentPhoneval) {
		formvalid = false;
		errormsg = errormsg + '  - Student Phone Number must be filled in.\n';
		focusfield = f.StudentPhone;
	}

	if (!StudentEmailval) {
		formvalid = false;
		errormsg = errormsg + '  - Student E-mail Address must be confirmed properly and match.\n';
		focusfield = f.StudentEmail1;
	}
	
	if (!StudentEmail2val) {
		formvalid = false;
		errormsg = errormsg + '  - Confirmed Student E-mail Address must be filled in.\n';
		focusfield = f.StudentEmail2;
	}
	
	if (!StudentEmail1val) {
		formvalid = false;
		errormsg = errormsg + '  - Student E-mail Address must be filled in.\n';
		focusfield = f.StudentEmail1;
	}

	if (!StudentCountryval) {
		formvalid = false;
		errormsg = errormsg + '  - Student Country must be selected.\n';
		focusfield = f.StudentCountry;
	}

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

	if (!StudentStateval) {
		formvalid = false;
		errormsg = errormsg + '  - Student Province or State must be selected.\n';
		focusfield = f.StudentState;
	}
	
	if (!StudentStateOtherval) {
		formvalid = false;
		errormsg = errormsg + '  - Other Student Province or State must be filled in if other.\n';
		focusfield = f.StudentStateOther;
	}
	
	if (!StudentCityval) {
		formvalid = false;
		errormsg = errormsg + '  - Student City must be filled in.\n';
		focusfield = f.StudentCity;
	}

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

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

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

// 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 dealer 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;
   }
}
