function attachFormHandlers()
{
//alert("setup");
  // 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 checkForm(this);}
	}
  }
}


function getStyle(el,styleProp)
{
	if(navigator.userAgent.indexOf("Safari") != -1){
		// huge hack for safari, as there's no way to retrieve the style of
		// a non-getElementById object reference (boooo!), and since we need
		// to actually, you know, validate the form... we have to force a value. :-(
		var y = "safari";		
	}else if (el.currentStyle){
		var y = el.currentStyle[styleProp];
	}else if (window.getComputedStyle){
		var y = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
	}
	return y;
}


// begin master validation


function addEvent(obj, evType, fn, useCapture){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, useCapture);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on" + evType, fn);
		return r;
	} else {
		window.status="Handler(s) could not be loaded";
	}
}

function checkForm(objForm)
{
  var arClass = new Array;
  var bValid, errorMsg;
  var objField = objForm.getElementsByTagName('*');
  var labels = objForm.getElementsByTagName('label');

  
  for (var iFieldCounter=0; iFieldCounter<objField.length; iFieldCounter++)
  {
    // Allow for multiple values being assigned to the class attribute

		var getAncestorStyle = function(node) {
		//walk up a node's ancestry to see if it's display prop
		//is 'inline' or 'block'

		//go back now if we're looking at the body node.
		if (node == document.body) return getStyle(node,'display');
		else {
		//check the node passed in first.
		var walker = node;
		do {
		//grab the style
		var myStyle = getStyle(walker,'display');
		//if this node's display: none, return it's style object
		if (myStyle == 'none') return myStyle;
		//otherwise get the node's parent
		walker = walker.parentNode;
		//and repeat until we hit the body element.
		} while (walker != document.body);
		//if we didn't find an invisible ancestor,
		//simply return the passed in node's style
		return getStyle(node,'display');
		}
		}


	var dProp = getAncestorStyle(objField[iFieldCounter]);
	var isShown = (dProp == "block" || dProp == "inline" || dProp == "safari");
	
	arClass = objField[iFieldCounter].className.split(' ');
	if(isShown == true){
	
		for (var iClassCounter=0; iClassCounter<arClass.length; iClassCounter++)
	    {

	      switch (arClass[iClassCounter])
	      {

	        case "string":
	           bValid = isString(objField[iFieldCounter].value.replace(/^\s*|\s*$/g, ''));
				// set error msg string
				for(i=0;i<labels.length;i++){			
					var forAttr = labels[i].getAttribute("for") ? labels[i].getAttribute("for") : labels[i].attributes["for"].value;
					if(forAttr == objField[iFieldCounter].id){
						errorMsg = "Please review the entry you provided for " + labels[i].firstChild.nodeValue + ".  This value is required.";
					}
				}
	           break;
			   
	        case "empty":
	           bValid = isNotEmpty(objField[iFieldCounter].value.replace(/^\s*|\s*$/g, ''));
				// set error msg string
				for(i=0;i<labels.length;i++){			
					var forAttr = labels[i].getAttribute("for") ? labels[i].getAttribute("for") : labels[i].attributes["for"].value;
					if(forAttr == objField[iFieldCounter].id){
						errorMsg = "Please review the entry that you provided for " + labels[i].firstChild.nodeValue + ".  This value is required.";
					}
				}
	           break;
	
	        case "number" :
	           bValid = isNumber(objField[iFieldCounter].value);
				// set error msg string
				for(i=0;i<labels.length;i++){			
					var forAttr = labels[i].getAttribute("for") ? labels[i].getAttribute("for") : labels[i].attributes["for"].value;
					if(forAttr == objField[iFieldCounter].id){
						errorMsg = labels[i].firstChild.nodeValue + " requires that you enter a number. Please check your entry.";
					}			
				}
	           break;
			   
	        case "email" :
	           bValid = isEmail(objField[iFieldCounter].value);
			   errorMsg = "Your email address doesn't appear to be valid. Please check your entry. This value is required.";
	             break;
				 
	        case "select" :
	           bValid = validateSelect(document.getElementById(objField[iFieldCounter].id)[document.getElementById(objField[iFieldCounter].id).selectedIndex].text);
				for(i=0;i<labels.length;i++){			
					var forAttr = labels[i].getAttribute("for") ? labels[i].getAttribute("for") : labels[i].attributes["for"].value;
					if(forAttr == objField[iFieldCounter].id){
						errorMsg = "Please ensure that you have selected an item from the " + labels[i].firstChild.nodeValue + " dropdown.";
					}			
				}
	           break;
			   
			case "password" :
			   bValid = comparePW();
			   errorMsg = "Please ensure that you have entered the same password in both fields.";
				 break;
	
			case "phone" :
			   bValid = isPhone(objField[iFieldCounter].value);
				// set error msg string
				for(i=0;i<labels.length;i++){			
					var forAttr = labels[i].getAttribute("for") ? labels[i].getAttribute("for") : labels[i].attributes["for"].value;
					if(forAttr == objField[iFieldCounter].id){
						errorMsg = "The " + labels[i].firstChild.nodeValue + " that you entered doesn't appear to be valid.  Please check your entry.";
					}			
				}
				 break;
	
			case "zip" :
			   bValid = isZip(objField[iFieldCounter].value);
				// set error msg string
				for(i=0;i<labels.length;i++){			
					var forAttr = labels[i].getAttribute("for") ? labels[i].getAttribute("for") : labels[i].attributes["for"].value;
					if(forAttr == objField[iFieldCounter].id){
						errorMsg = "The " + labels[i].firstChild.nodeValue + " that you entered doesn't appear to be valid. Please check your entry.";
					}			
				}
				 break;
	
			case "radio" :
				bValid = radioChosen(objField[iFieldCounter].name);
				for(i=0;i<labels.length;i++){			
					var forAttr = labels[i].getAttribute("for") ? labels[i].getAttribute("for") : labels[i].attributes["for"].value;
					if(forAttr == objField[iFieldCounter].id){
						errorMsg = "Please ensure that you have made a choice for the " + labels[i].firstChild.nodeValue + " question.";
					}
				}
				break;
	        default:
	           bValid = true;
	      }
	
	      if (bValid == false)
	      {
	        // If this field is invalid, leave the testing early,
	        // and alert the visitor to this error
	
	
		        alert(errorMsg);
		        //objField[iFieldCounter].select();
		        objField[iFieldCounter].focus();
		        return false;

		}

	}
		
		
    }
  }
  return true;
}

// end master validation


function isString(strValue)
{
  return (typeof strValue == 'string' && strValue != '' && isNaN(strValue));
}

function isNumber(strValue)
{
  return (!isNaN(strValue) && strValue != '');
}

function isEmail(strValue)
{
  var objRE = /^[\w-\.\']{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,}$/;

  return (strValue != '' && objRE.test(strValue));
}

function comparePW(){
	var pw1 = document.getElementById("password");
	var pw2 = document.getElementById("confPassword");
	if (pw1.value != pw2.value){return false}else{return true}
}

function validateSelect(strValue){
	if(strValue == "Please Select" || strValue == "Select State" || strValue == "Month" || strValue == "Day" || strValue == "Year" || strValue == "" || strValue == " ") {return false}else{return true}
}

function isPhone(strValue){
	var stripped = strValue.replace(/[\(\)\.\-\ ]/g, '');
	if (isNaN(parseInt(stripped))) {
	   return false;
	}

	if (!(stripped.length == 10)) {
	   return false;
	}
}

function trimString (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

String.prototype.trim = trimString;

function isZip(strValue){
try{var country = document.getElementById("country")[document.getElementById("country").selectedIndex].value;}

catch(er) {if(country == null) {country = document.getElementById("country").value;}}	

	if (strValue == ''){return false};	
	
	if(country == "US" || country == "AU" || country == "CA") {
		isString(strValue);
		
		if(country == "CA"){
			var objRegExp  = /^\s*[a-ceghj-npr-tvxy]\d[a-z](\s)?\d[a-z]\d\s*$/i;
		}
		
		if(country == "US" || country == " ") {
			var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)|(^\d{5}\s\d{4}$)/; 
		}

		strValue = strValue.trim();
					
		var zip = objRegExp.test(strValue);
		return zip;


	}	
	
}

function noDashes(strValue){
	var illegalChars = /\W/;
	if (illegalChars.test(strValue) == true) {return false;}
}

function radioChosen(str){
	//alert(str.length);
	//alert(str.checked);
	for(i=0;i<str.length;i++){
		if(str[i].checked == false) {return false;}
	}
}

function isNotEmpty(strValue){
	if(strValue == "" || strValue == null){return false;}
}
