﻿function Design(left, top, width, height, title, logo, url)
{   
  this.left = left;
  this.top = top;
  this.width = width;
  this.height = height; 
  this.title = title;  
  this.logo = logo;
  this.url = url;      
} 

function getDialog(id)
{
  var dlg = document.getElementById(id);                   
  if (!dlg) {
    var obj = frames[1];   
    if (!obj) {
      obj = parent.parent.frames[1];              
    }    
    if (obj)
      dlg = obj.document.getElementById(id);  
    else
      dlg = null;            
  }
  return dlg;
}

function isMinimized(id)
{
  var dlg = top.document.getElementById(id);  
  if (dlg) {
    var h = parseInt(dlg.style.height,10);    
    var min = (h < 50)?true:false;
    return min; 
  } else 
    return true;
}

function CalculateControlLeft(id, doc, left)
{
   var btn = doc.getElementById(id);     
   if (btn) 
     btn.style.left = left; 
}

function SetHeightDialog(height)
{
   var newHeight = height+2;
   if (newHeight > 800)
     newHeight = 800;    
   var dlg = top.document.getElementById('iDialog');
   if (dlg) {      
      var doc = dlg.contentWindow.document; 
      if (doc) {
         var divDlg = doc.getElementById('dlg');
         if (divDlg)            
            divDlg.style.height = newHeight - 2 + "px";          
      }      
      dlg.height = newHeight;      
      dlg.style.height = newHeight + 'px';
   }   
}

function ModifyPathHref(txt, iFrame)
{
  var iHTML = txt;  
  if (iFrame && (top.DialogDataFramesPath != '')) {     
     var path = top.GetHostPath() + top.DialogDataFramesPath + "/";    
     var target = ' onfocus="this.blur();" target="_blank"';
     var ap = '"';   
     iHTML = iHTML.replace(/<a href="/g, "<a " + target + " href=" + ap + path);       
     iHTML = iHTML.replace(/<A href="/g, "<a " + target + " href=" + ap + path);             
  }   
  return iHTML;
}

function ClearDivHTML(id)
{
  var dlg = top.document.getElementById(id);
  if (dlg) {
    var doc = dlg.contentWindow.document; 
    var divFrame = doc.getElementById('iFrameDiv');  
    if (divFrame)
      divFrame.innerHTML = '';
  }  
}

function HideFrameDlg(id)
{
  var dlg = top.document.getElementById(id);
  if (dlg) {
    var doc = dlg.contentWindow.document;
    var iFrameDlg = doc.getElementById('iFrameDlg');
    if (iFrameDlg)
      iFrameDlg.style.display = 'none';      
  }   
}

function DoVisibleFrame(frm, dialog)
{
    if ((frm.readyState == 'complete') || (!top.ie())) 
    {
       if (dialog) 
       {            
          dialog.style.visibility = 'visible';        
       }   
       top.Loading('none');      
    }
}

function CreateFrameDiv(doc)
{  
   if (!doc)
     return;
        
   var iFrame = doc.getElementById('iFrame');   
   var divFrame = doc.getElementById('iFrameDiv');  
   if (iFrame && divFrame) {                  
     var iHTML = top.DialogPropertiesInnerHTML ? top.DialogPropertiesInnerHTML:'';          
     if (iHTML != '') {
       var fHTML =  iFrame.contentWindow.document.documentElement.innerHTML;                          
       fHTML = ModifyPathHref(fHTML, iFrame);       
       iHTML += fHTML;     
       divFrame.innerHTML = iHTML;             
       var newHeight = divFrame.offsetHeight + 50;          
       SetHeightDialog(newHeight);         
     } else {    
       divFrame.innerHTML = '';  
       var iFrameDlg = doc.getElementById('iFrameDlg');
       if (iFrameDlg) { 
         iFrameDlg.src = iFrame.src;           
         if (iFrameDlg && iFrameDlg.src != '') {          
           var dlg = doc.getElementById('dlg');              
           iFrameDlg.width = parseInt(dlg.style.width,10) - 2;    
           iFrameDlg.height = parseInt(dlg.style.height,10) - 32; 
           iFrameDlg.style.width = iFrameDlg.width + 'px';
           iFrameDlg.style.height = iFrameDlg.height + 'px';                                          
           iFrameDlg.style.display = '';                                              
         } 
       }  
     } 
     minmaxDialog('iDialog', top);                
     var dialog = top.document.getElementById('iDialog');      
     if ((iFrame.src != '') && iFrameDlg) {              
        if (top.ie()) 
            iFrameDlg.onreadystatechange = function(){DoVisibleFrame(iFrameDlg,dialog);};
          else
            iFrameDlg.onload = function(){DoVisibleFrame(iFrameDlg,dialog);};           
     } else 
     {       
        dialog.style.visibility = 'visible'; 
        top.Loading('none');
     }  
   }       
}

function ResizeHeightDialog(id, newHeight)
{
  var dlg = top.document.getElementById(id); 
  if (dlg) 
     dlg.lang = newHeight + "px";      
}

function ShowFrameInDialog(ifrm)
{
  if ((ifrm.readyState == 'complete') || (!top.ie())) 
  {
     var frm = top.document.getElementById("iDialog"); 
     if (frm)
     {
        var doc = frm.contentWindow.document;
        CreateFrameDiv(doc);              
     }                          
  }   
}

function DesignDialog(id, idbar, design)
{
  var dlg = top.document.getElementById(id);
  if (dlg && design) {
    var doc = dlg.contentWindow.document;    
    if (doc) {     
      CalculateControlLeft('close',doc, design.width - 24 + 'px');          
      CalculateControlLeft('mini',doc, design.width - 44 + 'px');          
      CalculateControlLeft('restore',doc, design.width - 44 + 'px');      
      if (design.title) {              
        var title = doc.getElementById('title');        
        if (title)           
          title.innerHTML = top.GetWord(design.title);   
      }
      if (design.logo) {
        var logo =  doc.getElementById('logo');
        if (logo)
          logo.src = design.logo; 
      }   
      if (design.url)                     
        top.createFrame('iFrame', null, null, doc);                           
      var iFrame = doc.getElementById('iFrame');                    
      if (iFrame && design.url)
      {  
          iFrame.src = design.url;            
          if (top.ie()) 
            iFrame.onreadystatechange = function(){ShowFrameInDialog(iFrame);};
          else
            iFrame.onload = function(){ShowFrameInDialog(iFrame);};
      }
    }
  }  
}

function showDesignDialog(id, idbar, idfocus, barwidth, left, topDlg, width, height, title, logo, url, propHTML)
{ 
  if (propHTML) {  
    HideFrameDlg(id);
    top.DialogPropertiesInnerHTML = propHTML;
  } else {  
    top.DialogPropertiesInnerHTML = '';      
    ResizeHeightDialog(id, height);      
    ClearDivHTML(id);    
  }  
  var des = new Design(left, topDlg, width, height, title, logo, url);    
  DesignDialog(id, idbar, des);   
  showDialog(id, idbar, idfocus, barwidth, des);   
}

function showDialog(id, idbar, idfocus, barwidth, designDlg)
{ 
  var dlg = top.document.getElementById(id);   
  if (dlg) 
  {              
   // if (parseInt(dlg.style.left,10) < 0)         
   //   dlg.style.left = (top.navigatorwidth() - parseInt(dlg.style.width,10)) / 2 + "px";                   
    if (designDlg)
    {
      dlg.style.left = designDlg.left + "px";
      dlg.style.top = designDlg.top + "px";
      dlg.width = designDlg.width;
      var delta = designDlg.url?0:2   
      dlg.style.width = designDlg.width - delta  + "px";
      if (designDlg.height) { 
        dlg.height = designDlg.height;      
        dlg.style.height = designDlg.height - delta + "px";      
      }  
    }   
    if (!designDlg || !designDlg.url)        
      dlg.style.visibility = 'visible';     
    calculateResultDialog(id);    
    var bar  = top.document.getElementById(idbar);   
    if (bar)
    {      
      var bwidth = 50;
      if (barwidth)
        bwidth = barwidth; 
      bar.style.top = dlg.style.top;
      bar.style.left = dlg.style.left;
      bar.style.width =  parseInt(dlg.style.width,10) - bwidth + "px";  
      bar.style.display = 'block';
    }  
  }  
  var doc = dlg.contentWindow ? dlg.contentWindow.document : dlg.document;
  if (doc && doc.body)
  {
    try
    {
        doc.body.focus();
    }
    catch(Error) {}
  }
  if (idfocus && !isMinimized(id))
  {        
      if (doc)
      {
        var ctrl = doc.getElementById(idfocus);         
        if (ctrl)
        {       
            if (ctrl.style.display != 'none') 
                setTimeout("top.document.getElementById('" + id + "').contentWindow.document.getElementById('" + idfocus + "').focus();", 500);       
        }    
      }  
  }         
}

function DoVisibleDialog(ifrm, id, idbar, idfocus, barwidth, des, propHTML)
{     
  if ((ifrm.readyState == 'complete') || (!top.ie()))
  {              
    if (ifrm.src != '') 
    {                                      
        if (des && des.url) 
        {       
            showDesignDialog(id, idbar, idfocus, barwidth, des.left, des.top, des.width, des.height,
                des.title, des.logo, des.url, propHTML);                            
            return;
        }    
        if (des)                                 
            DesignDialog(id, idbar, des);                    
        showDialog(id, idbar, idfocus, barwidth, des);
    }
    top.Loading('none');   
  }
}

function showFrameDialog(id, idbar, idfocus, url, barwidth, left, topDlg, width, height, title, logo, urlDlg, propHTML)
{   
    var frame = top.document.getElementById(id);  
    if (frame) 
    {            
        var apppath = ie() ? "" : top.GetAppVirtualPath();           
        if ((frame.src != apppath + url + "?" + top.mapParams ) || (urlDlg)) 
        {                            
            top.Loading('');    
            var des = null;
            if (left)           
                 des = new Design(left, topDlg, width, height, title, logo, urlDlg);    
            if (!url)            
                url = "Dialog.aspx"; 
            var s = url.toString().indexOf('?') > 0 ? "&" : "?";        
            url = url + s + "lang=" + top.mapLang;    
            var newsrc = top.mapParams ? url + "&" + top.mapParams : url; 
            var framesrc = frame.src.toString().replace(apppath,'');                                                                                                  
            if (newsrc != framesrc)                
            {
                frame.src = newsrc;                                                                                  
                if (top.ie())                                           
                    frame.onreadystatechange = function() { DoVisibleDialog(frame, id, idbar, idfocus, barwidth, des, propHTML);};     
                else 
                    frame.onload = function() { DoVisibleDialog(frame, id, idbar, idfocus, barwidth, des, propHTML); };         
            } else  
               DoVisibleDialog(frame, id, idbar, idfocus, barwidth, des, propHTML);   
        } else
            showDialog(id, idbar, idfocus);
    }
}

function closeDialog(id, container, iddragbar)
{    
  var dlg;  
  if (!container) 
    dlg = getDialog(id);       
  else
    dlg = container.document.getElementById(id);   
  if (dlg){   
    top.SaveDialogPosition(dlg);
    dlg.style.visibility = "hidden";    
    if (iddragbar)   
    {
      dragbar = top.document.getElementById(iddragbar);        
      if (dragbar)
        dragbar.style.display = "none";       
    }  
  }  
  if (top.HideDialogId) { 
    clearTimeout(top.HideDialogId);    
    top.HideDialogId = null;  
  } 
  return false; 
}

function minmaxDialog(id, container, action)
{
  var dlg;  
  if (!container) 
    dlg = getDialog(id);       
  else
    dlg = container.document.getElementById(id);  
  if (dlg) {
    var mini = $get('mini' + id);
    if (!mini)
      mini = dlg.contentWindow.document.getElementById('mini');
    if (mini) 
      mini.style.display = action == 'min'?'none':'';
    var restore = $get('restore' + id);
    if (!restore)
      restore = dlg.contentWindow.document.getElementById('restore');
    if (restore) 
      restore.style.display = action == 'min'?'':'none';  
    if (action == 'min'){       
      dlg.lang = dlg.height ? dlg.height : dlg.style.height.replace("px","");  
      dlg.height = 31;
      dlg.style.height = '31px';     
    } else {      
      if (dlg.lang != '')
      {
        dlg.height = dlg.lang;
        dlg.style.height =  dlg.height + 'px';     
        dlg.lang = '';
      }  
    }  
  }  
}

function CalculateDialog(id,deltax,deltay,obj)
{
  var dlg = getDialog(id);   
  if (dlg) {     	                     
    var w = navigatorwidth(obj);                              
    var wDlg = dlg.style.width.replace("px","");    
    var left = (w - wDlg) / 2;   
    if (deltax)
      {left = left + deltax;}     
    dlg.style.left = left + 'px';   
    var h = navigatorheight(obj);         
    var hDlg = dlg.style.height.replace("px","");
    var top = 0;
    if (deltay > 0)
      top = deltay;
    else   
    { 
      top = (h - hDlg) / 2;          
      if (top < 0) 
        top = 0;  
    }      
    dlg.style.top = top + 'px';     
  }       
}

function ShowModalDialog(msg)
{       
   var doc  = top.document;      
   if (doc)
   {
     var bg = doc.getElementById('Modalbackground');                    
     if (bg)
       bg.style.display = "";
     var dlg = doc.getElementById('Modaldialog');
     if (dlg)
     {       
        dlg.innerHTML = msg;                               
        dlg.style.display = "";                
     }        
   }  
}

function HideModalDialog(doc)
{  
   if (doc)
   {
     var bg = doc.getElementById('Modalbackground');  
     if (bg)
       bg.style.display = "none";
     var dlg = doc.getElementById('Modaldialog');
     if (dlg)        
       dlg.style.display = "none";                   
   }  
}

function SetDialogSize(dlg, left, top, width, height, obj)
{  
  if (dlg) {
    dlg.style.width = width + "px";
    dlg.style.height = height + "px";
    if (!obj)
      obj = dlg;
    var w = navigatorwidth(obj);
    var h = navigatorheight(obj);
    var lc = (w - width) / 2 + 100; 
    var tc = (h - height) / 2;  
    var l = left == 0?lc:left;      
    var t = top == 0?tc:top; 
    dlg.style.left = l + "px";
    dlg.style.top = t + "px"; 
  }  
}

function ShowUserDialog(id, left, top, width, height, obj)
{
  var dlg = getDialog(id); 
  if (dlg) {    
    SetDialogSize(dlg,  left, top, width, height, obj);
    dlg.style.display = "";       
  }  
}

function FocusTxtInDialog(idDlg, idTxt)
{
  var dlg = getDialog(idDlg);  
  if (dlg) {        
    var txtFind = dlg.contentWindow.document.getElementById(idTxt);   
    if (txtFind) {    
      if (txtFind.style.display != 'none')
        txtFind.focus();
    } 
  }     
}  