var XHTTP;

//http readyState
var HRS = new Array();
HRS[0]="uninitialized";
HRS[1]="loading";
HRS[2]="loaded";
HRS[3]="interactive";
HRS[4]="complete";

//http status
var HST = new Array();
HST[100]="Continue";
HST[101]="Switching protocols";
HST[200]="OK";
HST[201]="Created";
HST[202]="Accepted";
HST[203]="Non-Authoritative Information";
HST[204]="No Content";
HST[205]="Reset Content";
HST[206]="Partial Content";
HST[300]="Multiple Choices";
HST[301]="Moved Permanently";
HST[302]="Found";
HST[303]="See Other";
HST[304]="Not Modified";
HST[305]="Use Proxy";
HST[307]="Temporary Redirect";
HST[400]="Bad Request";
HST[401]="Unauthorized";
HST[402]="Payment Required";
HST[403]="Forbidden";
HST[404]="Not Found";
HST[405]="Method Not Allowed";
HST[406]="Not Acceptable";
HST[407]="Proxy Authentication Required";
HST[408]="Request Timeout";
HST[409]="Conflict";
HST[410]="Gone";
HST[411]="Length Required";
HST[413]="Request Entity Too Large";
HST[414]="Request-URI Too Long";
HST[415]="Unsupported Media Type";
HST[416]="Requested Range Not Suitable";
HST[417]="Expectation Failed";
HST[500]="Internal Server Error";
HST[501]="Not Implemented";
HST[502]="Bad Gateway";
HST[503]="Service Unavailable";
HST[504]="Gateway Timeout";
HST[505]="HTTP Version Not Supported";

var AllowAsync = true;

function encodeFieldSp(str){
	str =  str.replace(/\=/g,"---EQL---");
	str =  str.replace(/\&/g,"---AND---");
	str =  str.replace(/\?/g,"---QST---");
	return str;
}

function decodeFieldSp(str){
	str =  str.replace(/---EQL---/g,'=');
	str =  str.replace(/---AND---/g,'&');
	str =  str.replace(/---QST---/g,'?');
	return str;
}

// create object XMLHttpRequest
function getXHTTP()
{
	try {
		XHTTP = new XMLHttpRequest();
	}
	catch(e) {
		AllowAsync = false;
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP");
		// try every versions until one works
		for (var i=0; i<XmlHttpVersions.length && !XHTTP; i++) {
			try {
				XHTTP = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch (e) {}
		}
	}
	if (!XHTTP)
		displayError("Your browser doesn't handle AJAX, please use Internet explorer, Safari or firefox");
	else
		return XHTTP;
}

function XHTTP_error(st,json){
	if (json)
		return '[{"HTTP_Error":"'+st+', '+HST[st]+'"}]';
	else
		return '"HTTP_Error:'+st+', '+HST[st];
}

// Synchronous JAX Request
function SJAX(url,mtd,json)
{
	var r=null;
	if (!url) return;
	if (!mtd || !AllowAsync) mtd = "GET";
	u = URLPREP(url,mtd);

	XHTTP = getXHTTP();

	XHTTP.open(mtd, u[0], false);
	if (mtd == "POST"){
		XHTTP.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		XHTTP.setRequestHeader("Content-length", u[1].length);
		XHTTP.setRequestHeader("Connection", "close");
	}
	else{
		XHTTP.setRequestHeader('Content-Type','text/html');
	}
	if (json) XHTTP.setRequestHeader('Accept', 'application/json');

	XHTTP.send(u[1]);

	r = (XHTTP.readyState == 4 && XHTTP.status == 200) ? XHTTP.responseText : XHTTP_error(XHTTP.status,json);

	delete XHTTP;

	if (json) r = eval("("+r+")");
	return r;
}

// Asynchronous JAX Request
function AJAX(url, fcallback, mtd, json)
{
	var r=null;
	if (AllowAsync){
		if (!url) return;
		if (!mtd || !AllowAsync) mtd = "GET";
		u = URLPREP(url,mtd);

		XHTTP = getXHTTP();

	  	XHTTP.onreadystatechange = function()
	   	{
	   		try{
			    if (XHTTP.readyState == 4){
				    	r = (XHTTP.status == 200) ? XHTTP.responseText : XHTTP_error(XHTTP.status,json);
				    	delete XHTTP;
						if (json) r = eval("("+r+")");
						if (fcallback) ret = fcallback(r);
			    }
	    	}
	    	catch (e){}
		}
		XHTTP.open(mtd, u[0], true);
		if (mtd == "POST"){
			XHTTP.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			XHTTP.setRequestHeader("Content-length", u[1].length);
			XHTTP.setRequestHeader("Connection", "close");
		}
		else{
			XHTTP.setRequestHeader('Content-Type','text/html');
		}
		if (json) XHTTP.setRequestHeader('Accept', 'application/json');

		XHTTP.send(u[1]);
	}
	else {
		r = SJAX(url, mtd, json);
		if (fcallback) ret = fcallback(r);
	}
}

// synchronous TJAX Request for translation
function TJAX(tx,p1,p2,p3,p4)
{
	var r=null;
	tx = (tx) ? encodeURIComponent(tx) : '';
	p1 = (p1) ? encodeURIComponent(p1) : '';
	p2 = (p2) ? encodeURIComponent(p2) : '';
	p3 = (p3) ? encodeURIComponent(p3) : '';
	p4 = (p4) ? encodeURIComponent(p4) : '';
	u = "gettext.php?tx="+tx+"&p1="+p1+"&p2="+p2+"&p3="+p3+"&p4="+p4;
	XHTTP = getXHTTP();
	XHTTP.open('GET', u, false);
	XHTTP.setRequestHeader('Content-Type','text/html');
	XHTTP.send(null);
	r = (XHTTP.readyState == 4 && XHTTP.status == 200) ? XHTTP.responseText : XHTTP_error(XHTTP.status,null);
	delete XHTTP;
	return r;
}

function URLPREP(url,mtd)
{
	var upath   = '';
	var usearch = '';
	var uparam  = new Array();
	var nocache = new Date();

	var qm = url.indexOf('?');
	if (qm != -1){
		upath   = url.substring(0,qm);
		usearch = url.substring(qm+1);
		uparam  = usearch.split('&');
		var us=new Array();
		for (var i in uparam){
			var prm = uparam[i].split('=');
			us[i]=prm[0]+'='+encodeURIComponent(decodeFieldSp(prm[1]));
		}
		usearch = us.join('&')+'&nocache='+encodeURIComponent(nocache);
	}
	else {
		upath = url;
		usearch = 'nocache='+encodeURIComponent(nocache);
	}
	if (mtd == 'GET')
		ret = new Array((upath+'?'+usearch),null);
	else
		ret = new Array(upath,usearch);
	return ret;
}

//CALL of SQL queries by name in queries directory with dataset return in JSON format
function SJSON(Query)
{
	return SJAX('ajson.php?Q='+Query,'GET',true);
}

function AJSON(Query,Callback)
{
	AJAX('ajson.php?Q='+Query,Callback,'GET',true);
}
