//  function iframer taken from:
//    http://forums.devshed.com/javascript-development-115/javascript-resources-224864.html
//  Pass both the ID and NAME of the iframe which you want the innerHTML of.
function iframer(ID,name) {
	var saf = navigator.userAgent.match(/Safari/i);
	var iFrame = document.getElementById(ID);
	var data="";
	if (iFrame.contentDocument && !saf) {
		// NS6 & Gecko
		data=iFrame.contentDocument.defaultView.document.body.innerHTML;
	} else if (iFrame.contentWindow) {
		// IE 5.5 & 6.x
		data=iFrame.contentWindow.document.body.innerHTML;
	} else if (saf) {
		// This may also support Konqueror, I haven't tested it yet.
		// Safari
		data=iFrame.document.body.innerHTML;
	} else if (document.all) {
		// IE 5.5 on the MAC
		data=document.frames[name].document.body.innerHTML;
	}
	return(data);
}

/* This implementation of the XMLHttpRequest object is taken from digg.com */
var xmlhttp
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
	try {
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
	} catch (e) {
		try {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	} catch (E) {
		xmlhttp=false
	}
}
@else
	xmlhttp=false
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false
	}
}
function myXMLHttpRequest() {
	var xmlhttplocal;
	try {
		xmlhttplocal= new ActiveXObject("Msxml2.XMLHTTP")
	} catch (e) {
		try {
			xmlhttplocal= new ActiveXObject("Microsoft.XMLHTTP")
		} catch (E) {
			xmlhttplocal=false;
		}
	}

	if (!xmlhttplocal && typeof XMLHttpRequest!='undefined') {
		try {
			var xmlhttplocal = new XMLHttpRequest();
		} catch (e) {
			var xmlhttplocal=false;
			alert('couldn\'t create xmlhttp object');
		}
	}
	return(xmlhttplocal);
}

/* The variable http will hold our new XMLHttpRequest object. */
var xmlhttp = myXMLHttpRequest(); 

function load(item,div) {
	//alert(item);
	//var answer = confirm("Are you sure you want to remove this file?");
	xmlhttp.open('GET', '/content/' + item, true);
	//alert('content/' + page + '_learnmore.php');
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4){ 
			var response = xmlhttp.responseText;
			//alert(response);
			if(response != "") {
				//info = response.split('|');
				//alert("File removed");
				document.getElementById(div).innerHTML = cleanReviews(response);
			}
		}
	}
	xmlhttp.send(null);
}

function toggleView(id) {
	document.getElementById("hide" + id).style.display="inline";
	document.getElementById("link" + id).style.display="none";
}

function hideView(id) {
	document.getElementById("hide" + id).style.display="none";
	document.getElementById("link" + id).style.display="block";
}

function cleanReviews(string) {
	return string.replace(/id=\"hide/g, "style=\"display: none\" id=\"hide");
}

function submit() {
	/* Create the request. The first argument to the open function is the method (POST/GET),
		and the second argument is the url... 
		document contains references to all items on the page
	*/
	xmlhttp.open('POST', '/content/joinlist-ajax.php', true);
		xmlhttp.setRequestHeader("Method", "POST /content/joinlist-ajax.php HTTP/1.1");
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	//alert(document.getElementById("email").value);
	xmlhttp.onreadystatechange = handleSubmit; 
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	xmlhttp.send("email=" + document.getElementById("email").value);
	//document.addArticle.tags.value = document.addArticle.body.value.length;
}

/* Function called to handle the list that was returned from the internal_request.php file.. */
function handleSubmit(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	if(xmlhttp.readyState == 4){ //Finished loading the response
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of 
			the XMLHttpRequest object. */
		var response = xmlhttp.responseText;
		if(response != "") {
			/* And now we want to change the <div> content.
			we do this using an ability to get/change the content of a page element 
			that we can find: innerHTML. */
			document.getElementById('mailinglist').innerHTML = response;
		} else {
			document.getElementById('mailinglist').innerHTML = "Sorry, something went wrong.  Your email address was not added to the mailing list.";
		}
	}
}