
// Iterates through a form's elements and creates a submit url string
// addlParams is an array of 2-value arrays that include name/value pairs of additional optional parameters
function ajaxFormSubmit(inForm, addlParams, checkBoxesAsArrays) {
    var params = "";
    var url=inForm.action+"?";
    for (var i = 0; i < inForm.elements.length; i++) {
            if (inForm.elements[i].type == "checkbox") {
                    if (checkBoxesAsArrays) {
                            // if we wish to pass checkboxes as arrays, then html will be of format:
                            // <form>
                            //              <input type='checkbox' name='colors[]' value='blue' checked />blue
                            //              <input type='checkbox' name='colors[]' value='red' checked />red
                            //              <input type='checkbox' name='colors[]' value='green' />red
                            // </form>
                            // which will yield colors=blue&colors=red&colors=&  and $_GET { Array colors[] => { 'blue', 'red' } } on server side
                            if (inForm.elements[i].checked) {
                                    params += inForm.elements[i].name + "=" + inForm.elements[i].value + "&";
                            } else {
                                    params += inForm.elements[i].name + "=&";
                            }
                    } else {
                            params += inForm.elements[i].name + "=" + inForm.elements[i].checked + "&";
                    }
            } else if (inForm.elements[i].type == "radio") {
                    if (inForm.elements[i].checked) {
                            params += inForm.elements[i].name + "=" + inForm.elements[i].value + "&";
                    }
            } else {
                    params += inForm.elements[i].name + "=" + escape(inForm.elements[i].value) + "&";
            }
    }

    if (addlParams) {
            for (var j = 0; j < addlParams.length; j++) {
                    params += escape(addlParams[j][0]) + "=" + escape(addlParams[j][1]) + "&";
            }
    }

    params += "sid="+Math.random();
	
	if (addlParams) {
		for (var j = 0; j < addlParams.length; j++) {
			url += escape(addlParams[j][0]) + "=" + escape(addlParams[j][1]) + "&";
		}
	}
	
	url += "sid="+Math.random();
	
	// alert(url);

    // per jq docs, params are concatenated to the end of the url if inForm.method == 'GET'
    var method = (inForm.method ? inForm.method : 'GET');
    xh = $.ajax({
        type: method,
        url: inForm.action,
        data: params,
        async: false,
        cache: false,
        error: ajaxError
    });

//	var xmlhttp = GetXmlHttpObject();
//	xmlhttp.open("GET",url,false); // false for synchronous
//	xmlhttp.send(null);
//	validateXML(xmlhttp, url);

	return xh;
}

function ajaxError (jqXHR, textStatus, errorThrown) {
    throw "AJAX ERROR:\r\n\ttextStatus:"+textStatus+"\r\n\terrorThrown:"+errorThrown+"\r\n\tResponse:"+jqXHR.responseText;
}

function validateXML(xh, url) {
	if (xh.responseXML) {
		if (xh.responseXML.documentElement.localName == "parsererror" || xh.responseXML.documentElement.localName == "errorResponse") {
			alert("Invalid XML Response:\r\n\r\n" + xh.responseText + "\r\n\r\nReceived from URL:\r\n\r\n" + url);
		} 
	} else {
		alert("Invalid XML Response:\r\n\r\n" + xh.responseText + "\r\n\r\nReceived from URL:\r\n\r\n" + url);
	}
}


function ajaxUrlSubmit(inUrl, async) {
    async = (async ? true : false); // $.ajax does not appear to treat undefined the same as false

    var xh = $.ajax({
        url: inUrl,
        async: async,
        cache: false,
        error: ajaxError
    });

    return xh;
}


function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	{
	  // code for IE7+, Firefox, Chrome, Opera, Safari
	  return new XMLHttpRequest();
	}
	if (window.ActiveXObject)
	{
	  // code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}


// used for simple retrieval of an element value; not to be used in loops or more complex xml structures, or when it's important to check for null values
function sXMLGet(xmlDoc, key) {
	if (xmlDoc.getElementsByTagName(key)[0].childNodes[0]) {
		return xmlDoc.getElementsByTagName(key)[0].childNodes[0].nodeValue;
	} else {
		return "";
	}
}
