//ret_func_name is the name of the return function, the return value will be pass to the first argument. eq: fret_function(r) => r is the return variable
//global_loaded_var is a passing by reference of consecutive/unoverlapped processing method (do not define this to thread the process), define this outside of this funtion. eq: globalvarname=1;
//ajax('url.php','var1=test&var2=asdf','ret_func_name','loaded',true,'Please wait still loading');
var ajax_timeout;
function ajax(url,var_pass,ret_func_name,global_loaded_string,useloading,loadingstr){
var req;
if(global_loaded_string!=null && global_loaded_string!=''){
eval("loaded="+global_loaded_string);
}
if(global_loaded_string==null || global_loaded_string=='' || loaded==1){
if(global_loaded_string!=null && global_loaded_string!='') eval(global_loaded_string+"=0");
if(window.XMLHttpRequest){
req=new XMLHttpRequest();
}else if(window.ActiveXObject){
req=new ActiveXObject("Microsoft.XMLHTTP");
}
try{
cancel=false;
req.open("POST",url,true);
req.onreadystatechange=callback;
req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
req.send(var_pass);
if(useloading==true){loading(true,loadingstr);}
ajax_timeout=setTimeout("ajax_cancel('"+global_loaded_string+"')",30000);
}catch(e){};
}

function callback(){
if(req.readyState==4){
if(req.status==200){
clearTimeout(ajax_timeout);
response=req.responseText;
try{
eval(ret_func_name+'(unescape("'+escape(req.responseText)+'"));');
}catch(e){}

loading();
if(global_loaded_string!=null && global_loaded_string!=''){
eval(global_loaded_string+"=1;");
}
}else{
loading();
if(global_loaded_string!=null && global_loaded_string!=''){
eval(global_loaded_string+"=1;");
}
clearTimeout(ajax_timeout);
alert("There was a problem retrieving the data:\n" + req.statusText);
}
}
}
}

function ajax_cancel(global_loaded_string){
alert('Server takes to long to response, the connection might be disconnected, you can try by refreshing the page');
loading();
if(global_loaded_string!=null && global_loaded_string!=''){
eval(global_loaded_string+"=1;");
}
}

function loading(f,loadingstr){
var obj,loadingblock,loadingmark;
if(loadingstr==null || loadingstr=="") loadingstr='PROCESSING<br><br>';
if(f==true){
obj=document.getElementById('loadingblock_id');
if(!obj){
loadingblock=document.createElement('div');
loadingblock.style.position='fixed';
loadingblock.style.width='100%';
loadingblock.style.height='100%';
loadingblock.style.left='0px';
loadingblock.style.top='0px';
loadingblock.style.backgroundColor='#000';
loadingblock.style.filter="alpha(opacity=60)";
loadingblock.style.MozOpacity='0.6';
loadingblock.id='loadingblock_id';
loadingblock.align='center';
document.body.appendChild(loadingblock);
}
obj=document.getElementById('loadingmark_id');
if(!obj){
loadingmark=document.createElement('table');
loadingmark.style.position='fixed';
loadingmark.style.left='0px';
loadingmark.style.top='0px';
loadingmark.style.width='100%';
loadingmark.style.height='100%';
loadingmark.style.fontSize='14px';
loadingmark.style.fontFamily='Tahoma';
loadingmark.style.fontWeight='bold';
loadingmark.style.color='#fff';
loadingmark.style.padding='0px 5px 0px 5px';
loadingmark.id='loadingmark_id';
loadingmark.align='center';
var otbody=document.createElement('tbody');
otbody.align='center';
var otr=document.createElement('tr');
var otd=document.createElement('td');
otr.appendChild(otd);
otbody.appendChild(otr);
otd.innerHTML="<font id='ajax_loading_label'></font><br><br><font id='ajax_loading_bar'>&nbsp;</font>";
loadingmark.appendChild(otbody);
document.body.appendChild(loadingmark);
loadingmark.style.zIndex=loadingblock.style.zIndex+1;
}
document.getElementById('ajax_loading_label').innerHTML=loadingstr;

try{clearTimeout(loadingtimer)}catch(e){};
loadingtimer=setInterval("loadingtick(50)",10);
document.getElementById('loadingmark_id').style.display='';
document.getElementById('loadingblock_id').style.display='';
document.onmousedown=new Function("return false");
document.oncontextmenu=new Function("return false");
document.onkeydown=new Function("return false");
}else{
try{clearTimeout(loadingtimer)}catch(e){};loadingtimer=null;
obj=document.getElementById('loadingblock_id');
if(obj) obj.style.display='none';
obj=document.getElementById('loadingmark_id');
if(obj) obj.style.display='none';
document.onmousedown=null;
document.oncontextmenu=null;
document.onkeydown=null;
}
}

function loadingtick(len){
var obj=document.getElementById('ajax_loading_bar');
if(obj.innerHTML.length<len){
obj.innerHTML+='&#9619; ';
}else{
obj.innerHTML='&#9619; ';
}
}
//copyrighted@2009 by bear_form
