/*
 * Copyright (c) 2006, XXI Technologies, LLC. All Rights Reserved.
 */
function onFocus(fieldName) {
	var field = eval(fieldName);

	field.style.background = '#ffffff';
	field.style.color = '#000000';

	if (field.createTextRange) {
		var range = field.createTextRange();
		range.moveStart("character", 0);
		range.moveEnd("character", field.value.length);
		range.select();
	}
	else {
		field.setSelectionRange(0, field.value.length);
	}
}

// pass in suggest host in case it runs on separate box
function setAsDefaultDeparture(suggestHost) {
	departure = document.queryform.departure.value.toUpperCase();
	cityCode = getCityFromField(departure, null);
	
	if (cityCode.length == 3) {
		document.location = "http://" + suggestHost + "/suggest/setDefaultDeparture?departure=" + cityCode;
	}
}

// pass in suggest host in case it runs on separate box
function setAsDefaultDepartureFromCompareDestinationsTab(suggestHost) {
	departure = document.queryform.departure2.value.toUpperCase();
	cityCode = getCityFromField(departure, null);

	if (cityCode.length == 3) {
		document.location = "http://" + suggestHost + "/suggest/setDefaultDeparture?departure=" + cityCode;
	}
}

// pass in suggest host in case it runs on separate box
function setAsDefaultDestination(suggestHost) {
	destination = document.queryform.destination.value.toUpperCase();
	cityCode = getCityFromField(destination, null);
	
	if (cityCode.length == 3) {
		document.location = "http://" + suggestHost + "/suggest/setDefaultDestination?destination=" + cityCode;
	}
}

// parses a city code from a text field
// if the text field is a suggest field, pass the associated hidden value in as well
function getCityFromField(rawValue, hiddenValue) {
	if (hiddenValue) {
		// hidden value is populated, use suggest-populated city code
		return hiddenValue;
	}
	
	charArray = rawValue.toArray();
	
	newValue = '';
	buffer   = '';
	
	for (i=0; i<rawValue.length; i++) {
		ch = rawValue.charAt(i);
	
		if (ch == ' ') {
			if (newValue.length == 0) {
				continue;
			}
			
			buffer = buffer + ch;
			continue;
		}
		
		if (buffer.length > 0) {
			newValue = newValue + buffer;
			buffer = '';
		}
		
		newValue = newValue + ch;
	}
	
	rawValue = newValue;
	
	if (rawValue.length == 3) {
		// field value is populated with 3 chars
		// use raw text from the field, it could be a city code
		return rawValue.toUpperCase();
	}

	// suggest has not completed and the raw field value is not a city code
	// check for 'city, st (code)' format and parse it if found
	openParen = rawValue.indexOf("(");
	closeParen = rawValue.indexOf(")");

	if (closeParen - openParen == 4) {
		// probably found a 'city, st (code)' format in field, return what is in parens
		return rawValue.substring(openParen + 1, closeParen);
	}

	// free text in field
	// return it and let the server try to match it
	return rawValue;
}



// The following functions are overridden by subpages that require specific loading behavior

function suggestInstall() {
    new suggest.CitySuggestField('departure');
    new suggest.CitySuggestField('destination');
}

function onTemplatePageLoad() {
}

function calendarInstall() {
}

// Called by the template body onload

function onPageLoad() {
	onTemplatePageLoad();
	suggestInstall();
	calendarInstall();
        readCookie("fc-query");
}
