//
// fce javascriptu provadene na strane klieta
//

//---------------------------------------------------------
//  otevre okno s datumem
//  sdate: fromat dd.mm.yyyy
function OpenDateWindow(caller) {
  var url = "kalendar.htm?caller=" + caller.name;

  if (caller.value.length > 0)
    url += "&date=" + escape(caller.value);
  else {
    var d = new Date()
    url += "&date=" + escape(d.getDate() + "." + (d.getMonth() + 1) + "." + d.getFullYear());
  }
  
  var features = new String();
  features = "menubar=no,scrollbars=no,status=no,toolbar=no,location=no,";
  features += "resizable=no,alwaysRaised=yes,height=145,width=200";
  wdate=window.open(url, "wdate", features);
  return;
}

//-------------------------------------------------------
// otevira okno prohlizece
// bez menu, toolbar apod
function OpenAuxWindw(url,name,h,w) {
  window.open(url,"auxwin","location=no,menubar=no,width=w,height=h,resizable=no,toolbar=no")
}

//-------------------------------------------------------
//  kontrola platnosti hesla:
//	- min 6 znaku
//	- zadne spec znaky
//	- zadne ceske znaky
function CheckPasswd(passwd) {
  if (passwd.length<6) return false;
  if (passwd != spcEscape(passwd)) return false;
  if (passwd != czEscape(passwd)) return false;
  return true;
}

//-------------------------------------------------------
//  vytvori retezec acl z checkboxu formulare
//  checkboxy: name=cl1 ...
//  pocet checkboxu: clNum
//  vystup do pole acl
function SetAcl(form) {
  var acl = new String();
  var n=parseInt(form.clNum.value);
  if (isNaN(n)) return acl;
  var chb;

  for (i=0;i<n;i++) {
    chb = form.elements["cl" + i];
    if (chb.checked) acl += chb.value + ";"
  }
  return acl;
}  // fce SetAcl();

//-------------------------------------------------------
//  kontrola formulare pro noveho uzivatele:
function CheckNewUser(form) {
  // jmeno, prijmeni
  if (form.fname.value.length == 0) {
    window.alert("Vyplňte prosím křestní jméno!");
    return false;
  }
  if (form.lname.value.length == 0) {
    window.alert("Vyplňte prosím příjmení!");
    return false;
  }
  if (form.email.value.length == 0) {
    window.alert("Vyplňte prosím e-mail!");
    return false;
  }
  if ((form.email.value.indexOf("@") == -1) || (form.email.value.indexOf(".") == -1) ) {
    window.alert("Vyplňte prosím platný e-mail!");
    return false;
  }
  if (form.usr.value.length == 0) {
    window.alert("Vyplňte prosím uživatelovo id!");
    return false;
  }
  if (form.passwd.value.length == 0) {
    window.alert("Zadejte prosím heslo!");
    return false;
  }
  if (form.passwd.value != form.passwdc.value) {
    window.alert("Heslo a potvrzení hesla je neshodují!");
    return false;
  }
  if (!CheckPasswd(form.passwd.value)) {
    window.alert("Heslo musí mít alespoň 6 znaků.\nMusí být složeno jen z písmen a číslic.\nNesmí obsahovat české znaky.\n");
    return false;
  }
  form.acl.value = SetAcl(form);
  if (form.acl.value.length == 0) return (window.confirm("Uživatel nemá nastaveny přístupy: acl:" + form.acl.value + "!"));
  
  return true;
}

//---------------------------------------------------------
function CheckNewPasswd(form) {
  // jmeno, prijmeni
  if (form.passwd.value.length == 0) {
    window.alert("Zadejte prosím heslo!");
    return false;
  }
  if (form.passwd.value != form.passwdc.value) {
    window.alert("Heslo a potvrzení hesla je neshodují!");
    return false;
  }
  if (!CheckPasswd(form.passwd.value)) {
    window.alert("Heslo musí mít alespoň 6 znaků.\nMusí být složeno jen z písmen a číslic.\nNesmí obsahovat české znaky.\n");
    return false;
  }
  
  return true;
}

//---------------------------------------------------------
//  urci id podle jmena a prijmeni:
function GetId(form) {
  var id = new String();
  id = form.fname.value.substr(0,1) + form.lname.value.substr(0,7);
  id = id.toLowerCase();
  id = czEscape(spcEscape(id));

  form.usr.value=id;
  return
}

//---------------------------------------------------------
//  escape fce:
function spcEscape(istr) {
  var pstr = new String(istr.toLowerCase());
  var ostr = new String();
  var i=0;

  for (i=0; i<pstr.length;i++) {
    if (pstr.charAt(i) == " ") ostr += "_";
    else if (pstr.charAt(i) == "\"") continue;
    else if (pstr.charAt(i) == "¨") continue;
    else if (pstr.charAt(i) == "!") continue;
    else if (pstr.charAt(i) == "?") continue;
    else if (pstr.charAt(i) == "\\") continue;
    else if (pstr.charAt(i) == "@") continue;
    else if (pstr.charAt(i) == "$") continue;
    else if (pstr.charAt(i) == "^") continue;
    else if (pstr.charAt(i) == "*") continue;
    else if (pstr.charAt(i) == "/") continue;
    else if (pstr.charAt(i) == "%") continue;
    else if (pstr.charAt(i) == "^") continue;
    else if (pstr.charAt(i) == "&") continue;
    else if (pstr.charAt(i) == ".") continue;
    else if (pstr.charAt(i) == ",") continue;
    else if (pstr.charAt(i) == "§") continue;
    else ostr += pstr.charAt(i);
  }

  return ostr;
}

function czEscape(istr) {
  var pstr = new String(istr.toLowerCase());
  var ostr = new String();
  var i=0;

  for (i=0; i<pstr.length;i++) {
    if (pstr.charAt(i) == " ") ostr += "_";
    else if (pstr.charAt(i) == "ě") ostr += "e";
    else if (pstr.charAt(i) == "š") ostr += "s";
    else if (pstr.charAt(i) == "č") ostr += "c";
    else if (pstr.charAt(i) == "ř") ostr += "r";
    else if (pstr.charAt(i) == "ž") ostr += "z";
    else if (pstr.charAt(i) == "ý") ostr += "y";
    else if (pstr.charAt(i) == "á") ostr += "a";
    else if (pstr.charAt(i) == "í") ostr += "i";
    else if (pstr.charAt(i) == "é") ostr += "e";
    else if (pstr.charAt(i) == "ó") ostr += "o";
    else if (pstr.charAt(i) == "ů") ostr += "u";
    else if (pstr.charAt(i) == "ú") ostr += "u";
    else if (pstr.charAt(i) == "ť") ostr += "t";
    else if (pstr.charAt(i) == "ď") ostr += "d";
    else if (pstr.charAt(i) == "ň") ostr += "n";
    else ostr += pstr.charAt(i);
  }

  return ostr;
}


//-------------------------------------------------------
//  kontrola formulare pro noveho uzivatele:
function CheckNewFolder(form) {
  // jmeno, prijmeni
  if (form.id.value.length == 0) {
    window.alert("Vyplňte název složky!");
    return false;
  }
  if (form.text.value.length == 0) {
    window.alert("Vyplňte popis složky!");
    return false;
  }  
  return true;
}

function CheckOrdForm(myForm) {
  if ( (myForm.fname.value.length == 0) || (myForm.lname.value.length == 0) ){
    window.alert("Vyplňte prosím své jméno a příjmení!");
    return false;
  }
  else if ( (myForm.from.value.length == 0) || (myForm.from.value.indexOf("@") <= 0) ||
            (myForm.from.value.indexOf("@") == (myForm.from.value.length-1)) ){
    window.alert("Vyplňte prosím svou e-mailovou adresu!");
    return false;
  }
  else if ( (myForm.tel.value.length == 0) && (myForm.mobil.value.length == 0) ){
    window.alert("Uveďte prosím své telefonní číslo (pevnou linku nebo mobil)!");
    return false;
  }
  var n=parseInt(myForm.pocet.value);
  if ( (myForm.pocet.value.length == 0) || ( isNaN(n) ) || (n<=0) ) {
    myForm.pocet.value=window.prompt("Kolik vstupenek objednáváte?");
    n=parseInt(myForm.pocet.value);
    if ( isNaN(n) || (n<=0) ) {
      window.alert("Vyplňte prosím platný počet vstupenek!");
      return false;
    }
  }
  return true;
}

