//===============================================
// Hardcore form validation functions Ver 2.3
// Written by Mark Liddington 2002
//===============================================
// Supports validation of the following fields
//
// text - is there something in the box?
// num - must consist of numbers only
// email - full validation
// select - with optional "other" field validation
// checkbox - has user checked it?
// file - with extension validation
// radio - have they selected at least one? (inc possible other field?)
//===============================================

	var fieldstovalidate=new Array();

// Take input string and strip all characters that aren't in comparison string
	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;
	}

// Display prompt box to user, and if they click "ok" go to specified URL
	function promptuser(thetext,theurl)
	{
		var aa=confirm(thetext);
		if(aa==1)
		{
			document.location=theurl;
		}
	}
	
// Validate email address
	function isValidEmailAddr(str)
	{
		var emailregexp = eval("/^[\\w-_~]+(\\.[\\w-_~]+)*@[\\w-_~]+(\\.[\\w-_~]+)+$/");
		var results = str.search(emailregexp);
		return (results == 0);
	}
	
// Extract file extension from supplied filename
	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;
	}
		
// Push field to validate onto stack
	function valField(form_id,obj_id,ob_name,obj_info)
	{
		if(fieldstovalidate[form_id] == null)
		{
			fieldstovalidate[form_id]=new Array();
		}
		var nextNum=fieldstovalidate[form_id].length;
		fieldstovalidate[form_id][nextNum]=new Array();
		fieldstovalidate[form_id][nextNum]["id"]=obj_id;
		fieldstovalidate[form_id][nextNum]["name"]=ob_name;
		fieldstovalidate[form_id][nextNum]["info"]=obj_info;
	}
	
////////////////////////////////////
// Validate stack of form elements//
////////////////////////////////////
	function validate(form_id)
	{
		var obj;
		var obj2;
		var stndstr="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		var stndNum="0123456789";
		var teststring1="";
		var teststring2="";
		var object_type;
		
		for(var i=0;i<fieldstovalidate[form_id].length;i++)
		{
			objId=fieldstovalidate[form_id][i]["id"];
			objTitle=fieldstovalidate[form_id][i]["name"];
			objInfo=fieldstovalidate[form_id][i]["info"];
			
			if(!document.getElementById(objId))
			{
				obj=eval("document.getElementById('"+form_id+"')."+objId);
				object_type = "radio";
			}else{
				obj=document.getElementById(objId);
				object_type = obj.type;
			}

			switch (object_type)
			{
				case "text":
					if(obj.value=="")
					{
						alert("Please fill out all required fields. The field '"+objTitle+"' must be completed");
						obj.focus();
						return false;
					}
					switch (objInfo)
					{
						case "email":
							if(!isValidEmailAddr(obj.value))
							{
						 		alert("Please check the email address supplied.\nIt appears not to be correct." );
								obj.focus();
			     	    return false;
						 	}
						break;
						case "num":
							obj.value=stripString(obj.value,stndNum);
							if(obj.value=="")
							{
								alert("Please enter a valid "+objTitle);
								obj.focus();
								return false;
							}
						break;
					}
				break;
				case "password":
					if(obj.value=="")
					{
						alert("Please fill out all required fields. The field '"+objTitle+"' must be completed");
						obj.focus();
						return false;
					}
					if(objInfo != "")
					{
						obj_repeated = document.getElementById(objInfo);
						
						if(obj.value != obj_repeated.value)
						{
							alert("Please ensure that the passwords you have entered match");
							obj_repeated.value = "";
							obj_repeated.focus();
							return false;
						}
					}
				break;
				case "file":
					if(objInfo=="")
					{
						if(obj.value=="")
						{
							alert("Please fill out all required fields. The field '"+objTitle+"' must be completed");
							obj.focus();
							return false;
						}
					}else{
						teststring1=objInfo.split(";");
						teststring2=getFileExtension(obj.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 '"+objTitle+"' field is not allowed by this server.\n\n The following types are supported ("+objInfo.toUpperCase()+")");
							obj.focus();
							return false;
						}
					}
				break;
				case "radio":
					var sel=0;
					
					obj=eval("document.getElementById('"+form_id+"')."+objId);
					
	    		for(var p=0;p<obj.length;p++)
					{
		        if(obj[p].checked)
						{
							if(objInfo!="")
							{
								if(obj[p].value=="")
								{
									if(document.getElementById(objInfo).value=="")
									{
										alert("Please enter a value for the '"+objTitle+ " other' field");
										document.getElementById(objInfo).focus();
										return false;
									}else{
										sel=1;
										break;
									}
								}else{
									sel=1;
		         			break;
								}
							}else{
		         		sel=1;
		         		break;
		         	}
						}
					}   	
					if(sel==0)
					{
						alert("Please select an option for '"+objTitle+"'");
						return false;
					}
				break;
				case "checkbox":
	        if(!obj.checked)
					{
	         	alert("Please check the '"+objTitle+"'");
						return false;
					} 	
				break;
				case "select-one":
					if(obj.options.selectedIndex==0)
					{
						alert("Please select a '"+objTitle+"'");
						obj.focus();
						return false;
					}
					if(objInfo!="")
					{
						obj2=document.getElementById(objInfo);
						if(obj.options.selectedIndex==(obj.options.length-1))
						{
							if(obj2.value=="")
							{
								alert("You have chosen an option currently not specifically listed under '"+objTitle+"'.\n Please fill in the extra provided text field.");
								obj2.focus();
								return false;
							}
						}
					}
				break;
			}
		}
		return true;
	}