// eWO AJAX - 2009
// www.eworks.ro
// Ajax Response - eworks electronic 2009
// Ajax response colection for javascript

var innerHTMLwait = '<br/><br/><br/><img onclick="javascript:window.location.reload(true);" src="b01main/main/images/load1.gif" width="24" height="24" /><br/>';
var innerHTMLerr =  'Your browser does not support AJAX!';

//encode decode uri
function urldecode(str) { var ret=str; ret=ret.replace(/\+/g,'%20'); ret=decodeURIComponent(ret); ret=ret.toString(); return ret;}
function urlencode(str) { var ret=str; if(ret==null||ret=='') ret=''; ret=ret.toString(); ret=encodeURIComponent(ret); ret=ret.replace(/%20/g,'+'); return ret;}

//checkbox value
function checkValue(chkId) { var chret = document.getElementById(chId).checked.toString();  return chret; }


// return querry string from parameter list id 
// "par1,param2,param3 ...." -> "param1=value1&param2=value2...."
// paramId - string list "par1,param2,param3 ...."
function qslist(paramIdList)
{	
	var paramIdArray = paramIdList.split(",");	
	var len=paramIdArray.length;
	var querry_string = "";
	for(var i=0; i<len; i++) 
	{
		var key = paramIdArray[i];
		var val = document.getElementById(key).value;		
		val = urlencode(val);
		querry_string =	querry_string + key + "=" + val + "&";
	}
	querry_string = querry_string.substring(0,querry_string.length-1); // remove last & 
	return querry_string;
}



// generate new object XML HTTP Request - browser independent
function newXMLHttp()
{
	var xmlhttp = null;	
	try { xmlhttp = new XMLHttpRequest(); } catch (e) {  }
	if(xmlhttp != null) return xmlhttp; 
	try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { } 
	if(xmlhttp != null) return xmlhttp; 
	try { xmlhttp = new ActiveXObject("Msxml3.XMLHTTP"); } catch (e) {  }  
	if(xmlhttp != null) return xmlhttp; 
	try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {  }  	
	return xmlhttp;
}

// function triggered by response from server
// change state function hadler 
function stateChanged_txt(xmlHttp, txtName) { if (xmlHttp.readyState==4) { document.getElementById(txtName).innerHTML = xmlHttp.responseText; } }

// 	txtName - the id of container span or div in witch the reponse will be store
//	req_url - filename witch the request is send to
//  querry_string - parameters "name1=dataname1&name2=dataname......" without '?'
function GETinnerHTML_txt(txtName, req_url, querry_string) 
{
	document.getElementById(txtName).innerHTML = innerHTMLwait; //wait text or picture
	var xmlHttp = newXMLHttp();	
	if (xmlHttp==null)	{ document.getElementById(txtName).innerHTML = innerHTMLerr;  return false; }		
	req_url = req_url + "?" + querry_string;	// add additonal URL params	//alert(url);		
	xmlHttp.open("GET", req_url, true);
	xmlHttp.onreadystatechange = function () { stateChanged_txt(xmlHttp, txtName);	}
	xmlHttp.send(null);							
}

// 	txtName - the id of container span or div in witch the reponse will be store
//	req_url - filename witch the request is send to
//  querry_string - parameters "name1=dataname1&name2=dataname......" without '?'
function POSTinnerHTML_txt(txtName, req_url, querry_string)
{
	document.getElementById(txtName).innerHTML = innerHTMLwait; //wait text or picture
	var xmlHttp = newXMLHttp();	
	if (xmlHttp==null)	{ document.getElementById(txtName).innerHTML = innerHTMLerr;  return false; }			
	xmlHttp.open("POST", req_url, true);
	xmlHttp.onreadystatechange = function () { stateChanged_txt(xmlHttp, txtName);	}
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", querry_string.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(querry_string); //send params	params = "lorem=ipsum&name=binny"; 
}

function GiHlst(txtName, req_url, paramIdList) { var querry_string = qslist(paramIdList); GETinnerHTML_txt(txtName, req_url, querry_string) ; }
function PiHlst(txtName, req_url, paramIdList) { var querry_string = qslist(paramIdList); POSTinnerHTML_txt(txtName, req_url, querry_string) ; }

