/************************************************************************************************************/
// 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  = 'myLoadingDiv';
var divDebug    = 'debug';
var animation   = '';

// 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);
  if (targetObj) {
    targetObj.innerHTML = ajaxObjects[ajaxIndex].response;
    if (targetObj.style.display = 'none') {
      targetObj.style.display = 'block';
    }
    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);
  if (targetObj) {
    targetObj.innerHTML = ajaxPostObjects.response;
    if (targetObj.style.display = 'none') {
      targetObj.style.display = 'block';
    }
    ajaxPostObjects = false;

    ajax_parseJs(targetObj);
  }
}

/*
 * Show POST content
 * @param: _resultDiv (string)
 * @param: _postOkCheck (string)
 * @param: _valueTrue (string)
 * @param: _valueFalse (string)
 */
function ajax_showPostContentValue(_resultDiv, _postOkCheck, _valueTrue, _valueFalse) 
{
  /*
   * 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(_resultDiv);
  if (targetObj) {
    var result = ajaxPostObjects.response;
    if (trim(result) == 'TRUE') {
      targetObj.innerHTML = _valueTrue
    } else {
      targetObj.innerHTML = _valueFalse
    }
    if (targetObj.style.display = 'none') {
      targetObj.style.display = 'block';
    }
    ajaxPostObjects = false;

    ajax_parseJs(targetObj);
  }
}

function ajax_silentPost(_frm, _actionDone) {
  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
  if (_actionDone ) ajaxPostObjects.onCompletion = function(){ eval(_actionDone); }; // Specify function that will be executed after post has been send
  ajaxPostObjects.runAJAX();                  // Execute AJAX function
}

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  
}

/*
 * POST content
 * @param: _form (string)
 * @param: _resultDiv (string)
 * @param: _postOkCheck (string)
 * @param: _valueTrue (string)
 * @param: _valueFalse (string)
 */
function ajax_postContentValue(_form, _resultDiv, _postOkCheck, _valueTrue, _valueFalse) 
{
  var form = document.getElementById(_form);
  
  //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('newsletterLoading'); };  // 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_showPostContentValue(_resultDiv, _postOkCheck, _valueTrue, _valueFalse); }; // 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  
}

// Div blinking
var blinkingTmr;

function initBlinking(divId) {
  animation  = '*blink*';
  divLoading = divId;
}

function ajax_showLoading (divId)
{
  if (animation == '*blink*') {
    // Start blinking
    document.body.focus();
    startBlinking(divLoading);
  } else {
    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';
      }
    }
  }
}

function startBlinking(myDiv) {
  var divObj = document.getElementById(myDiv);
  if (divObj) {
    if (divObj.style.visibility == 'hidden') {
      divObj.style.visibility = 'visible';
    } else {
      divObj.style.visibility = 'hidden';
    }
    blinkingTmr = window.setTimeout('startBlinking(\'' + myDiv + '\')', 100);
  }
}

function stopBlinking(myDiv) {
  window.clearTimeout(blinkingTmr);
  var divObj = document.getElementById(myDiv);
  if (divObj) 
    divObj.style.visibility = 'visible';
  animation = '';
  divLoading = '';
}

// Ajax Server Loaded state
function ajax_showLoaded(divId)
{
  // Stop blinking
  stopBlinking(divLoading);
  
  // 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);
  } 
}