//===============================================
// Hardcore form validation functions Ver 1.2
// Written by Mark Liddington 2002
// Javascript 1.1
//===============================================
// Supports validation of the following fields
// text - is there something in the box?
// email - full validation
// select - with optional "other" field validation
// file - with extension validation
// username - min value, valid characters
// password - min value, valid characters, optional password confirm field
//===============================================

function stripString(string1,validchars)
{
	var i;
	var ts="";
	var string2 = "";
	for(i=0;i<string1.length;i++)
	{
		ts=string1.substring(i,i+1);
		if (validchars.indexOf(ts) != -1)
		{
			string2+=ts;
		}
	}
	return string2;
}

function promptuser(thetext,theurl)
{
	var aa=confirm(thetext);
	if(aa==1)
	{
		document.location=theurl;
	}
}
	
function isValidEmailAddr(str)
{
	var emailregexp = eval("/^[\\w-_~]+(\\.[\\w-_~]+)*@[\\w-_~]+(\\.[\\w-_~]+)+$/");
	var results = str.search(emailregexp);
	return (results == 0);
}
	
function getFileExtension(thestring)
{
	for(var i=thestring.length-1;i>0;i--)
	{
		if(thestring.substr(i,1)==".")
		{
			return thestring.substr(i+1,thestring.length-1);
		}
	}
	return false;
}
	
function validateform(f)
{
	var tempobj;
	var tempobj2;
	var stndstr="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var stndNum="0123456789";
	var message;
	var teststring1="";
	var teststring2="";
	var teststring3="";
	for(var i=0;i<fieldstovalidate.length;i++)
	{
		tempobj=eval("f."+fieldstovalidate[i][0]);
		if(fieldstovalidate[i][1]=="text")
		{
			if(tempobj.value=="")
			{
				alert("Please fill out all required fields. The field '"+fieldstovalidate[i][2]+"' must be completed");
				tempobj.focus();
				return false;
			}
		}
		if(fieldstovalidate[i][1]=="email")
		{
			if(tempobj.value.length>0)
			{
				if(!isValidEmailAddr(tempobj.value))
				{
			 		alert("Please check the email address supplied.\nIt appears not to be correct." );
					tempobj.focus();
     	    		return false;
			 	}
			}else{
				alert("Please enter a valid email address");
				tempobj.focus();
				return false;
			}
		}
		if(fieldstovalidate[i][1]=="tel")
		{
			tempobj.value=stripString(tempobj.value,stndNum);
			if(tempobj.value=="")
			{
				alert("Please enter a valid "+fieldstovalidate[i][2]);
				tempobj.focus();
				return false;
			}
		}
		if(fieldstovalidate[i][1]=="username")
		{
			message="Usernames must consist of only numbers and letters of the alphabet, the exceptions being '"+fieldstovalidate[i][4]+"'. They must be at least "+fieldstovalidate[i][3]+" characters long and begin with a letter.";
			teststring1=stripString(tempobj.value,(stndstr+stndNum)+fieldstovalidate[i][4]);
			teststring2=stripString(tempobj.value.substr(0,1),stndstr);
			if((teststring1.length<fieldstovalidate[i][3])||(teststring2=="")||(teststring1.length!=tempobj.value.length))
			{
				alert("Please enter a valid "+fieldstovalidate[i][2]+"\n\n"+message);
				tempobj.focus();
				return false;
			}
		}
		if(fieldstovalidate[i][1]=="password")
		{
			message="Passwords must consist of only numbers and letters of the alphabet, the exceptions being '"+fieldstovalidate[i][4]+"'. They must be at least "+fieldstovalidate[i][3]+" characters long, start with a letter, and contain one number. Please re-enter it.";
			teststring1=stripString(tempobj.value,(stndstr+stndNum)+fieldstovalidate[i][4]);
			teststring2=stripString(tempobj.value.substr(0,1),stndstr);
			teststring3=stripString(tempobj.value,stndNum);
			if(fieldstovalidate[i][5]!="")
			{
				tempobj2=eval("f."+fieldstovalidate[i][5]);
			}
			if((tempobj.value.length!=teststring1.length)||(teststring2=="")||(teststring3=="")||(tempobj.value.length<fieldstovalidate[i][3]))
			{
				alert("Please enter a valid password.\n\n"+message);
				if(fieldstovalidate[i][5]!="")
				{
					tempobj2.value="";
				}
				tempobj.focus();
				return false;
			}
			if(fieldstovalidate[i][5]!="")
			{
				if(tempobj.value!=tempobj2.value)
				{
					alert("The passwords you have entered do not match, please re-enter them.");
					tempobj2.value="";
					tempobj.focus();
					return false;
				}
			}
		}
		if(fieldstovalidate[i][1]=="select")
		{
			if(fieldstovalidate[i][3])
			{
				if(tempobj.options.selectedIndex==0)
				{
					alert("Please select a "+fieldstovalidate[i][2]);
					tempobj.focus();
					return false;
				}
			}
			if(fieldstovalidate[i][4]!="")
			{
				tempobj2=eval("f."+fieldstovalidate[i][4]);
				if(tempobj.options.selectedIndex==(tempobj.options.length-1))
				{
					if(tempobj2.value=="")
					{
						alert("You have chosen an option currently not specifically listed under "+fieldstovalidate[i][2]+".\n Please fill in the extra provided text field.");
						tempobj.focus();
						return false;
					}
				}
			}
		}
		if(fieldstovalidate[i][1]=="file")
		{
			if(tempobj.value=="")
			{
				alert("Please select a file to upload. The field '"+fieldstovalidate[i][2]+"' must be completed");
				tempobj.focus();
				return false;
			}
			if(fieldstovalidate[i][3]!="")
			{
				teststring1=fieldstovalidate[i][3].split(";");
				teststring2=getFileExtension(tempobj.value);
				var exfound=false;
				if(teststring2!="")
				{
					for(var p=0;p<teststring1.length;p++)
					{
						if(teststring1[p].toUpperCase()==teststring2.toUpperCase())
						{
							exfound=true;
						}
					}
				}
				if(!exfound)
				{
					alert("The type of file you have selected to upload in the '"+fieldstovalidate[i][2]+"' field is not allowed by this server.\n\n The following types are supported ("+fieldstovalidate[i][3].toUpperCase()+")");
					tempobj.focus();
					return false;
				}
			}
		}
	}
	return true;
}