/*

Unless noted otherwise:

Copyright(C), 2007, Enfold Systems, Inc. - ALL RIGHTS RESERVED

This software is licensed under the Terms and Conditions
contained within the "license.txt" file that accompanied 
this software.  Any inquiries concerning the scope or 
enforceability of the license should be addressed to:

Enfold Systems, Inc.
4617 Montrose Blvd., Suite C215
Houston, Texas 77006 USA
p. +1 713.942.2377 | f. +1 832.201.8856
www.enfoldsystems.com
info@enfoldsystems.com

*/

/************************************************************************/
// This section handles the onload events.
// By default we're looking for a DOM ready event instead of an onload
// event, but if the browser can't support DOMready then we fallback
// to a regular onload.
/************************************************************************/


startStack=function() { };  // A stack of functions to run onload/domready
var ranOnload=0; // set to 1 if the dom ready events already executed the stack.

doRegisterOnLoad = function(func) {
	 // This function accepts a function as an argument
	 // and "pushes it" onto a stack which will be executed
	 // whenever the script determines that the DOM has been loaded.
   var orgOnLoad = startStack;
   if (typeof(orgOnLoad) != 'function') {
      startStack=func;
   } else {
      startStack = function () {
         orgOnLoad();
         func();
         return;
      }
   }
}

if (document.addEventListener) {
  // This is the mozilla style, it's pretty sane and sensible.
   document.addEventListener("DOMContentLoaded", function(){ranOnload=1; startStack()}, false);
}  else if (document.all && !window.opera) {
  // This is the IE style, it's a pretty ugly hack.  GG MS.
  // Basically we insert a script tag at this location in the document and then
  // attach an onreadystate handler to it.  Because of the way IE handles the
  // defer attribute we'll get a "complete" when the DOM is ready but before
  // loading images and such.
  document.write("<scr" + "ipt id='DOMReady' defer=true " + "src=//:><\/scr" + "ipt>");  
  document.getElementById("DOMReady").onreadystatechange=function(){
    if (this.readyState=="complete"&&(!ranOnload)){
      ranOnload=1
      startStack();
    }
  }
}

window.onload=function(){
  if (!ranOnload) {
     ranOnload=1;
     // only run this if the domready events didn't fire.
     startStack();
  }
}

/****************************************************/
/* This code populates $_GET emulating php's $_GET  */
/****************************************************/

$_GET = {};   // Emulates php's $_GET structure.

populateGet = function () {
  
    // This function loads up $_GET as an associative array (really a javascript object)
    // populated from the URL.   The goal of this function is to set up $_GET 
    // so that its use is compatible with php.
  
    var params = /\?(.*)$/.exec(document.location.href);
    if (params) {
      if (params[1]) {
         params=params[1].split('&');
         for (var i=0; i<params.length; i++) {
           var nameValue = params[i].split('=');
           if (!nameValue[1]) { nameValue[1]=''; }
           $_GET[nameValue[0]]=decodeURIComponent(nameValue[1].replace(/\'/g,'').replace(/\"/g,''));
         }
      }
   }
   if ($_GET['validate']!=undefined) {
     document.location.href='http://validator.w3.org/check?uri=referer';
   }
}(); // execute immediately

/****************************************************/
/* Set external targets                             */
/* XHTML strict doesn't allow target='_blank' in    */
/* Anchor tags so we'll add them through javascript */
/* for anchors with rel='external' tags.            */
/****************************************************/

setExternalLinks = function() {
   var anchors = document.getElementsByTagName('a');
   for (var i=0; i<anchors.length; i++) {
      if (anchors[i].rel.match(/external/i)) {
        anchors[i].target='_blank';
      }
   }
}

doRegisterOnLoad(setExternalLinks);

/****************************************************/
/* Cookie Code is Public Domain                     */
/****************************************************/

function cookiesAllowed() {
   setCookie('checkCookie', 'test', 1);
   if (getCookie('checkCookie')) {
      deleteCookie('checkCookie');
      return true;
   }
   return false;
}

function setCookie(name,value,expires, options) {
   if (options===undefined) { options = {}; }
   if ( expires ) {
      var expires_date = new Date();
      expires_date.setDate(expires_date.getDate() + expires)
   }
   document.cookie = name+'='+escape( value ) +
      ( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + 
      ( ( options.path ) ? ';path=' + options.path : ';path=/' ) +
      ( ( options.domain ) ? ';domain=' + options.domain : '' ) +
      ( ( options.secure ) ? ';secure' : '' );
}

function getCookie( name ) {
   var start = document.cookie.indexOf( name + "=" );
   var len = start + name.length + 1;
   if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
      return null;
   }
   if ( start == -1 ) return null;
   var end = document.cookie.indexOf( ';', len );
   if ( end == -1 ) end = document.cookie.length;
   return unescape( document.cookie.substring( len, end ) );
}

function deleteCookie( name, path, domain ) {
   if ( getCookie( name ) ) document.cookie = name + '=' +
      ( ( path ) ? ';path=' + path : '') +
      ( ( domain ) ? ';domain=' + domain : '' ) +
      ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

function toggleDiv(sourceId, destId) {
   var destId=document.getElementById(destId);
   var tmp = sourceId.innerHTML;
   sourceId.innerHTML=destId.innerHTML;
   destId.innerHTML=tmp;
   if (sourceId.style.cursor=='help') {
      sourceId.style.cursor='pointer';
   } else {
      sourceId.style.cursor='help';
   }
}

function stabilize() {
   var theTop=0;
   if (document.documentElement && document.documentElement.scrollTop) {
     theTop = document.documentElement.scrollTop;
   } else if (document.body) {
     theTop = document.body.scrollTop;
  }
  setCookie('stabilize',theTop,1);
}
  
doRegisterOnLoad(function() {
    if (getCookie('stabilize')) {
      window.scrollTo(0,getCookie('stabilize'));
      deleteCookie('stabilize','/');
    }
});  
   

