window.onload = attachFormHandlers;
document.createElement('header');
document.createElement('section');
document.createElement('nav');
document.createElement('article');
document.createElement('aside');
document.createElement('footer');
		 
function attachFormHandlers()
{
  // Ensure we're working with a 'relatively' standards 
  // compliant browser
     if (document.getElementsByTagName)
     {
     	var objForm = document.getElementsByTagName('form');     
	     for (var iCounter=0; iCounter<objForm.length; iCounter++)
     	objForm[iCounter].onsubmit = function(){return validateForm(this);}
     }
     deactivateIndividuals();
}

function validateForm(form)
{
	var valid = true;
	var message = "Error(s): ";
	
	// check against empty fields
	if ( isEmpty(form.firstname) ||
		isEmpty(form.lastname) ||
		isEmpty(form.address) ||
		isEmpty(form.city) ||
		isEmpty(form.state) ||
		isEmpty(form.zip) ||
		isEmpty(form.email))
	{
		message = message + "One or more required fields are empty. ";
		valid = false;
	}
	// check against improper zip or email
     /*if (!isValid(form.zip.value,"zip"))
     { 
          message = message + "Zip code must be five digits, or nine digits with a dash. ";
          valid = false;
     }*/
	if (!isValid(form.email.value,"email"))
     { 
          message = message + "Improperly formed email address. ";
          valid = false;
     }
	
	if (!valid) { alert(message); }
	return valid;
}

function isEmpty(elem)
{
	var str = elem.value;
	if (str.length == 0) { return true; }
	else { return false; }
}

function isValid(str, type) 
{
	switch(type)
	{
		case "zip": 
			var regex = new RegExp("[0-9]{5}(\-{1}[0-9]{4})?$"); break;
		case "email":
			var regex = new RegExp(".+@.+\..+"); break;
		default: 
			var regex = new RegExp(""); break;
	}
	return(regex.test(str));
}
	
function stopPropagation(e) {
  if (!e) e=window.event;
  if (typeof e.preventDefault!='undefined') e.preventDefault();
  if (typeof e.stopPropagation!='undefined') e.stopPropagation();
  return false;
}

function addEvent(o,e,f) {
  if (o.addEventListener) { o.addEventListener(e,f,false); return true; }
  else if (o.attachEvent) { return o.attachEvent('on'+e,f); }
  return false;
}

String.prototype.trim = function() {
  return this.replace('^\s+','').replace('\s+$','');
}

function deactivateIndividuals() { }

function showClassDetails(ClassID) {
	var URL = "/2010/schedule/details.php?ClassID=" + ClassID;
	window.open(URL,'classDetails','width=480,height=350,resizable=no,toolbar=no,scrollbars=yes,location=no,directories=no,menubar=no,status=no');	
}

function popupWindow(URL) {
	window.open(URL,'popupWindow','width=500,height=350,resizable=no,toolbar=yes,scrollbars=yes,location=no,directories=no,menubar=no,status=no');
}
