
// Proste trim :)
function trim(string)
{
   return string.replace(/^\s*|\s*$/g,"");
}


/**
 * Kontrola spravnosti emailu.
 */
function checkEmail(x)
{
  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 )
  {
      return false;
  }
  else
  {
      return true;
  }
}


/**
 * Kontrola odeslani kontaktu.
 */
function checkContactSend( form )
{
    var x;

    var isNotSet = false;
    var notSetLabels = "";

    x = trim(form.fNameSurname.value);
    if( x.length == 0 )
    {
        isNotSet = true;
        notSetLabels += "\n\tJméno a příjmení";
    }

    x = trim(form.fEmail.value);
    emptyEmail = false;
    if( x.length == 0 )
    {
        isNotSet = true;
        notSetLabels += "\n\tEmail";

        emptyEmail = true;
    }

    correctEmail = true;
    if( !checkEmail(form.fEmail.value) )
    {
        correctEmail = false;
    }

    if( isNotSet || !correctEmail )
    {
        text = "";
        if( isNotSet )
        {
            text = "Vyplňte, prosím, následující povinné údaje:" + notSetLabels + "\n\n";
        }

        if( !emptyEmail && !correctEmail )
        {
            text += "Email není ve správném formátu.";
        }

        alert(text);

        return false;
    }

    return true; //form je vyplneny OK
}
