﻿/*
Confirm popup replacement
*/
window.drConfirm = function (title, body, mozEvent, toRunInCallBackFnWhenOk, toRunInCallBackFnWhenCancel, doPostBack, width, height) {
    if (mozEvent === undefined) mozEvent = null;
    if (toRunInCallBackFnWhenOk === undefined) toRunInCallBackFnWhenOk = null;
    if (toRunInCallBackFnWhenCancel === undefined) toRunInCallBackFnWhenCancel = null;
    // By default it posts back when Ok is clicked
    if (doPostBack === undefined) doPostBack = true;
    if (width === undefined) width = 300;
    if (height === undefined) height = 100;

    var ev = mozEvent ? mozEvent : window.event; //Moz support requires passing the event argument manually     
    //Cancel the event     
    ev.cancelBubble = true;
    ev.returnValue = false;
    if (ev.stopPropagation) ev.stopPropagation();
    if (ev.preventDefault) ev.preventDefault();

    //Determine who is the caller     
    var callerObj = ev.srcElement ? ev.srcElement : ev.target;
    if (callerObj === undefined) callerObj = null;
    var callBackFn = null;
    //Call the original radconfirm and pass it all necessary parameters     
    if ((callerObj != null && doPostBack)
        || toRunInCallBackFnWhenOk != null
        || toRunInCallBackFnWhenCancel != null) {
        callBackFn = drSetCallBackFn(callerObj, toRunInCallBackFnWhenOk, toRunInCallBackFnWhenCancel, doPostBack);
    }
    //Show the confirm, then when it is closing, if returned value was true, automatically call the caller's click method again.
    var radcontrol = radconfirm(body, callBackFn, width, height, null, title);
    $j(radcontrol).focus();
    $j(radcontrol._popupElement).keydown(function (e) { drKeyPressEvent(e); });
    return false;
}

/*
Alert popup replacement
*/
window.drAlert = function (title, body, mozEvent, toRunInCallBackFnWhenOk, doPostBack, width, height) {
    if (mozEvent === undefined) mozEvent = null;
    if (toRunInCallBackFnWhenOk === undefined) toRunInCallBackFnWhenOk = null;
    // By default it does not post back when Ok is clicked
    if (doPostBack === undefined) doPostBack = false;
    if (width === undefined) width = 300;
    if (height === undefined) height = 100;

    var ev = mozEvent ? mozEvent : window.event; //Moz support requires passing the event argument manually     
    //Cancel the event     
    ev.cancelBubble = true;
    ev.returnValue = false;
    if (ev.stopPropagation) ev.stopPropagation();
    if (ev.preventDefault) ev.preventDefault();

    //Determine who is the caller     
    var callerObj = ev.srcElement ? ev.srcElement : ev.target;
    if (callerObj === undefined) callerObj = null;
    var callBackFn = null;
    //Call the original radconfirm and pass it all necessary parameters     
    if ((callerObj != null && doPostBack)
        || toRunInCallBackFnWhenOk != null) {
        callBackFn = drSetCallBackFn(callerObj, toRunInCallBackFnWhenOk, null, doPostBack);
    }
    //Show the confirm, then when it is closing, if returned value was true, automatically call the caller's click method again. 
    var radcontrol = radalert(body, width, height, title, callBackFn);
    $j(radcontrol).focus();
    $j(radcontrol._popupElement).keydown(function (e) { drKeyPressEvent(e); });
    return false;
}

function drSetCallBackFn(callerObj, toRunInCallBackFnWhenOk, toRunInCallBackFnWhenCancel, doPostBack) {
    var callBackFn = null;
    if (toRunInCallBackFnWhenOk == null && toRunInCallBackFnWhenCancel == null && callerObj != null) {
        callBackFn = function (arg) {
            if (arg) {
                drGetCallerObjectEvent(callerObj, doPostBack);
            }
        }
    }
    else {
        callBackFn = function (arg) {
            if (arg) {
                if (toRunInCallBackFnWhenOk != null)
                    toRunInCallBackFnWhenOk();
                if (doPostBack && callerObj != null)
                    setTimeout("__doPostBack('" + callerObj.id + "','')", 0);
            }
            else {
                if (toRunInCallBackFnWhenCancel != null)
                    toRunInCallBackFnWhenCancel();
            }
        }
    }

    return callBackFn;
}

function drGetCallerObjectEvent(callerObj, doPostBack) {
    if (doPostBack === undefined) doPostBack = false;

    var count = 0;
    currentCallerObj = callerObj;
    while (!(currentCallerObj.tagName == "A" || currentCallerObj.tagName == "INPUT") && count < 2) {
        currentCallerObj = currentCallerObj.parentNode;
        count++;
    }

    if (currentCallerObj.tagName == "A") {
        try {
            if (currentCallerObj.href != "")
                eval(unescape(currentCallerObj.href));
        }
        catch (e) { }
    }
    else if (currentCallerObj.tagName == "INPUT" && doPostBack) {
        currentCallerObj["onclick"] = "";
        currentCallerObj.click();
    }
//    else {
//        window.alert("Rad Window Error!");
//    }
}

function drKeyPressEvent(event) {
    if (event === undefined) event = null;
    var ev = event ? event : window.event; //Moz support requires passing the event argument manually  
    if ($j(ev.currentTarget) != null && $j(ev.currentTarget).is(':visible')) {

        var radControlName = ev.currentTarget.id.split('_')[1];
        if (ev.keyCode == 13 || ev.keyCode == 32) {
            ev.preventDefault();
            $find(radControlName).close(true);
        }
        else if (event.keyCode == 27) {
            ev.preventDefault();
            $find(radControlName).close(false);
        }
    }
}
