/**************************************************************************
**
**	Created 02.03.2007
**	Modified 10.05.2009
**	Created by Joonas Kirsebom
**	Version 2.0
**
****************************************************************************/

function XMLhttpObj()
{
	//Create a boolean variable to check for a valid Internet Explorer instance.
	this.xmlhttp = false;
	
	//Check if we are using IE.
	try
	{ //If the Javascript version is greater than 5.
		this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{ //If not, then use the older active x object.
		try
		{ //If we are using MS.
			this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{ //Else we must be using a non-IE browser.
			this.xmlhttp = false;
		}
	}
	
	//If we are using a non-IE browser, create a javascript instance of the object.
	if (!this.xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		this.xmlhttp = new XMLHttpRequest();
	}

	
	//this.xmlhttp = new XMLHttpRequest();
	this.url = "";
	this.resultTXT = "";
	this.resultXML = "";
	this.data = "";
	this.onsuccess = "";
	this.onerror = "";
	this.ready = true;
	this.charset = 'UTF-8'; // OBS, no effect in changing this!
	this.enctype = 'application/x-www-form-urlencoded';
	this.nocache = false;
	this.xml = null;
	this.async = true;
	
	this.nocacheCheck = function()
	{
		if( this.nocache )
		{
			if( this.url.indexOf('?') == -1 )
				this.url = this.url + '?' + (new Date()).getTime();
			else
				this.url = this.url + '&' + (new Date()).getTime();
		}
	}
	
	this.ajaxGET = function()
	{
		if(!this.ready)
			return;
		this.ready = false;
		var obj = this;

		var tmpUrl = this.url + this.data;
		if( tmpUrl.indexOf('?') == -1 )
			tmpUrl = tmpUrl.replace(/&/,'?');
		
		/* Set up the request */
		this.xmlhttp.open('GET', tmpUrl, this.async);
		this.xmlhttp.setRequestHeader('Content-Type', this.enctype+'; charset='+this.charset);

		/* The callback function */
		this.xmlhttp.onreadystatechange = function() 
		{
			if(obj.xmlhttp.readyState == 4) 
			{
				if(obj.xmlhttp.status == 200)
				{
					obj.xmlGET = obj.xmlhttp.responseXML;
					obj.txtGET = obj.xmlhttp.responseText;
					
					obj.resultTXT = obj.xmlhttp.responseText;
					obj.resultXML = obj.xmlhttp.responseXML;

					obj.ready = true;
					eval(obj.onsuccess);
				}
				else
				{
					obj.ready = true;
					eval(obj.onerror);
				}
			}
		}
		/* Send the GET request */
		this.xmlhttp.send(null);
	}

	this.ajaxPOST = function()
	{
		if(!this.ready)
			return;
		this.ready = false;
		var obj = this;
		
		/* Set up the request */
		this.xmlhttp.open('POST', this.url, this.async);
		this.xmlhttp.setRequestHeader('Content-Type', this.enctype+'; charset='+this.charset);
		
		/* The callback function */
		this.xmlhttp.onreadystatechange = function() 
		{
			if(obj.xmlhttp.readyState == 4) 
			{
				if(obj.xmlhttp.status == 200)
				{
					obj.xmlPOST = obj.xmlhttp.responseXML;
					obj.txtPOST = obj.xmlhttp.responseText;

					obj.resultTXT = obj.xmlhttp.responseText;
					obj.resultXML = obj.xmlhttp.responseXML;

					obj.ready = true;
					eval(obj.onsuccess);
				}
				else
				{
					obj.ready = true;
					eval(obj.onerror);
				}
			}
		}
		/* Send the POST request */
		this.xmlhttp.send(this.data);
	}
	
	this.open = function(type, url, formid, onsuccess, onerror, values)
	{
		this.url = url;
		this.nocacheCheck();
		
		onsuccess = onsuccess.replace(/\(.*\)/, "")+"(obj)";
		this.onsuccess = onsuccess;
		
		onerror = onerror.replace(/\(.*\)/, "")+"(obj)";
		this.onerror = onerror;
		
		this.data = getChildInputs(document.getElementById(formid));
		if( values )
		{
			this.data = this.data + "&" + values.replace("?", "&");
			this.data = this.data.replace("&&", "&");
		}
		
		if( type.toUpperCase() == "GET" )
		{
			this.ajaxGET();
		}
		else if( type.toUpperCase() == "POST" )
		{
			this.ajaxPOST();
		}
	}
/*
	this.ajaxFILE = function() // Not finished, problem with reading local files.
	{
		var boundaryString = 'AaB03x';
		var boundary = '--' + boundaryString;
		var requestBody = [
		boundary,
		'Content-Disposition: form-data; name="GOD"',
		'',
		'Kibo',
		boundary,
		'Content-Disposition: file; name="filen"; filename="prayer.txt"',
		'Content-Type: text/plain',
		'',
		'Kibology for all.\r\nAll for Kibology.',
		boundary
		].join('\r\n');
		var obj = this;
		
		this.xmlhttp.open('POST', 'test.php', true);
		this.xmlhttp.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + boundaryString);
		
		this.xmlhttp.onreadystatechange = function()
		{
			if (obj.xmlhttp.readyState == 4)
			{
				alert(obj.xmlhttp.status + ' ' + obj.xmlhttp.statusText + '\r\n' + obj.xmlhttp.getAllResponseHeaders() + '\r\n\r\n' + obj.xmlhttp.responseText);
			}
		}
		this.xmlhttp.send(requestBody);
	}*/
}
function getChildInputs(node)
{
	var input_values = "";
	if( node )
	{
		if( node.nodeName == "INPUT" )
		{
			switch(node.type)
			{
				case "checkbox":
					if(node.checked)
						input_values += "&"+ node.name +"="+ escape(node.value);
					break;
				case "radio":
					if(node.checked)
						input_values += "&"+ node.name +"="+ escape(node.value);
					break;
				case "text":
					input_values += "&"+ node.name +"="+ escape(node.value);
					break;
				case "password":
					input_values += "&"+ node.name +"="+ escape(node.value);
					break;
				case "hidden":
					input_values += "&"+ node.name +"="+ escape(node.value);
					break;
				case "submit": // no action yet
					break;
				case "image": // no action yet
					break;
				case "file": // no action yet
					break;
				default: // button, reset, will not be sent
			}
		}
		else if( node.nodeName == "TEXTAREA" )
		{
			input_values += "&"+ node.name +"="+ escape(node.value);
		}
		else if( node.nodeName == "SELECT" )
		{
			if(node.multiple)
			{
				for(j = 0; j < node.length; j++)
				{
					if(node.options[j].selected)
						input_values += "&"+ node.name +"="+ escape(node.options[j].value);
				}
			}
			else
				input_values += "&"+ node.name +"="+ escape(node.value);
		}
		else
		{
			for( var i = 0; node.childNodes[i]; i++ )
			{
				input_values += getChildInputs(node.childNodes[i]);
			}
		}
	}
	return input_values;
}
