//
// (c) Travel Power Limited 2006-07
//
var maDOW = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"); //LANGUAGE-SPECIFIC
function gTPFORMSToDayOfWeek(inDate)
{
	return(maDOW[inDate.getDay()]);
}

function gTPFORMSToDate(thisDate)
{
	var larr = thisDate.split("-")
	var lDate = new Date(larr[0],larr[1]-1,larr[2]);
	return(lDate);
}

function gTPFORMSNumberOfDays(startDate, endDate)
{
	var diff = new Date();
	var lStartDate = new Date()
	lStartDate = startDate;
	lStartDate.setUTCHours(11,00,00,00);
	
	var lEndDate = new Date()
	lEndDate = endDate;
	lEndDate.setUTCHours(11,00,00,00);
	
	diff.setTime(Math.abs(lEndDate.getTime() - lStartDate.getTime()));

	var timediff = diff.getTime();
	
	var days = Math.floor(timediff / (1000 * 60 * 60 * 24)); 

	//alert(lStartDate + "\n" + lEndDate + "\n" + diff + "\n" + timediff + "\n" + days);
	
	return(days);
}
function gTPFORMSAddToDate(startDate, numDays, numMonths, numYears)
{
	if (startDate == "")
		return("");

	var lStartDate = new Date()
	if (typeof startDate == "string")
		lStartDate = gdValidDateFromString(startDate);
	else
		lStartDate = startDate;

	var yearsToAdd;
	var monthsToAdd;
	var daysToAdd;
			
	lStartDate.setHours(13);
	var returnDate = new Date(lStartDate.getTime());
	returnDate.setHours(13);

	if (numYears== null)
		{yearsToAdd = 0;}
	else
		{yearsToAdd = numYears;}
	if (numMonths== null)
		{monthsToAdd = 0;}
	else
		{monthsToAdd = numMonths;}
	if (numDays== null)
		{daysToAdd= 0;}
	else
		{daysToAdd = numDays;}

	var month = returnDate.getMonth() + monthsToAdd;
	if (month > 11)
	{
		var ExtrayearsToAdd = Math.floor((month+1)/12);
		month -= 12*ExtrayearsToAdd;
		yearsToAdd += ExtrayearsToAdd;
	}
	returnDate.setMonth(month);
	returnDate.setFullYear(returnDate.getFullYear()	+ yearsToAdd);

	returnDate.setTime(returnDate.getTime()+60000*60*24*daysToAdd);
	returnDate.setHours(0);

	return returnDate;
}

var mTPFORMS_LastValue="";

function gTPFORMSStringIsInValidOnSubmit(thisControl, canBeBlank, errorMessage)
{
	mTPFORMS_LastValue = thisControl.value;
	if (mTPFORMS_LastValue == "")
	{
		if (canBeBlank == true) return(false);
		alert(errorMessage);
		
		try {thisControl.focus();}
		catch (er) {}
		
		return(true);
	}
	return(false);
}

function gTPFORMSDaysDropDown(thisDropDown,thisMonth, thisYear)
{
	var lNumberOfDays = gTPFORMSDaysInMonth(thisMonth, thisYear);
	//alert(thisMonth + " " + lNumberOfDays );
	
	//If there are extra items in the combo, remove them
	while( thisDropDown.length > lNumberOfDays )
		thisDropDown.remove(thisDropDown.length - 1);
	//If there are missing items in the combo, add them
	while( thisDropDown.length < lNumberOfDays )
		thisDropDown[thisDropDown.length] = new Option(thisDropDown.length + 1,thisDropDown.length + 1);		
}		

function gTPFORMSSetDropDownLength(thisDropDown,thisLength)
{
	//If there are extra items in the combo, remove them
	while( thisDropDown.length > thisLength)
		thisDropDown.remove(thisDropDown.length - 1);
	//If there are missing items in the combo, add them
	while( thisDropDown.length < thisLength)
		thisDropDown[thisDropDown.length] = new Option(thisDropDown.length + 1,thisDropDown.length + 1);		
}

function gTPFORMSDaysInMonth(thisMonth, thisYear)
{
		if (thisMonth==4 || thisMonth==6 || thisMonth==9 || thisMonth==11)
		{	
			return(30);
		}
		if (thisMonth==2)
		{
			return(gTPFORMSdaysInFebruary(thisYear));
		}
		return(31);
}
function gTPFORMSdaysInFebruary (thisYear){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((thisYear % 4 == 0) && ( (!(thisYear % 100 == 0)) || (thisYear % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) 
	{
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
	} 
   	return this
}

function gTPFORMSIsDate(thisDate){
	var daysInMonth = DaysArray(12)

	month=thisDate.getMonth()+1;
	day=thisDate.getDay();
	year=thisDate.getFullYear();

	if (month<1 || month>12)
	{
		return false
	}
	if (day<1 || day>31 || (month==2 && day>gTPFORMSdaysInFebruary(year)) || day > daysInMonth[month])
	{
		return false
	}
	return true
}
