function XcmAutoValidation(form){

	function GetLabel( el ){
		var labels = document.getElementsByTagName('LABEL');
		var i=0;
		for(i;i<labels.length;i++)
			if(labels[i].getAttribute("for") == el.id)	
				return labels[i];
		return false;
	};

	function Validate( el ){
		var reText = el.getAttribute("validate");
		if(reText){
			var re = new RegExp(reText,'i');
			if(!re.test(el.value)){	Failed[Failed.length] = el.getAttribute("ErrorMessage");
				if(XcmAutoValidation.highLightElement) XcmAutoValidation.highLightElement(el, GetLabel(el));
			}else if(XcmAutoValidation.resetElement) XcmAutoValidation.resetElement(el,GetLabel(el));
		};
	};

	var Failed = [];
	var l = form.elements.length;
	var i = 0;
	var el;

	for(i;i<l;i++){
		el = form.elements[i];
		if(el.tagName == 'INPUT'){
			switch(el.type){
				case 'text':
				case 'password':
				case 'hidden':
					Validate( el );
					break;
				case 'checkbox':
					if(el.getAttribute("validate") && !el.checked) 
						Failed[Failed.length] = el.getAttribute("ErrorMessage");
					break;
				case 'radio':
					if(el.getAttribute("validate") && !el.checked) {
						var name = el.name;
						var siblings = [];
						var checked = false;
						for(var j=0;j<l;j++){
							if(form.elements[j].name == name){
								if(form.elements[j].checked){
									checked = true;
									break;
								};
							};
							if(form.elements[j] != el && el.getAttribute("validate"))
								el.setAttribute("validate","");
						};
						if(!checked){
							Failed[Failed.length] = el.getAttribute("ErrorMessage");
						};
					};
			};
		}else if (el.tagName == 'TEXTAREA') Validate( el );
	};
	if(Failed.length > 0){
		alert('Sorry but this form can\'t be submitted because\n\n\t- ' + Failed.join('\n\t- ') + '\n\nPlease fix these problems and try again');
		return false;
	};
	return true;
};
function top(){
	if(String(window.location).indexOf("#top") < 0)
		window.location = window.location+'#top';
	else
		//handle subsequent clicks of the top link
		window.location = window.location;
}

function isEmailAddr (s){ 
	var rv = false
	if ((s == null) || (s.length == 0)) 
       	rv = false;
 	 else {
		var reEmail =/([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		//reEmail = /.+\@.+\..+$/
		
		rv = reEmail.test(s)
    }
	if(rv){
		return rv
	}else{
		return false
	}
}

function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}

function validateRadio(theRadio){
	// set var radio_choice to false
	var radio_choice = false;

	// Loop from zero to the one minus the number of radio button selections
	for (counter = 0; counter < theRadio.length; counter++){
		// If a radio button has been selected it will return true
		// (If not it will return false)
		if (theRadio[counter].checked){
			radio_choice = true; 
			strRadioValue = theRadio[counter].value;
		}
	}
	if(!radio_choice){
		return (false);
	}
	    return (true);
}

function validateSelect(selectbox){
	var iSelect = selectbox.options[selectbox.selectedIndex].value ;
	if(iSelect == 0){
		return false;
	}else{
		return true;
	}
}

function validateCheckbox(checkbox){
	var isChecked = false;
	
	for (var i = 0; i < checkbox.length; i++) {
   		if (checkbox[i].checked) {
    		isChecked = true;
   		}
	}
	
	return isChecked;
}
 
function validation(theForm){
	if (theForm.name.value == ""){
    	alert("Please enter your name.");
    	theForm.name.focus();
    	return (false);
  	}
	if (!isEmailAddr(theForm.email.value)){
    	alert("Please enter a complete email address in the form: yourname@yourdomain.com");
    	theForm.email.focus();
    	return (false);
 	}
  	if (!IsNumeric(theForm.phone.value)){
    	alert("Please enter your phone number");
    	theForm.phone.focus();
    	return (false);
 	}

	return true
}

