/* The following function creates an XMLHttpRequest object... */

/* 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 createRequestObject(){
	var request_o; //declare the variable to hold the object.
	//if(!(request_o = new XMLHttpRequest();)) request_o= new ActiveXObject("Microsoft.XMLHTTP");
	
	var ua = navigator.userAgent.toLowerCase();
	if (!window.ActiveXObject)
		request_o = new XMLHttpRequest();
	else if (ua.indexOf('msie 5') == -1)
		request_o = new ActiveXObject("Msxml2.XMLHTTP");
	else
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	
	//var browser = navigator.appName; //find the browser name
	//if(browser == "Microsoft Internet Explorer"){
	//	/* Create the object using MSIE's method */
	//	request_o = new ActiveXObject("Microsoft.XMLHTTP");
	//}else{
	//	/* Create the object using other browser's method */
	//	request_o = new XMLHttpRequest();
	//}
	return request_o; //return the object
}

/* You can get more specific with version information by using 
	parseInt(navigator.appVersion)
	Which will extract an integer value containing the version 
	of the browser being used.
*/

/* The variable http will hold our new XMLHttpRequest object. */
var http = createRequestObject();
var http2 = createRequestObject();


function loadPreview(item) {
	loadPreviewContent(item);
	loadPreviewImages(item);
}
/* Function called to get the product categories list */
function loadPreviewContent(item){
	/* 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
		We can reference document.form_category_select.select_category_select and we will
		be referencing the dropdown list. The selectedIndex property will give us the 
		index of the selected item. 
	*/
	http.open('get', '/content/banners/'+item+'.txt', true);
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	
	http.onreadystatechange = handleLoadPreviewContent; 
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	http.send(null);
}

/* Function called to handle the list that was returned from the internal_request.php file.. */
function handleLoadPreviewContent(){
	/* 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(http.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 = http.responseText;
		/* And now we want to change the product_categories <div> content.
			we do this using an ability to get/change the content of a page element 
			that we can find: innerHTML. */
		document.getElementById('previewContent').innerHTML = response;
	}
}

function loadPreviewImages(item) {
/* 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
		We can reference document.form_category_select.select_category_select and we will
		be referencing the dropdown list. The selectedIndex property will give us the 
		index of the selected item. 
	*/
	http2.open('get', '/content/banners/'+item+'_images.txt', true);
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	
	http2.onreadystatechange = handleLoadPreviewImages; 
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	http2.send(null);
}

/* Function called to handle the list that was returned from the internal_request.php file.. */
function handleLoadPreviewImages(){
	/* 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(http2.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 = http2.responseText;
		/* And now we want to change the product_categories <div> content.
			we do this using an ability to get/change the content of a page element 
			that we can find: innerHTML. */
		document.getElementById('previewImages').innerHTML = response;
	}
}

function previewLinks() {
	document.getElementById("preview_kookeys").href="javascript:loadPreview('kookeys');";
	document.getElementById("preview_net-warriorz").href="javascript:loadPreview('net-warriorz');";
	document.getElementById("preview_tracksterz").href="javascript:loadPreview('tracksterz');";
	document.getElementById("preview_virtual-pbr").href="javascript:loadPreview('virtual-pbr');";
	document.getElementById("preview_praise-pets").href="javascript:loadPreview('praise-pets');";
}

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("newsletteremail").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.";
		}
	}
}		
