var W3CDOM = (document.getElementsByTagName && document.createElement);

window.onload = function ()
{
	document.forms[0].onsubmit = function ()
	{
		return validate()
	}
}

firstError = null;
validForm =false;

function checkit(obj)
{
	if ((navigator.appName == "Microsoft Internet Explorer") || (navigator.appName == "Opera"))
	{
		if(obj.parentNode.previousSibling.firstChild.lastChild.nodeValue)
		{
			if(obj.parentNode.previousSibling.firstChild.lastChild.nodeValue.indexOf('*') !=-1) 
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		return true;
	}
	else
	{
		if(obj.parentNode.previousSibling.previousSibling.firstChild.firstChild)
		{
			if(obj.parentNode.previousSibling.previousSibling.firstChild.firstChild.nodeValue.indexOf('*') !=-1)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		return true;
	}
}

function testPhone(idx, arr)
{
	var t="";
	for (var i = idx; i<(idx+3); i++)
	{
		t+=""+arr[i].value
	}
	return !isNaN(t) && t.length==10
}

function validate()
{
	validForm = true;
	firstError = null;
	errorstring = '';

	//added by mm to beef up email validation
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

	var x = document.forms[0].elements;

	for (var i=0;i<x.length;i++)
	{
		if (x[i].name && x[i].name=='Email' && filter.test(x[i].value)==false) //ORIGINAL && x[i].value.indexOf('@') == -1
			writeError(x['Email'],'This is not a valid email address');
		else if (x[i].name && x[i].name=='postal' && (!/^[KLMNP]\d[a-z]\d[a-z]\d$/i.test(x[i].value)) )
			writeError(x['postal'],'This is not a valid postal code');
		else if (x[i].name && x[i].name.indexOf('phone') !=-1 && checkit(x[i]))
		{
			if (!testPhone(i,x)) writeError(x[i],'Enter a valid phone number')
			i+=2
		}
		else if (x[i].type && x[i].type!='hidden' && !x[i].value && checkit(x[i]))
			writeError(x[i],'This field is required');
	}
	if (!W3CDOM)
		alert(errorstring);
	if (firstError && firstError.focus)
	{
		firstError.focus();
	}
	if (validForm)
	{
		x["HomePhone"].value=x["hphone1"].value+x["hphone2"].value+x["hphone3"].value
		x["hphone1"].disabled=true; x["hphone2"].disabled=true; x["hphone3"].disabled=true;
		x["BusinessPhone"].value=x["bphone1"].value+x["bphone2"].value+x["bphone3"].value
		x["bphone1"].disabled=true; x["bphone2"].disabled=true; x["bphone3"].disabled=true;
	}
	return validForm;
}

function writeError(obj,message)
{
	validForm = false;

	if (!firstError)
	{
		firstError = obj;
	}
	if (obj.hasError) return;

	if (W3CDOM)
	{
		obj.className += ' error';
		obj.onchange = removeError;
		var sp = document.createElement('span');
		sp.className = 'error';
		sp.appendChild(document.createTextNode(message));
		obj.parentNode.appendChild(sp);
		obj.hasError = sp;
	}
	else
	{
		errorstring += obj.name + ': ' + message + '\n';
		obj.hasError = true;
	}
}

function removeError()
{
	this.className = this.className.substring(0,this.className.lastIndexOf(' '));
	this.parentNode.removeChild(this.hasError);
	this.hasError = null;
	this.onchange = null;
}