// JavaScript Document
function lilAJAX(url) {
	this.URL = url;
	this.sessionid = null;
	this.sessionname = null;
	
}
lilAJAX.prototype.xmlreqs = new Array();
lilAJAX.prototype.cookiesEnabled = null;


lilAJAX.prototype.getSession = function() {
	//this.getXMLAsync('_getSession', setSession);
	var req = this.getConnection();
	if (req) {

	  req.open('POST', this.URL, false);
	  // many server-side scripts require the Content-Type to be set:
	  req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	  var data = 'ajaxfunction=_getSession';
		data = data + '&ajaxresponcetype=XML';
		data = data + '&URL=' + encodeURIComponent(window.location.href);
	  	req.send(data);
		
		result = req.responseXML;
		
		this.sessionid = result.getElementsByTagName('sessionid')[0].firstChild.data;
		this.sessionname = result.getElementsByTagName('sessionname')[0].firstChild.data;

		if (!this.cookietest()) { // TODO: I should look at the url to see if this as appended.
	
			this.URL = this.URL + '?' + this.sessionname + '=' + this.sessionid;
		}
	}
}


lilAJAX.prototype.getURL = function() {
	return this.URL;
}

lilAJAX.prototype.cookietest = function() {
	if (this.cookiesEnabled !== null) {
		return this.cookiesEnabled;
	}

	var gCookieName;
	var gCookieValueRet;
	var r;
	gCookieName="lilAJAXCOOKIETEST";
	gCookieValue="lilAJAX_TESTING_COOKIE";
	gCookieValueRet="???"
	this.setCookie(gCookieName,gCookieValue,1);
	gCookieValueRet=this.getCookie(gCookieName);
	
	if (gCookieValueRet!==null) {
		this.cookiesEnabled = true;
	} else{ 
		this.cookiesEnabled = false;
	}
	
	this.deleteCookie(gCookieName);
	return this.cookiesEnabled;
}

lilAJAX.prototype.getCookie = function(name)
{ var pos
  var token = name + "=";
  var tnlen = token.length;
  var cklen = document.cookie.length;
  var i = 0;
  var j;

  while (i < cklen)
  { j = i + tnlen;
    if (document.cookie.substring(i, j) == token)
    { pos = document.cookie.indexOf (";", j);
      if (pos == -1)
        pos = document.cookie.length;
      return unescape(document.cookie.substring(j, pos));
    }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  } //End While
  return null;
}

lilAJAX.prototype.setCookie = function(name, value)
{ document.cookie = name + "=" + escape(value)
}

lilAJAX.prototype.deleteCookie = function(name)
{ var exp = new Date();
  exp.setTime (exp.getTime() - 1);
  var cval = this.getCookie (name);
  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();

}


lilAJAX.prototype.getNumberofConnections = function() {
	return this.xmlreqs.length;
}

lilAJAX.prototype.getConnection = function() {
	connection = null;
	 // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        connection = new XMLHttpRequest();
		 if (!connection) {
			alert('Unable to create XMLHttpRequest');
        }
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        connection = new ActiveXObject("Microsoft.XMLHTTP");
        if (!connection) {
			alert('Unable to create Microsoft.XMLHTTP');
        }
    }
	return connection
}

function CXMLReq(type, xmlhttp, usercallback) {

	this.type = type;
	this.xmlhttp = xmlhttp;
	this.callback = usercallback;
}

lilAJAX.prototype.getResponce = function(CXMLReq) {

	if (CXMLReq.type == 'TEXT') {

		return CXMLReq.xmlhttp.responseText;
	} else if (CXMLReq.type == 'XML') {
		return CXMLReq.xmlhttp.responseXML;
	} else {
		//alert('Unable to process ResponceType: ' + CXMLReq.type);
	}
}

// launch
lilAJAX.prototype.callback= function() {

	if (typeof(this.xmlreqs) == "undefined") 
		return; 
	
	for (var i=0; i < this.xmlreqs.length; i++) { 
		if (this.xmlreqs[i].xmlhttp.readyState == 4) { 
			if (this.xmlreqs[i].xmlhttp.status == 200 || this.xmlreqs[i].xmlhttp.status == 304) { 
			// 200 OK 
				var CXMLReq = this.xmlreqs[i];			
				this.xmlreqs.splice(i,1); 
				i--; 
				var responce = this.getResponce(CXMLReq);
				CXMLReq.callback(responce); 
			} else { 
			// error 
				//alert("There was a problem retrieving the data:\n" + this.xmlreqs[i].xmlhttp.statusText);
				this.xmlreqs.splice(i,1); 
				i--; 
			} 
		}
	}
}

lilAJAX.prototype.nullCallBack = function (response) {
	if (response != '0' && response != 0 && response != '' && response != ' ') {
		alert(response);
	}
}

// callback, function, arguments
lilAJAX.prototype.getTextAsync = function() {

	callbackmethod = null;
	remotefunction = null; 
	
		
	remotefunction = arguments[0];
	
	 if (typeof arguments[1] == 'function') {
		callbackmethod = arguments[1];
    } else {
		alert('First Argument of getTextAsync mustbe a method');
		return false;
	}

	

	var data = 'ajaxfunction=' + encodeURIComponent(remotefunction);
	data = data + '&ajaxresponcetype=TEXT';
	
	for(var i = 2; i < arguments.length; i++) {
			data = data + '&ajaxargs[]=' + encodeURIComponent(arguments[i]);
	}
	
	var _this = this;

	req = this.getConnection();
	req.onreadystatechange=function () { _this.callback() };
	req.open("POST",this.URL,true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	

	
	var xmlreq = new CXMLReq('TEXT', req, callbackmethod);
	this.xmlreqs.push(xmlreq);
	req.send(data);	
}


lilAJAX.prototype.getTextAsync_fromForm = function() {

	callbackmethod = null;
	remotefunction = null; 
	
		
	remotefunction = arguments[0];
	
	 if (typeof arguments[1] == 'function') {
		callbackmethod = arguments[1];
    } else {
		alert('First Argument of getTextAsync mustbe a method');
		return false;
	}

	

	var data = 'ajaxfunction=' + encodeURIComponent(remotefunction);
	data = data + '&ajaxresponcetype=TEXT';
	
	for(i=0; i<arguments[2].length; i++) {
		if(arguments[2][i].type=="checkbox") {
			data = data + '&ajaxargs['+arguments[2][i].name+'][data]=' + encodeURIComponent(arguments[2][i].checked);
		}
		else {
			data = data + '&ajaxargs['+arguments[2][i].name+'][data]=' + encodeURIComponent(arguments[2][i].value) + '&ajaxargs['+arguments[2][i].name+'][ctype]=' + encodeURIComponent(arguments[2][i].ctype);
		}
	}
	data = data + '&ajaxargs[formID]='+arguments[3];
	
	var _this = this;

	req = this.getConnection();
	req.onreadystatechange=function () { _this.callback() };
	req.open("POST",this.URL,true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	

	
	var xmlreq = new CXMLReq('TEXT', req, callbackmethod);
	this.xmlreqs.push(xmlreq);
	req.send(data);	
}

lilAJAX.prototype.getText_fromForm = function() {

	callbackmethod = null;
	remotefunction = null; 
	
		
	remotefunction = arguments[0];
	
	 if (typeof arguments[1] == 'function') {
		callbackmethod = arguments[1];
    } else {
		alert('First Argument of getTextAsync mustbe a method');
		return false;
	}

	

	var data = 'ajaxfunction=' + encodeURIComponent(remotefunction);
	data = data + '&ajaxresponcetype=TEXT';
	
	for(i=0; i<arguments[2].length; i++) {
		if(arguments[2][i].type=="checkbox") {
			data = data + '&ajaxargs['+arguments[2][i].name+'][data]=' + encodeURIComponent(arguments[2][i].checked);
		}
		else {
			data = data + '&ajaxargs['+arguments[2][i].name+'][data]=' + encodeURIComponent(arguments[2][i].value) + '&ajaxargs['+arguments[2][i].name+'][ctype]=' + encodeURIComponent(arguments[2][i].ctype);
		}
	}
	data = data + '&ajaxargs[formID]='+arguments[3];
	
	var _this = this;

	req = this.getConnection();
	req.open("POST",this.URL,false);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
	req.send(data);
	callbackmethod(req.responseText);	
}

// callback, function, arguments
lilAJAX.prototype.getText = function() {

	callbackmethod = null;
	remotefunction = null; 
	
		
	remotefunction = arguments[0];
	
	 if (typeof arguments[1] == 'function') {
		callbackmethod = arguments[1];
    } else {
		alert('First Argument of getTextAsync mustbe a method');
		return false;
	}

	

	var data = 'ajaxfunction=' + encodeURIComponent(remotefunction);
	data = data + '&ajaxresponcetype=TEXT';
	
	for(var i = 2; i < arguments.length; i++) {
			data = data + '&ajaxargs[]=' + encodeURIComponent(arguments[i]);
	}

	req = this.getConnection();
	req.open("POST",this.URL,false);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	

	req.send(data);
	callbackmethod(req.responseText);	
}


// callback, function, arguments
lilAJAX.prototype.getXMLAsync = function() {

	callbackmethod = null;
	remotefunction = null; 
	
		remotefunction = arguments[0];
		
	 if (typeof arguments[1] == 'function') {
		callbackmethod = arguments[1];
    } else {
		alert('First Argument of getTextAsync mustbe a method');
		return false;
	}
	
	var data = 'ajaxfunction=' + encodeURIComponent(remotefunction);
	data = data + '&ajaxresponcetype=XML';
	
	for(var i = 2; i < arguments.length; i++) {
			data = data + '&ajaxargs[]=' + encodeURIComponent(arguments[i]);
	}
	
	var _this = this;

	req = this.getConnection();
	req.onreadystatechange=function () { _this.callback() };
	req.open("POST",this.URL,true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
	
	var xmlreq = new CXMLReq('XML', req, callbackmethod);
	this.xmlreqs.push(xmlreq);
	req.send(data);	
}

lilAJAX.prototype.getXMLAsync_fromForm = function() {

	callbackmethod = null;
	remotefunction = null; 
	
		remotefunction = arguments[0];
		
	 if (typeof arguments[1] == 'function') {
		callbackmethod = arguments[1];
    } else {
		alert('First Argument of getTextAsync mustbe a method');
		return false;
	}
	
	var data = 'ajaxfunction=' + encodeURIComponent(remotefunction);
	data = data + '&ajaxresponcetype=XML';
	
	for(i=0; i<arguments[2].length; i++) {
		if(arguments[2][i].type=="checkbox") {
			data = data + '&ajaxargs['+arguments[2][i].name+'][data]=' + encodeURIComponent(arguments[2][i].checked);
		}
		else {
			data = data + '&ajaxargs['+arguments[2][i].name+'][data]=' + encodeURIComponent(arguments[2][i].value) + '&ajaxargs['+arguments[2][i].name+'][ctype]=' + encodeURIComponent(arguments[2][i].ctype);
		}
	}
	data = data + '&ajaxargs[formID]='+arguments[3];
	
	var _this = this;

	req = this.getConnection();
	req.onreadystatechange=function () { _this.callback() };
	req.open("POST",this.URL,true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
	
	var xmlreq = new CXMLReq('XML', req, callbackmethod);
	this.xmlreqs.push(xmlreq);
	req.send(data);	
}

// callback, function, arguments
lilAJAX.prototype.getXML = function() {

	callbackmethod = null;
	remotefunction = null; 
	
		remotefunction = arguments[0];
		
	 if (typeof arguments[1] == 'function') {
		callbackmethod = arguments[1];
    } else {
		alert('First Argument of getTextAsync mustbe a method');
		return false;
	}
	
	var data = 'ajaxfunction=' + encodeURIComponent(remotefunction);
	data = data + '&ajaxresponcetype=XML';
	
	for(var i = 2; i < arguments.length; i++) {
			data = data + '&ajaxargs[]=' + encodeURIComponent(arguments[i]);
	}
	

	req = this.getConnection();
	req.open("POST",this.URL,false);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	

	req.send(data);
	callbackmethod(req.responseXML);	
}

// callback, function, arguments
lilAJAX.prototype.callAsync = function() {

	callbackmethod = null;
	remotefunction = null; 
	
		
	remotefunction = arguments[0];
	
	callbackmethod = this.nullCallBack;
	
	var data = 'ajaxfunction=' + encodeURIComponent(remotefunction);
	data = data + '&ajaxresponcetype=TEXT';
	
	for(var i = 1; i < arguments.length; i++) {
			data = data + '&ajaxargs[]=' + encodeURIComponent(arguments[i]);
	}
	
	var _this = this;

	req = this.getConnection();
	req.onreadystatechange=function () { _this.callback() };
	req.open("POST",this.URL,true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	

	var xmlreq = new CXMLReq('TEXT', req, callbackmethod);
	this.xmlreqs.push(xmlreq);
	req.send(data);	
}

// callback, function, arguments
lilAJAX.prototype.call = function() {

	callbackmethod = null;
	remotefunction = null; 
	
		
	remotefunction = arguments[0];
	
	callbackmethod = this.nullCallBack;
	
	var data = 'ajaxfunction=' + encodeURIComponent(remotefunction);
	data = data + '&ajaxresponcetype=TEXT';
	
	for(var i = 1; i < arguments.length; i++) {
			data = data + '&ajaxargs[]=' + encodeURIComponent(arguments[i]);
	}
	

	req = this.getConnection();
	req.open("POST",this.URL,false);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	

	req.send(data);
	var response = req.responseText;
	if (response != '0' && response != 0 && response != '' && response != ' ') {
		alert(response);
	}	
}

