var oldDom = null;
  
  function swapForm(objectID) {
    dom = findDOM('formAddress', objectID, 1);
	if (oldDom) oldDom.visibility = 'hidden';
	dom.visibility = 'visible';
	oldDom = dom;
	oldObjectID = objectID;
  }
  
  function makeFormVisible(objectID) {
    dom = findDOM('formAddress', objectID, 1);
	dom.visibility = 'visible';
	oldDom = dom;
	oldObjectID = objectID;
  }
  
  function makeFormInvisible(objectID) {
    dom = findDOM('formAddress', objectID, 1);
	dom.visibility = 'hidden';
	oldDom = dom;
	oldObjectID = objectID;
  }

var id4DAccount = false;

function formvalidation(thisform)
{

	with(thisform) {

		if (name == "formItem") {
			if (!quantityValidation(basketquantity)) {
				basketquantity.focus();
				return false;
			}
		}
		else if (name == "formbasket") {
			if (!quantityValidation(basketquantity)) {
				basketquantity.focus();
				return false;
			}
		}
		else if (name == "basketreset") {
			if (value == "Empty Basket") {
				return clearBasket();
			}
		}
		else if (name == "gotocheckout2") {

			try {
				id4DAccount = document.getElementById('chk').value == "account";
			} catch (e) { }

			if (!nameValidation(formcheckout1.cardname)) {
				formcheckout1.cardname.focus();
				return false;
			}
			if (!addressValidation(formcheckout1.cardaddress)) {
				formcheckout1.cardaddress.focus();
				return false;
			}
			if (!postcodeValidation(formcheckout1.cardpostcode)) {
				formcheckout1.cardpostcode.focus();
				return false;
			}
			if (id4DAccount) {
				if (!invoicenameValidation(formcheckout1.invoicename)) {
					formcheckout1.invoicename.focus();
					return false;
				}
			}
			if (!emailValidation(formcheckout1.emailaddress)) {
				formcheckout1.emailaddress.focus();
				return false;
			}
			if (!telephoneValidation(formcheckout1.telephone)) {
				formcheckout1.telephone.focus();
				return false;
			}
			
		}
		else if (name == "resetcheckout") {
			return clearCheckout();
		}
		else if (name == "gotocheckout1") {
  		    if (!issueValidation(formcheckout2.cardissue)) {
  		      formcheckout2.cardissue.focus();
  		      return false;
  		    }
		}
        else if (name == "placeorder") {
        
			try {
        		id4DAccount = document.getElementById('chk').value == "account";
			} catch (e) { }

			if (id4DAccount) {
				if (!securitycodeValidation(formcheckout2.securitycode)) {
					formcheckout2.securitycode.focus();
					return false;
				}
				if (!orderrefValidation(formcheckout2.cardtype)) {
					formcheckout2.cardtype.focus();
					return false;
				}
			}

		    if (!cardtypeValidation(formcheckout2.cardtype)) {
		      formcheckout2.cardtype.focus();
		      return false;
  		    }

  		    if (formcheckout2.cardtype.value != "American") {
  				if (!cardnumbValidation(formcheckout2.cardnumber)) {
  				  formcheckout2.cardnumber.focus();
  				  return false;
  				}
  			}
  		    if (!cardexpiryValidation(formcheckout2.cardexpm, formcheckout2.cardexpy)) {
  		      formcheckout2.cardexpy.focus();
  		      return false;
  		    }
  		    if (!issueValidation(formcheckout2.cardissue)) {
  		      formcheckout2.cardissue.focus();
  		      return false;
  		    }
			if (!sourceValidation(formcheckout2.source)) {
				formcheckout2.source.focus();
				return false;
			}
			
		}
        else if (name == "submitsection" && value == "Purchase Site Promotion") {
			if (!nameValidation(applicationform.cardname)) {
				applicationform.cardname.focus();
				return false;
			}
			if (!addressValidation(applicationform.cardaddress)) {
				applicationform.cardaddress.focus();
				return false;
			}
		    if (!cardtypeValidation(applicationform.cardtype)) {
		      applicationform.cardtype.focus();
		      return false;
  		    }
  		    if (applicationform.cardtype.value != "American") {
  				if (!cardnumbValidation(applicationform.cardnumber)) {
  				  applicationform.cardnumber.focus();
  				  return false;
  				}
  			}
  		    if (!cardexpiryValidation(applicationform.cardexpm, applicationform.cardexpy)) {
  		      applicationform.cardexpy.focus();
  		      return false;
  		    }
  		    if (!issueValidation(formcheckout2.cardissue)) {
  		      applicationform.cardissue.focus();
  		      return false;
  		    }
		}
		else if (name == "submitsection" && value == "Next >>") {
  		    
			if (!emailValidation(applicationform.emailaddress)) {
				applicationform.emailaddress.focus();
				return false;
			}
		}
	}
	return true;
}

function quantityValidation(qty) {

	if (qty.value > 0) {
		if (qty.value.indexOf(".") != -1) {
          alert("Quantity must be a whole number, please re-enter.");
		  return false;
		}
		else {
			if (qty.value > 1000) {
				alert("Quantity cannot exceed 1000, please re-enter.");
				return false;
			}
			else {
				return true;
			}
		}
	}
	else {
		alert("Quantity must be numeric, please re-enter.");
		return false;
	}
}

function issueValidation(qty) {
	try {
		if (qty.value >= 0) {
			if (qty.value.indexOf(".") != -1) {
			alert("Card issue must be a whole number, please re-enter.");
			return false;
			}
			else {
				if (qty.value > 1000) {
					alert("Card issue cannot exceed 1000, please re-enter.");
					return false;
				}
				else {
					return true;
				}
			}
		}
		else {
			alert("Card issue must be numeric, please re-enter.");
			return false;
		}
	} catch (e) {
		return true;
	}

}

function emailValidation(entered)
{
	with (entered)
	{
		atpos=value.indexOf("@"); 
		dotpos=value.lastIndexOf(".");
		lastpos=value.length-1;
		if (atpos<1 || dotpos-atpos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) 
		{
		  alert("Please enter a valid e-mail address before submitting");
		  return false;
		}
		else
		{
		  return true;
		}
	}
}

function expiresValidation(entered)
{
	try {
		if (entered.value.length<1) 
		{
			alert("Please enter an expiry date before submitting");
			return false;
		}
		else
		{
			return true;
		}
	} catch (e) {
		return true;
	}
}

function cardnumbValidation(entered)
{
	try {
		if (!ValidCard(entered.value))
		{
			alert("Please check you have entered your card number correctly");
			return false;
		}
		else
		{
			return true;
		}
	} catch (e) {
		return true;
	}
}

function securitycodeValidation(entered)
{
	try {
		if (entered.value.length<2) 
		{
			alert("Please enter your deadline date before submitting");
			return false;
		}
		else
		{
			return true;
		}
	} catch (e) {
		return true;
	}
}

function orderrefValidation(entered)
{
	try {
		if (entered.value.length<2) 
		{
			alert("Please enter your order reference before submitting");
			return false;
		}
		else
		{
			return true;
		}
	} catch (e) {
		return true;
	}
}

function ValidCard(CreditCard){
// Valid Input Syntax:
// Type1: **************** (16 columns)
// Type2: **** **** **** **** (19)
// Type3: ****-****-****-**** (19)
// Input is a credit card number in the form of a string.
// Validates numbers which use a "double-add-double MOD 10"
// check digit Output is False if not valid, True if valid.

var Validity = false;       // Assume invalid card
var LN = CreditCard.length;	 // Get input value length

  if (CreditCard == "Not Required Yet") {
	return true;
  }

  try {

	if ((16 <= LN) && (LN <= 19)){
		LN --;
		CheckSum = 0;	// start with 0 checksum
		Dbl = false;	// Start with a non-doubling

		//------------------------------------------------------------
		//	Beginning backward loop through string
		//-------------------------------------------------------------

		for (Idx = LN; Idx >= 0; Idx --){
		Digit = CreditCard.substr(Idx, 1);	  // Isolate character
		if (("0" <= Digit && Digit <= "9")
				|| Digit == " " || Digit == "-"){
			if (Digit != " " && Digit != "-"){ // Skip connector
			Digit -= "0";	  // Remove ASCII bias
			if (Dbl){		  // If in the "double-add" phase
				Digit += Digit; // Then double first
				if (Digit > 9){ // Cast nines
				Digit -= 9;
				}
			}
			Dbl = !Dbl; // Flip doubling flag
			CheckSum += Digit;  // Add to running sum
			if (CheckSum > 9){  // Cast tens
				CheckSum -= 10; // Same as MOD 10, but faster
			}
			}
		} else {
			return(Validity);	// Invalid
		}
		}
		Validity = (CheckSum == 0) ? true : false; // Must sum to 0
	}
	return(Validity);
  } catch (e) {
    return true;
  }
  
}

function nameValidation(entered)
{
	try {
		if (entered.value.length<2) 
		{
			if (id4DAccount) {
				alert("Please enter your company name before submitting");
			} else {
				alert("Please enter your name before submitting");
			}
			return false;
		}
		else
		{
			return true;
		}
	} catch (e) {
		return true;
	}
}

function invoicenameValidation(entered)
{
	try {
		if (entered.value.length<2) 
		{
			alert("Please enter your contact name before submitting");
			return false;
		}
		else
		{
			return true;
		}
	} catch (e) {
		return true;
	}
}

function addressValidation(entered)
{
	try {
		if (entered.value.length<10) 
		{
			alert("Please enter your address before submitting");
			return false;
		}
		else
		{
			return true;
		}
	} catch (e) {
		return true;
	}
}

function postcodeValidation(entered)
{
	try {
		if (entered.value.length<3) 
		{
			if (id4DAccount) {
				alert("Please enter your account number before submitting");
			} else {
				alert("Please enter your postcode before submitting");
			}
			return false;
		}
		else
		{
			return true;
		}
	} catch (e) {
		return true;
	}
}

function telephoneValidation(entered)
{
	try {
		if (entered.value.length<3) 
		{
			alert("Please enter your telephone number before submitting");
			return false;
		}
		else
		{
			return true;
		}
	} catch (e) {
		return true;
	}

}

function cardtypeValidation(entered)
{
	try {
		with (entered){
			
			if (selectedIndex != 0)	{
				return true;
			}
			else {
				alert("Please select your credit card type from the list before submitting.");
				return false;
			}

		}
	} catch (e) {
		return true;
	}

}

function cardexpiryValidation(monthEx, yearEx) {

	try {
		var m = monthEx.selectedIndex;
		var y = yearEx.selectedIndex;
	  
		timeNow = new Date();
		expiryDate = new Date((2000 + (1 * eval("yearEx[" + y + "].text"))), (eval("monthEx[" + m + "].text")), timeNow.getDate());

		if (expiryDate < timeNow) {
			alert("Your card has expired with the selected date please check.");
			return false;
		}
		else {
			return true;
		}
	} catch (e) {
		return true;
	}

}

function sourceValidation(entered)
{
	try {
		with (entered){
			
			if (selectedIndex != 0)	{
				return true;
			}
			else {
				alert("Please select where did you hear about this site from the list before submitting.");
				return false;
			}
		}
	} catch (e) {
		return true;
	}

}

function clearBasket() {
    return confirm('Are you sure you wish to clear the Shopping Basket?');
}

function clearCheckout() {
    return confirm('Are you sure you wish to clear the checkout details you have entered?');
}
