var lgfs = Array()
var nVoices = 0

/*	FOR DROPDOWN MENU	*/

sfHover = function () {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i = 0; i < sfEls.length; i++) {
		sfEls[i].onmouseover = function () {
			this.className += " sfhover";
		}
		sfEls[i].onmouseout = function () {
			this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}

/*	FOR ALL TEXT INPUT FIELDS	*/

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

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

/*	FOR LGF EFFECT	*/

function init () {
	lgfs.push("petition")
	var i
	for (i = 1; i <= nVoices; i++) {
		lgfs.push("voices" + i)
	}
	if(document.addEventListener) { // Safari or Konqueror or WebKit-based
		window.addEventListener("mousemove" , MovesInWebKit , true);
	}
}

function doLGF (myObject, myBoolean) {
	document.getElementById("lgf_" + myObject.id).style.display = myBoolean
}

function all_lgfs (event) {
	var i
	for (i = 0; i < lgfs.length; i++) {
		if (document.getElementById("lgf_" + lgfs[i].replace(/lgf_/, "")).style.display == "block") {
			document.getElementById("lgf_" + lgfs[i]).style.left = event.x + 15
			document.getElementById("lgf_" + lgfs[i]).style.top = event.y + 15 + document.body.scrollTop
		}
	}
}

function MovesInWebKit (evt) {
	var i
	for (i = 0; i < lgfs.length; i++) {
		if (document.getElementById("lgf_" + lgfs[i].replace(/lgf_/, "")).style.display == "block") {
			document.getElementById("lgf_" + lgfs[i]).style.left = evt.clientX + 15
			document.getElementById("lgf_" + lgfs[i]).style.top = evt.clientY + 15 + document.body.scrollTop
		}
	}
}

/*	FOR AJAX	*/

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;
}

/*	FOR READ THE TRUTH DROPDOWNS	*/

function doChange (mySelect) {
	if (mySelect.selectedIndex == 0)
		return
	myOption = mySelect.options[mySelect.selectedIndex]
	window.open(myOption.value)
}

/*	FOR REFER A FRIEND	*/

function validate_emailList (id) {
	document.getElementById(id).value = document.getElementById(id).value.replace
		(/;/g, ",").replace(
		/ *, */g, ",").replace(/[, ]$/, "").replace(/, *,/g, ",").replace(/^ */, "")
	emailList = document.getElementById(id).value.split(",")
	if (emailList.length > 10)
		return "You may not enter more than 10 email addresses."
	for (i = 0; i < emailList.length; i++)
		if (emailList[i].search(
			/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/) == -1)
			return "The email address" + "\n" + emailList[i] + "\n" + "is invalid. Please check your entry and try again."
	return ""
}

function checkFields_refer () {
	if (document.getElementById("refer_yourEmail").value.search(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/) == -1) {
		alert("Please enter a valid email address.")
		document.getElementById("refer_yourEmail").focus()
		return
	}
	if (result = validate_emailList("refer_friendEmail")) {
		alert(result)
		document.getElementById("refer_friendEmail").focus()
		return
	}
	recommend()
}

function friend_stateChanged () { 
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.responseText == "OK") {
			alert("Thank you. A link has been sent to the address you entered.")
		}
		else {
			document.getElementById("show_xml").innerHTML = xmlHttp.responseText
		}
	}
}

function recommend () {
	xmlHttp = GetXmlHttpObject()
	if (xmlHttp == null) {
		alert("Your browser does not support AJAX!")
		return
	}
	var url = MTBlogURL + "mail.php"
	xmlHttp.onreadystatechange = friend_stateChanged
	xmlHttp.open("POST" , url , true)
	xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=UTF-8;')
	xmlHttp.send("your_email=" + document.getElementById("refer_yourEmail").value + "&friend_email=" +
		document.getElementById("refer_friendEmail").value + "&url=" + window.location.href +
		"&blogTitle=" + document.title.replace(/ /g, "xyzzyx"))
}

/*	FOR SIGN PETITION	*/

function checkFields_petition () {
	if (document.getElementById("petition_email").value.search(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/) == -1) {
		alert("Please enter a valid email address.")
		document.getElementById("petition_email").focus()
		return
	}
	join_petition()
}

function join_petition () {
	xmlHttp = GetXmlHttpObject()
	if (xmlHttp == null) {
		alert("Your browser does not support AJAX!")
		return
	}
	var url = MTBlogURL + "subscribe.php"
	xmlHttp.onreadystatechange = petition_stateChanged
	xmlHttp.open("POST" , url , true)
	xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=UTF-8;')
	xmlHttp.send("email=" + document.getElementById("petition_email").value +
		"&petition=yes&updates=" + (document.getElementById("updates").checked ? "yes" : "no") +
		"&countryCode=" + document.getElementById("petition_country").value +
		"&cellphone=" + document.getElementById("petition_cellphone").value)
}

function petition_stateChanged () { 
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.responseText == "OK") {
			alert("Thank you for your signature.")
		}
		else
			document.getElementById("show_xml").innerHTML = xmlHttp.responseText
	}
}

/*	FOR STAY CONNECTED */

function checkFields_connected () {
	if (document.getElementById("connected_email").value.search(/^(\*|Your Email here)$/) != -1 &&
		(document.getElementById("join_cellphone").value.search(/^(\*|Your Cellphone here)$/) != -1
		|| document.getElementById("join_country").selectedIndex == 0)) {
		alert("Please enter country and cellphone, or enter email address.")
		document.getElementById("join_country").focus()
		return
	}
	if (document.getElementById("connected_email").value.search(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/) == -1
		&& document.getElementById("connected_email").value.search(/^(\*|Your Email here)$/) == -1) {
		alert("Please enter a valid email address.")
		document.getElementById("connected_email").focus()
		return
	}
	join_connected()
}

function connected_stateChanged () { 
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.responseText == "OK") {
			alert("Thank you for joining CaseForFreedom.org!")
		}
		else
			document.getElementById("show_xml").innerHTML = xmlHttp.responseText
	}
}

function join_connected () {
	xmlHttp = GetXmlHttpObject()
	if (xmlHttp == null) {
		alert("Your browser does not support AJAX!")
		return
	}
	var url = MTBlogURL + "subscribe.php"
	xmlHttp.onreadystatechange = connected_stateChanged
	xmlHttp.open("POST" , url , true)
	xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=UTF-8;')

	xmlHttp.send("email=" + document.getElementById("connected_email").value +
		"&petition=no&updates=yes" +
		"&countryCode=" + document.getElementById("join_country").value +
		"&cellphone=" + document.getElementById("join_cellphone").value)
}

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 resize (myID, newWidth) {
	myEmbeds = document.getElementById(myID).getElementsByTagName("embed")
	for (i = 0; i < myEmbeds.length; i++) {
		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 = ""
}

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

