/*
This script makes it easier doing xmlhttprequest 
Copyright (C) 2005, 2007  Vegard Hammerseth <vegard@hammerseth.com> (http://vegard.hammerseth.com)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

v1.0.0
*/

function xmlhttprequest()
{
	try
	{
		x = new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			x = new ActiveXObject('Msxml2.XMLHTTP');
		}
		catch (e)
		{
			try
			{
				x = new ActiveXObject('Microsoft.XMLHTTP');
			}
			catch (e)
			{
				x = false;
			}
	       	}
	}
	if (x)
	{
		return x;
	}
}



/* insert xml/data/info/value into a element by id */
function xml_insert(arg_id,arg_data)
{
	if (document.getElementById(arg_id))
	{
		document.getElementById(arg_id).innerHTML = arg_data;
	}
}



/* function to load page using GET supporting asynchronize and synchronize */
function http_get(arg_url,arg_id)
{
	req = xmlhttprequest();
	req.open('GET',arg_url,(arg_id?true:false));
	req.send(null);

	/* if we used asynchroniz query */
	if (arg_id)
	{
		req.onreadystatechange = function ()
		{
			if (req.readyState == '4')
			{
				xml_insert(arg_id,req.responseText);
			}
		}
	}
	else if (req.status == '200')
	{
		return req.responseText;
	}
}



/* function to load page using POST */
function http_post(arg_url,arg_parameters,arg_id)
{
	req = xmlhttprequest();
	req.open('POST',arg_url,(arg_id?true:false));
	req.setRequestHeader('content-type','application/x-www-form-urlencoded');
	req.setRequestHeader('connection','close');
	req.setRequestHeader('content-length',arg_parameters.length);
	req.send(arg_parameters);

	/* if we used asynchroniz query */
	if (arg_id)
	{
		req.onreadystatechange = function ()
		{
			if (req.readyState == '4')
			{
				xml_insert(arg_id,req.responseText);
			}
		}
	}
	else if (req.status == '200')
	{
		return req.responseText;
	}
}


function http_file_post(arg_url,arg_posts_array,arg_files_array,arg_id)
{
	/* NOTICE!
	- arg_posts_array must be an array and the array-key will be the "name" (eg. <input type="text" name="THISNAME"/>)
	- arg_files_array must also be an array. Key 'name' for fieldname, key 'filename' for filename, key 'type' for content type and 'content for file content.
	*/

	/* fix post data */
	var boundarystring 	= 'w3btorrent';
	var boundary 		= '--'+boundarystring;
	var post		= boundary+'\r\n';

	/* run throgh our posts and put all post in our query */
	for (x in arg_posts_array)
	{
		post += 'content-disposition: form-data; name="'+x+'"\r\n\r\n';
		post += arg_posts_array[x]+'\r\n\r\n';
		post += boundary+'\r\n';
	}

	/* put our files in the post query */
	for (x in arg_files_array)
	{
		post += 'content-disposition: form-data; name="'+arg_files_array[x]['name']+'"; filename="'+arg_files_array[x]['filename']+'"\r\n';
		post += 'content-type: '+arg_files_array[x]['type']+'\r\n\r\n';
		post += arg_files_array[x]['content']+'\r\n';
		post += boundary+'\r\n';
	}

	
	/* http part, send query */
	req = xmlhttprequest();
	req.open('POST',arg_url,(arg_id?true:false));
	req.setRequestHeader('content-type','multipart/form-data; boundary="'+boundarystring+'"');
	req.setRequestHeader('connection','close');
	req.setRequestHeader('content-length',post.length);
	req.send(post);

	/* if we used asynchroniz query */
	if (arg_id)
	{
		req.onreadystatechange = function ()
		{
			if (req.readyState == '4')
			{
				xml_insert(arg_id,req.responseText);
			}
		}
	}
	else if (req.status == '200')
	{
		return req.responseText;
	}
}
