//common

/** set tag's property disabled to false, to editable input
 *@param element
 */
function accessBrowse(checkbox, id){
  var element = document.getElementById(id);
  if(element){
    if(checkbox.checked) element.disabled = false;
      else element.disabled = true;
  }
  return;
}

/** show confirm window
 *@param url - next location
 *@param text - text in window
 */
function confirmOperation(url, text){
  go = window.confirm(text);
  if(go) location.href = url;
  return;
}

function check_checkbox(id, mod){
  var ch = document.getElementById(id);
  switch(mod){
    case 0:  if(ch) ch.checked = !ch.checked; break; //invert value
    case 1:  if(ch) ch.checked = true; break; // checked always
    case 2:  if(ch) ch.checked = false; break; // unchecked always
    default :  if(ch) ch.checked = !ch.checked;
  }
  return;
}

function getEuro(kc, kurs){
  kc = kc.replace(" ","");
  var reg = new RegExp("^[0-9]+$");
  var euro = 0;
  if(!reg.test(kc)){
    alert("Cena obsahuje nepovolené znaky.");
  }else euro = kurs ? Math.round(kc / kurs) : 0;

  return euro;
}

/** change class of itself, it doesn't matter of element ID
 *@param elem - element
 *param newclass - required class for change
 */
function classChangeSelf(elem, newclass){
  elem.className = newclass;
}

/** e-mail control
 *@param email - input element
 *param alert_msg - alerting message
 */
function kontrola_emailu(email, alert_msg){
  if(window.RegExp){
    maska = new RegExp("^[^.]+(\.[^.]+)*@([^.]+[.])+[a-z]{2,3}$");
    if(email){
      if (!maska.test(email.value)){
        window.alert(alert_msg);
        email.focus();
        return false;
      }else return true;
    }else return true;
  }else return true;
}

/** zmeni select pro ceny
 *@param sel - select
 */
function zmenCeny(sel){

  if(sel[sel.selectedIndex].value == 'P'){
    //prodej
    document.getElementById('s_cena_najem').style.display = 'none';
    document.getElementById('s_cena').style.display = 'block';
    document.getElementById('s_cena_najem_flag').value = 0;
  }else{
    //najem
    document.getElementById('s_cena').style.display = 'none';
    document.getElementById('s_cena_najem').style.display = 'block';
    document.getElementById('s_cena_najem_flag').value = 1;
  }

  return;
}

/** zmeni select pro ceny
 *@param sel - select
 */
function zmenVelikosti(sel){

  if(sel[sel.selectedIndex].value != 'P'){
    //normal
    document.getElementById('s_vel_pozemek').style.display = 'none';
    document.getElementById('s_vel').style.display = 'block';
    document.getElementById('s_vel_pozemek_flag').value = 0;
  }else{
    //rozloha
    document.getElementById('s_vel').style.display = 'none';
    document.getElementById('s_vel_pozemek').style.display = 'block';
    document.getElementById('s_vel_pozemek_flag').value = 1;
  }

  return;
}

/** zmeni select pro mesta
 *@param sel - select
 */

function zmenMesta(sel){
  for(i = 0; i < 14; i++){
    if(sel.selectedIndex == i){
      mesto_id = "mesta_kraj_" + i;
      document.getElementById(mesto_id).style.display = 'block';
      document.getElementById(mesto_id).disabled = false;
      setDisplayNoneMesta(i);
    }
  }


  return;
}

function setDisplayNoneMesta(visible){
  for(i = 0; i < 14; i++){
    if(i != visible){
      mesto_id = "mesta_kraj_" + i;
      document.getElementById(mesto_id).style.display = 'none';
      document.getElementById(mesto_id).disabled = true;
    }
  }
  
  return;
}


function hideGeneralDiv(id){
  var e = document.getElementById(id);
  if(e) e.style.display = 'none';
  return;
}

function showGeneralDiv(id){
  var e = document.getElementById(id);
  if(e) e.style.display = 'block';
  return;
}

/* NEMMOVITOST INFO ********************************************************** */
var aktual_foto = 0;

function showC(imgID, x, y){
  var foto = document.getElementById(imgID);
  var src = foto.src;
  src = src.replace(/_a/g, "_c");

  //hlavni foto
  var hlavni_foto = document.getElementById('hlavni_obr');
  hlavni_foto.src = src;
  hlavni_foto.style.width = x + "px";
  hlavni_foto.style.height = y + "px";

  //$imgID = 'obr_' . $key;
  aktual_foto = imgID.replace(/obr_/g, "");
}
function openFotos(nid){
  msg = window.open('fotos.php?nid=' + nid + '&i=' + aktual_foto, '_blank', 'toolbar=no, menubar=no, location=no, directories=no, scrollbars=no, resizable=no, status=no, top=50, width=720, height=520');
}

function openPoslat_znamemu(nid){
  msg = window.open('poslat-znamemu.php?nid=' + nid, '_blank', 'toolbar=no, menubar=no, location=no, directories=no, scrollbars=no, resizable=no, status=no, top=50, width=400, height=330');
}

function openFotogalerii(nid){
  msg = window.open('fotogalerie.php?nid=' + nid, '_blank', 'toolbar=no, menubar=no, location=no, directories=no, scrollbars=yes, resizable=no, status=no, top=50, width=740, height=580');
}

function openMam_zajem(nid){
  msg = window.open('zajem.php?nid=' + nid, '_blank', 'toolbar=no, menubar=no, location=no, directories=no, scrollbars=no, resizable=no, status=no, top=50, width=400, height=370');
}

function openFotogalerii_tisk(nid){
  msg = window.open('fotogalerie.php?nid=' + nid + '&tisk=1', '_blank', 'toolbar=no, menubar=no, location=no, directories=no, scrollbars=yes, resizable=yes, status=no, top=50, width=750');
}

function openLinkMap(url){
  msg = window.open(url, '_blank', 'toolbar=yes, menubar=yes, location=yes, directories=yes, scrollbars=yes, resizable=yes, status=yes, top=50, width=740, height=580');
}


/* POSLAT ZNAMEMU ************************************************************ */
function kontrola(text1, text2, text3){
  var bool_1 = kontrola_emailu(document.forms['form_znamemu'].prijemce, text1);
  var bool_2 = kontrola_emailu(document.forms['form_znamemu'].odesilatel, text2);
  var bool_3 = (document.forms['form_znamemu'].zprava.value == "") ? false : true;
  if(!bool_3) alert(text3);
  if(bool_1 && bool_2 && bool_3) document.forms['form_znamemu'].submit();
    else return false;
}

/* MAM ZAJEM ***************************************************************** */
function kontrola2(text1, text2, text3){
  var bool_2 = (document.forms['form_mamzajem'].jmeno.value == "") ? false : true;
  if(!bool_2) alert(text2);
  
  mail = document.forms['form_mamzajem'].email;
  if(mail.value == '@') mail.value = '';

  bool_1 = false;
  if(mail.value){
    bool_1 = kontrola_emailu(mail, text3);
  }

  if(bool_1 || (document.forms['form_mamzajem'].telefon.value != "")){
    return true;
  }else{
    alert(text1);
    return false;
  } 
  
}

function isMSIE(){
  var browser = navigator.appName;
  return (browser == "Microsoft Internet Explorer") ? true : false;
}
