//Call a url and send the returned content to the method <methodName>
//Scripts in called url are not evaluated
function ajaxCall(type,url,pars,methodName){
	pars += "&isAjaxCall=1";
	var myAjax;
	try{
			if(methodName!=""){			  
				myAjax= new Ajax.Request(				
						url, 
						{
							method: type, 
							parameters: pars, 
							onSuccess: eval(methodName),
							onException: exception
						});
			}else{		
				myAjax= new Ajax.Request(
						url, 
						{
							method: type, 
							parameters: pars
						});
			}		
	}catch(e){}
	
}
//Call a url and evaluate the scripts found in this url
function ajaxCallEvalScripts(type,url,pars){
	pars += "&isAjaxCall=1";
	var myAjax;
	try{			
		myAjax= new Ajax.Request(
				url, 
				{
					onSuccess: evalScripts,
					method: type, 
					parameters: pars
				});
					
	}catch(e){}
}
//Call a url and update the content in div with the content found in the url
//Scripts in url are evaluated
function ajaxUpdate(type,url,pars,div){
	pars += "&isAjaxCall=1";
	var myAjax = new Ajax.Updater(
				div,												
				url, 
				{
					evalScripts: true,
					method: type, 
					parameters: pars				
				});	
}
//Call a url and update the content in div with the content found in the url
//Scripts in url are evaluated
function ajaxUpdateSynchronous(type,url,pars,div){
	pars += "&isAjaxCall=1";
	var myAjax = new Ajax.Updater(
				div,												
				url, 
				{
					evalScripts: true,
					method: type, 
					parameters: pars,
					asynchronous: false
				});	
}

//Call a url and update the content in div with the content found in the same div id found in the url
//Scripts in url are not evaluated
function ajaxReplace(type,url,pars,div){
	pars += "&isAjaxCall=1";
   if(window.ActiveXObject)
    var e = new Effect.Fade(div, {from:1.0, to:0.00});
   var myAjax= new Ajax.Request(				
  		url, 
  		{
  			method: type, 
  			parameters: pars, 
  			onSuccess: function(t){replaceDiv(t,div)},
  			onComplete: function(){if(window.ActiveXObject){e.cancel();Effect.Appear(div);}},
  			onException: exception
  		});                     
}
//Call ajaxCall periodically
function ajaxPeriodicalCall(type,url,pars,method,seconds){
 return new PeriodicalExecuter(new Function("ajaxCall('"+type+"','"+url+"','"+pars+"','"+method+"')"), seconds);
}
//Call ajaxCallEvalScripts periodically
function ajaxPeriodicalCallEvalScripts(type,url,pars,seconds){
 return new PeriodicalExecuter(new Function("ajaxCallEvalScripts('"+type+"','"+url+"','"+pars+"')"), seconds);
}
//Call ajaxReplace periodically
function ajaxPeriodicalReplace(type,url,pars,div,seconds){
 return new PeriodicalExecuter(new Function("ajaxReplace('"+type+"','"+url+"','"+pars+"','"+div+"')"), seconds);
}
//Call ajaxUpdate periodically
function ajaxPeriodicalUpdate(type,url,pars,div,interval){
	pars += "&isAjaxCall=1";
	var myAjax = new Ajax.PeriodicalUpdater (
				div,												
				url, 
				{
					evalScripts: true,
					frequency: interval,
					method: type, 
					parameters: pars				
				});	
}
function replaceDiv(o,id){ 
	var t=o.responseText;  
	var myRegExp = new RegExp("<(\\w)+ id=\""+id+"\"", "gi");	
	var str =  new String(t.match(myRegExp));
	var idPos = t.indexOf(str);
	var idPosEnd = 0;
	var divName = str.substr(1,str.indexOf(" ")-1);
	var findStr = new String("<\/"+divName+">");
	var found=false;
	var rest = new String(t.substr(idPos+1,t.length));
	var found=false;	
	var x=1;
	z=0;
	while(!found && z<100){	
	
		var divEnd = rest.indexOf(findStr);
		var divStart =  rest.indexOf("<"+divName);
		if(divStart<divEnd){ x++;
			rest = rest.substr(divStart+1,rest.length);
			idPosEnd+=divStart+1;			 
		}	else{ x--;
			rest = rest.substr(divEnd+1,rest.length);	
			idPosEnd+=divEnd+1;			  
		}	
		z++;
		if(divStart==-1){
			idPosEnd=t.length;
			found=true;
		}		
		if(x==0)found=true;	
	}	
	$(id).innerHTML=t.substr(idPos,idPosEnd+findStr.length);	 	
}
function evalScripts(o){
	var t=o.responseText;  		  		  
	idPos = t.indexOf("<script>")+new String("<script>").length; 
	var	idPos2 = t.indexOf("<\/script>");  
	eval(t.substring(idPos,idPos2));											
}
function exception(element,ex){
  //alert('Ajax Exception: '+ex.message+'.  Please advice webmaster in comment/bugs.')
}
