var REQUEST_SUCCESS = 4;
var HTTP_SUCCESS    = 200;
var HTTP_FAILED     = 404;

var CLASSNAME_VISIBLE   = "unchecked";
var CLASSNAME_INVISIBLE = "unchecked-hidden";

function CreateRequest()
{
	var $request = null;
	var $success = true;

	if (window.XMLHttpRequest)
	{
		try { $request = new XMLHttpRequest(); }
		catch ($catch)
		{ $success = false; }
	}
	else if (window.ActiveXObject)
	{
		try { $request = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch ($catch)
		{ $success = false; }
		
		if ($request == null || $request == undefined || $request == false)
		{
			$success = true;
			try { $request = new ActiveXObject("Microsoft.XMLHTTP"); }
			catch ($catch)
			{ $success = false; }
		}
	}
	
	return ($success == false ? null : $request);
}

function HttpRequest($url, $parameters)
{
	/* Makes the http ajax request */
	var $arguments = new Array();
	var $request = CreateRequest();
	$parameters = ($parameters != null && $parameters.length == 0 ? null : $parameters)

	for(var $argument = 2; $argument < arguments.length; $argument++)
		$arguments[$arguments.length] = arguments[$argument];

	if ($request != null)
	{
		/* Sets the action */
		var $action = ($parameters == null ? "GET" : "POST")
		$request.open($action, $url, true);
		
		$request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		if ($parameters != null) $request.setRequestHeader("Content-length", $parameters.length);
		
		$request.onreadystatechange = function()
						{
							if ($request.readyState == REQUEST_SUCCESS)
							{
								if ($request.status == HTTP_SUCCESS)
								{
									/* Converts the xml's string to an xml object */
									var $response = $request.responseText;
									ReadXML($response, $arguments);
									$response = null;
								}
							}	
						}
		$request.send($parameters);
	}
}

function ReadXML($xml, $arguments)
{
	var $doc = null;

    $xml = String($xml).trim();

	/* Microsoft */
	if (window.ActiveXObject)
	{
		$doc = new ActiveXObject("Microsoft.XMLDOM");
		$doc.async = false;
		$doc.loadXML($xml);
	}
	/* All other browsers */
	else if (document.implementation && document.implementation.createDocument)
	{
		var $parser = new DOMParser();
    		$doc = $parser.parseFromString($xml,"text/xml");
    		$parser = null;
	}	

	if ($doc != null)
		HandleXML($doc, $arguments);

	$doc = null;
}
function StatusTrackName($doc, $arguments) {
    var $obj = $arguments[0];
    var $nodes = $doc.childNodes;
    /* Finds the service's response */
    for (var $index = 0; $index < $nodes.length; $index++) {
        var $node = $nodes[$index];
        if ($node.nodeName == 'response') {
            $node = $node.childNodes[0];
            for (var $attribute = 0; $attribute < $node.attributes.length; $attribute++) {
                if ($node.attributes[$attribute].nodeName == 'value') {
                    $res = (String($node.attributes[$attribute].nodeValue).toUpperCase() == 'true'.toUpperCase());
                    if ($res)
                        window.location.href = $obj.href;
                }
            }
        }
        $node = null;
    }
    $nodes = null;
}


function HandleXML($doc, $arguments) {
    /* Evaluates which is the action to do */
    var $attributes = $doc.documentElement.attributes;
    var $length = $attributes.length;

    for (var $index = 0; $index < $length; $index++) {
        var $attribute = $attributes[$index];
        var $value = $attribute.nodeValue;

        switch ($value) {
            case 'getraces':
                StatusTrackName($doc, $arguments);
                break;
        }

        $attribute = null;
    }

    $attributes = null;
}


