
function inputy (id) {
  if (uzplny[id]!=1) {
    document.getElementById(id).value = '';
    document.getElementById(id).style.color = '#333';
  }
}

var uzplny = new Array();
function deinput (id) {
  if (document.getElementById(id).value =='') {
    document.getElementById(id).style.color = '#808080';
    if (id=='jmeno') document.getElementById(id).value = 'Tvé jméno..';
    if (id=='web') document.getElementById(id).value = 'Tvůj web..';
    if (id=='rututu') document.getElementById(id).value = 'Tvůj email.. (nebude zobrazen)';
  } else {
    uzplny[id]=1;
  }
}

function textarea (id) {
  if (uzplny[id]!=1) {
    document.getElementById(id).value = "";
    document.getElementById(id).style.color = '#333';
  }
}

function detextarea (id) {
  if (document.getElementById(id).value =='') {
    document.getElementById(id).style.color = '#808080';
    if (id=='pris') document.getElementById(id).value = 'Co se ti honí hlavou?';
  } else {
    uzplny[id]=1;
  }
}


/* AXAJ */
if (window.ActiveXObject) {
  httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
} else {
  httpRequest = new XMLHttpRequest();
}

function processRequest(kde) {
  if (httpRequest.readyState == 4) {
    if(httpRequest.status == 200) {
      document.getElementById(kde).innerHTML = httpRequest.responseText;
          
    deinput ('jmeno'); deinput ('web'); deinput ('rututu'); detextarea('pris');
    } else {
      alert("Chyba pri nacitani stanky "+ httpRequest.status +":"+ httpRequest.statusText);
    }
  }
}

function komentar (spot) {
  var jmeno=document.getElementById('jmeno').value;
  var web=document.getElementById('web').value;
  var mail=document.getElementById('rututu').value;
  var text=document.getElementById('pris').value;

  var check="";
  if (jmeno.length<3 || jmeno=="Tvé jméno..") check="Tvoje jméno je příliž krátké\n";
  if (text.length<3 || text=="Co se ti honí hlavou?") check=check+"Tak toho chceš snař říct víc, ne?\n";
  if (mail.length>3 && mail!="Tvůj email.. (nebude zobrazen)") {
    if (kontrola(mail)==false) check=check+"Todle že má být e-mailová adresa\n";
  }
  
  if (check=="") {
    document.getElementById('poskoment').innerHTML = "<center><img style='margin:2px 0'src='/img/ajax-loader2.gif' /></center>";
    var url = '/pridatkoment.php?jmeno='+jmeno+'&web='+web+'&mail='+mail+'&text='+text+'&spot='+spot;
    httpRequest.open("GET", url, true);
    httpRequest.onreadystatechange= function () {processRequest('poskoment');}

    
    httpRequest.send(null);
  } else {
    alert(check);
  }
}

function kontrola (adresa) {
    var pozice_zavinace = adresa.indexOf("@");
    if (pozice_zavinace < 0)
        return false;
    var cast_pred_zavinacem = adresa.substring(0,pozice_zavinace);
    var cast_po_zavinaci = adresa.substring(pozice_zavinace+1,adresa.length);
    if (cast_po_zavinaci.indexOf("@") >= 0)
        return false;
    if (cast_pred_zavinacem.length <= 0)
        return false;
    if (cast_po_zavinaci.length <= 0)
        return false;
    var pozice_posledni_tecky = cast_po_zavinaci.lastIndexOf(".");
    if (pozice_posledni_tecky < 0)
        return false;
    var pocet_znaku_za_posledni_teckou = cast_po_zavinaci.length - pozice_posledni_tecky - 1;
    if (pocet_znaku_za_posledni_teckou < 2  ||  pocet_znaku_za_posledni_teckou > 3)
        return false;
    var pozice_dvou_tecek_vedle_sebe = adresa.indexOf("..");
    if (pozice_dvou_tecek_vedle_sebe >= 0)
        return false;
    if (cast_pred_zavinacem.charAt(0) == "."  ||  cast_pred_zavinacem.charAt(cast_pred_zavinacem.length-1) == ".")
        return false;
    if (cast_po_zavinaci.charAt(0) == "."  ||  cast_po_zavinaci.charAt(cast_po_zavinaci.length-1) == ".")
        return false;
    return true;
}