/*
addLoadEvent allows you to load multiple functions at window.load
*/
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

/*
prepare popups assigns all "a" tags with a class of popup an onclick handler
*/
function preparePopups() {
  if (!document.getElementsByTagName) return false;
  var lnks = document.getElementsByTagName("a");
  for (var i=0; i<lnks.length; i++) {
	if(lnks[i].className.indexOf("popup") != -1){
      lnks[i].onclick = function() {
        popUp(this.getAttribute("href"));
        return false;
      }
    }
  }
}
/*
popUp opens a new window durr
*/
function popUp(winURL) {
  window.open(winURL);
}

/*
focusLabels ensures that all browsers properly handle the label tag
*/
function focusLabels()
{
	if(!document.getElementsByTagName) return false;
	var labels = document.getElementsByTagName("label");
	for(var i=0; i < labels.length; i++)
	{
		if(!labels[i].getAttribute("for")) continue;
		labels[i].onclick = function()
		{
			var id = this.getAttribute("for");
			if(!document.getElementById(id)) return false;
			var element = document.getElementById(id);
			element.focus();
		}
	}
}

/*
finds all forms and assigns an onsubmit event handler
onsubmit runs validateForm()
*/
function prepareForms()
{
	for(var i=0; i<document.forms.length; i++)
	{
		var thisform = document.forms[i];
		thisform.onsubmit = function()
		{
			return validateForm(this);
		}
	}
}

/*
checks to make sure that a feild has a value
*/
function isFilled(field)
{
	if(field.value.length < 1)
	{
		return false;
	}
	else
	{
		return true;
	}
}

/*
checks to make sure that a feild has a valid email address
*/
function isEmail(field)
{
	if(field.value.indexOf("@") == -1 || field.value.indexOf(".") == -1)
	{
		return false;
	}
	else
	{
		return true;
	}
}

/*
validate forms
all fields that have a class of required are checked by isFilled()
all fields with a class of email are validated against isEmail()
*/
function validateForm(whichform)
{
	for(var i=0; i<whichform.elements.length; i++)
	{
		var element = whichform.elements[i];
		if(element.className.indexOf("required") != -1)
		{
			if(!isFilled(element))
			{
				alert("Please fill in the "+element.id+" field.");
				return false;
			}
		}
		if(element.className.indexOf("email") != -1)
		{
			if(!isEmail(element))
			{
				alert("The "+element.name+" field must be a valid email address");
				return false;
			}
		}
	}
	return true;
}

//// stripe tables ////
function stripeTables() {
  if (!document.getElementsByTagName) return false;
  var tables = document.getElementsByTagName("table");
  for (var i=0; i<tables.length; i++) {
    var odd = false;
    var rows = tables[i].getElementsByTagName("tr");
    for (var j=0; j<rows.length; j++) {
      if (odd == true) {
        addClass(rows[j],"odd");
        odd = false;
      } else {
        addClass(rows[j],"even");
        odd = true;
      }
    }
  }
}
function addClass(element,value) {
  if (!element.className) {
    element.className = value;
  } else {
    newClassName = element.className;
    newClassName+= " ";
    newClassName+= value;
    element.className = newClassName;
  }
}

//// highlight row on hover ////
function highlightRows() {
	if(!document.getElementsByTagName) return false;
	var rows = document.getElementsByTagName("tr");
	for (var i=0; i<rows.length; i++)
	{
		// get current style
		var bgcolor = rows[i].style.backgroundColor;
		rows[i].onmouseover = function()
		{
			this.style.backgroundColor = "#E7F2FE";
		}
		rows[i].onmouseout = function()
		{
			this.style.backgroundColor = bgcolor;
		}
	}
}

/*
initilize behaviors
*/
addLoadEvent(preparePopups);
addLoadEvent(focusLabels);
addLoadEvent(prepareForms);
addLoadEvent(highlightRows);
addLoadEvent(stripeTables);