/************************************************************************************************************/
// Dynamix Solutions Ajax Implementation of the SACK
// Tim Haselaars
// Dynamix Solutions
// VERSION 1.0 
// DATE 11-10-2006
/************************************************************************************************************/

// Variables
var debug_mode = false;
var enableCache = false;
var jsCache = new Array();

// Variables DIV Id
var divLoading  = 'loading';
var divDebug    = 'debug';

// Makes it possible to load multiple ajax pages
var ajaxObjects = new Array();

// Show content from URL in Div
function ajax_showContent(divId,ajaxIndex,url)
{
  /*
   * Safari Workaround
   */
  var myDiv = divLoading; 
  var divObj = document.getElementById(myDiv);
  if (divObj) {
    if (divObj.style.display == '' || divObj.style.display == 'block' ) {
      // HIDE LOADING IMAGE
      divObj.style.display = 'none';
    }
  }

  //Show debug info in div with id debug
  ajax_debug(ajaxObjects[ajaxIndex]);
  
  var targetObj = document.getElementById(divId);
  targetObj.innerHTML = ajaxObjects[ajaxIndex].response;
  if(enableCache){
    jsCache[url] =  ajaxObjects[ajaxIndex].response;
  }
  ajaxObjects[ajaxIndex] = false;
  
  ajax_parseJs(targetObj)
}

// Show content from POST in Div
function ajax_showPostContent(divId) 
{
  /*
   * Safari Workaround
   */
  var myDiv = divLoading; 
  var divObj = document.getElementById(myDiv);
  if (divObj) {
    if (divObj.style.display == '' || divObj.style.display == 'block' ) {
      // HIDE LOADING IMAGE
      divObj.style.display = 'none';
    }
  }

  //Show debug info in div with id debug
  ajax_debug(ajaxPostObjects);
    
  var targetObj = document.getElementById(divId);
  targetObj.innerHTML = ajaxPostObjects.response;
  ajaxPostObjects = false;
  
  ajax_parseJs(targetObj);
}

function ajax_postContent(divId, frm)
{
  var form = document.getElementById(frm);
  
  //Initialize the sack object
  ajaxPostObjects = new sack();

  //Loop through all form element and add them to the objects vars
  for(i=0; i<form.elements.length; i++)
  {
    if (form.elements[i].type != undefined)
    {
      ajaxPostObjects.setVar(form.elements[i].name, form.elements[i].value); // recomended method of setting data to be parsed.
    }
  }
  ajaxPostObjects.requestFile = form.action; //Get form action
  ajaxPostObjects.method = form.method; //Get form method
  //ajaxPostObjects.element = 'replaceme';
  ajaxPostObjects.onLoading = function(){ ajax_showLoading(); };  // Specify function that will be executed while file has been found
  //ajaxPostObjects.onLoaded = function(){ ajax_showLoaded(); };  // Specify function that will be executed after file has been found
  ajaxPostObjects.onCompletion = function(){ ajax_showPostContent(divId); }; // Specify function that will be executed after post has been send
  ajaxPostObjects.runAJAX();    // Execute AJAX function  
}

// Ajax Server request
function ajax_loadContent(divId,url)
{
  if(enableCache && jsCache[url]){
    document.getElementById(divId).innerHTML = jsCache[url];
    return;
  }
  var ajaxIndex = ajaxObjects.length;
  ajaxObjects[ajaxIndex] = new sack();
  ajaxObjects[ajaxIndex].requestFile = url; // Specifying which file to get
  ajaxObjects[ajaxIndex].onLoading = function(){ ajax_showLoading(); }; // Specify function that will be executed while file has been found
  ajaxObjects[ajaxIndex].onLoaded = function(){ ajax_showLoaded(); }; // Specify function that will be executed after file has been found
  ajaxObjects[ajaxIndex].onCompletion = function(){ ajax_showContent(divId,ajaxIndex,url); }; // Specify function that will be executed after file has been found
  ajaxObjects[ajaxIndex].runAJAX();   // Execute AJAX function  
}

// Ajax Server Loading state
function ajax_showLoading (divId)
{
  if (divId == undefined)
  {
    divId = divLoading; 
  }
  var divObj = document.getElementById(divId);
  if (divObj) {
    if (divObj.style.display == '' || divObj.style.display == 'block' ) {
      // HIDE LOADING IMAGE
      divObj.style.display = 'none';
    } else {
      // SHOW LOADING IMAGE
      divObj.style.display = 'block';
    }
  }
}

// Ajax Server Loaded state
function ajax_showLoaded(divId)
{
  // Toggle of the loading div
  ajax_showLoading(divId);
  
  // Other actions when page is loaded
}

function ajax_debug(object) {
  if (debug_mode == true)
  {
    var debug_div = document.getElementById(divDebug); 
    if (object.responseStatus){
      var string = "<p>Status Code: " + object.responseStatus[0] + "</p><p>Status Message: " + object.responseStatus[1] + "</p><p>URLString Sent: " + object.URLString + "</p>";
    } else {
      var string = "<p>URLString Sent: " + object.URLString + "</p>";
    }
    debug_div.innerHTML = string; 
  }
}

// Parse javascript from URL
function ajax_parseJs(inputObj)
{ 
  var jsTags = inputObj.getElementsByTagName('SCRIPT');
  for(var no=0;no<jsTags.length;no++){
    eval(jsTags[no].innerHTML);
  } 
}