/**
 * @author	Library Automation Services Team
 * @version	1.0
 * @copyright	Sarawak Information Systems Sdn. Bhd.
 *
 * Development Environment		: TextPad 4.4.1
 * Name of the Application		: date.js
 * Overview of Application		: Contains javascript functions for date operations
 * Creation/Modification History	:
 *		28-May-2001		Created
 *		28-May-2001		Edited
 * 
 * Function Declaration:
 *		function chkDate(objName, intChange)
 *		function leapYear(intYear)
 *		function dateLessEqual(from, to, intChange)
 *		function dateLess(from, to, intChange)
 *		function formatDate(dateObj)
 *		function getTodayDate()
 *	 	function getLastDay(month, year)
 *		function addDate(date, num, measure)
 *
 *
 */
 
function chkDate(objName, intChange) //intChange=> 0:change value;1:check only;2:return value
{	//var strDatestyle = "US"; //United States date style
	var strDatestyle = "EU";  //European date style
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var intElementNr;
	var err = 0;
	var datefield = objName;
	var strSeparatorArray = new Array("-"," ","/",".");
	var booFound = false;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "JAN";
	strMonthArray[1] = "FEB";
	strMonthArray[2] = "MAR";
	strMonthArray[3] = "APR";
	strMonthArray[4] = "MAY";
	strMonthArray[5] = "JUN";
	strMonthArray[6] = "JUL";
	strMonthArray[7] = "AUG";
	strMonthArray[8] = "SEP";
	strMonthArray[9] = "OCT";
	strMonthArray[10] = "NOV";
	strMonthArray[11] = "DEC";
	strDate = datefield.value;

	//any string present?
	if (strDate.length < 1)
	{	return -1;
	}

	//retrieve or separate the string
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++)
	{	if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1)
		{	strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3)
			{	err = 1;//not complete date
				return err;
			}
			else
			{	strDay = strDateArray[0];
				strMonth = strDateArray[1];
				strYear = strDateArray[2];
			}
			booFound = true;
		}
	}

	//only one continuos string found
	if (booFound == false)
	{	if (strDate.length>5)
		{	strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
		}
		else
		{	err = 1;
			return err;//incomplete date
		}
	}

	//if year only 2 characters
	if (strYear.length == 2)
	{	strYear = '20' + strYear;
	}
	else
	{	if (strYear.length != 4)
		{	err = 4;
			return err;
		}
	}
	// US style; date swap
	if (strDatestyle == "US")
	{	strTemp = strDay;
		strDay = strMonth;
		strMonth = strTemp;
	}

	//
	intday = parseInt(strDay, 10);
	if (isNaN(intday))
	{	err = 2;
		return err;//inproper day input
	}

	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth))//when month not in number format
	{	for (i = 0;i<12;i++)
		{	if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase())
			{	intMonth = i + 1;
				strMonth = strMonthArray[i];
				i = 12;//?
			}
		}
		if (isNaN(intMonth))//see if any successful conversion to number formated month
		{	err = 3;
			return err;//inproper month input
		}
	}

	intYear = parseInt(strYear, 10);
	if (isNaN(intYear))
	{	err = 4;
		return err;//inproper year input
	}

	//all day, month and year in numeric format at this level

	if (intMonth>12 || intMonth<1)
	{	err = 5;
		return err;//invalid month range
	}

	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 	10 || intMonth == 12) && (intday > 31 || intday < 1))
	{	err = 6;
		return err;//invalid day range
	}

	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 	1))
	{	err = 6;
		return err;//invalid day range
	}

	if (intMonth == 2)
	{	if (intday < 1)
		{	err = 6;
			return err;//invalid day range
		}
		if (leapYear(intYear) == true)
		{	if (intday > 29)
			{	err = 6;
				return err;//invalid day range
			}
		}
		else
		{	if (intday > 28)
			{	err = 6;
				return err;//invalid day range
			}
		}
	}

	//done date validating at this level
	
	var temp = String("");
	
	if (intday < 10)
	{	intday = "0" + intday;
	}
	if (strDatestyle == "US")
	{	temp = strMonthArray[intMonth-1] + "-" + intday+"-" + strYear;
	}
	else
	{	temp = intday + "-" + strMonthArray[intMonth-1] + "-" + strYear;
	}
	
	if (intChange == 0)
	{	datefield.value = temp;
		return 0;

	}
	if (intChange == 1)
	{ 	return 0;
	}
	if (intChange == 2)
	{	return temp;
	}

}

function leapYear(intYear)
{	if (intYear % 100 == 0)
	{	if (intYear % 400 == 0) { return true; }
	}
	else
	{	if ((intYear % 4) == 0) { return true; }
	}
	return false;
}

function dateLessEqual(from, to, intChange) //intChange=> 0:change value;1:check only;2:check value not objeect
{	if (intChange != 2)
	{	var f = chkDate(from, intChange);
		var t = chkDate(to, intChange);
		if ((f == 0) && (t == 0))
		{	if ((Date.parse(Replace(from, "-", " "))) <= (Date.parse(Replace(to, "-", " "))))
			{	return true;
			}
			else
			{	return false;
			}
		}
		return false;
	}
	else
	{	if ((Date.parse(ReplaceStr(from, "-", " "))) <= (Date.parse(ReplaceStr(to, "-", " "))))
		{	return true;
		}
		else
		{	return false;
		}
	}
	
	return false;
}

function dateLess(from, to, intChange) //intChange=> 0:change value;1:check only;
{	var f = chkDate(from, intChange);
	var t = chkDate(to, intChange);

	if ((f == 0) && (t == 0))
	{	//alert(Replace(from, "-", " "));
		if ((Date.parse(Replace(from, "-", " "))) < (Date.parse(Replace(to, "-", " "))))
		{	return true;
		}
		else
		{	return false;
		}

	}
	return false;
}

function dateDiff(objBig, objSmall)
{	var intTemp = (parseInt(Date.parse(Replace(objBig,"-"," "))-Date.parse(Replace(objSmall,"-"," ")))+86400000)/86400000;
	if (isNaN(intTemp))
	{	return "";
	}
	return intTemp;
}



var todayDate = new Date();
var strSeparatorArray = new Array("-"," ","/",".");
var strMonthArray = new Array(12);
	strMonthArray[0] = "JAN";
	strMonthArray[1] = "FEB";
	strMonthArray[2] = "MAR";
	strMonthArray[3] = "APR";
	strMonthArray[4] = "MAY";
	strMonthArray[5] = "JUN";
	strMonthArray[6] = "JUL";
	strMonthArray[7] = "AUG";
	strMonthArray[8] = "SEP";
	strMonthArray[9] = "OCT";
	strMonthArray[10] = "NOV";
	strMonthArray[11] = "DEC";
/**
 * Function called to format the date obj into string
 */
function formatDate(dateObj) {
var	day = dateObj.getDate();
var	month = dateObj.getMonth();
var	year = dateObj.getYear();
	return day + "-" + strMonthArray[month] + "-" + year;
}

/** function called to return the last day of the month */
function getLastDay(month, year) {
	if (month == 2) {
		if (leapYear(year)) {return 29;}
		else {return 28;}
	} else if ((month == 4)||(month == 6)||(month == 9)||(month == 11)) {	
		return 30;
	} else if ((month == 1)||(month == 3)||(month == 5)||(month == 7)||(month == 8)||(month == 10)||(month == 12)) {
		return 31;
	}
}

/**
 * Function called to get the today date or system date
 */
function getTodayDate() {
var	day = todayDate.getDate();
var	month = todayDate.getMonth();
var	year = todayDate.getYear();
	if (day < 10) {
		day = "0" + day;
	}
	if (year < 2000) {
		year = year + 1900;
	}
	return day + "-" + strMonthArray[month] + "-" + year;
}

/** function called to calculate the date by days, months or years */
function addDate(date, num, measure) {
var tDate = date;
	if (tDate==""){alert("Please enter a date first!"); return}
		//retrieve or separate the string
	for (var idx = 0; idx < strSeparatorArray.length; idx++) {
		if (tDate.indexOf(strSeparatorArray[idx]) != -1) {
			mDate = tDate.split(strSeparatorArray[idx]);
			if (mDate.length != 3)	{	err = 1; }//not complete date
			else {
				strDay = mDate[0];
				strMonth = mDate[1];
				strYear = mDate[2];
			}
		}
	} 
	m = parseInt(strMonth, 10);
	if (isNaN(m)) {//when month not in number format
		for (i = 0;i<12;i++) {
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase())	{	
				m = i + 1;	strMonth = strMonthArray[i]; i = 12; }
		}
		if (isNaN(m)) {err = 3; return err; }//inproper month input
	}
	if (measure == 'DAY') {
		var lastDay = getLastDay(m, strYear);
		num = parseInt(num) + parseInt(strDay);
		while (parseInt(num) > 0) {
			lastDay = getLastDay(m, strYear);	
			if (parseInt(num) > parseInt(lastDay)) {
				num = num - lastDay;
				m = parseInt(m) + 1;
				if (parseInt(m) > 12) {
					m = parseInt(m) - 12;
					strYear = parseInt(strYear) + 1;
				}
			}	else {strDay = (num>9?num:'0'+num); num = 0; }
		}
	} else if (measure == 'MONTH') {
		num = parseInt(m) + parseInt(num); 
		while (parseInt(num) > 0) {
			if (parseInt(num) > 12) {
				num = parseInt(num) - 12;
				strYear = parseInt(strYear) + 1;
			} else {m = num; num = 0; }
		}
	} else {strYear = parseInt(strYear) + parseInt(num); }
	return strDay + "-" + strMonthArray[m-1] + "-" + strYear;
}

