﻿var WarningFocusControl;
var HighlightFocusControl;

var isConfirm = false;
var confirmOwner;
var charactersLimit = 500;

var messageType;

var warningControl = new IOkno('divAlertWindow', 'wc_bg_iOkno');

function ShowWarning(name, focusControl, type, owner, itemsToDelete, highlightFocusControl, arguments) {
    var message = name;
    for (var i in KeysArray) {
        if (KeysArray[i] == name) {
            message = BodiesArray[i];
        }
    }

    WarningFocusControl = focusControl;
    HighlightFocusControl = highlightFocusControl;
    
    warningControl.Show();

    var textToDisplay = message;
    if (itemsToDelete != null) {
        textToDisplay = textToDisplay.replace('?', '')
        textToDisplay += ': <b>' + itemsToDelete + '</b>';
    }

    if (arguments != null)
        for (var i = 0; i < arguments.length; i++)
        textToDisplay = textToDisplay.replace('{' + i + '}', arguments[i]);

    $(".p_text").html(textToDisplay);

    switch (type) {
        case "confirm":
            ShowCommentsArea(owner);
            messageType = "confirm";
            $(".confirmComment").css({ display: 'block' });
            break;
        case "commonConfirm":
            ShowCommentsArea(owner);
            messageType = "commonConfirm";
            $(".confirmComment").css({ display: 'none' });
            break;
        case "rulesAgreements":
            ShowCommentsArea(owner);
            messageType = "rulesAgreements";
            $(".rulesAgreements").css({ display: 'block' });
            $(".confirmComment").css({ display: 'none' });
            break;
        default:
            $(".confirmComment").css({ display: 'none' });
            $("#ConfirmButtons").css({ display: 'none' });
            $("div[id*=warningButtons]").css({ display: 'block' });
            break;
    }

    if (isIE6() && $("#cmp_warning_content").height() < 70)
        $("#cmp_warning_content").css("height", 70);
    if (isIE6() && $.browser.msie && $.browser.version == "6.0" && $(".text_wrapper").height() < 50) 
        $(".text_wrapper").css("height", 50);
}

function ShowCommentsArea(owner) {
    $("#cmp_warning_content").css({ height: '' });
    $(".confirmComment").css({ display: 'block' });
    $("#ConfirmButtons").css({ display: 'block' });
    $(".rulesAgreements").css({ display: 'none' });
    $("div[id*=warningButtons]").css({ display: 'none' });
    confirmOwner = owner;
}

function IsConfirm() {
    return isConfirm;
}

function OkClick() {
    HideWarningPopupAndClick();
}

function ProcessCustomAlertOkHandler() {
    if (typeof (customAlertObject) != "undefined") {
        if (customAlertObject.CustomAlertOkClick) {
            var ptCustomAlertOkClick = customAlertObject.CustomAlertOkClick;
            customAlertObject = undefined;
            return ptCustomAlertOkClick();
        }
    }
    return null;
}

function AlertOkClick() {
    var retVal = ProcessCustomAlertOkHandler();
    if (retVal != null)
        return retVal;

    var checkbox = $(".cmp_warn_content_text input[type='checkbox']");
    var checked = checkbox.attr('checked');

    if (checked == true) {
        var textArea = $(".cmp_warn_content_text textarea");
        var textAreaValue = textArea.val();
        if (textAreaValue != '') {
            $("#ErrorMessage").css({ display: 'none' });
            HideWarningPopupAndClick();
        }
        else {
            if (messageType == "confirm")
                $("#ErrorMessage").css({ display: 'block' });
            else
                HideWarningPopupAndClick();
        }
    }
    else {
        $("#LiteralCommentCheckboxSilentMode").css({ display: 'none' });
        HideWarningPopupAndClick();
    }
}

function HideWarningPopupAndClick() {
    $("#divAlertWindow").css({ display: 'none' });
    isConfirm = true;
    if (confirmOwner)
        confirmOwner.click();

    confirmOwner = null;
    isConfirm = false;

    warningControl.Hide();
}

function CloseWarning() {
    if (WarningFocusControl) {
        if (WarningFocusControl.constructor != Array)
            WarningFocusControl = [WarningFocusControl];
        $('#' + WarningFocusControl[0]).focus();
        if (HighlightFocusControl)
            $.each(WarningFocusControl, function(i, val) { $('#' + val).css('color', 'red'); })
    }

    var textArea = $(".cmp_warn_content_text textarea");
    if (textArea) {
        textArea.val('');
    }
    warningControl.Hide();
}

$(document).ready(function() {
    if (typeof (commentTextAreaId) != "undefined")
        $('#' + commentTextAreaId).maxLength(500, 'counter');

    var window_size = getPageSize();
    $("input[id*=coverWidth]").val(window_size[0] + "px");
    $("input[id*=coverHeight]").val(window_size[1] + "px");
    $("input[id*=coverLeft]").val((window_size[2] - 400) / 2 + "px");
    $("input[id*=coverTop]").val(window_size[3] / 2 - 100 + "px");
});
