/*    ./javascript/ajax.js    */

// Inicializace Ajaxu
var xhr;
// For IE7+, Firefox, Chrome, Opera, Safari
if (window.XMLHttpRequest) {
  xhr = new XMLHttpRequest();
}
// For IE6, IE5
if (window.ActiveXObject) {
  xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
if(!xhr)
  alert("Došlo&nbsp;k&nbsp;chybě při vytváření objektu XMLHttpRequest!\n\nNainstalujte si prosím novější prohlížeč!");



// Funkce pro práci s Ajaxem

function check_field(field, type, error) {

  switch (type) {
    case 'language_from':
      if (xhr) {
        xhr.open("GET", "./linguistics/check_field.php?value="+field.value+"&type=language&kodovani=č",true);
        xhr.onreadystatechange=function() {
          switch (xhr.readyState) {
            case 4:
              if (xhr.status == 200) {
                if (xhr.responseText=="neznámý jazyk") {
                  field.style.color="#aa0000";
                  error.innerHTML = xhr.responseText+" z";
                } else {
                  field.style.color="#000000";
                  field.value = xhr.responseText;
                  error.innerHTML = "";
                }
              }
              break;
          };
        };
        xhr.send(null);
      }
      break;
      
    case 'language_to':
      if (xhr) {
        xhr.open("GET", "./linguistics/check_field.php?value="+field.value+"&type=language&kodovani=č",true);
        xhr.onreadystatechange=function() {
          switch (xhr.readyState) {
            case 4:
              if (xhr.status == 200) {
                if (xhr.responseText=="neznámý jazyk") {
                  field.style.color="#aa0000";
                  error.innerHTML = xhr.responseText+" do";
                } else {
                  field.style.color="#000000";
                  field.value = xhr.responseText;
                  error.innerHTML = "";
                }
              }
              break;
          };
        };
        xhr.send(null);
      }
      break;

    case 'phone':
      old_cislo=field.value;
      new_cislo="";
      
      if (old_cislo.substr(0,4)=="+420") {old_cislo=old_cislo.substr(4);}
      
      for (i=0; i<old_cislo.length; i++) {
        cislice = old_cislo.charAt(i);
        if (cislice>=0 && cislice<=9 && cislice!=" ") {new_cislo+=cislice.toString();}
      }
      
      if (new_cislo.length!=9) {
        field.style.color="#aa0000";
        error.innerHTML = "zadejte 9ti místné číslo";
      } else {
        field.style.color="#000000";
        field.value=new_cislo.substr(0,3)+" "+new_cislo.substr(3,3)+" "+new_cislo.substr(6,3);
        error.innerHTML = "";
      }
      break;

    case 'email':
      norma = /^[^.]+(\.[^.]+)*@([^.]+[.])+[a-z]{2,4}$/;
  
      if (field.value.search(norma)!=0) {
        field.style.color="#aa0000";
        error.innerHTML = "špatný tvar e-mailu";
      } else {
        field.style.color="#000000";
        error.innerHTML = "";
      }
      break;

  };

}
