function IsValidDate(sDate)
{
	var aDateTime, aDateParts = new Array();
	if (sDate.indexOf("/") > -1) {
		aDateTime = sDate.split(" ");	//Split date and time
		aDateParts = aDateTime[0].split("/");

	} else if (sDate.indexOf("-") > -1)	{
	
		aDateTime = sDate.split(" ");	//Split date and time
		aDateParts = aDateTime[0].split("-");
		
		if (parseInt(aDateParts[0], 10) > 100) //format 'yyyy-mm-dd'
		{
			var tmp = aDateParts[0];
			aDateParts[0] = aDateParts[1];
			aDateParts[1] = aDateParts[2];
			aDateParts[2] = tmp;
		}
	}
	if (aDateParts.length == 1 || aDateParts.length == 2)
		return false;

	if (aDateParts.length)
	{
		var sTime;
		if (aDateTime.length > 2)
			sTime = aDateTime[1] + " " + aDateTime[2];
		else if (aDateTime.length > 1)
			sTime = aDateTime[1];
		else
			sTime = "00:00:00";
		if (aDateParts[2].length <= 2)
			aDateParts[2] = (parseInt(aDateParts[2], 10)<70) ? 2000 + parseInt(aDateParts[2], 10) :  1900 + parseInt(aDateParts[2], 10);
		else if (aDateParts[2].length == 3)
			return false;
			
		var oDate = new Date(Date.parse(aDateParts[0] + "/" + aDateParts[1] + "/" + aDateParts[2] + " " + sTime))
		return (oDate.getMonth() + 1 == parseInt(aDateParts[0], 10) && 
				oDate.getDate() == parseInt(aDateParts[1], 10) && 
				oDate.getFullYear()==parseInt(aDateParts[2], 10))
	}
	return (!isNaN(Date.parse(sDate)));
}
function String2Date(sDate)
{
	var aDateTime, aDateParts = new Array();
	if (sDate.indexOf("/") > -1)
	{
		aDateTime = sDate.split(" ");	//Split date and time
		aDateParts = aDateTime[0].split("/");
	}
	else if (sDate.indexOf("-") > -1)
	{
		aDateTime = sDate.split(" ");	//Split date and time
		aDateParts = aDateTime[0].split("-");
		if (parseInt(aDateParts[0], 10) > 100) //format 'yyyy-mm-dd'
		{
			var tmp = aDateParts[0];
			aDateParts[0] = aDateParts[1];
			aDateParts[1] = aDateParts[2];
			aDateParts[2] = tmp;
		}
	}
	if (aDateParts.length == 1 && aDateParts.length == 2)
		return false;

	if (aDateParts.length)
	{
		var sTime;
		if (aDateTime.length > 2)
			sTime = aDateTime[1] + " " + aDateTime[2];
		else if (aDateTime.length > 1)
			sTime = aDateTime[1];
		else
			sTime = "00:00:00";
		if (aDateParts[2].length <= 2)
			aDateParts[2] = (parseInt(aDateParts[2], 10)<70) ? 2000 + parseInt(aDateParts[2], 10) :  1900 + parseInt(aDateParts[2], 10);

		return (new Date(Date.parse(aDateParts[0] + "/" + aDateParts[1] + "/" + aDateParts[2] + " " + sTime)));
	}
	else
		return (new Date(Date.parse(sDate)));
}
function Date2String(oDate, bTime)
{
	var sDate = oDate.getFullYear() + "-";
	if (oDate.getMonth() < 9)
		sDate += "0";
	sDate += (oDate.getMonth() + 1) + "-";
	if (oDate.getDate() < 10)
		sDate += "0";
	sDate += oDate.getDate();
	if (bTime)
	{
		sDate += " ";
		if (oDate.getHours() < 10)
			sDate += "0";
		sDate += oDate.getHours() + ":";
		if (oDate.getMinutes() < 10)
			sDate += "0";
		sDate += oDate.getMinutes() + ":";
		if (oDate.getSeconds() < 10)
			sDate += "0";
		sDate += oDate.getSeconds();
	}
	return sDate;
}
function FormatDate(sDate, bTime) // Independant
{
	var sFormatted = "";
	var oDate = String2Date(sDate);	
	if (!isNaN(oDate))
		sFormatted = Date2String(oDate, bTime);
	return sFormatted;
}
function IsStartBeforeFinish( sDateStart, sDateEnd ) {
	/* previous code --------
	return String2Date(sDateStart) < String2Date(sDateEnd);
	*/
	startArr = sDateStart.split('-');
	endArr = sDateEnd.split('-');
	
	y1 = startArr[2];
	m1 = startArr[1];
	d1 = startArr[0];
	
	y2 = endArr[2];
	m2 = endArr[1];
	d2 = endArr[0];
	
	date1 = new Date(y1,m1,d1);
	date2 = new Date(y2,m2,d2);
	
	if (date1.getTime() <= date2.getTime()) {
		return true;
	} else {
		return false;
	}
}

// Function to check start date or end date should not be less then current date
function IsStartBeforeCurrent( sDateStart, sDateEnd ) // Independant
{
	return String2Date(sDateStart) <= String2Date(sDateEnd);
}

function TDate(sDt)
{
	var sDate = sDt;
	var bDateValid = false;
	var nSign = 0; 
	var valArray;
	if (sDate.toUpperCase().indexOf("T") == -1)
	{
		bDateValid = false;
	}
	else
	{
		if (sDate.indexOf("-") != -1)
		{
			nSign = -1;
			valArray = sDate.split("-");
		}
		else if (sDate.indexOf("+") != -1)
		{
			nSign = 1;
			valArray = sDate.split("+");
		}
		if (nSign != 0)
		{
			if (VB_trim(valArray[0]).toUpperCase() == "T" && !isNaN(valArray[1]))
			{
				var dToday = new Date();
				var nDays = parseInt(valArray[1], 10);

				var nmSec = Date.parse(dToday.toDateString());
				nmSec = nmSec + nDays*24*60*60*nSign*1000;
				sDate = Date2String(new Date(nmSec));
				bDateValid = true;
			}
		}
		else if (VB_trim(sDate).toUpperCase() == "T")
		{
			sDate = Date2String(new Date());
			bDateValid = true
		}
	}
	if (!bDateValid)
		sDate = "######";

	// oItem.value = sDate;
	return bDateValid;
}

function CheckDateInput(sDate) // Independant
{
	var bValid = IsValidDate(sDate);
	if (!bValid)
		return TDate(sDate);

	return bValid;
}
function VB_trim(s)
{
	return String(s).replace( /^\s+/,"").replace(/\s+$/,"");
}

// Function for retrieving total number of days in a given month and year
function GetDaysInMonth(sMonth, nYr)
{
	var nDays
	if ("Jan" == sMonth || "Mar" == sMonth || "May" == sMonth || "Jul" == sMonth || "Aug" == sMonth || "Oct" == sMonth || "Dec" == sMonth)
	{
		nDays = 31;
	}
	else if ("Apr" == sMonth || "Jun" == sMonth || "Sep" == sMonth || "Nov" == sMonth)
	{
		nDays = 30;
	}
	else if ("Feb" == sMonth)
	{
		if (((nYr%4 == 0) && (nYr%100 != 0) ) || (nYr%400==0) ) 
		{ 
			nDays = 29; // Leap Yr
		}
		else 
		{ 
			nDays = 29; // Non-Leap Yr
		}
	}
	
	return nDays;
}