
var digits = "0123456789";
			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*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }

   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}
		

function isEmpty(s){   
	return ((s == null) || (s.length == 0))
}


function isDigit (c){   
	return ((c >= "0") && (c <= "9"))
}
	

function isInRange(data, start, end) {
	for ( var i = 0 ; i< data.length ; i++ ) {
		if ( ! isDigit(data.charAt(i)) ) {
			return false ;
		}
	}

	if ( (data >= start) && (data <= end) ) {
		return true ;
	} else {
		return false ;
	}
}
function isNumber(data) {
	// check for null value first
	//if(isEmpty(data)) {
	//	return false;
	//}
	
	for ( var i = 0 ; i< data.length ; i++ ) {
		if ( ! isDigit(data.charAt(i)) ) {
			if (i == 0 && (data.charAt(i) == "-" || data.charAt(i) == "+") ) {
				return true ;
			} else {
				return false ;
			}
		}
	}
	// check if the number contains only sign
	if(data.length == 1 && (!isDigit(data.charAt(0))) ) {
		// alert("Caught you!!! " + data.charAt(0));
		return false;
	}
	return true ;
} 
// isNumber(data)

// checks whether the supplied String contains whitespace or not.
// 'true' if contains whitespace else 'false'
// These are whitespaces : " \t\n\r"
// Eg. "ab cd", "abc " are having whitespace.
function hasWhitespace(data) {
		for(var i = 0 ; i<data.length ; i++) {
			c = data.charAt(i) ;
			if(whitespace.indexOf(c) == -1) {
				continue ;
			} else {
				return true ;
			}
		}

		return false ;
}

// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";

function isPhoneNumber(data) {
		if( isEmpty(data) ) {
			return false ;
		}
		
		for(var i=0 ; i < data.length ; i++ ) {
			if ( isDigit(data.charAt(i)) || ( phoneNumberDelimiters.indexOf(data.charAt(i)) != -1 ) ) {
				continue ;
			} else {
				return false ;
			}
		}
		
		return true ;
}
			
			var daysInMonth = new Array ( 31,29,31,30,31,30,31,31,30,31,30,31 ) ;

function isSignedInteger (s) {
		if (isEmpty(s)) 
       if (isSignedInteger.arguments.length == 1) return false;
       else return (isSignedInteger.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = false ;

        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;    
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}


function isNonnegativeInteger (s) {
		var secondArg = false;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];

    

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) )
         && ((s.charAt(0) != "-") && (s.charAt(0) != "+")) );
}


function isInteger (s) {
		var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return false;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

function isIntegerInRange (s, a, b) {
		if (isEmpty(s)) 
       if (isIntegerInRange.arguments.length == 1) return false;
       else return (isIntegerInRange.arguments[1] == true);

    // Catch non-integer strings to avoid creating a NaN below
    if (!isInteger(s, false)) return false;

    var num = parseInt (s, 10);
    return ((num >= a) && (num <= b));
}

function isYear (s) {
		if (isEmpty(s)) 
       if (isYear.arguments.length == 1) return false;
       else return (isYear.arguments[1] == true);
    if (!isNonnegativeInteger(s)) return false;
    return ((s.length == 2) || (s.length == 4));
}


function isMonth (s) {
		if (isEmpty(s)) 
       if (isMonth.arguments.length == 1) return false;
       else return (isMonth.arguments[1] == true);
    return isIntegerInRange (s, 1, 12);
}


function isDay (s) {
		if (isEmpty(s)) 
       if (isDay.arguments.length == 1) return false;
       else return (isDay.arguments[1] == true);   
    return isIntegerInRange (s, 1, 31);
}


function daysInFebruary (year) {
		// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}


function isDate (year, month, day) {
		// catch invalid years (not 2- or 4-digit) and invalid months and days.
    if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;

    // Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var intYear = parseInt(year, 10);
    var intMonth = parseInt(month, 10);
    var intDay = parseInt(day, 10);

    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth-1]) return false; 

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}

//==========================================================
// Eg. : 1/12/2001 INVALID. Must be in 01/12/2001 ONLY  
//==========================================================

function checkDate(elemvalue) {
	 // Separator char STRING.
		//var elem1 = document.getElementById(elemID);
		var data = trimAll(elemvalue);
		//var elem2 = document.getElementById("td_" + elemID);
		var separator = "/" ;
		
		if(data.length < 10)
		{ 
		//elem2.innerHTML = "(Invalid)";
		return false ;
//		alert("data : "+data) ;
		}
	 var separteChar ;
	 var month = data.substring(0,2) ;
	 
	 separteChar = data.charAt(2);
	 if ( separator.indexOf(separteChar) == -1 )
	 {
	  // elem2.innerHTML = "(Invalid)";
	   return false ;
	 }
	 var day   = data.substring(3,5);
	 
 	 separteChar = data.charAt(5);
 	 if ( separator.indexOf(separteChar) == -1 ) 
 	 {
 	 //elem2.innerHTML = "(Invalid)";
 	 return false ;
	 }
	 var year  = data.substring(6,11);
	 
		if ( ! isDate ( year, month, day) ) {
			//elem2.innerHTML = "(Invalid)";
			//alert("Invalid Date : "+data) ;
			return false ;
		} else {
			//elem2.innerHTML = "&nbsp;";
			return true ;
		}
}


