
// JavaScript Document
function emailvalidation(entered, alertbox)
{
// E-mail Validation by Henrik Petersen / NetKontoret
// Explained at www.echoecho.com/jsforms.htm
// Please do not remove this line and the two lines above.
with (entered)
{
apos=value.indexOf("@"); 
dotpos=value.lastIndexOf(".");
lastpos=value.length-1;
if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) 
{if (alertbox) {alert(alertbox);} return false;}
else {return true;}
}
}


function valuevalidation(entered, min, max, alertbox, datatype)
{
// Value Validation by Henrik Petersen / NetKontoret
// Explained at www.echoecho.com/jsforms.htm
// Please do not remove this line and the two lines above.
with (entered)
{
checkvalue=parseFloat(value);
if (datatype)
{smalldatatype=datatype.toLowerCase();
if (smalldatatype.charAt(0)=="i") {checkvalue=parseInt(value)};
}
if ((parseFloat(min)==min && checkvalue<min) || (parseFloat(max)==max && checkvalue>max) || value!=checkvalue)
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
} 



	
function emptyvalidation(entered, alertbox)
{
	// Emptyfield Validation by Henrik Petersen / NetKontoret
	// Explained at www.echoecho.com/jsforms.htm
	// Please do not remove this line and the two lines above.
	with (entered)
	{
		if (value==null || value=="")
		{if (alertbox!="") {alert(alertbox);} return false;}
		else {return true;}
	}
}



function validateDropDown(entered, alertbox)
{
	with (entered)
	{
		if (value==null || value=="")
			{if (alertbox!="") {alert(alertbox);} return false;}
		else {return true;}
	}
}

function membershipvalidation(entered, alertbox)
{
	
	/*
	membership # is in the format X0abcdefgH
	Where 	X is one of the following letters: A,C,G,J,V
			0 is the number 0
			abcdefg is a number between 1000001 and 1420000
			H is the following calculation: mod((a*1)+(b*2)+(c*3)+(d*4)+(e*5)+(f*6)+(g*7), 9)
	*/
	
		//replace letter o with number 0	
		var str = entered.value;
		
		out = "o";
		add = "0";
		var temp = str.toLowerCase();
		while (temp.indexOf(out)>-1) 
		{
			pos= temp.indexOf(out);
			temp = "" + (temp.substring(0, pos) + add + 
			temp.substring((pos + out.length), temp.length));
		}
		
		var value = temp.split(' ').join('');
		
		
		var charcount = value.length;
		
		//check if character length = 10
		if(charcount == 10 && (value.charAt(1)=="0"))
		{						
			//check if firstchar is a letter			
			var firstchar = value.charAt(0).toLowerCase();
			if( (firstchar == "a") || (firstchar == "c") || (firstchar == "g") || (firstchar == "j") || (firstchar == "v") )
			{
				
				//check if memeber number is between 1000001 and 1420000
				var membernum = Number(value.slice(2, 9));
				if( (membernum >= 1000001) && (membernum <= 1420000) )
				{
					var tempval =0;			
					for(var i=2;i<9;i++)
						tempval += (Number(value.charAt(i))*(i-1))
					
					checkdigit = Number(tempval % 9);
					
				    //check the last digit
					if( Number(value.charAt(9))==checkdigit )
						return true;//alert(Number(value.charAt(9)) + " good: " + checkdigit );
					else alert(alertbox); return false;
					

				}else alert(alertbox); return false;
			}else alert(alertbox); return false;
			
			
			
		}else alert(alertbox); return false;
		
}



function formvalidation(thisform)
{
// This function checks the entire form before it is submitted
// Note: This function needs to be customized to fit your form
	
	with (thisform)
	{
		
		if (membershipvalidation(cardno, "Please enter a valid card number.")==false) {cardno.focus(); return false;};
		if (emptyvalidation(fname,"Please enter your first name.")==false) {fname.focus(); return false;};
		if (emptyvalidation(lname,"Please enter your last name.")==false) {lname.focus(); return false;};
		if (emailvalidation(email,"Please enter your email.")==false) {email.focus(); return false;};
		
		
	}
	
}

