// JavaScript Document

function IsInteger(str,allownull,signed,format)
{
  if (format=='hex') if (allownull) return str.match(/^\s*#?((\d|[A-F]|[a-f])+\s*)?$/); else return str.match(/^\s*#?(\d|[A-F]|[a-f])+\s*$/); 
  	else if (signed) if (allownull) return str.match(/^\s*[-+]?(\d+\s*)?$/); else return str.match(/^\s*[-+]?\d+\s*$/); 
  		else if (allownull) return str.match(/^\s*(\d+\s*)?$/); else return str.match(/^\s*\d+\s*$/); 
}

function validateProsper()
{
       ok = true;
        
		if(document.newLead.firstName.value == '')
       {
	   ok = false;
	   alert("Please enter your First name in the space provided.");
	   return false;
		}
		if(document.newLead.lastName.value == '')
       {
	   ok = false;
	   alert("Please enter your Last name in the space provided.");
	   return false;
		}
		var currEmailVal = document.newLead.email.value;
		currEmailVal = currEmailVal.toLowerCase()

        var emailok = false;
        for(var i = 0; i <= document.newLead.email.value.length; i++){
           if(document.newLead.email.value.charAt(i) == "@"){
           emailok = true;
           }
        }
        var emLength = currEmailVal.length;
        if(currEmailVal.substring(emLength - 3, emLength) != "com" && currEmailVal.substring(emLength - 3, emLength) != "net" &&  currEmailVal.substring(emLength - 3, emLength) != "edu" &&  currEmailVal.substring(emLength - 3, emLength) != "gov" && currEmailVal.substring(emLength - 3, emLength) != "org")
		{
        emailok = false;
		}        
        if(emailok == false){
        alert("Invalid Email Address");
        ok = false;
		return false;
		}


	if (document.newLead.homePhone1.value==''){
		alert("Please enter Home phone");
		return false;
	}
	if (document.newLead.homePhone2.value==''){
		alert("Please enter Home phone");
		return false;
	}
	if (document.newLead.homePhone3.value==''){
		alert("Please enter Home phone");
		return false;
	}
	if (!IsInteger(document.newLead.homePhone1.value)){
		alert("Please enter a valid Home phone number");
	}
	if (!IsInteger(document.newLead.homePhone2.value)){
		alert("Please enter a valid Home phone number");
	}
	if (!IsInteger(document.newLead.homePhone3.value)){
		alert("Please enter a valid Home phone number");
	}
	
        
	if(document.newLead.state.value == '0') {
	   ok = false;
	   alert("Please select the State from the drop down menu on the form.");
	   return false;
		}

		 if(ok){return ok;}
        else{return false;}

}

function stripAlphaChars(pstrSource) 
{ 
var m_strOut = new String(pstrSource); 
    m_strOut = m_strOut.replace(/[^0-9]/g, ''); 

    return m_strOut; 
}

function validPhone(thePhone) {
	if (thePhone==''){
		alert("Please enter phone number");
		return false;
	}

	thePhone = stripAlphaChars(thePhone);
	
	if (!IsInteger(thePhone)){
		alert("Please enter a valid phone number");
		return false;
	}
	
	if (thePhone.length != 10) {
		alert("Please enter a 10 digit phone number including area code");
		return false;
	}
	
	document.newLead2.homePhone1.value = thePhone.substring(0,3);
	document.newLead2.homePhone2.value = thePhone.substring(3,6);
	document.newLead2.homePhone3.value = thePhone.substring(6,10);
	
	return true;
}

function sitetuners_validateProsper()
{
       ok = true;
        
		if(document.newLead2.firstName.value == '')
       {
	   ok = false;
	   alert("Please enter your First name in the space provided.");
	   return false;
		}
		if(document.newLead2.lastName.value == '')
       {
	   ok = false;
	   alert("Please enter your Last name in the space provided.");
	   return false;
		}
        var emailok = false;
        for(var i = 0; i <= document.newLead2.email.value.length; i++){
           if(document.newLead2.email.value.charAt(i) == "@"){
           emailok = true;
           }
        }
        var emLength = document.newLead2.email.value.length;
        if(document.newLead2.email.value.substring(emLength - 3, emLength) != "com" && document.newLead2.email.value.substring(emLength - 3, emLength) != "net" &&  document.newLead2.email.value.substring(emLength - 3, emLength) != "edu" &&  document.newLead2.email.value.substring(emLength - 3, emLength) != "gov" && document.newLead2.email.value.substring(emLength - 3, emLength) != "org")
		{
        emailok = false;
		}        
        if(emailok == false){
        alert("Invalid Email Address");
        ok = false;
		return false;
		}

	if (!validPhone(document.newLead2.phone.value)) {
		return false;
	}

	if(ok) {
		return ok;
	}
	else {
		return false;
	}
}