//
//	/*
//	*	Ajax Functions
//	*/
//	header('Content-Type: text/javascript');
//
	
/*
*	Makes a new object for communicating with the server
*/
function makeXMLHTTP(){
	var xmlHttp;
	try {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}  catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}


function submitRedemptionCode(e){
	var keycode = false;
	
	if(e && e.which){
		keycode = e.which;
	} else {
		keycode = event.keyCode;
	}
	
	if(keycode == 13){
		validateRedemptionCode();
	}
}

function validateRedemptionCode(){
	var redemptionCode = document.getElementById('redemptioncode').value;
	var errorParagraph = document.getElementById("redemptioncodeerror");
	var url = "/ajaxportal.php";
	var parameters = "action=validateRedemptionCode&redemptionCode="+redemptionCode;

	var xmlHTTP = makeXMLHTTP();
	xmlHTTP.onreadystatechange = function(){
									// This function is called when the xmlhttp objects state changes (i.e. after the rest of the validateRedemptionCode function)
									//errorParagraph.innerHTML += "STATE CHANGED TO " + xmlHTTP.readyState + "<br />";
									if(xmlHTTP.readyState == 4){
										// The server has returned something
										if(xmlHTTP.responseText == "Invalid"){
											// Not valid, set the text
											errorParagraph.innerHTML = "Sorry, this code is not valid, please try again.";
										} else {
											// Valid, redirect to the redemption pdf page
											window.location = "/redemptioncode/";
											//errorParagraph.innerHTML = xmlHTTP.responseText;
											//errorParagraph.innerHTML = "VALID! will redirect to: /redemptioncode/";
										}
									}
	}
	
	// Send the original request
	xmlHTTP.open("POST", url, true);
	xmlHTTP.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	xmlHTTP.send(parameters);	
}

function logSearch(searchTerm){
	var url = "/ajaxportal.php";
	var parameters = "action=dfcMap_logSearch&searchTerm=" + searchTerm;

	var xmlHTTP = makeXMLHTTP();
	xmlHTTP.onreadystatechange = function(){
									// This function is called when the xmlhttp objects state changes (i.e. after the rest of the validateRedemptionCode function)
									//errorParagraph.innerHTML += "STATE CHANGED TO " + xmlHTTP.readyState + "<br />";
									if(xmlHTTP.readyState == 4){
										// The server has returned something
										window.location = xmlHTTP.responseText;
									}
	}
	
	// Send the original request
	xmlHTTP.open("POST", url, true);
	xmlHTTP.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	xmlHTTP.send(parameters);		
}
