/* *************************************************************

	VALIDATION SCRIPT BY ALEX BURNHAM OF FREETIMERS INTERNET

	www.freetimers.co.uk - info@freetimers.co.uk

	Tested with IE5.01, IE5.5, Navigator 4.7, Opera 5.02
	15 June 2001

	Original: function emailCheck() written by
		Sandeep V. Tamhankar (stamhankar@hotmail.com)
		Script and many more are available free online at
		The JavaScript Source!! http://javascript.internet.com

	Script is used to validate simple information request forms

    	function validate() returns true or false.
		All conditions must be met to return true.

		Use as follows;

			if(validate("document.form"))
				document.form.submit();

	Known Issues:
		None.


*************************************************************** */
function stripNonNumbers (InString)
{
	// DO NOT CHANGE ANY OF CODE BELOW!!
	OutString="";
	for (Count=0;Count<InString.length;Count++)
	{
		TempChar=InString.substring (Count,Count+1);
		Strip=false;
 		CharString="0123456789";
		for (Countx=0;Countx<CharString.length;Countx++)
		{
			StripThis=CharString.substring (Countx,Countx+1)
			if (TempChar==StripThis)
			{
				Strip=true;
				break;
			}
		}
		if (Strip)
		OutString=OutString+TempChar;
	}
	return (OutString);
	// DO NOT CHANGE ABOVE CODE
}

function validate(formname)
{
	var theCheck = new Array('fname','lname','address','city','state','zip','numReg','fees','dietary','shipcost');

	for (x=0;x<theCheck.length;x++)
	{
		var theString = formname + "." + theCheck[x];

		if(eval(theString) != null)
		//check to ensure that the field exists!!!
		{
			thisCheck = eval(theString + ".value");

			if (thisCheck=="")
			{
				alert("Please enter your " + theCheck[x] + ".");
				eval(theString + ".focus()");
				return false;
			}
		}
	}

	//card only
	if (document.orderform.payment.value == "creditcard")
	{

	var cardCheck = new Array('cardnumber','cardname','cardaddress','cardexpiry','cardsignature');

	for (x=0;x<cardCheck.length;x++)
	{
		var cardString = formname + "." + cardCheck[x];

		if(eval(cardString) != null)
		//check to ensure that the field exists!!!
		{
			thiscardCheck = eval(cardString + ".value");

			if (thiscardCheck=="")
			{
				alert("Please enter your " + cardCheck[x] + ".");
				eval(cardString + ".focus()");
				return false;
			}
		}
	}

	}

	var numCheck = new Array('phone');

	for (x=0;x<numCheck.length;x++)
	{
		var minLength = 6;
		var maxLength = 16;

		var theNumber = formname + "." + numCheck[x];

		var whichNum = numCheck[x];

		if(whichNum != 'phone' &&  whichNum != 'fax')
		{
			switch (whichNum)
			{
				case 'hometel':
					whichNum = 'home telephone';
					break;

				case 'mobitel':
					whichNum = 'mobile phone';
					break;

				case 'worktel':
					whichNum = 'work telephone';
					break;

				case 'workext':
					whichNum = 'work extension';
					minLength = 1;
					maxLength = 6;
			}
		}

		if(eval(theNumber) != null)
		// check to ensure that the field exists!!!
		{
			eval(theNumber + ".value = stripNonNumbers(" + theNumber + ".value);");

			thisCheck = eval(theNumber + ".value.length");

			if((thisCheck>maxLength || thisCheck<minLength) && thisCheck>0)
			// Check number fields are right length
			{
				alert("Please check your " + whichNum + " number.\nMinimum length is " + minLength + " digits,\nand maximum length is " + maxLength + " digits.");
				eval(theNumber + ".focus();");
				return false;
			}
			else if(thisCheck==0 && numCheck[x]!="workext")
			// Check ALL main number fields are completed.
			{
				if (confirm("You have not entered your " + whichNum + " number.\nClick 'OK' to enter it now, or click 'Cancel' to ignore!"))
				{
					eval(theNumber + ".focus();");
					return false;
				}
			}
		}
	}

	var eCheck = new Array('email','priemail','secemail');

	var aCounter = 0; // check value to ensure main email address is filled in.

	for (x=0;x<eCheck.length;x++)
	{
		var theEmail = formname + "." + eCheck[x];

		if(eval(theEmail) != null) // check to ensure that the field exists!!!
		{
			thisCheck = eval(theEmail + ".value");

			if (eval(theEmail + ".value.length")>0)
			{
				if(!emailCheck(eval(theEmail + ".value")))
				{
					eval(theEmail + ".focus()");
					return false;
				}
			}
			else if (thisCheck=="" && aCounter==0)
			// first email address is essential... alway always always required!!!
			{
				alert("Please enter your email address.");
				eval(theEmail + ".focus()");
				return false;
			}

			aCounter++;
		}
	}

	// you got to the end - must be OK!!
	return true;
}
