/******************************************************************************
 * AJAX XHTML Object
 ******************************************************************************/
var ajax=new Object();
var curRequest;
var ajaxIntervalID;

ajax.READY_STATE_UNINITIALIZED=0;
ajax.READY_STATE_LOADING=1;
ajax.READY_STATE_LOADED=2;
ajax.READY_STATE_INTERACTIVE=3;
ajax.READY_STATE_COMPLETE=4;

ajax.Queue=function() {
  this.isRunning = 0;
  this.isProcessing = 0;
  this.queueBuffer = new Array();
}

ajax.Queue.prototype={

  addRequest:function(action,param,contid,url,onload,onerror) {
    var newReq = new ajax.Request(action,param,contid,url,onload,onerror,'N');
    this.queueBuffer.push(newReq);
    if (this.isRunning == 0) {
      this.isRunning = 1;
      this.run();
    }
  },
  
  run:function() {
    if (this.queueBuffer.length > 0) {
      ajaxIntervalID = setInterval(function () {
        if (ajaxQueue.isProcessing == 0) {
          curRequest = ajaxQueue.queueBuffer.shift();
          ajaxQueue.isProcessing = 1;
          curRequest.run();
        }
        else {
          //debugHTML = document.getElementById("debug").innerHTML;
          //document.getElementById("debug").innerHTML = debugHTML + curRequest.url + curRequest.action + curRequest.param + " (Status: " + curRequest.req.readyState + ")<br>";
          if (curRequest.req.readyState == ajax.READY_STATE_COMPLETE)
            ajaxQueue.isProcessing = 0;
        }
        if (ajaxQueue.queueBuffer.length == 0 && ajaxQueue.isProcessing == 0) {
          clearInterval(ajaxIntervalID);
          ajaxQueue.isRunning = 0;
        }
      }, 50);
    }
  }
}

ajax.Request=function(action,param,contid,url,onload,onerror,runNow) {
  this.action=action;
  this.param=param;
  this.req=null;
  this.contid=(contid) ? contid : '';
  this.url=(url) ? url : './web/php/spu/spu_content_load.php?action=';
  this.onload=(onload) ? onload : this.defaultLoader;
  this.onerror=(onerror) ? onerror : this.defaultError;
  this.runNow=(runNow) ? runNow : 'Y';
  
  if (this.runNow == 'Y')
    this.run();
}

ajax.Request.prototype={

  run:function() {
    this.loadProgressBar();
    this.loadXMLDoc(this.url + this.action + this.param);
  },

  loadProgressBar:function(){
    if (this.contid == 'main_cont')
     document.getElementById(this.contid).innerHTML = "<div style='text-align:center;'><br><br><br><img src='" + gl_home_dir_img + "/pleasewait.gif'><\/div>";
  },

  loadXMLDoc:function(url){
    if (window.XMLHttpRequest){
      this.req=new XMLHttpRequest();                      //Mozila, Opera, Firefox
    } 
    else if (window.ActiveXObject){
      try {
        this.req=new ActiveXObject("Msxml2.XMLHTTP");     //IE (Old version)
      }
      catch (e) {
        this.req=new ActiveXObject("Microsoft.XMLHTTP");  //IE (New version)
      }
    }
    if (this.req){
      try {
        var loader=this;
        this.req.onreadystatechange=function(){
          loader.onReadyState.call(loader);
        }
        this.req.open('GET',url,true);
        this.req.send(null);
      } catch (err){
        this.onerror.call(this);
      }
    }
  },

  onReadyState:function(){
    var req=this.req;
    var ready=req.readyState;
    if (ready==ajax.READY_STATE_COMPLETE){
      //var httpStatus=req.status;
      //if (httpStatus==200 || httpStatus==0){
      this.onload.call(this);
    //}else{
    // this.onerror.call(this);
    }
  },
    
  defaultError:function(){
    alert("error fetching data!"
    +"\n\nreadyState:"+this.req.readyState
    +"\nstatus: "+this.req.status
    +"\nheaders: "+this.req.getAllResponseHeaders());
  },
  
  defaultLoader:function(){
    if (this.contid != '') {
      if (this.req.responseText.substring(7,0) == 'session')
        window.location.href = gl_home + '?' + this.req.responseText;
      else if (this.req.responseText.substring(5,0) == 'error') {
        this.contid = this.req.responseText.substring(this.req.responseText.indexOf('->',6),7);
        document.getElementById(this.contid).innerHTML = this.req.responseText.substring(this.req.responseText.indexOf('->',7) + 2);
      }
      else
        document.getElementById(this.contid).innerHTML = this.req.responseText;
    }
  }
}
//Create the global ajaxQueue object
var ajaxQueue = new ajax.Queue();



/******************************************************************************
 * Wrapper Function for old AJAX Requests
 ******************************************************************************/
function reloadContent(action, param, contid) {
 var ajaxReq = new ajax.Request(action, param, contid);
}


/******************************************************************************
 * Menu Tree Functions
 ******************************************************************************/
function showHideNodes(parentObjectID) { 
 if ((document.getElementById(parentObjectID + '1') != null) && (!isVisible(parentObjectID + '1'))) {
  var i = 1;
  while (document.getElementById(parentObjectID + i) != null) {
   setVisibility(parentObjectID + i,true);
   i++;
  }
 }
 else if ((document.getElementById(parentObjectID + '1') != null) && (isVisible(parentObjectID + '1'))) {
   hideAllNodes(parentObjectID);
 }
}

function hideAllNodes(parentObjectID) {
 if (document.getElementById(parentObjectID + '1') != null) {
  var i = 1;
  while (document.getElementById(parentObjectID + i) != null) {
   setVisibility(parentObjectID + i,false);
   hideAllNodes(parentObjectID + i);
   i++;
  }
 }
}

function isVisible(htmlObjectID) {
 if (document.getElementById(htmlObjectID).style.display == 'none')
  return false;
 else
  return true;
}

function setVisibility(htmlObjectID,visible) {
 if (visible == true)
  document.getElementById(htmlObjectID).style.display = 'block';
 else
  document.getElementById(htmlObjectID).style.display = 'none';
}



/******************************************************************************
 * Image Functions
 ******************************************************************************/
function hlImage(Image) {
 Image.src = Image.src.substring(0,Image.src.length-4) + "_hl" + Image.src.substring(Image.src.length-4,Image.src.length);
}

function unhlImage(Image) {
 Image.src = Image.src.substring(0,Image.src.length-7) + Image.src.substring(Image.src.length-4,Image.src.length);
}



/******************************************************************************
 * Form Validation Function
 * ****************************************************************************/
function validateFormField(str,pattern,mode,emptyallowed) {
 var flag = true;
 
 if ((!emptyallowed) && (str == ''))
  return false;
 else if ((emptyallowed) && (str == ''))
  return true;
 else
  flag = ((pattern.test(str)) ? true : false);
 
 return ((mode == 'R') ? !flag : flag);
}
