var gBadCharsArray = new Array("<", ">");



// -------------------------------------------------------------------
// TabNext()
// Function to auto-tab phone field
// Arguments:
//   obj :  The input object (this)									
//   event: Either 'up' or 'down' depending on the keypress event	
//   len  : Max length of field - tab when input reaches this length
//   next_field: the HTML id of the next input object to get focus	
// -------------------------------------------------------------------
//
var phone_field_length=0;
function TabNext(obj,event,len,next_field) {
	if (event == "down") {
		phone_field_length=obj.value.length;
	}
	else if (event == "up") {
		if (obj.value.length != phone_field_length) {
			phone_field_length=obj.value.length;
			if (phone_field_length == len) {
				var n = document.getElementById(next_field);
				n.focus();
			}
		}
	}
}

// -------------------------------------------------------------------
// ValidationObject()
// the basic validation object. 
// Arguments:
//   element:		 The object to validate							
//   message:		 The error message to report if validation fails
//   vType:			 The type of validation							
//   compareElement: (optional) The id of an element to compare to	
// -------------------------------------------------------------------
//
function ValidationObject(element, message, vType, compareElement) {
	if(element != "") {
		// if the arguments are being passed in, 
		// populate the object with them. 
		//
		this.HtmlElement	= element;
		this.ErrorMessage	= message;
		this.ValidationType	= vType;
		this.CompareElement = compareElement;
	} else {
		// otherwise, let's just create the empty object.
		//
		this.HtmlElement	= null;
		this.ErrorMessage	= null;
		this.ValidationType = null;
		this.CompareElement = null;
	}
}

// -------------------------------------------------------------------
// Validate()
// validates the form
// Arguments:
//   vo: an array of validationObjects to run through.	
// -------------------------------------------------------------------
//
function Validate(vo) {
	// loop through all the validationObjects
	//
	for(var i=0; i<vo.length; i++) {

	
		// now, let's determine the type of validation to use. 
		// if the ValidationType property is null, we'll just 
		// do the simple "IsRequired" validation. 
		//
		if(vo[i].ValidationType == "email") {
			// verify the field contains an email address
			//
			if(!IsEmailAddress(vo[i].HtmlElement)) {
				alert(vo[i].ErrorMessage);
				vo[i].HtmlElement.focus();
				return false;
			}
		} else if(vo[i].ValidationType == "compare") {
			// verify the field, and the compareElement match
			//
			if(!CompareFields(vo[i].HtmlElement, vo[i].CompareElement)) {
				alert(vo[i].ErrorMessage);
				vo[i].HtmlElement.focus();
				return false;
			}
		
		} else if(vo[i].ValidationType == "checked") {
			// verify the field, and the compareElement match
			//
			if(!IsChecked(vo[i].HtmlElement)) {
				alert(vo[i].ErrorMessage);
				vo[i].HtmlElement.focus();
				return false;
			}
			
		} else if(vo[i].ValidationType == "numeric") {
			// verify the field, and the compareElement match
			//
			if(!IsNumeric(vo[i].HtmlElement)) {
				alert(vo[i].ErrorMessage);
				vo[i].HtmlElement.focus();
				return false;
			}
		
		} else if(vo[i].ValidationType == "minlength") {
			// verify the field has the right length, 
			// and pass it through the IsRequired
			//
			if(vo[i].HtmlElement.value.length < vo[i].CompareElement 
				&& !IsRequiredField(vo[i].HtmlElement)) {
				alert(vo[i].ErrorMessage);
				vo[i].HtmlElement.focus();
				return false;
			}
		} else {
			// perform basic "not empty" validation
			//
			if(!IsRequiredField(vo[i].HtmlElement)) {
				alert(vo[i].ErrorMessage);
				vo[i].HtmlElement.focus();
				return false;
			}
		}
	}
}

// basic form field validation. 
//
function IsRequiredField(e) {
	var inputStr = Trim(e.value)
	if(inputStr == "" || inputStr == null) {
		return false;
	}
	for(var i=0; i<gBadCharsArray.length; i++) {
		if(inputStr.indexOf(gBadCharsArray[i])!=-1) {
			return false;
		}
	}
	return true;
}

// basic comparision validation.
//
function CompareFields(e1, e2) {
	if(e1.value == e2.value) {
		return true;
	}
	return false;
}

// email address field validation. 
//
function IsEmailAddress(e) {
	// first let's do the easy work, and just look for 1 @ sign. 
	var bits = e.value.split("@");
	if (bits.length != 2) {return false;}
	if (e.value.indexOf(" ")!=-1) {return false;}
	// now do the regex.
	var emailRegxp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!emailRegxp.test(e.value)) {
		return false;
	}
	return true;
}

// validates that a checkbox is checked
//
function IsChecked(e) {
	if(e.checked) {
		return true
	}
	return false;
}

// validates that a value is numeric. 
//
function IsNumeric(PossibleNumber) {
	var regex = /^\d+$/;
	return regex.test(PossibleNumber.value);
}

// removes any white space from the left side of a string.
//
function LTrim(str) {
   var whitespace = new String(" \t\n\r");
   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...
      //
      var j=0, i = s.length;
      
      // Iterate from the far left of string until we
      // don't have any more whitespace...
      //
      while (j<i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }
   return s;
}

// removes any whitespace from the right side of a string
//
function RTrim(str){
   var whitespace = new String(" \t\n\r");
   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      var i = s.length - 1;       // Get length of string
      while (i>=0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }
   return s;
}

// removes any whitespace from the left and right side of a string
//
function Trim(str) {
	if (str.length != 0) {
		var newStr = RTrim(LTrim(str));
		//alert(newStr);
		return newStr;
	} else {
		//alert("returning nothing...");
		return null;
	}
}

