
function next()
{
        if (checkform())
        {
		document.form1.direction.value = "NEXT";
		document.form1.submit();
        }
}

function finish()
{
        if (checkform())
        {
		document.form1.direction.value = "FINISH";
		document.form1.submit();
        }
}

function getTextArray(f, grpName)
{
	var grp, rad, r = 0, sum =0;
	var lang = 'english';

	if (document.form1.lang.value != "" || document.form1.lang != null)
	{
		lang = document.form1.lang.value;
	}

	grp = (typeof grpName == 'string') ? document.form1[grpName] : grpName;
	while (rad = grp[r++])
	{ 
		if (rad.value > 0) 
    			sum = sum + parseInt(rad.value);
	}
	return sum;
}

function sumTextArray(f, grpName)
{
	var grp, rad, r = 0, sum =0;
	var lang = 'english';

	if (document.form1.lang.value != "" || document.form1.lang != null)
	{
		lang = document.form1.lang.value;
	}

	grp = (typeof grpName == 'string') ? document.form1[grpName] : grpName;

	if (grp.length > 0)
	{
		while (rad = grp[r++])
		{ 
			if (!IsNumeric(rad.value))
			{
				if(lang == 'french')
					alert("Veuillez entrer un chiffre");
				else
					alert("Please enter a numeric value");

				return false;
			}

			if (rad.value > 0) 
				sum = sum + parseInt(rad.value);
		}
	}
	else
	{
		sum = grp.value;

		if (!IsNumeric(sum))
		{
			if(lang == 'french')
				alert("Veuillez entrer un chiffre");
			else
				alert("Please enter a numeric value");

			return false;
		}
	}

	document.form1.SumTotalTextCombo.value = sum; 
}


function checkform()
{
	var lang = 'english';

	if (document.form1.lang.value != "" || document.form1.lang != null)
	{
		lang = document.form1.lang.value;
	}

	//date validation to deny future dates MM
	if (document.form1.DATE_NEW != null)
	{
		var result = true;
		var message = "";
		var userDate = document.form1.DATE_NEW.value;
		var strYear = userDate.substring(6,10);
		var strMonth = userDate.substring(3,5);
		var strDay = userDate.substring(0,2);
	
		if (strMonth.length < 2)
		{
			strMonth = "0"+ strMonth;
		}
		if (strDay.length < 2)
		{
			strDay = "0"+ strDay;
		}
		


		var now = new Date();	

		var currentYear = now.getFullYear();
		var currentMonth = now.getMonth() + 1;
		var currentDay = now.getDate();	



		//date validation MM
		if((strDay > currentDay && strDay < 31 && strMonth == currentMonth && strMonth < 13 && strYear == currentYear) || (strMonth > currentMonth && strMonth < 13 && strYear == currentYear) || (strYear > currentYear)) 
		{
			if (strDay < 1 || strDay > 31) 
			{
				message += "Day must be between 1 and 31.\n";
			}
			else if (strMonth < 1 || strMonth > 12 ) 
			{ 
				message += "Month must be between 1 and 12.\n";
			}
			else
			{
				message += "You have entered a date from the future.\nPlease select the correct date of your visit.\n";
			}
			alert(message);	
			return false;
		}else if (strYear <= 2008)
		{
			if (strYear == 2008 && strMonth > 9)
			{
				return true;
			}
			else
			{
				message += "You have entered an invalid date.\nPlease select the correct date of your visit.\n";
				alert(message);	
				return false;
			}
		}
		else 
		{
			return true;
		}

	}

	//TOMBSTONE VALIDATION
	if (document.form1.FirstName !=null)
	{
		//var valid = isAlpha(document.form1.FirstName.value);
		var valid = true;

		if(hasNumbers(document.form1.FirstName.value))
			valid = false;

		if (valid == false)
		{
			//alert("saadi" + lang);
			if (lang == 'french')
				alert ("Veuillez entrer votre prénom.");
			else
				alert ("Please enter a valid First Name.");

			document.form1.FirstName.focus();
			return false;
		}
	}

	if (document.form1.LastName !=null)
	{
		//var valid = isAlpha(document.form1.LastName.value);
		var valid = true;


		if(hasNumbers(document.form1.LastName.value))
			valid = false;

		if (valid == false)
		{
			if (lang == 'french')
				alert ("Veuillez entrer votre nom de famille.");
			else
				alert ("Please enter a valid Last Name.");

			document.form1.LastName.focus();
			return false;
		}
	}

	if (document.form1.EmailAddress !=null)
	{
		var valid = emailValidate(document.form1.EmailAddress.value);

		if (document.form1.EmailAddress.value.length >= 1 && valid == false)
		{
			alert ("Please enter a valid Email address.");
			document.form1.EmailAddress.focus();
			return false;
		}else if (document.form1.EmailAddress.value == "")
		{
			alert ("Please enter a valid Email address.");
			document.form1.EmailAddress.focus();
			return false;
		}
	}

/*	if (document.form1.Address !=null)
	{
		var valid = true;

		if (document.form1.Address.value == "")
		{
			if (lang == 'french')
				alert ("Veuillez entrer votre addresse.");
			else
				alert ("Please enter a valid Address.");

			document.form1.Address.focus();
			return false;
		}
	}

	if (document.form1.City !=null)
	{
		var valid = true;

		if (document.form1.City.value == "")
		{
			if (lang == 'french')
				alert ("Veuillez entrer votre ville.");
			else
				alert ("Please enter a valid City.");

			document.form1.City.focus();
			return false;
		}
	}

	if (document.form1.StateProv !=null)
	{
		var valid = true;

		if (document.form1.StateProv.value == "")
		{
			if (lang == 'french')
				alert ("Veuillez entrer votre province.");
			else
				alert ("Please enter a valid State / Province.");

			document.form1.StateProv.focus();
			return false;
		}
	}

	if (document.form1.PostCode !=null)
	{
		var valid = true;

		if (document.form1.PostCode.value == "")
		{
			if (lang == 'french')
				alert ("Veuillez entrer votre province.");
			else
				alert ("Please enter a valid Zip / Postal code.");

			document.form1.PostCode.focus();
			return false;
		}
	}
*/	
	// Telephone element for UK clients
	if (document.form1.Telephone != null)
	{
		var valid = true;
		var phonum = document.form1.Telephone.value;

		if (document.form1.Telephone.value == "")
		{
			if (lang == 'french')
				alert ("");
			else
				alert ("Please enter a valid Telephone.");

			document.form1.PostCode.focus();
			return false;
		}
		else
		{
			if (!IsNumeric(phonum))
			{
				if(lang == 'french')
					alert("Veuillez entrer un chiffre");
				else
					alert("Please enter a numeric Telephone number");
				document.form1.Telephone.focus();			
				return false;
			}
		}
	}

	if (document.form1.TelephoneNumber !=null || document.form1.TelephoneAreaCode !=null)
	{
		var areacode = document.form1.TelephoneAreaCode.value;
		var areacode2 = replaceChars(areacode);
		var phoneno = document.form1.TelephoneNumber.value;
		var phoneno2 = replaceChars(phoneno);

		if (areacode.length != 3 || IsNumeric(areacode) == false)
		{
			if (lang == 'french' || lang=='fr')
				alert ("Veuillez entrer votre indicatif régional.");
			else if(lang == "spanish")
				alert ("Entre por favor un válido código de área.");
			else
				alert ("Please enter a valid Area Code.");

			document.form1.TelephoneAreaCode.focus();
			return false;
		}
		else if(phoneno2.length != 7 || IsNumeric(phoneno2) == false)
		{
			if (lang == 'french' || lang=='fr')
				alert ("Veuillez entrer un numéro de téléphone valide sous le format 5555555");
			else if(lang == "spanish")
				alert ("Ingrese por favor un número de teléfono válido en el formato siguiente: 5555555");
			else
				alert ("Please enter a valid telephone number in the following format: 5555555");

			document.form1.TelephoneNumber.focus();
			return false;
		}
	}

	if (document.form1.TelephoneExtension !=null)
	{
		var telext = document.form1.TelephoneExtension.value;
		var telext2 = replaceChars(telext);
		var valid = IsNumeric(telext2);

		if (valid == false)
		{
			alert ("Please enter a valid Telephone Extension.");
			document.form1.LastName.focus();
			return false;
		}
		else
		{
			document.form1.TelephoneExtension.value = telext2;
		}
	}

	return true;
}

function emailValidate(emailaddress)
{	
	var checkTLD=0; 
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/; 
	var emailPat=/^(.+)@(.+)$/; 
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]"; 
	var validChars="\[^\\s" + specialChars + "\]"; 
	var quotedUser="(\"[^\"]*\")"; 
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/; 
	var atom=validChars + '+'; 
	var word="(" + atom + "|" + quotedUser + ")"; 
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$"); 
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$"); 
	var matchArray=emailaddress.match(emailPat);

	if (matchArray==null)
	{ 
		//alert("The Email Address Is Invalid"); 
		return false; 
	}

	var user=matchArray[1]; 
	var domain=matchArray[2]; 

	for (i=0; i<user.length; i++)
	{ 
		if (user.charCodeAt(i)>127)
		{ 
			//alert("The Username Contains Invalid Characters."); 
			return false; 
		} 
	}

	for (i=0; i<domain.length; i++)
	{ 
		if (domain.charCodeAt(i)>127)
		{ 
			//alert("Ths Domain Name Contains Invalid Characters."); 
			return false; 
		}
	}

	if (user.match(userPat)==null)
	{ 
		//alert("The Username Is Invalid."); 
		return false; 
	} 

	var IPArray=domain.match(ipDomainPat); 

	if (IPArray!=null)
	{
		for (var i=1;i<=4;i++)
		{ 
			if (IPArray[i]>255)
			{ 
				//alert("The Destination IP Address Is Invalid."); 
				return false; 
			}
		}
		return true; 
	} 

	var atomPat=new RegExp("^" + atom + "$"); 
	var domArr=domain.split("."); 
	var len=domArr.length; 

	for (i=0;i<len;i++)
	{ 
		if (domArr[i].search(atomPat)==-1) 
		{ 
			//alert("The Domain Name Is Invalid."); 
			return false; 
		} 
	}

	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) 
	{
		//alert("The Domain Name Extension Is Invalid"); 
		return false; 
	} 

	if (len<2)
	{ 
		//alert("The Address Is Missing A Hostname."); 
		return false; 
	}
}


function replaceChars(entry)
{
	//out = "()+-x"; // replace this
	var add = ""; // with this
	var temp = "" + entry; // temporary holder
	var i = 0;
	var out2 = new Array(5);
	out2[0] = "(" ;
	out2[1] = ")" ;
	out2[2] = "+" ;
	out2[3] = "-" ;
	out2[4] = "x" ;

	for (i = 0; i < out2.length; i++)
	{
		var out = out2[i];
		while (temp.indexOf(out)>-1)
		{
			pos= temp.indexOf(out);
			temp = "" + (temp.substring(0, pos) + add + 
			temp.substring((pos + out.length), temp.length));
		}
	}
	//alert(temp);
	return temp;
}

function IsNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}

var letters="abcdefghijklmnopqrstuvwxyz";
var LETTERS="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

function isAlpha(c)
{
	// is c a String or a character?
	if(c.length>1)
	{
		for(j=0;j<c.length;j++)
		{
			// call isAlpha recursively for each character
			alpha=isAlpha(c.substring(j,j+1));

			if(!alpha)
				return alpha;
		}
		return alpha;
	}
	else
	{
		// if c is alpha return true
		if(letters.indexOf(c)>=0 || LETTERS.indexOf(c)>=0)
			return true;
		return false;
	}
}

function hasNumbers(text)
{
	for(k=0;k<text.length;k++)
	{
		noNum = isNaN(text.substring(k,k+1));

		if(text.substring(k,k+1) == " ")
			noNum = true;

		if(!noNum)
		{
			return true;
			k = text.length;
		}
	}
}

function checkbay(src)
{
	//code for parsing the bay hbc card number
	var cd = "";

	if(src.length==9)
	{
		cd = src;
	}
	else
	{
		return false;
	}

	//now that we have a code, check it
	//first, eliminate codes that start with digits less than 6
	var nums = new Array(9);

	for(x=0;x<9;x++)
	{
		nums[x] = cd.charAt(x);		
	}

	if(nums[0]<6)
	{
		return false;
	}

	var checksum = 0;
	var checkcalc = 0;

	for(x = 0;x<8;x++)
	{
		checksum = (checksum ) + (nums[x] * (x+1));
	}

	var stringchecksum = checksum + "";
	var sumlastdigit = stringchecksum.substring(stringchecksum.length-1);
	var lastcheck = 10 - sumlastdigit;

	if(lastcheck==10)
		lastcheck = 0;
	if(nums[8]==lastcheck)
	{
		return true;
	}
	else
	{
		return false;
	}

}