function test(src) {
     var emailReg = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
     var regex = new RegExp(emailReg);
     return regex.test(src);
  }

function highlight(obj) {
if ((document.all)||(document.getElementById)) {obj.style.backgroundColor = '#FF8888';}
}

function  dehighlight(obj) {
if ((document.all)||(document.getElementById)) {obj.style.backgroundColor = '#FFFFFF';}
}

function Validate(theForm)
{
  var errorstring ="";
  var Stage_string="";
  dehighlight(theForm.email);
  dehighlight(theForm.First_Name);
  dehighlight(theForm.Last_Name);
  dehighlight(theForm.Job_Title);
  dehighlight(theForm.Company);
  dehighlight(theForm.Address1);
  dehighlight(theForm.City);
  dehighlight(theForm.State);
  dehighlight(theForm.Zip);
  dehighlight(theForm.phone_number);
  dehighlight(theForm.DirectDial);
  dehighlight(theForm.role);
  //for(i=0;i<4;i++){dehighlight(theForm.Stages[i]);}

if (test(theForm.email.value)==false)
 {
 errorstring+="Required Field: Email Address is invalid.\n";
 highlight(theForm.email);
 }

  if (theForm.First_Name.value == "")
  {
  errorstring+="Required Field: First Name has not been completed.\n";
  highlight(theForm.First_Name);
  }

  if (theForm.Last_Name.value == "")
  {
  errorstring+="Required Field: Last Name has not been completed.\n";
  highlight(theForm.Last_Name);
  }

  if (theForm.Job_Title.value == "")
  {
  errorstring+="Required Field: Job Title has not been completed.\n";
  highlight(theForm.Job_Title);
  }

  if (theForm.Company.value == "")
  {
    errorstring+="Required Field: Company Name has not been completed.\n";
	highlight(theForm.Company);
  }

  if (theForm.Address1.value == "")
  {
    errorstring+="Required Field: Address has not been completed.\n";
	highlight(theForm.Address1);
  }

  if (theForm.City.value == "")
  {
    errorstring+="Required Field: City has not been completed.\n";
	highlight(theForm.City);
  }

  if (theForm.State.value == "" && theForm.Country.value=="USA")
  {
    errorstring+="Required Field: State has not been selected.\n";
	highlight(theForm.State);
  }

  if (theForm.State.value == "" && theForm.Country.value=="Canada")
  {
    errorstring+="Required Field: Province has not been selected.\n";
	highlight(theForm.State);
  }

  if (theForm.Zip.value == "")
  {
    errorstring+="Required Field: Zip Code has not been completed.\n";
	highlight(theForm.Zip);
  }

  if (theForm.Zip.value != "" && theForm.Zip.value.search(/^(\d{5}(( |-)\d{4})?)$/)==-1 && theForm.Country.value=="USA")
  {
    errorstring+="Required Field: Zip Code is not in a valid US Zip Code format\n";
	highlight(theForm.Zip);
  }

  if (theForm.Zip.value.search(/^\D{1}\d{1}\D{1}(( |-)?\d{1}\D{1}\d{1})$/)==-1 && theForm.Country.value=="Canada")
  {
    errorstring+="Required Field: Zip Code is not in a valid Canadian Zip Code format\n";
	highlight(theForm.Zip);
  }

  if (theForm.phone_number.value == "")
  {
  errorstring+="Required Field: Phone Number has not been completed.\n";
  highlight(theForm.phone_number);
  }

 if (theForm.Country.value!="USA" && theForm.Country.value!="Canada" && theForm.DirectDial.value=="")
  {
  errorstring+="Required Field: Please select a Direct Dialing Code.\n";
  highlight(theForm.DirectDial);
  }

  if (theForm.role.value=="")
  {
  errorstring+="Please let us know what your primary role within the payment card industry is.\n";
  highlight(theForm.role);
  }
  //if (!theForm.Stages[0].checked && !theForm.Stages[1].checked && !theForm.Stages[2].checked && !theForm.Stages[3].checked)
		//{
		//errorstring+="Please inform us of what decision making stage you are in.\n";
		//for(i=0;i<4;i++){highlight(theForm.Stages[i]);}
		//}

if (errorstring=="")
{
    if(theForm.EMAIL_PERM.checked)
	{theForm.EMAIL_PERMISSION.value="Yes!"}
	else
	{theForm.EMAIL_PERMISSION.value="NO"}

if ((theForm.Country.value!="USA")&&(theForm.Country.value!="Canada"))
   {
    theForm.phone_number.value = "+" + theForm.DirectDial.value + " " + theForm.phone_number.value
   }

if (theForm.role_other.value != "")
  {
  theForm.comments.value = theForm.role.value + " " +theForm.role_other.value
  }else{theForm.comments.value = theForm.role.value}
 
 theForm.State.options[0].value = "xx";   

//combine all checkboxes into a multivalue field
		//for (i = 0;i<4;i++){
		//if(theForm.Stages[i].checked)
		//Stage_string = Stage_string + theForm.Stages[i].value + ", ";
		//}
//Stage_string = Stage_string.substring(0,(Stage_string.length-2))
//theForm.Stage.value=Stage_string;

//LEAD LABELING
theForm.source1.value = getEngine(theForm);
theForm.source2.value = getKeyWord(theForm);

//SET BACK TO TRUE
return(true);
}
else{
    alert("We're sorry, the following errors occured while processing your form:\n\n"+ errorstring + "\nThe above trouble areas have been highlighted for you, thank you for your cooperation.");
	return(false);
	}
}
