/*
if (document.implementation && document.implementation.createDocument)
{
	Document.prototype.loadXML = function(str) // implement MS loadXML on Mozilla
	{
		var DOMParser = new DOMParser();
		var DOMDocumnt = DOMParser.parseFromString(str,"text/xml"); 
	} 
}
*/

function xmlremote(url)
{
	this.init(url);
}

xmlremote.prototype.method	= "POST";
xmlremote.prototype.action	= '';
xmlremote.prototype.async	= true;
xmlremote.prototype.target	= '';
xmlremote.prototype.data	= '';
xmlremote.prototype.mime	= 'application/x-www-form-urlencoded';
xmlremote.prototype.type	= "Text";
xmlremote.prototype.callback = false;
xmlremote.prototype.readystatechange = false;

//xmlremote.prototype.xmlDoc	= '';

xmlremote.prototype.init	= function(url)
{
	if ( document.implementation && document.implementation.createDocument )//or if(window.XMLHttpRequest)
	{
		//this.xmlDoc		= document.implementation.createDocument("","",null);
		this.xmlhttp		= new XMLHttpRequest();
	}
	else if ( window.ActiveXObject )
	{
		//this.xmlDoc		= new ActiveXObject("Microsoft.XMLDOM");
		//this.xmlDoc.async	= false; //Enforce download of XML file first. IE only.
		this.xmlhttp		= new ActiveXObject("Microsoft.XMLHTTP");
		/*
		try 
		{
		  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) 
		{
			try 
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (E) 
			{
				xmlhttp = false;
			}
		}
		*/
	}

	if (url)
	{
		this.setUrl(url);
	}
}

xmlremote.prototype.setFormMethod	= function(method){this.method = method;}
xmlremote.prototype.setUrl			= function(url){this.action = url;}
xmlremote.prototype.setTarget		= function(target){this.target = target;}
xmlremote.prototype.setPostParam	= function(param){this.data = param;}
xmlremote.prototype.setType			= function(type){this.type = type;}
xmlremote.prototype.setCallback		= function(callback){this.callback = callback;}
xmlremote.prototype.post			= function(params, target)
{
	var i = 0, paramStr = '';
	var query = '';
	this.method = (arguments[2] == "GET") ? "GET" : "POST";

	if (typeof target == 'function')
	{
		this.async = true;
		this.callback = target;
	}
	else
		this.async  = false;
	
	for ( i in params )
	{
		if ( i == 'func' )
		{
			query += i + "=" + params[i];
		}
		else
		{
			paramStr +=  i + ":" + params[i] + "_";
		}
	}
	query += "&params[]=" + paramStr;
	this.data   = query;
	this.target = target || "";

	return this.send()
}

xmlremote.prototype.get				= function(params, target)
{
	return this.post(params, target, "GET");
}


xmlremote.prototype.send			= function()
{
	if ( this.async )
	{
		var Me = this;
		this.xmlhttp.abort(); //cancels current request. the current request's (ready)state is maintained and needs to be cleared.
		this.xmlhttp.onreadystatechange=function() 
		{							
			switch (Me.xmlhttp.readyState)
			{
				case 0:
					//alert("Not Initialized");
					break;
				case 1:
					//alert("readyState1");
					break;	
				case 2:
					//alert("readyState2");
					break;
				case 3:
					//alert("readyState3");
					break;		
				case 4://(xmlhttp.readyState==4) 
				{
					//alert("readyState4");
					return Me._response();
				}
			}
		}
	}

	if (this.method.toLowerCase() == 'get')
	{
		this.mime = "text/plain";
	}

/*	
	if (this.method.toLowerCase() == 'post')
	{
		this.mime = "application/x-www-form-urlencoded";
	}
*/
	this.xmlhttp.open(this.method, this.action, this.async);
	this.xmlhttp.setRequestHeader("Content-Type", this.mime);
	this.xmlhttp.send(this.data);
	if ( !this.async )
	{
		return this._response();
	}
}

xmlremote.prototype.addHeader = function(){}

xmlremote.prototype._response = function()
{
	var output = eval("this.xmlhttp.response" + this.type);
	if (this.callback)
	{
		if ( this.type == 'XML' || output.charAt(0) == '<' )
		{
			output = this.xmlhttp.responseXML;
		}
		else if ( this.type != 'XML' && (output.charAt(0) == '{' || output.charAt(0) == '[') )
		{
			eval("output = " + output);
		}
		this.callback( output );
		this.callback = false;
	}
	else if ( this.target )
	{
		var objType = document.getElementById(this.target).nodeName;

		if ( objType  == 'DIV' )
		{
			document.getElementById(this.target).innerHTML =  output; //eval("this.xmlhttp.response" + this.type);
		}
		if ( objType == 'INPUT' )
		{
			document.getElementById(this.target).value =  output; //eval("this.xmlhttp.response" + this.type);
		}
		if ( objType == 'IFRAME' || objType == 'FRAME' )
		{
			document.getElementById(this.target).src =  output; //eval("this.xmlhttp.response" + this.type);
		}
	}
	else
	{
		return output;
	}
	//else
	//	window.location.href = action;
}

// Method name change from 'call' to 'dial'
// as opposed to 'setupCall'
xmlremote.prototype.dial = function(func, params)
{
	var paramList = '';

	if ( params )
	{
		var p = params.split(/,/);

		for ( var i=0; i<p.length; ++i )
		{
			paramList += "&params[]=" + p[i];
		}
	}
	this.setPostParam("func="+func+paramList);
}

xmlremote.counter = -1;

xmlremote.prototype.request = function()
{
	if (xmlremote.counter == -1)// is first call? //(arguments[0][arguments[0].length-1].req == 'req')//if (data[0] == 'cb')
	{
		var func = this.request.caller.toString().split(' ')[1].split('(')[0];
		this.setCallback(this.request.caller);
		this.invoke(arguments[0], func);
		++xmlremote.counter;
		return false;
	}
	--xmlremote.counter;
	return true;
}


xmlremote.prototype.invoke = function()
{
	var paramList = [];
	var method = arguments[arguments.length-1]; //this.invoke.caller.toString().split(' ')[1].split('(')[0];
	var temp = "";
	var i, j = 0;
	for ( i=0; i<arguments.length; ++i )
	{
		temp = arguments[i];
		if (arguments[i] == this.invoke.caller.caller.toString().split(' ')[1].split('(')[0])
		{
			break;
		}
		if ( typeof arguments[i]  == 'string' )
		{
			temp = "";

			temp += arguments[i] + "_";
			temp = temp.substr(0,temp.length-1);
		}
		else if ( arguments[i].length != undefined )
		{
			temp = "";

			for ( j=0; j < arguments[i].length; ++j )
			{
				temp += arguments[i][j] + "_";
			}
			temp = temp.substr(0,temp.length-1);
		}	
		else if ( typeof arguments[i]  == 'object' )
		{
			temp = "";

			for ( j in arguments[i] )
			{
				temp += j + ":" + arguments[i][j] + "_";
			}
			temp = temp.substr(0,temp.length-1);
		}
		paramList.push(temp);
	}

	var params = paramList.join(",");
	if (!this.callback)
	{
		this.async = false;
	}
	this.dial(method,params);
	return this.send();
}


/*
function _initCaps(str)
{
	str.toLowerCase();
	tmpChar = str.substring(0,1).toUpperCase();
	postString = str.substring(1,str.length);
	str = tmpChar + postString;	
	return str;
}


function createQueryStr(oForm)
{
	return parseForm(oForm);
}

function parseForm(oForm)
{
	var str;
	var len;
	len = oForm.elements.length;
	ele = oForm.elements
	for ( var i=2; i < len; ++i )
	{
		if ( ele[i].type == 'radio' || ele[i].type == 'checkbox')
		{
			if ( ele[i].checked )
			{
				str +=  ele[i].name + "=";
				str +=  ele[i].value + "&";
				continue;
			}
			continue;
		}
		str +=  ele[i].name + "=";
		str +=  ele[i].value + "&";
	}
	return str;	
}
*/
