/* CCSTAL - Client-Side Cachable Template */

/* 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.

/* make an effort to obfuscate contact info from scrapers...think of all the poor email addresses that have been lost to these nefarious folks :-( */
var EMa = 'mai'+'lto'+':'; var EMb = 'rwe'+'bmin'+'@'; 
var EMc = 'gma'+'il.co'+'m'; var EMd = '?subj'+'ect='+location.href;

/* don't want to call ads on functionality from others */
var isSearchResults = location.pathname.match(/search-results/); 

/* 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 as well as 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 ("skin" meaning "look and feel" of the web pages) 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 hContact(obj){
  var doStuff = EMa+EMb+EMc+EMd;
  var html = ''
  +'<h2>Contact the Author:</h2>'
  +'<p><b>Just a quick note:</b>'
  +'<p>If you are contacting me to report a problem you are experiencing, please include this info:'
  +'<ol>'
  +'<li>a quick description of the trouble.'
  +'<li>the url/address of the web page or tool.'
  +'<li>the browser and OS you are using so I can debug the problem.'
  +'</ol>'
  +'<br><br>You can cut/paste the url and your browser info from below if it\'s convenient: (these are from javascript)'
  +'<p><hr>'
  +'<p><b>URL = '+location.href+'</b>'
  +'<p><b>UserAgent = '+navigator.userAgent+'</b>'
  +'<p><hr>'
  +'<p><a href="'+doStuff+'">Click here</a> to attempt to launch your email program (if this doesn\'t work, you\'ll need to do it manually).'
  var e = 'contactAuthor';
  var obj = document.getElementById(e);
  if(!obj){
    var el = document.createElement('div');
    el.id = e;
    el.className = 'contactMe';
    el.ondblclick = function(){ this.style.display = 'none'; }
    el.innerHTML = '<div style="float:right;" style="cursor:pointer;" onclick="var obj=document.getElementById(\''+e+'\'); if(obj){ obj.style.display=\'none\'; }; return false;"><b>[close]</b></div>'
      +html
    document.body.appendChild(el);
  }
  else{ 
    obj.style.display = '';
  }
}

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'],
      ['javascript:void(hContact(this));','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>'
    +'</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>'

  return html
}



window.domains_sale = function(){
  var html = ''
  +'<a href="/redirect.html?id=DEALSONDOMAINS&redir='+encodeURIComponent('http://www.securepaynet.net/domains/search.aspx?ci=1774&prog_id=hosting411')+'" target="_blank" '
  +'style="text-decoration:none; background-color:red; color:white; font-weight:bold; padding:5px 20px; margin:5px;">'
  +'<span style="border-bottom:1px white dotted;">LATEST DOMAIN NAME PRICING</span></a>'
  
  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:45px; 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:#DDDDFF;"></td></tr>'
  +'</table>'
  +'<table width="100%" cellspacing="20" cellpadding="10" class="main">'
  +'  <tr valign="top">'

  /* column */
  html+=''
  +'    <td width="160" align="center">'
  if(location.host && !isSearchResults){
    if(window.X160x600){
        html += '<iframe id="ab8ac9ef" name="ab8ac9ef" src="'+window.X160x600+'" 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>'
    }
    else{
      html += '<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>'
    }
  }
  html += ''
  +'      <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</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 && location.host && !isSearchResults){
    html += ''
    +'<table cellspacing="0" cellpadding="0" align="right"><tr><td style="padding:70px 0px 20px 20px;">'
    //+'<iframe src="'+mpath+'ads/2010/300x250-esqsoft-howto.htm" '
    //  +' width="300" height="250" '
    //  +' framespacing="0" frameborder="no" scrolling="no" allowtransparency="true"></iframe>'
    +'<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 && location.host && !isSearchResults){
    html += ''
    +'<div style="padding: 20px 0 0 0;">'
    // +'<iframe src="'+mpath+'ads/2010/728x90-esqsoft-howto.htm" '
    // +' width="728" height="90" '
    // +' framespacing="0" frameborder="no" scrolling="no" allowtransparency="true"></iframe>'
    +'<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>'

  +getTracking()

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

  return html
}


window.shuttleCode = function(){
  /* a means of showing "code" sections without having the browser interpret/run the code -- this is for places we want to be able to show the example code */ 
  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>'
    }
  }
}

