// SOM JavaScript Collection

// Style Switcher

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && 
!a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function reader(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);


// -------------------------------------


// No Style Switcher

if (document.getElementsByTagName)
	document.getElementsByTagName('link')[0].disabled = true;

function changeCSS(nr)
{
	if (document.getElementsByTagName)
		x = document.getElementsByTagName('link');
	else if (document.all)
		x = document.all.tags('link');
	else
	{
		alert('This script does not work in your browser');
		return;
	}
	nr--;
	for (var i=0;i<x.length;i++)
	{
		dis = !(i == nr);
		x[i].disabled = dis;
	}
}


// -------------------------------------


// toggle visibility

function toggle (targetId) {
  if (document.getElementById) {
  	target = document.getElementById (targetId);
		if (target.style.display == "none") {
			target.style.display = "";
		} else {
			target.style.display = "none";
		}
	}
}


// -------------------------------------


// New Window

function newWindow() {
  for (i = 0; i < document.links.length; i++) {
    if (document.links[i].getAttribute('class') == 'newWindow'
    || document.links[i].className == 'newWindow') {
       document.links[i].setAttribute('target', '_blank')
    }
  }
}

// -------------------------------------
	

// Admission Event/Registration PopUp Window

function AdmReg(url) {
    window.open(url,'AdmReg','resizable,toolbar=no,location=no,scrollbars=yes,status=yes,width=550,height=600');
    }

// -------------------------------------

	
// Close Window

function closeWindow() {
  for (i = 0; i < document.links.length; i++) {
    if (document.links[i].getAttribute('class') == 'closeWindow'
    || document.links[i].className == 'closewindow') {
       document.links[i].setAttribute('title', 'this link will close this window')
       document.links[i].setAttribute('href', 'javascript:self.close();');
    }
  }
}


// -------------------------------------


// Link to Parent Window & Close PopUp

function specialPurpose(newURL) {
		opener.document.location = newURL
		window.close();
	}


// -------------------------------------


// Open PopUp
// Snips borrowed from Simon Willison and Christian Heilmann

function openPop() {
  for (i = 0; i < document.links.length; i++) {
    if (document.links[i].getAttribute('class') == 'popUp'
    || document.links[i].className == 'popUp') {
       document.links[i].setAttribute('target', '_pop')
    }
  }
	if(!window.opener) {
		var as,i,popfun
		as=document.getElementsByTagName('a');
		for (i=0;i<as.length;i++) {
			if(as[i].target)
			{
				popfun=function(){window.open(this.href,'',windowAttributes);return false;};
				as[i].onclick=popfun;
				as[i].onkeypress=popfun;
			}
		}
	}
}



// -------------------------------------
// Google Search Results

 function resetForms() {
          for (var i = 0; i < document.forms.length; i++ ) { 
              document.forms[i].reset();
          }
        }



// -------------------------------------


// Zebra Tables tweaked from original source by A List Apart

  // this function is need to work around 
  // a bug in IE related to element attributes
  function hasClass(obj) {
     var result = false;
     if (obj.getAttributeNode("class") != null) {
         result = obj.getAttributeNode("class").value;
     }
     return result;
  }   



 function zebraTable() 
 {
	 if (hasClass = "zebraTable") 
	 {
	
		// the flag we'll use to keep track of 
		// whether the current row is odd or even
		var even = false;
	  
		// if arguments are provided to specify the colours
		// of the even & odd rows, then use the them;
		// otherwise use the following defaults:
		var evenColor = arguments[1] ? arguments[1] : "#f4eddc";
		var oddColor = arguments[2] ? arguments[2] : "#feffef";
	  
		// by definition, tables can have more than one tbody
		// element, so we'll have to get the list of child
		// &lt;tbody&gt;s 
		var tbodies = document.getElementsByTagName("tbody");
	
		// and iterate through them...
		for (var h = 0; h < tbodies.length; h++) 
		{
		
		 // find all the &lt;tr&gt; elements... 
		  var trs = tbodies[h].getElementsByTagName("tr");
		  
		  // ... and iterate through them
		  for (var i = 0; i < trs.length; i++) 
		  {
	 
			 // get all the cells in this row...
			  var tds = trs[i].getElementsByTagName("td");
			
			  // and iterate through them...
			  for (var j = 0; j < tds.length; j++) 
			  {
				var mytd = tds[j];
				  mytd.style.backgroundColor = even ? evenColor : oddColor;
			  }
			
			// flip from odd to even, or vice-versa
			even =  ! even;
		  }
		}
	  }
}


// -------------------------------------


window.onload = function()
{
  closeWindow();
  openPop();
  newWindow();
  resetForms();
  zebraTable();
}