function performSearch() {	
    var i;
    var theForm = document.offerSearchForm;
    var searchUrl = theForm.action + "?";

    // process dates
    if(theForm.ciDate.value != "MM/DD/YYYY") {
        // check-in date
        searchUrl += "&ciDate=" + theForm.ciDate.value;
        searchUrl += "&coDate=" + theForm.coDate.value;
		highlightSearchForm.resetErrors();
		checkOfferDates();	
		if (highlightSearchForm.hasErrors()) {
			highlightSearchForm.showErrors();	
			return false;
		} 
    }

    // process places
    if(theForm.where.value != "destinations") {
        // destinations
        searchUrl += "&destinationID=" + theForm.where.value;
    }
	else{
		highlightSearchForm.addError("destinationNotSelectedError_offer","departureDateLabel");
		if (highlightSearchForm.hasErrors()) {
			highlightSearchForm.showErrors();	
			return false;
		}
	}
    SW.tools.Cookie.set("wo_dest",theForm.where.value);
	
    document.location = searchUrl;

    return false;
}

function checkOfferDates() {
	if ((document.offerSearchForm.ciDate.value == dateFormatString) && (document.offerSearchForm.coDate.value == dateFormatString)) {
		return "DATES_NOT_SET";
	}

	var checkIn = new validDate(document.offerSearchForm.ciDate);
	var checkOut = new validDate(document.offerSearchForm.coDate);

	if (!(checkIn.valid) && (!checkOut.valid) ) {
		highlightSearchForm.addError("~ciAnddepartureDatesInvalidError", "arrivalDateLabel","departureDateLabel")
	} else if (!checkIn.valid ) {
		highlightSearchForm.addError("~arrivalDateInvalidError","arrivalDateLabel")
	} else if (!checkOut.valid ) {
		highlightSearchForm.addError("~departureDateInvalidError","departureDateLabel")
	} else 	if ( (checkIn.valid) & (checkOut.valid)  ) {
		var datesDelta = checkIn.diffDate( checkOut.d);
		var nowDelta = checkIn.diffDate(new Date());
		if (nowDelta > 0 ) {
			highlightSearchForm.addError("~checkInEarlierThanTodayError","arrivalDateLabel");
		} else if (nowDelta < -551) {
			highlightSearchForm.addError("~bookToFarInFutureError","arrivalDateLabel");
			highlightSearchForm.addError()
		} else if (datesDelta < 0) {
			highlightSearchForm.addError("~departureBeforeArrivalError","departureDateLabel");
		} else if (datesDelta > 90) {
			highlightSearchForm.addError("~maximumLengthStayExceededError_offer", "arrivalDateLabel","departureDateLabel");
		} else if (datesDelta < 1) {
			highlightSearchForm.addError("~arrivalEqualsDepartureError", "arrivalDateLabel","departureDateLabel");
		}
	}
	return "";
}


function checkSearchDates(form, event) {
    var formObj = form;
    if (event.type == "focus")   {
        if(formObj.ciDate.hasFocus == "true")    {
            var checkIn = new validDate(formObj.ciDate);
            if (  !checkIn.valid ) { formObj.ciDate.value="";}
        }
        if(formObj.coDate.hasFocus == "true")    {
            var checkOut = new validDate(formObj.coDate);
            if (  !checkOut.valid ) { formObj.coDate.value="";}
        }
    }
    if (event.type == "blur")   {
        if(formObj.ciDate.hasFocus == "false")    {
            var checkOut = new validDate(formObj.coDate);
            var checkIn = new validDate(formObj.ciDate);
            if ( checkIn.valid ) {
                checkIn.setField();
                if (checkOut.valid ) { before = (checkIn.diffDate(checkOut.d) <= 0);}
                if ( (! (checkOut.valid)) || before) {
                    checkOut.setD(checkIn.d);
                    checkOut.nextDay();
                    checkOut.setField();
                }
            } else {
                formObj.ciDate.value=dateFormatString;
            }
        }
        if(formObj.coDate.hasFocus == "false")    {
            var checkOut = new validDate(formObj.coDate);
            if ( checkOut.valid ) { checkOut.setField(); } else {formObj.coDate.value=dateFormatString;}
        }
    }
}

function hilteSpan(elementId,action){
    var container = document.getElementById(elementId);

    if (action == "off" && container.className != "locked") container.style.backgroundImage = "";
    else if (action == "on") container.style.backgroundImage = "url('/whotels/images/offers/w_offers_span_highlight.gif')";
}

