﻿function DetectBrowser() {
    var agent = window.navigator.userAgent.toLowerCase();

    if (agent.indexOf("msie") != -1)
        return "MSIE";

    if (agent.indexOf("firefox") != -1)
        return "Firefox";

    return "Unknown";
}


function PostbackDefaultButton(clientButtonId, e) {
    var ev;
    var browser = DetectBrowser();

    if (browser == "MSIE") {
        if (window.event.keyCode == 13) {
            window.event.returnValue = false;
            window.event.cancel = true;
        }

        ev = window.event;
    }
    else {
        ev = e;
    }

    if (e.keyCode == 13) {
        //INFO: With a validator on the page, this method does not work in IE!
        //__doPostBack(clientButtonId, "");
        //return false;

        //INFO: The WebForm_DoPostBackWithOptions method is called on button click, but this doesn't work either...
        //WebForm_PostBackOptions(eventTarget, eventArgument, validation, validationGroup, actionUrl, trackFocus, clientSubmit)
        //WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(clientButtonId, "", true, "", "", false, false))

        // So, we simulate the button click
        var button = document.getElementById(clientButtonId);

        if (button.click) {
            button.click();
            return false;
        }
    }

    return true;
}
