﻿/*****************************************************************
// CommonAjaxLibrary.js
// Common JavaScript AJAX Library
// 9/12/2009
*****************************************************************/

/* Common values for the ReadyState of the XMLHttpRequest object */
var READYSTATE_UNINITIALIZED = 0;
var READYSTATE_LOADING = 1;
var READYSTATE_LOADED = 2;
var READYSTATE_INTERACTIVE = 3;
var READYSTATE_COMPLETE = 4;

/* Common values for HTTP status codes */
var HTTPSTATUS_OK = 200;

//////////////////////////////////////////////////////////////////////////////////////
// Utility function to obtain a valid XMLHttpRequest object.
//////////////////////////////////////////////////////////////////////////////////////

function CreateXmlHttpRequestObject(showAlert)
{
    var xmlHttpRequest;
    if (window.ActiveXObject)
    {
      try
      {
         xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e)
      {
         xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
      }
    }
    else
      xmlHttpRequest = new XMLHttpRequest();
        
    if (showAlert && xmlHttpRequest == null)
    {
      alert ("Your browser does not support AJAX!");
    }
    return xmlHttpRequest;
} // CreateXmlHttpRequestObject()

function GetXmlHttpRequestStatusError(xmlHttpRequest)
{
  var statusText = "";
  if (xmlHttpRequest.status == 404)
  {
    statusText = "URL does not exist";
  }
  else if (xmlHttpRequest.status == 12030)  // ERROR_INTERNET_CONNECTION_ABORTED
  {
    statusText = "The connection with the server has been terminated.";
  }
  else
  {
    statusText = xmlHttpRequest.statusText;
  }
  return "xmlHttpRequest.status=" + xmlHttpRequest.status 
        + "\nStatus Text=" + statusText
        + "\nresponseText=" + xmlHttpRequest.responseText;
} // GetXmlHttpRequestStatusError()
  
//////////////////////////////////////////////////////////////////////////////////////
// Process Response
//////////////////////////////////////////////////////////////////////////////////////


function InsertXmlHttpResponseText(xmlHttpRequest, elementResult)
{
  if (xmlHttpRequest.readyState != 4) // not READYSTATE_COMLETE
  {
    return;
  }

  if (xmlHttpRequest.status != 200) // not HTTPSTATUS_OK
  {
    alert("Error-xmlHttpRequest: " + GetXmlHttpRequestStatusError(xmlHttpRequest));
    return;
  }
  
  var xmlDoc = xmlHttpRequest.responseXML;
  var result = xmlDoc.lastChild.childNodes[0].nodeValue;
  // var result = xmlDoc.lastChild.text;
  elementResult.innerHTML = result;
} // InsertXmlHttpResponseText()

function GetXmlHttpResponseText(xmlHttpRequest)
{
  if (xmlHttpRequest.readyState != 4) // not READYSTATE_COMLETE
  {
    return "";
  }

  if (xmlHttpRequest.status != 200) // not HTTPSTATUS_OK
  {
    return "Error-xmlHttpRequest: " + GetXmlHttpRequestStatusError(xmlHttpRequest);
  }
  
  var xmlDoc = xmlHttpRequest.responseXML;
  if (xmlDoc == null)
  {
    return "";
  }
  try 
  {
    return xmlDoc.lastChild.childNodes[0].nodeValue;
  }
  catch (e)
  {
    return "";
  }
  // return xmlDoc.lastChild.text;    // not for FireFox
} // GetXmlHttpResponseText()

function GetXmlHttpResponseXML(xmlHttpRequest)
{
  if (xmlHttpRequest.readyState != 4) // not READYSTATE_COMLETE
  {
    return null;
  }

  if (xmlHttpRequest.status != 200) // not HTTPSTATUS_OK
  {
    return null;
  }
  
  var xmlDoc = xmlHttpRequest.responseXML;
  // alert(xmlDoc.lastChild.text);
  return xmlDoc;
} // GetXmlHttpResponseXML()

//////////////////////////////////////////////////////////////////////////////////////


// mozXPath [http://km0ti0n.blunted.co.uk/mozxpath/] km0ti0n@gmail.com
// Code licensed under Creative Commons Attribution-ShareAlike License 
// http://creativecommons.org/licenses/by-sa/2.5/

if( document.implementation.hasFeature("XPath", "3.0") )
{
	XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
	{
		if( !xNode ) { xNode = this; } 

		var oNSResolver = this.createNSResolver(this.documentElement)
		var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
		var aResult = [];
		for( var i = 0; i < aItems.snapshotLength; i++)
		{
			aResult[i] =  aItems.snapshotItem(i);
		}
		
		return aResult;
	}
	
	XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
	{
		if( !xNode ) { xNode = this; } 

		var xItems = this.selectNodes(cXPathString, xNode);
		if( xItems.length > 0 )
		{
			return xItems[0];
		}
		else
		{
			return null;
		}
	}

	Element.prototype.selectNodes = function(cXPathString)
	{
		if(this.ownerDocument.selectNodes)
		{
			return this.ownerDocument.selectNodes(cXPathString, this);
		}
		else{throw "For XML Elements Only";}
	}

	Element.prototype.selectSingleNode = function(cXPathString)
	{	
		if(this.ownerDocument.selectSingleNode)
		{
			return this.ownerDocument.selectSingleNode(cXPathString, this);
		}
		else{throw "For XML Elements Only";}
	}

}

//////////////////////////////////////////////////////////////////////////////////////


/**********************************************************************************
synchronous -
    the script stops and waits for the server to send back a reply before continuing
asynchronous -
    the script allows the page to continue to be processed and will handle the reply if and when it arrives
**********************************************************************************/