var stateChangeFunction;
var xmlHttp;


/* 
Creates the XML request object.
*/
function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
}

/*
Starts the transfer process.
*/

function startTransfer(destination, stateChangeFunctionName) {

	// Add value to parameter list to prevent utilization of cached results.
	var parametersExist = destination.indexOf("?");
	
	var currentDate = new Date();
	
	if (parametersExist >= 0) {
		destination = destination + '&cachingPreventionParameterMS=' + currentDate.getTime();
	} else  {
		destination = destination + '?cachingPreventionParameterMS=' + currentDate.getTime();
	}
	
	// Set state changed function to variable.
	stateChangeFunction = stateChangeFunctionName;
	
	//alert("startTransfer:destination=" + destination);
	//alert("startTransfer:stateChangeFunctionName=" + stateChangeFunctionName);
	
	// Create the http xml request object.
    createXMLHttpRequest();
    
    xmlHttp.onreadystatechange = handleStateChange;
    
    xmlHttp.open("GET", destination, true);
    xmlHttp.send(null);
}

/*
Handles the response from the Transfer Handler.
*/
function handleStateChange() {

	//alert("handleStateChange:xmlHttp.readyState:" + xmlHttp.readyState);
    if(xmlHttp.readyState == 4) {

        if(xmlHttp.status == 200) {
			stateChangeFunction();
        }
        
    }
}

/*
Creates a new form and adds to the existing body element.
*/
function createForm() {

    // Create new form.
    var newForm = document.createElement("form");
    
    // Get the current body element.
    var currentBody = document.getElementsByTagName("body")[0];
    
    // Add form to body.
    currentBody.appendChild(newForm);
    newForm.method="POST";
    
    return newForm;
}

/*
Turns any parameters included in the XML into hidden fields on the passed in form object.
*/
function createHiddenFields(form, parameters) {

    //alert("createHiddenFields:parameters.childNodes.length=" + parameters.childNodes.length);
    
    // Add each element as a hiddend field to the form.
    for(var i = 0; i < parameters.childNodes.length; i++) {
    
        var lField = document.createElement("input");
        lField.setAttribute("type", "hidden");
        form.appendChild(lField);
        
        var lName = parameters.childNodes[i].tagName;
        //alert("createHiddenFields:lName=" + lName);
        
        var lValue = getNodeValue(parameters.childNodes[i]);
        //alert("createHiddenFields:lValue=" + lValue);
        
        lField.setAttribute("name", lName);
        lField.setAttribute("value", lValue);
    }
}

/*
Retrieves a node parameter, handling issues where certain browsers break element values into individual arrays.
*/
function getNodeValue(node)
{
	var lResult = "";
	
	for (var lIndex=0; lIndex<node.childNodes.length; lIndex++)
	{
		lResult += node.childNodes[lIndex].nodeValue;
	}
	
	return lResult;
}
