//////////////////////////////////////////////////////////////////////
/*				FIRST MAKE SURE URL HAS WWW, FOR XML				*/
//////////////////////////////////////////////////////////////////////

if ("http://www.ericcantorforvp.com/".search(/www/) != -1 && window.location.href.search(/^https?:\/\/www/) == -1)
	window.location = window.location.href.replace(/^(https?:\/\/)(.*)$/, "$1" + "www." + "$2")

//////////////////////////////////////////////////
/*				GENERIC FUNCTIONS				*/
//////////////////////////////////////////////////


function createCookie (name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = ";expires=" + date.toGMTString();
	}
	else
		var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie (name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0)==' ')
			c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0)
			return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function eraseCookie (name) {
	createCookie(name, "", -1);
}

function numbersOnly (myField, e, decimal) {
	 var key, keyChar
	 if (window.event)
		 key = window.event.keyCode
	 else if (e)
		 key = e.which
	 else
		 return true
	 keyChar = String.fromCharCode(key)
	 // control keys
	 if ((key == null) || (key == 0) || (key == 8) || (key == 9) || (key == 13) || (key == 27))
		 return true
	 // numbers
	 else if (("0123456789").indexOf(keyChar) > -1)
		 return true
	// decimal point jump -- jump to the next field. code not working at this point.
	 else if (decimal && (keyChar == ".")) {
		myField.form.elements[decimal].focus()
		return false
	 }
	 else
		return false
}

function loadAttacher (loadFunc) {
	if (window.addEventListener) {
		window.addEventListener("load", loadFunc, false)
	}
	else if (document.addEventListener) {
		document.addEventListener("load", loadFunc, false)
	}
	else if (window.attachEvent) {
		window.attachEvent("onload", loadFunc)
	}
}

function noContextPaste (myID) {
	var myObject = document.getElementById(myID)
	if (browserType() == "IE") {
		myObject.oncontextmenu = MSnoContextPaste
	}
	else {
		myObject.addEventListener("contextmenu", FFnoContextPaste, true)
	}
}

function browserType () {
	if (found(navigator.appName, "Microsoft"))
		return "IE"
	if (navigator.userAgent.search(/Safari/i) != -1)
		return "Safari"
	if (navigator.userAgent.search(/Firefox/i) != -1)
		return "FF"
	return "FF"
}

function GetXmlHttpObject () {
	var xmlHttp = null
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest()
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")
		}
		catch (e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
		}
	}
	return xmlHttp
}

function found (haystack, needle) {
	return haystack.search(needle) != -1
}

function empty (value) {
	return found(value, /^\s*$/)
}

function isObject (myID) {
	return (document.getElementById(myID) + "").search(/^\[object.*\]$/) != -1
}

function isnull (arg) {
	arg = arg + '';
	return (arg == '' || arg == 'null' || arg == 'undefined')
}

function removeWhiteSpace (myObject) {
	setValue(myObject.id, myObject.value.replace(/(^ *| *$)/g, "").replace(/ +/g, " "))
}

function simpleCheck (id, pattern, message) {
	myValue = getValue(id)
	if (empty(myValue))
		return ""
	return found(myValue, pattern) ? "" : message
}

function validate_email (id) {
	return simpleCheck(id, /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/, "Please enter a valid email address.")
}

function getValue (id) {
	return document.getElementById(id).value
}

function touchdown (myInput, defaultText) {
	if (myInput.value == defaultText) {
		myInput.style.textAlign = "left"
		myInput.value = ""
	}
}

function leaveField (myInput, defaultText) {
	if (myInput.value == "") {
		myInput.value = defaultText
	}
}

var xmlHttp

function resize (myID, newWidth) {
	myEmbeds = document.getElementsByTagName("embed")
	for (i = 0; i < myEmbeds.length; i++) {
		if (myEmbeds[i].src.search(/youtube/i) == -1)
			continue
		currentEmbed = myEmbeds[i]
		if (!isnull(currentEmbed.style.width)) {
			currentWidth = parseInt(currentEmbed.style.width)
			currentHeight = parseInt(currentEmbed.style.height)
			newHeight = newWidth / currentWidth * currentHeight
			currentEmbed.style.width = newWidth
			currentEmbed.style.height = newHeight
		}
		else if (!isnull(currentEmbed.width)) {
			currentWidth = parseInt(currentEmbed.width)
			currentHeight = parseInt(currentEmbed.height)
			newHeight = newWidth / currentWidth * currentHeight
			currentEmbed.width = newWidth
			currentEmbed.height = newHeight
		}
	}
	document.getElementById(myID).style.display = ""
}


//////////////////////////////////////////////////
/*		GENERAL FUNCTIONS FOR THIS PROJECT		*/
//////////////////////////////////////////////////


//////////////////////////////////////////////////
/*				FORM FUNCTIONS					*/
//////////////////////////////////////////////////


if (window.location.href.search(/register/) == -1 && !readCookie("email") && !readCookie("skip")) {
	newLocation = "http://www.ericcantorforvp.com/register.php"
	if (window.location.href.search(/\?/) != -1)
		newLocation += window.location.href.replace(/^.*(\?.*)$/, "$1")
	window.location = newLocation
}

function skipRegistration () {
	createCookie("skip", "YES", 200)
	window.location = "http://www.ericcantorforvp.com/index.php" + (document.getElementById("referrer").value == "" ? "" : ("?petitionemail=" + document.getElementById("referrer").value))
}

var petitionList = 1
var registerList = 2
var status

function submitRegistrationForm () {
	if (empty(getValue("email")) || validate_email("email") != "") {
		alert("Please enter a valid email address.")
		document.getElementById("email").focus()
		return
	}
	if (document.getElementById("state").selectedIndex == 0) {
		alert("Please select a state.")
		document.getElementById("state").focus()
		return
	}
	if (getValue("zipcode").search(/[0-9]{5}/) == -1) {
		alert("Please enter a valid zip code.")
		document.getElementById("zipcode").focus()
		return
	}
	document.getElementById("enterTD").innerHTML = "Please wait..."
	createCookie("email", getValue("email"), 200)
	createCookie("state", getValue("state"), 200)
	createCookie("zipcode", getValue("zipcode"), 200)
	status = registerList
	subscribe_12all()
}

function submitPetitionForm () {
	if (empty(getValue("email")) || validate_email("email") != "") {
		alert("Please enter a valid email address.")
		document.getElementById("email").focus()
		return
	}
	if (document.getElementById("state").selectedIndex == 0) {
		alert("Please select a state.")
		document.getElementById("state").focus()
		return
	}
	if (getValue("zipcode").search(/[0-9]{5}/) == -1) {
		alert("Please enter a valid zip code.")
		document.getElementById("zipcode").focus()
		return
	}
	document.getElementById("signTD").innerHTML = "Please wait..."
	createCookie("petition", "yes", 200)
	status = petitionList
	subscribe_12all()
}

function fix_url (url) {
	if (window.location.href.search(/^http:\/\/www\./) != -1 && url.search(/^http:\/\/www\./) == -1) {
		url = url.replace(/^http:\/\//, "http://www.")
	}
	if (window.location.href.search(/^http:\/\/www\./) == -1 && url.search(/^http:\/\/www\./) != -1) {
		url = url.replace(/^http:\/\/www\./, "http://")
	}
	return url
}

function subscribe_12all () {
	xmlHttp = GetXmlHttpObject()
	if (xmlHttp == null) {
		return "Your browser does not support AJAX!"
	}
	var url = "http://www.ericcantorforvp.com/subscribe.php"
	url = fix_url(url)
	xmlHttp.onreadystatechange = stateChanged
	xmlHttp.open("POST" , url , true)
	xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=UTF-8;')
	try {
		xmlHttp.send(
			"email=" + escape(document.getElementById("email").value)
			+ "&zipcode=" + escape(document.getElementById("zipcode").value)
			+ ((status == petitionList && isObject("referrer") && !empty(getValue("referrer"))) ? ("&referrer=" + escape(document.getElementById("referrer").value)) : "")
			+ "&state=" + escape(document.getElementById("state").value)
			+ "&list=" + status
		)
	}
	catch (e) {
		xmlHttp.send(null)
	}
}

function stateChanged () { 
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.responseText == "OK") {
			if (status == petitionList)
				alert("Thank you for signing the EricCantorForVP.com petition")
			else {
				alert("Thank you for registering with EricCantorForVP.com")
				window.location = "index.php"
			}
		}
		else
			alert("There was a problem.")
	}
}

function prefillPetition () {
	if (isObject("petition_form")) {
		if (readCookie("email") && !readCookie("petition")) {
			document.getElementById("email").value = readCookie("email")
			document.getElementById("state").value = readCookie("state")
			document.getElementById("zipcode").value = readCookie("zipcode")
			if (isObject("sign_now1")) {
				document.getElementById("sign_now1").style.display = ""
				document.getElementById("signTD1").id = "signTD"
			}
		}
		else
			if (isObject("sign_now2")) {
				document.getElementById("sign_now2").style.display = ""
				document.getElementById("signTD2").id = "signTD"
			}
	}
}

loadAttacher(prefillPetition)
