// knihovna JavaScript funkci pro podporu stranek s pruvodci

// funkce pro vraceni "demo" hodnot do formulare pro generovani zadosti
function getDemoValue(customer,cert,id) {
// customer ... typ zakaznika (PO, OSVC, FO)
// cert     ... typ certifikatu (sign, mark)
  retvalue = "";
  if (customer == "PO") {
    switch (id) {
      case "o_name": retvalue = "My Organization, ltd."; break;
      case "o_ic": retvalue = "12345678"; break;
      case "ou": retvalue = "accounts department"; break;
      case "cn": if (cert == "sign") retvalue = "John Doe";
                 if (cert == "mark") retvalue = "E-registry";
                 break;
      case "persnum": retvalue = "123"; break;
      case "perstitle": retvalue = "account manager"; break;
      case "email": if (cert == "sign") retvalue = "doe@myorg.com";
                    if (cert == "mark") retvalue = "e-registry@myorg.com";
                    break;
      default: retvalue = "";
    }
  }
  if (customer == "OSVC") {
    switch (id) {
      case "o_name": retvalue = "John Doe"; break;
      case "o_ic": retvalue = "12345678"; break;
      case "ou": retvalue = ""; break;
      case "cn": if (cert == "sign") retvalue = "John Doe";
                 if (cert == "mark") retvalue = "John Doe, auto-response";
                 break;
      case "persnum": retvalue = "1"; break;
      case "perstitle": retvalue = ""; break;
      case "email": if (cert == "sign") retvalue = "john@doe.cz";
                    if (cert == "mark") retvalue = "autoresponse@doe.cz";
                    break;
      default: retvalue = "";
    }
  }
  if (customer == "FO") {
    switch (id) {
      case "cn": if (cert == "sign") retvalue = "Alice Smith";
                 if (cert == "mark") retvalue = "Automatic responder";
                 break;
      case "email": retvalue = "alice.smith@myemail.com"; break;
      case "l_street": retvalue = "Oxford street 28"; break;
      case "l_city": retvalue = "London"; break;
      default: retvalue = "";
    }
  }
  return retvalue;
}

// funkce pro vyplneni poli formulare pro generovani klicu ukazkovymi hodnotami
function keygenDemoValues(frm,customer,cert) {
// frm      ... objekt s formularem
// customer ... typ zakaznika (PO, OSVC, FO)
// cert     ... typ certifikatu (sign, mark)
  if ((customer == "PO")||(customer == "OSVC")) {
    frm.o_name.value = getDemoValue(customer,cert,"o_name");
    frm.o_ic.value = getDemoValue(customer,cert,"o_ic");
    frm.ou.value = getDemoValue(customer,cert,"ou");
    frm.email.value = getDemoValue(customer,cert,"email");
    if (cert == "sign") {
      frm.cn_sign.value = getDemoValue(customer,cert,"cn");
      frm.persnum.value = getDemoValue(customer,cert,"persnum");
      frm.perstitle.value = getDemoValue(customer,cert,"perstitle");
    }
    if (cert == "mark") frm.cn_mark.value = getDemoValue(customer,cert,"cn");
  }
  if (customer == "FO") {
    if (cert == "sign") frm.cn_sign.value = getDemoValue(customer,cert,"cn");
    if (cert == "mark") frm.cn_mark.value = getDemoValue(customer,cert,"cn");
    frm.email.value = getDemoValue(customer,cert,"email");
    frm.l_street.value = getDemoValue(customer,cert,"l_street");
    frm.l_city.value = getDemoValue(customer,cert,"l_city");
  }
}

// funkce pro kontrolu, zda ve formulari pro generovani klicu nejsou nastaveny jen ukazkove hodnoty
function keygenOnlyDemoValues(frm,customer,cert) {
// frm      ... objekt s formularem
// customer ... typ zakaznika (PO, OSVC, FO)
// cert     ... typ certifikatu (sign, mark)
  retvalue =  true;
  if ((customer == "PO")||(customer == "OSVC")) {
    if (cert == "sign") retvalue = (frm.cn_sign.value == getDemoValue(customer,cert,"cn"))&&
                                   (frm.persnum.value == getDemoValue(customer,cert,"persnum"))&&
		                   (frm.perstitle.value == getDemoValue(customer,cert,"perstitle")); 
    if (cert == "mark") retvalue = (frm.cn_mark.value == getDemoValue(customer,cert,"cn")); 
    if (retvalue) retvalue = (frm.o_name.value == getDemoValue(customer,cert,"o_name"))&&
                             (frm.o_ic.value == getDemoValue(customer,cert,"o_ic"))&&
                             (frm.ou.value == getDemoValue(customer,cert,"ou"))&&
                             (frm.email.value == getDemoValue(customer,cert,"email"));
  }
  if (customer == "FO") {
    if (cert == "sign") retvalue = (frm.cn_sign.value == getDemoValue(customer,cert,"cn"));
    if (cert == "mark") retvalue = (frm.cn_mark.value == getDemoValue(customer,cert,"cn")); 
    if (retvalue) retvalue = (frm.email.value == getDemoValue(customer,cert,"email"))&&
                             (frm.l_street.value == getDemoValue(customer,cert,"l_street"))&&
                             (frm.l_city.value == getDemoValue(customer,cert,"l_city"));
  }
  return retvalue;
}

function keygenCheckValues(frm,customer,cert) {
// frm      ... objekt s formularem
// customer ... typ zakaznika (PO, OSVC, FO)
// cert     ... typ certifikatu (sign, mark)
  var rxMailAddress = /^[a-z0-9_\-\.]+@[a-z0-9_\-]+(\.[a-z0-9_\-]+)+$/i;
  if (customer == "PO") {
    if (frm.o_name.value == "") {
      window.alert("The name of the organization is missing."); frm.o_name.focus(); return false;
    } 
    if (frm.o_ic.value == "") {
      window.alert("The ID No of the organization is missing."); frm.o_ic.focus(); return false;
    } 
    if ((cert == "sign")&&(frm.cn_sign.value == "")) {
      window.alert("Your name and surname is missing."); frm.cn_sign.focus(); return false;
    } 
    if ((cert == "sign")&&(frm.persnum.value == "")) {
      window.alert("Employee number is missing."); frm.persnum.focus(); return false;
    } 
    if ((cert == "mark")&&(frm.cn_mark.value == "")) {
      window.alert("The name of the certificate is missing."); frm.cn_mark.focus(); return false;
    }
    if (frm.email.value == "") { // varovani pri nezadane e-mailove adrese
      if (cert == "sign") {
        window.alert("The e-mail address is missing."); frm.email.focus(); return false;
      }
      if (cert == "mark") {
        retval = window.confirm("Warning! The certificate without e-mail address can not be used in e-mail communication.\n\nAre you sure you want to continue without entered e-mail address?");
        if (!retval) frm.email.focus();
        return retval;
      }
    } else if (!rxMailAddress.exec(frm.email.value)) { // kontrola zadane e-mailove adresy
      window.alert("E-mail address is not in a correct form:\n     mailbox@domain.cz"); frm.email.focus(); return false;
    } 
    if (keygenOnlyDemoValues(frm,customer,cert)) { // varovani pri ponechanych demo hodnotach
      retval = window.confirm("All the form fields contain example values.\nWe recommend to enter real data to the form.\n\nAre you sure you want to continue with the example values entered?");
      return retval;
    }
    return true;
  } // konec kontrol pro "PO"
  if (customer == "OSVC") {
    if (frm.o_name.value == "") {
      window.alert("Your business name is missing."); frm.o_name.focus(); return false;
    } 
    if (frm.o_ic.value == "") {
      window.alert("Your ID No is missing."); frm.o_ic.focus(); return false;
    } 
    if ((cert == "sign")&&(frm.cn_sign.value == "")) {
      window.alert("Your name and surname is missing."); frm.cn_sign.focus(); return false;
    } 
    if ((cert == "sign")&&(frm.persnum.value == "")) frm.persnum.value="1";
    if ((cert == "mark")&&(frm.cn_mark.value == "")) {
      window.alert("The name of the certificate is missing."); frm.cn_mark.focus(); return false;
    }
    if (frm.email.value == "") { // varovani pri nezadane e-mailove adrese
      if (cert == "sign") {
        window.alert("The e-mail address is missing."); frm.email.focus(); return false;
      }
      if (cert == "mark") {
        retval = window.confirm("Warning! The certificate without e-mail address can not be used in e-mail communication.\n\nAre you sure you want to continue without entered e-mail address?");
        if (!retval) frm.email.focus();
        return retval;
      }
    } else if (!rxMailAddress.exec(frm.email.value)) { // kontrola zadane e-mailove adresy
      window.alert("E-mail address is not in a correct form:\n     mailbox@domain.cz"); frm.email.focus(); return false;
    } 
    if (keygenOnlyDemoValues(frm,customer,cert)) { // varovani pri ponechanych demo hodnotach
      retval = window.confirm("All the form fields contain example values.\nWe recommend to enter real data to the form.\n\nAre you sure you want to continue with the example values entered?");
      return retval;
    }
    return true;
  } // konec kontrol pro "OSVC"
  if (customer == "FO") {
    if ((cert == "sign")&&(frm.cn_sign.value == "")) {
      window.alert("Your name and surname is missing."); frm.cn_sign.focus(); return false;
    } 
    if ((cert == "mark")&&(frm.cn_mark.value == "")) {
      window.alert("The name of the certificate is missing."); frm.cn_mark.focus(); return false;
    }
    if (frm.email.value == "") { // varovani pri nezadane e-mailove adrese
      if (cert == "sign") {
        window.alert("The e-mail address is missing."); frm.email.focus(); return false;
      }
      if (cert == "mark") {
        retval = window.confirm("Warning! The certificate without e-mail address can not be used in e-mail communication.\n\nAre you sure you want to continue without entered e-mail address?");
        if (!retval) frm.email.focus();
        return retval;
      }
    } else if (!rxMailAddress.exec(frm.email.value)) { // kontrola zadane e-mailove adresy
      window.alert("E-mail address is not in a correct form:\n     mailbox@domain.cz"); frm.email.focus(); return false;
    } 
    if (keygenOnlyDemoValues(frm,customer,cert)) { // varovani pri ponechanych demo hodnotach
      retval = window.confirm("All the form fields contain example values.\nWe recommend to enter real data to the form.\n\nAre you sure you want to continue with the example values entered?");
      return retval;
    }
    return true;
  } // konec kontrol pro "FO"
  return false;
}

// funkce pro kontrolu, zda zadany string "input" obsahuje zakazane znaky definovane v "prohibite"
function prohibitedChars(input,prohibite) {
// input ..... string se vstupni hodnotou
// allowed ... string se seznamem zakazanych znaku
  retval=false;
  for (i=0;i<input.length;i+=1)
    if (prohibite.indexOf(input.charAt(i))!=-1) {
      retval=true; break;
    }
  return retval;
}

// funkce, ktera za kazdy znak ve stringu "input" prida mezeru
function spaced(input) {
// input ..... string se vstupni hodnotou
  retval="";
  for (i=0;i<input.length;i+=1) retval += input.charAt(i)+" ";
  retval.length--; // odstraneni posledni mezery
  return retval;
}

// funkce pro kontrolu jmena zadaneho souboru pro ulozeni zadosti o certifikat
function keygenCheckFilename(element) {
// element ... objekt s prvkem formulare, ktery se ma zkontrolovat
  // upozorneni na prazdne jmeno souboru
  if (element.value=="") {
    return window.confirm("The file name is missing!\n\nThe content of the certificate request will be shown\nin a new window of the browser. The content\nof the window can be copied into a file.\n\nDo you want to continue?\n(Recommended for advanced users only.)");
  }
  // upozorneni na ukladani na disketu
  if (element.value.toLowerCase().indexOf("a:\\")==0) {
    return window.confirm("The storage of the certificate request on the diskette is set.\n\nDue to the low reliability of the diskettes we recommend\nyou store the request to the hard drive first.\n\nDo you want to continue the key generation?");
  }
  // kontrola zakazanych znaku
  prohibit = '/*?"<>';
  if (prohibitedChars(element.value,prohibit)) {
    window.alert("The name of the file consists of one\nor more illegal characters:\n          "+spaced(prohibit));
    element.focus();
    return false;
  }
  return true;
}

// funkce pro varovani pred zakazanim exportu klicu
function noExportWarn(element) {
  if (!element.checked) return (window.confirm("Do you really want to prohibit the private key backup?\n\nIf you prohibit the private key backup, in case of technical\nproblems or re-installation of your computer you will have\nto generate new keys and have the new certificate issued!"));
  else return true; 
}

// funkce pro oznameni o uspesne ci neuspesne inicializaci komponenty Cenroll
function cenrollStatus(cstatus,guide) {
// cstatus ... jaky byl status komponenty Cenroll; true=podarila se inicializace, false=nepodarila se
// guide ... udava, zda inicializace probihala v ramci generovani klicu ("keygen") nebo instalace vydaneho certifikatu ("crtinst")
  if (cstatus) {
    msg = "The Cenroll component has been successfully initialized.";
    if (guide == "keygen") msg += "\n\nYour browser should be able to generate keys and the\ncertificate request."
    if (guide == "crtinst") msg += "\n\nYour browser should be able to install issued certificates."
  } else {
    msg = "The initialization of the Cenroll component failed.";
    if (guide == "keygen") msg += "\n\nIt is not possible to generate the keys and certificate request."
    if (guide == "crtinst") msg += "\n\nThe issued certificates can not be installed this way."
  }
  window.alert(msg);
}

// funkce pro smazani predanych prvku formulare
function formResetValues() {
  if (window.confirm("Do you really want to delete the filled in values?")) {
    for (var i=0; i<formResetValues.arguments.length; i++)
      formResetValues.arguments[i].value = "";
  }
}

// funkce pro kontrolu jmena zadaneho souboru s vydanym certifikatem
function crtinstCheckFilename(element) {
// element ... objekt s prvkem formulare, ktery se ma zkontrolovat
  // upozorneni na prazdne jmeno souboru
  if (element.value=="") {
    window.alert("The file name is missing!\n\nIt is necessary to enter the file with the certificate.");
    element.focus();
    return false;
  }
  // kontrola spravnosti souboru podle pripony
  fext = element.value.substring(element.value.lastIndexOf(".")); if (fext == element.value) fext = "";
  if (!(fext == ".crt" || fext == ".cer" || fext == ".der")) {
    msg = "The files with the certificates have usually\n'.crt', '.cer' or '.der' extensions.\n";
    if (fext == "") msg += "You entered the file without the extension."; else msg += "You entered the file with the extension '"+fext+"'.";
    msg += "\n\nAre you sure the file is correct?";
    return window.confirm(msg);    
  }
  // kontrola na zadane certifikaty autorit
  if (element.value.indexOf("postsignum_qca_root") != -1 || element.value.indexOf("postsignum_qca_sub") != -1 || element.value.indexOf("postsignum_vca_sub") != -1) {
    msg = "It was found out that you have probably entered the file with the name:\n  * postsignum_qca_root,\n  * postsignum_qca_sub,\n  * postsignum_vca_sub\n";
    msg += "In this file there is usually stored the certificate of the authority,\nwhich is to be installed differently.";
    msg += "\n\nAre you sure the file is correct?";
    return window.confirm(msg);    
  } 
  // kontrola zakazanych znaku
  prohibit = '/*?"<>';
  if (prohibitedChars(element.value,prohibit)) {
    window.alert("The file name consists of one\nor more illegal characters:\n          "+spaced(prohibit)+"\n\nSuch a file cannot exist.");
    element.focus();
    return false;
  }
  return true;
}

// funkce pro reagovani na chybove stavy vracene komponentou CEnroll
function solveException(e,proc) {
// e    ... objekt s exception
// proc ... pri jake cinnosti doslo k chybe ("keygen-common" = bezne generovani, "keygen-email" = vkladani emailu do zadosti, "keygen-title" = vkladani funkce do zadosti, "crtinst" = instalace certifikatu)
  errhexnumber = (e.number) ? i32_to_hex(e.number) : "N/A";
  retval = "An unexpected error has occurred.\nTechnical information: procedure="+proc+", error code="+errhexnumber;
  if (proc == "keygen-email") { // chyba pri vkladani emailu do zadosti 
    retval = "An error has occurred during the certificate request generation!\n\nAddition of the 'e-mail address' entry failed.\nTechnical information: "+errhexnumber;
  }
  if (proc == "keygen-title") { // chyba pri vkladani emailu do zadosti 
    retval = "An error has occurred during the certificate request generation!\n\nAddition of the 'employee function' entry failed.\nTechnical information: "+errhexnumber;
  }
  if (proc == "keygen-common") { // chyby pri vlastnim generovani klicu
    retval = "An error has occurred during the certificate request generation!\nTechnical information: "+errhexnumber;   
    switch (errhexnumber) {
      case "0x80074c7": retval += "\n\nKey generation has been cancelled by the user."; break;
      case "0x800a46": retval += "\n\nStoring the certificate request into a file has failed."; break;
      default: retval += "\n\n(This error has not been detected yet.)";
    }
  }
  if (proc == "crtinst") { // chyby pri instalaci certifikatu
    retval = "An error has occurred during the certificate installation!\nTechnical information: "+errhexnumber;   
    switch (errhexnumber) {
      case "0x80074c7": retval += "\n\nThe installation of the certificate has been cancelled by the user."; break;
      case "0x80092004": retval += "\n\nIn the system there were not found the keys,\nwhich correspond to the issued certificate."; break;
      case "0x80092009": retval += "\n\nThe file without the certificate has been entered."; break;
      case "0x800a46": retval += "\n\nLoading of the content of the file with the certificate has failed."; break;
      default: retval += "\n\n(This error has not been detected yet.)";
    }
    if (errhexnumber=="0x80092004") document.getElementById("crtinst-error").className = "visible";
  }
  return retval;
}

// funkce pro kontrolu formulare s vyberem objednavky
function checkDwnldContract(frm) {
// frm ... objekt s formularem
  retval = false;
  for (i=0; i<frm.region.length; i++) { // zjisteni, zda byl vybran region
    if (frm.region[i].checked) retval = true;
  }
  if (!retval) {
    window.alert("Choose the region in which you want to make a contract,\nor select the list of all accessible files.");
  }
  return retval;
}

// funkce pro kontrolu formulare s vyberem dodatku ke smlouve
function checkDwnldClause(frm) {
// frm ... objekt s formularem
  retval = (frm.change.checked)||(frm.general.checked);
  if (!retval) {
    window.alert("It is necessary to choose at least one type of clause to the contract.");
  }
  return retval;
}

// funkce pro obsluhu zmen ve formulari pro stazeni seznamu zadatelu (krok 1)
function frmApplist(stepnum) {
// stepnum ... kolik kroku bude pruvodce mit
  document.getElementById("steps-2").className = "hidden"; document.getElementById("steps-3").className = "hidden";
  document.getElementById("steps-"+stepnum).className = "visible";
}

// funkce pro kontrolu formulare pro stazeni formularu seznamu zadatelu
function checkDwnldApplist(frm) {
// frm ... objekt s formularem
  retval = ((frm.crtsign.checked)||(frm.crtmark.checked));
  for (i=0; i<frm.formtype.length; i++) { // pri nastaveni zobrazeni vsech souboru potlacit kontrolu zaskrtnuti checkboxu
    if (frm.formtype[i].checked) curfrmtype = frm.formtype[i].value;
  }
  if (curfrmtype == "all") retval = true;
  if (!retval) { // zjisteni, zda byl zaskrtnut alespon jeden typ certifikatu
    window.alert("It is necessary to choose at least one type of certificate.");
  }
  return retval;
}

// funkce pro kontrolu formulare pro prihlaseni/odhlaseni odberu aktualit PostSignum e-mailem
function checkNewspick(frm) {
// frm ... objekt s formularem
  var rxMailAddress = /^[a-z0-9_\-\.]+@[a-z0-9_\-]+(\.[a-z0-9_\-]+)+$/i;
  if (!rxMailAddress.exec(frm.email.value)) { // kontrola zadane e-mailove adresy
      window.alert("E-mail address is not in a correct form:\nExample: mailbox@domain.cz"); frm.email.focus(); return false;
  }
}