/*****************************************************************************************************
 ******************** /////////////////// AJXJT \\\\\\\\\\\\\\\\\\\\ *********************************
 *------------------- Asincronous Javascript And Xml or Json or Http ------------------------------- *
 * Invented and created by Marco Caggiano
 * E-Mail <chang112x@gmail.com> or <chang112x[at]gmail[dot]com>
 * WebSite <http://www.caggianolabs.net> or <http[doubledot][doubleslash]www[dot]caggianolabs[dot]net
 * This software is FREE DISTRIBUITED and FREE MODIFICABLE.
 * There is NO WARRANTY that this technology works efficently, and there isn't ANY AUTHOR'S RESPONSABILITY of
 * the damages that this software could do.
 *	Copyright © 2006/2007
 *****************************************************************************************************/

var req = null; /*The XMLHttpRequest*/
var parseXML = null; /*Function to parsing XML*/
var parseJSON = null; /*Function to parsing JSON*/
var numform = 0; /*If you use forms, this is the number of the form (default 0)*/
var advice = "Please Wait Loading..."; /*Message displaying during loading*/

/*
	*-* url *-* The url to call like script.php?var1=value1&var2=value2 (GET mode) or script.php (POST mode)

	*-* target *-* The html object id that will receive the response

	*-* waitTarget *-* If you want to display the loading message in other div, set this with the div id
	* (default target)

	*-* mode *-* The type of response (Could be XML, JSON, TXT), if you don't set anything the default is TXT,
	* otherwise you have to define a function to parsing the JSON or XML data in your page using AJXJT :
	* Example :
	*    <script type="text/javascript" src="ajxjt.js"></script>
	*    <script type="text/javascript">
	*      var parseJSON = function(jsonText){ //parsing }; //for JSON Mode
	*      var parseXML = function(xml){ //parsing }; //for XML Mode
	*    </script>
	*-*

	*-* data *-* The data to send in POST mode, if you want to use GET mode don't set this parameter (leave null)
	* Examples :
	*    ajxjt("somescript.php", "TargetDivId", "waitDivId", "TXT", "var1=value1&var2=value2") POST mode with TXT
	*    ajxjt("somescript.php?var1=value1&var2=value2", "TargetDivId", "waitDivId") GET mode with TXT
	*    ajxjt("somescript.php?var1=value1&var2=value2", "TargetDivId") GET mode with TXT
	**   (the response and the wait message will display both in TargetDivId)
	*    ajxjt("somescript.php", "TargetDivId", "waitDivId", "XML", "var1=value1&var2=value2") POST with XML
*/

function ajxjt(url, target, waitTarget, mode, data){
	try{
		req = new XMLHttpRequest();
	}
	catch(e){
		try{
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(ex){
			try{
				req = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(exx){
				req = false;
			}
		}
	}
	if(!data){
		sendGET(url, target, waitTarget, mode);
	}
	else{
		sendPOST(url, data, target, waitTarget, mode);
	}
}

function sendGET(url, target, waitTarget, mode){
	req.onreadystatechange = function(){ Done(target, waitTarget, mode); };
	req.open("GET", url, true);
	req.send(null);
}

function sendPOST(url, data, target, waitTarget, mode){
	req.onreadystatechange = function(){ Done(target, waitTarget, mode); };
	req.open("POST", url, true);
	req.setRequestHeader("content-type", "application/x-www-form-urlencoded");
	req.send(data);
}

function Done(target, waitTarget, mode){
	if((req.readyState >= 0) && (req.readyState < 4)){
		if(waitTarget)
			wait(waitTarget);
		else
			wait(target);
	}
	if(req.readyState == 4){
		if(req.status == 200){
			if(mode == "xml" || mode == "XML")
				document.getElementById(target).innerHTML = parseXML(req.responseXML);
			else if(mode == "json" || mode == "JSON")
				document.getElementById(target).innerHTML = parseJSON(req.responseText);
			else
				document.getElementById(target).innerHTML = req.responseText;

		}
		else{
			document.getElementById(target).innerHTML = "AHAH Engine Error " + req.statusText;
		}
	}
}

function wait(target){
	/*
	 * If you want some images just add
	 * advice += "<img src='yourimage.format' width="xx" height='xx' border='x' />"
	 * in your page
	 *
	 */
	var parent = document.getElementById(target);
	parent.innerHTML = advice;
}

/*******************************************************************************************
 * SUBMIT METHOD FOR FORMS                                                                 *
 *******************************************************************************************
 * This method is used when you want to use forms whit AJXJT *******************************
 ************************************************************ Here are some explanation ****
 * and examples ****************************************************************************
 **************
 * FILE : the script or page where send form's elements values
 ********
 * METHOD : the method to use, could be GET or POST (HEAD not implemented yet)
 *****************
 * Example :    *
 *****************
 * <form action="javascript:ajxjt('script.php', 'POST')">
 *  <input type="text" name="Name" />
 *  <button type="button" name="Submit" onclick="numform = 0 // or the number of the form;">Submit</button>
 * </form>
 *******************************************************************************************
 * The submit method doesn't work very well with radio and checkbox buttons yet            *
 *******************************************************************************************
 *******************************************************************************************/

function submit(FILE, METHOD){

	var elements = document.forms[numform].elements
	var num = elements.length;
	var types = "text, hidden, password, file, checkbox, radio, select-one, textarea";
	var url = FILE;
	var data = "";
	var target = "target" + numform;

	for(var k = 0; k < num; k++){
		type = elements.item(k).type;
		if(types.indexOf(type) != -1){
			data += elements.item(k).name + "=" + escape(elements.item(k).value) + "&";
		}
	}
	if((METHOD == "GET") || (METHOD == "get")){
		url += "?" + data;
		ajxjt(url, target);
	}
	else{
		ajxjt(url, target, "","TEXT", data);
	}
}

/*
 ************************************************
 ** Method to get values from the query string **
 ************************************************
 * Example : *
 *************
 * //query = "?var1=value1&var2=value2"
 * var1 = jsGet("var1") //Return value1
 * var2 = jsGet("var2") //Return value2
 ************************************************
*/

function jsGet(get){

	search = location.search.split("?")[1];

	vars = search.split("&");

	for(i = 0; i < vars.length; i++){

		substr = vars[i].split("=");

		if(get == substr[0]){

			return substr[1];

		}

	}

}

function getKeyCode(e){

	var key;

	if(window.event){

		key = e.keyCode;

	}
	else if(e.which){

		key = e.which;

	}
	else{

		return true;

	}

	return key;

}

function getKeyChar(e){

	var key;

	if(window.event){

		key = e.keyCode;

	}
	else if(e.which){

		key = e.which;

	}
	else{

		return true;

	}

	return String.fromCharCode(key);

}

function scrollto(id){

	node = document.getElementById(id);

	window.scroll(0, node.offsetTop);

}
