/*********************************************************************************************************************************************
*	Validation scripts to check form input	
**********************************************************************************************************************************************/
	


function checkLength(objInput, objCounter, maxLength) {

	objCounter.value = maxLength - objInput.value.length;

	if (objInput.value.length > maxLength) {
		objInput.value = objInput.value.substring(0,maxLength);
		objCounter.value = 0;
	}

}

function CheckEnteredField(obj, errMsg) {
	switch (obj.type) {
		case "text":
			if (obj.value.length > 0) 
				return true;
			else {
				alert(errMsg);
				obj.focus();
				return false;
			}
			break;
		case "textarea":
			if (obj.value.length > 0) 
				return true;
			else {
				alert(errMsg);
				obj.focus();
				return false;
			}
			break;		
		case "select-one":
			if (obj.options[obj.selectedIndex].value.length > 0) 
				return true;
			else {
				alert(errMsg);
				obj.focus();
				return false;
			}
			break;
			
	}
}


function isEmail(obj, errMsg) {

	var str = obj.value;
	
	// are regular expressions supported?
	var supported = 0;
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}
	if (!supported) {
		if( (str.indexOf(".") > 2) && (str.indexOf("@") > 0) ) {
			return true;
		} else {
			alert(errMsg);
			obj.focus();
			return false;
		}
	}
	else {
		var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
		var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	
		if((!r1.test(str) && r2.test(str))) {
			return true;
		} else {
			alert(errMsg);
			obj.focus();
			return false;
		}
	}
}


function validateNotEmpty( strValue ) {
/************************************************
DESCRIPTION: Validates that a string is not all
  blank (whitespace) characters.

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.
*************************************************/
   var strTemp = strValue;
   strTemp = trimAll(strTemp);
   if(strTemp.length > 0){
     return true;
   }
   return false;
}


function trimAll( strValue ) {
/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/
	var objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;

   //check for leading &amp; trailing spaces
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}


function validateEmail( strValue) {
/************************************************
DESCRIPTION: Validates that a string contains a
  valid email pattern.

*************************************************/
  var objRegExp = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
  return objRegExp.test(strValue);

  //check for valid email
  return objRegExp.test(strValue);
}


function  validateNumeric( strValue ) {
/******************************************************************************
DESCRIPTION: Validates that a string contains only valid numbers.

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.
******************************************************************************/
  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;

  //check for numeric characters
  return objRegExp.test(strValue);
}

