//--------------------------
// These functions are used to validate fields on forms
//--------------------------
//This function checks the entire form before it is submitted.
function formvalidation(thisform)
{
with (thisform)
{
if (emptyvalidation(name,"Please Enter Your Name")==false) {name.focus(); return false;};
if (emptyvalidation(organization,"Please Enter Your Company or Organization")==false) {organization.focus(); return false;};
if (emptyvalidation(address,"Please Enter Your Address")==false) {address.focus(); return false;};
if (emptyvalidation(city,"Please Enter Your City")==false) {city.focus(); return false;};
//element 1 (0 based) of state select list array is 'non-usa', so province text field must be non-empty
if (state[1].selected) 
{
	if (emptyvalidation(province,"Please Enter a State or Province since you've selected State=Non-USA")==false) {province.focus(); return false;};
}

if (state[0].selected) 
{
	if (emptyvalidation(state,"Please select a State or 'Non-USA' if other than United States")==false) {state.focus(); return false;};
}

//element 1 (0 based) of state select list array is 'non-usa', so country must not be US (element 0 of country select list is 'US') 


if (state[1].selected) 
	{
		if (country[0].selected)
		{
			alert("Please select a Country other than 'United States' since you've selected State=Non-USA");
			country.focus();
			return false;
		}
	}

if (emptyvalidation(zip,"Please Enter Your Zip Code")==false) {zip.focus(); return false;};
if (emptyvalidation(phone,"Please Enter Your Phone Number")==false) {phone.focus(); return false;};
if (emailvalidation(email,"Please Enter a Valid eMail Address. Example: name@domain.ext")==false) {email.focus(); return false;};
//for radio buttons, you cannot put the focus back on the radio button, so put focus somewhere nearby
if (radiovalidation(referer,"Please answer: How did you find out about PQMPLus?")==false) {otherreferer.focus(); return false;};
//if (valuevalidation(Value,0,5,"Value MUST be in the range 0-5")==false) {Value.focus(); return false;};
//if (digitvalidation(Digits,3,4,"You MUST enter 3 or 4 integer digits","I")==false) {Digits.focus(); return false;};
}
//alert ("done validation");
return true;
} 
//--------------------------
//Checking if the content has the general syntax of an email.
//Optional parameters are: 
//text--text that will show in an alertbox if content is illegal.
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;}
}
} 
//--------------------------
//Checking if the content is a number in a limited area.
//Optional parameters are:
//min --minimum value allowed in the field.
//max --maximum value allowed in the field.
//text --text that will show in an alertbox if content is illegal.
//type --enter "I" if only integers are allowed.
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;}
}
} 
//--------------------------
//Checking if the content has a certain number of digits.
//Optional parameters are:
//min --minimum number of digits allowed in the field.
//max --maximum number of digits allowed in the field.
//text --text that will show in an alertbox if content is illegal.
//type --enter "I" if only integers are allowed.
function digitvalidation(entered, min, max, alertbox, datatype)
{
// Digit 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 (value.indexOf(".")!=-1) {checkvalue=checkvalue+1}};
}
if ((parseFloat(min)==min && value.length<min) || (parseFloat(max)==max && value.length>max) || value!=checkvalue)
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
} 
//--------------------------
//Checking if the field is empty.
//Optional parameters are:
//text --text that will show in an alertbox if content is illegal.
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;}
}
}

// Radio Button Validation
// Copyright Island Images - author: Don Adkins
function radiovalidation(btn, alertbox) {
var cnt = -1;
for (var i=0; i < btn.length; i++) {
   if (btn[i].checked) {
	   cnt = i; 
	   i = btn.length;
	   }
   }
   //alert(cnt);
if (cnt == -1) {
	if (alertbox!="") {
		alert(alertbox);
		} 
	return false;
	}
	else {return true;}
}