function getWindowWidth() {
  var windowWidth = 0;
  if( typeof(window.innerWidth) == 'number' ) {
    //Non-IE
    windowWidth = window.innerWidth;
  }
  else if( document.documentElement && ( document.documentElement.clientWidth ) ) {
    //IE 6+ in 'standards compliant mode'
    windowWidth = document.documentElement.clientWidth;
  }
  else if( document.body && ( document.body.clientWidth) ) {
    //IE 4 compatible
    windowWidth = document.body.clientWidth;
  }
  return windowWidth;
}

function getWindowHeight() {
  var windowHeight = 0;
  if( typeof(window.innerWidth) == 'number' ) {
    //Non-IE
    windowHeight = window.innerHeight;
  }
  else if( document.documentElement && ( document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    windowHeight = document.documentElement.clientHeight;
  }
  else if( document.body && ( document.body.clientHeight ) ) {
    //IE 4 compatible
    windowHeight = document.body.clientHeight;
  }
//   alert(windowHeight+" - "+document.body.clientHeight+" - "+document.documentElement.clientHeight)
  return windowHeight;
}

function isClientIE() {
  if(window.navigator.appName.indexOf('Microsoft') > -1) {
    if(window.navigator.appVersion.indexOf('MSIE 6') > -1){
      return "6";
    }
    else if(window.navigator.appVersion.indexOf('MSIE 7') > -1){
      return "7";
    }
    if(window.navigator.appVersion.indexOf('MSIE 8') > -1){
      return "8";
    }
    if(window.navigator.appVersion.indexOf('MSIE 9') > -1){
      return "9";
    }
  }
  return false;
}

function ValidateArticle(f, lang) {
  if(!lang) {
    lang = 'cs';
  }
  var text = f['text'].value;
  var sign = f['sign'].value;
  var secure = f['secure'].value;
  var email = f['email'].value;
  var message = '';
  if(text == '') {
    if(lang == 'en') {
      message += 'You did not fill text<br />';
    }
    else {
      message += 'Nevyplnil/a jste text<br />';
    }
  }
  if(sign == '') {
    if(lang == 'en') {
      message += 'You did not fill sign<br />';
    }
    else {
      message += 'Nevyplnil/a jste podpis<br />';
    }
  }
  if(email == '') {
    if(lang == 'en') {
      message += 'You did not fill e-mail<br />';
    }
    else {
      message += 'Nevyplnil/a jste e-mail<br />';
    }
  }
  if(secure == '') {
    if(lang == 'en') {
      message += 'You did not fill confirmation<br />';
    }
    else {
      message += 'Nevyplnil/a jste ověření<br />';
    }
  }
  if(message != '') {
    message += '<div><br /><input type="button" value="OK" onclick="removeModal();"/></div>';
    createModal(message);
    return false;
  }
  return true; 
}

function createModal(html) {
  if(html != "") {
    var width = "400";
    var height = "250";
    var IE = isClientIE();
    var windowWidth = getWindowWidth();
    var windowHeight = getWindowHeight();
  // Global modal cover
    var modalDiv = document.createElement("div");
    modalDiv.setAttribute("id", "MODAL");
    modalDiv.style.position = "absolute";
    modalDiv.style.left = "0";
    modalDiv.style.top = "0";
    modalDiv.style.zIndex = "1000000000";
    document.body.appendChild(modalDiv);
  // Eement with mask - must not contain any content
    var maskDiv = document.createElement("div");
    maskDiv.setAttribute("id", "MASK");
    maskDiv.style.width = "100%";
    maskDiv.style.height = document.body.clientHeight+'px';
    maskDiv.style.backgroundColor = "black";
    if(IE == "6") {
      maskDiv.style.position = "absolute";
      window.scrollTo(0,0);
    }
    else {
      maskDiv.style.position = "fixed";
    }
    if(IE) {
      maskDiv.style.filter = "alpha(opacity=50)";
    }
    else {
      maskDiv.style.opacity = "0.5";
    }
    maskDiv.setAttribute("onclick", "removeModal();");
    maskDiv.style.cursor = "pointer";
  // 1.
    modalDiv.appendChild(maskDiv);
  // AlertBox
    var modalBoxDiv = document.createElement("div");
    modalBoxDiv.setAttribute("id", "MODALBOX");
    modalBoxDiv.style.width = width+"px";
    modalBoxDiv.style.height = height+"px";
    if(IE == "6") {
      modalBoxDiv.style.position = 'absolute';
    }
    else {
      modalBoxDiv.style.position = 'fixed';
    }
    var marginWidth = getWindowWidth() - width;
    var marginHeight = getWindowHeight() - height - 30;
    modalBoxDiv.style.top = marginHeight / 2 +'px';
    modalBoxDiv.style.left = marginWidth / 2 +'px';
    
    var modalContentDiv = document.createElement("div");
    modalContentDiv.setAttribute("id", "MODALCONTENT");
    modalContentDiv.style.width = width+"px";
    modalContentDiv.style.height = height+"px";
    modalContentDiv.style.overflow = 'auto';
  
    modalContentDiv.innerHTML = html;
    modalBoxDiv.appendChild(modalContentDiv);
    
    modalDiv.appendChild(modalBoxDiv);
    document.getElementsByTagName("body")[0].appendChild(modalDiv);
  }
}

function removeModal() {
  var modal = document.getElementById("MODAL");
  if(modal) {
    modal.parentNode.removeChild(modal);
    var action = document.getElementById('ACTIONBLOCK');
  	if(action) {
  		action.style.display = 'none';
  		$("#ACTIONBLOCK").slideToggle("slow");
  	}
  }
}

