/*
----------- FundSys.net(TM) Copyright SySys(R) Corp 2002 ------------
====================================================================
  Created By: BH
  Last Edited By: BH
  Inception Date: 1/1/2002
  Last Edited Date: 1/1/2002
  Description:  None
  File Dependencies: global.js
                     validate_string.js
										 Manage_date_time.js
										 manage_arrays.js
===================================================================''*/

/*'''---------------------------------
'' isYear(str)
--------------------------------------
   Created By: SySys:bh
   Compatibility: IE6
   Description: checks for a valid year
   Parameters:
     str: string to check
   Returns: returns true if string str is a valid year
   Notes: Must be 2 or 4 digits only. (not BC or 10000 year compliant)
   Dependencies: validate_string.js:isPositiveInteger
----------------------------------'''*/ 
function isYear(str) {
  if (str.length < 1) 
    return false;
  else{ 
    if (!isPositiveInteger(str)) return false;
      return ((str.length == 2) || (str.length == 4));
  }
}

/*'''---------------------------------
'' isLeapYear(str)
--------------------------------------
   Created By: SySys:bh
   Compatibility: IE6
   Description: checks for a leap year
   Parameters:
     str: string to check
   Returns: returns true if year is a leap year
----------------------------------'''*/ 
function isLeapYear(str) {
  return ((str % 4 == 0) && (!(str % 100 == 0) || (str % 400 == 0)));
}


/*'''---------------------------------
'' isLeapYear(str)
--------------------------------------
   Created By: SySys:bh
   Compatibility: IE6
   Description: checks for a valid month number (1-12)
   Parameters:
     str: string to check
   Returns: isMonth returns true if string str is a valid month number between 1 and 12.
   Dependencies: validate_string.js:isIntegerInRange
----------------------------------'''*/ 
function isMonth(str) {
  if ((str.length < 1) || (str.length > 2))
    return false;
  if (str.charAt(0) == 0) 
    return false; 
    return isIntegerInRange (str,1,12);
}

/*'''---------------------------------
'' isLeapYear(str)
--------------------------------------
   Created By: SySys:bh
   Compatibility: IE6
   Description: checks for a valid day number (1-12)
   Parameters:
     str: string to check
   Returns: isMonth returns true if string str is a valid day number between 1 and 31.
   Notes: the function has no context for specific months that have less than 31 days (use isDate)
   Dependencies: validate_string.js:isIntegerInRange
----------------------------------'''*/ 
function isDay(str) {
  if ((str.length < 1) || (str.length > 2))
    return false;
  if (str.charAt(0) == 0) 
    return false;
    return isIntegerInRange(str, 1, 31);
}

/*'''---------------------------------
'' isTime(str)
--------------------------------------
   Created By: SySys:bh
   Compatibility: IE6
   Description: Checks if time is in HH:MM:SS AM/PM format.
   Parameters:
     str: time in HH:MM:SS AM/PM format. (The seconds and AM/PM are optional.)
   Returns: true if valid time
----------------------------------'''*/ 
function isTime(str) {
  var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
  var matcharay = str.match(timePat);
  if (matcharay == null) {
    return false;
  }
  hour = matcharay[1];
  minute = matcharay[2];
  second = matcharay[4];
  ampm = matcharay[6];
  if (second=="") { second = null; }
  if (ampm=="") { ampm = null }
  if (hour < 0  || hour > 23) {
    return false;
  }
  if (hour <= 12 && 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;
}


/*'''---------------------------------
'' isDate(year, month, day)
--------------------------------------
   Created By: SySys:bh
   Compatibility: IE6
   Description: Checks if year, month and day form a valid date
   Parameters:
     year: year string
     month: month string
     day: day string
   Dependencies: Manage_date_time.js:daysInFebruary
   Returns: true if string arguments year, month, and day form a valid date.
----------------------------------'''*/ 
function isDate(year, month, day) {
  // catch invalid years (not 2- or 4-digit) and invalid months and days.
  if (! (isYear(year) && isMonth(month) && isDay(day))) {
    return false;
  } else {
    return true;
  }
  // Explicitly change type to integer to make code work in both
  // JavaScript 1.1 and JavaScript 1.2.
  var intYear = parseInt(year);
  var intMonth = parseInt(month);
  var intDay = parseInt(day);

  // catch invalid days, except for February
  if (intDay > daysInMonth[intMonth]) {
    return false; 
  } else {
    return true;
  }
  if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) {
    return false;
  } else {
    return true;
  }
}

/*'''---------------------------------
'' isDateString(str)
--------------------------------------
   Created By: SySys:bh
   Compatibility: IE6
   Description: Checks if date is valid
   Parameters:
     str: date string to check
   Returns: true if date is valid
            false if blank and allowBlank is true
   Dependencies: manage_arrays.js:split 
                 validate_date_time.js:isDate
                 validate_string.js:isNegativeInteger
   Notes:
     this function is designed to mimic the "date" portion of the
     VBScript IsDate() function, allowing dates to be validated
     with JavaScript in browsers before you run into a problem
     in ASP pages with date database columns or the VBScript
     CDate() function; an exception is that string months
     ("Jan," etc.) are not accepted
     this function does not handle BC dates or dates past 12/31/9999
----------------------------------'''*/ 
function isDateString(str) {
  var theString = new String(str);
  		
  // determine the delimiter character (must be "/" "-" or space)
  var delimitercharacter
  if ( theString.indexOf('/') > 0 )
  	delimitercharacter = '/';
  else
  	if ( theString.indexOf('-') > 0 )
  		delimitercharacter = '-';
  	else
  		if ( theString.indexOf(' ') > 0 )
  			delimitercharacter = ' ';
  		else
  			return false;
  					
  // split the string into an array of tokens
  var theTokens = theString.split(delimitercharacter);
  		
  // there must be either two or three tokens
  if ( theTokens.length < 2 || theTokens.length > 3 )
  	return false;
  		
  // convert the tokens to String objects, which will be needed later,
  // stripping a single leading 0
  var tokenIndex;
  for ( tokenIndex = 0; tokenIndex < theTokens.length; tokenIndex++ ) {
  	theTokens[tokenIndex] = new String(theTokens[tokenIndex])			
  	if ( theTokens[tokenIndex].charAt(0) == '0' )
  		theTokens[tokenIndex] = theTokens[tokenIndex].substring(1, theTokens[tokenIndex].length);
  }

  // all of the tokens must be positive integers
  for ( tokenIndex = 0; tokenIndex < theTokens.length; tokenIndex++ ) {
  	if (isNegativeInteger(theTokens[tokenIndex]) )
  		return false;
  }
  		
  // we need to identify the year, month, and day tokens
  var numericValue;
  var yearTokenIndex = -1;
  var monthTokenIndex = -1;
  var dayTokenIndex = -1;
  for ( tokenIndex = 0; tokenIndex < theTokens.length; tokenIndex++ ) {
  					
  	// convert the value
  	numericValue = parseInt(theTokens[tokenIndex], 10);
  					
  	// could this token be a month?
  	if ( numericValue <= 12 ) {
  					
  		// yes; do we already have a month?
  		if ( monthTokenIndex == -1 ) {
  						
  			// no; assign this token to the month and continue
  			monthTokenIndex = tokenIndex;
  			continue;
  		}
  		else {
  							
  			// we already have a month; this token could
  			// also be the day; do we already have a day?
  			if ( dayTokenIndex == -1 ) {
  						
  				// no; assign this token to the day and continue
  				dayTokenIndex = tokenIndex;
  				continue;
  			}
  			else {
  							
  				// we already have a day; this token could
  				// also be the year; do we alreay have a year?
  				if ( yearTokenIndex == -1 ) {
  						
  					// no; assign this token to the year and continue
  					dayTokenIndex = tokenIndex;
  					continue;
  				}
  			}
  		}
  	}
  	else {
  						
  		// the value is too large for a month;
  		// could this token be a day?
  		if ( numericValue <= 31 ) {
  						
  			// yes; do we already have a day?
  			if ( dayTokenIndex == -1 ) {
  						
  				// no; assign this token to the day and continue
  				dayTokenIndex = tokenIndex;
  				continue;
  			}
  			else {
  							
  				// we already have a day; this token could
  				// also be the year; do we alreay have a year?
  				if ( yearTokenIndex == -1 ) {
  						
  					// no; assign this token to the year and continue
  					dayTokenIndex = tokenIndex;
  					continue;
  				}
  			}
  		}
  		else {
  						
  			// the value is too large for a day;
  			// could this token be a year?
  			if ( numericValue <= 9999 ) {
  					
  				// yes; do we already have a year?
  				if ( yearTokenIndex == -1 ) {
  						
  					// no; assign this token to the year
  					yearTokenIndex = tokenIndex;
  				}
  			}
  		}
  	}
  }	// end of for loop
  		
  // evaluate, based on the number of tokens
  if ( theTokens.length == 2 ) {
  			
  	// two tokens can be either a month/year combination or a month/day
  	// combination with the current year assumed; either way, we must have
  	// a month
  	if ( monthTokenIndex == -1 )
  				
  		// no month
  		return false;
  				
  	// do we have a year?
  	if ( ! (yearTokenIndex == -1) ) {
  			
  		// yes; month/year combination; must be okay
  		return true;
  	}
  	else
  				
  		// no year; do we have a day?
  		if ( ! (dayTokenIndex == -1) ) {
  				
  			// yes; month/day combination; get the current year
  			var today = new Date();
  			var currentYear = today.getYear();

  			// make sure it's a valid date (we were testing days using
  			// 31, and that might be too many for the month)
  			return isDate(currentYear, theTokens[monthTokenIndex], theTokens[dayTokenIndex]);
  		}
  		else
  				
  			// we have neither a year nor a day
  			return false;
  }
  else {
  			
  	// three tokens; we should have found tokens for year, month, and day
  	if ( yearTokenIndex == -1 || monthTokenIndex == -1 || dayTokenIndex == -1 )
  				
  		// missing one or more
  		return false;
  	else
  				
  		// found all; however, VBScript can only handle the following sequences
  		if ( monthTokenIndex == 0 ) {
  				
  			// must be m/d/y
  			if ( dayTokenIndex != 1 || yearTokenIndex != 2)
  				return false;
  		}
  		else
  			if ( dayTokenIndex == 0 ) {
  				
  				// must be d/m/y
  				if ( monthTokenIndex != 1 || yearTokenIndex != 2)
  					return false;
  			}
  			else
  				if ( yearTokenIndex == 0 ) {
  				
  					// must be y/m/d
  					if ( monthTokenIndex != 1 || dayTokenIndex != 2)
  						return false;
  				}
  				else
  						
  					// something is wrong
  					return false;
  				
  		// make sure it's a valid date (we were testing days using a value
  		// of 31, and that might be too many for the actual month)
  		return isDate(theTokens[yearTokenIndex], theTokens[monthTokenIndex], theTokens[dayTokenIndex]);
  }
} // end isDateString(str)



