<!--

var totalPassengers             = 0;
var totalInfants                = 0;
var totalAccompaniedChildren    = 0;
var totalUnaccompaniedChildren  = 0;
var totalAdults                 = 0;
var totalConcessionCard         = 0;

function submitExpressTicket() {
    toggleTravelDirection('return');
    $(f["uxExpressTicketForm"]).submit();
}

// Opens a popup window that allows the user to search bus stops.
function findStop(point) {
    var w = window.open(pathOffWebRoot + 'Bookings/search-for-bus-stop.aspx?point=' + point, 'searchStop_' + point, 'location=0,status=0,resizable=0,scrollbars=0,width=480,height=330');
    
    w.focus();
    return false;
}

function setStop(point, value) {
    $(f[point]).value = value;
}

// This function is called when date is selected in calendar.
function popupCalendarUpdateExpressDate(sender) {
    var selectedDate = sender.get_selectedDate();
    var direction    = "";

    if (selectedDate) {
        if (sender.get_id().indexOf("oDepart") > -1) {
	        updateExpressDate('Depart', selectedDate);
	        
	        selectedDate.setDate(selectedDate.getDate() + 1);
	        updateExpressDate('Return', selectedDate);
    	    
	    } else if (sender.get_id().indexOf("oReturn") > -1) {
	        updateExpressDate('Return', selectedDate);
	    }
    }
}

// This function is called when the drop down value changes.
function onChangeUpdateExpressDate(sender) {
    if (sender.id.indexOf("Depart") > -1) {
        var dateString = getTicketDate('Depart');
        
        var y = parseInt(dateString.substring(0, 4));
        var m = parseInt(dateString.substring(5, 7)) - 1;
        var d = parseInt(dateString.substring(8, 10));
        
        var selectedDate = new Date(y, m, d);
        
        selectedDate.setDate(selectedDate.getDate() + 1);
        updateExpressDate('Return', selectedDate);
    }
}

function updateExpressDate(direction, selectedDate) {
    if (selectedDate) {
        var m = (selectedDate.getMonth() + 1) + "";
	    var d = selectedDate.getDate() + "";
	    var y = selectedDate.getFullYear() + "";
	    
	    d = getZeros(d, 2) + d;
        m = getZeros(m, 2) + m;
	    
	    calendarUpdate(direction, y, m, d);
    }
}  

// Updates the current departure or return date based on the calendar selection.
function calendarUpdate(direction, y, m, d) {
    $(f['ux' + direction + 'Day']).selectedIndex = d - 1;
    
    for (var i = 0; i < $(f['ux' + direction + 'MonthYear']).options.length; i++) {
        if ($(f['ux' + direction + 'MonthYear']).options[i].value == y + "-" + m) {
	        $(f['ux' + direction + 'MonthYear']).selectedIndex = i;
        }
    }
}  

// Gets the current departure or return date in YYYY-MM-DD format.
function getTicketDate(direction) {
    var my = $F(f["ux" + direction + "MonthYear"]);
    var d  = $F(f["ux" + direction + "Day"]);
    
    if (my == "" || d == "") { return null; }
    return (my + "-" + d);
} 

function initTravelDirection() {
    if (f['uxTravelling_1'] != null && $F(f['uxTravelling_1']) == "return") {
        toggleTravelDirection("return");
    } else {
        toggleTravelDirection("oneWay");
    }
}

// Enables and disabled the return date fields based on the direction the visitor is travelling.
function toggleTravelDirection(direction) {
    if (f['uxReturnDay'] != null) {
        if (direction == "return") {
            $(f['uxReturnDay']).enable();
            $(f['uxReturnMonthYear']).enable();
            
        } else {
            $(f['uxReturnDay']).disable();
            $(f['uxReturnMonthYear']).disable();
        }
    }
}

// Calculates various totals based on the fare bases.
function calculateFareBaseTotals() {
    var i = 1;
    
    totalPassengers             = 0;
    totalInfants                = 0;    
    totalAccompaniedChildren    = 0;
    totalUnaccompaniedChildren  = 0;
    totalAdults                 = 0;
    totalConcessionCard         = 0;
    
    while (true) {
        if ($(f['uxFareBasisCount' + i]) == null) { break; } 
        
        var countControl    = f["uxFareBasisCount" + i];
        var fareBaseControl = f["uxFareBasisCode" + i];
       
        // Infant
        if (isInfantCode($F(fareBaseControl))) {
            totalInfants += parseInt($F(countControl));

        // Accompanied Child
        } else if (isAccompaniedCode($F(fareBaseControl))) {
            totalAccompaniedChildren += parseInt($F(countControl));        
        
        // Unaccompanied Child
        } else if (isUnaccompaniedChildCode($F(fareBaseControl))) {
            totalUnaccompaniedChildren += parseInt($F(countControl));        
        
        // Adult
        } else if (isAdultCode($F(fareBaseControl))) {
            totalAdults += parseInt($F(countControl));
        }
        
        // If the fare base requires a concession card.
        if (isConcessionCardFare($F(fareBaseControl))) {
            totalConcessionCard += parseInt($F(countControl));
        }
        
        totalPassengers += parseInt($F(countControl));
        
        i = i + 1;
    }
    
   
//   $('ctl00_cMainContentHolder_uxPromoCode').value = 'I:' +  totalInfants + ' AC:' + totalAccompaniedChildren + ' UC:' + totalUnaccompaniedChildren + ' A:' + totalAdults + ' C:' + totalConcessionCard;
   
}

// Determines if the fare base requires a concession card.
function isConcessionCardFare(fareBase) {
    return (fareBaseConcession[fareBase] == 'Y');
}

// VALIDATION

function validateStops(source, args) {
    args.IsValid = ($F(f['uxOrigin']).toUpperCase() != $F(f['uxDestination']).toUpperCase());
}

function validateDepartDate(source, args) {
    args.IsValid = validateDate($F(f['uxDepartDay']), $F(f['uxDepartMonthYear']));
}

function validateReturnDate(source, args) {
    var currentDateObject = new Date();
    
    var m = "" + (currentDateObject.getMonth() + 1);
    var d = "" + currentDateObject.getDate();
    var y = "" + currentDateObject.getFullYear();
    
    d = getZeros(d, 2) + d;
    m = getZeros(m, 2) + m;
	    
    var currentDate = "" + y + m + d;
    var departDate  = "" + $F(f['uxDepartMonthYear']).replace("-", "") + $F(f['uxDepartDay']);
    var returnDate  = "" + $F(f['uxReturnMonthYear']).replace("-", "") + $F(f['uxReturnDay']);
    
    if ($F(f['uxTravelling_1']) == "return") {
        args.IsValid = (validateDate($F(f['uxReturnDay']), $F(f['uxReturnMonthYear'])) && (departDate >= currentDate) && (departDate <= returnDate));
    } else {
        args.IsValid = (departDate >= currentDate);
    }
}

function validatePassengerFieldsRequired(source, args) {
    args.IsValid = (totalPassengers > 0);
}

function validateFareBaseInfant(source, args) {
    args.IsValid = ((totalInfants / 2 <= (totalAdults + totalConcessionCard)) && totalInfants <= 4);
}

function validateFareBaseAccompaniedChild(source, args) {
    args.IsValid = ((totalAccompaniedChildren > 0 && (totalAdults + totalConcessionCard) > 0 && totalAccompaniedChildren <= 7) || totalAccompaniedChildren == 0) 
}

function validateFareBaseUnaccompaniedChild(source, args) {    
    args.IsValid = ((totalUnaccompaniedChildren > 0 && (totalAdults + totalConcessionCard) == 0 && totalUnaccompaniedChildren <= 7) || totalUnaccompaniedChildren == 0);
}

function validateFareBaseConcessionCard(source, args) {
    args.IsValid = (totalConcessionCard <= 2);
}

function validateDate(day, monthYear) {
    var dt    = monthYear + "-" + day;
    var regEx = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/;
    
    return (regEx.test(dt) && validateDaysInMonth(day, monthYear));
}

function validateDaysInMonth(days, monthYear) {
    var y = monthYear.substring(0, 4);
    var m = monthYear.substring(5, 7);
    var d = parseInt(days);
    
    var daysInMonth = getDaysInDate(m, y);

    return (daysInMonth >= d);
}

//-->
