/**************************************************************************
**	Created 02.03.2007
**	Modified 07.06.2007
**	Created by Joonas Kirsebom
**	Version 1.2
**
**	Needs sarissa-lib to work
**	sarissa.sourceforge.net
****************************************************************************/

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.xmlPOST = "";
	this.txtPOST = "";
	this.xmlGET = "";
	this.txtGET = "";
	this.postData = "";
	this.callback = "";
	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.nocacheCheck = function(url)
	{
		if( this.nocache )
		{
			if( url.indexOf('?') == -1 )
				return '?'+(new Date()).getTime();
			else
				return '&'+(new Date()).getTime();
		}
		return '';
	}
	
	this.ajaxGET = function(url,callback,formName)
	{
		if(!this.ready)
			return;
		this.ready = false;
		this.callback = callback;
		var obj = this;

		url = url + getChildInputs(document.getElementById(formName));
		if( url.indexOf('?') == -1 )
			url = url.replace(/&/,'?');
		url = url + this.nocacheCheck(url);
		
		/* Set up the request */
		this.xmlhttp.open('GET',url,true);
		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.ready = true;
					eval(callback);
				}
				else
				{
					obj.ready = true;
					//alert('Kunne ikke hente data, prøv igjen senere');
				}
			}
		}
		/* Send the GET request */
		this.xmlhttp.send(null);
	}

	this.ajaxPOST = function(url,callback,formName)
	{
		if(!this.ready)
			return;
		this.ready = false;
		this.callback = callback;
		if( document.getElementById(formName) )
			this.postData = getChildInputs(document.getElementById(formName));
		var obj = this;

		url = url + this.nocacheCheck(url);
		
		/* Set up the request */
		this.xmlhttp.open('POST',url,true);
		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.ready = true;
					eval(callback);
				}
				else
				{
					obj.ready = true;
					(document.getElementById(formname)).submit();
				}
			}
		}
		/* Send the POST request */
		this.xmlhttp.send(this.postData);
	}
/*
	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 getChildInputs2(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 += getChildInputs2(node.childNodes[i]);
			}
		}
	}
	return input_values;
}
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;
}