/*
  CCSTAL - Caching Client-Side Template Abstraction Layer - Yeah Acronyms!
  More Details about this approach: http://thomas.ballard.ws/html-templates

  Author: Thomas Ballard (http://thomas.ballard.ws)
  © 2003-10  E.S.Q. Software (http://www.esqsoft.com)

  Questions, Comments, or Work? Let us build or convert a template for you. 
  Contact us at our website or by email.
*/



/* globals */
window.ccstalHeaderCalled = 0;    // assume when this library is included that if the header() isn't called explicitly that we want to permit the auto-loader to skin this page
window.ccstalSkinLateCalled = 0;  // similar to ccstalHeaderCalled, but for any manual skinLate() callers.

var EMa = 'mai'+'lto'+':'; var EMb = 'rwe'+'bmin'+'@'; 
var EMc = 'gma'+'il.co'+'m'; var EMd = '?subj'+'ect='+location.href;



/* determine caller's directory relative to the location of the template components. 
   intent is to allow CCSTAL template to function for FILE and HTTP protocols to accommodate 
   development on a file system and to operate in a production environment.
   In a file system, "/" often refers to a different location than docroot does on the web server.
*/
var mpath = location.href.match(/\.(com|net|info|ws)(.+)/)
try{
  mpath = mpath[2];
  var x = mpath.match(/\//g) || 0
  mpath = '';
  if(x && x.length){
    for(var i=0; i<x.length-1; i++){ mpath += '../'; }
  }
} catch(e){}



window.breadcrumbs = function(){
  sURL = new String;
  bits = new Object;
  var x = 0;
  var stop = 0;
  var sep = '&#172;';
  var output = "<a href=\"/\">ESQSoft.com</a> "+sep+" ";
  sURL = location.href;
  sURL = sURL.slice(8,sURL.length);
  chunkStart = sURL.indexOf("/");
  sURL = sURL.slice(chunkStart+1,sURL.length)
  while(!stop){
    chunkStart = sURL.indexOf("/");
    if (chunkStart != -1){ bits[x] = sURL.slice(0,chunkStart); sURL = sURL.slice(chunkStart+1,sURL.length); }
    else stop = 1;
    x++;
  }
  for(var i in bits){
    output += "<a href=\"";
    for(y=1;y<x-i;y++){
      output += "../";
    }
    output += bits[i] + "/\">" + bits[i] + "</a> "+sep+" ";
  }
  var t = document.title; 
  var total = output.length + t.length
  if(total>150 && (t.length > 20)){ t = t.substr(0,20) + '...'; }
  return output+t
}



/* start: skin late */
window.skinLate = function(){
  /* "CCSTAL" function designed to be called at the end of an HTML page. 
     This will slurp the page content before skinning, 
     create a skin using header() and footer() defined in this library, 
     and drop the original content into that skin. 
  */

  //alert('skinLate called')

  if(window.ccstalSkinLateCalled) return; /* if we already called this, don't do it again. (for the onload auto-skinning feature) */
  else window.ccstalSkinLateCalled = 1;

  if(window.ccstalHeaderCalled) return; /* if we already called the header, don't do it again */

  var oldHTML = document.body.innerHTML;
  document.body.innerHTML = '';
  var HTML = window.header() + oldHTML + window.footer()
  document.body.innerHTML = HTML

  //var el = document.createElement('div');
  //  el.innerHTML = HTML;
  //document.body.appendChild(el);
}

/* assume everybody is using this library with intent to auto-skin */
if(window.addEventListener){ window.addEventListener('DOMContentLoaded',skinLate,false); }
else if(window.attachEvent){ window.attachEvent('onload',skinLate); }
else{ window.onload = skinLate; }

//if(document.addEventListener) {
//  document.addEventListener("DOMContentLoaded", skinLate, false);
//} else {
//  window.onload = skinLate  
//}
/* end: skin late */







/* CUSTOM FUNCTIONS */

function populateNavLinks(){
  /*
    1. get ref to nav container (after it's been created on the DOM)
    2. get the default links list that goes in it (JSON structure)
    3. examine the calling HTML for an optional list, and dedupe it into the default list
  */

  var htmlObj = document.getElementById('ccstalNavLinks'); if(!htmlObj) return; 

  /* get default links, then check the parent for other links + dedupe */
    var arrLinks = [
      ['http://www.esqsoft.com/','Home'],
      ['http://www.esqsoft.com/site-map.htm','Sitemap'],
      [EMa+EMb+EMc+EMd,'Contact the author']
    ];

  /* scan for related links */  
    var srcObj = document.getElementById('relatedLinks'); /* optional: this should appear in the .htm|.html which uses this template */
    if(srcObj){
      var arrTmp = srcObj.getElementsByTagName('a');
      if(arrTmp && arrTmp.length){
        for(var i=0; i<arrTmp.length; i++){
          /* dedupe */
          var isDupe = 0;
          for(var j=0; j<arrLinks.length; j++){
            //alert('debug: \nD: '+arrTmp[i].href + '\nN: '+arrLinks[j][0]);
            if(arrLinks[j][0] == arrTmp[i].href){ 
              /* if it's a duplicate, use the one from the HTML file rather than the default--in case the text is different */
              var txt = arrTmp[i].text?arrTmp[i].text:arrTmp[i].innerText; /* IE or FF */
              arrLinks[j] = [ arrTmp[i].href, txt ]; 
              j=arrLinks.length; isDupe = 1; 
            }
          }
          if(!isDupe){ arrLinks[arrLinks.length] = [ arrTmp[i].href, arrTmp[i].text ]; }
        }
      }
    }
  
  //alert('debug:' + arrLinks);

  var html = ''
    +'<div align="left">'
    +'<h4>More</h4>'
    +'<ul class="list2">'

  for(var i=0; i<arrLinks.length; i++){
    html += '<li><a href="'+arrLinks[i][0]+'">'+arrLinks[i][1]+'</a>'
  }

  html += ''
    +'</ul>'
  
    +'<br><br><h4>Fun/Misc</h4>'
    +'<ul class="list2">'
    +'<li><a href="http://hangman.bappy.com" target="_blank">Hangman</a>'
    +'<li><a href="http://www.mavenspun.com/jigsaw-puzzles/" target="_blank">JigSaw Puzzles</a>'
    +'</ul>'
    +'</div>'

  htmlObj.innerHTML = html
}



function getTracking(){
  var html = ''

  html += ''
    +'<div style="margin-left:10px;"><!-- Start of StatCounter Code -->'
    +'<sc'+'ript type="text/javascript" language="javascript">'
    +'var sc_project=412504; '
    +'var sc_partition=2; '
    +'</sc'+'ript>'
    +'<sc'+'ript type="text/javascript" language="javascript" src="http://www.statcounter.com/counter/counter.js"></sc'+'ript>'
    +'<!-- End of StatCounter Code --></div>'

  /* google analytics */
  var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
  document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
  try {
    var pageTracker = _gat._getTracker("UA-214064-1");
    pageTracker._trackPageview();
  } catch(err) {}

  return html
}



window.domains_sale = function(){
  /* start domain search */
  var extensions = ''
    +'COM,INFO,NET,NET.BR,ME,FR,ORG,BIZ,COM.CO,NET.CO,'
    +'NOM.CO,IT,COM.BR,MOBI,MX,COM.ES,WS,US,NOM.ES,ES,ORG.ES,'
    +'CA,NL,ASIA,IN,COM.MX,AM,AT,BE,BZ,COM.BZ,CC,NET.BZ,DE,'
    +'EU,FM,GS,CO.IN,FIRM.IN,GEN.IN,IND.IN,NET.IN,ORG.IN,'
    +'JOBS,JP,MS,NU,CO.NZ,NET.NZ,ORG.NZ,TC,TK,TV,TW,COM.TW,'
    +'ORG.TW,IDV.TW,CO.UK,ME.UK,ORG.UK,VG'

  var html = ''
    +'<div id="domain_search" style="margin:0; padding:0;">'
    +'<form style="margin:0; padding:0;" method="post" action="https://www.securepaynet.net/gdshop/registrar/search.asp?se=%2B&prog%5Fid=hosting411&from=rhp" id="form1" name="form1" target="_blank"> '
    +'<table width="500" cellspacing="0" cellpadding="5" style="border:1px #eee solid; background:#cdd; white-space:nowrap;">'
    +'  <tr><td style="color:#000; border-right:1px #000 dotted; font-weight:bold;">'
    +'    DOMAIN NAME&nbsp;'
    +'  </td><td width="100%" style="color:#000; padding-left:10px;">'
    +'    <input type="hidden" name="checkAvail" value="1"> '
    +'    <input type="hidden" name="JScriptOn" value="yes"> '
    +'    <input type="hidden" name="submit1" value="Submit"> '
    +'    <u>w</u>ww<b style="font-size:125%;color:#000;">.</b>'
    +'    <input type="text" name="domainToCheck" maxlength="63" accesskey="w" style="border:1px #ccc solid;">'
    +'    <b style="font-size:125%;color:#000;">.</b><select name="TLD"> '

  var ext = extensions.split(/,/);
  for(var i=0; i<ext.length; i++){
    html += ''
    +'<option value=".'+ext[i].toUpperCase()+'">'+ext[i].toLowerCase()
  }

  html += ''
    +'    </select> '
    +'    <input type="button" value="Price/Availability" id="submit1" name="spoof" onclick="this.form.submit()"> '
    +'  </td>'
    +'</tr></table>'
    +'</form>'
    +'</div>'
  
  return (html)
}



/* CORE FUNCTIONS */
window.header = function(){
  if(window.ccstalHeaderCalled){ 
      /* if header was already called, don't auto-load it */
      window.footer = function(){ /* don't let the footer() counterpart get called again either */ }; 
      return; 
  } /* if we already called this, don't do it again. (for the onload auto-skinning feature) */
  else window.ccstalHeaderCalled = 1

  var html = ''

  var p = mpath+'template/template1.css'; if(location.host) p = mpath+'/template/template1.css'
  html+='<link rel="stylesheet" href="'+p+'">'

  +'<table width="100%" cellpadding="7" cellspacing="0" class="mainheading" style="position:fixed; height:40px; top:0;">'
  +'  <tr>'
  +'    <td width="470" style="padding-left:10px; white-space:nowrap;">'

  +'      <!-- Search Google -->'
  +'      <form style="display:inline;" action="http://www.esqsoft.com/javascript/search-results.htm" id="cse-search-box">'
  +'      <div>'
  +'        <input type="hidden" name="cx" value="partner-pub-1157892973816149:hfy1pm-l4em" />'
  +'        <input type="hidden" name="cof" value="FORID:11" />'
  +'        <input type="hidden" name="ie" value="ISO-8859-1" />'
  +'        <input type="text" name="q" size="33" />'
  +'        <input type="submit" name="sa" value="Search" />'
  +'      </div>'
  +'    </form>'
  +'    <sc'+'ript type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=en"></sc'+'ript>'
  +'      <!-- Search Google -->'
  +'    </td>'
  +'    <td align="right" width="440" id="u440x20">'

  + domains_sale()

  +'    </td>'
  +'  </tr>'
  +'</table>'

  html+=''
  +'<table width="1000" cellpadding="0" cellspacing="0"><tr valign="top"><td>'
  +'<table width="100%" cellspacing="0" cellpadding="0" style="border-top:1px black solid;border-right:1px black solid;margin-top:50px;">'
  +'<tr><td colspan="2" style="height:4px;padding:0px;margin:0px;background-color: #3679C4;"><img src="/fs_img/spacer.gif" width="728" height="4"></td></tr>'
  +'</table>'
  +'<table width="100%" cellspacing="20" cellpadding="10" class="main">'
  +'  <tr valign="top">'

  /* column */
  html+=''
  +'    <td width="160" align="center">'
  +'      <iframe id="ab8ac9ef" name="ab8ac9ef" src="http://hostagnostic.com/openads/adframe.php?n=ab8ac9ef&amp;clientid=7" framespacing="0" frameborder="no" scrolling="no" width="160" height="600" allowtransparency="true"><a href="http://hostagnostic.com/openads/adclick.php?n=ab8ac9ef" target="_blank"><img src="http://hostagnostic.com/openads/adview.php?clientid=7&amp;n=ab8ac9ef" border="0" alt=""></a></iframe>'
  +'      <div id="ccstalNavLinks"></div>'
  +'    </td>'

  /* column */
  html+=''
  +'    <td style="padding:10px;">'

  html+=''
  +'<table width="100%" cellpadding="5" cellspacing="0" style="background:#f3f3f3; border: 1px #ddd solid;border-left:0;border-right:0; margin-top:0px;margin-bottom:3px;"><tr><td>'
  +'<i style="font-size:10pt;font-weight:bold;">Web Developer Resources&nbsp;&nbsp;&nbsp;...How-Not-To lose your hair from Web Dev</i>'
  +'<div style="margin:0;margin-left:10px;">'
  +'You Are Here: '
  +breadcrumbs()
  +'</div>'
  +'</td>'
  +'<td width="100"><a href="http://www.esqsoft.com/" style="border:0;text-decoration:none;"><img src="http://www.esqsoft.com/images/2007/esqsm.gif" alt="E.S.Q. Software Logo" title="Education, Standards, Quality, in software ..ugh, what were we thinking adding our goal to our name?!" border="0"></a>'
  +'</td></tr></table>'

  if(!window.X300x250){
  html += ''
  +'<table cellspacing="0" cellpadding="0" align="right"><tr><td style="padding:70px 0px 20px 20px;">'
  +'<iframe id=\'a9920e5a\' name=\'a9920e5a\' src=\'http://hostagnostic.com/openads/adframe.php?n=a9920e5a&amp;clientid=8\' framespacing=\'0\' frameborder=\'no\' scrolling=\'no\' width=\'300\' height=\'250\' allowtransparency=\'true\'><a href=\'http://hostagnostic.com/openads/adclick.php?n=a9920e5a\' target=\'_blank\'><img src=\'http://hostagnostic.com/openads/adview.php?clientid=8&amp;n=a9920e5a\' border=\'0\' alt=\'\'></a></iframe>'
  +'</td></tr></table>'
  }

  +'<p>'

  return html
}



window.footer = function(isml,other_menu){
  if(!other_menu) other_menu = ''

  var html = ''
  if(!window.X728x90){
  html += ''
  +'<div style="padding: 20px 0 0 0;">'
  +'<iframe id=\'ad50696b\' name=\'ad50696b\' src=\'http://hostagnostic.com/openads/adframe.php?n=ad50696b&amp;clientid=6\' framespacing=\'0\' frameborder=\'no\' scrolling=\'no\' width=\'728\' height=\'90\' allowtransparency=\'true\'><a href=\'http://hostagnostic.com/openads/adclick.php?n=ad50696b\' target=\'_blank\'><img src=\'http://hostagnostic.com/openads/adview.php?clientid=6&amp;n=ad50696b\' border=\'0\' alt=\'\'></a></iframe>'
  +'</div>'
  }

  html += ''
  +'  <table width="100%" cellpadding="5" cellspacing="5">'
  +'    <tr valign="top">'
  +'      <td>'
  +         other_menu
  +'      </td>'
  +'    </tr>'
  +'  </table>'

  +'    </td>'
  +'  </tr>'
  +'</table>'
  +'</td></tr></table>'

  /* plea */
  html += ''
  +'<div id="helpdialog" '
  +'   style="display:none; position:absolute; right:20px; top:60px; z-index:999; width:185px; border:5px #393 solid; background:#ffa; padding:10px; '
  +'    opacity:0.9; filter:alpha(opacity=\'80\'); '
  +'">'
  +'Please help us with the cost of making this free. '
  +'In general, we receive a few cents when you click a sponsors advertisement. While it\'s small, every cent counts. '
  +'Your consideration of a sponsors offer is very much appreciated. Please take just a moment and show your support. '
  +'Thank you! '
  +'<div style="float:right;">(<a href="#" onclick="document.getElementById(\'helpdialog\').style.display=\'none\';">click to close</a>)</div>'
  +'<img id="beacon" src="/fs_img/spacer.gif" border="0" width="1" height="1">'
  +'</div>'
  +'<sc'+'ript>'
  +'var r=parseInt(Math.random()*100); '
  +'if(r>80){ document.getElementById(\'helpdialog\').style.display=\'\'; document.getElementById(\'beacon\').src=\'http://c.statcounter.com/5339983/0/337374cb/1/\'; }'
  +'</sc'+'ript>'


  // if a code sample is present, make the source available (requires ivnS (source) and ivnT (target) containers)
  //html+=''
  //+'<sc'+'ript type="text/javasc'+'ript">'
  //+'window.codeSource = function(){'
  //+'  if(document && document.getElementById){'
  //+'    sObj = document.getElementById("ivnS");tObj = document.getElementById("ivnT");'
  //+'    if(sObj && sObj.innerHTML && tObj) tObj.innerHTML = \'<textarea wrap="soft" class="code-sample" ondblclick="this.select()" onclick="if(!this.init){this.init=1;this.select();}">\'+sObj.innerHTML+\'</textarea>\''
  //+'  }'
  //+'}; window.codeSource();'
  //+'</sc'+'ript>'

  // add animator to search
  /*
  if(!location.host) html+='<sc'+'ript src="file://c:/data_lt/websites/esqsoft.com/examples/search_lib.js"></sc'+'ript>'
  else html+='<sc'+'ript src="http://esqsoft.com/examples/search_lib.js"></sc'+'ript>'
  html += ''
  +'<sc'+'ript>'
  +'window.drawAttention = function(){'
  +'  if(window.ivnRotate) window.ivnRotate1 = new window.ivnRotate("ivnRotate1",0,document.ivnFormG.q)'
  +'};'
  +'setTimeout("window.drawAttention()",1000)'
  +'</sc'+'ript>'
  */

  +getTracking()

  setTimeout('if(window.shuttleCode){window.shuttleCode();}',1000);
  setTimeout('populateNavLinks()',1000);

  return html
}


window.shuttleCode = function(){
  // alternative method
  if(document && document.getElementById){
    sObj = document.getElementById("ivnS");
    tObj = document.getElementById("ivnT");
    if(sObj && sObj.innerHTML && tObj){
      tObj.innerHTML = ''
      +'<textarea wrap="soft" class="code-sample" ondblclick="this.select()" onclick="if(!this.init){this.init=1;this.select();}">'
      +sObj.innerHTML
      +'</textarea>'
    }
  }
}