﻿var PageID = 0;
var PageName = null;
function DialogForm_Show(clientId, action,id, headerText, width, height,onCloseUpHandler)
{
    var popup = DialogForm_GetPopupElement(clientId);
    //if(popup.IsVisible()) return;
    if(popup)
    {
        //Debuger.ShowInfo(popup);
        if (headerText)
            popup.SetHeaderText(headerText);

        if (width && height)
        {
            popup.Show();
            popup.SetSize(width, height);
            popup.Hide();
        }
        popup.Show();
        var cbp = gfGetElement(clientId+'_popupDialogForm_cbpDialogForm');
        //Debuger.ShowInfo(cbp);
        cbp.PerformCallback(action+'|'+id);
    }
    if (!popup.closeUpEventHandlers) {
        popup.closeUpEventHandlers = new Array();
        popup.addCloseUpEventHandler = function(handler) {
            if (handler && typeof(handler) =='function')
                popup.closeUpEventHandlers[popup.closeUpEventHandlers.length] = handler;
        }
    }
    else
        popup.closeUpEventHandlers = new Array();
    popup.addCloseUpEventHandler(onCloseUpHandler);
}
function DialogForm_GetPopupElement(clientId) {
    return gfGetElement(clientId + '_popupDialogForm');
}
function DialogForm_AddCloseUpEventHandler(clientId, handler) {
    var popup = DialogForm_GetPopupElement(clientId);
    popup.addCloseUpEventHandler(handler);
}
function DialogForm_Close(clientId)
{
    gfClosePopup(clientId+'_popupDialogForm');
}
function popupDialogForm_CloseUp(clientId,e)
{
    
    var hf = document.getElementById(clientId + "_popupDialogForm_cbpDialogForm_hfDialogParam");
    gfClearHidden(clientId + "_popupDialogForm_cbpDialogForm_hfDialogParam");
    
    var popup = DialogForm_GetPopupElement(clientId);
    //execute event handlers
    if (popup.closeUpEventHandlers && popup.closeUpEventHandlers.length > 0) {
        for (var i = 0; i < popup.closeUpEventHandlers.length; i++) {
            var handler = popup.closeUpEventHandlers[i];
            handler(popup, e);
        }
    }
}




var arrStack = new Array();

function GetLastEvent() {
    return window.parent.arrStack[window.parent.arrStack.length-1][0];
}

function RemoveLastEvent() {
    window.parent.arrStack.pop();
}


function DialogForm_ShowFrame(headerText, editForm, queryString, event, width, height) {
    if (!width) width = DialogForm_GetWidth(editForm);
    if (!height) height = DialogForm_GetHeight(editForm);
    var _window = this;
    var _src = 'popuppage.aspx?view=' + editForm;
    if (!isNullOrEmpty(queryString))
        _src += '&' + queryString;
        
    while (_window.parent != _window) {
        _window = _window.parent
    }
    _src += '&PageID=' + PageID + '&PageName=' + PageName;
    var arr = new Array(3);
    arr[0] = event;    
    _window.arrStack[_window.arrStack.length] = arr;
    arr[1] = CreatePopupControlFrame(_window, _window.arrStack.length, _src);      
    var params = new Object();
    params.width = width;
    params.height = height;
    params.title = headerText;
    params.path = _src;
    _window.arrStack[_window.arrStack.length - 1][1].Show(params);
}

function CloseDialogFrame(_val) {
    var _name = LastDivName();
    var _over = LastDivOverlayName();
    if (arrStack.length > 1) {
        arrStack[arrStack.length - 1][1].Hide();
        var _fun = arrStack[arrStack.length - 1][0];
        
        arrStack.pop();
        
        //execScript
        if (window.execScript) {
            
            execScript('arrStack[arrStack.length - 1][1].GetIFrameDlg().contentWindow.' + _fun + '(\'' + _val + '\');');
        } else {
            
            eval('arrStack[arrStack.length - 1][1].GetIFrameDlg().contentWindow.' + _fun + '(\'' + _val + '\');');
        }
        
        
    } else {
        arrStack[arrStack.length - 1][1].Hide();
        if (window.execScript) {
            execScript(arrStack[arrStack.length - 1][0] + '(\'' + _val + '\');');
        } else {
            eval(arrStack[arrStack.length - 1][0] + '(\'' + _val + '\');');
        }
                
        arrStack.pop();
    }

    RemoveDivDialog(_name);
    RemoveDivOverlay(_over);
}

function RemoveDivDialog(name) {
    
    var _div = document.getElementById(name);
    //var _lbl = document.getElementById('lblPopup');
    //_lbl.removeChild(_div);
    document.body.removeChild(_div);
}

function LastDivName() {
    var _name = 'divDlg' + (arrStack.length);
    return _name;
}

function LastDivOverlayName() {
    var _name = 'divDlgOverlay' + (arrStack.length);
    return _name;
}

function RemoveDivOverlay(name) {

    var _div = document.getElementById(name);    
    document.body.removeChild(_div);
}

function HideDialogFrame() {
    var _name = LastDivName();
    var _over = LastDivOverlayName();
    arrStack[arrStack.length - 1][1].Hide();
    arrStack.pop();
    RemoveDivDialog(_name);
    RemoveDivOverlay(_over);
}

function CreatePopupControlFrame(_window, _num, _src) {
//    var str = '<div id="divDlgOverlay' + _num + '" class="OpacityDialogOverlay" style=" display: none;left: 0px; top: 0px; width: 100%; height: 100%;background-color: Black; z-index: 900; position: absolute;"></div>';

    /*var str = '<div id="divDlg' + _num + '" style=" display: none; border: solid 1px black; background-color: White; left: 10px; top: 10px; width: 100px; height: 100px; z-index: 901; position: absolute;">';
    str += '<table id="tblDlgTitle' + _num + '" width="100%" cellpadding="0" cellspacing="0">';
    str += '<tr>';
    str += '<td style="height: 25px; width: 5px;" class="DialogTitleStart">';
    str += '</td>';
    str += '<td style="width: 5px;" align="left" valign="middle" class="DialogTitle">';
    str += '</td>';
    str += '<td align="left" valign="middle" class="DialogTitle">';
    str += '<span id="spanDlgTitle' + _num + '" class="DialogTitleCaption"></span>';
    str += '</td>';
    str += '<td align="right" valign="middle" class="DialogTitle">';
    str += '<img src="' + ImagesPath + '/Dialog/close.jpg" align="absmiddle" alt="" style="cursor: pointer;"';
    //str += 'onmouseover="tooltip(this, \'Закрыть\');" onmouseout="tooltipHide(this);"';
    //str += 'onclick="Dialog.Hide();" />'
    str += 'onclick="HideDialog();" />';
    str += '</td>';
    str += '<td style="width: 5px;" class="DialogTitleEnd">';
    str += '</td>';
    str += '</tr>';
    str += '</table>';
    str += '<table cellpadding="3" cellspacing="3">';
    str += '<tr>';
    str += '<td id="tdDlgContent' + _num + '" align="center" valign="middle">';
    str += '</td>';
    str += '</tr>';
    str += '</table>';
    str += '</div>';*/
    // <div id="divDlgOverlay" class="OpacityDialogOverlay" style=" display: none;left: 0px; top: 0px; width: 100%; height: 100%;background-color: Black; z-index: 900; position: absolute;"></div>
    var _zBck = 99999;
    var _zDiv = 99999;
    var x = _window.arrStack.length * 2;
    _zBck = _zBck + x;
    _zDiv = _zBck + 1;
    var _Div = document.createElement('div');
    _Div.id = 'divDlgOverlay' + _num;
    _Div.style.display = 'none';

    if (_window.arrStack.length == 1) {
        _Div.style.backgroundColor = 'silver';
    } else {
        _Div.style.backgroundColor = 'silver';
    }
    _Div.className = 'OpacityDialogOverlay';
    _Div.style.left = '0px';
    _Div.style.top = '0px';
    _Div.style.width = '100%';
    _Div.style.height = '100%';
    _Div.style.zIndex = _zBck;
    _Div.style.position = 'absolute';
    _window.document.body.appendChild(_Div);
   
   
    //var _css = 'className';
    var newDiv=document.createElement('div');
    newDiv.id = 'divDlg' + _num;
    newDiv.style.display = 'none';
    newDiv.style.border = 'solid 1px black';
    newDiv.style.backgroundColor = 'White';
    newDiv.style.left = '10px';
    newDiv.style.top = '10px';
    newDiv.style.width = '100px';
    newDiv.style.height = '100px';
    newDiv.style.zIndex = _zDiv;
    newDiv.style.position = 'absolute';
    //var _newDiv = _window.document.getElementById('divDlg' + _num);
    //_window.document.getElementById('lblPopup').InsertAdjacentHTML('beforeEnd', str);
    var tbl = document.createElement('table');
    tbl.id = 'tblDlgTitle' + _num;
    tbl.style.width = '100%';
    tbl.setAttribute('cellSpacing', '0px');
    tbl.setAttribute('cellPadding', '0px');
    var tblBody = document.createElement('tbody');
    var row = document.createElement('tr');
    var cellLeft = document.createElement('td');
    //height: 25px; width: 5px;" class="DialogTitleStart
    cellLeft.style.width = '5px';
    cellLeft.style.height = '25px';
    //cellLeft.setAttribute(_css, 'DialogTitleStart');
    cellLeft.className ='DialogTitleStart';
    var cellCenter = document.createElement('td');
    // style="width: 5px;" align="left" valign="middle" class="DialogTitle">
    cellCenter.style.width = '5px';
    cellCenter.setAttribute('align', 'left');
    cellCenter.setAttribute('valign', 'middle');
    //cellCenter.setAttribute(_css, 'DialogTitle');
    cellCenter.className = 'DialogTitle';
    var cellRight = document.createElement('td');
    // align="left" valign="middle" class="DialogTitle"
    cellRight.setAttribute('align', 'left');
    cellRight.setAttribute('valign', 'middle');
    //cellRight.setAttribute(_css, 'DialogTitle');
    cellRight.className = 'DialogTitle';
    // <span id="spanDlgTitle' + _num + '" class="DialogTitleCaption"></span>
    var spanTitle = document.createElement('span');
    spanTitle.setAttribute('id', 'spanDlgTitle' + _num);
    //spanTitle.setAttribute(_css, 'DialogTitleCaption');
    spanTitle.className = 'DialogTitleCaption';
    cellRight.appendChild(spanTitle);
    
    var cell4 = document.createElement('td');
    //<td align="right" valign="middle" class="DialogTitle">
    cell4.setAttribute('align', 'right');
    cell4.setAttribute('valign', 'middle');
    //cell4.setAttribute(_css, 'DialogTitle');
    cell4.className = 'DialogTitle';
    var _img = document.createElement("img");
    // img src="' + ImagesPath + '/Dialog/close.jpg" align="absmiddle" alt="" style="cursor: pointer;" onclick="HideDialog();"
    _img.src = ImagesPath + '/Dialog/close.jpg';
    _img.setAttribute('align', 'absmiddle');
    _img.setAttribute('alt', '');
    //_img.setAttribute('onclick', 'alert(\'test\');');
    _img.onclick = function() { HideDialogFrame(); }; 
    _img.style.cursor = 'pointer';
    cell4.appendChild(_img);
    var cell5 = document.createElement('td');
    // <td style="width: 5px;" class="DialogTitleEnd">';
    cell5.style.width = '5px';
    //cell5.setAttribute(_css, 'DialogTitleEnd');
    cell5.className = 'DialogTitleEnd';
    // <table cellpadding="3" cellspacing="3">
    var tblContent = document.createElement('table');        
    tblContent.setAttribute('cellSpacing', '3px');
    tblContent.setAttribute('cellPadding', '3px');
    var tblBodyContent = document.createElement('tbody');
    var rowContent = document.createElement('tr');
    var cellContent = document.createElement('td');
    //<td id="tdDlgContent' + _num + '" align="center" valign="middle">
    cellContent.setAttribute('id', 'tdDlgContent' + _num);
    cellContent.setAttribute('valign', 'middle');
    cellContent.setAttribute('align', 'center');
    
    tblContent.appendChild(tblBodyContent);
    tblBodyContent.appendChild(rowContent);
    rowContent.appendChild(cellContent);
    
    row.appendChild(cellLeft);
    row.appendChild(cellCenter);
    row.appendChild(cellRight);
    row.appendChild(cell4);
    row.appendChild(cell5);
    tblBody.appendChild(row);
    tbl.appendChild(tblBody);
    newDiv.appendChild(tbl);
    newDiv.appendChild(tblContent);
    //_window.document.getElementById('lblPopup').appendChild(newDiv);
    _window.document.body.appendChild(newDiv);
    
    //_window.document.getElementById('lblPopup').innerHTML = _window.document.getElementById('lblPopup').innerHTML+str;       
    return new Dialog(_window, _num, _src);
}

