
function validate()
{

// define required feilds 
document.quote.QLName.required =true;
document.quote.ConMethod.required =true;

// set display messages
document.quote.QLName.msg ="Could we please know your last name?";
document.quote.ConMethod.msg ="How would you like to be contacted?";
var PhMsg ="What is your phone number?";
var FaxMsg ="What is your fax number?";
var EMMsg ="What is your e-mail address?";
var RMMsg ="What is your mailing address?";
// check required feilds are empty
gobackmsg = "";
sendmsg = false;
x = 1;
for (i=0;i<=document.quote.length-4;i++){
	var elem =document.quote.elements[i]
	if (elem.required){
		if ((elem.value == null) || (elem.value == "")||(elem.value == " ")||(elem.value == "  ")) {	
			gobackmsg += "\n" + elem.msg;
			sendmsg = true;
			var x = i;
		}
	}	
}

// check contact method
if (!document.quote.ConMethod[0].checked && !document.quote.ConMethod[1].checked && !document.quote.ConMethod[2].checked && !document.quote.ConMethod[3].checked) {
	gobackmsg += "\n" + document.quote.ConMethod.msg;
	sendmsg = true;
}

// check fields required for contact completed
if (document.quote.ConMethod[0].checked && (document.quote.QPh.value.length == 0)){
	gobackmsg += "\n" + PhMsg;
	sendmsg = true;
}	
if (document.quote.ConMethod[1].checked && (document.quote.QFax.value.length == 0)){
	gobackmsg += "\n" + FaxMsg;
	sendmsg = true;
}	
if (document.quote.ConMethod[2].checked && (document.quote.QEMail.value.length == 0)){
	gobackmsg += "\n" + EMMsg;
	sendmsg = true;
}	
if (document.quote.ConMethod[3].checked && (document.quote.QAdd.value.length == 0)){
	gobackmsg += "\n" + RMMsg;
	sendmsg = true;
}	
// create alert message
alertmsg = "----------------------------------------------------------------------\n"
alertmsg +="Sorry. We need more information.\n"
alertmsg +="-----------------------------------------------------------------------\n"
alertmsg +=gobackmsg
alertmsg +="\n\n-------------------------------------------------------------------\n"
alertmsg +="Thank You\n"
alertmsg +="-------------------------------------------------------------------\n"
// return message  or submit form
if (sendmsg){
	alert (alertmsg);
	document.quote.QComp.focus();
	return false;
}
else {
	document.quote.submit();
	return true;
}
}
//end of validate


function chkpin(tf,tpin) {
//check pin is number and 5 characters
if (tpin.length < 5 || isNaN(parseInt(tpin))){
	alert ("The Pin Number should be 5 digits - 5 digits\ni.e. 12345-67890");
	tf.focus();
	tf.value="";
}
}

function chkemail()
{
// check email feild is properly formatted        
var address = document.quote.QEMail.value
if ((address.indexOf('@', 0) <=0) || (address.indexOf('.', 0) <=2))	{
	if (confirm("If you wish to be contacted,\nplease enter a proper e-mail address.\n\nfor example\n myname@myprovider.ca")) {
		document.quote.QEMail.focus();
		document.quote.QEMail.value=""; 
		}
	else {
		document.quote.QEMail.focus();
		document.quote.QEMail.value=" ";
		document.quote.QContact.focus();
		}
	}
}

//replace = sign with equal to avoid splitting while parsing
//and truncate textarea boxes at 1024 to avoid hackers who like to cut and paste
function shorteq(tf,tval) {
	tarray=tval.split("=");
	tstring=tarray.join(" equals ");
	if (tstring.length > 1024){
		tshort=tstring.substring(0,1024);
	}
	else {
		tshort=tstring;
	}
	tf.value=tshort;
}


//check ph# and fax number
function chkphone(tf,tph) {
//set base values
baseph="";
pherr="no";
//step thru each letter create base number
	for (i=0;i<tph.length;i++)	{
		var ch = tph.charAt(i);
		if (ch != ')' && ch != '(' && ch != '-'  && ch != ' ')	{
			if (isNaN(parseInt(ch))) {pherr="Yes";}
			else {baseph += ch;}
		}
	}	
//create a properly formatted phone number or fail
var phl =  (baseph.length);
if (phl==11){goodph=baseph.substring(0,1)+"-"+baseph.substring(1,4)+"-"+baseph.substring(4,7)+"-"+baseph.substring(7,11);}
else if (phl==10){goodph="1-"+baseph.substring(0,3)+"-"+baseph.substring(3,6)+"-"+baseph.substring(6,10);}
else if (phl==8){goodph=baseph.substring(0,1)+"-"+baseph.substring(1,4)+"-"+baseph.substring(4,8);}
else if (phl==7){goodph=baseph.substring(0,3)+"-"+baseph.substring(3,7);}
else pherr="Yes";
//display error message or enter proper phone number	
if (pherr=="Yes") {	
	alert(" Please enter a proper phone number \n (i.e. 123-4567 or 1-613-123-4567)");
	tf.focus();
	tf.value="";
	}
else {tf.value=goodph;}	
}

// end hiding -->
