register("SYM.util.xhr");
register("SYM.util.xhr.load");
SYM.util.xhr =function( url ,  callback , szvalues, obj)
{
	this.timeout = 0;
	this.url = url;
	this.szvalues = szvalues;
	this.status = 0;	
	this.callbackfunction = callback;
	this.custom = null;
	this.debug = false;
	this.cache = true;
	this.callbackobj = obj; // if we are sending a object, this means that an instance is calling us.
	this.init = function()
	{
		if (status == 0)
		{
			if ( this.url.substring(0,4) == "http" )
			{
				/* SET TO USE PASSTHRU ACCESS INSTEAD */
				this.url = "/" + dbfp + "/ajaxpassthru?openagent&now="+new Date()+"&url=" + this.url;
			}else
			{
				if ( !this.cache )
				{
					this.url += "&now=" + new Date();
				}
			}
			this.load();			
		}
	}	
		
	this.inform = function()
	{	
		try
		{
			if (this.debug)
			{
				alert(this.xmlhttp.responseText);
			}	
				
			if (SYM.util.isString(this.callbackfunction))
			{
				if (this.callbackobj == null)
				{
					eval( this.callbackfunction);
				}else
				{
					eval( "this.callbackobj." + this.callbackfunction )
				}
			}else
			{
				this.callbackfunction();
			}
		}catch(oou){ if(this.debug){alert("error callback");} }
	}
	
	this.load = function load()
	{
		if(navigator.appName == "Microsoft Internet Explorer"){
			this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}else{ 
			this.xmlhttp = new XMLHttpRequest(); 
		}
		if (this.xmlhttp)
		{		
			var oThis = SYM.bind(this.callback , this);
			this.xmlhttp.onreadystatechange = function() { oThis() }
			if (this.szvalues == null)
			{
				this.xmlhttp.open("GET",this.url,true)
				this.xmlhttp.setRequestHeader("Connection", "close");
				this.xmlhttp.send(null);
			}else
			{
				this.xmlhttp.open('POST', this.url, true);
				this.xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				this.xmlhttp.setRequestHeader("Content-length", this.szvalues.length);
				this.xmlhttp.setRequestHeader("Connection", "close");
				this.xmlhttp.send(this.szvalues);
			}
			this.status  = 1;
		}
	}
	this.getText = function()
	{
		if ( this.status == 200 && this.xmlhttp != null )
		{
			return this.xmlhttp.responseText;
		}else
		{
			return "";
		}
	}	
	this.callback = function()
	{
		if (this.xmlhttp.readyState==4)
		{
			if (this.xmlhttp.status==200)
	    		{
				this.status = 200;
			}else
			{this.status = 404;}
			this.inform();
		}  
	}
	
}
getForm = function ( szform )
{
	var szvalues = "";
	var elements = document.forms[szform].getElementsByTagName("INPUT");
	for (i =0  ; i < elements.length; i++)
	{
		field = elements[i];
		if (field.type != "file" && field.name != "")
		{
			if ( szvalues !=  ""){szvalues +="&amp;"}
			szvalues += field.name + "=" + encodeURIComponent (getFieldValue(field.type  , field ));
		}
	}
	var elements = document.forms[szform].getElementsByTagName("SELECT");
	for (i =0  ; i < elements.length; i++)
	{
		field = elements[i];
		if ( szvalues !=  ""){szvalues +="&amp;"}
		szvalues += field.name + "=" + encodeURIComponent (getFieldValue(field.type  , field ));
	}
 
	var elements = document.forms[szform].getElementsByTagName("TEXTAREA");
	for (i =0  ; i < elements.length; i++)
	{
		field = elements[i];
		if ( szvalues !=  ""){szvalues +="&amp;"}
		szvalues += field.name + "=" + encodeURIComponent(getFieldValue(field.type  , field ));
	}
	return szvalues; 
} 

SYM.util.xhr.load = function ( url , div )
{
	
	if (!document.addEventListener && document.readyState != "complete")		
	{		
		
		SYM.util.dom.onLoaded(   function() {SYM.util.xhr.load( url,div ) }  )
		return;
	}
 
	this.div = div;
	this.obj = new SYM.util.xhr( url , "setDiv(this.xmlhttp,'"+ this.div +"')" , null , this );
	this.obj.init();
	this.txt = "";
	
	this.setDiv = function( obj , divname )
	{
		var o = SYM.util.dom.getObject( divname );
		if (o != null)
		{
			o.innerHTML = "";
			var txt = obj.responseText 
			var a = document.createElement('div');
			a.innerHTML = txt
			o.appendChild(a);			
		}
	}
}
 

