var do_process;

do_process=false;
function show_processing() {
  var msg=returnObjById('msg_processing');
  if (msg!=null && do_process) msg.style.display="block";
}
function hide_processing() {
do_process=false;
 var msg=returnObjById('msg_processing');
  if (msg!=null) msg.style.display="none";


}
function cursor_wait() {
 
  document.body.style.cursor = 'wait';
  do_process=true;
  setTimeout("show_processing()",600);
  
}

function cursor_clear() {
  document.body.style.cursor = 'default';
  hide_processing();
}
function findPosX(obj)
{



	var curleft = 0;

	

	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
		curleft += obj.offsetLeft
		obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}
function findPosY(obj)
{

	
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
		curtop += obj.offsetTop
		obj = obj.offsetParent;
		}
}
else if (obj.y)
curtop += obj.y;
return curtop;
}
function returnObjById( id ) 
{ 
	if (document.getElementById) 
		var returnVar = document.getElementById(id); 
	else if (document.all) 
		var returnVar = document.all[id]; 
	else if (document.layers) 
		var returnVar = document.layers[id];
		 
	return returnVar; 
}

var xmlreqs = new Array();
function CXMLReq(destination, xmlhttp) {
	this.destination = destination;
	this.xmlhttp = xmlhttp;
}


function createXMLHttpRequest(){
    if(typeof XMLHttpRequest != "undefined"){
        xmlHttp = new XMLHttpRequest();
    }
    else if(typeof window.ActiveXObject != "undefined"){
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");
        }
        catch(e){
            try {
                xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
            }
            catch(e){
                try {
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch(e){
                    xmlHttp = null;
                }
            }
        }
    }
	return xmlHttp;
}

function loadXMLDoc(url,vars, destination)
{
cursor_wait();
xmlhttp=null
// code for Mozilla, etc.

xmlhttp=createXMLHttpRequest();

if (xmlhttp!=null)
  {
  
  
  xmlhttp.onreadystatechange=state_Change

  
  xmlhttp.open("POST",url,true);
  xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
//alert(vars);
  xmlhttp.send(unescape(vars));
  
  var xmlreq = new CXMLReq(destination, xmlhttp);
	xmlreqs.push(xmlreq);

   // alert(url);
  }
else
  {
  alert("Your browser does not support XMLHTTP.")
  }
}

function state_Change()
{

if (typeof(window['xmlreqs']) == "undefined") return;
	var xmldoc = null;
	var destination=null;
	for (var i=0; i < xmlreqs.length; i++) {
		if (xmlreqs[i].xmlhttp.readyState == 4) {
			if (xmlreqs[i].xmlhttp.status == 200 || xmlreqs[i].xmlhttp.status == 304) {
				if (document.implementation && document.implementation.createDocument) {
					xmldoc = document.implementation.createDocument("", "", null);
				} else if (window.ActiveXObject) {
					xmldoc = new ActiveXObject("Microsoft.XMLDOM");
				}
				xmldoc = xmlreqs[i].xmlhttp.responseText;
				
				destination=xmlreqs[i].destination;
				xmlreqs.splice(i,1); i--;
				handle_response(xmldoc,destination);
			} else {
				// error
				xmlreqs.splice(i,1); i--;
			}
		}
	}

 }    



function handle_response(xmldoc,destination) {
	
	if (destination!=null) {
		cursor_clear();
		var obj=returnObjById(destination);
		if (obj==null) return;
		//alert(destination);
		//alert(xmldoc);
		if (xmldoc.indexOf("<!-- NULL -->")==-1) { obj.innerHTML="";
				
		
		while(obj.hasChildNodes()) 
			{ 
			//alert("HERE");
			oDiv.removeChild(obj.childNodes[0]);
			 
			} 
		}
	
		if (xmldoc.indexOf("<!-- NULL -->")==-1) obj.innerHTML=xmldoc;
		//obj.innerHTML="abc";
		var onBind="on_bind_"+destination+"();";
		var start_script=xmldoc.indexOf("<script>");
		
		var end_script=xmldoc.indexOf("</script>");
		if (start_script>-1) {
			var js=xmldoc.substring(start_script+8,end_script);
			//alert(js);
			eval(js);
		
			
		}
		try
			  {
			 	eval(onBind);
			  }
			catch(err)
			  {
				
			}		
	}
	
}

function generateRequest(form_id) {
var response=new String();
 var ii=0;
for (z=0;z<=3;z++) { 
if (document.forms[z]!=null) {
//  var obj=returnObjById(form_id);
  //	if (obj!=null){
		
	  var x = document.forms[z].elements;
		//alert("here");
	//  var x=obj.elements;
	 
	// alert("here2"+x.length);
	 for (var i=0;i<x.length;i++)
	 {
	 // alert(i);
	  if (x[i].name!=null && x[i].name!=""){
		  
		   if (getElementValue(x[i])!=null) {
		   	 val=new String(getElementValue(x[i]));
		   	 if (ii>0) response=response+"&";
			response=response+x[i].name+"="+val.replace(/&/g,"^^^^^*^^^^^");
			//alert(response);
		  	 ii++;
			 }
	  }
	    // notify user of error
	}
	
 }
 else {
 	//alert('form ['+form_id+'] not found!');
 }

} 
return response;

}


function getElementValue(formElement)
{
	
	if(formElement.length != null) var type = formElement.type;
	//alert(formElement.type);
	if((typeof(type) == 'undefined') || (type == 0)) var type = formElement.type;
	
	switch(type)
	{
		case 'undefined': return;

		case 'radio':
			if (formElement.checked==true) {
				return formElement.value;
			}
			else return null;
		case 'select-one':
			if (formElement.options==null) {
			for(var x=0; x < formElement.length; x++) 
				if(formElement[x].checked == true)
			return formElement[x].value;
			
			return;
			}
			if (formElement.options.selectedIndex>-1) return formElement.options[formElement.options.selectedIndex].value;
			return ;
		case 'select-multiple':
			var myArray = new Array();
			for(var x=0; x < formElement.length; x++) 
				if(formElement[x].selected == true)
					myArray[myArray.length] = formElement[x].value;
			return myArray;

		case 'checkbox': {
			if (formElement.checked) return formElement.value;
			return null;
		}
		default: return formElement.value;
	}
}

function bind(destination,resource_id,form_id,def_value) {
	var form_data=generateRequest(form_id);
	//alert(destination);
	loadXMLDoc("ajax_call.php","resource_id="+resource_id+"&form_id="+form_id+"&def_value="+def_value+"&"+form_data,destination);
	
}

function call_php_proc(form_id,proc_name,parameters) {
	var form_data=generateRequest(form_id);
	//alert(form_data);
	loadXMLDoc("ajax_call.php","form_id="+form_id+"&ajax_proc="+proc_name+"&"+parameters+"&"+form_data,null);
	
}

function fire_event(form_id,res_id,event_fired) {
	var form_data=generateRequest(form_id);
//	alert(form_data);
	loadXMLDoc("ajax_call.php","form_id="+form_id+"&resource_id="+res_id+"&event_fired="+event_fired+"&"+form_data,form_id+"__"+res_id);
	
}

function rebind(form_id,res_id,timeout) {
	return setTimeout("bind('"+form_id+"__"+res_id+"',"+res_id+",'"+form_id+"',null)",timeout);
}