// function parameters are: field - the string field, count - the field for remaining characters number and max - the maximum number of characters 
function CountLeft(field, count, max) {
// if the length of the string in the input field is greater than the max value, trim it 
if (field.value.length > max)
field.value = field.value.substring(0, max);
else
// calculate the remaining characters 
count.value = max - field.value.length;
}

function isEmail(str) {
	// are regular expressions supported?
	var supported = 0;
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}
	if (!supported) 
		return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
	return (!r1.test(str) && r2.test(str));
}


function isNumeric(strString) {
   //  check for valid numeric strings 		   
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)  {
	  strChar = strString.charAt(i);
	  if (strValidChars.indexOf(strChar) == -1) 
	  {
		 blnResult = false;
	  }
   }
   return blnResult;
}


function validate(theForm) {

	var error = "Please fill in :\n"; 
	
	if (theForm.first_name.value == "")	{
		error += "  First name\n";
	}  
	if (theForm.last_name.value == "")	{
		error += "  Last Name\n";
	}
	if ( ( theForm.preferred_contact[0].checked == false )
    && ( theForm.preferred_contact[1].checked == false ) ){
		error += "  How you would prefer to be contacted\n";
	}
	if ( theForm.how_do_u_hear_us.selectedIndex == 0 )
    {
       error += "  How you heard about us\n";
    }
	if (theForm.reason_for_contact.selectedIndex == 0)	{
		error += "  Your reason for contacting us\n";
	}
	if (theForm.area_code.value ==""||theForm.phone1.value ==""||theForm.phone2.value =="")
	{
		error += "  Telephone\n";
	}else if (isNumeric(theForm.area_code.value) == false||isNumeric(theForm.phone1.value) == false||isNumeric(theForm.phone2.value) == false)
	{
		error += "  Phone Number must be numeric\n";
	}
	if (isEmail(theForm.contact_email.value) == false)
	{
		error += "  E-mail\n";
	} 	
	
	if (isEmail(theForm.verify_email.value) == false)
	{
		error += "  Verify E-mail\n";
	} 	else if (theForm.verify_email.value!=theForm.contact_email.value)
	{
		error += "  Verify E-mail address doesn't match\n";
	}
	
	if (error != "Please fill in :\n")	{
		alert(error);
		return false;
	} else {
		return true;
	}
}
