

//------------------------------------------------
// Trim trailing/leading whitespace from string //
//------------------------------------------------

function trim(str)
{
	return str.replace(/^\s+|\s+$/g, '');
}



//------------------------------------------------
// Prompt to correct mistakes, set field focus
//------------------------------------------------

function fieldinfo(alerttxt, fld)
{	
	alert(alerttxt);
	fld.focus();
}



//------------------------------------------------
// CONTACT US Form validation //
//------------------------------------------------

function Validate_cform(form)
{
	var alerttxt;
	var fldtxt=new Array(4);
	var fldtofocus;
	var goodtogo=true;
	var tfld;
	
	fldtxt[0]="";
	fldtxt[1]="";
	fldtxt[2]="";
	fldtxt[3]="";

	alerttxt="Please check that you have completed the following items correctly: \n"

	tfld=trim(form.details.value);
	if(tfld=="")
		{fldtxt[0] ="-- Questions Comments\n";
		goodtogo=false;
		fldtofocus=form.details;
		}


	tfld=trim(form.tel.value);
	if(tfld=="")
		{fldtxt[1] ="-- Phone\n";
		goodtogo=false;
		fldtofocus=form.tel;
		}


	x = form.email.value;
	filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (!filter.test(x)) 
		{fldtxt[2] ="-- Valid Email\n";
		goodtogo=false;
		fldtofocus=form.email;
		}	


	tfld=trim(form.wname.value);
	if(tfld=="")
		{fldtxt[3] ="-- Name\n";
		goodtogo=false;
		fldtofocus=form.wname;
		}
	

	alerttxt=alerttxt+=fldtxt[3]+=fldtxt[2]+=fldtxt[1]+=fldtxt[0];

	if (goodtogo==false) {fieldinfo(alerttxt, fldtofocus)}

	return goodtogo;

}


//------------------------------------------------
// FREE CONSULT Form validation //
//------------------------------------------------

function Validate_fform(form)
{
	var alerttxt;
	var fldtxt=new Array(7);
	var fldtofocus;
	var goodtogo=true;
	var tfld;
	
	fldtxt[0]="";
	fldtxt[1]="";
	fldtxt[2]="";
	fldtxt[3]="";
	fldtxt[4]="";
	fldtxt[5]="";
	fldtxt[6]="";

	alerttxt="Please check that you have completed the following items correctly: \n"
	
	tfld=trim(form.details.value);
	if(tfld=="")
		{fldtxt[0] ="-- Questions Comments\n";
		goodtogo=false;
		fldtofocus=form.details;
		}


	tfld=form.besttime.value;
	if(tfld=="none")
		{fldtxt[1] ="-- Best time to call\n";
		goodtogo=false;
		}


	if ((form.otherfirm[0].checked==false)
		&& (form.otherfirm[1].checked==false))	
		{fldtxt[2] ="-- Spoken with another firm\n";
		goodtogo=false;
		}


	tfld=trim(form.tel.value);
	if(tfld=="")
		{fldtxt[3] ="-- Phone\n";
		goodtogo=false;
		fldtofocus=form.tel;
		}


	x = form.email.value;
	filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (!filter.test(x)) 
		{fldtxt[4] ="-- Valid Email\n";
		goodtogo=false;
		fldtofocus=form.email;
		}	


	tfld=trim(form.lastname.value);
	if(tfld=="")
		{fldtxt[5] ="-- Last Name\n";
		goodtogo=false;
		fldtofocus=form.lastname;
		}
	

	tfld=trim(form.firstname.value);
	if(tfld=="")
		{fldtxt[6] ="-- First Name\n";
		goodtogo=false;
		fldtofocus=form.firstname;
		}

	
	alerttxt=alerttxt+=fldtxt[6]+=fldtxt[5]+=fldtxt[4]+=fldtxt[3]+=fldtxt[2]+=fldtxt[1]+=fldtxt[0];

	if (goodtogo==false) {fieldinfo(alerttxt, fldtofocus)}

	return goodtogo;

}
