//error supression
//function stoperror(){ return true } window.onerror=stoperror;
/////////////////////////////////////////////////////////////////

// Begin clear-default-text.js

/*
 * Clear Default Text: functions for clearing and replacing default text in
 * <input> elements.
 *
 */

addEvent(window, 'load', init, false);

function trim (str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim (str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim (str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function isIE () {
	var agt = navigator.userAgent.toLowerCase();
	if (agt.indexOf("msie") != -1) return true;
	return false;
}

function initStoreLink () {
	//CANCEL IF NOT IE
	var agt = navigator.userAgent.toLowerCase();
	if (agt.indexOf("msie") == -1) return;
	
	var store_link = document.getElementById("store_link");
	
	//CANCEL IF LINK DOESN'T EXIST
	if (typeof store_link == "undefined" || !store_link) return; 
	
	//RESET THE HREF
	store_link.href = "http://enssc.com/default.aspx?store=532&cas=h";
	
	//SET THE ONCLICK HANDLER
	store_link.onclick = function () {		
		document.cookie = "store_click=y";
		return true;
	}

}

function initGlobalNav () {
	//MAKE STORE LINK DO SOME COOKIE STUFF
	initStoreLink();
	
	var form = document.getElementById("global_nav_search_form");
	
	//DO NOTHING IF NO FORM EXISTS
	if (typeof form == "undefined" || !form) return;
	
	//DON'T SUBMIT THE FORM UNLESS THERE'S A REAL VALUE  
	var element = form.q;
	var default_txt = trim(element.value);
	
	form.onsubmit = function () {
		var val = trim(element.value);
		element.value = val;
		
		if (val == "") return false;
		if (val == default_txt) return false;
		return true;
	}
}

function init() {
    initGlobalNav();
    
	//CLEAR DEFAULT TEXT
	var formInputs = document.getElementsByTagName('input');
	var i = formInputs.length;
	
	while (i--) {
		var element = formInputs[i];
		
        if (element.className.match(/\bcleardefault\b/)) {  
			element.defaulttxt = element.value;
			
			element.onfocus = function () {
				if (this.value == this.defaulttxt) {
					this.value = "";
				}
			}

			element.onblur = function () {
				if (this.value == "") {
					this.value = this.defaulttxt;
				}
			}
        }   
	}
}

function clearDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == target.defaultText) {
        target.value = '';
    }
}

function replaceDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == '' && target.defaultText) {
        target.value = target.defaultText;
    }
}

// End clear-default-text.js

// Begin rollover.js

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// End rollover.js

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

// Begin tabcontent.js

/***********************************************
* Tab Content script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
* Last updated: Nov 8th, 06
***********************************************/
var showtab = ''; // This variable will have a value set by default_header.php if the page is called with a GET variable t=n where n is the tab number to be opened. This overrides any cookies that were set to show the last active tab.

var enabletabpersistence=1 //enable tab persistence via session only cookies, so selected tab is remembered?

////NO NEED TO EDIT BELOW////////////////////////
var tabcontentIDs=new Object()

function expandcontent(linkobj){
var ulid=linkobj.parentNode.parentNode.id //id of UL element
var ullist=document.getElementById(ulid).getElementsByTagName("li") //get list of LIs corresponding to the tab contents

for (var i=0; i<ullist.length; i++){
ullist[i].className=""  //deselect all tabs
if (typeof tabcontentIDs[ulid][i]!="undefined") //if tab content within this array index exists (exception: More tabs than there are tab contents)
document.getElementById(tabcontentIDs[ulid][i]).style.display="none" //hide all tab contents
}
linkobj.parentNode.className="selected"  //highlight currently clicked on tab
document.getElementById(linkobj.getAttribute("rel")).style.display="block" //expand corresponding tab content
saveselectedtabcontentid(ulid, linkobj.getAttribute("rel"))
}

function expandtab(tabcontentid, tabnumber){ //interface for selecting a tab (plus expand corresponding content)
var thetab=document.getElementById(tabcontentid).getElementsByTagName("a")[tabnumber]
if (thetab.getAttribute("rel"))
expandcontent(thetab)
}

function savetabcontentids(ulid, relattribute){// save ids of tab content divs
if (typeof tabcontentIDs[ulid]=="undefined") //if this array doesn't exist yet
tabcontentIDs[ulid]=new Array()
tabcontentIDs[ulid][tabcontentIDs[ulid].length]=relattribute
}

function saveselectedtabcontentid(ulid, selectedtabid){ //set id of clicked on tab as selected tab id & enter into cookie
if (enabletabpersistence==1) //if persistence feature turned on
setCookie(ulid, selectedtabid)
}

function getullistlinkbyId(ulid, tabcontentid){ //returns a tab link based on the ID of the associated tab content
var ullist=document.getElementById(ulid).getElementsByTagName("li")
for (var i=0; i<ullist.length; i++){
if (ullist[i].getElementsByTagName("a")[0].getAttribute("rel")==tabcontentid){
return ullist[i].getElementsByTagName("a")[0]
break
}
}
}

function initializetabcontent(){
for (var i=0; i<arguments.length; i++){ //loop through passed UL ids
if (enabletabpersistence==0 && getCookie(arguments[i])!="") //clean up cookie if persist=off
setCookie(arguments[i], "")
if (showtab) {
	var clickedontab=showtab
}
else {
	var clickedontab=getCookie(arguments[i]) //retrieve ID of last clicked on tab from cookie, if any
}
var ulobj=document.getElementById(arguments[i])
var ulist=ulobj.getElementsByTagName("li") //array containing the LI elements within UL
for (var x=0; x<ulist.length; x++){ //loop through each LI element
var ulistlink=ulist[x].getElementsByTagName("a")[0]
if (ulistlink.getAttribute("rel")){
savetabcontentids(arguments[i], ulistlink.getAttribute("rel")) //save id of each tab content as loop runs
ulistlink.onclick=function(){
expandcontent(this)
return false
}
if (ulist[x].className=="selected" && clickedontab=="") //if a tab is set to be selected by default
expandcontent(ulistlink) //auto load currenly selected tab content
}
} //end inner for loop
if (clickedontab!=""){ //if a tab has been previously clicked on per the cookie value
var culistlink=getullistlinkbyId(arguments[i], clickedontab)
if (typeof culistlink!="undefined") //if match found between tabcontent id and rel attribute value
expandcontent(culistlink) //auto load currenly selected tab content
else //else if no match found between tabcontent id and rel attribute value (cookie mis-association)
expandcontent(ulist[0].getElementsByTagName("a")[0]) //just auto load first tab instead
}
} //end outer for loop
}


function getCookie(Name){ 
var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
if (document.cookie.match(re)) //if cookie found
return document.cookie.match(re)[0].split("=")[1] //return its value
return ""
}

function setCookie(name, value){
document.cookie = name+"="+value //cookie value is domain wide (path=/)
}

// End tabcontent.js

// Begin util-functions.js

/* 
 * Cross-browser event handling
 */
function addEvent(element, eventType, lamdaFunction, useCapture) {
    if (element.addEventListener) {
        element.addEventListener(eventType, lamdaFunction, useCapture);
        return true;
    } else if (element.attachEvent) {
        var r = element.attachEvent('on' + eventType, lamdaFunction);
        return r;
    } else {
        return false;
    }
}

/* 
 * Kills an event's propagation and default action
 */
function knackerEvent(eventObject) {
    if (eventObject && eventObject.stopPropagation) {
        eventObject.stopPropagation();
    }
    if (window.event && window.event.cancelBubble ) {
        window.event.cancelBubble = true;
    }
    
    if (eventObject && eventObject.preventDefault) {
        eventObject.preventDefault();
    }
    if (window.event) {
        window.event.returnValue = false;
    }
}

/* 
 * Safari doesn't support canceling events in the standard way, so we must
 * hard-code a return of false for it to work.
 */
function cancelEventSafari() {
    return false;        
}

/* 
 * Cross-browser style extraction, from the JavaScript & DHTML Cookbook
 * <http://www.oreillynet.com/pub/a/javascript/excerpt/JSDHTMLCkbk_chap5/index5.html>
 */
function getElementStyle(elementID, CssStyleProperty) {
    var element = document.getElementById(elementID);
    if (element.currentStyle) {
        return element.currentStyle[toCamelCase(CssStyleProperty)];
    } else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(element, '');
        return compStyle.getPropertyValue(CssStyleProperty);
    } else {
        return '';
    }
}

/* 
 * CamelCases CSS property names. Useful in conjunction with 'getElementStyle()'
 * From <http://dhtmlkitchen.com/learn/js/setstyle/index4.jsp>
 */
function toCamelCase(CssProperty) {
    var stringArray = CssProperty.toLowerCase().split('-');
    if (stringArray.length == 1) {
        return stringArray[0];
    }
    var ret = (CssProperty.indexOf("-") == 0)
              ? stringArray[0].charAt(0).toUpperCase() + stringArray[0].substring(1)
              : stringArray[0];
    for (var i = 1; i < stringArray.length; i++) {
        var s = stringArray[i];
        ret += s.charAt(0).toUpperCase() + s.substring(1);
    }
    return ret;
}

/*
 * Disables all 'test' links, that point to the href '#', by Ross Shannon
 */
function disableTestLinks() {
  var pageLinks = document.getElementsByTagName('a');
  for (var i=0; i<pageLinks.length; i++) {
    if (pageLinks[i].href.match(/[^#]#$/)) {
      addEvent(pageLinks[i], 'click', knackerEvent, false);
    }
  }
}


function newWin(name,width,height) {

	var w = width;
	var h = height;
	var X = (screen.availWidth - w) / 2;
	var Y = (screen.availHeight - h) / 2;

	var popUp = window.open("",name,"menubar=no, status=no, location=no, toolbar=no, scrollbars=no, resizable=yes,width="+w+",height="+h+",left="+X+",top="+Y+"");
	
}

function togglePopUpLayer(div_id) {

	// Call this function to hide or block a div. For legacy reasons, assume the div id is "popup" if none is passed.
	
	if (! div_id) {
		div_id = 'popup';
	}
	
	if (document.getElementById(div_id).className == "hidden") {
	
		document.getElementById(div_id).className = div_id;
		
	} else {
	
		document.getElementById(div_id).className = "hidden";
		
	}
	
}

function hide(the_div) {

	var the_style = getStyleObject(the_div);

	the_style.display = 'none';
	
}

function show(the_div) {

	var the_style = getStyleObject(the_div);

	the_style.display = 'block';

}

function toggle(the_div) {

	var the_style = getStyleObject(the_div);

	if (the_style.display == 'none') {
	
		the_style.display = 'block';
		
	}
	else {
	
		the_style.display = 'none';
		
	}
	
}

function getStyleObject(objectId) {

	if (document.getElementById && document.getElementById(objectId)) {
		return document.getElementById(objectId).style;
	}
	else {
		if (document.all && document.all(objectId)) {
			return document.all(objectId).style;
		}
		else {
			return false;
		}
	}
}

function open_block(block) {

  // First hide the other blocks
  
  for (var i=1; i<11; i++) {
 
 	var div = 'panel_link_' + i;
 	
 	if (i != block) {
 		hide(div);
 	}
 	
  }
  
  // Now open our block
  
  for (var i=1; i<11; i++) {
 
 	var div = 'panel_link_' + i;
 	
 	if (i == block) {
 		show(div);
 	}
 	
  }
  
}

// For opening new windows that are 80% of the height of the parent window. This is for links to pages on the old site

function smallerWin(name) {

	var w = 980;
	var l = (screen.availWidth - w ) / 2;
	var h = 580 ;	
	var t = (screen.availHeight - h ) / 2;
	
	var popUp = window.open("",name,"menubar=yes, status=yes, location=yes, toolbar=yes, scrollbars=yes, resizable=yes, width=" + w +", height=" + h +", left=" + l + ", top=" + t);
	
	popUp.focus();
	
}


/////ty's additions

function findIt(theObj, theDoc)

{

  var p, i, foundObj;

  

  if(!theDoc) theDoc = document;

  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)

  {

    theDoc = parent.frames[theObj.substring(p+1)].document;

    theObj = theObj.substring(0,p);

  }

  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];

  for (i=0; !foundObj && i < theDoc.forms.length; i++) 

    foundObj = theDoc.forms[i][theObj];

  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 

    foundObj = findObj(theObj,theDoc.layers[i].document);

  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);

  

  return foundObj;

}

function theAddresses(addresses){
	theserver = "calacademy";
	theextension = "org";
	for (i=1; i<=addresses; i++){
		objectVar = "address-+-" + i;
		theObject = findIt(objectVar);
		theOldHtml = theObject.innerHTML;
		theNewHtml = "<a href='mailto:" + theOldHtml + "@" + theserver + "." + theextension + "'>" + theOldHtml + "@" + theserver + "." + theextension + "</a>";
		theObject.innerHTML = theNewHtml;
	}
}

function thephone(phone){
	theCode = "415";
	//theextension = "org";
	for (i=1; i<=phone; i++){
		objectVar = "phone-+-" + i;
		theObject = findIt(objectVar);
		theOldHtml = theObject.innerHTML;
		theNewHtml = "(" + theCode + ") " + theOldHtml;
		theObject.innerHTML = theNewHtml;
	}
}

function populateTheContactInfo(addr, ph)
{
	theAddresses(addr);
	thephone(ph);
}	


/***********************************************
* Fixed ToolTip script- © Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
		
var tipwidth='150px' //default tooltip width
var tipbgcolor='#005"'  //tooltip bgcolor
var disappeardelay=250  //tooltip disappear speed onMouseout (in miliseconds)
var vertical_offset="7px" //horizontal offset of tooltip from anchor link
var horizontal_offset="-3px" //horizontal offset of tooltip from anchor link

/////No further editting needed

var ie4=document.all
var ns6=document.getElementById&&!document.all

if (ie4||ns6)
document.write('<div id="fixedtipdiv" style="visibility:hidden;width:'+tipwidth+';background-color:'+tipbgcolor+'" ></div>')

function getposOffset(what, offsettype){
var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
var parentEl=what.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}


function showhide(obj, e, visible, hidden, tipwidth){
if (ie4||ns6)
dropmenuobj.style.left=dropmenuobj.style.top=-500
if (tipwidth!=""){
dropmenuobj.widthobj=dropmenuobj.style
dropmenuobj.widthobj.width=tipwidth
}
if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover")
obj.visibility=visible
else if (e.type=="click")
obj.visibility=hidden
}

function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function clearbrowseredge(obj, whichedge){
var edgeoffset=(whichedge=="rightedge")? parseInt(horizontal_offset)*-1 : parseInt(vertical_offset)*-1
if (whichedge=="rightedge"){
var windowedge=ie4 && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15
dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure-obj.offsetWidth
}
else{
var windowedge=ie4 && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure+obj.offsetHeight
}
return edgeoffset
}

function fixedtooltip(menucontents, obj, e, tipwidth){
if (window.event) event.cancelBubble=true
else if (e.stopPropagation) e.stopPropagation()
clearhidetip()
dropmenuobj=document.getElementById? document.getElementById("fixedtipdiv") : fixedtipdiv
dropmenuobj.innerHTML=menucontents

if (ie4||ns6){
showhide(dropmenuobj.style, e, "visible", "hidden", tipwidth)
dropmenuobj.x=getposOffset(obj, "left")
dropmenuobj.y=getposOffset(obj, "top")
dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+"px"
dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px"
}
}

function hidetip(e){
if (typeof dropmenuobj!="undefined"){
if (ie4||ns6)
dropmenuobj.style.visibility="hidden"
}
}

function delayhidetip(){
if (ie4||ns6)
delayhide=setTimeout("hidetip()",disappeardelay)
}

function clearhidetip(){
if (typeof delayhide!="undefined")
clearTimeout(delayhide)
}


/////////////////////////
/*
Foil email scrapers
Greg Rotter, 7/17/08
*/
/////////////////////////

function writeEmail (user, site, title) {
	if (site == undefined || site == '' || site == 'undefined') site = "calacademy.org";
	var email = user + '@' + site;
	
	if (title == undefined || title == '' || title == 'undefined') title = email;
	document.write('<a href=\"mailto:' + email + '\">' + title + '</a>');
}
