/*
This script makes it easier doing xmlhttprequest 
Copyright (C) 2005, 2008  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.1
*/

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 XMLinsert(argId,arg_data)
{
	if (document.getElementById(argId))
	{
		document.getElementById(argId).innerHTML = arg_data;
	}
}



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

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


/* function to parse XML string */
function parseXML(argString)
{
	try //Internet Explorer
	{
		xml 			= new ActiveXObject('Microsoft.XMLDOM');
		xml.async	= "false";
		xml.loadXML(argString);
	}
	catch(e)
	{
		try //Firefox, Mozilla, Opera, etc.
		{
			xml = new DOMParser();
			xml = xml.parseFromString(argString,'text/xml');
		}
		catch(e)
		{
			//alert(e.message);
  			return;
  		}
	}
	return xml;
}

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

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


function httpFilePost(argUrl,argPostsArray,argFilesArray,argId)
{
	/* NOTICE!
	- argPostsArray must be an array and the array-key will be the "name" (eg. <input type="text" name="THISNAME"/>)
	- argFilesArray 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 argPostsArray)
	{
		post += 'content-disposition: form-data; name="'+x+'"\r\n\r\n';
		post += argPostsArray[x]+'\r\n\r\n';
		post += boundary+'\r\n';
	}

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

	
	/* http part, send query */
	req = xmlhttprequest();
	req.open('POST',argUrl,(argId?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 (argId)
	{
		req.onreadystatechange = function ()
		{
			if (req.readyState == '4')
			{
				XMLinsert(argId,req.responseText);
			}
		}
	}
	else if (req.status == '200')
	{
		return req.responseText;
	}
}

