// simplevalidation.js
//
// SUMMARY
//
// whitspace Global variable contains all white space
// Functions:
// trim(strParam)				Right and Left trim the string
// ltrim(strParam)				Left trim the string	
// rtrim(strParam)				Right trim the string
// ReplaceAll(strParam,strSearch,strReplaced) 	replace searchable characters of string with other
// formatedFloat(floatParam,intDecimal)		reformat float with number of decimals
// roundFloat(floatParam,intDecimal)   		round float to number of digits
// isFloat(floatParam,intdigit,intDecimal) 	check if float with max number of digits and decimal
// isDate(intDay,intMonth,intYear)    		Check is Valid Date
// isPassword(strPassword,intMaxSize) 		Check if valid password with max size minimum character<6 and contains only characters and digit and _
// isLetter (c)                       		Check whether character c is an English letter 
// isDigit (c)                        		Check whether character c is a digit 
// isEmail (s [,eok])                 		True if string s is a valid email address.
// checkEmail (theField [,eok])		  	Check that theField.value is a valid Email.
// isWhitespace (s)				Check whether string s is empty or whitespace.
// isEmpty(s)					Check whether string s is empty.
// warnInvalid (theField, s)			Notify user that contents of field theField are invalid.
// isValidDate (day, month, year)		Check if the date is valid
//----------------------------------------------------------------------------

	var defaultEmptyOK;

	var whitespace = " \t\n\r";
	/*
		Eliminate all left and right spaces
		Parameters Input: strParam string want to trim
		Return string after trim
	*/
	function trim(strParam)
	{
		strParam=ltrim(strParam);
		strParam=rtrim(strParam);
		return strParam;
	}
	
//-----------------------------------------------------------------------------	
	/*
		Eliminate all left spaces
		Parameters Input: strParam string want to trim
		Return string after left trim
	*/
	function ltrim(strParam)
	{
		for(i=0;i<strParam.length;i++)
		{
			var c = strParam.charAt(i);
			if (whitespace.indexOf(c) == -1)
				break;
		}
		strParam=strParam.substring(i,strParam.length);
		return strParam;
	}
	
//-----------------------------------------------------------------------------
	/*
		Eliminate all right spaces
		Parameters Input: strParam string want to trim
		Return string after right trim
	*/
	function rtrim(strParam)
	{
		for(i=strParam.length-1;i>=0;i--)
		{
			var c = strParam.charAt(i);
			if (whitespace.indexOf(c) == -1)
				break;
		}
		if(i!=strParam.length-1)
			strParam=strParam.substring(0,i+1);
		return strParam;
	}

//-----------------------------------------------------------------------------
	/*
		Replace All searchable strings by other string
		Parameters Input: strParam string want to search at
						  strSearch string want to search for
						  strReplaced string want to replace with
		Return string after replaced searchable string
	*/
	function ReplaceAll(strParam,strSearch,strReplaced)
	{
		if(strParam.indexOf(strSearch)==-1)
			return strParam;
		while(strParam.indexOf(strSearch)!=-1)
			{
			strParam=strParam.replace(strSearch,strReplaced);
			}
			return strParam;
	}
	
	
//-----------------------------------------------------------------------------
	/*
		Format decimal float with determined number of decimal point
		Parameters Input: floatParam float whic we want to reformat
						  intDecimal number of decimals
		Return float after formated
	*/
	function formatedFloat(floatParam,intDecimal)
	{
		floatParam=parseInt(parseFloat(floatParam)*Math.pow(10,intDecimal))/Math.pow(10,intDecimal);
		return floatParam;
	}
	
//-----------------------------------------------------------------------------	
/*
		Format decimal float with determined number of decimal point
		Parameters Input: floatParam float whic we want to reformat
						  intDecimal number of decimals
		Return float after formated
*/
	function roundFloat(floatParam,intDecimal)
	{
		floatParam=Math.round(parseFloat(floatParam)*Math.pow(10,intDecimal))/Math.pow(10,intDecimal);
		return floatParam;
	}


//-----------------------------------------------------------------------------	
	/*
		check value is float number with correct number of digits,decimals
		Parameters Input: floatParam string which we want to check is it float
						  intDecimal number of digits
						  intDecimal number of decimals
		Return  3 If Not Correct Float
				2 If number of digits larger 
				1 If number of decimal larger
				0 If Correct
	*/
	function isFloat(floatParam,intdigit,intDecimal)
	{
		if(!parseFloat(floatParam))
			return 3;
		for(i=0;i<floatParam.length;i++)
		{
			if(!isDigit(floatParam.charAt(i))&&floatParam.charAt(i)!=".")
				return 3;
		}
		if(parseFloat(floatParam)>=parseInt(intdigit)*10)
				return 2;
		if(parseFloat(parseFloat(floatParam)*Math.pow(10,intDecimal))-parseInt(parseFloat(floatParam)*Math.pow(10,intDecimal))>0)
				return 1;
		return 0;
	}
	
//-----------------------------------------------------------------------------
	/*
		validate Date
		Parameters Input: intDay day
						  intMonth Month as integer 0,1,2
						  intYear Year 
		return true if valid date
			   false if not valid
	*/
	function isDate(intDay,intMonth,intYear)
	{
		var varDate=new Date(intYear,intMonth,intDay);
		if(varDate.getDay!=intDay)
			return false;
		return true;
	}
	
//-----------------------------------------------------------------------------
	/*
		validate Password
		Parameters Input: strPassword password want to check
						  intMaxSize max size of password
		return 0 if valid password
			   1 password must be more than 6 charcters
			   2 password must be less than max size
			   3 password must start with charcter
			   4 password must contain digit
			   5 password contains only charaters and numbers and _
	*/
	function isPassword(strPassword,intMaxSize)
	{
		var boolHasChar,boolHasDigit;
		boolHasChar=false;boolHasDigit=false;
		if(trim(strPassword).length<6)
			return 1;
		if(trim(strPassword).length>intMaxSize)
			return 2;
		if(isLetter(trim(strPassword).charAt(0))==false)
			return 3;
		for(i=1;i<trim(strPassword).length;i++)		
		{
			if(isDigit(trim(strPassword).charAt(i)))
				boolHasDigit=true;
			else
				if(isLetter(trim(strPassword).charAt(i))&&trim(strPassword).charAt(i)!="_")
					return 5;
		}
		if(boolHasDigit==false)
			return 4;
		return 0;;
	}
	
//-----------------------------------------------------------------------------
	/*
		check if charcter
		Parameters Input: strChar character want to check
		return true if character
			   false if not is character
	*/
	function isLetter(strChar)
	{
		return (((strChar >= "a") && (strChar <= "z"))||((strChar >= "A") && (strChar <= "Z")))
	}


//-----------------------------------------------------------------------------
	/*
		check if digit
		Parameters Input: strChar character want to check
		return true if character
			   false if not is character
	*/
	function isDigit(strChar)
	{
		return ((strChar >= "0") && (strChar <= "9"))
	}


//-----------------------------------------------------------------------------
	// isEmail (STRING s [, BOOLEAN emptyOK])
	// 
	// Email address must be of form a@b.c -- in other words:
	// * there must be at least one character before the @
	// * there must be at least one character before and after the .
	// * the characters @ and . are both required
	//
	// For explanation of optional argument emptyOK,
	// see comments of function isInteger.
	function isEmail (s)
	{   
		if (isEmpty(s)) 
	       if (isEmail.arguments.length == 1) return defaultEmptyOK;
	       else return (isEmail.arguments[1] == true);
	   
	    // is s whitespace?
	    if (isWhitespace(s)) return false;
	    
	    // there must be >= 1 character before @, so we
	    // start looking at character position 1 
	    // (i.e. second character)
	    var i = 1;
	    var sLength = s.length;

	    // look for @
	    while ((i < sLength) && (s.charAt(i) != "@"))
	    { i++
	    }

	    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
	    else i += 2;

	    // look for .
	    while ((i < sLength) && (s.charAt(i) != "."))
	    { i++
	    }

	    // there must be at least one character after the .
	    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
	    else return true;
	}

//-----------------------------------------------------------------------------
	// checkEmail (TEXTFIELD theField [, BOOLEAN emptyOK==false])
	//
	// Check that string theField.value is a valid Email.
	//
	// For explanation of optional argument emptyOK,
	// see comments of function isInteger.

	function checkEmail (theField, emptyOK)
	{   if (checkEmail.arguments.length == 1) emptyOK = defaultEmptyOK;
	    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
	    else if (!isEmail(theField.value, false)) 
	       return warnInvalid (theField, iEmail);
	    else return true;
	}
	
//-----------------------------------------------------------------------------
	// Returns true if string s is empty or 
	// whitespace characters only.
	function isWhitespace (s)
	{   
		var i;

	    // Is s empty?
	    if (isEmpty(s)) return true;

	    // Search through string's characters one by one
	    // until we find a non-whitespace character.
	    // When we do, return false; if we don't, return true.

	    for (i = 0; i < s.length; i++)
	    {   
	        // Check that current character isn't whitespace.
	        var c = s.charAt(i);

	        if (whitespace.indexOf(c) == -1) return false;
	    }

	    // All characters are whitespace.
	    return true;
	}


//-----------------------------------------------------------------------------
	// Check whether string s is empty.
	function isEmpty(s)
	{   
	return ((s == null) || (s.length == 0))
	}

//-----------------------------------------------------------------------------
	// Notify user that contents of field theField are invalid.
	// String s describes expected contents of theField.value.
	// Put select theField, pu focus in it, and return false.
	function warnInvalid (theField, s)
	{   
		theField.focus()
	    theField.select()
	    alert(s)
	    return false
	}

//-----------------------------------------------------------------------------
function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function isValidDate (day, month, year) {
// checks if date passed is valid
// will accept dates in following format:
// isDate(dd,mm,ccyy), or
// isDate(dd,mm) - which defaults to the current year, or
// isDate(dd) - which defaults to the current month and year.
// Note, if passed the month must be between 1 and 12, and the
// year in ccyy format.

    var today = new Date();
    year = ((!year) ? y2k(today.getYear()):year);
    month = ((!month) ? today.getMonth():month-1);
    if (!day) return false
    var test = new Date(year, month, day);
    if ( (y2k(test.getYear()) == year) &&
         (month == test.getMonth()) &&
         (day == test.getDate()) )
        return true;
    else
        return false
}

