﻿ cCore = function() {
	this.name		    = "c";
	this.ajax           = new cAjax;
	this.callback       = new cAjaxCall;
	this.ie             = (document.all)	   ? true : false;
	this.bwok           = (this.ie);
	this.ns4            = (document.layers) ? true : false;
	this.windowname     = window.name;
};//end c core

cAjax = function() {
	this.version = "1.0";
	this.loaded  = true;
	this.requests = [];
	this.debug    = true;
}

cAjax.prototype.invoke = function(processorUrl,callbackFnc) {	
	var x = 0;
	for(x=0;x<this.requests.length;x++) {
		if(this.requests[x].Available)
			return this.requests[x].invoke(processorUrl,callbackFnc);
	}
	var obj = new cAjaxCall(this.requests.length)
	this.requests.push(obj);
	return obj.invoke(processorUrl,callbackFnc);
}

// Ajax Implementation
cAjaxCall = function(index) {
	this.Available      = true;
	this.httpRequest    = null;
	this.callbackFnc    = null;
	this.index          = index;
	this.debug          = true;
}

cAjaxCall.prototype.getXMLHttpRequest = function() {
	if(c.ie)
		return new ActiveXObject("Microsoft.XMLHTTP");
	if(typeof(XMLHttpRequest)) 
		return new XMLHttpRequest;
	return null;
}

cAjaxCall.prototype.onCallBack = function(data) {
	this.Available   = true;
	if(c.callback.debug) window.status = 'request (' + this.index + ',' + this.action + ') completed at ' + (new Date());
	if(this.callbackFnc)
		return this.callbackFnc(data);
	else
		return data;
};

cAjaxCall.prototype.invoke = function(processorUrl,callbackFnc) {	
	
	var callBackIndex = this.index;	

	try {
		if(!this.httpRequest) {			
			this.httpRequest= c.callback.getXMLHttpRequest();
		}
		if(!this.Available)	{
			alert('Request in progress, please wait...');
			return false;
		}	

		this.Available = false;

		this.callbackFnc = callbackFnc;
		this.httpRequest.open('POST', processorUrl + '&ajax=' + (new Date()), false);
		this.httpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		
		this.httpRequest.onreadystatechange = function () { 				
			c.ajax.requests[callBackIndex].onReadyStateChange(false);
		}
		
		this.httpRequest.send('');

		return this.httpRequest.responseText;
			
	}catch(ex) {
		//var ret = this.httpRequest.responseText;
		alert('Invoke: ' +  ex.message);
	}
};//

cAjaxCall.prototype.onReadyStateChange = function(async)  {
	if(c.callback.debug) window.status = 'processing request (' + this.index + ') ' + (new Date());
	try
	{	if(this.httpRequest.readyState==4){
			var ret = this.httpRequest.responseText;
			if(ret.toString().indexOf('ERR$')>=0)
			{	var msg = ret.substring(4, ret.length)
				var err =  new Error()
				err.message = 'Ajax Server Error : ' + msg
				throw err;
			}else
				if(async) {
					var resp = this.httpRequest.responseText.split("|*|");
					if(resp[1])	c.getObject("__VIEWSTATE").value  = resp[1];			
					return this.onCallBack(resp[0]);				
				}
		}
	}
	catch(ex)
	{   alert('Error OnReadyStateChange: ' + ex.message)
		return false;
	}
};

function BrowserCheck() {
	this.ns4 = (document.layers)? true:false;
	this.ie = (document.all&&(!window.opera))? true:false;
	this.dom = (document.getElementById)? true:false;
	this.ns6 = (window.sidebar)? true:false;
	this.moz = (window.sidebar||navigator.userAgent.indexOf('Gecko')!=-1)? true:false;
	this.opera = (window.opera)? true:false;
	this.mac = (navigator.userAgent.indexOf('Mac')!=-1)? true:false;
}

//Initialize Client Side JS
var c;

c = new cCore()
c.ua = new BrowserCheck();