/* Special purpose routine to check the fields that are supposed to be filled have been filled
   or else return back to the form. */
function contactForm(theForm) {
  allCorrect=true ;
  
  if (theForm.FirstName.value=="" || 
      theForm.LastName.value=="" ||
	  theForm.Phone.value=="" ||
	  theForm.Email.value=="" ) {
    allCorrect=false ;
  }
  if (allCorrect==false)
  {
    alert("Please fill in all fields marked *") ;
	return false ;
  } 
    return true ;
}

function checkCCFormEsec(theForm) {
  if (theForm.EPS_CARDNUMBER.value=="testsuccess" || theForm.EPS_CARDNUMBER.value=="testfailure") {
	return true;
  }	
  var allCorrect=true ;
  var theDate = new Date();
  
  if (theForm.EPS_CARDNUMBER.value=="" ||
	  theForm.EPS_CCV.value=="" ||
	  theForm.EPS_CARDTYPE.selectedIndex==0 ||
	  theForm.EPS_EXPIRYMONTH.selectedIndex==0 ||
	  theForm.EPS_EXPIRYYEAR.selectedIndex==0 ||
	  theForm.EPS_NAMEONCARD.value=="" ) {
    alert("Please fill in all fields.") ;
	return false ;
  }

  if (theForm.EPS_EXPIRYMONTH.selectedIndex==0 ||
	  theForm.EPS_EXPIRYYEAR.selectedIndex==0) {
    alert("Please fill in an expiry date.") ;
	return false ;
  }
  else if (theForm.EPS_EXPIRYMONTH.options[theForm.EPS_EXPIRYMONTH.selectedIndex].value < theDate.getMonth() &&
	  theForm.EPS_EXPIRYYEAR.options[theForm.EPS_EXPIRYYEAR.selectedIndex].value == theDate.getYear()) {
    alert("Please fill in a valid expiry date.") ;
	return false ;
  }
  else if (theForm.EPS_EXPIRYYEAR.options[theForm.EPS_EXPIRYYEAR.selectedIndex].value < theDate.getYear()) {
    alert("Please fill in a valid expiry date.") ;
	return false ;
  }
 
  if (!checkCC(theForm.EPS_CARDNUMBER, theForm.EPS_CARDTYPE)) {
	//alert("Your credit card number is invalid, please try again.") ;
	return false ;
  }
 
  //return true ;
}

function checkCCFormSSL(theForm) {
  var allCorrect=true ;
  var theDate = new Date();
  
  if (theForm.CCNumber.value=="" ||
	  theForm.CCVNNumber.value=="" ||
	  theForm.CCType.selectedIndex==0 ||
	  theForm.CCExpiryMonth.selectedIndex==0 ||
	  theForm.CCExpiryYear.selectedIndex==0 ||
	  theForm.CCName.value=="" ||
      theForm.CCAddress.value=="" ||
      theForm.CCCity.value=="" ||
      theForm.CCState.value=="" ||
      theForm.CCPostcode.value=="" ||
      theForm.CCCountry.value=="" ||
	  theForm.CCTelephone.value=="" ||
	  theForm.CCEmail.value=="" ) {
    alert("Please fill in all fields.") ;
	return false ;
  }

  if (theForm.CCExpiryMonth.selectedIndex==0 ||
	  theForm.CCExpiryYear.selectedIndex==0) {
    alert("Please fill in an expiry date.") ;
	return false ;
  }
  else if (theForm.CCExpiryMonth.options[theForm.CCExpiryMonth.selectedIndex].value < theDate.getMonth() &&
	  theForm.CCExpiryYear.options[theForm.CCExpiryYear.selectedIndex].value == theDate.getYear()) {
    alert("Please fill in a valid expiry date.") ;
	return false ;
  }
  else if (theForm.CCExpiryYear.options[theForm.CCExpiryYear.selectedIndex].value < theDate.getYear()) {
    alert("Please fill in a valid expiry date.") ;
	return false ;
  }
 
  if (!checkCC(theForm.CCNumber, theForm.CCType)) {
	//alert("Your credit card number is invalid, please try again.") ;
	return false ;
  }
 
  //return true ;
}

/* CHECK CC NUMBER */
function checkCC(cardNumber, cardTypeDropDown) {
	var cclength = "";
	var ccprefix = "";
	var allNumeric=true;
	for (i=0; i<cardNumber.value.length; i++)
	{
		if (cardNumber.value.charAt(i) == " ") {
			cardNumber.value = cardNumber.value.replace(" ", "");
			i = i-1;
		}
	}
	var ccNumber = cardNumber.value;
	//alert("cardNumber.value " + cardNumber.value);
	//alert("ccNumber " + ccNumber);
	var cardType = cardTypeDropDown.options[cardTypeDropDown.selectedIndex].value;
	
	if (ccNumber.length==0) {
		return true;
	}
	else 
	{
		if (cardType == '') {
			alert('Please select a credit card type.');
			return false ;
		}
	
		switch (cardType.toLowerCase()) {
			case "visa":
				cclength = "13;16";
				ccprefix = "4";
				break;
			case "mastercard":
				cclength = "16";
				ccprefix = "51;52;53;54;55";
				break;
			case "bankcard":
				cclength="16";
				ccprefix="5";
				break;
			case "amex":
				cclength = "15";
				ccprefix = "34;37";
				break;
			case "dinersclub":
				cclength = "14";
				ccprefix = "300;301;302;303;304;305;36;38";
				break;
		}
		//alert("ccprefix " + ccprefix);
		arrPrefixes = ccprefix.split(";");
		arrLengths = cclength.split(";");
		var prefixValid = false;
		var lengthValid = false;
		for (i=0;i<arrPrefixes.length;i++) {
			//alert("ccNumber " + ccNumber.indexOf(arrPrefixes[i]));
			//alert("arrPrefixes " + arrPrefixes[i]);
			//alert("ccNumber.substring(arrPrefixes[i]) " + ccNumber.indexOf(arrPrefixes[i]));
			if (ccNumber.indexOf(arrPrefixes[i]) == 0)
				prefixValid = true;
		}
		for (i=0;i<arrLengths.length;i++) {
			if (ccNumber.length == arrLengths[i]) 
				lengthValid = true;
		}
		
		if (!prefixValid)
		{
			alert('The credit card number is invalid. Please check the number and try again.');
			//numberField.value = '' ;
			cardNumber.focus() ;
			return false ;
		}
		if (!lengthValid)
		{
			alert('The credit card number is invalid. Please check the number and try again');
			//numberField.value = '' ;
			cardNumber.focus() ;
			return false ;
		}
		return true;
	}
}

