//=========================================================================
// This function checks to see if a string is Empty
//=========================================================================
function isEmpty(strInput){
	if ( trim(strInput) == '' || trim(strInput) == null ) {
		return true;
	}
	return false;
}


//====================================================
// lTrim() : trim all spaces from left side of string
//   Input  : string
//   Output : string
//====================================================
function lTrim( myString ) {
	var string = new String(myString);
	var copy = false;
	var strTrim = '';

	// Trim whitespace from front of string
	for ( var i = 0 ; i < string.length ; i++ ) {
		ch = string.charAt(i);

		if ( ch != ' ' ) {
			copy = true;
		}

		if ( copy ) {
			strTrim = strTrim + ch;
		}
	}

	return strTrim;
}


//====================================================
// rTrim() : trim all spaces from right side of string
//   Input  : string
//   Output : string
//====================================================
function rTrim( myString ) {
	var string = new String(myString);
	var copy = false;
	var strTrim = '';

	// Trim whitespace from end of string
	for ( var i = string.length-1 ; i >= 0 ; i-- ) {
		ch = string.charAt(i);

		if ( ch != ' ' ) {
			copy = true;
		}

		if ( copy ) {
			strTrim = ch + strTrim;
		}
	}

	return strTrim;
}


//====================================================
// trim() : trim all spaces from ends of string
//   Input  : string
//   Output : string
//====================================================
function trim( strInput ) {
	return rTrim(lTrim(strInput));
}


//==========================================================================
// isNumeric(): Validates that a string has only numeric characters.
//==========================================================================
function isNumeric(strString)
{
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;
 
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
      {
         blnResult = false;
	  }
   }
   return blnResult;
}


//==========================================================================
// validateText(): Validates any required text elements in the form.
//==========================================================================
function validateText(objElem,strName){
	if ( isEmpty(objElem.value) ) {
		notifyInvalid(objElem,strName + ' is required.');
		return false;
	}
	return true;
}


//==========================================================================
// validateTextLength(): Validates the length of text elements in the form.
//==========================================================================
function validateTextLength(objElem,strName,intReqLength){
	if ( objElem.value.length < intReqLength ) {
		notifyInvalid(objElem,strName + ' must be a minimum of ' + intReqLength + ' characters.');
		return false;
	}
	return true;
}


//==========================================================================
// validateSelect(): Validates the Age element in the form.
//==========================================================================
function validateSelect(objElem,strName){
	var blnSelectionMade = false;
	for ( var i = 0; i < objElem.options.length; i++ ) {
		if ( objElem[i].selected ) {
			blnSelectionMade = true;
		}
	}
	if ( !blnSelectionMade ) {
		alert('You must select a ' + strName + '.');
		objElem.focus();
		return false;
	}
	return true;
}


//==========================================================================
// validateMultSelect(): Validates the Age element in the form.
//==========================================================================
function validateMultSelect(objElem,strName){
	if (objElem.value == '') {
		alert('You must select a ' + strName + '.');
		objElem.focus();
		return false;
	}
	return true;
}


//==========================================================
// validateCheckbox() : check that a checkbox is selected
//==========================================================
function validateCheckbox(objElem,intLen,strName) {
	for ( var i = 0; i < intLen; i++ ) {
		if ( objElem[i].checked ) { 	
			return true;
		}
	}
	
	if ( objElem.checked ) {
		return true;
	}
	
	alert('You must select one of the ' + strName + ' checkboxes.');
	return false;
}


//==========================================================
// validateRadio(): check that radio is selected
//==========================================================
function validateRadio(objElem,strName) {

	for ( var i = 0; i < objElem.length; i++ ) {
		if ( objElem[i].checked ) {
			return true;
		}
	}
/*	if ( elem.checked ) { return true; }*/
	alert('You must select one of the ' + strName + ' radio buttons.');
	return false;
}


//==========================================================
// validateLength(): check that a field is the proper length
//==========================================================
function validateLength(objElem,intLength) {
	if ( objElem.value.length != intLength ) {
		return false;
	}
	return true;
}


//==========================================================================
// validateEmail(): Validates the e-mail element in the form.
//==========================================================================
function validateEmail(objElement) {
	objElement.value = trim(objElement.value)
	var strString = objElement.value;
	var strFilter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if ( strFilter.test(strString) ) {
		return true;
	}
	else {
		notifyInvalid(objElement,'You have entered an invalid e-mail address');
		return false;
	}
}


//==========================================================================
// validateDomain(): Validates the website domain element in the form.
//==========================================================================
function validateDomain(objElement) {
	objElement.value = trim(objElement.value)
	
	var strElemString = objElement.value;
	var strTmpString = '';
	
	if ( strElemString.length > 4 ) {
		if ( strElemString.charAt(0) == 'w' && strElemString.charAt(1) == 'w' && strElemString.charAt(2) == 'w' && strElemString.charAt(3) == '.') {
			for (var i = 4; i < strElemString.length; i++) {
				strTmpString += strElemString.charAt(i);
			}
			strElemString = strTmpString;
			objElement.value = strElemString;
		}
	}
	
	var strFilter  = /^(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if ( strFilter.test(strElemString) ) {
		return true;
	}
	else {
		notifyInvalid(objElement,'You have entered an invalid website domain name.');
		return false;
	}
/*	var strDomainExt = 'aero,biz,com,coop,edu,gov,info,int,mil,museum,name,net,org,pro'
	strDomainExt += ',ca,de,dk,fi,fr,ie,il,jp,no,se,tv,uk,us';*/
}


//==========================================================================
// notifyInvalid(): Displays error message and sets cursor focus. 
//==========================================================================
function notifyInvalid( objElement, strMessage ) {
	alert(strMessage);
	objElement.focus();
	objElement.select();
}
