

var DOMtype = '';
if (document.getElementById) DOMtype = 'isDOM';
if (document.all) DOMtype = 'isIE';
if (document.layers) DOMtype = 'isNS4';
var domObjects = new Array();

function fetchObject(idname) {
	if (typeof(domObjects[idname]) == 'undefined') {
		switch (DOMtype) {
			case 'isDOM': domObjects[idname] = document.getElementById(idname);
			break;
			case 'isIE': domObjects[idname] = document.all[idname];
			break;
			case 'isNS4' : domObjects[idname] = document.layers[idname];
			break;
		}
	}
	return domObjects[idname];
}
function fetchImage(idname) {
	return document.images[idname]
}
function y2k(number) {
	return (number < 1000) ? number + 1900 : number;
}

whichCalendar = 1;

function writeCall(which) {
	which = which + '';
	hider = (which.indexOf('1') != -1) ? '2' : '1';
	if (DOMtype != 'isNS4') document.write('<div onClick="hideMe(' + hider + ',' + which + '); moveLayerTo(\'calendarLayer\',' + which + '); return false;"><div class="calendarBtn"></div></div>');
}

function moveLayerTo(layerName,calBut) {
	element = 'offset' + calBut;
	layerName = layerName + calBut;
	el = fetchObject(element);
	layerName = fetchObject(layerName);
	x = getPageOffsetLeft(el);
	y = getPageOffsetTop(el);
	if (DOMtype == 'isDOM') {
		layerName.style.left = x;
		layerName.style.top = y;
	} else if (DOMtype == 'isIE') {
		layerName.style.pixelLeft = x;
		layerName.style.pixelTop = y;
	}
	layerName.style.visibility = 'visible';
	whichCalendar = calBut;
}

//find x and y coordinates of element
function getPageOffsetLeft(el) {
	var objLeft = el.offsetLeft;
	while ((el = el.offsetParent) != null) objLeft += el.offsetLeft;
	return objLeft;
}
function getPageOffsetTop(el) {
	var objTop = el.offsetTop;
	while((el = el.offsetParent) != null) objTop += el.offsetTop;
	return objTop;
}

function select_dates(dateObj) {
	whichForm = document.quickQuote;
	if (whichCalendar == 1) {
		setSelectBox(whichForm.month1, dateObj.getMonth() + 1);
		setSelectBox(whichForm.date1, dateObj.getDate());
		setSelectBox(whichForm.year1, dateObj.getFullYear());
		updateCal(1);
		updateCal(2);
	} else {
		setSelectBox(whichForm.month2, dateObj.getMonth() + 1);
		setSelectBox(whichForm.date2, dateObj.getDate());
		setSelectBox(whichForm.year2, dateObj.getFullYear());
		updateCal(2);
	}
	for (i = 1; i <= 2; i++) {
		thisCal = fetchObject('calendarLayer' + i)
		thisCal.style.visibility = 'hidden';
	}
}
function setSelectBox(frm, val){
	for(i = 0; i < frm.length; i++) {
		if(frm[i].value == val) {
			frm[i].selected = true;
			break;
		}
	}
}
resort = 1; // set to 1 for walt disney world and 2 for disney land
daysOut = 29;
maximumTripLength = 29;
minimumTripLength = 7;
upperLimit = 365*2;
arrivalDate = false;
departureDate = false;
activeTab = "";
myCalendar1 = new Calendar('myCalendar1');
myCalendar1.font_size = '12px';
myCalendar1.backgroundcolor  = "#FFFFFF";
myCalendar1.weekdayHeaderBackgroundColor = "#626ada";
myCalendar1.dayBackgroundColor = "#8597ff";
myCalendar1.currentDayBackgroundColor = "#F7F944";
myCalendar2 = new Calendar('myCalendar2');
myCalendar2.font_size = myCalendar1.font_size;
myCalendar2.backgroundcolor = myCalendar1.backgroundcolor;
myCalendar2.weekdayHeaderBackgroundColor = myCalendar1.weekdayHeaderBackgroundColor;
myCalendar2.dayBackgroundColor = myCalendar1.dayBackgroundColor;
myCalendar2.currentDayBackgroundColor = myCalendar1.currentDayBackgroundColor;

function datePlus(dateObj, numDays) {
	if (dateObj) return new Date(dateObj.getFullYear(), dateObj.getMonth(), dateObj.getDate() + numDays);
	else return false;
}

function switchToTab(editArrive,editDepart) {
	tabName = 'ARRIVAL';
	if (tabName == 'ARRIVAL') {
		//Prepare calendar for arrival selection
		if (arrivalDate) {
			myCalendar1.selectedDate = arrivalDate;
			myCalendar2.selectedDate = departureDate;
		} else {
			myCalendar1.selectedDate = CALENDAR_CurrentDate;
			myCalendar2.selectedDate = datePlus(CALENDAR_CurrentDate, 7);
		}
		if (departureDate) {
			myCalendar1.upperLimit = upperLimit;
			myCalendar2.upperLimit = upperLimit;
		}
		myCalendar1.lowerLimit = datePlus(CALENDAR_CurrentDate, 0);
		//myCalendar1.upperLimit = datePlus(CALENDAR_CurrentDate, upperLimit);
		myCalendar1.onDateSelect = "selectArrival(this.selectedDate);";
		myCalendar2.lowerLimit = datePlus(CALENDAR_CurrentDate, minLengthOfStay);
		//myCalendar2.upperLimit = myCalendar1.upperLimit;
		myCalendar2.upperLimit = datePlus(CALENDAR_CurrentDate, maxLengthOfStay);
		myCalendar2.onDateSelect = myCalendar1.onDateSelect;
	}
	myCalendar1.renderToLayer('calendarLayer1', 'outerDiv');
	myCalendar2.renderToLayer('calendarLayer2', 'outerDiv');
}

function daysElapsed(date1,date2) {
	difference = Date.UTC(y2k(date2.getYear()),date2.getMonth(),date2.getDate(),0,0,0) - Date.UTC(y2k(date1.getYear()),date1.getMonth(),date1.getDate(),0,0,0);
	return difference/1000/60/60/24;
}

SafeAddOnload(switchToTab);
function selectArrival(d) { select_dates(d); }
function updateCal(type) {
	var form = document.forms['quickQuote'];
	var arrivalMonthVal = form.month1.options[form.month1.selectedIndex].value-1;
	var arrivalYearVal = form.year1.options[form.year1.selectedIndex].value;
	var arrivalDateVal = form.date1.options[form.date1.selectedIndex].value;
	var departureMonthVal = form.month2.options[form.month2.selectedIndex].value-1;
	var departureYearVal = form.year2.options[form.year2.selectedIndex].value;
	var departureDateVal = form.date2.options[form.date2.selectedIndex].value;
	now = new Date(arrivalYearVal,arrivalMonthVal,arrivalDateVal);
	depart	= new Date(arrivalYearVal, departureMonthVal, departureDateVal);
	// daysDiff	= daysElapsed(now, depart)
	temp = new Date()
	if (type == 1) {
		//get date from select box in format of yyyy/mm/dd = javascript specific
		myCalendar1.selectedDate = new Date(arrivalYearVal, arrivalMonthVal, arrivalDateVal);
		myCalendar1.selectMonth(arrivalYearVal, arrivalMonthVal);
		myCalendar1.upperLimit = datePlus(myCalendar1.selectedDate, 29);
	}
	if (type == 2) {
		// This automatically adjusts the departure date
		myCalendar2.selectedDate = new Date(departureYearVal, departureMonthVal, departureDateVal);
		myCalendar2.selectMonth(departureYearVal, departureMonthVal);
		myCalendar2.upperLimit = datePlus(myCalendar2.selectedDate, 29);
		
	}
}
function hideMe(which,whichOpp) {
	obj			= fetchObject('calendarLayer' + which)
	objOpp		= fetchObject('calendarLayer' + whichOpp)
	if (obj && objOpp) {
		obj.style.display		= 'block'
		objOpp.style.display	= 'block'
		if (whichOpp == '1' || whichOpp == '2') obj.style.display	= 'none'
		setTimeout("objOpp.style.display = 'none';",16000);
	}
}

function daysElapsed(date1,date2) {
	difference = Date.UTC(y2k(date2.getYear()),date2.getMonth(),date2.getDate(),0,0,0) - Date.UTC(y2k(date1.getYear()),date1.getMonth(),date1.getDate(),0,0,0);
	return difference/1000/60/60/24;
}