/************************************************
DESCRIPTION: Trims trailing whitespace chars.
    
PARAMETERS:
   strValue - String to be trimmed.  
      
RETURNS:
   Source string with right whitespaces removed.
*************************************************/
function rightTrim( strValue ) {
var objRegExp = /^([\w\W]*)(\b\s*)$/;
 
      if(objRegExp.test(strValue)) {
       //remove trailing a whitespace characters
       strValue = strValue.replace(objRegExp, '$1');
    }
  return strValue;
}


/************************************************
DESCRIPTION: Trims leading whitespace chars.
    
PARAMETERS:
   strValue - String to be trimmed
   
RETURNS:
   Source string with left whitespaces removed.
*************************************************/
function leftTrim( strValue ) {
var objRegExp = /^(\s*)(\b[\w\W]*)$/;
 
      if(objRegExp.test(strValue)) {
       //remove leading a whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}


/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/
function trimAll( strValue ) {
 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;
}


/******************************************************************************
DESCRIPTION: Validates that a string contains only valid numbers.

PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
function  validateNumeric( strValue ) {
  var objRegExp  =  /(^\d\d*\.\d*$)|(^\d\d*$)|(^\.\d\d*$)/;
 
  //check for numeric characters
  return objRegExp.test(strValue);
}

/******************************************************************************
DESCRIPTION: Validates that a string contains only valid positive numbers.

PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
function  validatePosNumeric( strValue ) {
  var objRegExp  =  /(^\d\d*\.\d*$)|(^\d\d*$)|(^\.\d\d*$)/;
 
  //check for numeric characters
  if(!objRegExp.test(strValue))
  {
	return false;
  }

  if(strValue == 0)
  {
	return false;
  }

  return true;
}


/******************************************************************************
DESCRIPTION: Validates that a string contains only 
    valid integer number.
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
function validateInteger( strValue ) {
  var objRegExp  = /(^\d\d*$)/;
 
  //check for integer characters
  return objRegExp.test(strValue);
}


/******************************************************************************
DESCRIPTION: Validates that a string contains only 
    valid positive integer number.
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
function validatePosInteger( strValue ) {
	var objRegExp  = /(^\d\d*$)/;

	//check for integer characters
	if(!objRegExp.test(strValue))
	{
		return false;
	}
	
	if(strValue < 1)
	{
		return false;
	}
	
	return true;
}

/************************************************
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.) and optionally,
  a valid country suffix.  Since email has many
  forms this expression only tests for near valid
  address.  Some additional validation may be
  required.
*************************************************/
function validateEmail( strValue) {
var objRegExp  = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,3}(\.[a-z]{2}){0,2})$/i;
  //check for valid email
  return objRegExp.test(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.
*************************************************/
function validateNotEmpty( strValue ) {
   var strTemp = strValue;
   strTemp = trimAll(strTemp);
   if(strTemp.length > 0){
     return true;
   }  
   return false;
}


/************************************************
DESCRIPTION: Validates that a string contains only 
    valid dates with 2 digit month, 2 digit day, 
    4 digit year. Date separator can be ., -, or /.
    Uses combination of regular expressions and 
    string parsing to validate date.
    Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
   
REMARKS:
   Avoids some of the limitations of the Date.parse()
   method such as the date separator character.
*************************************************/
function validateDate( strValue ) {
  var objRegExp = /^\d{1,2}(\/)\d{1,2}\1\d{4}$/
 
  //check to see if in correct format
  if(!objRegExp.test(strValue))
    return false; //doesn't match pattern, bad date
  else{
    var arrayDate = strValue.split(RegExp.$1); //split date into month, day, year
	var intDay = parseInt(arrayDate[0],10);
	var intMonth = parseInt(arrayDate[1],10); 
	var intYear = parseInt(arrayDate[2],10);
	
	//check for valid month
	if(intMonth > 12 || intMonth < 1) {
		return false;
	}
	
	
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
  
    //check if month value and day value agree
    if(arrayLookup[arrayDate[1]] != null) {
      if(intDay <= arrayLookup[arrayDate[1]] && intDay != 0)
        return true; //found in lookup table, good date
    }
	
    //check for February
	var booLeapYear = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0));
    if( ((booLeapYear && intDay <= 29) || (!booLeapYear && intDay <=28)) && intDay !=0)
      return true; //Feb. had valid number of days
  }
  return false; //any other values, bad date
}

/************************************************
DESCRIPTION: Compares two dates and determines 
    whether one is later or earlier than the 
    other. 
    
PARAMETERS:
   dateFrom - 
   dateTo -
   difference -
   
RETURNS:
   0 - The dates are equal to each other
   1 - The from date is greater
   2 - The to date is greater
   
REMARKS:
   Avoids some of the limitations of the Date.parse()
   method such as the date separator character.
*************************************************/
function compareDates(dateFrom, dateTo, difference) {
	
	var objRegExp = /^\d{1,2}(\/)\d{1,2}\1\d{4}$/
	
	var arrayDateFrom = dateFrom.split(RegExp.$1); //split date into month, day, year
	var intDayFrom = parseInt(arrayDateFrom[0],10);
	var intMonthFrom = parseInt(arrayDateFrom[1],10); 
	var intYearFrom = parseInt(arrayDateFrom[2],10);
	
	var arrayDateTo = dateTo.split(RegExp.$1); //split date into month, day, year
	var intDayTo = parseInt(arrayDateTo[0],10);
	var intMonthTo = parseInt(arrayDateTo[1],10); 
	var intYearTo = parseInt(arrayDateTo[2],10);
	
	if(intYearFrom == intYearTo)
	{
		if(intMonthFrom == intMonthTo)
		{
			if(intDayFrom == intDayTo)
			{
				return 0;
			}
			else if(intDayFrom > intDayTo)
			{
				return 1;
			}
			else if(intDayFrom < intDayTo)
			{
				return 2;
			}
		}
		else if(intMonthFrom > intMonthTo)
		{
			return 1;
		}
		else if(intMonthFrom < intMonthTo)
		{
			return 2;
		}
	}
	else if(intYearFrom > intYearTo)
	{
		return 1;
	}
	else if(intYearFrom < intYearTo)
	{
		return 2;
	}
}

/************************************************
DESCRIPTION: Validates that a string contains no 
  special HTML or XML characters, such as <>&/
  
 PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
   
REMARKS: Currently allows only alphanumeric,
  +, - and 
*************************************************/
function validateNoSpecialChars( strValue) {
	var specialChars = '\"\'<>&'; 
	
	for(i = 0; i < strValue.length; i++)
	{
		if(specialChars.indexOf(strValue.charAt(i)) != -1)
		{
			return false;
		}
	}
	return true;
}

/***********************************************
DESCRIPTION: Validates that the string is a 
	valid time string

PARAMETERS:
	timeStr - The time string

RETURNS:
	true if the string is valid time 
	else false.
************************************************/
function validateTime(timeStr)
{
	// Checks if time is in HH:MM:SS AM/PM format.
	// The seconds and AM/PM are optional.

	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

	var matchArray = timeStr.match(timePat);
	if (matchArray == null)
	{
		return false;
	}
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	ampm = matchArray[6];

	if (second=="") { second = null; }
	if (ampm=="") { ampm = null }

	if (hour < 0  || hour > 23)
	{
		return false;
	}
	if (ampm != null)
	{
		return false;
	}
	if  (hour > 12 && ampm != null)
	{
		return false;
	}
	if (minute<0 || minute > 59)
	{
		return false;
	}
	if (second != null && (second < 0 || second > 59))
	{
		return false;
	}
	return true;
}

/************************************************
DESCRIPTION: Compares Two Times Format for time is
	HH:MM
  
 PARAMETERS:
   time1 - Time 1 to be compared
   time2 - Time 2 to be compared
RETURNS:
	Time difference in seconds
   
REMARKS: Please make sure that the times are of 
	a valid 24 hr Format

*************************************************/


function timeComparison(time1,time2)
{
	return toSeconds(time2) - toSeconds(time1);
}


/************************************************
DESCRIPTION: Returns seconds for a given time
PARAMETERS:
   time - Time  to be converted to seconds
RETURNS:
	time in seconds
*************************************************/
function toSeconds(time)
{
	var hour=0;
	var min=0;
	var seconds;

	hour  	 = time.substring(0,time.indexOf(':'));	
	tmpvalue = time.substring(time.indexOf(':')+1,time.length);
	if (tmpvalue.indexOf(':') != -1)
	{
		min = tmpvalue.substring(0,tmpvalue.indexOf(':'));	
	}
	else
	{ 
		min = tmpvalue;	
	}
	hour  	= trim(hour);
	min 	= trim(min);
	seconds = (hour*60*60)+(min*60);
	return parseInt(seconds,10);
}

function trim(strValue)
{
	
	var sValue = strValue;
	var nLength      = sValue.length;
	var nFirstindex  = 0;
	var nSecondindex = nLength-1;
	while (nFirstindex < nLength && ((sValue.charAt(nFirstindex) == ' ')||(sValue.charAt(nFirstindex)=='\r')||(sValue.charAt(nFirstindex)=='\n')||(sValue.charAt(nFirstindex)=='\f')||(sValue.charAt(nFirstindex)=='\t')||(sValue.charAt(nFirstindex)=='\b')))
	{
		nFirstindex++;
	}
	while (nSecondindex > nFirstindex && sValue.charAt(nSecondindex) == ' ')
	{
		nSecondindex--;
	}
	return sValue.substring(nFirstindex, nSecondindex+1);
}
