// this file organizes valid routes
var arrRoutes = new Array("AC", "CA", "AM", "MA", "GM", "MG", "MT", "TM");

// checks whether a selected route is valid or not
function IsRouteValid(p_chrDeparture, p_chrDestination) {
	var strRoute = p_chrDeparture + p_chrDestination;

	for (var index = 0; index < arrRoutes.length; index++) {
		if (strRoute == arrRoutes[index]) {
			return true;
		}
	}

	return false;
};

// this function is called by the script itself
function CheckRoute(p_objInputDeparture, p_objInputDestination, p_objChanged) {
	if (!IsRouteValid(p_objInputDeparture.value, p_objInputDestination.value))
		ChangeRoute(p_objInputDeparture, p_objInputDestination, p_objChanged);
};

// search for first valid destination port
function ChangeRoute(p_objInputDeparture, p_objInputDestination, p_objChanged) {
	var chrSource = p_objChanged.value;

	for (var index = 0; index < arrRoutes.length; index++) {
		if (chrSource == arrRoutes[index].substring(0, 1)) {
			if (p_objChanged == p_objInputDeparture) {
				SetPort(p_objInputDestination, arrRoutes[index].substring(1, 2));
			} else {
				SetPort(p_objInputDeparture, arrRoutes[index].substring(1, 2));
			}
		}
	}
};

// sets the port which was not changed by user
function SetPort(p_objSelect, p_strValue) {
	for (var index = 0; index < p_objSelect.options.length; index++) {
		if (p_objSelect.options[index].value == p_strValue) {
			p_objSelect.selectedIndex = index;

			return;
		}
	}
};





// this is to store boxBooking information into session object
// via webservice to evaluate it on the booking page
function GoToBooking(p_chrDepature, p_chrDestination, p_datOutward, p_datReturn, p_strTypeOfDayTrip, p_srcPage, p_blnV2) {
    if (!p_blnV2) {
        strUrl = "scripts/ws/ws_setBoxBookingParameter.php?depPort=" + p_chrDepature + "&destPort=" + p_chrDestination + "&datOut=" + p_datOutward + "&datRet=" + p_datReturn + "&strTypeOfDayTrip=" + p_strTypeOfDayTrip + "&srcPage=" + p_srcPage;
    } else {
        strUrl = "/_v2/scripts/ws/ws_setBoxBookingParameter.php?depPort=" + p_chrDepature + "&destPort=" + p_chrDestination + "&datOut=" + p_datOutward + "&datRet=" + p_datReturn + "&strTypeOfDayTrip=" + p_strTypeOfDayTrip + "&srcPage=" + p_srcPage;
    }

    jQuery.ajax({
          type: "GET",
          url: strUrl,
          success: RedirectToBooking,
          error: ShowBoxBookingError
    });
};

function RedirectToBooking(data, ioArgs) {
	// redirect the user to the online booking page
	location.href = data;
};

function ShowBoxBookingError(data, ioArgs) {
	alert("An error occurred!\nPlease try again.");
};