
//
// SET FOCUS TO FIRST AVAILABLE FIELD ON PAGE
//

	var bFound = false;

	// for each form
	for (f=0; f < document.forms.length; f++)
	{
	// for each element in each form
	for(i=0; i < document.forms[f].length; i++)
	{
	  // if it's not a hidden element
	  if (document.forms[f][i].type != "hidden")
	  {
		//and not read-only
		if (document.forms[f][i].readonly != true)
		{
			// and it's not disabled
			if (document.forms[f][i].disabled != true)
			{
				// set the focus to it
				document.forms[f][i].focus();
				var bFound = true;
			}
		}
	  }
	  // if found in this element, stop looking
	  if (bFound == true)
		break;
	}
	// if found in this form, stop looking
	if (bFound == true)
	  break;
	}

//
// STRING METHODS FOR CLEANING INPUT
//

	String.prototype.trim = function() {
		return this.replace(/^\s+|\s+$/g,"");
	}
	String.prototype.ltrim = function() {
		return this.replace(/^\s+/,"");
	}
	String.prototype.rtrim = function() {
		return this.replace(/\s+$/,"");
	}

//
// SET MAX LENGTH FOR FIELD
//

function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
	obj.value=obj.value.substring(0,mlength)
}

//
// Get HTML object for validation
//

function getObject(id) {
	if (document.getElementById){
		obj = document.getElementById(id);
	}else if (document.all){
		obj = document.all[id];
	}else if (document.layers) {
		obj = document.layers[id];
	}
	
	return obj;
}

//
// Compare Confirmation Email Field
//
function confirmField(obj, obj_c) {
			
	var s_obj = obj.value.toLowerCase();
	var s_obj_c = obj_c.value.toLowerCase();
	
	if (s_obj != s_obj_c) {
		obj_c.className = 'error';
		var isValid = false;
	}else{
		obj_c.className = '';
		var isValid = true;
	}
	
	return isValid;
}


//
// Define Validate class Constructor
//

function Validate() {}
/*
Validate.prototype.isOnlyAlphaNumeric = function(string){
	var invalidCharactersRegExp = /[^a-z\d ]/i;
	var isValid = !(invalidCharactersRegExp.test(string));
	
	return isValid;
}
*/

Validate.prototype.na = function(el,req){
	var string = el.value;
	
	var isValid = true;
	
	if (string.length == 0) {
		if (req =='y'){
			isValid = false;
		}else{
			isValid = true;
		}
	}
	
	if (!isValid) {
		el.className = 'error';
	}else{
		el.className = '';
	}	
	
	return isValid;
	
}

Validate.prototype.isOnlyAlphaNumeric = function(el,req){
	var string = el.value;
	var invalidCharactersRegExp = /[^a-z\d\.,\&\- ]/i;
	var isValid = !(invalidCharactersRegExp.test(string));
	
	if (string.length == 0) {
		if (req =='y'){
			var isValid = false;
		}else{
			var isValid = true;
		}
	}
	
	if (!isValid) {
		el.className = 'error';
	}else{
		el.className = '';
	}	
	
	return isValid;
}


/*
Validate.prototype.isOnlyAlphaNumericNoSpace = function(string){
	var invalidCharactersRegExp = /[^a-z\d]/i;
	var isValid = !(invalidCharactersRegExp.test(string));
	
	return isValid;
}*/

Validate.prototype.isOnlyAlpha = function(el,req){
	var string = el.value;
	var invalidCharactersRegExp = /[^a-z \.,\-]/i;
	var isValid = !(invalidCharactersRegExp.test(string));
	
	if (string.length == 0) {
		if (req =='y'){
			var isValid = false;
		}else{
			var isValid = true;
		}
	}
	
	if (!isValid) {
		el.className = 'error';
	}else{
		el.className = '';
	}	
	
	return isValid;
}

Validate.prototype.isOnlyAlphaNoSpace = function(el,req){
	var string = el.value;
	var invalidCharactersRegExp = /[^a-z]/i;
	var isValid = !(invalidCharactersRegExp.test(string));
	
	if (string.length == 0) {
		if (req =='y'){
			var isValid = false;
		}else{
			var isValid = true;
		}
	}
	
	if (!isValid) {
		el.className = 'error';
	}else{
		el.className = '';
	}
	
	return isValid;
}

Validate.prototype.isValidAddress = function(el,req){
	var string = el.value;
	var validCharactersRegExp = /[^a-z\d\s\.\-]/i;
	var isValid = !(validCharactersRegExp.test(string));
	
	if (string.length == 0) {
		if (req =='y'){
			var isValid = false;
		}else{
			var isValid = true;
		}
	}
	
	if (!isValid) {
		el.className = 'error';
	}else{
		el.className = '';
	}
	
	return isValid;
}

Validate.prototype.isOnlyNumeric = function(el,req){
	var string = el.value;
	var invalidCharactersRegExp = /[^\d\%\.\-]/;
	var isValid = !(invalidCharactersRegExp.test(string));
	
	if (string.length == 0) {
		if (req =='y'){
			var isValid = false;
		}else{
			var isValid = true;
		}
	}
	
	if (!isValid) {
		el.className = 'error';
	}else{
		el.className = '';
	}
	
	return isValid;
}
/*
Validate.prototype.isValidInteger = function(string){
	var invalidCharactersRegExp = /[^\d-]/;
	var isValid = !(invalidCharactersRegExp.test(string));
	
	return isValid;
}

Validate.prototype.isValidFloatingPoint = function(string){
	var invalidCharactersRegExp = /[^\d\.-]/i;
	var isValid = !(invalidCharactersRegExp.test(string));
	
	return isValid;
}

Validate.prototype.isValidAge = function(age){
	var isValid = false;
	if(this.isOnlyNumeric(age)){
		isValid = (parseInt(age) >= 0 && parseInt(age) < 140)
	}
	
	return isValid;	
}

Validate.prototype.isValidPassword = function(password){
	var invalidCharactersRegExp = /[^a-z\d]/i;
	var isValid = !(invalidCharactersRegExp.test(password));

	if(isValid){
		isValid = (password.length >= 8 && password.length <= 16);
	}
	
	return isValid;	
}*/

Validate.prototype.isValidTelephoneNum = function(el,req){
	var string = el.value;
	//var validFormatRegExp = /^(\+\d{1,3} ?)?(\(\d{1,5}\)|\d{1,5})(?:-)? ?\d{3,4}(?:-)? ?\d{0,7}(?: (x|xtn|extn?|extension)\.? ?\d{1,5})?$/i;
	//var validFormatRegExp = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(?: (x|xtn|extn?|extension)\.? ?\d{1,5}){0,1}$/;
	var validFormatRegExp = /^(((\+)?\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,3})|(\(?\d{2,3}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(?: (x|xtn|extn?|extension)\.? ?\d{1,5}){0,1}$/;
	var isValid = validFormatRegExp.test(string);
	
	if (string.length == 0) {
		if (req =='y'){
			var isValid = false;
		}else{
			var isValid = true;
		}
	}
	
	if (!isValid) {
		el.className = 'error';
	}else{
		el.className = '';
	}
	
	return isValid;	
}

Validate.prototype.isValidZipCode = function(el,req){
	var string = el.value;
	var validFormat = /^(\d{4,5}(-\d{4})?|[a-z]{1,2}[\da-z]{1,2} ?\d[a-z][a-z])$/i
	var isValid = validFormat.test(string);
	
	if (string.length == 0) {
		if (req =='y'){
			var isValid = false;
		}else{
			var isValid = true;
		}
	}
	
	if (!isValid) {
		el.className = 'error';
	}else{
		el.className = '';
	}
	
	return isValid;
}

Validate.prototype.isValidEmail = function(el,req){
	var string = el.value;
	//var validFormatRegExp = /^\w(\.?\w)*@\w(\.?[-\w])*\.([a-z]{3}(\.[a-z]{2})?|[a-z]{2}(\.[a-z]{2})?)$/i
	var validFormatRegExp = /^([0-9a-zA-Z]+[-._+&amp;])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$/;
	var isValid = validFormatRegExp.test(string);
	
	if (string.length == 0) {
		if (req =='y'){
			var isValid = false;
		}else{
			var isValid = true;
		}
	}
	
	if (!isValid) {
		el.className = 'error';
	}else{
		el.className = '';
	}
	
	return isValid;
}

Validate.prototype.isValidUrl = function(el,req){
	var string = el.value;
	var validFormatRegExp =  /^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([\w]+)(.[\w]+){1,2}$/;
	var isValid = validFormatRegExp.test(string);

	if (string.length == 0) {
		if (req =='y'){
			var isValid = false;
		}else{
			var isValid = true;
		}
	}
	
	if (!isValid) {
		el.className = 'error';
	}else{
		el.className = '';
	}
	
	return isValid;
}

Validate.prototype.isValidName = function(el,req){
	var string = el.value;
	//var invalidCharactersRegExp = /[^a-z \s-\']/i;
	//var invalidCharactersRegExp = /^((?:[A-Z](?:('|(?:[a-z]{1,3}))[A-Z])?[a-z]+)|(?:[A-Z][a-z]\.))(?:([ -])((?:[A-Z](?:('|(?:[a-z]{1,3}))[A-Z])?[a-z]+)|(?:[A-Z]\.)))?$/i;
	var invalidCharactersRegExp = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z\í\ñ\è\é]*)*$/;
	var isValid = invalidCharactersRegExp.test(string);
	
	if (string.length == 0) {
		if (req =='y'){
			var isValid = false;
		}else{
			var isValid = true;
		}
	}
	
	if (!isValid) {
		el.className = 'error';
	}else{
		el.className = '';
	}
	
	return isValid;

}



/*
Validate.prototype.isValidDate = function(day, month,year){
	var isValid = true;
	
	var enteredDate = new Date(day + " " + month + " " + year);
	if (enteredDate.getDate() != day){
		isValid = false;
	}	
	return isValid;
}
*/

