/*--------------------------------------------------------------------*\
| This file is part of Videocore 3.1                                  *|
|---------------------------------------------------------------------*|
| Copyright (C) 2008 Silversoft Solutions Ltd. All Rights Reserved    *|
| This file may not be redistributed in whole or significant part     *|
| Videocore is not Free Software | http://www.videocore.com           *|
|-------------------------------------------------------------------- *|
| For more information see http://www.videocore.com/license.html      *|
\*--------------------------------------------------------------------*/

var ajax_http;

function initialize_ajax (url, callback)
{
	try
	{
		// ##### FIREFOX AND THE NORMAL ONES #####
		ajax_http = new XMLHttpRequest ();
		if (ajax_http.overrideMimeType)
		{
			ajax_http.overrideMimeType('text/xml');
		}
	}
	catch (e)
	{
		// ##### THAT OTHER GUY #####
		try
		{
			ajax_http = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				ajax_http = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				Dialog.Message ("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	ajax_http.onreadystatechange = function ()
	{
		if (ajax_http.readyState == 4)
		{
			if (ajax_http.status == 200)
			{
				if (ajax_http.responseXML.documentElement == null)
				{
					var parser = new ActiveXObject ("Microsoft.XMLDOM");
					parser.async = 'false';
					parser.loadXML (ajax_http.responseText);
					var xml = parser.documentElement;
				}
				else
				{
					var xml = ajax_http.responseXML.documentElement;
				}
				var xmldoc = parse_xml(xml);
				eval (callback + '(xmldoc);');
			}
			else
			{
				Dialog.Message ("There was a problem completing your request.  If the problem continues please notify us.")
			}
		}
	}

	ajax_http.open ('GET', url, true);
	ajax_http.send (null);
	return true;
}

function load_ajax (url, callback)
{
	return initialize_ajax (url, callback);
}

function parse_xml (xml)
{
	var values = new Array ();

	// ##### IF COMMENT NODE #####
	if (xml.nodeType == 7)
	{
		return null;
	}

	// ##### TEXT NODE OR CDATA NODE #####
	if (xml.nodeType == 3 || xml.nodeType == 4)
	{
		// ##### IGNORE WHITE SPACES #####
		if (xml.nodeValue.match( /[^\x00-\x20]/ ))
		{
			//return;
		}
		return xml.nodeValue;
	}

	// ##### PARSE CHILDREN #####
	if (xml.childNodes && xml.childNodes.length)
	{
		// ##### LOOP THROUGH CHILDREN ####
		for (var child = 0; child < xml.childNodes.length; child++)
		{
			if (xml.childNodes[child].nodeType == 3 || xml.childNodes[child].nodeType == 4)
			{
				// ##### GET RID OF EMPTY VALUES #####
				if (trim(xml.childNodes[child].nodeValue) == 0) continue;
			}

			// ##### CHECK IF ARRAY KEY EXISTS #####
			if (typeof (values[xml.nodeName]) == 'undefined')
			{
				values[xml.nodeName] = new Array ();
				//values[xml.nodeName][values[xml.nodeName].length] = parse_xml (xml.childNodes[child]);
			}

			values[xml.nodeName][values[xml.nodeName].length] = parse_xml (xml.childNodes[child]);
		}
	}

	return values;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

