
/**
	File:		im_ajax.js
	Written By:	RonB, AlbertH
	Date:		November 22, 2007
	Date:		December 27, 2007
	version:	1.1.0
*/

var im_ajax = {
	
	isAjaxProcessing: false,
	isDebug: false,
	xmlhttp: null,
	_retFunc: null,

	send: function(page, retFunc, extras, isFromSalat, isGetXML, extra_params_obj){
		if (this.isAjaxProcessing) return false;
		if (typeof(isFromSalat)== "undefined") isFromSalat = false;
		if (typeof(isGetXML)== "undefined") isGetXML= false;
		if (typeof(extras)=="undefined") extras = '';
		if (typeof(extra_params_obj)=="undefined") extra_params_obj = '';
		this.xmlhttp = this.xmlhttp||this._createXMLHTTPObject();
		extras = 'file='+page+'&'+extras;
		if (this.xmlhttp){
			if (isFromSalat){
				this.xmlhttp.open("POST", "/salat2/_ajax/ajax.index.php", true);
			}else{
				this.xmlhttp.open("POST", "/_ajax/ajax.index.php", true);
			}
			if (this.isDebug) alert('before ajax');
			this._retFunc = retFunc;
			this.xmlhttp.onreadystatechange = function(){
				//alert(im_ajax.xmlhttp);
				if (im_ajax.xmlhttp.readyState == 4){ if (im_ajax.xmlhttp.status == 200){
					if (im_ajax.isDebug) alert('in ajax');
					im_ajax.isAjaxProcessing = false;
					document.body.style.cursor = "auto";
					retFunc(isGetXML ? im_ajax.xmlhttp.responseXML : im_ajax.xmlhttp.responseText, extra_params_obj);
				}}
				 /*try{ if (im_ajax.xmlhttp.readyState == 4){ if (im_ajax.xmlhttp.status == 200){
					if (im_ajax.isDebug) alert('in ajax');
					im_ajax.isAjaxProcessing = false;
					document.body.style.cursor = "auto";
					retFunc(checkFuncAfterLogin(isGetXML ? im_ajax.xmlhttp.responseXML : im_ajax.xmlhttp.responseText), extra_params_obj);
				}}}catch(e){ 
					im_ajax.isAjaxProcessing=false;
					//alert("XMLHTTP Error\n\n"+e.message); 
					im_center.show('sendtofriend', 
						im_center.templates.compile({
							'TITLE' : 'Server Error',
							'CONTENT' : e.message+"<br /><br />Please check your internet connection"
						})
					);
				 }*/
			};
			this.xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			// send request
			this.isAjaxProcessing = true;
			this.xmlhttp.send(extras);
			document.body.style.cursor = "wait";
			return (true);
		}else{
			alert("You browser does not support Ajax functionality");
			return (false);
		}
	},
		
	innerData: function(id, data, addingPos){
		if(typeof(id)=='object'){
			if(typeof(addingPos)=='undefined' || !addingPos){
				id.innerHTML = data;
			}else if(addingPos=='begin' || addingPos=='before'){
				id.innerHTML = data + id.innerHTML;
			}else if(addingPos=='end' || addingPos=='after'){
				id.innerHTML += data;
			}
		}else{
			if(typeof(addingPos)=='undefined' || !addingPos){
				this.obj(id).innerHTML = data;
			}else if(addingPos=='begin' || addingPos=='before'){
				this.obj(id).innerHTML = data + this.obj(id).innerHTML;
			}else if(addingPos=='end' || addingPos=='after'){
				this.obj(id).innerHTML += data;
			}
		}
	},
	
	evalScript: function(){
		var tmp = document.getElementById('ajaxScript');
		if (tmp){
			if(tmp.innerHTML && tmp.innerHTML!=''){
				try{
					eval(tmp.innerHTML);
					tmp.parentNode.removeChild(tmp);
				}catch(e){
					//alert("e1: "+e.message);
				}
			}
		}
	},
	
	sendMulti: function(arrReqs,isFromSalat){
		//
	},
	
	_createXMLHTTPObject: function(){
		var xmlhttpTmp = false;
		var factories = this._XMLHttpFactories();
		for (var i=0;i<factories.length;i++){
			try{
				xmlhttpTmp = factories[i]();
			}catch (e){
				continue;
			}
			break;
		}
		return xmlhttpTmp;
	},
	
	_XMLHttpFactories: function(){
		return[
			function () {return new XMLHttpRequest()},
			function () {return new ActiveXObject("Msxml2.XMLHTTP")},
			function () {return new ActiveXObject("Msxml3.XMLHTTP")},
			function () {return new ActiveXObject("Microsoft.XMLHTTP")}
		];
	}
	
};
