﻿function Verify_Contact() {



	// set the fields to an array

	var fields = new Array();

	fields[0] = document.getElementById('First_Name');

	fields[1] = document.getElementById('Company_Name');

	fields[2] = document.getElementById('City');

	fields[3] = document.getElementById('Phone');

	fields[4] = document.getElementById('Email');

	fields[5]  = document.getElementById('Comments');

	
	fields[6]  = document.getElementById('code');

	

	// loop each field making sure its correct

	var boolValid = true;

	for (obj in fields) {

		var color = '';

		if (fields[obj].value == '') { 

			color = '#f8de56';

			boolValid = false;

		}				

		fields[obj].style.backgroundColor = color;

	}

	

	// If atleast 1 is bad, error, else validate email

	if (boolValid == false) { 

		alert('Please fill in all fields that are highlighted in yellow');

	}else{

		if (echeck(fields[4].value) != true) { 

			fields[4].style.backgroundColor = '#f8de56';

			boolValid=false;

			alert('You must enter a valid email address');

		

		

		// If atleast 1 is bad, error, else validate email

		}else if (fields[3].value.length < 7) { 

			fields[3].style.backgroundColor = '#f8de56';

			boolValid=false;

			alert('Please enter your phone number in this format [xxxxxxx]');

		}

		

	}

	

		



	

	return boolValid;

	

}



/**

 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)

 */



function echeck(str) {



	var at="@"

		var dot="."

		var lat=str.indexOf(at)

		var lstr=str.length

		var ldot=str.indexOf(dot)

		if (str.indexOf(at)==-1){

		   return false

		}



		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){

		   return false

		}



		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){

		    return false

		}



		 if (str.indexOf(at,(lat+1))!=-1){

		    return false

		 }



		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){

		    return false

		 }



		 if (str.indexOf(dot,(lat+2))==-1){

		    return false

		 }

		

		 if (str.indexOf(" ")!=-1){

		    return false

		 }



 		 return true					

}

