
var prm = Sys.WebForms.PageRequestManager.getInstance();

prm.add_initializeRequest(initializeRequest);
prm.add_endRequest(endRequest);

var _postBackElement;

function initializeRequest(sender, e) {
    if (prm.get_isInAsyncPostBack()) {
        e.set_cancel(true);
    }
    _postBackElement = e.get_postBackElement();

    var loc = _postBackElement.id.indexOf('btnMoreRates');

    if (loc > -1) {
        var prefix = _postBackElement.id.substring(0, loc);
        $get(prefix + 'RunMsg').style.display = 'block';
    }

}

function endRequest(sender, e) {

    //.NET add text to the beginning of generated variable names
    //we need to get the control clicked.
    //use the postbackelelement and find out which control is click with IndexOf

    var controlPosition=0;
    var loc1 = _postBackElement.id.indexOf('btnMoreRates');
    var loc2 = _postBackElement.id.indexOf('lnkMoreRatesTop');
    var loc3 = _postBackElement.id.indexOf('btnBestRateReserve');

    if (loc1 > -1)
        controlPosition = loc1;
        
    if (loc2 > -1)
        controlPosition = loc2;

    if (loc3 > -1) {
        controlPosition = loc3;
    }

    loc = controlPosition;


    if (loc > -1) {

        var prefix = _postBackElement.id.substring(0, loc);
        var btnMoreRates = $get(prefix + 'btnMoreRates');
        var lnkMoreRatesTop = $get(prefix + 'lnkMoreRatesTop');
        var lnkReserve = $get(prefix + 'btnBestRateReserve');

        if( lnkMoreRatesTop != null )
        {
                if (lnkMoreRatesTop.innerHTML.indexOf('Show') > -1) {

                window.scrollBy(0, 750);
                lnkMoreRatesTop.focus();
                window.scrollBy(0, -25);

                lnkMoreRatesTop.innerHTML = 'Hide All Rates';

                if (btnMoreRates.type == 'image') {
                    btnMoreRates.src = '/images/hide-all-rates-button.png';
                }
                else {
                    btnMoreRates.className = 'hide-all-rates-button';
                }

                $get(prefix + 'RunMsg').style.display = 'block';

            }
            else {
                //the set the button to "show" rates
                lnkMoreRatesTop.innerHTML = 'Show All Rates';

                if (btnMoreRates.type == 'image') {
                    btnMoreRates.src = '/images/show-all-rates-button.png';
                }
                else {
                    btnMoreRates.className = 'show-all-rates-button';
                }
            }
        }
    }

    var loc = _postBackElement.id.indexOf('btnMoreRates');

    if (loc > -1) {
        var prefix = _postBackElement.id.substring(0, loc);
        $get(prefix + 'RunMsg').style.display = 'none';
    }
}

