/*
	xmlHttp(url,layerName,method,moreParams);

	put thise line in your template
		<script src="../js/ajax.js" type="text/javascript"></script>

	example script
		http://castor/modules/ajax

	ex.
	xmlHttp('../phpScript/redirect.php','','get',{redirectLink:'http://castor/doc/'}); // in case or redirect layerName is not mandatory
	xmlHttp('post.php','layer2','post',{form:'formLogin',redirectLink:'../main/hm.php',loadingDiv:0});
		- form - form name
		- redirectLink - redirect if ...
			return text of php script must by echo('redirect');
			and the site goes to redirectLink
		- loadingDiv:0 - without loading animation
					 1 - with loading animation
					 default is 1
		- in case of session expire use ../include/sessionExpire.php
*/

var _msg_loading = "Loading";
var _loadingImg = "../img/loader.gif";
var _loadingDiv = "<table border='0' cellpadding='0' cellspacing='0'><tr><td><img src='"+_loadingImg+"' alt='"+_msg_loading+"' width='16' height='16' /></td><td>&nbsp;"+_msg_loading+"...</td></tr></table>";
include("../js/prototype_1.6.0.2.js");

function xmlHttp(url,layerName,method,moreParams){
	var postParameters = '';
	if(moreParams){
		if(moreParams.form){
			formObj = eval('document.'+moreParams.form);
			postParameters = Form.serialize(formObj);
		}else{
			postParameters = '';
		}
		if(moreParams.redirectLink){
			redirectLink = moreParams.redirectLink;
		}else{
			redirectLink = '/';
		}
		if(moreParams.loadingDiv >= 0){
			loadingDiv = moreParams.loadingDiv;
		}else{
			loadingDiv = 1;
		}
	}

	var layerArray = layerName.split(",");
	var ajax = new Ajax.Request(
		 url,
		 {
			asynchronous: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{
					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 include(scriptFilename) {
	document.write('<' + 'script');
    document.write(' language="javascript"');
    document.write(' type="text/javascript"');
    document.write(' src="' + scriptFilename + '">');
    document.write('</' + 'script' + '>');
}