// Proste trim :)
function trim(string)
{
   return string.replace(/^\s*|\s*$/g,"");
}


// Kontrola, zda-li je korektne zadana emailova adresa
function checkCorrectEmail(email)
{
  var correctFormat = true;

  str = "";

  var x = trim(email);

  var at = x.indexOf("@");
  var lastAt = x.lastIndexOf("@");
  var firstDot = x.indexOf(".");
  var lastDot = x.lastIndexOf(".");
  var doubleDot = x.indexOf("..");

  var posLast = x.length - 1;

  if( at < 1 || at != lastAt || at > lastDot || firstDot < 1 || lastDot >= posLast || doubleDot >= 0 )
  {
    correctFormat = false;
  }

  return correctFormat;
}


// Kontrola, zda-li je korektne vyplneny formular pro registraci noveho uctu
function checkCustRegistration(form)
{
  var correct = true; //priznak chyby
  var tmpValue;
  message = "";

  tmpValue = form.fFirstname.value;
  if( tmpValue.length == 0 )
  {
    correct = false;
    message += "\n\tKřestní jméno";
  }

  tmpValue = form.fSurname.value;
  if( tmpValue.length == 0 )
  {
    correct = false;
    message += "\n\tPříjmení";
  }

  tmpValue = form.fEmail.value;
  if( tmpValue.length == 0 || !checkCorrectEmail(tmpValue) )
  {
    correct = false;
    message += "\n\tEmail";
  }

  if( form.fPassword.value == 0 || (form.fPassword.value != form.fPassword2.value) )
  {
    correct = false;
    message += "\n\tHeslo";
  }

  tmpValue = form.fAddressStreet.value;
  if( tmpValue.length == 0 )
  {
    correct = false;
    message += "\n\tUlice a číslo popisné";
  }

  tmpValue = form.fAddressCity.value;
  if( tmpValue.length == 0 )
  {
    correct = false;
    message += "\n\tObec";
  }

  tmpValue = form.fPostalCode.value;
  if( tmpValue.length == 0 )
  {
    correct = false;
    message += "\n\tPSČ";
  }

  tmpValue = form.fTelephone.value;
  if( tmpValue.length == 0 )
  {
    correct = false;
    message += "\n\tTelefon";
  }

  if( correct == false )
  { // nastala nejaka chyba
    alert("Vyplňte, nebo opravte, prosím, následující položky:" + message);
    return false;
  }
  else
    return true;
}


function checkDeskCustInformation(form)
{
  var correct = true; //priznak chyby
  var tmpValue;
  message = "";

  tmpValue = form.fFirstname.value;
  if( tmpValue.length == 0 )
  {
    correct = false;
    message += "\n\tKřestní jméno";
  }

  tmpValue = form.fSurname.value;
  if( tmpValue.length == 0 )
  {
    correct = false;
    message += "\n\tPříjmení";
  }

  tmpValue = form.fEmail.value;
  if( tmpValue.length == 0 || !checkCorrectEmail(tmpValue) )
  {
    correct = false;
    message += "\n\tEmail";
  }

  tmpValue = form.fAddressStreet.value;
  if( tmpValue.length == 0 )
  {
    correct = false;
    message += "\n\tUlice a číslo popisné";
  }

  tmpValue = form.fAddressCity.value;
  if( tmpValue.length == 0 )
  {
    correct = false;
    message += "\n\tObec";
  }

  tmpValue = form.fPostalCode.value;
  if( tmpValue.length == 0 )
  {
    correct = false;
    message += "\n\tPSČ";
  }

  tmpValue = form.fTelephone.value;
  if( tmpValue.length == 0 )
  {
    correct = false;
    message += "\n\tTelefon";
  }

  if( correct == false )
  { // nastala nejaka chyba
    alert("Vyplňte, nebo opravte, prosím, následující položky:" + message);
    return false;
  }
  else
    return true;
}
