/**********--Site Section Loader--************************************************************/
/*
/* This is a cross-browser content loader which receives data in the form of a string FREE OF
/* CARRAIGE RETURNS and with SLASHES applied to all quotes.
/*
/* loadLinks(); should be entered into the body onload event handler. This converts ALL links
/* on the given site to content-loader links. Any links that are to completely reload the page
/* (or load to the specified address) should have the attribute "redirect" equalling "1"
/* (e.g., <a href="www.outside.com" redirect="1">link</a>). The content source file should output
/* a json format plaintext string of the format "sectionName" : "sectionContent", etc., with 
/* quotes escaped. Specify these names in the "sectionNames" array below, and be sure that the
/* target container tags have the same name as that specified in the array. If the item is an
/* HTML tagName (e.g. <title>), add three stars (***) to the end of the name with no spaces.
/*
/* The source file is provided with the flag $_GET['js'] == 1 to tell it to output json format
/* (in case you're using a dual-output file).
/*
/*********************************************************************************************/

var sectionNames = new Array('title___','nav_links','body_sub','body_content'); //Edit this to accommodate sections to fill.

function loadLinks() {
 var i;
 var docLinks = document.getElementsByTagName('a')
 for(i = 0; i < docLinks.length; i++) {
  docLinks[i].onclick = function() {
   if (this.getAttribute('redirect') == 1) {
    return true;
   } else {
    var ref = this.href;
    if (!ref.match('jsReq=1')) {
     if (!ref.match('=')) {
      ref += '?jsReq=1';
     } else {
      ref += '&jsReq=1';
     }
    }
    var req = makeRequest(ref);
    if (!req) {
     return true;
    }
    return false;
   }
  }
 }
}

function makeRequest(url,method,params) {
 toggleLoad();
 http_request = false;
 if (!method) {
  var method = 'GET';
 }
 if (!params) {
  var params = null;
 }
 if (window.XMLHttpRequest) { // Mozilla, Safari,...
  http_request = new XMLHttpRequest();
 } else if (window.ActiveXObject) { // IE
  try {
   http_request = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
   http_request = new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
 if (!http_request) {
  return false;
 }

 http_request.onreadystatechange = fillContents;
 http_request.open(method, url, true);
 http_request.send(params);
 return true;
}

function fillContents() {
 if (http_request.readyState == 4) {
  toggleLoad();
  if (http_request.status == 200) {
   var cont = eval('('+http_request.responseText+')');
   var nm;
   for(var i = 0; nm = sectionNames[i]; i++) {
    var evlStr = '';
    if (nm.match('___')) {
     nm = nm.substring(0,nm.indexOf('___'));
     evlStr += ' var '+nm+' = cont.'+nm+';';
     evlStr += ' if (document.'+nm+') { document.'+nm+' = '+nm+'; }';
    } else {
     evlStr += ' var '+nm+' = cont.'+nm+';';
     evlStr += ' var '+nm+'Cont = document.getElementById(\''+nm+'\');';
     evlStr += ' if ('+nm+'Cont) { contentReplace('+nm+'Cont,'+nm+') }';
    }
    eval(evlStr);
   }
   loadLinks();
  } else {
   alert('There was a problem with the request. Please try a different selection.');
  }
 }
}

function contentReplace(nmCont,cont) {
 var ua = navigator.userAgent;
 if (ua.match('IE') && nmCont.tagName == 'TABLE') {
  var i;
  while(cont.indexOf('<td',ind) != -1) {
   var ind = 0;
   var ind = cont.indexOf('<td',ind);
   var idSt = cont.indexOf('id=',ind)+4;
   var idEnd = cont.indexOf('"',idSt);
   var cellId = cont.substring(idSt,idEnd);

   var cntSt = cont.indexOf('>',ind)+1;
   var cntEnd = cont.indexOf('</td>',cntSt);
   var cellCnt = cont.substring(cntSt,cntEnd);

   var cell = document.getElementById(cellId);
   if (cell) {
    cell.innerHTML = cellCnt;
   }
   cont = cont.substr(cntEnd+5);
  }
 } else {
  nmCont.innerHTML = cont;
 }
}

var loading = false;
function toggleLoad() {
 var loadingDiv = document.getElementById('loading');
 if (loadingDiv) {
  if (!loading) {
   loadingDiv.style.display = 'block';
   //document.getElementById('loadingImg').src = 'imgs/loading.gif';
   loading = true;
  } else {
   loadingDiv.style.display = 'none';
   loading = false;
  }
 }
}

function preloadImgs() {
 var d=document;
 d.p_ar = new Array();
 var args = preloadImgs.arguments;
 for(var i = 0; i < args.length; i++) {
  d.p_ar[i] = new Image;
  d.p_ar[i].src = args[i];
 }
}