// Verifikace formulare rychleho vyhledavani
function verifyQuickSearchForm(quickSearchForm) {
  var why = "";

  if((quickSearchForm.searchPhrase.value == "") || (quickSearchForm.searchPhrase.value == "Rychlé vyhledávání v katalogu")) {
    why += "Zadejte hledaný výraz!\n";
  }

  StrLen = quickSearchForm.searchPhrase.value.length;
  if(StrLen<3 ){
    why += "Hledaný výraz musí obsahovat alespoň 3 znaky!\n";
  }
  
  if (why != ""){
    alert(why);
    return false;
  }

  return true;
}


// Generovani progressbaru pro maximalni delku textu zpravy v textarea 

var MaxLengthLock = false;
function MaxLengthCount(fieldObj,fieldMaxLength)
{
  if (!MaxLengthLock)
  {
    MaxLengthLock = true;
    if (fieldObj.value.length > fieldMaxLength)
    {
      alert("Maximální možná délka textu je " + fieldMaxLength + " znaků!");
      fieldObj.value = fieldObj.value.substring(0,fieldMaxLength);
    }
    var percentage = parseInt(100 - (( fieldMaxLength - fieldObj.value.length) * 100)/fieldMaxLength);
    document.getElementById(fieldObj.id + "_PBar").style.width = parseInt((parseInt(fieldObj.offsetWidth)*percentage)/100)+"px";
    MaxLengthLock = false;
  }
}

// Verifikace formuare pro registraci uzivatele

function VerifyCustomerRegisterForm(customerRegisterForm) {
  var why = "";
 
  if ((customerRegisterForm.fakturacni_jmeno.value == "") || (customerRegisterForm.fakturacni_prijmeni.value == "")) {
    why += "Zadejte Vaše jméno a příjmení.\n";
  }

  if ((customerRegisterForm.fakturacni_psc.value == "") || (customerRegisterForm.fakturacni_obec.value == "") || (customerRegisterForm.fakturacni_ulice.value == "")) {
    why += "Zadejte celou fakturační adresu.\n";
  }

  if(customerRegisterForm.email.value == ""){
    why += "Zadejte Vaši e-mailovou adresu. Tento e-mail bude sloužit k přihlášení k Vašemu účtu.\n";
  }
  if (customerRegisterForm.email.value != ""){
	  why += checkEmail(customerRegisterForm.email.value);
  }
  
  StrLen = customerRegisterForm.heslo1.value.length;
  if(StrLen>4) {
	  if(customerRegisterForm.heslo1.value != customerRegisterForm.heslo2.value){
	    why += "Zadaná hesla nejsou shodná!\n";
	  }
  }
  else {
	    why += "Uživatelské heslo je příliš krátké. Heslo musí obsahovat alespoň 5 znaků!\n";
  }
  
  if(customerRegisterForm.salesterms.checked == false){
    why += "Před odesláním je třeba souhlasit s podmínkami registrace.\n";
  }


  if (why != ""){
    alert(why);
    return false;
  }

  return true;
}

// Verifikace formuare pro aktualizace uzivatele

function VerifyCustomerUpdateForm(customerUpdateForm) {
  var why = "";
 
  if ((customerUpdateForm.fakturacni_jmeno.value == "") || (customerUpdateForm.fakturacni_prijmeni.value == "")) {
    why += "Zadejte Vaše jméno a příjmení.\n";
  }

  if ((customerUpdateForm.fakturacni_psc.value == "") || (customerUpdateForm.fakturacni_obec.value == "") || (customerUpdateForm.fakturacni_ulice.value == "")) {
    why += "Zadejte celou fakturační adresu.\n";
  }
  
  if(customerUpdateForm.email.value == ""){
    why += "Zadejte Vaši e-mailovou adresu. Tento e-mail bude sloužit k přihlášení k Vašemu účtu.\n";
  }
  if (customerUpdateForm.email.value != ""){
	  why += checkEmail(customerUpdateForm.email.value);
  }

  StrLen = customerUpdateForm.heslo1.value.length;
  if (StrLen>0) {
	  if (StrLen<5) {
	    why += "Uživatelské heslo je příliš krátké. Heslo musí obsahovat alespoň 5 znaků!\n";
	  }
	  if(customerUpdateForm.heslo1.value != customerUpdateForm.heslo2.value){
	    why += "Zadaná hesla nejsou shodná!\n";
	  }
  }
  
  if (why != ""){
    alert(why);
    return false;
  }

  return true;
}

// Verifikace formuare pro aktualizace uzivatele

function VerifyOrderContactForm(orderContactForm) {
  var why = "";
 
  if ((orderContactForm.fakturacni_jmeno.value == "") || (orderContactForm.fakturacni_prijmeni.value == "")) {
    why += "Zadejte Vaše jméno a příjmení.\n";
  }

  if ((orderContactForm.fakturacni_psc.value == "") || (orderContactForm.fakturacni_obec.value == "") || (orderContactForm.fakturacni_ulice.value == "")) {
    why += "Zadejte celou fakturační adresu.\n";
  }
  
  if(orderContactForm.email.value == ""){
    why += "Zadejte Vaši e-mailovou adresu.\n";
  }
  if (orderContactForm.email.value != ""){
	  why += checkEmail(orderContactForm.email.value);
  }

  if(orderContactForm.telefon.value == ""){
    why += "Zadejte Vaše telefonní číslo.\n";
  }


  if (why != ""){
    alert(why);
    return false;
  }

  return true;
}

// Kontrola formatu emailu
function checkEmail (strng) {
  var error = "";

  var emailFilter=/^.+@.+\..{2,4}$/;
  if (!(emailFilter.test(strng))) {
    error += "Zadaná e-mailová adresa nemá platný formát.\n";
  }

  var illegalChars= /[\(\)\<\>\,\;\:\\\\[\]]/
  if (strng.match(illegalChars)) {
    error += "Zadaná e-mailová adresa obsahuje nepovolené znaky.\n";
  }
  return error;
}

