//global vlidation object encapsulates validation routines
var validator = {
	//validates that the field value string has one or more characters in it
	isNotEmpty : function(elem){
		var str = elem.value;
		var re = /.+/;
		if (!str.match(re)) {
			alert("Please fill in the required field.");
			setTimeout("validator.focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
			return false;
		}else{
			return true;
		}
	},
	//validates that the entry is a positive or negative number
	isNumber : function(elem){
		var str = elem.value;
		var re = /^[-]?\d*\.?\d*$/;
		str = str.toString();
		if (!str.match(re)){
			alert("Enter only numbers into the field.");
			setTimeout("validator.focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
			return false;
		}
		return true;
	},
	//validates that the entry is 16 characters long
	isLen16 : function(elem){
		var str = elem.value;
		var re = /\b.{16}\b/;
		if (!str.match(re)){
			alert("Entry does not contain the required 16 characters.");
			setTimeout("validator.focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
			return false;
		}else{
			return true;
		}
	},
	//validates that the entry is formatted as an email address
	isEmailAddr : function(elem){
		var str = elem.value;
		var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
		if (!str.match(re)){
			alert("Verify the e-mail address format.");
			setTimeout("validator.focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
			return false;
		}else{
			return true;
		}
	},
	//validate that the user made a selection other than default
	isChosen : function(select){
		if (select.selectedIndex == 0){
			alert("Please make a choice from the list.");
			return false;
		}else{
			return true;
		}
	},
	//validate that the user has checked one of the radio buttons
	isValidRadio : function(radio){
		var valid = false;
		for (var i = 0; i < radio.length; i++){
			if (radio[i].checked){
				return true;
			}
		}
		alert("Make a choice from the radio buttons.");
		return false;
	},
	focusElement: function(formName, elemName){
		var elem = document.forms[formName].elements[elemName];
		elem.focus();
		elem.select();
	},
	isChecked : function () {
		if (document.tip_newsletter.yes.checked == false){
			alert ('You must check \'Yes\' to subscribe to the Slim Tip of the Week Newsletter');
			return false;
		}else{
			return true;
		}
	},
	isSame : function (email1, email2){
		if (email1 == email2){
			return true;
		}else{
			alert("Please verify that the email addresses match.");
			return false;
		}
	}
}

//batch validation router tailored for "tip_newsletter" form
function validateTipForm(form){
	if (validator.isNotEmpty(form.full_name)){
		if (validator.isNotEmpty(form.email1)){
			if (validator.isNotEmpty(form.email2)){
				if (validator.isChecked(form.yes)){
					if (validator.isEmailAddr(form.email1)){
						if (validator.isNotEmpty(form.email2)){
							if (validator.isSame(form.email1.value, form.email2.value)){
							return true;
							}
						}
					}
				}
			}
		}
	}
	return false;
}

//dispatcher, in case of multiple forms
function dispatchValidation(evt){
	evt = (evt) ? evt : window.event;
	if (evt){
		var elem = (evt.target) ? evt.target : evt.srcElement;
		if (elem.name == "tip_newsletter"){
			validateTipForm(elem,evt);
		}
	}
}

function setElementEvents(){
	addEvent(document.getElementById("tip_newsletter"), "submit", dispatchValidation, false);
}

addOnLoadEvent(setElementEvents);