﻿///<reference name="MicrosoftAjax.js" />
///<reference name="jquery-1.2.6-vsdoc.js" />

var _postbackControl;

function addRequestHandlers()
{
    // Note that the handler function names must be unique
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(utilsBeginRequestHandler);
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(utilsEndRequestHandler);
}

function utilsBeginRequestHandler(sender, args)
{
    // Disable the control that caused the postback
    // taken from http://disturbedbuddha.wordpress.com/2007/12/10/disabling-a-trigger-control-during-asynchronous-postback/
    _postbackControl = args.get_postBackElement();
    _postbackControl.disabled = true;
}

function utilsEndRequestHandler(sender, args)
{
    if (_postbackControl)
    {
        // Re-enable the control that originally caused the postback
        _postbackControl.disabled = false;
        _postbackControl = undefined;
    }
}

function enableCombo(txt, cboID)
{
    var disabled = !isPositiveInteger(txt.value);
    $get(cboID).disabled = disabled;
}

function validateNumericTextBox(txt)
{
    if (isPositiveInteger(txt.value))
    {
        // Note that text like "5fgfg" will get us here
        txt.value = parseInt(txt.value);
    }
    else
    {
        txt.value = "";
    }
}

function isPositiveInteger(str)
{
    var value = parseInt(str);

    return (!isNaN(value) && (value > 0));
}

function refreshRoversTotal(totalID, ticketIDs, ticketPrices, totalValidatorID)
{
    var total = 0;

    for (i = 0; i < ticketIDs.length; i++)
    {
        var txtTicketCount = $get(ticketIDs[i]);

        // The ticketPrice attribute will have been added to this TextBox
        // in code-behind
        // BUGBUG: This attribute cannot be obtained in JavaScript using FireFox
        total += ticketTotal(txtTicketCount.value, ticketPrices[i]);
    }

    $get(totalID).value = "£" + (isNaN(total) || total == 0 ? "-.-" : total.toFixed(2));

    // Total validator might have been previously shown, so let's hide it
    var val = $get(totalValidatorID);
    if (val) // val will be undefined unless it is currently shown
    {
        val.isvalid = true;
        ValidatorUpdateDisplay(val);
    }
}

function refreshFlexipassTotal(totalID, quantityID, originID, destinationID, pricesIndexID, pricesTableID)
{
    var quantity = parseInt($get(quantityID).value);

    var ticketPrice = NaN;

    var cboOrigin = $get(originID);
    var cboDestination = $get(destinationID);

    if ((cboOrigin.selectedIndex > 0) && (cboDestination.selectedIndex > 0))
    {
        var hidPricesIndex = $get(pricesIndexID);
        var pricesIndex = parseInt(hidPricesIndex.value);

        var route = cboDestination[cboDestination.selectedIndex].value;

        // This syntax is that of jQuery database (http://github.com/nkallen/jquery-database/tree/master)
        var cell = $("#" + pricesTableID)
            .where('.route:eq(' + route + ')')
            .select('.price' + pricesIndex);

        ticketPrice = parseFloat(cell[0][0]);
    }

    var total = quantity * ticketPrice;

    $get(totalID).value = "£" + (isNaN(total) || total == 0 ? "-.-" : total.toFixed(2));

    // Total validator might have been previously shown, so let's hide it
//    var val = $get(totalValidatorID);
//    if (val != null)
//    {
//        val.isvalid = true;
//        ValidatorUpdateDisplay(val);
//    }
}

function ticketTotal(price, count)
{
    var tt = price * count;

    return (!isNaN(tt) && tt >= 0 ? tt : 0);
}

function validateRailcard(source, clientside_arguments)
{
    var railcardSelected = $get(source.cboID).selectedIndex > 0;
    var ticketsSelected = parseInt($get(source.txtID).value);

    clientside_arguments.IsValid = !((ticketsSelected > 0) && !railcardSelected);
}

function showCardSecurityPopup(popupID)
{
    $find(popupID).show();
}
