function IsDate(asDate){
	var sDate = asDate, sDateArray, sDate2, chkDate, cmpDate

	if(sDate.length < 8)
		return false
	
	//This only works for dates with 4 digit year	
	sDate = sDate.substr(0, sDate.length - 4) + sDate.substr(sDate.length - 2, 2)
	
	if (sDate.indexOf("-", 0) != -1)
		sDateArray = sDate.split("-")
	else
		sDateArray = sDate.split("/")

	chkDate = new Date(Date.parse(sDate))	
	cmpDate = (chkDate.getMonth() + 1) + "/" + (chkDate.getDate()) + "/" + (chkDate.getYear())
	sDate2 = (Math.abs(sDateArray[0])) + "/" + (Math.abs(sDateArray[1])) + "/" + (Math.abs(sDateArray[2]))
	
	if (sDate2 != cmpDate)
		return false
	else {
	    if(cmpDate == "NaN/NaN/NaN")
			return false
		else
			return true
	}
}

function get4DigitYear(asDate) {
	var sDate = asDate

	if(parseInt(sDate.substr(sDate.length - 2, 2)) < 10 && sDate.length == 8)
		sDate = sDate.substr(0, sDate.length - 2) + '20' + sDate.substr(sDate.length - 2, 2)
	else if(parseInt(sDate.substr(sDate.length - 2, 2)) > 90 && sDate.length == 8)
		sDate = sDate.substr(0, sDate.length - 2) + '19' + sDate.substr(sDate.length - 2, 2)
	
	return sDate
}

function formatDate(aoItem, aiYearSize) {
	//Take the Date and Format like 01/01/2002
	var sVal = aoItem.value, bRet = false
	
	if(aiYearSize == undefined) aiYearSize = 4

	iPos1 = sVal.indexOf("/")
	if(iPos1 > 0) {
		sMonth = sVal.substr(0, iPos1)
		sVal = sVal.substr(iPos1 + 1)
		iPos2 = sVal.indexOf("/")
		if(iPos2 > 0) {
			sDay = sVal.substr(0, iPos2)
			sVal = sVal.substr(iPos2 + 1)
			sYear = sVal
			
			if(sDay.length == 1)
				sDay = "0" + sDay
			if(sMonth.length == 1)
				sMonth = "0" + sMonth
			if(sYear.length == 1)
				sYear = "0" + sYear
				
			if(aiYearSize==4)
				aoItem.value = get4DigitYear(sMonth + '/' + sDay + '/' + sYear)			
			else
				aoItem.value = sMonth + '/' + sDay + '/' + sYear
		}
	}
}

function dateAdd(start, interval, number){
  // Create 3 error messages, 1 for each argument. 
  var startMsg = "Sorry the start parameter of the dateAdd function\n"
      startMsg += "must be a valid date format.\n\n"
      startMsg += "Please try again." ;
		
  var intervalMsg = "Sorry the dateAdd function only accepts\n"
      intervalMsg += "d, h, m OR s intervals.\n\n"
      intervalMsg += "Please try again." ;

  var numberMsg = "Sorry the number parameter of the dateAdd function\n"
      numberMsg += "must be numeric.\n\n"
      numberMsg += "Please try again." ;
		
  // get the milliseconds for this Date object. 
  var buffer = Date.parse( start ) ;
	
  // check that the start parameter is a valid Date. 
  if ( isNaN (buffer) ) {
      alert( startMsg ) ;
      return null ;
  }
	
  // check that an interval parameter was not numeric. 
  if ( interval.charAt == 'undefined' ) {
      // the user specified an incorrect interval, handle the error. 
      alert( intervalMsg ) ;
      return null ;
  }

  // check that the number parameter is numeric. 
  if ( isNaN ( number ) )	{
      alert( numberMsg ) ;
      return null ;
  }

  // so far, so good...
  //
  // what kind of add to do? 
  switch (interval.charAt(0))
  {
      case 'd': case 'D': 
          number *= 24 ; // days to hours
          // fall through! 
      case 'h': case 'H':
          number *= 60 ; // hours to minutes
          // fall through! 
      case 'm': case 'M':
          number *= 60 ; // minutes to seconds
          // fall through! 
      case 's': case 'S':
          number *= 1000 ; // seconds to milliseconds
          break ;
      default:
      // If we get to here then the interval parameter
      // didn't meet the d,h,m,s criteria.  Handle
      // the error. 		
      alert(intervalMsg) ;
      return null ;
  }
  return new Date( buffer + number ) ;
}

