var alertDialog = null;
var alertX = -1; // centers dialog in window!
var alertY = -1;
var i18N_MyAppAlert = "MyApp Alert"; // can be changed by application

function MyAppAlertSetXY(x, y) {
        // call prior to 'MyAppAlert()'
        alertX = x;
        alertY = y;
        }

function MyAppAlertXY(ev) {
        // call using 'onclick' prior to 'MyAppAlert()'
        var e = jt_fixE(ev);
        MyAppAlertSetXY(e.clientX, e.clientY + document.body.scrollTop);
        return true;
         }

function MyAppAlertInit(title, icon, canc)
{
  var locTitle = i18N_MyAppAlert;
  if (title) locTitle =  title;
  if (alertDialog == null)
  {
    alertDialog = new jt_AppAlert(icon ? icon : '');
    alertDialog.setTitle(locTitle);
    if (canc)
    {
      alertDialog.cancelbtn =function()
      {
        alertDialog.hide.call(alertDialog, false)
      }
      var button = document.createElement("button");
      button.id="MyCancel";
      button.style.fontSize = "10pt";
      button.style.width = "100px";
      button.style.margin = "0px 5px";
      button.className="button";
      button.innerHTML = "Отмена";
      button.linkNum = 2;
      button.appDialog = alertDialog;
      button.onclick = alertDialog.cancelbtn;
      alertDialog.buttonDIV.appendChild(button);
    }
  }
  else
  {
    if (title) alertDialog.setTitle(locTitle);
    if (icon) alertDialog.setIcon(icon);
    if (!canc)
    {
      if(document.getElementById("MyCancel")) document.getElementById("MyCancel").style.display="none";
      alertDialog.callOK = alertDialog.cancelbtn;
    }
    else
    {
      if(document.getElementById("MyCancel")) document.getElementById("MyCancel").style.display="inline";
      else
      {
        alertDialog.cancelbtn =function()
        {
          alertDialog.hide.call(alertDialog, false)
        }
        var button = document.createElement("button");
        button.id="MyCancel";
        button.style.fontSize = "10pt";
        button.style.width = "100px";
        button.style.margin = "0px 5px";
        button.className="button";
        button.innerHTML = "Отмена";
        button.linkNum = 2;
        button.appDialog = alertDialog;
        button.onclick = alertDialog.cancelbtn;
        alertDialog.buttonDIV.appendChild(button);
      }
    }
  }
}

function MyAppAlert(msg, title, icon, style, recall, act) {

        if(act=='') MyAppAlertInit(title, icon);
        else MyAppAlertInit(title, icon, 1);
        var str = msg;
        if((str.indexOf('http') + 1)==1)
        {
          alertDialog.setUrl(msg,550,800);
          alertDialog.moveTo(100, 100);
          alertDialog.lblOK="Закрыть";
        }
        else
        {
          alertDialog.setContent('<form name="AlertFrm" id="AlertFrm" onSubmit="return false;">'+msg+'</form>');
          alertDialog.moveTo(alertX, alertY);
        }
        if (style) alertDialog.getContentNode().className='dialog_'+style
        if (recall)        alertDialog.callOK=recall;
        alertDialog.show();
        if (act)
        {
          document.getElementById("MyCancel").style.display="inline";
          document.getElementById(act).focus();
        }
        else
          if(document.getElementById("MyCancel")) document.getElementById("MyCancel").style.display="none";
        if ((alertX == -1) || (alertY == -1)) alertDialog.moveTo(alertX, alertY);
        }

function MyAppConfirm(msg, title, x, y)
{
        MyAppAlertInit(title, '', false);
        if((msg.indexOf('http') + 1)==1)
        {
          alertDialog.setUrl(msg,x,y);
          alertDialog.moveTo(100, 100);
          if(document.getElementById("buttonOk")) document.getElementById("buttonOk").innerHTML="Закрыть";
        }
        else
        {
          alertDialog.setContent(msg);
          alertDialog.moveTo(alertX, alertY);
        }
        alertDialog.show();
        if ((alertX == -1) || (alertY == -1)) alertDialog.moveTo(alertX, alertY);
}
