var xmlHttpRequest;

function init()
{
	if (window.XMLHttpRequest)
	{
	   return new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
	   return new ActiveXObject("Microsoft.XMLHTTP");
    }
}

function validateCaptchaId()
{
	//alert("Validating captcha") ;
	xmlHttpRequest=init();
	var captchaId=document.getElementById("captchaId");
	var appendUrl="?captchaId="+captchaId.value;
	//alert("appendURL is "+appendUrl) ;
	xmlHttpRequest.open("GET", "http://samepage.etouch.net/forms/validatecaptcha"+appendUrl, true);
	xmlHttpRequest.onreadystatechange=processRequest;
	xmlHttpRequest.send(null); 
}

function processRequest()
{
	if(xmlHttpRequest.readyState==4)
	{
	   if(xmlHttpRequest.status==200)
	   {
	      processResponse();
       }
    }
}

function processResponse()
{

	var xmlMessage=xmlHttpRequest.responseXML;

	var valid=xmlMessage.getElementsByTagName("valid")[0].firstChild.nodeValue;

	if(valid=="true")
	{		
	}
	if(valid=="false")
	{
		alert("Image Verification Text is incorrect.");		
		document.contactForm.captchaText.value='';
		document.contactForm.captchaText.focus();
		history.go(0);
	}
 }

function submitContactForm()
{
	if(document.contactForm.firstName.value == '')
	{		
		alert("Please enter a valid First Name.");
		document.contactForm.firstName.focus();
		return false;
	}
	else if (document.contactForm.lastName.value == '')
	{
		alert("Please enter a valid Last Name.");
		document.contactForm.lastName.focus();
		return false;
	}
	else if (document.contactForm.email.value == '')
	{
			alert("Please enter a valid Email Address.");
			document.contactForm.email.focus();
			return false;
	}
	else if (echeck(document.contactForm.email.value)==false)
	{
			//document.contactForm.email.value="";
			document.contactForm.email.focus();
			return false;
	}	
	else if (document.contactForm.company.value == '')
	{
		alert("Please enter a valid Company Name.");
		document.contactForm.company.focus();
		return false;
	}
	else if (document.contactForm.sPhone.value == '')
	{
		alert("Please enter a valid Phone Number.");
		document.contactForm.company.focus();
		return false;
	}
	else if(validatePhone(document.contactForm.sPhone) == false)
	{
			document.contactForm.sPhone.focus();
			return false;
	}
	else if (document.contactForm.captchaText.value == '')
	{
		alert("Please enter the Image Verification Text");
		document.contactForm.captchaText.focus();
		return false;
	}

	document.contactForm.submit();

}

function echeck(str) {

		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		var domainstr = str.substring(lat+1,ldot);

		var invalidaddress=new Array(); // list of blocked emails
		invalidaddress[0]="hotmail";
		invalidaddress[1]="yahoo";
		invalidaddress[2]="rocketmail";

		for(i=0; i < invalidaddress.length; i++)
		{
			if ( domainstr == invalidaddress[i])
			{
				alert("Please use your corporate email address instead of web email addresses like hotmail or yahoo.");
				return false;
			}
		}

		if (str.indexOf(at)==-1){
		   alert("Please enter a valid Email Address.");
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Please enter a valid Email Address.");
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Please enter a valid Email Address.");
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Please enter a valid Email Address.");
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Please enter a valid Email Address.");
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Please enter a valid Email Address.");
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Please enter a valid Email Address.");
		    return false;
		 }
		 
		 return true;					
	}

function validatePhone(textfield) {
     phoneOK = true; // phoneOK is GLOBAL
     var digits = 0;
     for (var i=0; i < textfield.value.length; i++) {
        var theChar = textfield.value.charAt(i);
        if ((theChar >= "0") && (theChar <= "9")) {
           digits++;
           continue;
        }
        if (theChar == " ") continue;
        if (theChar == "-") continue;
        if (theChar == "(") continue;
        if (theChar == ")") continue;
		if (theChar == "+") continue;
        phoneOK = false;
     }

     phoneOK = phoneOK;
     if (!phoneOK) {
        alert("Phone number can contain numbers and optionally these characters: +,-,(,)");
        textfield.focus();
        textfield.select();
     }
     return phoneOK;
  }

  function submitRetrieveForm()
{
	document.retrieveForm.form_type.value=document.retrieveForm.retrieve.value;
	document.retrieveForm.fetch_records.value=document.retrieveForm.records.value;
	document.retrieveForm.submit();
}

  function submitDeleteForm(reqid)
{
	//document.displayForm.firstName.value=firstName;
	//document.displayForm.lastName.value=lastName;
	//document.displayForm.email.value=email;
	document.displayForm.reqid.value=reqid;
	document.displayForm.action='/forms/delete';
	if (confirm('Are you sure you want to delete?'))
	{
		document.displayForm.submit();
	}
}
	
  function submitSFForm(id,firstName,lastName,email,company,phone,description,formType)
{
	var urlstr='/forms/salesforce?sf_first_name='+firstName+'&sf_last_name='+lastName+'&sf_email='+email+'&sf_company='+company+'&sf_phone='+phone+'&sf_description='+description+'&formType='+formType+'&sf_id='+ id;
	window.open (urlstr,"SF","status=0,toolbar=1,resizable=1"); 
}

function refreshReport(formType, fetchRecords)
{
	var url = window.location.href;
	if (url.indexOf("?") == -1)
	{
		url = url +'?form_type='+formType+'&fetch_records='+ fetchRecords;	
	}
	window.location.href = url;
}

//function openFile() {
//	var openFileName = '/forms/UY65_Ttyr799arTjRb/Aberdeen_Sales_2_Benchmark_Report.pdf';
//	window.open (openFileName,"Report","status=0,toolbar=1,resizable=1"); 

//}
  function enableSFForm(id,firstName,lastName,email,company,phone,description,formType)
{
	var urlstr='/forms/enablesalesforce?sf_id='+ id+'&formType='+formType;
	window.open (urlstr,"ESF","status=0,toolbar=1,resizable=1"); 
}
