var activeHTTPRequest;
var activeHTTPRequestCollection = new Object();
var activeCallback;
function hasOptions(obj) {
	if (obj!=null && obj.options!=null) return true;
	return false;
}
function selectall(obj)
{
	if(!hasOptions(obj)) return false;
        for (var i=0; i<obj.options.length; i++)
        {
                obj.options[i].selected = true;
        }
}
function unselectall(obj)
{
	if(!hasOptions(obj)) return false;
        for (var i=0; i<obj.options.length; i++)
        {
                obj.options[i].selected = false;
        }
}
function xhtmlEncode(data)
{
	return encodeURIComponent(data);
}
function xxhtmlEncode(data)
{
	var s = data;
	s.replace(/\%/g,'%25');
	s.replace(/\+/g,'%2B');
	s = escape(s);
	return s;
}
function htmlEncode(data)
{
	return escape(data).replace(/\+/g,'%2B');
}
function createPostData(form, formElement, forceSelect)
{
	var data='';
	var key='';
	var value=''
	var elements=new Array();
	var found=0;
	for (var i=0;i<form.elements.length;i++)
	{
		if (form.elements[i]==formElement) found++;
		elements[elements.length]=form.elements[i];
	}
	if (!found && formElement!=null) elements[elements.length]=formElement;
	for (var i=0;i<elements.length;i++)
	{
		var el=elements[i];
		if (el.name=='') continue;
		switch(el.tagName.toLowerCase())
		{
			case "select":
				if(el.options!=null)
				{
					for(var j=0;j<el.options.length;j++)
					{
						if(el.options[j].selected==true||(forceSelect&&formElement==el))
						{
							key = el.name;
							value = el.options[j].value;
							data+=(data=='')?'':'&';
							data+=htmlEncode(key)+'='+htmlEncode(value);
						}
					}
				}
				break;
			case "input":
				switch(el.type.toLowerCase())
				{
					case "checkbox":
					case "radio":
						if(el.checked==true)
						{
							key = el.name;
							value = el.value;
							data+=(data=='')?'':'&';
							data+=htmlEncode(key)+'='+htmlEncode(value);
						}
						break;
					case "text":
					case "hidden":
					case "file":
					case "password":
						key = el.name;
						value = el.value;
						data+=(data=='')?'':'&';
						data+=htmlEncode(key)+'='+htmlEncode(value);
						break;
					case "image":
					case "submit":
						/*
						if(formElement.src && formElement==el)
						{
							key = formElement.name + ".x";
							value = window.event.x;
							data+=(data=='')?'':'&';
							data+=htmlEncode(key)+'='+htmlEncode(value);
							key = formElement.name + ".y";
							value = window.event.y;
							data+=(data=='')?'':'&';
							data+=htmlEncode(key)+'='+htmlEncode(value);
						}
						else
						*/
						if(formElement == el)
						{
							key = el.name;
							value = el.value;
							data+=(data=='')?'':'&';
							data+=htmlEncode(key)+'='+htmlEncode(value);
						}
						break;
					case "button":
					case "reset":
						break;
					default:
						break;
				}
				break;
			default:
				break;
		}
	}
	return data;
}

function HTTPRequest(formElement, callback, forceSelect)
{
	var form=null;
	if (formElement.form) form=formElement.form;
	else
	{
		var tmp = formElement;
		while(tmp.nodeName!="FORM" && tmp.parentNode) tmp = tmp.parentNode;
		if(tmp.nodeName=="FORM") form=tmp;
		else form=formElement;
	}

	if(forceSelect == null) forceSelect=false;
	var data=createPostData(form, formElement, forceSelect);
	activeHTTPRequest=null;
	if (window.XMLHttpRequest)
        	activeHTTPRequest = new XMLHttpRequest();
	else if (window.ActiveXObject)
		activeHTTPRequest = new ActiveXObject("Microsoft.XMLHTTP");
	activeHTTPRequest.onreadystatechange = processReqChange;
	if(callback) activeCallback=callback;
	else activeCallback = false;
	if (form.method.toUpperCase()=='POST')
	{
		activeHTTPRequest.open(form.method, form.action, true);
		activeHTTPRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		if (activeHTTPRequest.overrideMimeType) activeHTTPRequest.overrideMimeType("application/x-www-form-urlencoded; charset=iso-8859-1");
		activeHTTPRequest.send(data);
	}
	else
	{
		var separator = '?';
		if (form.action.indexOf('?')>=0) separator = '&';
		activeHTTPRequest.open(form.method, form.action+separator+data, true);
		activeHTTPRequest.send(null);
	}
	return false;
}

function HTTPQuery(urlfull,callback,id,argument)
{
	if (id==null || id==undefined || id=="") id = "default";
	if(callback) async=true;
	else async = false;
	var pos = urlfull.indexOf("?");
	var data = "";
	var url = urlfull;
	if(pos>0)
	{
		url = urlfull.substring(0,pos);
		data = urlfull.substring(pos+1,urlfull.length);
	}
	eval("activeHTTPRequestCollection._"+id+"=null;");
	if (window.XMLHttpRequest)
        eval("activeHTTPRequestCollection._"+id+" = new XMLHttpRequest();");
	else if (window.ActiveXObject)
		eval("activeHTTPRequestCollection._"+id+" = new ActiveXObject('Microsoft.XMLHTTP');");
	eval("var obj = activeHTTPRequestCollection._"+id+";");
	if(async)
	{
		eval("obj.onreadystatechange = function() { processReqChange2('"+id+"');}");
		eval("activeHTTPRequestCollection._"+id+"Callback = callback;");
	}
	else
	{
		activeHTTPRequestCollection["_"+id+"Callback"] = false;
	}
	obj.open("POST", url, async);
	if(data.length>0)
	{
		obj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		if (obj.overrideMimeType) obj.overrideMimeType("application/x-www-form-urlencoded; charset=iso-8859-1");
		obj.send(data);
	}
	else obj.send(null);
	if(async) return false;
	else if (obj.status == 200) return obj.responseText;
	else alert('Error: HTTP/'+obj.status);
	return false;
}

function processReqChange2(id)
{
	eval("var obj = activeHTTPRequestCollection._"+id+";");
	eval("var callback = activeHTTPRequestCollection._"+id+"Callback;");
	try
	{
	    if (obj.readyState == 4) // complete
	    {
	        if (obj.status == 200)
	        {
	        	if (!callback && window.console) console.log('No active callback function');
	        	else if(callback) callback(obj.status,obj.responseText);
	        }
	        else
			{
	        	
	        	if (!obj.callback) 
	        	{
	        		//alert("There was a problem retrieving the XML data:\n" + obj.statusText);
					if (window.console) console.log("There was a problem retrieving the XML data:\n" + obj.statusText);	        		
					if(obj && obj.abort) obj.abort();
	        	}
	        	else if(callback) callback(obj.status,obj.statusText);
	       	}
	    }
	}
	catch (e)
	{
		if(obj && obj.abort) obj.abort();
	}
}

function processReqChange()
{
	try
	{
	    if (activeHTTPRequest.readyState == 4) // complete
	    {
	        if (activeHTTPRequest.status == 200)
			{
				if (!activeCallback && window.console) console.log('No active callback function');
				else if(activeCallback) activeCallback(activeHTTPRequest.status,activeHTTPRequest.responseText);
	        }
	        else
	        {
	        	if (activeCallback==null) 
	        	{
	        		//alert("There was a problem retrieving the XML data:\n" + activeHTTPRequest.statusText);
	        		if (window.console) console.log("There was a problem retrieving the XML data:\n" + activeHTTPRequest.statusText);
	        		if(obj && obj.abort) obj.abort();
	        	}
	        	else if(activeCallback) activeCallback(activeHTTPRequest.status,activeHTTPRequest.statusText);
	       	}
	    }
	}
	catch (e)
	{
		if(obj && obj.abort) obj.abort();
	}
}


function http_parse_attrs(str)
{
        var result=new Array();
        var parts=explode(",",str);
        for (var i=0;i<parts.length;i++)
        {
                var varname=parts[i].substring(0,parts[i].indexOf('='));
                var varvalue=true;
                if (parts[i].indexOf('=')>-1)
                {
                        varname=parts[i].substring(0,parts[i].indexOf('='));
                        varvalue=parts[i].substring(varname.length+1);
                }
                result[varname]=varvalue;
        }
        return result;
}
function http_encode_attrs(arr)
{
        var result="";
        for (var i in arr) result+=(result==''?'':',')+i+'='+arr[i];
        return result;
}
function http_set_cookie(cookie_name, cookie_value)
{
        document.cookie = cookie_name + "=" + escape(cookie_value) + ";PATH=/";
        return true;
}
function http_get_cookie(cookie_name)
{
        if ((i = document.cookie.indexOf(cookie_name + "=")) != -1)
        {
                i += cookie_name.length + 1;
                var j = document.cookie.indexOf(";", i);
                if (j == -1) j = document.cookie.length;
                return unescape(document.cookie.substring(i, j));
        }
        return "";
}
