﻿///<reference name="MicrosoftAjax.js" />

var _postbackControl;

var _inboundDate = "";

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 toggleElementVisible(elem)
{
    elem.style.display = (elem.style.display == "none" ? "" : "none");
}

function originSelected(ddlDestinationID)
{
    var ddlDestination = $get(ddlDestination);
    alert(ddlDestination.length);
}

function refreshSearchOneWay(searchControlPrefix)
{
    var oneWay = $get(searchControlPrefix + "_ctlSearch_chkOneWay").checked;

    // Set enabled for the actual textbox
    $get(searchControlPrefix + "_ctlSearch_txtInbound").disabled = oneWay;

    // Set enabled for all the validators
    var inboundValidators =
    [
        searchControlPrefix + "_ctlSearch_txtInbound_RegularExpressionValidator",
        searchControlPrefix + "_ctlSearch_txtInbound_CompareValidator",
        searchControlPrefix + "_ctlSearch_txtInbound_RangeValidator"
    ];

    for (var i = 0; i < inboundValidators.length; i++)
    {
        var validator = $get(inboundValidators[i]);
        quietValidatorEnable(validator, !oneWay);
    }

    // Effectively show or hide the watermark
    var watermarkExtender = $find(searchControlPrefix + "_ctlSearch_txtOutbound_TextBoxWatermarkExtender");
    if (watermarkExtender != null)
    {
        var watermarkText = $find(searchControlPrefix + "_ctlSearch_txtOutbound_TextBoxWatermarkExtender").get_WatermarkText();
        $find(searchControlPrefix + "_ctlSearch_txtInbound_TextBoxWatermarkExtender").set_WatermarkText(oneWay ? "" : watermarkText);
    }

    // If the user has already entered am inbound date and we're now one-way, remember and clear it
    // If we're no longer one-way and the user had already entered an inbound date, restore it
    var inboundDateElem = $get(searchControlPrefix + "_ctlSearch_txtInbound");
    
    if (oneWay)
    {
        _inboundDate = inboundDateElem.value;
        inboundDateElem.value = "";
    }
    else if (_inboundDate != "")
    {
        inboundDateElem.value = _inboundDate;
    }
}

function quietValidatorEnable(validator, enable)
{
    ValidatorEnable(validator, enable);

    if (enable)
    {
        // The validator will fire at this point, which is not desirable
        // Setting the display style seems to be the simplest way to "clear" the validator
        validator.style.display = "none";
    }
}

function showUserFeedbackPopup(popupClientID)
{
    showPopup(popupClientID);
    $get("ctl00_Body_ctlUserFeedbackPopup_panSubmitting").style.display = 'none';
    $get("ctl00_Body_ctlUserFeedbackPopup_txtComments").focus();
}


function showUserFeedbackPopup_VT(popupClientID)
{
    showPopup(popupClientID);
    $get("ctl00_Body_ctlUserFeedbackPopup_panSubmitting").style.display = 'none';
    $get("ctl00_Body_ctlUserFeedbackPopup_txtComments").focus();
}

function showPopup(clientID)
{
    $find(clientID).show();
}

function hidePopup(clientID)
{
    $find(clientID).hide();
}

function submittingFeedback()
{
    if (Page_ClientValidate("Feedback"))
    {
        // This gets called when the user clicks the send button on the feedback popup,
        // we just show a thank you message with an animation    
       
       $get("ctl00_Body_ctlUserFeedbackPopup_panSubmitting").style.display = '';   
       
    }
}

