var bookingSendOptionalParamters = false;

function doBookingSearch() {
	
	if (!validateDateField('bookingWidget', 'arrive', 'Check-in date')) {
		return false;
	}
	
	if (!validateDateField('bookingWidget', 'depart', 'Check-out date')) {
		return false;
	}
	
	var date1 = document.bookingWidget.arrive.value;
	var date2 = document.bookingWidget.depart.value;
	var island = document.bookingWidget.destinationAreaId.value;
	var propertyGroupId = document.bookingWidget.propertyGroupId.value;
	var dest = '';
	
	//saveBookingParams();
	
	if (island == 41) {
		dest = 'OAHU';
	} else if (island == 42) {
		dest = 'MAUI';
	} else if (island == 43) {
		dest = 'KAUAI';
	} else if (island == 44) {
		dest = 'HAWAII';
        } else if (island == 11) {
		dest = "CA";
	} else if (island == 22) {
		dest = "ID";
	} else if (island == 66) {
		dest = "NV";
	} else if (island == 171) {
		dest = "HONLY";
	} else {
		alert('Please choose a Destination.');
		return false;
	}

	if (new Date(date1).getTime() < new Date(date2).getTime()) {
		var SynXis = '?chain=' + document.bookingWidget.Chain.value
				+ '&propertyGroupId=' + propertyGroupId
				+ '&arrive=' + document.bookingWidget.arrive.value
				+ '&depart=' + document.bookingWidget.depart.value
				+ '&adult=' + document.bookingWidget.adult.value
				+ '&child=' + document.bookingWidget.children.value
				+ '&src=' + document.bookingWidget.Src.value
				+ '&shell=' + document.bookingWidget.shell.value
				+ '&template=' + document.bookingWidget.template.value
				+ '&dest=' + dest;
				
		if (bookingSendOptionalParamters) {
			var promo = document.bookingWidget.promo.value;
			if (promo.length > 0) {
				SynXis += '&promo=' + promo;	
			}
			var iata = document.bookingWidget.iata.value;
			if (iata.length > 0) {
				SynXis += '&iata=' + iata;	
			}
		}
		if ((island == 11) || (island == 22) || (island == 66)) {
			SynXis = 'http://www.astonhotels.com/multiBook.php' + SynXis;
		} else {
			SynXis = contextPath + '/book.jsp' + SynXis;
		}
		location.href = (SynXis);
	} else {
		alert('Please enter an arrival date prior to departure');
	}
	return false;
}

function bookindLoadResorts(destinationName) {
	var destinationValue = destinationName.options[destinationName.selectedIndex].value;
	var names = bookingWidgetResortNames[destinationValue];
	var ids = bookingWidgetResortIds[destinationValue];
	while (document.bookingWidget.propertyGroupId.length != 0) {
		document.bookingWidget.propertyGroupId.options[
				document.bookingWidget.propertyGroupId.length - 1] = null;
	}
	var options = document.bookingWidget.propertyGroupId.options;
	options[options.length] = new Option("Select Hotel or Condominium", "");
	options[options.length] = new Option("--Compare All--", "");
	for (var i = 0; i < names.length; ++i) {
		options[options.length] = new Option(names[i], ids[i]);
	}
}

function bookingSelectResort(areaId, propertyGroupId) {
	
	if (isUndefined(areaId)) {
		areaId = getCookie('bookingAreaId');
		if (areaId == null || areaId == '') {
			areaId = undefined;
		}
	}
	
	if (isUndefined(propertyGroupId)) {
		propertyGroupId = getCookie('bookingPropertyGroupId');
		if (propertyGroupId == null || propertyGroupId == '') {
			propertyGroupId = undefined;
		}
	}
	
	if (isUndefined(areaId)) {
		return;
	}
	var sel = document.bookingWidget.destinationAreaId;
	var selIndex = -1;
	var opts = sel.options;
	for (var i = 0; i < opts.length; i++) {
		if (opts[i].value == areaId) {
			selIndex = i;
			break;
		}
	}
	if (selIndex < 0) {
		return;	
	}
	sel.selectedIndex = selIndex;
	bookindLoadResorts(sel);
	
	if (isUndefined(propertyGroupId)) {
		return;
	}
	sel = document.bookingWidget.propertyGroupId;
	selIndex = -1;
	opts = sel.options;
	for (var i = 0; i < opts.length; i++) {
		if (opts[i].value == propertyGroupId) {
			selIndex = i;
			break;
		}
	}
	if (selIndex >= 0) {
		sel.selectedIndex = selIndex;	
	}
}

function setBookingDefaults() {
	
	if (isUndefined(bookingLOS)) {
		bookingLOS = 1;
	}
	
	var now = new Date();
	var arriveDate = getCookie('bookingArriveDate');
	if (isUndefined(arriveDate) || arriveDate == null || arriveDate == '' || new Date(arriveDate) < now) {
		arriveDate = formatShortDate(now);
	}
	document.bookingWidget.arrive.value=arriveDate;
	setBookingDayLabel("arriveDay", new Date(arriveDate));
	
	var departDate = getCookie('bookingDepartDate');
	if (isUndefined(departDate) || departDate == null || departDate == '' || new Date(departDate) < new Date(arriveDate)) {
		departDate = formatShortDate(new Date(new Date().getTime() + 5*24*60*60*1000*bookingLOS));	
	}
	document.bookingWidget.depart.value=departDate;
	setBookingDayLabel("departDay", new Date(departDate));

	var selects = [document.bookingWidget.adult, document.bookingWidget.children];
	var vars = ['bookingAdults', 'bookingChildren'];
	for (var i = 0; i < selects.length; i++) {
		var val = getCookie(vars[i]);
		if (!isUndefined(val) && val != null && val != '') {
			var selIndex = -1;
			var opts = selects[i].options;
			for (var j = 0; j < opts.length; j++) {
				if (opts[j].value == val) {
					selIndex = j;
					break;
				}
			}
			if (selIndex >= 0) {
				selects[i].selectedIndex = selIndex;	
			}
		}
	}
	
	var show = false;
	
	var promo = getCookie('bookingPromo');
	if (!isUndefined(promo) && promo != null && promo != '') {
		document.bookingWidget.promo.value = promo;
		show = true;
	}
	
	if (!isUndefined(bookingPromo)) {
		document.bookingWidget.promo.value = bookingPromo;
		show = true;
	}
	
	var iata = getCookie('bookingIata');
	if (!isUndefined(iata) && iata != null && iata != '') {
		document.bookingWidget.iata.value = iata;
		show = true;
	}
	
	if (!isUndefined(bookingIata)) {
		document.bookingWidget.iata.value = bookingIata;
		show = true;
	}
	
	if (show) {
		bookingShowOptionalInputs(true);
	}
}

function saveBookingParams() {
	var expires = undefined;
	var path = '/';
	var domain = undefined;
	var secure = undefined;
	setCookie('bookingAreaId', document.bookingWidget.destinationAreaId.value, expires, path, domain, secure);
	setCookie('bookingPropertyGroupId', document.bookingWidget.propertyGroupId.value, expires, path, domain, secure);
	setCookie('bookingArriveDate', document.bookingWidget.arrive.value, expires, path, domain, secure);
	setCookie('bookingDepartDate', document.bookingWidget.depart.value, expires, path, domain, secure);
	setCookie('bookingAdults', document.bookingWidget.adult.value, expires, path, domain, secure);
	setCookie('bookingChildren', document.bookingWidget.children.value, expires, path, domain, secure);
	setCookie('bookingPromo', isUndefined(bookingPromo) || document.bookingWidget.promo.value != bookingPromo ? document.bookingWidget.promo.value : '', expires, path, domain, secure);
	setCookie('bookingIata', isUndefined(bookingIata) || document.bookingWidget.iata.value != bookingIata ? document.bookingWidget.iata.value : '', expires, path, domain, secure);
	setCookie('bookingIata', document.bookingWidget.iata.value, expires, path, domain, secure);
}

function doAirInclusivePackages(a) {
	var date1 = document.bookingWidget.arrive.value;
	var date2 = document.bookingWidget.depart.value;
	var adult = document.bookingWidget.adult.value;
	var url = a.href;
	var pos = url.indexOf("&numadult=2"); 
	if (pos > 0) {
		url = url.substring(0, pos) + url.substring(pos + 11);
		url += "&numadult=" + adult;
		url += "&fromdate=" + date1;
		url += "&todate=" + date2;
	}
	location.href = (url);
	return false;
}

var bookingOptionalInputsAnim = null;

function bookingShowOptionalInputs(show) {
	
	var optionalInputs = document.getElementById('bookingOptionalInputs');
	
	if (bookingOptionalInputsAnim == null) {
		bookingOptionalInputsAnim = new YAHOO.util.Anim(optionalInputs);
		bookingOptionalInputsAnim.attributes.height = {to: 0};
		bookingOptionalInputsAnim.attributes.opacity = {to: 0};
		bookingOptionalInputsAnim.duration = 0.5;
		bookingOptionalInputsAnim.method = YAHOO.util.Easing.easeOut;
	    var style = optionalInputs.style;
	    var originalVisibility = style.visibility;
	    var originalPosition = style.position;
	    style.visibility = 'hidden';
	    style.position = 'absolute';
	    style.display = '';
   		bookingOptionalInputsAnim.originalHeight = optionalInputs.clientHeight;
		style.height = '1px';
		style.opacity = '0';
	    style.display = 'block';
	    style.position = originalPosition;
	    style.visibility = originalVisibility;		
//		bookingOptionalInputsAnim.onStart.subscribe(function() {
//			if (bookingOptionalInputsAnim.attributes.height.to > 0) {
//				YAHOO.util.Dom.setStyle('bookingOptionalInputs', 'display', 'block');
//			}
//		}, bookingOptionalInputsAnim);
//		bookingOptionalInputsAnim.onComplete.subscribe(function() {
//			if (bookingOptionalInputsAnim.attributes.height.to == 0) {
//				YAHOO.util.Dom.setStyle('bookingOptionalInputs', 'display', 'none');
//			}
//		}, bookingOptionalInputsAnim);
	}
	
	if (bookingOptionalInputsAnim.isAnimated()) {
		return;
	}
	
	bookingSendOptionalParamters = show;
	
	bookingOptionalInputsAnim.attributes.height.to = show ? bookingOptionalInputsAnim.originalHeight : 1; 
	bookingOptionalInputsAnim.attributes.opacity.to = show ? 1 : 0;
    bookingOptionalInputsAnim.animate();
	
    YAHOO.util.Dom.setStyle('bookingOpenOptionalInputs', 'display', show ? 'none' : 'block');
    YAHOO.util.Dom.setStyle('bookingCloseOptionalInputs', 'display', show ? 'block' : 'none');
}

function setBookingDayLabel(id, dt) {
	if (dt == null) {
		return;
	}
	document.getElementById(id).innerHTML = formatLongWeekDay(dt);
}

function bookingCheckDate(ip, d, pr) {
	var d2;
	var str = ip.value;
	if (str.length > 0) {
		d2 = new Date(str);
		if ((pr && d.getTime() > d2.getTime()) || (!pr && d.getTime() < d2.getTime())) {
			return null;
		}
	}
	d2 = new Date(d.getTime() + (pr ? -1 : 1) * 24*60*60*1000*bookingLOS);
	ip.value = formatShortDate(d2);
	return d2;
}

function setBookingCalendarDate(date) {
    if (calendarPanel.inputElement != null) {
        var dayLabelId = null;
        if (calendarPanel.inputElement.name == "arrive") {
            setBookingDayLabel("arriveDay", date);
            setBookingDayLabel("departDay", bookingCheckDate(document.bookingWidget.depart, date, false));
        } else if (calendarPanel.inputElement.name == "depart") {
        	setBookingDayLabel("departDay", date);
            setBookingDayLabel("arriveDay", bookingCheckDate(document.bookingWidget.arrive, date, true));
        } 
    }
}

function formatShortDate(date) {
	return YAHOO.util.Date.format(date, {format: '%m/%d/%Y'}, 'en-US');
}

function formatShortWeekDay(date) {
	return YAHOO.util.Date.format(date, {format: '%a'}, 'en-US');
}

function formatLongWeekDay(date) {
	return YAHOO.util.Date.format(date, {format: '%A'}, 'en-US');
}

function setFieldFocus(formName, fieldName) {
	document.forms[formName].elements[fieldName].focus();
	if (document.forms[formName].elements[fieldName]['select']) {
		document.forms[formName].elements[fieldName].select();
	}
}

function validateDateField(formName, fieldName, fieldLabel) {
	var val = document.forms[formName].elements[fieldName].value;

 	if(!val.match(/^\d{1,2}\/\d{1,2}\/\d{4}$/)) {
 		alert(fieldLabel + ' is not valid (invalid date format)!. The date format should be : MM/DD/YYYY'); 	
		setTimeout("setFieldFocus('" + formName + "', '" + fieldName + "');", 20);
		return false;
 	}
 	
   	var f = val.split ("/");
   	var d = new Date(val);
   	if (f[0] - 1 != d.getMonth() || f[1] != d.getDate() || f[2] != d.getFullYear()) {
		alert(fieldLabel + ' is not valid!');
		setTimeout("setFieldFocus('" + formName + "', '" + fieldName + "');", 20);
		return false;
   	}
   	return true;
}

