function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

// updated to work with safari
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		while (true) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
			if(!obj.offsetParent)
			  break;
			obj = obj.offsetParent;
		}
	} 
	
	return [curleft,curtop];
}

function getElementsByClassName(theClass,d,c) {
  if(!document.hasChildNodes) return false;
   
  if(!c) var c = new Array();
  if(!d) var d = document.body;
  
  if(d.nodeType == 1 && d.className.match("^" + theClass + "$")) {
    c.push(d);
  }
  
  if(d.hasChildNodes()) {
    
    for(var i = 0; i < d.childNodes.length; i++) {
      getElementsByClassName(theClass,d.childNodes[i],c);
    }
    
    return c;
    
  } else {
    
    return c;
    
  }
}

function newElement(elementType,attributes) {
  
  var d = document;
  if(!d.createElement) return false;
  
  /* if we're trying to make a radio button */
  if(elementType == "input") {
    
    /* Try the IE way first */
    var inputStr = "<input";
    
    for(var j in attributes) {
      inputStr += " " + j + "=\"" + attributes[j] + "\"";
    }
    
    inputStr += " />";
    
    try {
      newEl = d.createElement(inputStr);
    } catch(e) {
      // do nothing
    }
    
  }
  
  if(elementType == "button") {
    
    var buttStr = "<button ";
    
    for(var j in attributes) {
      
      switch(j) {
        case "value":
          // do nothing
          break;
          
        case "className":
          buttStr += " class=\"" + attributes[j] + "\"";
          break;
          
        case "text":
          // do nothing
          break;
          
        default:
          buttStr += " " + j + "=\"" + attributes[j] + "\"";
      }
      
    }
    
    buttStr += " ></button>";
    
    try {
      newEl = d.createElement(buttStr);
      
      if(typeof(attributes["text"]) != "undefined" && attributes["text"] != null) {
        newEl.appendChild(document.createTextNode(attributes["text"]));
      } else {
        newEl.appendChild(document.createTextNode(""));
      }
    } catch(e) {
      
    }
    
  }
  
  // if newEl hasn't already been created      
  if(typeof(newEl) == "undefined") {
    
    var newEl = d.createElement(elementType);
    
    for(var j in attributes) {

      if(j == "text") {

        var text = d.createTextNode(attributes[j]);
        newEl.appendChild(text);

      } else if (j == "className") {

        newEl.setAttribute(j,attributes[j]);
        newEl.setAttribute(j.substr(0,5),attributes[j]);

      } else {

        newEl.setAttribute(j,attributes[j]);

      }
    }
  }

  return newEl;

}

function setClass(item,classText) {
  item.setAttribute("className",classText);
  item.setAttribute("class",classText);
}

function setStyles(element,styleObj) {
  
  for(var j in styleObj) {
    
    if(j == "float") {
      if(document.all) {
        element.style.styleFloat = styleObj[j];
      } else {
        element.style.cssFloat = styleObj[j];
      }
    } else {
      element.style[j] = styleObj[j];
    }
  }
  
}

String.prototype.stripWhite = function() {
  
  // strip leading whitespace
  tmp_str = this.replace(/^\s+/m,"");
  
  // strip trailing whitespace
  tmp_str = tmp_str.replace(/\s+$/m,"");
  
  return tmp_str;
}

// this function gives the "indexOf" functionality to the Apple Safari Browser
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

function exists(node) {
  if(typeof(node) != "undefined" && node != null) {
    return true;
  } else {
    return false;
  }
}

function callback(obj,method) {
  return function() {
    method.apply(obj,arguments);
  }
}

function findParent(node,tag) {
  
  while(node.nodeName.toLowerCase() != tag) {
    node = node.parentNode;
  }
  
  return node;
  
}

function getViewportSize()
{
 var size = [0, 0];

 if (typeof window.innerWidth != 'undefined')
 {
   size = [
       window.innerWidth,
       window.innerHeight
   ];
 }
 else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
 {
   size = [
       document.documentElement.clientWidth,
       document.documentElement.clientHeight
   ];
 }
 else
 {
   size = [
       document.getElementsByTagName('body')[0].clientWidth,
       document.getElementsByTagName('body')[0].clientHeight
   ];
 }

 return {w:size[0],h:size[1]};
}

function getDocumentSize() {
  
  var size = [];
  
  if(document.documentElement && document.documentElement.scrollWidth) {
    
    size = [document.documentElement.scrollWidth,
            document.documentElement.scrollHeight];
    
  } else if(document.body.scrollWidth) {
    
    size = [document.body.scrollWidth,
            document.body.scrollHeight];
    
  }
  
  return {w:size[0],h:size[1]};
  
}

function getScrollXY() {
  
  var x = window.pageXOffset || 
          document.body.scrollLeft || 
          document.documentElement.scrollLeft;
          
  var y = window.pageYOffset || 
          document.body.scrollTop || 
          document.documentElement.scrollTop;
          
  return {x:x ? x : 0,y:y ? y : 0};
  
}

startList = function() {
if (document.all&&document.getElementById) {
navRoot = document.getElementById("header_tabs");
for (i=0; i<navRoot.childNodes.length; i++) {
node = navRoot.childNodes[i];
if (node.nodeName=="LI") {
node.onmouseover=function() {
this.className+=" over";
  }
  node.onmouseout=function() {
  this.className=this.className.replace(" over", "");
   }
   }
  }
 }
}

function getTargetElement(e) {
  if(!e) var e = window.event;
  if(e.target) targ = e.target;
  else if(e.srcElement) targ = e.srcElement;
  if(targ.nodeType == 3) // defeat safari bug
    targ = targ.parentNode;
    
  return targ;
}

function insertAfter(theParent,newNode,targetNode) {
  
  if(exists(targetNode.nextSibling)) {
    theParent.insertBefore(newNode,targetNode.nextSibling);
  } else {
    theParent.appendChild(newNode);
  }
  
}

function removeChildren(node) {
  
  while(node.hasChildNodes()) {
    
    node.removeChild(node.lastChild);
    
  }
  
}

function changeOpac(opac,node) {
  
  var obj = node.style;
  obj.opacity = (opac / 100);
  obj.mozOpacity = (opac / 100);
  obj.KhtmlOpacity = (opac / 100);
  obj.filter = "alpha(opacity=" + opac + ")";
}

function mouseXY(e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	
	return {x:posx,y:posy};

}

function selectSemester(selectDropDown) {
  
  window.location = "http://www.mycesar.ca/courseevaluations/default/default/" + selectDropDown.value + "/";
  
}

