/* Simple AJAX Code-Kit (SACK) */
/* ©2005 Gregory Wild-Smith */
/* www.twilightuniverse.com */
/* Software licenced under a modified X11 licence, see documentation or authors website for more details */

function sack(file){
	this.AjaxFailedAlert = "Your browser does not support the enhanced functionality of this website, and therefore you will have an experience that differs from the intended one.\n";
	this.requestFile = file;
	this.method = "POST";
	this.URLString = "";
	this.encodeURIString = true;
	this.execute = false;

	this.onLoading = function() { };
	this.onLoaded = function() { };
	this.onInteractive = function() { };
	this.onCompletion = function() { };

	this.createAJAX = function() {
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (err) {
				this.xmlhttp = null;
			}
		}
		if(!this.xmlhttp && typeof XMLHttpRequest != "undefined")
			this.xmlhttp = new XMLHttpRequest();
		if (!this.xmlhttp){
			this.failed = true; 
		}
	};
	
	this.setVar = function(name, value){
		if (this.URLString.length < 3){
			this.URLString = name + "=" + value;
		} else {
			this.URLString += "&" + name + "=" + value;
		}
	}
	
	this.encVar = function(name, value){
		var varString = encodeURIComponent(name) + "=" + encodeURIComponent(value);
	return varString;
	}
	
	this.encodeURLString = function(string){
		varArray = string.split('&');
		for (i = 0; i < varArray.length; i++){
			urlVars = varArray[i].split('=');
			if (urlVars[0].indexOf('amp;') != -1){
				urlVars[0] = urlVars[0].substring(4);
			}
			varArray[i] = this.encVar(urlVars[0],urlVars[1]);
		}
	return varArray.join('&');
	}
	
	this.runResponse = function(){
		eval(this.response);
	}
	
	this.runAJAX = function(urlstring){
		this.responseStatus = new Array(2);
		if(this.failed && this.AjaxFailedAlert){ 
			alert(this.AjaxFailedAlert); 
		} else {
			if (urlstring){ 
				if (this.URLString.length){
					this.URLString = this.URLString + "&" + urlstring; 
				} else {
					this.URLString = urlstring; 
				}
			}
			if (this.encodeURIString){
				var timeval = new Date().getTime(); 
				this.URLString = this.encodeURLString(this.URLString);
				this.setVar("rndval", timeval);
			}
			if (this.element) { this.elementObj = document.getElementById(this.element); }
			if (this.xmlhttp) {
				var self = this;
				if (this.method == "GET") {
					var totalurlstring = this.requestFile + "?" + this.URLString;
					this.xmlhttp.open(this.method, totalurlstring, true);
				} else {
					this.xmlhttp.open(this.method, this.requestFile, true);
				}
				if (this.method == "POST"){
  					try {
						this.xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded')  
					} catch (e) {}
				}

				this.xmlhttp.send(this.URLString);
				this.xmlhttp.onreadystatechange = function() {
					switch (self.xmlhttp.readyState){
						case 1:
							self.onLoading();
						break;
						case 2:
							self.onLoaded();
						break;
						case 3:
							self.onInteractive();
						break;
						case 4:
							self.response = self.xmlhttp.responseText;
							self.responseXML = self.xmlhttp.responseXML;
							self.responseStatus[0] = self.xmlhttp.status;
							self.responseStatus[1] = self.xmlhttp.statusText;
							self.onCompletion();
							if(self.execute){ self.runResponse(); }
							if (self.elementObj) {
								var elemNodeName = self.elementObj.nodeName;
								elemNodeName.toLowerCase();
								if (elemNodeName == "input" || elemNodeName == "select" || elemNodeName == "option" || elemNodeName == "textarea"){
									self.elementObj.value = self.response;
								} else {
									self.elementObj.innerHTML = self.response;
								}
							}
							self.URLString = "";
						break;
					}
				};
			}
		}
	};
this.createAJAX();
}
var table_global_id='table_global_id';
var table_global_id2='table_global_id2';
var menu_global_id='main_menu';
var  popup_global_id='popup_global_id';
function doHTTPRequest(url, parameters, callback) {
  var http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
    http_request = new XMLHttpRequest();
    if(http_request.overrideMimeType) {
      http_request.overrideMimeType('text/xml');
    }
  } else if (window.ActiveXObject) { // IE
    try {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
          http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
    }
  }
  if (!http_request) {
    alert('Cannot create XMLHTTP instance');
    return false;
  }

  //http_request.onreadystatechange = callback;
  http_request.onreadystatechange = function () {
		if (http_request.readyState == 4) 
		if (http_request.status == 200) {
                        callback();
        } else {
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
  }

  http_request.open('POST', url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded;multipart/form-data");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
  
  return http_request;
}

function parseSubscriber() {
	if (SubRequest.readyState == 4) {
		document.getElementById('serverResponse').innerHTML = SubRequest.responseText;
	}
}



function LoadTable(floder_home,table_id,page,param){
	table_global_id =  table_id;
	document.getElementById(table_id).innerHTML = '<table   cellspacing="0" cellpadding="0" class="solidfull"><tr><th colspan="100%"><img src="image/loading.gif"  align="absmiddle">&nbsp; Processing....</th></tr></table>';
	param=param + '&p=' + page;
	GroupSelectTable = doHTTPRequest(floder_home + table_id + '.php',param,parseSelect );
}
function parseSelect(){
	if (GroupSelectTable.readyState == 4) {
		document.getElementById(table_global_id).innerHTML =GroupSelectTable.responseText;
	}
}

function LoadPopup(table_file,param , weight, height){
	if (document.getElementById('box').style.display == 'none')
	{
	Lightbox.showBoxByAJAX( table_file + '?First=0' + param, weight,height);
	}else
	{
	document.getElementById('boxContents').innerHTML = '<table   cellspacing="0" cellpadding="0"  height="100%" width="100%" style="vertical-align:middle;"><tr><th colspan="100%"><img src="image/loading.gif"  align="absmiddle">&nbsp; Processing....</th></tr></table>';
	GroupSelect = doHTTPRequest( table_file ,param,parseSelectPopup );
	}
}
function parseSelectPopup(){
	if (GroupSelect.readyState == 4) {
		document.getElementById('boxContents').innerHTML =GroupSelect.responseText;
	}
}

function LoadMenuFile(menu_file){
	document.getElementById(menu_global_id).innerHTML = '<table   cellspacing="0" cellpadding="0" class="solidfull"><tr><th colspan="100%"><img src="image/loading.gif"  align="absmiddle">&nbsp; Processing....</th></tr></table>';
	param='';
	GroupSelect = doHTTPRequest(menu_file,param,parseMainMenu );


}
function parseMainMenu(){
	if (GroupSelect.readyState == 4) {
		document.getElementById(menu_global_id).innerHTML =GroupSelect.responseText;
	}
}


