// AJAXBasic.js
// created by Brian S Paskin (brian@paskino.com)

// global variables
var READY_STATE_UNITIALIZED = 0;
var READY_STATE_LOADING = 1;
var READY_STATE_LOADED = 2;
var READY_STATE_INTERACTIVE = 3;
var READY_STATE_COMPLETE = 4;
var request = null;




// ***********************************************************************
// * getXMLHTTRequest                                                    *
// *                                                                     *
// * function to query the browser and return the appropriate object     *
// * to be used.                                                         *
// ***********************************************************************
function getXMLHTTPRequest() {
	var oRequest = null;

	// check to see if we are using another browser that supports XMLHTTPRequest
	// or Internet Explorer
	if (window.XMLHttpRequest) {
		oRequest = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
       	 	oRequest = new ActiveXObject("Msxml2.XMLHTTP");
       	 } catch (e) {
       	 	oRequest = new ActiceXObject("Microsoft.XMLHTTP");
       	 }
	}
	
	// Return the object that was created.
	return oRequest;
}

// ***********************************************************************
// * sendRequest                                                         *
// *                                                                     *
// * Sends the request to the server                                     *
// ***********************************************************************
function sendRequest(url, params, HTTPMethod) {
	
	// check if an HTTP method has been sent.  GET and POST are valid.
	if (!HTTPMethod) {
		HTTPMethod = "POST";
	}
	
	if (HTTPMethod != "POST" || HTTPMethod != "GET") {
		HTTPMethod = "POST";
	}
	
	// retrieve the request object
	request = getXMLHTTPRequest();
		
	// set the callback function
	request.onreadystatechange = readyStateChangeCallback;
	
	// if the object returned a null then the browser does not support XMLHTTPRequest.
	// Pop up a window.alert
	if (request == null) {
		alert ("Sorry, but your browser does not support asynchronous messaging.");
		return;
	}
	
	// open the connection
	request.open(HTTPMethod, url, true);
	
	// set the request headers
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	// send the request with data to the server
	request.send(params);
}

// ***********************************************************************
// * readyStateChangeCallback                                            *
// *                                                                     *
// * This is a callback method when the state changes.  The method will  *
// * take the text that is returned and call the method "receiveData"    *
// * that is a custom method for each application or page.               *
// *                                                                     *
// * If the state is not ready, text can be displayed to the user        *
// * stating that the application is still waiting on data.  This is a   *
// * simple modification                                                 *
// ***********************************************************************
function readyStateChangeCallback() {
	
	// variable for the response data
	var response = null;
	
	// check to see if the state is complete and send the data to another function
	// for processing
	if (request.readyState == READY_STATE_COMPLETE) {
		response = request.responseText;
		receiveData(response);
	}
}

// ***********************************************************************
// * createXMLDocument                                                   *
// *                                                                     *
// * Creates a DOM or XML Document based on browser.  The function takes *
// * the string representation of the XML document and creates the       *
// * appropriate object, which is returned to the calling function       *                          
// ***********************************************************************
function createXMLDocument(xml) {

	var parser;
	var xmlDoc;
	
	// check to see if the browser is Netscape, Firefox, Safari, etc
	if (document.implementation && document.implementation.createDocument)
	{
		// create a new DOMParser and parse the xml, which is in string format
		parser = new DOMParser();
		xmlDoc = parser.parseFromString(xml, "text/xml");
		
	}
	// else is the browser IE
	else if (window.ActiveXObject)
	{
		// create a new ActiveX XMLDOM and then load the XML string into the object
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.loadXML(xml);
		
	} 
	// Sorry, out of luck!
	else
	{
		alert("Your browser can\'t handle this script");
	}
	
	return xmlDoc;
	 
}

// sendMsg is the function to gather the data and send the message
function sendMsg() {
  
   var href=window.location.href;
   var baseHref=href.substring(0,href.lastIndexOf('/') + 1);

   document.getElementById("result").innerHTML="";
   var params="posLeft=" + document.inputForm.posleft.value + "&posRight=" + document.inputForm.posright.value + "&tts=" + document.inputForm.tts.value + "&voice=" + document.inputForm.voice.value;
   sendRequest("http://www.stephthegeek.tv/nabaztag.php", params, "POST");
}

// receiveData is the result of the call
function receiveData(data) {
   document.getElementById("result").innerHTML=data;
}

