//
// Validates form mail. To define which fields are required, add the field name
// to the value of the "required_fields" hidden input field in the contact.php file.
// Values need to be colon separated.
//
function validate(formObj)
{
	var error_str = "";

	// Validate First Name Field
	if ( isRequired(formObj.required_fields, "first_name") )
    {
		if ( isEmpty(formObj.first_name.value) )
		{
			highlightOn("first_name");
			error_str = error_str + "=> Please provide your first name.\n";
		}else{
			highlightOff("first_name");
		}// if
	}// if
	
	// Validate Last Name Field
    if ( isRequired(formObj.required_fields, "last_name") )
    {
	}// if
	
	// Validate Email Field
	if ( isRequired(formObj.required_fields, "email") )
    {
		if ( !validEmail(formObj.email.value) )
		{
			highlightOn("email");
			errors.push("Email address is empty or incorrectly formatted.");
		}else{
			highlightOff("email");
		}// if
	}// if
	
	// Validate Message Field.
	if ( isRequired(formObj.required_fields, "message") )
    {
		if ( isEmpty(formObj.message.value) )
		{
			highlightOn("message");
			error_str = error_str + "=> Please provide a message. Make sure to provide a email address\n";
			error_str = error_str + "       or telephone number if you would like us to respond to you directly.";
		}else{
			highlightOff("message");
		}// if
	}// if
	if(formObj.message.value.indexOf("http") > -1){  
	highlightOn("message");
	  error_str = error_str + "=> Hyperlinks are not allowed in your message. They are considered spam.\n";
	}else{
			highlightOff("message");
		}// if 
	if(formObj.message.value.indexOf("www") > -1){ 
	highlightOn("message");
	   error_str = error_str + "=> Hyperlinks are not allowed in your message. They are considered spam.\n";
}else{
			highlightOff("message");
		}// if
	
	// Show errors or confirm if no errors.
	if ( isEmpty(error_str) )
	{
		return confirm("Send Message?");
	}else{
		
		alert("Please correct the following errors:   \n\n" + error_str);
		return false;
	}// if

}

function validZip(str) 
{
	//	<form name=zip onSubmit="return validateZIP(this.zip.value)">
	var valid = "0123456789-";
	var hyphencount = 0;

	if (str.length!=5 && str.length!=10) return false;
	
	for (var i=0; i < str.length; i++) 
	{
		temp = "" + str.substring(i, i+1);
		if (temp == "-") { hyphencount++; };
		if (valid.indexOf(temp) == "-1") { return false; };
		if ((hyphencount > 1) || ((str.length==10) && "" + str.charAt(5)!="-")) { return false; };
   	}
	
	return true;
}//validZip

function validEmail(str)
{
  var filter=/^.+@.+\..{2,3}$/;
  return (filter.test(str));
}// validEmail

function highlightOn(id,color,weight)
{
	if ( !color ) color = "#FF0000";
	if ( !weight ) weight = "4px";
	
	document.getElementById(id).style.borderColor = color;
	document.getElementById(id).style.borderWidth = weight;
}

function highlightOff(id)
{
	document.getElementById(id).style.borderColor = '';
	document.getElementById(id).style.borderWidth = '';
}

function isEmpty(str)
{
   if ((str.length==0) || (str==null)) return true;
	return false;
}

function isRequired(requiredFieldsObj,fieldName)
{
	var requiredFieldsArr = requiredFieldsObj.value.split(":");

	for(i=0;i<requiredFieldsArr.length;i++)
		if ( requiredFieldsArr[i] == fieldName ) return true;
		
	return false;
}// isRequired


