/*
Asynchronous Javascript And HTML
by Michael Cramer
Copyright (c) 2006 Cramer Technologies
parts of this program are by Andrew Gregory and have a separate license (see blow)
Purpose: To provide a simple way to incorporate real-time
application type funtionality into any web page without a refresh.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

To receive a copy of the GNU General Public License visit
http://www.gnu.org/licenses/gpl.txt or write to
Free Software Foundation, Inc.
51 Franklin Street, Fifth Floor
Boston, MA  02110-1301, USA.

*/
/*

Cross-Browser XMLHttpRequest v1.1
=================================

Emulate Gecko 'XMLHttpRequest()' functionality in IE and Opera. Opera requires
the Sun Java Runtime Environment <http://www.java.com/>.

by Andrew Gregory
http://www.scss.com.au/family/andrew/webdesign/xmlhttprequest/

This work is licensed under the Creative Commons Attribution License. To view a
copy of this license, visit http://creativecommons.org/licenses/by/1.0/ or send
a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305,
USA.

Not Supported in Opera
----------------------
* user/password authentication
* responseXML data member

Not Fully Supported in Opera
----------------------------
* async requests
* abort()
* getAllResponseHeaders(), getAllResponseHeader(header)

*/
// IE support
if (window.ActiveXObject && !window.XMLHttpRequest) {
  window.XMLHttpRequest = function() {
    var msxmls = new Array('Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0',
                           'Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP','Microsoft.XMLHTTP');
    for (var i = 0; i < msxmls.length; i++) {
      try {
        return new ActiveXObject(msxmls[i]);
      } catch (e) {
      }
    }
    return null;
  };
}
// Gecko support
/* ;-) */
// Opera support
if (window.opera && !window.XMLHttpRequest) {
  window.XMLHttpRequest = function() {
    this.readyState = 0; // 0=uninitialized,1=loading,2=loaded,3=interactive,4=complete
    this.status = 0; // HTTP status codes
    this.statusText = '';
    this._headers = [];
    this._aborted = false;
    this._async = true;
    this._defaultCharset = 'ISO-8859-1';
    this._getCharset = function() {
      var charset = _defaultCharset;
      var contentType = this.getAllResponseHeader('Content-type').toUpperCase();
      val = contentType.indexOf('CHARSET=');
      if (val != -1) {
        charset = contentType.substring(val);
      }
      val = charset.indexOf(';');
      if (val != -1) {
        charset = charset.substring(0, val);
      }
      val = charset.indexOf(',');
      if (val != -1) {
        charset = charset.substring(0, val);
      }
      return charset;
    };
    this.abort = function() {
      this._aborted = true;
    };
    this.getAllResponseHeaders = function() {
      return this.getAllResponseHeader('*');
    };
    this.getAllResponseHeader = function(header) {
      var ret = '';
      for (var i = 0; i < this._headers.length; i++) {
        if (header == '*' || this._headers[i].h == header) {
          ret += this._headers[i].h + ': ' + this._headers[i].v + '\n';
        }
      }
      return ret;
    };
    this.setRequestHeader = function(header, value) {
      this._headers[this._headers.length] = {h:header, v:value};
    };
    this.open = function(method, url, async, user, password) {
      this.method = method;
      this.url = url;
      this._async = true;
      this._aborted = false;
      this._headers = [];
      if (arguments.length >= 3) {
        this._async = async;
      }
      if (arguments.length > 3) {
        opera.postError('XMLHttpRequest.open() - user/password not supported');
      }
      this.readyState = 1;
      if (this.onreadystatechange) {
        this.onreadystatechange();
      }
    };
    this.send = function(data) {
      if (!navigator.javaEnabled()) {
        alert("XMLHttpRequest.send() - Java must be installed and enabled.");
        return;
      }
      if (this._async) {
        setTimeout(this._sendasync, 0, this, data);
        // this is not really asynchronous and won't execute until the current
        // execution context ends
      } else {
        this._sendsync(data);
      }
    };
    this._sendasync = function(req, data) {
      if (!req._aborted) {
        req._sendsync(data);
      }
    };
    this._sendsync = function(data) {
      this.readyState = 2;
      if (this.onreadystatechange) {
        this.onreadystatechange();
      }
      // open connection
      var url = new java.net.URL(new java.net.URL(window.location.href), this.url);
      var conn = url.openConnection();
      for (var i = 0; i < this._headers.length; i++) {
        conn.setRequestProperty(this._headers[i].h, this._headers[i].v);
      }
      this._headers = [];
      if (this.method == 'POST') {
        // POST data
        conn.setDoOutput(true);
        var wr = new java.io.OutputStreamWriter(conn.getOutputStream(), this._getCharset());
        wr.write(data);
        wr.flush();
        wr.close();
      }
      // read response headers
      // NOTE: the getHeaderField() methods always return nulls for me :(
      var gotContentEncoding = false;
      var gotContentLength = false;
      var gotContentType = false;
      var gotDate = false;
      var gotExpiration = false;
      var gotLastModified = false;
      for (var i = 0; ; i++) {
        var hdrName = conn.getHeaderFieldKey(i);
        var hdrValue = conn.getHeaderField(i);
        if (hdrName == null && hdrValue == null) {
          break;
        }
        if (hdrName != null) {
          this._headers[this._headers.length] = {h:hdrName, v:hdrValue};
          switch (hdrName.toLowerCase()) {
            case 'content-encoding': gotContentEncoding = true; break;
            case 'content-length'  : gotContentLength   = true; break;
            case 'content-type'    : gotContentType     = true; break;
            case 'date'            : gotDate            = true; break;
            case 'expires'         : gotExpiration      = true; break;
            case 'last-modified'   : gotLastModified    = true; break;
          }
        }
      }
      // try to fill in any missing header information
      var val;
      val = conn.getContentEncoding();
      if (val != null && !gotContentEncoding) this._headers[this._headers.length] = {h:'Content-encoding', v:val};
      val = conn.getContentLength();
      if (val != -1 && !gotContentLength) this._headers[this._headers.length] = {h:'Content-length', v:val};
      val = conn.getContentType();
      if (val != null && !gotContentType) this._headers[this._headers.length] = {h:'Content-type', v:val};
      val = conn.getDate();
      if (val != 0 && !gotDate) this._headers[this._headers.length] = {h:'Date', v:(new Date(val)).toUTCString()};
      val = conn.getExpiration();
      if (val != 0 && !gotExpiration) this._headers[this._headers.length] = {h:'Expires', v:(new Date(val)).toUTCString()};
      val = conn.getLastModified();
      if (val != 0 && !gotLastModified) this._headers[this._headers.length] = {h:'Last-modified', v:(new Date(val)).toUTCString()};
      // read response data
      var reqdata = '';
      var stream = conn.getInputStream();
      java.lang.System.err.println(stream);
      if (stream) {
        var reader = new java.io.BufferedReader(new java.io.InputStreamReader(stream, this._getCharset()));
        var line;
        while ((line = reader.readLine()) != null) {
          if (this.readyState == 2) {
            this.readyState = 3;
            if (this.onreadystatechange) {
              this.onreadystatechange();
            }
          }
          reqdata += line + '\n';
        }
        reader.close();
        this.status = 200;
        this.statusText = 'OK';
        this.responseText = reqdata;
        this.readyState = 4;
        if (this.onreadystatechange) {
          this.onreadystatechange();
        }
        if (this.onload) {
          this.onload();
        }
      } else {
        // error
        this.status = 404;
        this.statusText = 'Not Found';
        this.responseText = '';
        this.readyState = 4;
        if (this.onreadystatechange) {
          this.onreadystatechange();
        }
        if (this.onerror) {
          this.onerror();
        }
      }
    };
  };
}
// ActiveXObject emulation
if (!window.ActiveXObject && window.XMLHttpRequest) {
  window.ActiveXObject = function(type) {
    switch (type.toLowerCase()) {
      case 'microsoft.xmlhttp':
      case 'msxml2.xmlhttp':
      case 'msxml2.xmlhttp.3.0':
      case 'msxml2.xmlhttp.4.0':
      case 'msxml2.xmlhttp.5.0':
        return new XMLHttpRequest();
    }
    return null;
  };
}

function fillDivByID(html,id)
{
    var elem = myGetElementByID(id);
    elem.innerHTML = html;
}

function appendDivByID(html,id)
{
    var elem = myGetElementByID(id);
    elem.innerHTML = elem.innerHTML + html;
}

function myGetElementByID(elemID)
{
    if (document.getElementById)
    {   // this is the way the standards work
        return(document.getElementById(elemID));
    }
    else if (document.all)
    {   // this is the way old msie versions work
        return(document.all[elemID]);
    }
    else if (document.layers)
    {   // this is the way nn4 works
        return(document.layers[elemID]);
    }
}
/* The following is the AJAH code by Michael Cramer of Cramer Technologies */
window.ajahResult = function()
{
	this.error = 0;
	this.errorDetail = new function() {this.code = 0; this.string = ''; this.raw = '';};
	this.value = null;
}
window.ajahObj = function()
{
	var xmlhttp;
	this.cmdID = 0;
	this.execID = 1;
	this.successCallback = null;
	this.failureCallback = null;
	this.passbackValue = null;
	this.useGlobalIndicator = false;
	
	this.init = function()
	{
	    this.queue = new Array();
	    this.req = null;
	    this.baseRef = document.location.href.substring(0,document.location.href.lastIndexOf("/") + 1);
	    this.idToFill = '';
	    this.callInProgress = false;
	    //alert('queue created: ' + this.queue.length);
	}
	this.init();
	this.resultsFunc = function(results)
	{
	    try {
	        if(this.idToFill != null)
	        {
	            var elem = myGetElementByID(this.idToFill);
	            //alert('elem.innerHTML: ' + elem.innerHTML);
	            if(elem != null) {
					elem.innerHTML = unescape(results.value);
				}
	        }
	        if(results.error == 0 && this.successCallback != null) {
	            if(this.passbackValue != null) {
	                this.successCallback(this.passbackValue, results.value);
	            } else { 
	                this.successCallback(results.value);
	            }
	        } else if(results.error != 0 && this.failureCallback != null) {
	            this.failureCallback(results.value);
	        }
	    } catch(err) {
			alert('Error caught in resultsFunc: ' + err.message + '\r\nPossible error in ajah page.');
	    }
	    this.hideLoading();
	    this.callInProgress = false;
	    this.execID++;
	    this.executeNextCall();
	}
	this.serviceStateChange = function()
	{
		var result = new window.ajahResult;
		try
		{
		    //alert('readyState: ' + this.req.readyState);
		    if ( this.req != null && this.req.readyState > 2)
		    {
                if(this.req.readyState > 3 && this.req.status == 200)
                {
                    try { 
                        result.error = 0;
                        result.errorDetail.code = 0;
                        result.errorDetail.string = this.req.responseStatus;
                        result.errorDetail.raw = this.req.responseText;
                        result.value = this.req.responseText;
                    } catch(err) {
		                alert('Error caught before resultsFunc: ' + err.message);
                    }
                    this.resultsFunc(result);
                    return;
                }
		        else if(this.req.readyState == 4)
		        {
			        //alert('this.req.readyState: ' + this.req.readyState + '\nthis.req.status: ' + this.req.status);
			        result.error = 1;
			        result.errorDetail.code = -1;
			        result.errorDetail.string = 'Unknown error';
			        result.errorDetail.raw = this.req.responseText;
			        //alert('status: ' + this.req.status + '\nresponse: ' + this.req.responseText);
			        result.value = 'AJAH Error: ' + escape(this.req.responseText);
			        this.resultsFunc(result);
			        return;
		        }
		    }
		} catch(err)
		{
			result.error = 1;
			result.errorDetail.code = -1;
			result.errorDetail.string = 'Unknown error';
			result.errorDetail.raw = this.req.responseText;
			result.value = 'AJAH Error thrown: ' + err.message + '\nresponse: ' + this.req.responseText;
		    this.resultsFunc(result);
		}
		//alert('this.req.readyState: ' + this.req.readyState + '\nthis.req.status: ' + this.req.status);
	}
	this.createRequest = function(page, id, success, failure, passbackValue, globalIndicator)
	{
		var ajahRequest = new Object();
	    ajahRequest.page = page;
	    ajahRequest.id = id;
	    ajahRequest.successCallback = success;
	    ajahRequest.failureCallback = failure;
	    ajahRequest.passbackValue = passbackValue;
	    ajahRequest.method = 'GET';
	    ajahRequest.postData = '';
	    ajahRequest.useGlobalIndicator = globalIndicator;
	    return ajahRequest;
	}
	this.getCmdIDandExecute = function() {
	    var myID = ++this.cmdID;
	    //alert('myID: ' + myID + ', execID: ' + this.execID);
	    if(myID == this.execID) {
	        this.executeNextCall(this);
	    }
	}
	this.fillByID = function(page, id)
	{
	    this.queue.push(this.createRequest(page, id, null, null, null, false));
	    this.getCmdIDandExecute();
	}
	this.fillByIDCallback = function(page, id, success, failure, useGlobalIndicator)
	{
	    if(useGlobalIndicator == null) useGlobalIndicator = false;
	    this.queue.push(this.createRequest(page, id, success, failure, null, useGlobalIndicator));
	    this.getCmdIDandExecute();
	}
	this.fillByIDCallbackWithValue = function(page, id, success, failure, passbackValue, useGlobalIndicator)
	{
	    if(useGlobalIndicator == null) useGlobalIndicator = false;
	    this.queue.push(this.createRequest(page, id, success, failure, passbackValue, useGlobalIndicator));
	    this.getCmdIDandExecute();
	}
	this.getValue = function(page, success, failure, useGlobalIndicator)
	{
	    if(useGlobalIndicator == null) useGlobalIndicator = true;
	    this.queue.push(this.createRequest(page, null, success, failure, null, useGlobalIndicator));
	    this.getCmdIDandExecute();
	}
	this.fillByIDPostCB = function(page, id, success, failure, postData, useGlobalIndicator)
	{
	    if(useGlobalIndicator == null) useGlobalIndicator = true;
	    var req = this.createRequest(page, id, success, failure, null, useGlobalIndicator);
	    req.method = 'POST';
	    req.postData = postData;
	    this.queue.push(req);
	    this.getCmdIDandExecute();
	}
	this.executeNextCall = function()
	{
	    var Me = this;
		if (Me.callInProgress == false && Me.queue.length > 0)
		{
		    var now = new Date();
		    Me.callInProgress = true;
           	Me.xmlhttp = new XMLHttpRequest();
            Me.req = Me.xmlhttp;
		    var ajahRequest = Me.queue.shift();
		    Me.idToFill = ajahRequest.id;
		    Me.useGlobalIndicator = ajahRequest.useGlobalIndicator;
		    Me.showLoading();
        	Me.successCallback = ajahRequest.successCallback;
	        Me.failureCallback = ajahRequest.failureCallback;
	        Me.passbackValue = ajahRequest.passbackValue;
			Me.req.onreadystatechange = function() { Me.serviceStateChange(); };
			//var url = Me.baseRef + ajahRequest.page;
			var url = ajahRequest.page;
			if(url.indexOf('?') > 0) {
			    url = url + '&';
			} else {
			    url = url + '?';
			}
			url = url + 'ver=' + Me.execID + now.getTime().toString();
			//alert('url: ' + url + '\npostData: ' + ajahRequest.postData);
			Me.req.open(ajahRequest.method, url);
			if(ajahRequest.method == 'POST') {
			    Me.req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			    Me.req.setRequestHeader("Content-length", ajahRequest.postData.length);
			}
			Me.req.send(ajahRequest.postData);
			//alert('readyState: '+this.req.readyState);
		}
    }
    this.showGlobalLoading = function()
    {
        var bodyElem = document.getElementsByTagName('body')[0];
        var loadingDiv = document.createElement('div');
        loadingDiv.setAttribute('id', 'ajahLoading');
        loadingDiv.className = 'ajahLoading';
        loadingDiv.innerHTML = '<img src="/images/comm/loading.gif" border="0">';
        bodyElem.appendChild(loadingDiv);
    }
    this.hideGlobalLoading = function()
    { 
         var bodyElem = document.getElementsByTagName('body')[0];
         var loadingDiv = myGetElementByID('ajahLoading');
         bodyElem.removeChild(loadingDiv);
    }
    this.showLoading = function()
    {
        if(this.useGlobalIndicator == true) {
            this.showGlobalLoading(); 
        } else {
            var elem = myGetElementByID(this.idToFill);
            if(elem != null) {
				elem.innerHTML = '<img src="/images/comm/loading.gif" border="0">';
			}
        }
    }    
    this.hideLoading = function()
    {
        if(this.useGlobalIndicator == true) {
            this.hideGlobalLoading();
        }
    }   
}
var ajah = new ajahObj();