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;

	}
	
	

	}

  }

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;

			case 'notZero' :
				bValid = notZero(objField[iFieldCounter].value);
				for(i=0;i<labels.length;i++){			
					if(labels[i].attributes["for"].value == objField[iFieldCounter].id){
						errorMsg = labels[i].firstChild.nodeValue + " needs to be greater than zero.  Please check your entry.";
					}
				}
				 break;

			case 'pastDate' : // customized for group inquiries form
				bValid = pastDate();
				for(i=0;i<labels.length;i++){			
					if(labels[i].attributes["for"].value == objField[iFieldCounter].id){
						errorMsg = "Please ensure that your Preferred Dates are not in the past.";
					}
				}
				 break;

			case 'validDate' :  // customized for group inquiries form
				bValid = validDate();
				for(i=0;i<labels.length;i++){			
					if(labels[i].attributes["for"].value == objField[iFieldCounter].id){
						errorMsg = " Please ensure that your Preferred start date is before your end date.";
					}
				}
				 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 = "#ffffff";
		        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 == "State" || strValue == "Please Select" || strValue == "Select State" || (strValue == "Month" || strValue=="mm") || (strValue == "Day" || strValue=="dd") || (strValue == "Year" || strValue=="yyyy") || strValue == "" || strValue == " " || strValue=="How did you hear about us?") {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;}
}

function notZero(strValue){
	if(strValue == 0 || strValue == "0"){return false;}
}

function pastDate(){
	var mo = document.getElementById("preferredFromMonth")[document.getElementById("preferredFromMonth").selectedIndex].value-1;
	var da = document.getElementById("preferredFromDay")[document.getElementById("preferredFromDay").selectedIndex].value;
	var yr = document.getElementById("preferredFromYear")[document.getElementById("preferredFromYear").selectedIndex].value;

	var d = new Date();
	var cMo = d.getMonth();
	var cDa = d.getDate();
	var cYr = d.getFullYear();
	
	var t = Date.UTC(cYr,cMo,cDa);
	var pDt = Date.UTC(yr,mo,da)	
	
	if(pDt < t){return false};	
}

function validDate(){
	var Fmo = document.getElementById("preferredFromMonth")[document.getElementById("preferredFromMonth").selectedIndex].value-1;
	var Fda = document.getElementById("preferredFromDay")[document.getElementById("preferredFromDay").selectedIndex].value;
	var Fyr = document.getElementById("preferredFromYear")[document.getElementById("preferredFromYear").selectedIndex].value;

	var Tmo = document.getElementById("preferredToMonth")[document.getElementById("preferredToMonth").selectedIndex].value-1;
	var Tda = document.getElementById("preferredToDay")[document.getElementById("preferredToDay").selectedIndex].value;
	var Tyr = document.getElementById("preferredToYear")[document.getElementById("preferredToYear").selectedIndex].value;
	
	var fT = Date.UTC(Fyr,Fmo,Fda);
	var Tt = Date.UTC(Tyr,Tmo,Tda)	

	if(fT > Tt){return false};
}