// global variables //
var TIMER = 1;
var SPEED = 20;
var WRAPPER = 'container';

// calculate the current window width //
function pageWidth() {
  return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}

// calculate the current window height //
function pageHeight() {
  return window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}

// calculate the current window vertical offset //
function topPosition() {
  return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}

// calculate the position starting at the left of the window //
function leftPosition() {
  return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}

// build/show the dialog box, populate the data and call the fadeDialog function //
function showDialog(title,message,misc,misc2,misc3,type,autohide) {
  if(!type) {
    type = 'error';
  }
   if(!misc) {
    misc = '';
  }
  if(!misc2) {
    misc2 = '';
  }
   if(!misc3) {
    misc3 = '';
  }
  var dialog;
  var dialogheader;
  var dialogclose;
  var dialogtitle;
  var dialogcontent;
  var dialogmask;
  if(!document.getElementById('dialog')) {
    dialog = document.createElement('div');
    dialog.id = 'dialog';
    dialogheader = document.createElement('div');
    dialogheader.id = 'dialog-header';
    dialogtitle = document.createElement('div');
    dialogtitle.id = 'dialog-title';
    dialogclose = document.createElement('div');
    dialogclose.id = 'dialog-close'
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';
    dialogmask = document.createElement('div');
    dialogmask.id = 'dialog-mask';
    document.body.appendChild(dialogmask);
    document.body.appendChild(dialog);
    dialog.appendChild(dialogheader);
    dialogheader.appendChild(dialogtitle);
    dialogheader.appendChild(dialogclose);
    dialog.appendChild(dialogcontent);;
    dialogclose.setAttribute('onclick','hideDialog()');
    dialogclose.onclick = hideDialog;
  } else {
    dialog = document.getElementById('dialog');
    dialogheader = document.getElementById('dialog-header');
    dialogtitle = document.getElementById('dialog-title');
    dialogclose = document.getElementById('dialog-close');
    dialogcontent = document.getElementById('dialog-content');
    dialogmask = document.getElementById('dialog-mask');
    dialogmask.style.visibility = "visible";
    dialog.style.visibility = "visible";
  }
  dialog.style.opacity = .00;
  dialog.style.filter = 'alpha(opacity=0)';
  dialog.alpha = 0;
  var width = pageWidth();
  var height = pageHeight();
  var left = leftPosition();
  var top = topPosition();
  var dialogwidth = dialog.offsetWidth;
  var dialogheight = dialog.offsetHeight;
  var topposition = top + (height / 3) - (dialogheight / 2);
  var leftposition = left + (width / 2) - (dialogwidth / 2);
  dialog.style.top = topposition + "px";
  dialog.style.left = leftposition + "px";
  dialogheader.className = type + "header";
  dialogtitle.innerHTML = title;
  dialogcontent.className = type;
  if (type == 'prompt') { //ürlap a customalertben
	if (misc3 == 'CHG') //jelszócsere
		dialogcontent.innerHTML = message+'<br/><br/>'+
		'<form action="index.php?menu='+misc2+'&act=newpwd" method="post">'+
		'<input type="hidden" name="eml" value="'+misc+'"/>'+
		'<input id="0ipw" type="password" size="10" name="jelsz" style="border: solid #000 1px; background-color:#ddd; color:#000"/><br />'+
		'<input type="submit" value="Jelszó csere" style="margin-top: 4px;padding: 2px 6px 2px 6px; border: solid #000 1px; background: url(\'customalert/images/prompt_header.gif\') repeat-x left top;color:#fff"/>'+
		'</form>';
	if (misc3 == 'REM') //jelszóemlékeztető küldés lehetősége
		dialogcontent.innerHTML = message+'<br/><br/>'+
		'<form action="index.php?menu='+misc2+'&act=remember" method="post">'+
		'<input type="hidden" name="eml" value="'+misc+'"/><br />'+
		'<input type="submit" value="Új jelszó kérés" style="margin-top: 4px;padding: 2px 6px 2px 6px; border: solid #000 1px; background: url(\'customalert/images/prompt_header.gif\') repeat-x left top;color:#fff"/>'+
		'</form>';
	if (misc3 == 'LOG') //bejelentkezés
		dialogcontent.innerHTML = message+'<br/><br/>'+
		'<form action="index.php?menu='+misc2+'&act=logpw" method="post">'+
		'<input type="hidden" name="eml" value="'+misc+'"/>'+
		'<input id="0ipw" type="password" size="25" name="jelsz" style="border: solid #000 1px; background-color:#ddd; color:#000"/><br />'+
		'<input type="submit" value="BEJELENTKEZÉS" style="margin-top: 4px;padding: 2px 6px 2px 6px; border: solid #000 1px; background: url(\'customalert/images/prompt_header.gif\') repeat-x left top;color:#fff"/>'+
		'</form>';
	if (misc3 == 'REG') //regisztráció
		dialogcontent.innerHTML = message+'<br/><br/>'+
		'<form action="index.php?menu='+misc2+'&act=reg2" method="post">'+
		'<input id="0ipw" type="text" size="25" name="eml" style="border: solid #000 1px; background-color:#ddd; color:#000"/><br />'+
		'<input type="submit" value="REGISZTRÁCIÓ" style="margin-top: 4px;padding: 2px 6px 2px 6px; border: solid #000 1px; background: url(\'customalert/images/prompt_header.gif\') repeat-x left top;color:#fff"/>'+
		'</form><script type="text/javascript">document.getElementById[\'0ipw\'].focus();</script>';
	if (misc3 == 'SIG') //Feliratkozás hírlevélre
		dialogcontent.innerHTML = message+'<br/><br/>'+
		'<form action="index.php?menu='+misc2+'&act=hir2" method="post">'+
		'<input id="0ipw" type="text" size="25" name="eml" style="border: solid #000 1px; background-color:#ddd; color:#000"/><br />'+
		'<input type="submit" value="FELIRATKOZÁS" style="margin-top: 4px;padding: 2px 6px 2px 6px; border: solid #000 1px; background: url(\'customalert/images/prompt_header.gif\') repeat-x left top;color:#fff"/>'+
		'</form><script type="text/javascript">document.getElementById[\'0ipw\'].focus();</script>';
	}
  else dialogcontent.innerHTML = message;
  var content = document.getElementById(WRAPPER);
  dialogmask.style.height = content.offsetHeight + 'px';
  dialog.timer = setInterval("fadeDialog(1)", TIMER);
  dialogclose.style.visibility = "visible";
  if (autohide) { window.setTimeout("hideDialog()", (autohide * 1000)); }
}

// hide the dialog box //
function hideDialog() {
  var dialog = document.getElementById('dialog');
  clearInterval(dialog.timer);
  dialog.timer = setInterval("fadeDialog(0)", TIMER);
}

// fade-in the dialog box //
function fadeDialog(flag) {
  if(flag == null) {
    flag = 1;
  }
  var dialog = document.getElementById('dialog');
  var value;
  if(flag == 1) {
    value = dialog.alpha + SPEED;
  } else {
    value = dialog.alpha - SPEED;
  }
  dialog.alpha = value;
  dialog.style.opacity = (value / 100);
  dialog.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(dialog.timer);
    dialog.timer = null;
	if (document.getElementById('0ipw')) document.getElementById('0ipw').focus()
  } else if(value <= 1) {
    dialog.style.visibility = "hidden";
    document.getElementById('dialog-mask').style.visibility = "hidden";
    clearInterval(dialog.timer);
  }
}
