/*
  this file contains a collection of useful functions for manipulating date and time
	requires the file
	  ../includes/string.js
 */

// return the mm/dd/yyyy format
function dateout(day,month,year){
  if (year == "") year = getcurrentyear()
  return padString(month,2,true,"0") + "/" + padString(day,2,true,"0") + "/" + year
}

// return the yyyy-mm-dd format
function datein(day,month,year){
  if (year == "") year = getcurrentyear();
  return year + "-" + padString(month,2,true,"0") + "-" + padString(day,2,true,"0")
}

// return the hh:mm format
function timeout(hours,minutes){
  return padString(hours,2,true,"0") + ":" + padString(minutes,2,true,"0") 
}

// return the yyyy-mm-dd hh:mm format
function datetimein(day,month,year,hours,minutes){
  return datein(day,month,year) + " " + timeout(hours,minutes)
}

// return the mm/dd/yyyy hh:mm format
function datetimeout(day,month,year,hours,minutes){
  return dateout(day,month,year) + " " + timeout(hours,minutes)
}

function nowOut(){
  var oDate = new Date()
  return datetimeout(oDate.getDate(),oDate.getMonth()+1,oDate.getFullYear(),oDate.getHours(),oDate.getMinutes())
}

// this function returns 1 if (day1,month1,year1) represents an earlier date than (day2,month2,year2)
//                       0 if they represent the same date
//                       -1 if (day1,month1,year1) represents a later date than (day2,month2,year2) 
function datecompare(day1,month1,year1,day2,month2,year2){
  // first compare years
  if (year1 < year2) return 1
  if (year1 > year2) return -1
  // if we are here, then year1 = year2
  // now compare months
  if (month1 < month2) return 1
  if (month1 > month2) return -1
  // if we are here, then month1 = month2 and year1 = year2
  // now compare days
  if (day1 < day2) return 1
  if (day1 > day2) return -1
  // and if we are here, then they must represent the same date
  return 0	
}

// this function returns 1 if time1 represents an earlier time than time2
//                       0 if they represent the same time
//                       -1 if time1 represents a later time than time2
// time1 and time2 are expected to be in the hh:mm format
function timecompare2(time1,time2){
  var time1Arr,time2Arr
	
  time1Arr = time1.split(":")
  time2Arr = time2.split(":")
  return timecompare(time1Arr[0],time1Arr[1],time2Arr[0],time2Arr[1])
}

// this function returns 1 if (hours1,minutes1) represents an earlier time than (hours2,minutes2)
//                       0 if they represent the same time
//                       -1 if (hours1,minutes1) represents a later time than (hours2,minutes2) 
function timecompare(hours1,minutes1,hours2,minutes2){
  // first compare hours
  if (hours1 < hours2) return 1
  if (hours1 > hours2) return -1
  // if we are here, then hours1 = hours2
  // now compare  minutes
  if (minutes1 < minutes2) return 1
  if (minutes1 > minutes2) return -1
  // and if we are here, then they must represent the same time
  return 0	
}

// return the time portion of the dateTime
// dateTime is expected to be in the mm/dd/yyyy hh:mm format
function extractTime(dateTime){
  var dateTimeArr
	
  dateTimeArr = dateTime.split(" ")
  if (dateTimeArr[1])	{
    return dateTimeArr[1]
  } else {
    return ""
  }
}

// return the date portion of the dateTime
// dateTime is expected to be in the mm/dd/yyyy hh:mm format
function extractDate(dateTime){
  var dateTimeArr
	
  dateTimeArr = dateTime.split(" ")
  return dateTimeArr[0]
}

// return true if year is a leap year, false otherwise
function leapYear(year){
  return ( (year % 4 == 0) && ( (year % 100 != 0) || (year % 400 == 0) ) )
}

function getcurrentyear(){
  var oDate
	
  oDate = new Date()
  return oDate.getFullYear()
}

// returns the number of days in a month
// takes leap year into consideration
function daysInMonth(month, year) {
  var ar = new Array(12);
  ar[0] = 31; // January
  ar[1] = (leapYear(year)) ? 29 : 28; // February
  ar[2] = 31; // March
  ar[3] = 30; // April
  ar[4] = 31; // May
  ar[5] = 30; // June
  ar[6] = 31; // July
  ar[7] = 31; // August
  ar[8] = 30; // September
  ar[9] = 31; // October
  ar[10] = 30; // November
  ar[11] = 31; // December
  return ar[month-1];
}

// returns the difference (in days) between two days
function dateDiff(date1,date2){
  var oDate1
  var oDate2
	
  if ( (date1.length == 0) || (date2.length == 0) ) return ""
  
  oDate1 = new Date(date1)
  oDate2 = new Date(date2)
  return Math.floor( (oDate2.getTime() - oDate1.getTime())/ (1000 * 60 * 60 * 24) )
}

// return true if str represents a valid date in mm/dd/yyyy format, false otherwise
function getDateIn(str){
  var oArr
  var oDate
  var year 
  var oArr2
  var time
		
  oDate = new Date(str)
  // if str does not represent a valid date, return thr string back
  if (isNaN(oDate.valueOf())) {
 	 return str
  }

  // otherwise return the date in internal (yyyy/mm/dd) format
  oArr = str.split("/")
  if (typeof(oArr[2]) == "undefined") return str

  // handle the case when we also have time
  oArr2 = oArr[2].split(" ")
  oArr[2] = oArr2[0]
  if (typeof(oArr2[1]) != "undefined") {
    time = oArr2[1]
  } 

  year = parseInt(oArr[2])

  // if invalid year format, then return the string back
  if (isNaN(year)) return str

  if (year < 10) {
	 year = 2000 + year
  } else {
    if (year < 100) year = 1900 + year 
  }	
  
  if (time != "") time = " " + time	

  return year + "/" + padString(oArr[0],2,true,"0") + "/" + padString(oArr[1],2,true,"0")
}

