/*******************************************************************
*	Trim Prototype, this function eliminates the leading and 
*	trailing white spaces from a String.
*	To use: strValue.trim();
*******************************************************************/
	String.prototype.trim = function() { 
		return( this.replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1') ); 
	}
/*******************************************************************
*	isEmpty fucntion returns true if the value of the parameter is
*	empty or null. Also returns an error Message depending on what
*	language is specified.
*******************************************************************/
	function isEmpty(objText) {
			if(objText.value.trim() == "") 
				return true;
		return false;
	}
/*******************************************************************
*	isInt function returns true if the value of the parameter is
*	a valid Integer value.
********************************************************************/
	function isInt(oInput) {
		var strNumber = oInput.value;

		var strNumber = oInput.value + "";

			if(strNumber.trim() == "")
				return false;

			for(var i=0; i<strNumber.length; i++)
					if (strNumber.charAt(i) < "0" || strNumber.charAt(i) > "9") 
						return false;

		return true;
	}
/*******************************************************************
*	isFloat fucntion returns true if the value of the parameter is
*	a valid flaoting point number. Also returns an error Message 
*	depending on what language is specified.
*******************************************************************/
function isFloat(oInput) 
{
		var strNumber = oInput.value + "";

			if(strNumber.trim() == "")
				return false;

		var iDot = strNumber.indexOf(".");
		var iNegative = strNumber.indexOf("-");
		var ind=0;
		
			if(iNegative == 0)
			{
				ind=1;
			}				

			if(iDot != -1) 
			{
					for(var i=ind; i<iDot; i++)
						if (strNumber.charAt(i) < "0" || strNumber.charAt(i) > "9") 
							return false;

					for(var i=iDot+1; i<strNumber.length; i++)
						if (strNumber.charAt(i) < "0" || strNumber.charAt(i) > "9" ) 
							return false;
			}
			else 
			{
					for(var i=ind; i<strNumber.length; i++)
						if (strNumber.charAt(i) < "0" || strNumber.charAt(i) > "9" ) 
							return false;
			}			
		return true;
	}


/*******************************************************************
*	isDate_SP returns true if the paraemter is a valid string in
*	format MM/DD/YYYY. If not, it returns error messages in 
*	spanish
*******************************************************************/
	function isDate(oInput) {
		var dateStr = oInput.value;
		var datePat = /^(\d{1,2})(\/)(\d{1,2})(\/)(\d{4})$/;
		var matchArray = dateStr.match(datePat);
		
   			if (matchArray == null) {
				alert("Introduzca una fecha con el formato mm/dd/yyyy.");
				oInput.focus();
				return false;
			}
		
		iMonth	= matchArray[1];
		iDay	= matchArray[3];
		iYear	= matchArray[5];

			if (iMonth < 1 || iMonth > 12) {
				alert("El mes debe ser entre 01 y 12.");
				oInput.focus()
				return false;
			}

		    if (iDay < 1 || iDay > 31) {
				alert("El dia debe ser entre 01 y 31.");
				oInput.focus();
				return false;
			}

			if ((iMonth==4 || iMonth==6 || iMonth==9 || iMonth==11) && iDay==31) {
				alert("El mes " + iMonth + " no tiene 31 dias!")
				oInput.focus()
				return false;
			}

			if (iMonth == 2) {
				var isLeap = (iYear % 4 == 0 && (iYear % 100 != 0 || iYear % 400 == 0));
		
					if (iDay > 29 || (iDay==29 && !isLeap)) {
						alert("Febrero del " + iYear + " no tiene " + iDay + " dias!");
						oInput.focus();
						return false;
					}
			}
		
		return true;
	}
	
/*******************************************************************
*	isEmail retruns false if is not a valid e-mail address
*******************************************************************/
function isEmail(oInput) {
	var email=/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
	return(email.test(oInput));
}

/*******************************************************************
*	roundFloat rounds up a number to 2 decimals
*******************************************************************/
function roundFloat(strValue) {
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
		if(intCents<10)
			strCents = "0" + strCents;

	dblValue.substring(dblValue.length-(4*i+3));

	return (dblValue + '.' + strCents );
}

/*******************************************************************
*	isPercentage function returns true if the value of the parameter is
*	a valid Percentage value[0-100].
********************************************************************/
	function isPercentage(strValue) 
	{
		Num  = new Number(strValue);		
		if(Num <0 || Num>100)
		{
			return false;
		}
		return true;
	}
	
/*******************************************************************************/

function validateEmail( strValue) 
{
/************************************************
DESCRIPTION: Validates that a string contains a
  valid email pattern.

 PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS: Accounts for email with country appended
  does not validate that email contains valid URL
  type (.com, .gov, etc.) or valid country suffix.
*************************************************/
var objRegExp  = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*) (\.[a-z]{3})(\.[a-z]{2})*$)/i;

  //check for valid email
  return objRegExp.test(strValue);
}

