//Used by all validations and updates
var resultLine;
var userField;
var oTextbox1, oTextbox2;
var paramTextbox;

window.onload = function() {
    displayWidth = 0;
    maxHeight = 0;
    if (document.getElementById) {
        resultLine = document.getElementById("resultline");
        oTextbox1 = new AutoSuggestControl(document.getElementById("origin"), new TripSuggestions(), displayWidth, maxHeight);
        oTextbox2 = new AutoSuggestControl(document.getElementById("destination"), new TripSuggestions(), displayWidth, maxHeight);
    } else if (document.all) {
        resultLine = document.all["resultline"];
        oTextbox1 = new AutoSuggestControl(document.all["origin"], new TripSuggestions(), displayWidth, maxHeight);
        oTextbox2 = new AutoSuggestControl(document.all["destination"], new TripSuggestions(), displayWidth, maxHeight);
    }

    if (document.captureEvents) {
        document.captureEvents(Event.KEYPRESS);
    }
    
    document.onkeypress = captureReturn;
}

/**
 * Cross-browser method of capturing the return key on text fields,
 * to prevent form from being submitted prematurely.
 */
function captureReturn(e) {
    e = (e) ? e : window.event;
    srcElem = (e.srcElement) ? e.srcElement : e.target;
    var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode;

    if (srcElem.type == "text" && charCode == 13) {
        return false;
    }
    
}

function checkForm(form) {

	var trip_error = "no";
	var error_message = "";

	var origin_error = "no";
	var destination_error = "no";
	
	if (form.orig_dest_type[0].checked || form.orig_dest_type[1].checked) {
		if (!form.origin.value) {
        	//resultLine.innerHTML = "Please enter the trip origin.";
	        //form.origin.focus();
			origin_error = "yes";
			trip_error = "yes";
		}
		else {
		    extractCode('origin');
		}
	}
		
	if (form.orig_dest_type[0].checked || form.orig_dest_type[2].checked) {
		if (!form.destination.value) {
        	//resultLine.innerHTML = "Please enter the trip origin.";
	        //form.origin.focus();
			destination_error = "yes";
			trip_error = "yes";
		}
		else {
		    extractCode('destination');
		}
	}

	
	if (trip_error == "yes") {	
	
		if ((origin_error == "yes") && (destination_error == "yes")) {
			alert('Leaving From and Going To fields are Required');
		}
		
		if ((origin_error == "yes") && (destination_error == "no")) {
			alert('Leaving From field is Required');
		}

		if ((origin_error == "no") && (destination_error == "yes")) {
			alert('Going To field is Required');
		}

		return false;
		
	}
	else {
		return true
	}
	
}

function checkForm_ORIGINAL_new_one_above(form) {

	if (!form.origin.value) {
        resultLine.innerHTML = "Please enter the trip origin.";
        form.origin.focus();
        return false;
    } else if (!form.destination.value) {
        resultLine.innerHTML = "Please enter the trip destination.";
        form.destination.focus();
        return false;
    } else if (form.origin.value == form.destination.value) {
        resultLine.innerHTML = "The origin and destination are the same.";
        form.destination.focus();
        return false;
    }
    
    //populate the hidden fields with the airport codes
    extractCode('origin');
    extractCode('destination');

    return true;
}

function extractCode(fieldName) {
    dataString = document.tripform.elements[fieldName].value;
    re = /^\((.*)\)/;
    foundArray = re.exec(dataString);
    document.tripform.elements[fieldName + '_ac'].value = foundArray[1];
}

/**
 * Autosuggest control object creation
 */
function TripSuggestions() {
}

/**
 * Request suggestions for the given autosuggest control. 
 * @scope protected
 * @param oAutoSuggestControl The autosuggest control to provide suggestions for.
 */
TripSuggestions.prototype.requestSuggestions = function (oAutoSuggestControl /*:AutoSuggestControl*/,
                                                          bTypeAhead /*:boolean*/) {
    paramTextbox = oAutoSuggestControl;
    var sTextboxValue = paramTextbox.textbox.value;
    
    if (sTextboxValue.length >= 3) {
    
        //Get matching data from server
        var httpRequest = new HttpRequest("XML");
        httpRequest.onComplete = onComplete;
        
        document.callbackform.tripinfo.value = sTextboxValue;
        httpRequest.send('/includes/getairports.php', 'POST', document.callbackform);
    }

}

/**
 * Called by the httpRequest object when a response from the server is recvd.
 * Error codes are fully documented in 'getairports.php'
 */
function onComplete(responseText, responseXML) {
    
    var strings = getXMLValues(responseXML, 'data_string');
    if (strings.length == 0) {
        return;
    }
    
    //provide suggestions to the control
    paramTextbox.autosuggest(strings, false);
    
}

