//Called by quick selector to set both date selectors

// set the vars - so other elements on the page not related to the date picker can call these events too
var _text1Id;
var _monthYear1Id;
var _day1Id;

var _text2Id;
var _monthYear2Id;
var _day2Id;

var _daysSelectId;

function SetID(text1Id, monthYear1Id, day1Id, text2Id, monthYear2Id, day2Id, daysSelectId)
{
	_text1Id = text1Id;
	_monthYear1Id = monthYear1Id;
	_day1Id = day1Id;
	_text2Id = text2Id;
	_monthYear2Id = monthYear2Id;
	_day2Id = day2Id;
	_daysSelectId = daysSelectId;

}

function doQuickSelect(selectStatement, text1Id, monthYear1Id, day1Id, text2Id, monthYear2Id, day2Id) {
	
	if (selectStatement != "") {
		var a = selectStatement.split(":");
		var date1 = a[0];
		var date2 = a[1];
	
		//Calls function in DatePicker to set first date selector
		setControlFromDate(date1, text1Id, monthYear1Id, day1Id);
	
		//Calls function in DatePicker to set second date selector
		setControlFromDate(date2, text2Id, monthYear2Id, day2Id);

		return;
	}	
}

//Ensures the end date is not before the start date
function ensureSpan(startIsChanging, text1Id, monthYear1Id, day1Id, text2Id, monthYear2Id, day2Id, daysSelectId) {
	
	var textBox1 = document.getElementById(text1Id);
	var textBox2 = document.getElementById(text2Id);
	var daysSelect = document.getElementById(daysSelectId);
	
	//var parsedDate1 = Date.parse(textBox1.value);
	//var parsedDate2 = Date.parse(textBox2.value);
	var parsedToday = Date.parse(new Date());
	
	var date1 = GetDateValue(textBox1.value);
	var date2 = GetDateValue(textBox2.value);
	
	var numDays = 0;
	var bNumDaysPresent = false;

/*	
	// worry abt this validation later - since htis control is used all over
	if (parsedDate1 < parsedToday)
	{
		alert("Arrival Date cannot be before today.");
		textBox1.value = new Date();
		setControlFromDate(textBox2.value, text2Id, monthYear2Id, day2Id);
		return;
	}
	else if (parsedDate2 < parsedToday)
	{
		alert("Departure Date cannot be before today.");
		SetDate2(parsedDate1, numDays, textBox2, text2Id, monthYear2Id, day2Id)
		return;
	}
*/	
	if (null != daysSelect)
	{
		numDays = daysSelect.options[daysSelect.selectedIndex].value;
		numDays = numDays - 0;
		bNumDaysPresent = true;
	}
			
	/*if (startIsChanging) {
		SetDate2(parsedDate1, numDays, textBox2)
	} else {
			date1 = new Date(parsedDate1);
			date2 = new Date(parsedDate2);
			daysSelect.value = date2.getDate() - date1.getDate();
	}*/
	
	if (startIsChanging) 
	{
		//The StartDay is changing, so:
		// 1. If the NumDays is present, then:
		//		a. we change EndDate to be StartDate+NumDays.
		// 2. If the NumDays is not present, then:
		//		a. If the EndDate is equal or later than the StartDate, then we leave it alone.
		//		b. If the EndDate is before the StartDate, then we change to be equal to StartDate.
		
		if (bNumDaysPresent)
		{
			date2 = AddDays(date1, numDays);
			SetDate2(date2, textBox2, text2Id, monthYear2Id, day2Id);
		}
		else
		{
			if (date1 > date2)
			{
				date2 = AddDays(date1, numDays);
				SetDate2(date2, textBox2, text2Id, monthYear2Id, day2Id);
			}
		}
		
		
	} 
	else 
	{
		//The EndDay is changing, so:
		// 1. If the NumDays is present, then:
		//		a. If the StartDate is before than the EndDate, then we update NumDays with the difference between the two dates. 
		//		b. If the StartDate is later or equal to the EndDate, then we change StartDate to be EndDate-NumDays.
		// 2. If the NumDays is not present, then:
		//		a. If the StartDate is equal or before than the EndDate, then we leave it alone.
		//		b. If the StartDate is after the EndDate, then we change to be equal to EndDate.
		
		if (bNumDaysPresent)
		{
			if (date2 > date1)
			{
				var diffDate = new Date();
				diffDate.setTime(date2.getTime() - date1.getTime());
				dDiff = diffDate.getTime();
				dDiff = Math.floor(dDiff / (1000 * 60 * 60 * 24)); 

				if (dDiff > 90)
					dDiff = 90;
				
				daysSelect.value = dDiff;		
			}
			else
			{
				date1 = AddDays(date2, 0-numDays);
				setControlFromDate(date1, text1Id, monthYear1Id, day1Id);

			}
		}
		else
		{
			if (date2 < date1)
			{
				date1 = AddDays(date2, 0-numDays);
				setControlFromDate(date1, text1Id, monthYear1Id, day1Id);
			}
		}
		
	}
}

function AddDays(oldDate,days) 
{
    var newDate = new Date(oldDate.getFullYear(),oldDate.getMonth(),oldDate.getDate()+days);
    return newDate;
}

function SetDate2(date2, textBox2, text2Id, monthYear2Id, day2Id)
{
	setControlFromDate(date2, text2Id, monthYear2Id, day2Id);
}

function SetControlFromNumDays(text1Id, monthYear1Id, day1Id, text2Id, monthYear2Id, day2Id, daysSelectId)
{
	var daysSelect = document.getElementById(daysSelectId);
	if (null == daysSelect)
		return;
	
	var numDays = 0;
	
	numDays = daysSelect.options[daysSelect.selectedIndex].value;
	numDays = numDays - 0;
	
	var textBox1 = document.getElementById(text1Id);
	var date1 = GetDateValue(textBox1.value);
	var date2 = AddDays(date1,numDays);

	setControlFromDate(date2, text2Id, monthYear2Id, day2Id);
}

function SetControlFromCalendar2(dateYear, dateMonth, dateDay, textClientId, monthYearClientId, dayClientId)
{
	// right now this is a hack .. using predefined values;
	var date1 = new Date(dateYear, dateMonth-1, dateDay);
	setControlFromDate(date1, textClientId, monthYearClientId, dayClientId);
}

function SetControlFromCalendar(dateYear, dateMonth, dateDay)
{
	// right now this is a hack .. using predefined values;
	var date1 = new Date(dateYear, dateMonth-1, dateDay);

	setControlFromDate(date1, _text1Id, _monthYear1Id, _day1Id);
	ensureSpan(true,  _text1Id, _monthYear1Id, _day1Id, _text2Id, _monthYear2Id, _day2Id, _daysSelectId)
}

function ToggleDatePicker(number, hide)
{
	var picker = document.getElementById("DatePicker" + number);
	
	picker.style.display = (hide)?("none"):("block");

}


