var xmlHttpReq
var XML = new Object;

function xmlhttpMain(strURL,queryStr,submitType,resultFunction,resultFunctionParam1,resultFunctionParam2,resultFunctionParam3,resultFunctionParam4) {
  var xmlHttpReq = false;
  var self = this;
  
  var asyncFlag = (resultFunction != null);
  
  // Mozilla/Safari
  if (window.XMLHttpRequest) {
    xmlHttpReq = new XMLHttpRequest();
  }
  // IE
  else if (window.ActiveXObject) {
    xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (xmlHttpReq) {
    if (submitType == 'POST') {
      fullURL = strURL;
    } else {
      fullURL = strURL+'?'+queryStr;
    }
    xmlHttpReq.open(submitType, fullURL, asyncFlag);
    xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttpReq.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");   //This line prevents IE caching of requests
    if (asyncFlag) {
	    xmlHttpReq.onreadystatechange = function() {
	      if (xmlHttpReq.readyState == 4) {
          resultFunction(xmlHttpReq.responseText,resultFunctionParam1,resultFunctionParam2,resultFunctionParam3,resultFunctionParam4);
	      }
	    }
    }
    if (submitType == 'POST')
    	xmlHttpReq.send(queryStr);
    else
    	xmlHttpReq.send();
    if (asyncFlag) {
	    return true;
    } else {
    	return xmlHttpReq.responseText;
    }
  } else {
    alert("Ajax not supported!");
    return "AJAX_UNSUPPORTED";
  }
}

function xmlhttpPost(strURL,queryStr,resultFunction,resultFunctionParam1,resultFunctionParam2,resultFunctionParam3,resultFunctionParam4) {
	return xmlhttpMain(strURL,queryStr,'POST',resultFunction,resultFunctionParam1,resultFunctionParam2,resultFunctionParam3,resultFunctionParam4);
}

function xmlhttpGet(strURL,queryStr,resultFunction,resultFunctionParam1,resultFunctionParam2,resultFunctionParam3,resultFunctionParam4) {
	return xmlhttpMain(strURL,queryStr,'GET',resultFunction,resultFunctionParam1,resultFunctionParam2,resultFunctionParam3,resultFunctionParam4);
}

// Hint.. you might actually want getEncodedFormQueryString() below...
function getFormQueryString(formObj) {
  qs = '';
  i = 0;
  while (i < formObj.elements.length) {
    field = formObj.elements[i];
    if (field != null && field.name != '' && (field.type != 'radio' || field.checked == true)) {
      if (qs) { qs += '&'; }
      qs += field.name + '=' + escape(field.value)
    }
    i++;
  }
	return qs;
}

function getEncodedFormQueryString(formObj) {
  qs = '';
  i = 0;
  while (i < formObj.elements.length) {
    field = formObj.elements[i];
    if (field != null && field.name != '' && (field.type != 'radio' || field.checked == true)) {
      if (qs) { qs += '&'; }
      qs += field.name + '=' + encodeURIComponent(field.value)
    }
    i++;
  }
	return qs;
}

function simpleFormValidation(strURL,formObj) {
  var queryStr = getFormQueryString(formObj) + "&ajax=1";
  var result = xmlhttpPost(strURL,queryStr);
  if (result != 1) {
    alert(result);
    return false;
  }
  return true;
}

function createNewXMLDoc(rootTagName, namespaceURL) {
  if (!rootTagName) rootTagName = "";
  if (!namespaceURL) namespaceURL = "";

  if (document.implementation && document.implementation.createDocument) {
    return document.implementation.createDocument(namespaceURL, rootTagName, null);
  } else {
    var doc = new ActiveXObject("MSXML2.DOMDocument");

    // If there is a root tag, initialize the document
    if (rootTagName) {
      // Look for a namespace prefix
      var prefix = "";
      var tagname = rootTagName;
      var p = rootTagName.indexOf(':');
      if (p != -1) {
        prefix = rootTagName.substring(0, p);
        tagname = rootTagName.substring(p+1);
      }

      // If we have a namespace, we must have a namespace prefix
      // If we don't have a namespace, we discard any prefix
      if (namespaceURL) {
        if (!prefix) prefix = "a0"; // What Firefox uses
      } else {
        prefix = "";
      }

      // Create the root element (with optional namespace) as a
      // string of text
      var text = "<" + (prefix?(prefix+":"):"") + tagname +
                 (namespaceURL ? (" xmlns:" + prefix + '="' + namespaceURL +'"')
                               : "") +
                 "/>";

      // And parse that text into the empty document
      doc.loadXML(text);
    }
    return doc;
  }
}

/**
 * Create a new Document object. If no arguments are specified,
 * the document will be empty. If a root tag is specified, the document
 * will contain that single root tag. If the root tag has a namespace
 * prefix, the second argument must specify the URL that identifies the
 *namespace.
 **/
XML.newDocument = function(rootTagName, namespaceURL) {
    if (!rootTagName) rootTagName = "";
    if (!namespaceURL) namespaceURL = "";

    if (document.implementation && document.implementation.createDocument) {
        // This is the W3C standard way to do it
        return document.implementation.createDocument(namespaceURL, 
                       rootTagName, null);
    }
    else { // This is the IE way to do it
        // Create an empty document as an ActiveX object
        // If there is no root element, this is all we have to do
        var doc = new ActiveXObject("MSXML2.DOMDocument");

        // If there is a root tag, initialize the document
        if (rootTagName) {
            // Look for a namespace prefix
            var prefix = "";
            var tagname = rootTagName;
            var p = rootTagName.indexOf(':');
            if (p != -1) {
                prefix = rootTagName.substring(0, p);
                tagname = rootTagName.substring(p+1);
            }

            // If we have a namespace, we must have a namespace prefix
            // If we don't have a namespace, we discard any prefix
            if (namespaceURL) {
                if (!prefix) prefix = "a0"; // What Firefox uses
            }
            else prefix = "";

            // Create the root element (with optional namespace) as a
            // string of text
            var text = "<" + (prefix?(prefix+":"):"") + tagname +
                (namespaceURL
                 ?(" xmlns:" + prefix + '="' + namespaceURL +'"')
                 :"") +
                "/>";
            // And parse that text into the empty document
            doc.loadXML(text);
        }
        return doc;
    }
};


/**
 * Parse the XML document contained in the string argument and return
 * a Document object that represents it.
 */
XML.parse = function(text) {
    if (typeof DOMParser != "undefined") {
        // Mozilla, Firefox, and related browsers
        return (new DOMParser()).parseFromString(text, "application/xml");
    }
    else if (typeof ActiveXObject != "undefined") {
        // Internet Explorer.
        var doc = XML.newDocument( );   // Create an empty document
        doc.loadXML(text);              //  Parse text into it
        return doc;                     // Return it
    }
    else {
        // As a last resort, try loading the document from a data: URL
        // This is supposed to work in Safari. Thanks to Manos Batsis and
        // his Sarissa library (sarissa.sourceforge.net) for this technique.
        var url = "data:text/xml;charset=utf-8," + encodeURIComponent(text);
        var request = new XMLHttpRequest();
        request.open("GET", url, false);
        request.send(null);
        return request.responseXML;
    }
};