var FormHandler = new Object ();

FormHandler.initialize = function ()
{
	this.request = null;
};

FormHandler.fieldsToPost = function (_formElement)
{
	var inputs 			= _formElement.getElementsByTagName ("INPUT");
	var textareas 	= _formElement.getElementsByTagName ("TEXTAREA");
	var selects 		= _formElement.getElementsByTagName ("SELECT");
	
	var postData = new Array ();
	
	// inputs (text, password, hidden, checkbox, radio)
	for (var i=0; i < inputs.length; i++)
	{
		var element = inputs[i];
		var type = element.getAttribute("type");
		
		// if checkbox not checked
		if ((type == "checkbox" || type == "radio") && !element.checked)
			continue;
		
		if (type != "text" && type != "hidden" && type != "password" && type != "checkbox" && type != "radio")
			continue;
		
		var key 	= element.getAttribute ("name");
		var value = element.getAttribute ("value");
		postData[key] = encodeURIComponent(value);
	}
	
	// textareas
	for (var i=0; i < textareas.length; i++)
	{
		var key 	= element.getAttribute ("name");
		var value = element.getAttribute ("value");
		postData[key] = encodeURIComponent(value);
	}
	
	// selects
	for (var i=0; i < selects.length; i++)
	{
		var key 	= element.getAttribute ("name");
		var value = element.options[element.selectedIndex].value;
		postData[key] = encodeURIComponent(value);
	}
	
	return this.arrayToPost (postData); //returned as a string
};

FormHandler.arrayToPost = function (arr)
{
	var postData = "";
	for(var key in arr)
	{
		var value = arr[key];
		
		if (postData != "")
			postData += "&amp;";
			
		postData += key + "=" + value;
	}
	
	return postData;
};

// Checking if there already a request running in the background
FormHandler.inUse = function()
{
	if (isSet (FormHandler.request))
		return true;
	
	return false;
};

FormHandler.submitGetHtml = function (_element, action, postData)
{
	Content.setElement (_element);
	
	if (typeof (postData) === "object")
		postData = this.arrayToPost (postData);

	if (postData.indexOf ("&amp;") != -1)
		postData = postData.replace (/&amp;/g, "&");
	
	this.request					= new XMLHttpRequest ();
	this.request.open				("POST", action, true);
	this.request.setRequestHeader	("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
	this.request.setRequestHeader	("Content-Length", postData.length);
	this.request.setRequestHeader	("Connection", "close");
	this.request.onreadystatechange = this.onStateChange;
	this.request.send				(postData);
	
	Content.clear ();
	
	return true;
};

FormHandler.submitObjectGetHtml = function (_element, _formElement, postData)
{
	Content.setElement (_element);
	
	if (typeof (postData) === "object")
		postData = this.arrayToPost (postData);

	if (postData.indexOf ("&amp;") != -1)
		postData = postData.replace (/&amp;/g, "&");
	
	var action = _formElement.action;
	postData += this.fieldsToPost (_formElement)
	
	this.request					= new XMLHttpRequest ();
	this.request.open				("POST", action, true);
	this.request.setRequestHeader	("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
	this.request.setRequestHeader	("Content-Length", postData.length);
	this.request.setRequestHeader	("Connection", "close");
	this.request.onreadystatechange = this.onStateChange;
	this.request.send				(postData);
	
	Content.clear ();
	
	return true;
};


// Handles server responses.
FormHandler.onStateChange = function ()
{
	if (! isSet (FormHandler.request))
	{
		alert ("Error: request not set");
		return;
	}
	
	if (FormHandler.request.readyState != 4 || FormHandler.request.status != 200)
		return;
		
		if (FormHandler.request.responseText)
		{
			try
			{
				var response = FormHandler.request.responseText;
				FormHandler.initialize();
				Content.setContent (response);
			}
			catch (e)
			{
				alert ("onStateChange Error: "+FormHandler.request.responseText);
			}
    }
    else
    	alert ("Weird");
}
