var f5 = {};
// In the future convert all of this crap to be object oriented and use prototype

function f5_required(form) {
	if (form.gaveup == true) {
		return true;
	}
	var tags = f5_getFormElementsByClass(form, 'f5_required');
	var echos = '';
	for (i=0;i<tags.length;i++){
		thetag = tags[i].tagName;
		switch(thetag){
			case 'div':
			case 'DIV':
				var subtags = tags[i].getElementsByTagName("input");
				var foundChecked = false;
				for (j=0;j<subtags.length;j++){
					if (subtags[j].checked) {
						foundChecked = true;
					}
				}
		 		if (foundChecked == false) {
		 			tags[i].className = tags[i].className +' f5_alert';
		 			var ptags = tags[i].getElementsByTagName("p");
		 			var label = subtags[0].name;
					for (k=0;k<ptags.length;k++){
						if (ptags[k].className.match("pseudolabel")) {
					 		label = ptags[k].innerHTML;
					 	}
					}
	 				echos = echos + label + ' must be checked\n';
				}
				break;
			case 'input':
			case 'INPUT':
				thetype = tags[i].type;

				switch(thetype){
					case 'checkbox':
					case 'radio':
						if (tags[i].checked == false) {
				 			tags[i].className = tags[i].className +' f5_alert';
							label = f5_findLabel(tags[i]);
				 			if (label) {
				 				echos = echos + label.innerHTML + ' must be checked\n';
				 				label.className = label.className +' f5_alert';
				 			} else {
								echos = echos + tags[i].name + ' must be checked\n';
				 			}
						}
						break;
					default:
				} // switch
			default:
				if (tags[i].value == '' || tags[i].value == null) {
		 			tags[i].className = tags[i].className +' f5_alert';
					label = f5_findLabel(tags[i]);
		 			if (label) {
		 				echos = echos + label.innerHTML + ' is empty\n';
		 				label.className = label.className +' f5_alert';
		 			} else {
						echos = echos + tags[i].name + ' is empty\n';
		 			}
				}
		} // switch
	}
	if (echos) {
		alert(echos);
		return false;
	}


	f5_hideSubmits();
	return true;
}

function f5_forgetrequired(id) {
	var form = document.getElementById(id);
	form.gaveup = true;
}

function f5_getFormElementsByClass(form, classname){
	var inc=0;
	var alltags=form.getElementsByTagName("*");
	var customcollection = new Array();
	for (i=0;i<alltags.length;i++){
		if (alltags[i].className.match(classname) && !(alltags[i].tagName == 'LABEL' || alltags[i].tagName == 'label'))
			customcollection[inc++]=alltags[i];
		}
	return customcollection;
}

function f5_findLabel(input) {
	if (input.id) {
		var label = document.getElementById(input.id+'label');
		if (label) {
			return label;
		}
	}
	return false;
}

/**
 *
 * @access public
 * @return void
 **/
function f5_repeat(field){
	var myid = field.id;
	var matchid = myid.substring(1);
	var match = document.getElementById(matchid);
	if (match.value == field.value) {
	} else {
		field.value = '';
		label = f5_findLabel(field);
		if (label) {
			echo = label.innerHTML;
		} else {
			echo = field.name;
		}
		echo = echo + ' must match ';
 		label = f5_findLabel(match);
		if (label) {
			echo = echo + label.innerHTML + '.';
		} else {
			echo = echo + field.name + '.';
		}
		alert (echo);
	}
}

function f5_password(field) {
	var teststr = new String(field.value);
	var regex = /^(?=(?=(?=[a-zA-Z0-9\-_]{8,}$).*[0-9].*).*[A-Z].*).*[a-z].*/

	if (regex.exec(teststr)) {
	} else {
		field.value = '';
		label = f5_findLabel(field);
		if (label) {
			echo = label.innerHTML;
		} else {
			echo = field.name;
		}
		echo = echo + ' must be at least 8 characters long and must include a lower case letter, an upper case letter, and a number.';
		alert (echo);
	}
}

function f5_float(){

}

function f5_email(field) {
	var teststr = new String(field.value);
	var regex = /^[-a-z0-9!#$%&\'*+\/=?^_`{|}~]+(\.[-a-z0-9!#$%&\'*+\/=?^_`{|}~]+)*@(([a-z0-9]([-a-z0-9]*[a-z0-9]+)?){1,63}\.)+([a-z0-9]([-a-z0-9]*[a-z0-9]+)?){2,63}$/i

	if (regex.exec(teststr)) {
	} else {
		field.value = '';
		label = f5_findLabel(field);
		if (label) {
			echo = label.innerHTML;
		} else {
			echo = field.name;
		}
		echo = echo + ' requires a valid email address';
		alert (echo);
	}
}

function f5_hideSubmits(){
	var submits = document.getElementsByName("submit");
	for (var i=0;i<submits.length;i++){
		submits[i].style.visibility = "hidden";
	}

}
function f5_showSubmits(){
	var submits = document.getElementsByName("submit");
	for (i=0;i<submits.length;i++){
		submits[i].style.visibility = "visible";
	}

}

function f5_hold(on){
	var target = document.getElementById('f5_hold');
	target.className = (on) ? 'f5_hold_on' : 'f5_hold_off';
}



// JavaScript Functions by Shawn Olson
// Copyright 2006
// http://www.shawnolson.net
function f5_checkUncheckAll(theElement) {
	var theForm = theElement.form, z = 0;
	for(z=0; z<theForm.length;z++){
		if(theForm[z].type == 'checkbox'){
			theForm[z].checked = theElement.checked;
		}
	}
}




/**
 *
 * @access public
 * @return void
 **/
function f5_selectRadioValues(value,theElements) {
	//Programmed by Shawn Olson
	//Copyright (c) 2007
	//Permission to use this function provided that it always includes this credit text
	//  http://www.shawnolson.net
	//Find more JavaScripts at http://www.shawnolson.net/topics/Javascript/
	//This script was modified from the function checkUncheckSome() also
	//created by Shawn Olson

	//theElements is an array of objects designated as a comma separated list of their IDs
	//If an element in theElements is not a checkbox, then it is assumed
	//that the function is recursive for that object and will check/uncheck
	//all checkboxes contained in that element


         var formElements = theElements.split(',');
	 for(var z=0; z<formElements.length;z++){
	  theItem = document.getElementById(formElements[z]);
	  if(theItem){
	  theInputs = theItem.getElementsByTagName('input');
	  for(var y=0; y<theInputs.length; y++){
	   if(theInputs[y].type == 'radio'){
         theName = theInputs[y].name;
         if(theInputs[y].value==value){
		   theInputs[y].checked='checked';
		 }
	    }
	  }
	  }
     }
    }