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);}

		var objFormEls = objForm[iCounter].childNodes;

	}	

	// THE FOLLOWING LINES WERE REMOVED BECAUSE THEY ARE OVER_RIDING INLINE ONCHANGE EVENTS
	// IF WE NEED TO CHANGE THE BACKGROUND COLOUR LATER WE WILL HAVE TO DO IT IN A SMARTER WAY - davet 09/17/07
	/*
	var objFormSelects = document.getElementsByTagName('select');
		
		for(x=0;x<objFormSelects.length;x++){
			objFormSelects[x].onchange = function(){this.style.backgroundColor = "#ffffff";}
		}	
	
	var objFormInputs = document.getElementsByTagName('input');		
		for(x=0;x<objFormInputs.length;x++){
			objFormInputs[x].onkeypress = function(){this.style.backgroundColor = "#ffffff";}			
		}	
	*/

	}

}

function getStyle(el,styleProp)
{
	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 checkForm(objForm)
{
  var arClass, 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");
	
	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++){			
					if(labels[i].attributes["for"].value == 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++){			
					if(labels[i].attributes["for"].value == 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++){			
					if(labels[i].attributes["for"].value  == 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++){			
					if(labels[i].attributes["for"].value == 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++){			
					if(labels[i].attributes["for"].value == 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++){			
					if(labels[i].attributes["for"].value == objField[iFieldCounter].id){
						errorMsg = "The " + labels[i].firstChild.nodeValue + " that you entered doesn't appear to be valid. Please check your entry.";
					}			
				}
				 break;
	
			case 'iatan' :
			   bValid = noDashes(objField[iFieldCounter].value);
			   errorMsg = "The IATAN number that you entered doesn't appear to be valid. Please check your entry, and ensure the number is free of dashes.";
				 break;
				 
			case 'radio' :
				bValid = radioChosen(objField[iFieldCounter]);
				for(i=0;i<labels.length;i++){			
					if(labels[i].attributes["for"].value == objField[iFieldCounter].name){
						errorMsg = "Please ensure that you have made a choice for the " + labels[i].firstChild.nodeValue + " question.";
					}
				}
				 break;
				  
			case 'checkbox' :
				bValid = checkBoxChecked(objField[iFieldCounter].name);
				for(i=0;i<labels.length;i++){			
					if(labels[i].attributes["for"].value == 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].style.backgroundColor = "#FFCC00";
		        objField[iFieldCounter].focus();
		        return false;

		}

	}
		
		
    }
	//quick fix to remove highlighted yellow...should probably move to classes later.
	if (objField[iFieldCounter].style.backgroundColor.rgbToHex()=="#ffcc00")objField[iFieldCounter].style.backgroundColor="#fff"
  }
  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){
	
	var radios = document.getElementsByTagName("input");
	var checkThese = new Array;
	var x = 0;
	var n = 0;
	
	for(i=0;i<radios.length;i++){
		if(radios[i].name == str.name){checkThese[x] = radios[i];x++;}
	}
	
	for(i=0;i<checkThese.length;i++){
		if(checkThese[i].checked == false) {n++}
	}

	if(n != 1){return false;}

}

function checkBoxChecked(str){
	var box = document.getElementById(str);
	if(box.checked == false) {return false;}	
}

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