var xmlHttp
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 checkFields1 () {
	if (document.getElementById("contact_name").value.search(/^ *$/) != -1) {
		alert("Required Field: Name")
		document.getElementById("contact_name").focus()
		return
	}
	if (document.getElementById("contact_email").value.search(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/) == -1) {
		alert("Please enter a valid email address.")
		document.getElementById("contact_email").focus()
		return
	}
	if (document.getElementById("contact_message").value.search(/^\s*$/) != -1) {
		alert("Required Field: Message.")
		document.getElementById("contact_message").focus()
		return
	}
	processContactForm()
}

function processContactForm () {
	xmlHttp = GetXmlHttpObject()
	if (xmlHttp == null) {
		return "Your browser does not support AJAX!"
	}
	var url = "http://www.nytermlimits.com/processContact.php"
	xmlHttp.onreadystatechange = contactStateChanged
	xmlHttp.open("POST" , url , true)
	xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=UTF-8;')
	try {
		xmlHttp.send("name=" + escape(document.getElementById("contact_name").value)
			+ "&email=" + escape(document.getElementById("contact_email").value)
			+ "&message=" + escape(document.getElementById("contact_message").value))
	}
	catch (e) {
		xmlHttp.send(null)
	}
}

function contactStateChanged () { 
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.responseText == "OK") {
			alert("Thank you!\nYour message has been submitted.")
		}
	}
}

function updateCheckboxHidden (myObject, myID) {
	document.getElementById(myID + "Value").value = myObject.checked ? "yes" : "no"
}

function checkFields2 () {
	if (document.getElementById("name").value.search(/^ *$/) != -1) {
		alert("Required Field: Name")
		document.getElementById("name").focus()
		return
	}
	if (document.getElementById("break_email").value.search(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/) == -1) {
		alert("Please enter a valid email address.")
		document.getElementById("break_email").focus()
		return
	}
	if (document.getElementById("state").selectedIndex == 0) {
		alert("Required Field: State.\nResidents outside the USA select the first option: 'Non-USA.'")
		document.getElementById("state").focus()
		return
	}
	if (document.getElementById("NYSpetitionValue").value == "no" && document.getElementById("NYCpetitionValue").value == "no") {
		alert("Please sign at least one of the above Petitions in order to continue.")
		return
	}
	if (document.getElementById("NYCresidentValue").value == "yes")
		document.getElementById("state").value = "NY"
	subscribe2()
}

function subscribe_stateChanged () { 
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.responseText == "OK") {
			alert("Thank you!")
			didSave = true
		}
		else
			document.getElementById("show_result") = xmlHttp.responseText
	}
}

function subscribe2 () {
	xmlHttp = GetXmlHttpObject()
	if (xmlHttp == null) {
		alert("Your browser does not support AJAX!")
		return
	}
	var url = "http://www.nytermlimits.com/subscribe.php"
	xmlHttp.onreadystatechange = subscribe_stateChanged
	xmlHttp.open("POST" , url , true)
    xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=UTF-8;')
	xmlHttp.send(
		"name=" + document.getElementById("name").value.replace(/ /g, "%20") +
		"&email=" + document.getElementById("break_email").value +
		"&state=" + document.getElementById("state").value +
		"&sendUpdateValue=" + document.getElementById("sendUpdateValue").value +
		"&NYCresidentValue=" + document.getElementById("NYCresidentValue").value +
		"&NYCpetitionValue=" + document.getElementById("NYCpetitionValue").value +
		"&NYSpetitionValue=" + document.getElementById("NYSpetitionValue").value
	)
}

function doMail (myObject) {
	if (myObject.selectedIndex > 0)
		window.location = "mailto:" + myObject.options[myObject.selectedIndex].value
}

function poll_stateChanged () {
	if (xmlHttp.readyState == 4) {
		createCookie("voted", 1, 7)
		alert("Thank you for voting!")
		document.getElementById(voteChoice).innerHTML = parseInt(document.getElementById(voteChoice).innerHTML) + 1
		document.getElementById("preVote").style.display = "none"
		document.getElementById("postVote").style.display = ""
	}
}

var voteChoice
function doVote () {
	if (document.getElementById("yesVoteValue").value == "no"
		&& document.getElementById("noVoteValue").value == "no") {
		alert("Please indicate your choice.")
		return
	}
	voteChoice = document.getElementById("yesVoteValue").value == "yes" ? "yesVotes" : "noVotes"
	xmlHttp = GetXmlHttpObject()
	if (xmlHttp == null) {
		alert("Your browser does not support AJAX!")
		return
	}
	var url = "/doPoll.asp"
	xmlHttp.onreadystatechange = poll_stateChanged
	xmlHttp.open("POST" , url , true)
	xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=UTF-8;')
	xmlHttp.send("vote=" + voteChoice)
}
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 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 voteCheck () {
	document.getElementById(readCookie("voted") ? "postVote" : "preVote").style.display = ""
}

sfHover = function () {
	var sfEls = document.getElementById("lgf")
	sfEls.onmouseover = function () { this.className += " sfhover" }
	sfEls.onmouseout = function () { this.className = this.className.replace(new RegExp(" sfhover\\b"), "") }
	var sfEls1 = document.getElementById("lgf1")
	sfEls1.onmouseover = function () { this.className += " sfhover1" }
	sfEls1.onmouseout = function () { this.className = this.className.replace(new RegExp(" sfhover1\\b"), "") }
}
if (window.attachEvent)
	window.attachEvent("onload", sfHover);
