var LoadingImage=site_url("assets/images/loading.gif");
var _LoadingDiv="";

//---------------------------------------------------------------------------AJAX ACT - Single

var last_act = 0;
var act_data = {};
//Postvar  = {var1:value1,var2:value2}
//param.form =["input element name"]
//param.loading ="" // Default, divname , "_self" , "_none"
//param.callback : callback script on success
function act(url,divid,postvar,param){
	var v;

	//SET VALUE
	act_id = "a-" + Math.random();

	v={act_id:act_id};
	form = null;
	if(param && param.form) form = param.form;

	if(form!=null){
		if (typeof(form) == "string") {
			form=[form];
		}

		try{
      var el;
			for (i = 0; i < form.length; i++){
        if($(form[i]).tagName=='FORM'){
          Object.extend(v ,$F(form[i]).serialize(true));
        }else{
				  try{
					  for (j = 0; j < $(form[i]).elements.length; j++){
              el=$(form[i]).elements[j];
              if(el.type=='check'||el.type=='radio'){
                if(el.checked)
                  v[el.id] = el.value;
              }else{
						    v[el.id] = el.value;
              }
					  }
				  }catch(e){
					  v[form[i]] = $F(form[i]);
				  }
        }
			}
		}catch(e){}

	}

	if(postvar!=null)
		Object.extend(v ,postvar);

	v.AJAX=true;

	//Loading
	loading = null;
	if(param){
		if(param.loading){
			try{
				loading = param.loading;
				if(divid!=null){
					if(loading=="_self"){
						$(divid).innerHTML= "<P><center><img src='" + LoadingImage + "'></center></p>";
					}
				}
				if(loading!="_self"&&loading!="_none"){
					if(loading!=null)
						_LoadingDiv=loading;
					if(_LoadingDiv!=""){
						try{
							$(_LoadingDiv).style.display='';
							$(_LoadingDiv).style.visible='visible';
		          /*setTimeout(function(){$(_LoadingDiv).style.display='';
		          $(_LoadingDiv).style.visible='visible';},1000);*/
						}catch(e){	}
					}
				}
			}catch(e){	}
		}
	}
	new Ajax.Request(site_url(url),{parameters:v,
		onComplete:onAjaxSucess_single,
    onFailure: function() {
        msbox("Error loading.. Please try again");
      }
  });
  last_act = act_id;
  act_data[act_id] = [url,divid,postvar,param];
}

function act_repeat(){
	act(last_act[0],last_act[1],last_act[2],last_act[3]);
}

function  onAjaxSucess_single(t) {
	var response="res = " + (t.responseText);
	try{
		eval(response);
		//check act_id
		if(!act_data[res.act_id]) return;
		divid = act_data[res.act_id][1];
		param = act_data[res.act_id][3];

		//main output
		try{
			if(divid)
				if(divid!=""){
					$(divid).innerHTML = res.output;
          tooltip_process(divid);
				}
		}catch(e){	}

		//sub output
		if(res.content){
			try{
				for(key in res.content){
					$(key).innerHTML = res.content[key];
          tooltip_process(key);
				}
				}catch(e){	}
		}

		//script
		try{
			eval(res.script);
			if(param && param.callback)
				param.callback(res);
		}catch(e){
			alert('can\'t run ' + res.script +'\n' + e)
		}

		try{
			if(_LoadingDiv!=""){
		    $(_LoadingDiv).style.display='none';
				$(_LoadingDiv).style.visible='hidden';
			}
      /*
      if($('msbox_bg').style.display == "none"&&$('msbox_bg_in').style.display != "none"){
        $('msbox_bg_in').style.display = "none";
      }*/
		}catch(e){	}
	}catch(e){
		if (response != "") {
			alert("Error parsing : " + response);
		}
	}
}