/*----------------------------------------------------------------------------
*                                  ajax.js
*             Copyright (c) 2009, AssetShadow, All Rights Reserved.
*     You may not use this code without the express written permission of
*     Assetshadow. You may not redistribute, sell, or offer this code for
*     download, in any form or on any medium, without the express written
*     permission of AssetShadow. This includes, but no limited to, adding
*     it to a script archive or bundling it with other software.
*-----------------------------------------------------------------------------
*  getMethod('url',['params','fnct']);
*	params: 'name=val1&name2=val2&etc';  func: function name.
*
*  postMethod('url',['df','fnct']);
*	df:     'form to post';  func: function name.
*
*  function <fnct>(recvd) {...}
*	This function MUST be added to process the asynchronous Ajax return.
*	The function name defaults to response();
*---------------------------------------------------------------------------*/
var Ajax = {
	busy: false,
	xmlObj: (typeof window.ActiveXObject != 'undefined') ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(),

	getMethod: function(url, params, fnct) {
		if (Ajax.busy) { return; }
		Ajax.busy = true;
		if (params) {
			url = url+'?'+params;
		}
		var fnt = (/\./.test(fnct)) ? fnct.split('.')[1] : (fnct) ? fnct : 'response';
		var obj = (/\./.test(fnct)) ? fnct.split('.')[0] : '';
		Ajax.xmlObj.open("GET", url, true);
		Ajax.xmlObj.onreadystatechange=function() {
			if (Ajax.xmlObj.readyState==4) {
				Ajax.busy = false;
				(obj) ? window[obj][fnt](Ajax.xmlObj.responseText) : window[fnt](Ajax.xmlObj.responseText);
			}
		}
		Ajax.xmlObj.send(null);
	},

	postMethod: function(url, df, fnct) {
		if (!df) return;
		if (Ajax.busy) return;
		Ajax.busy = true;  
		var params = '';
		for (var i=0; i<df.length; i++) {
 			if (df[i].name) {
	 			if (/^(checkbox|radio)$/.test(df[i].type) && !df[i].checked) { continue; }
				params += ((params)?'&':'')+df[i].name+'='+escape(df[i].value);
			}
		}
		var fnt = (/\./.test(fnct)) ? fnct.split('.')[1] : (fnct) ? fnct : 'response';
		var obj = (/\./.test(fnct)) ? fnct.split('.')[0] : '';
		Ajax.xmlObj.open("POST", url, true);
		Ajax.xmlObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		Ajax.xmlObj.setRequestHeader("Content-length", params.length);
		Ajax.xmlObj.setRequestHeader("Connection", "close");
		Ajax.xmlObj.onreadystatechange=function() {
			if (Ajax.xmlObj.readyState==4) {
				Ajax.busy = false; 
				(obj) ? window[obj][fnt](Ajax.xmlObj.responseText) : window[fnt](Ajax.xmlObj.responseText);
			}
		}
		Ajax.xmlObj.send(params);
	}
};

