/*  
/*--------------------------------------------------------------------------*/

function $(theName)
{
	return document.getElementById(theName);	
}

function $$(theTag)
{
	return document.getElementsByTagName(theTag);	
}

var jsl = {
	'Version'	: '0.1',
	'browser'	: {
		isIE		: function(){ return navigator.userAgent.toLowerCase().indexOf('msie')!=-1 && navigator.userAgent.toLowerCase().indexOf('opera')==-1 && navigator.userAgent.toLowerCase().indexOf('webtv')==-1;},			
		isFirefox	: function(){ return navigator.userAgent.toLowerCase().indexOf('firefox')!=-1;},
		isOpera		: function(){ return navigator.userAgent.toLowerCase().indexOf('opera')!=-1;},
		isSafari	: function(){ return navigator.userAgent.toLowerCase().indexOf('webkit')!=-1;},
		isNN		: function(){ return navigator.userAgent.toLowerCase().indexOf('netscape')!=-1 || navigator.userAgent.toLowerCase().indexOf('navigator')!=-1;},
		isFlock		: function(){ return navigator.userAgent.toLowerCase().indexOf('flock')!=-1;},
		isKonqueror	: function(){ return navigator.userAgent.toLowerCase().indexOf('konqueror')!=-1;},
		isWebTV		: function(){ return navigator.userAgent.toLowerCase().indexOf('webtv')!=-1;}
	},
	
	// AJAX engine
	AJAX	: {
		activeRequests	: {},
		create	: function()
		{
			var xhr = null;
			if (window.XMLHttpRequest) {
				try {
					xhr = new XMLHttpRequest();
				} catch (e){}
			} else if (window.ActiveXObject) {
				try {
					xhr = new ActiveXObject('Msxml2.XMLHTTP');
				} catch (e){
				  try {
					  xhr = new ActiveXObject('Microsoft.XMLHTTP');
				  } catch (e){}
				}
			}
			return xhr;
		},
		/*
			load : function to load an AJAX response text into an HTML element
			parametres:
			theURL - the URL to get the contents from
			theElm - an element to load response text to
			theParams - an array of parametres to set (JSON format)
				# reqName : a request name. It is useful to abort the last request if a new one is similar
				# method : GET or POST
				# onBeforeStart : function to execute before sending the request
				# onBeforeComplete : function to execute before completing the request
				# onListen : function to execute while the stateListener function works 
				# stateListener : an "onreadystate" listener function
				# onLoad  : a function to execute when the request completes
				# onAbort  : a function to execute when the request is aborted
				# headers : headers to set. requers an object in JSON format.
				# qParams : query parametres
				# noCache : a flag for the request not to be cached
		*/
		load : function(theURL, theElm, theParams)
		{
			if (typeof(theURL)=='undefined') return false;
			var ajx = jsl.AJAX;
			var theXHR = {};
			theXHR.XMLHttp = ajx.create();
			theXHR.aborted = false;
			var rPara = null;
			
			if (typeof(theParams)!='undefined')
			{
				// Set the onreadystetechange event handler. If not present use the default
				if (theParams.stateListener) { theXHR.XMLHttp.onreadystatechange = function(){theParams.stateListener(theXHR);} }
				else {
					var ls = ajx.listener;
					theXHR.XMLHttp.onreadystatechange = function(){ls(theXHR);}
				}
				// Execute the onListen function if present
				if (theParams.onListen) {theXHR.onListen = theParams.onListen; }
				
				// Set the onLoad event handler. If not present use the default
				if (theParams.onLoad) { theXHR.onLoad = function(){ theParams.onLoad(theXHR);} }
				else { 
					theElm = (typeof(theElm)!='undefined')?theElm:null;
					theXHR.loadInto = theElm;
					theXHR.onLoad = function(xhr){ ajx.loadInto(theXHR);}
				}
				// Execute the onBeforeLoad function if present
				if (theParams.onBeforeLoad) {theXHR.onBeforeLoad = theParams.onBeforeLoad; }
							
				// Set the request method. If not present use the default
				if (theParams.method) {theXHR.method = theParams.method; }
				else {theXHR.method = 'GET'; }
				
				// Prepare the query parametres if exist
				if (theParams.qParams)
				{
					rPara = '';
					for (var pn in theParams.qParams)
					{
						rPara += '&'+escape(pn)+'='+escape(theParams.qParams[pn]);
					}
					if (theXHR.method == 'GET') // add the parametres to the URL
					{
						var fl = /.*\?.*/.test(theURL);
						theURL += fl?rPara:'?'+rPara;
						//alert(fl);
						rPara=null;
					}
					else { /* POST */ }
				}
				
				// Set the async flag. If not present use the default
				if (typeof(theParams.async)!='undefined') {theXHR.async = theParams.async?true:false; }
				else {theXHR.async = true; }
				
				// Set the onAbort event handler if present
				if (theParams.onAbort) theXHR.onAbort = theParams.onAbort;
				
				// Add the request into the active request object if reqName is present
				if(theParams.reqName)
				{
					// Abort the previous request with the similar reqName is exists
					if (ajx.activeRequests[theParams.reqName]) { ajx.abort(ajx.activeRequests[theParams.reqName]); ajx.activeRequests[theParams.reqName] = null;}
					ajx.activeRequests[theParams.reqName] = theXHR;
				}
				// Execute the onBeforeStart function if present
				if (theParams.onBeforeStart) {theParams.onBeforeStart(); }
			}
			else	// Set default values
			{
				// Setting the default onreadystatechange event handler
				var ls = ajx.listener;
				theXHR.XMLHttp.onreadystatechange = function(){ls(theXHR);};
				theXHR.method	= 'GET';	// Set the request method to GET
				theXHR.async	= true;		// Default async flag - true
				// Try to set the elemnt to load the response text into
				if (typeof(theElm) !='undefined' && theElm.nodeType==1)
				{
					theElm = (typeof(theElm)!='undefined')?theElm:null;
					theXHR.loadInto = theElm;
				}
				theXHR.onLoad = function(xhr){ ajx.loadInto(theXHR);}
			}
			theXHR.XMLHttp.open(theXHR.method, theURL, theXHR.async);
			try{
				// Set the request headers if present
				if (typeof(theParams.headers)!='undefined') { for (var hn in theParams.headers){ajx.setHeader(theXHR,hn, theParams.headers[hn]);}}
				// Set the POST method header
				if (theXHR.method == 'POST') { ajx.setHeader(theXHR,"Content-Type", "application/x-www-form-urlencoded"); }
				// Set the "no-cahe" header
				if (theParams.noCache) {
					ajx.setHeader(theXHR,"If-Modified-Since", "Thu, 01 Jan 1970 00:00:00 GMT");
					ajx.setHeader(theXHR,"Pragma", "no-cache");
					ajx.setHeader(theXHR,"Cache-Control", "no-cache");
				}
			} catch (e){}
			theXHR.XMLHttp.send(rPara);
		},
		
		listener : function(xhrObj) 
		{
			try // Fixing the Mozilla bug on Network error AND on aborted request
			{
				if (xhrObj.aborted) return;
				if (xhrObj.XMLHttp.readyState == 4)
				{
					if (xhrObj.XMLHttp.status == 200)
					{ 
						if (xhrObj.onBeforeLoad) { xhrObj.onBeforeLoad(xhrObj);}
						xhrObj.onLoad(xhrObj);	
					}
					else
					{
						if (xhrObj.onError) xhrObj.onError(xhrObj);	
					}
				}
				else
				{
					if (xhrObj.onListen) { xhrObj.onListen(xhrObj);}	
				}
			} catch(e){}
		},
		
		loadInto	: function(xhrObj)
		{
			if (typeof(xhrObj.loadInto)!='undefined')
			{
				xhrObj.loadInto.innerHTML=xhrObj.XMLHttp.responseText;	
			}
		},
		
		abort		: function(xhrObj)
		{
			if (typeof(xhrObj)=='undefined') return false;
			xhrObj.aborted = true;
			if (xhrObj.onAbort) xhrObj.onAbort(xhrObj);
			xhrObj.XMLHttp.abort();
		},
		
		setHeader	: function(xhrObj, hName, hVal)
		{
				xhrObj.XMLHttp.setRequestHeader(hName, hVal);
		}
	}
	// end of AJAX engine
}

jsl.DOM = {
	DOMReady	: false,
	addListener	: function(elm,evt, listener)
	{
			
	}
}

