/*
	xmlHttp(url,layerName,method,moreParams);


*/

var _msg_loading = "Loading";
var _loadingImg = "Loading.gif";
//var _loadingDiv = "<table border='0' cellpadding='0' cellspacing='0'><tr><td id='loading'><img src='"+_loadingImg+"' alt='"+_msg_loading+"' width='16' height='16' /></td><td class='loading'>&nbsp;"+_msg_loading+"...</td></tr></table>";
var _loadingDiv = '<div align="center"  id="wait"></div>';

include("js/prototype.js");

function xmlHttp(url,layerName,method,moreParams,asynchron){
	var postParameters = '';
	var loadingDiv = 1;
	var urlsplit =url.split('?');
	var urlExt = urlsplit[0].substring(urlsplit[0].length-3,urlsplit[0].length);//.php?
	if(moreParams){
		if(moreParams.form){
			formObj = eval('document.'+moreParams.form);
			postParameters = Form.serialize(formObj);
		}else if(moreParams.params){
			postParameters = moreParams.params;
		}else{
			postParameters = '';
		}
		if(moreParams.redirectLink){
			redirectLink = moreParams.redirectLink;
		}else{
			redirectLink = '/';
		}
		if(moreParams.loadingDiv >= 0){
			loadingDiv = moreParams.loadingDiv;
		}
	}
	if (!moreParams || !moreParams.form) {
		url = applyEncodeURI(url);
		url = url.replace(/%2F/g,'/');//doesn't affect functionality, just to show the paths
	}

	if(typeof(layerName) == "string"){
		var layerArray = layerName.split(",");
	}else{
		var	layerArray = [layerName];
	}

	var ajax = new Ajax.Request(
		 url,
		 {
			asynchronous: asynchron ? asynchron : true,
			method: method,
			parameters: postParameters,
			onLoading: function(req){
				if(req.readyState == 1 && loadingDiv){
					for(i=0; i<layerArray.length; i++){
						$(layerArray[i]).innerHTML = _loadingDiv;
					}
				}
			},
			onSuccess: function(req){
				if(req.responseText == 'redirect'){
					window.location = redirectLink;
				}else{
					if (layerName=='') {
						setTimeout(function() {req.responseText.evalScripts()}, 10);
					} else {
						for(i=0; i<layerArray.length; i++){
							$(layerArray[i]).innerHTML = req.responseText.stripScripts();
							setTimeout(function() {req.responseText.evalScripts()}, 10);
						}
					}
				}
			}
		 }
	)
}

function xmlHttpUtf8(url,layerName,method,moreParams){
	url = '../include/utf8Encoder.php?urlEncoder='+url.replace(/\?/,'&');
	xmlHttp(url,layerName,method,moreParams);
}

function xmlHttpQueueUtf8(url,layerName,method,moreParams){
	//this.url += (this.url.match(/\?/) ? '&' : '?') + parameters;
	url += (url.match(/\?/)?'&':'?') + escape('!fromQueue') + '=1';
	xmlHttpUtf8(url,layerName,method,moreParams);
}

function xmlHttpQueue(url,layerName,method,moreParams){
	//this.url += (this.url.match(/\?/) ? '&' : '?') + parameters;
	url += (url.match(/\?/)?'&':'?') + escape('!fromQueue') + '=1';
	xmlHttp(url,layerName,method,moreParams);
}
function applyEncodeURI(url) {
	//encodeURIComponent
	var idQuote = url.indexOf('?');
	var idAnd = url.indexOf('&');
	var startPos=-1;
	if (idQuote!=-1) {
		startPos = idQuote;
	} else if(idAnd!=-1) {
		startPos = idAnd;
	}
	if (startPos!=-1) {
		//alert(startPos);
		var script = url.substring(0,startPos+1);
		var params = url.substring(startPos+1);
		var pairsArr = params.split('&');
		//alert(pairsArr);

		var encParams='';
		for(var i=0;i<pairsArr.length;i++) {
			var pairArr = pairsArr[i].split('=');
			if (pairArr.length==2) {
				encParams += '&'+encodeURIComponent(applyDecodeURI(pairArr[0]))+'='+encodeURIComponent(applyDecodeURI(pairArr[1]));
			} else {
				encParams += encodeURIComponent(applyDecodeURI(((i>0)?'&':'1')+pairsArr[i]));/* 1 is a dummy, it will be trimmed*/
			}
		}
		encParams = encParams.substring(1);
		//alert(encParams);
		url = script+encParams;
		//alert(url);
		//document.write(encParams);
			/*alert(script);
			alert(params);*/
	}
	return url;
}
function applyDecodeURI(element) {
	try{
		decoded = decodeURIComponent(element);
	}catch(e){
		decoded = unescape(element);
	}
	return decoded;
}
/*function applyEncodeURI(url) {
	//encodeURIComponent
	var idQuote = url.indexOf('?');
	var idAnd = url.indexOf('&');
	var startPos=-1;
	if (idQuote!=-1) {
		startPos = idQuote;
	} else if(idAnd!=-1) {
		startPos = idAnd;
	}
	if (startPos!=-1) {
		//alert(startPos);
		var script = url.substring(0,startPos);
		var params = url.substring(startPos);
		var encParams='';
		try{
			encParams = decodeURI(params);
		}catch(e){
			//alert('caught');
			encParams = unescape(params);
		}
		encParams = encodeURI(encParams);
		encParams = encParams.replace(/\+/g,'%2B');
		url = script+encParams;
		//alert(url);
		//document.write(encParams);
			/*alert(script);
			alert(params);* /
	}
	return url;
}*/

function include(scriptFilename) {
	document.write('<' + 'script');
    document.write(' language="javascript"');
    document.write(' type="text/javascript"');
    document.write(' src="' + scriptFilename + '">');
    document.write('</' + 'script' + '>');
}