
//------------------------------------------------------------------------------------
// function: isWhiteSpace
//           Function to check whether the given argument consists of charactes other
//           than a space and \t
//------------------------------------------------------------------------------------
function isWhiteSpace(argWhiteSpace) {
	argWs = argWhiteSpace.toString()
	
	for (var intI=0; intI < argWs.length; intI++)
		if (argWs.charAt(intI) != ' ' && argWs.charAt(intI) != '\t')
			return false
	
	return true
}

//------------------------------------------------------------------------------------
// function: isLeapYear
//           Function to tell, whether the given year is leap year or not
//------------------------------------------------------------------------------------
function isLeapYear(argYear) {
	return ((argYear % 4 == 0) && (argYear % 100 != 0)) || (argYear % 400 == 0) 
}

//------------------------------------------------------------------------------------
// function: daysInMonth
//           Function to return the maximum number of days in a given month of a
//           given year
//------------------------------------------------------------------------------------
function daysInMonth(argMonth, argYear) {
	switch (Number(argMonth)) {
		case 1:		// Jan
		case 3:		// Mar
		case 5:		// May
		case 7:		// Jul
		case 8:		// Aug
		case 10:		// Oct
		case 12:		// Dec
			return 31;
			break;
		
		case 4:		// Apr
		case 6:		// Jun
		case 9:		// Sep
		case 11:		// Nov
			return 30;
			break;
		
		case 2:		// Feb
			if (isLeapYear(argYear))
				return 29
			else
				return 28
			break;
		
		default:
			return 0;
	}
}

//------------------------------------------------------------------------------------
// function: getDateSeparator
//           Function to return the date separator
//           This function expects date in the format of mm/dd/yyyy or mm/dd/yy
//           or mm-dd-yyyy or mm-dd-yy
//------------------------------------------------------------------------------------
function getDateSeparator(argDate) {
	// Are there invalid separators?
	if ((argDate.indexOf('-') > 0) && (argDate.indexOf('/') > 0))
		return ' '

	if (argDate.indexOf('-') > 0)
		return '-'
	else
		if (argDate.indexOf('/') > 0)
			return '/'
		else
			return ' '
}

//------------------------------------------------------------------------------------
// function: getYear
//           Function to return the year part of the given date.
//           This function expects date in the format of mm/dd/yyyy or mm/dd/yy
//           or mm-dd-yyyy or mm-dd-yy
//------------------------------------------------------------------------------------
function getYear(argDate) {
	var dateSep = getDateSeparator(argDate)
	
	if (dateSep == ' ')
		return 0

	if(argDate.split(dateSep).length == 3)
		return argDate.split(dateSep)[2]
	else
		return 0
}

//------------------------------------------------------------------------------------
// function: getMonth
//           Function to return the month part of the given date.
//           This function expects date in the format of mm/dd/yyyy or mm/dd/yy
//           or mm-dd-yyyy or mm-dd-yy
//------------------------------------------------------------------------------------
function getMonth(argDate) {
	var dateSep = getDateSeparator(argDate)
	
	if (dateSep == ' ')
		return 0

	if(argDate.split(dateSep).length == 3)
		return argDate.split(dateSep)[0]
	else
		return 0
}

//------------------------------------------------------------------------------------
// function: getDay
//           Function to return the day part of the given date.
//           This function expects date in the format of mm/dd/yyyy or mm/dd/yy
//           or mm-dd-yyyy or mm-dd-yy
//------------------------------------------------------------------------------------
function getDay(argDate) {
	var dateSep = getDateSeparator(argDate)
	
	if (dateSep == ' ')
		return 0

	if(argDate.split(dateSep).length == 3)
		return argDate.split(dateSep)[1]
	else
		return 0
}

//------------------------------------------------------------------------------------
// function: isProperDay
//           Function to tell whether the given day of the given month is valid
//------------------------------------------------------------------------------------
function isProperDay(argDay, argMonth, argYear) {
	if ((isWhiteSpace(argDay)) || (argDay == 0))
		return false

	if ((argDay > 0) && (argDay < daysInMonth(argMonth, argYear) + 1))
		return true
	else 
		return false
}

//------------------------------------------------------------------------------------
// function: isProperMonth
//           Function to tell whether the given month is a valid one
//------------------------------------------------------------------------------------
function isProperMonth(argMonth) {
	if ((isWhiteSpace(argMonth)) || (argMonth == 0))
		return false

	if ((argMonth > 0) && (argMonth < 13))
		return true
	else
		return false
}

//------------------------------------------------------------------------------------
// function: isProperYear
//           Function to tell whether the given Year is a valid one
//------------------------------------------------------------------------------------
function isProperYear(argYear) {
	if ((isWhiteSpace(argYear)) || (argYear.toString().length > 4) || (argYear.toString().length == 3))
		return false

	switch (argYear.toString().length) {
		case 1:
			if (argYear >=0 && argYear < 10)
				return true
			else
				return false
			
		case 2:
			if (argYear >=0 && argYear < 100)
				return true
			else
				return false
			
		case 4:
			if (((argYear >=1900) || (argYear >=2000)) && ((argYear < 3000) || (argYear < 2000)))
				return true
			else
				return false
		
		default:
			return false
	}
}

//------------------------------------------------------------------------------------
// function: isProperUSDate
//           Function to tell whether the given date is valid or not
//           This function expects date in the format of mm/dd/yyyy or mm/dd/yy
//           or mm-dd-yyyy or mm-dd-yy
//------------------------------------------------------------------------------------
function isProperUSDate(argDate) {
	var tmpDay = getDay(argDate)
	var tmpMon = getMonth(argDate)
	var tmpYear = getYear(argDate)
	return isProperDay(tmpDay, tmpMon, tmpYear) && isProperMonth(tmpMon) && isProperYear(tmpYear)
}

//--------------------------------------------------------------------------
// function: isProperDate
//	     Function to tell wheter the given date is valid or not
//           This function receives a date in Brazilian format (dd/mm/yyyy), translates
//           it to US format and calls isProperUSDate
//--------------------------------------------------------------------------
function isProperDate(argDate) {
	return isProperUSDate(getDay(argDate) + "/" + getMonth(argDate) + "/" + getYear(argDate));
}


//--------------------------------------------------------------------------
// function: isProperSiclidDate
//	     Function to tell wheter the given date is valid or not
//           This function receives a date in Siclid format (yyyymmdd), translates
//           it to US format and calls isProperUSDate
//--------------------------------------------------------------------------
function isProperSiclidDate(argDate) {
	return isProperUSDate(argDate.substring(4,5) + "/" + argDate.substring(6,7) + "/" + argDate.substring(0,3));
}


//--------------------------------
//
//
//    Expects date in format dd/mm/yyyy, so need to swap getDay and getMonth
//--------------------------------
function convDateToSiclid(argDate) {
	if ( isProperDate(argDate) )
		return (getYear(argDate) + getDay(argDate) + getMonth(argDate));
	else
		return "";
}


//--------------------------------
//
//
//    Expects date in format YYYYMMDD
//--------------------------------
function convSiclidToDate(argDate) {
	if ( isProperSiclidDate(argDate) )
		return (argDate.substring(6,7) + "/" + argDate.substring(4,5) + "/" + argDate.substring(0,3))
	else
		return false;

}

//--------------------------------
//
//
// adiciona o separador de data para o formato DD/MM/YYYY, pois algumas funcoes necessitam do mesmo.
//--------------------------------
function insertSeparator(argDate) {
      return ( argDate.substring(0,2) + "/" + argDate.substring(2,4) + "/" + argDate.substring(4,8) );
}


//--------------------------------
//
// Returns:
//	 days between argDate2 and argDate1
//--------------------------------
function elapsedDays(argDate1, argDate2) {
	var aux1 = dateToUTC(argDate1);
	var aux2 = dateToUTC(argDate2);
	return ( (aux2 - aux1)/(1000*24*60*60) );
}

//--------------------------------
//
// Returns:
//	 0, if date1 == date2;
//	 1, if date1 > date2;
//       2, if date2 > date1;
//--------------------------------
function compareDates(argDate1, argDate2) {
	var aux = elapsedDays(argDate1, argDate2);

	if (aux==0)
           return 0;
	else if (aux < 0)
	   return 1;
	else if (aux > 0)
	   return 2;
}


//--------------------------------
//
// Returns:
//	 days between argDate2 and argDate1
//--------------------------------
function dateToUTC(argDate) {
	if (isProperDate(argDate)) {
		return ( Date.UTC(getYear(argDate),getDay(argDate)-1, getMonth(argDate)) );
	} else {
		return ( NaN );
	}
}


//--------------------------------
//
// Returns:
//	 Add days in argDate
//--------------------------------
function addDaysInDate(argDate, days) {
	var tmpDate;
	if (isProperDate(argDate)) {
		days = days * 24 * 60 * 60 * 1000;
		tmpDate = days + Date.UTC(getYear(argDate),getDay(argDate)-1, getMonth(argDate)) ;
		return ( new Date(tmpDate) );
	} else {
		return ( NaN );
	}
}


//--------------------------------
//
// Returns:
//	 Converte um objeto date para o formato "Proper" (ddmmaaaa)
//--------------------------------
function convDateToProperDate(argDate)
{
	var day = "";
	var month = "";
	var year = "";
	var properDate = "";

	if ( argDate.getDate() < 10 )
	{
		day += "0";
	}
	day += argDate.getDate();

	if ( ( argDate.getMonth() + 1 ) < 10 )
	{
		month += "0";
	}
	month += (argDate.getMonth() + 1);

	year += argDate.getFullYear();

	properDate += day + month + year;

	return ( properDate );
}
