//------------------------------------------------------------------------------
// Gets a handle that can be used to send AJAX requests to the server
//------------------------------------------------------------------------------
function eajax_getRequestHandle()
{
  if ( window.XMLHttpRequest ) { reqHandle = new XMLHttpRequest(); }
  else { reqHandle = new ActiveXObject( "Microsoft.XMLHTTP "); }

  return reqHandle;
}



//------------------------------------------------------------------------------
// Creates an object to hide an AJAX calling object so that it cannot be re-
// activated whilst the AJAX call is being processed by the server.
//------------------------------------------------------------------------------
function eajax_hideCaller( divId )
{
  waitDiv = document.createElement('div');
  waitDiv.id = divId + "AJAXWait";
  waitDiv.style.position = "absolute";
  waitDiv.style.top = "0px";
  waitDiv.style.left = "0px";
  waitDiv.style.width = "100%";
  waitDiv.style.height = "100%";
  waitDiv.style.backgroundColor = "transparent";
  waitDiv.style.overflow = "hidden";
  waitDiv.style.display = "block";
  waitDiv.innerHTML = "<div class=\"eajaxwaitcaption\"><img src=\"http://www.spanglywoohaa.com/e/common/wait30trans.gif\" /><br />Processing... Please wait</div><div class=\"eajaxwaitfade\"></div>";
  document.getElementById( divId ).appendChild( waitDiv );
}



function eajax_showCaller( divId )
{
  var shadow = document.getElementById( divId + "AJAXWait" )
  if ( shadow ) {
    var object = document.getElementById( divId );
    object.removeChild( shadow );
  }
}

//------------------------------------------------------------------------------
// Shows or hides an object. The object to be manipulated must have an id in the
// form xxxxcontent where xxxx is the value passed to this function in the divId
// parameter. The visibility parameter can be 'show', 'hide' or it can be
// omitted altogether in which case the object's visibility will be toggled.
//------------------------------------------------------------------------------
function eajax_showHideContent( divId, visibility )
{
  ob = document.getElementById( divId + "content" );
  if ( visibility == "show" ) {
    newVisibility = "block"; 
  } else if ( visibility == "hide" ) {
    newVisibility = "none";
  } else {
    if ( ob.style.display != "block" ) {
      newVisibility = "block"; 
    } else {
      newVisibility = "none";
    }
  }
  if ( ob )
    ob.style.display = newVisibility;
}  



//------------------------------------------------------------------------------
// Interprets a response from the web-server associated with a supplied request
// handle.
//------------------------------------------------------------------------------
function eajax_xmlResponse( reqHandle, divId )
{
  if ( !divId ) divId = '';
//alert( reqHandle.readyState );
  if ( reqHandle.readyState == 4 ) {
//alert( reqHandle.readyState + ":" + reqHandle.status );
    if ( reqHandle.status == 200 ) {
      var xmlDoc = reqHandle.responseXML;
//alert( reqHandle.responseText );
      var responses = xmlDoc.getElementsByTagName("response")
//alert( responses.length );
      for ( i=0; i<responses.length; i++ ) {
        id = responses[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
        var destinationElement = document.getElementById( id );
        if ( !destinationElement ) {
          alert( "Attempting to write into a non-existent object : " + id );
          return;
        }
        if ( responses[i].getElementsByTagName("content")[0] ) {
          content = responses[i].getElementsByTagName("content")[0].childNodes[0].nodeValue;
          destinationElement.innerHTML = content;
        }
        if ( responses[i].getElementsByTagName("visibility")[0] ) {
          visibility = responses[i].getElementsByTagName("visibility")[0].childNodes[0].nodeValue;
          eajax_showHideContent( id, visibility );
        }
//alert( id + "     " + visibility + "      " + content );
      }  // end for loop

      var errors = xmlDoc.getElementsByTagName( "error" );
      for ( i=0; i<errors.length; i++ ) {
        alert( errors[i].childNodes[0].nodeValue )
      }
      if ( ( errors.length > 0 ) && ( divId != "" ) ) eajax_showCaller( divId );

      var reload = xmlDoc.getElementsByTagName( "reload" );
      if ( reload.length > 0 ) {
        window.location.reload();
      }
      return true;
    }
  }
  return false;
}



function eajax_setCookie( c_name, value, exdays, path )
{
  var exdate=new Date();
  exdate.setDate( exdate.getDate() + exdays );
//  var c_value = escape( value ) + ( ( exdays == null ) ? "" : "; expires=" + exdate.toUTCString() ) + ';path=' + path;
  var c_value = escape( value ) + ( ( exdays == null ) ? "" : "; expires=" + exdate.toUTCString() );
  document.cookie = c_name + "=" + c_value;
}



function eajax_getCookie( c_name )
{
  var i,x,y,ARRcookies = document.cookie.split( ";" );
  for ( i = 0; i < ARRcookies.length; i++ ) {
    x = ARRcookies[ i ].substr( 0, ARRcookies[ i ].indexOf( "=" ) );
    y = ARRcookies[ i ].substr( ARRcookies[ i ].indexOf( "=" ) + 1 );
    x = x.replace(/^\s+|\s+$/g,"");
    if ( x == c_name ) {
      return unescape( y );
    }
  }
}
