var ns4 = document.layers;
var ie = document.all;
var ns6 = document.getElementById && !document.all;
var alertboxmessage = "";
var frm;

function Question(type,name,divid,pattern,message)
{
	this.type=type;
	this.name=name;
	this.divid=divid;
	this.pattern=pattern;
	this.message=message;
}

function getElement(id)
{
	if (ie)
	{
		obj = document.all[id];
	}
		else if (ns6)
	{
		obj = document.getElementById(id);
	}

	if (!obj) 
	{
		return null;
	}
	else
	{
		return obj;
	}
}

function getValue(q)
{
	//check if form element is valid


	if (q.type=="text")
	{
		return frm.elements[q.name].value;
	}

	if (q.type=="select")
	{
		var pickedindex=frm.elements[q.name].selectedIndex;
		return frm.elements[q.name].options[pickedindex].value;
	}

	if (q.type=="radio")
	{
		tempvalue="";
		for (i=0;i<frm.elements[q.name].length;i++) 
		{
			if (frm.elements[q.name][i].checked) 
			{
				tempvalue=frm.elements[q.name][i].value;
			}
		}	
		return tempvalue;
	}
}

function processForm()
{
	//post-validation processing

}


function validateForm(FormName)
{
var validform=true;
frm=document.forms[FormName];

//define question parameters
//question type, field name, field error div id, regular expression, alertbox message

var questions = new Array();
questions[0]=new Question('text','first_name','first_name_error','[\\S]+','Please enter your first name.');
questions[1]=new Question('text','last_name','last_name_error','[\\S]+','Please enter your last name.');
questions[2]=new Question('select','00N30000001NUyf','stage_error','[\\S]+','Please select the stage of your Biosecurity Plan.');
questions[3]=new Question('radio','00N30000001NUwK','kit_error','[\\S]+','Please select which Biosecurity Kit best fits your needs.');
questions[4]=new Question('text','email','email_error','^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*\\.(\\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$','Please enter a valid e-mail address.');


//hide previous warnings
alertboxmessage="";
for (var x=0;x<questions.length;++x)
{
	hideWarning(questions[x].divid);
}

//loop through question array
for (var x=0;x<questions.length;++x)
{
	if (checkValue(questions[x])==false)
	{
		validform=false;
	}
}


if (validform==true)
{
	processForm();
	return validform;
}
else if (ns4)
{
	alert(alertboxmessage);
	return validform;
}
else
{
	showError("submit_error","");
	self.scroll(0,200);
	return validform;
}

}

function checkValue(qst)
{
	var RegEx = new RegExp(qst.pattern);

	if (RegEx.test(getValue(qst)))
	{
		return true;
	}
	else
	{
		showError(qst.divid,qst.message);
		return false;
	}
	return true;
}


function showError(id,message)
{
	alertboxmessage=alertboxmessage +"\n" + message;
	obj=getElement(id);
	if (!obj) 
	{
		return false;
	}
	else
	{
		obj.style.display = "inline";
		return true;
	}
}


function hideWarning(id)
{
	obj=getElement(id);
	if (!obj)
	{
		return false;
	}
	else
	{
		obj.style.display = "none";
		return true;
	}
}