var http;
var aboutMenu = false;

	function sendAnnotatedMailTo(one, two, domain, subject, body) {
		   	locationstring = 'mai' + 'lto:' + two + '@' + one + '.' + domain + "?subject=" + escape(subject) + "&body=" + escape(body);
	   		window.location.replace(locationstring);
	   }

	
	  
	
	   function showAbout(x) {   
	   
	   if ((aboutMenu === false) && (x == "trigger")) {}
	   
	   else if ( ((aboutMenu === false) && (x != "trigger")) || ((aboutMenu === true) && (x == "trigger")) ) {
	           
	       x = document.getElementById('lpkLogo').offsetLeft;
	       
	       document.getElementById('aboutLPK').style.left = (x-45) + "px";
	       
	       document.getElementById('aboutLPK').style.zIndex="600";
	       
	       document.getElementById('aboutLPK').style.display = "block";
	   	       
	       aboutMenu = true;
	       
	       }
	       
	       else {
	       
	       document.getElementById('aboutLPK').style.display = "none";
	       
	       }
	       
	   
	   }
	   
	   function closeAbout() {
	   
	       document.getElementById('aboutLPK').style.display = "none";
	       aboutMenu = false;
	   
	   }

 function checkForm() {	   
	 
	   if ((document.getElementById("email").value == "") && (document.getElementById("phone").value == "")) {
	       
	           alert("We hate to nag, but we can't get in touch without a correct email address or phone number. Thanks!");
	           
	           document.getElementById("phone").focus(); return false;                 
	       
	       }
	  

	     
	     if ((document.getElementById("phone").value.length != "13") && (document.getElementById("email").value == "")) {
	     
	    	 alert("We hate to nag, but we can't get in touch without a correct email address or phone number. Thanks!");
	           
            formEdit("phone");
            
            return false;
	     
	     
	     }
	     
	  	  var pattern=/(^[ \-_\.a-zA-Z0-9]+)@((([0-9]{1,3}\.){3}([0-9]{1,3})((:[0-9])*))|(([a-zA-Z0-9\-]+)(\.[a-zA-Z]{2,})+(\.[a-zA-Z]{2})?((:[0-9])*)))/; 

	     
	      if ((document.getElementById("phone").value == "") && (document.getElementById("email").value.search(pattern) == -1)) {
	     
	    	 alert("We hate to nag, but we can't get in touch without a correct email address or phone number. Thanks!");
	           
            formEdit("email");
            
            return false;
	     
	     
	     }
	     
	     
	     
	       
	     else { contactUs(); return false; }
	   
	   }

function createAJAXObject() {
    var ajax = false;
    if (window.XMLHttpRequest) { ajax = new XMLHttpRequest(); }
    else if (window.ActiveXObject) {
        try {
            ajax = new ActiveXObject("Microsoft.XMLHTTP");
          } catch(e) {
            try {
                  ajax = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(e) {
                  ajax = false;
            }
        }
    }
    
    return ajax;
}

function doAJAXWork( data_send, cbfunction ) {

	

    var toSend = "";
    if( typeof data_send == "object" ) {
        for( var i = 0; i < data_send.length; i++ ) {
            if( typeof data_send[i] == "object" ) {
                toSend += data_send[i][0] + "=" + escape( data_send[i][1] ) + "&";
            } else {
                toSend += data_send[i] + "&";
            }
        }
    } else {
        toSend += data_send;
    }
    
    if( toSend.lastIndexOf( "&" ) == ( toSend.length - 1 ) ) toSend = toSend.substring( 0, toSend.length - 1 ); // remove last '&'
    
    
    
    http.open( "get", "write.php?" + toSend, true );
    http.send(null);
    http.onreadystatechange = function() {
        if ( http.readyState == 4 ) {
            if( http.status == 200 ) {
                cbfunction();
            } else {
                throw http.statusText;
            }
        }
    };
}

function contactUs() {

    //if( !http ) return true;
    
    var s_greenlight = document.getElementById( "firstname" ).value +"**"+ document.getElementById( "phone" ).value+"**"+document.getElementById( "email" ).value;
               
    try {
        doAJAXWork( [ [ "data", s_greenlight ] ], process_result );
    } catch( e ) {
        alert( e );
    }
    
   	document.getElementById('formArea').innerHTML = "Processing...";

    return false;
    
}


function process_result() {

	//alert(http.responseText);

    if( http.responseText.indexOf("success")!=-1 ) {
    
		document.getElementById('formArea').innerHTML = 
		
			"<div id=\"thankyou\"><div id=\"formHeaderText\">Thanks for contacting us.</div><p>If you have any immediate needs or questions, feel free to contact us.</p><p>We look forward to speaking with you.</p> <b>Call our sales desk:</b><br />718-989-1414<p><b>or, Email us at:</b><br /><a href=\"javascript:sendAnnotatedMailTo('landingpageking','questions','com','Landing Page King','')\" name=\"contact\" id=\"contact2\">questions@landingpageking.com</a></div></div>";
          
    } else {
        
		document.getElementById('formArea').innerHTML = "Huh?";

    }
}

function maskPhone(f){ 

	if (f.value =="") {}
	
	else {
	
	workCopy = f.value.replace( /\D/g, '' );

	document.getElementById('phone').setAttribute('maxlength', '13');

	tel='('; 
	var val = workCopy.split(''); 
	
	if (val[0] == "1") { val.shift(); }
	
	for(var i=0;i<val.length;i++){ 
	if(i==2){val[i]=val[i]+')'} 
	if(i==5){val[i]=val[i]+'-'} 
	tel=tel+val[i] 
	}

	f.value=tel; 

	f.disabled = true;

	document.getElementById('emailHelper').innerHTML = "<a href=\"javascript: formEdit('phone');\">Click here to edit your phone number</a>";
	
	}
	
}


function formEdit(x) {

	if (x == "phone") { document.getElementById('emailHelper').innerHTML = "Just the numbers. No \( \) or \- needed."; }
	
	document.getElementById(x).disabled = false;
	
	document.getElementById(x).value = "";
	
	document.getElementById(x).focus();

}

function checkKey(e) {

	if (!e) var e = window.event;
	var charCode = e.which || e.keyCode;

	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;

}


window.onload = function() {
    http = createAJAXObject();
}