var _msg_loading = "Loading";
var _loadingImg = path_name+"images/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"><font color="#000066"><b><img src="'+path_name+'images/Loading.gif"></b></font></div>';
var _loadingDiv = '<td colspan="9" align="center" id="wait"><img src="'+path_name+'images/Loading.gif"></td>';
include(path_name+"prototype/dist/prototype_orig.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);
//		alert (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 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);
		url = script+encParams;
	}
	return url;
}
function applyDecodeURI(element) {
	try{
		decoded = decodeURIComponent(element);
	}catch(e){
		decoded = unescape(element);
	}
	return decoded;
}

function include(scriptFilename) {
	document.write('<' + 'script');
    document.write(' language="javascript"');
    document.write(' type="text/javascript"');
    document.write(' src="' + scriptFilename + '">');
    document.write('</' + 'script' + '>');
}