var okMessage = 'Thanks for signing up for email updates. To take immediate action, help <a href="http://push.pickensplan.com/">Push the Plan</a> now!';
var errorMessage = 'We already have your email address, thank you! Get involved now and help us <a href="http://push.pickensplan.com/">Push the Plan!</a>';

function submitform(theForm) {
	var isValid = validate(theForm);
	
	if (isValid) {
		return startajax(theForm);
	}
	
	return false;
}

function submitFormWithRedirect(theForm) {
    var isValid = CheckSignupForm(theForm);
	
	if (isValid) {
		ajaxSubmit(theForm);
	}	
	
	return false;
}

function ajaxhttp()
{
   try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
   try { return new XMLHttpRequest(); } catch(e) {}
   alert("XMLHttpRequest not supported");
   return null;
}

function startajax(theForm)
{
	var firstname = "First" /*theForm.firstname.value*/;
	var lastname = "Last" /*theForm.lastname.value*/;
	var email = theForm.email.value;
	var zip = theForm.zipcode.value;
	var mobile =  "000-000-0000" /*theForm.phone.value*/;
	var appid = theForm.appid.value;
	
    var url = "/inc/freq.php";
				
	/* optional zip code option - this is a temporary fix. TODO: fix it php code */
				
	if ((zip==null)||(zip.length==0))
		zip = "00000";
				
	var params = "appid=" + escape(appid) + "&" 
				+ "firstname=" + escape(firstname) + "&" 
				+ "lastname=" + escape(lastname) + "&"
				+ "email=" + escape(email) + "&" 
				+ "zip=" + escape(zip) + "&" 
				+ "mobile=" + escape(mobile);
						
    var xmlhttp = ajaxhttp();
	
	xmlhttp.open("POST", url, true);
	
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");

	xmlhttp.send(params);
	
    xmlhttp.onreadystatechange = function(){ajaxstatechange(xmlhttp)};
	
	return false;
}


function ajaxSubmit(theForm) {
	var firstname = theForm.first_name.value;
	var lastname = theForm.last_name.value;
	var email = theForm.email_address.value;
	var zip = theForm.zip.value;
	var mobile =  "000-000-0000" /*theForm.phone.value*/;
	var appid = theForm.appid.value;
	
    var url = "/inc/freq.php";
				
	/* optional zip code option - this is a temporary fix. TODO: fix it php code */
				
	if ((zip==null)||(zip.length==0))
		zip = "00000";
				
	var params = "appid=" + escape(appid) + "&" 
				+ "firstname=" + escape(firstname) + "&" 
				+ "lastname=" + escape(lastname) + "&"
				+ "email=" + escape(email) + "&" 
				+ "zip=" + escape(zip) + "&" 
				+ "mobile=" + escape(mobile);
						
    var xmlhttp = ajaxhttp();
	
	xmlhttp.open("POST", url, true);
	
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");

	xmlhttp.send(params);
	
	xmlhttp.onreadystatechange = function(){redirectreponse(xmlhttp)};
	
	return false;
}


function redirectreponse(xmlhttp)
{
	if (xmlhttp.readyState == 4)
	{
		window.location = "/act/thankyou.php";
	}
}



function ajaxstatechange(xmlhttp)
{
    if (xmlhttp.readyState == 4) {
        displayResult(xmlhttp.responseText);
        //conversionTracking();
        //googleConversionTracking();
		//microsoftConversionTracking();
		new MercuryTracker('http://mtracker.addthree.com', 1).track();
    }
}

function processJoinXMLResponse(xmlMessage)
{
	var startIndex, endIndex;
	var msg;
	
	startIndex = xmlMessage.indexOf("Status");
	
	if (startIndex <= 0)
		return;
	
	endIndex = xmlMessage.indexOf("/Status");
	
	var status = xmlMessage.substring(startIndex+7, endIndex-1);
	
	if (status == "OK")
		return true;
	else
		return false;
}

function displayResult(xmlMessage) 
{	
	var ok = processJoinXMLMessage(xmlMessage);
	var msg;
	
	if(ok)
		msg = okMessage;
	else
		msg = errorMessage;
	
	displayMessage(msg);
}

function displayMessage(message) {
	var msgNode = document.getElementById('response');
	var formNode = document.getElementById('signupForm');
	
	formNode.style.display = "none";
	formNode.visibility = "hidden";
	
	msgNode.innerHTML = message;
}

function conversionTracking() {
// Yahoo! Inc.
// Event Type ID: 3
// Segment Type ID: 1
if (typeof(window.ysm_customData) != 'object') window.ysm_customData = new Object();
window.ysm_customData.segment_1QTG7DM7DRGGJO = "event=1,transId=,currency=,amount=";

}

// Google Code for Email Sign-Ups Conversion Page
function googleConversionTracking() {
var google_conversion_id = 1048421341;
var google_conversion_language = "en_US";
var google_conversion_format = "1";
var google_conversion_color = "ffffff";
if (5.00) {
 var google_conversion_value = 5.00;
}
var google_conversion_label = "X80BCIXsVRDdx_bzAw";
}

function microsoftConversionTracking() {
	microsoft_adcenterconversion_domainid = 52830;
	microsoft_adcenterconversion_cp = 5050;
}