//funkce pro validaci formularoveho pole.. zadne mezery a zalomeni radku
function containsspaces(s) {
	for(var i = 0; i < s.value.length; i++) {
	var c = s.value.charAt(i);
	if ((c == ' ') || (c == '\n') || (c == '\t')) {
		alert('Pole nesmí obsahovat mezerové znaky');
		return false;
		}
	}
	return true;
}

function isblank(s) {
	for(var i = 0; i < s.length; i++) {
	var c = s.charAt(i);
	if ((c != ' ') && (c != '\n') && (c != '\t')) {
		return false;
		}
	}
	return true;
}

//funkce pro souhrnou validaci formulare
function verify(f) 
{
  var msg;
  var empty_fields = "";
  var errors = "";

  // Loop through the elements of the form, looking for all
  // text and textarea elements that don't have an
  //  "optional" property defined.  Then, check for fields
  // that are empty and make a list of them.
  // Also, if any of these elements have a "min" or a "max"
  // property defined, then verify that they are numbers 
  // and that they are in the right range.
  // Put together error messages for fields that are wrong.
  for(var i = 0; i < f.length; i++)
  {
     var e = f.elements[i];

     if (((e.type == "text") || (e.type == "password") || (e.type == "textarea")) && e.required == true)
     {
        // first check if the field is empty
        if ((e.value == null) ||
            (e.value == "") ||
            isblank(e.value))
        {
           empty_fields += "\n        " +
                           e.description;
           continue;
        }

        // Now check for fields that are supposed 
        // to be numeric.
        if (e.numeric ||
           (e.min != null) ||
           (e.max != null))
        {
           var v = parseFloat(e.value);
           if (isNaN(v) ||
              ((e.min != null) && (v < e.min)) ||
              ((e.max != null) && (v > e.max)))
           {
              errors += "\n- The field " +
                        e.description +
                        " must be a number";
              if (e.min != null)
                 errors += " that is greater than " +
                           e.min;

              if (e.max != null &&
                  e.min != null)
                 errors += " and less than " +
                           e.max;

              else if (e.max != null)
                 errors += " that is less than " +
                           e.max;

              errors += ".\n";
           }
        }

        // Now check for fields that are supposed 
        // to be emails.
        // Not exactly as described in RFC 2822, but 
        // a rough attempt
        // of the form "local-bit@domain-bit"
        if (e.email && !isblank(e.value))
        {
           var seenAt = false;
           var append = "";
           for(var j = 0; j < e.value.length; j++)
           {
              var c = e.value.charAt(j);
              if ((c == ' ') ||
                  (c == '\n') ||
                  (c == '\t'))
                 append += 
     "\n           - neobsahovat mezeru";
              if ((c == '@') && (seenAt == true))
                 append += 
     "\n           - obsahovat pouze jeden znak @";
              if (c == '@')
                 seenAt = true;		
           }

           if (seenAt == false)
              append += 
     "\n           - obsahovat znak @";
           if (append)
              errors += "- Pole " +
                        e.description +
                        " musí: " + append;
        }

        // Now check for fields that are supposed 
        // to be DOBs.
        if (e.dob && !isblank(e.value))
        {
           var slashCount = 0;
           var append = "";
           var addedError1 = false;
           var addedError2 = false;

           for(var j = 0; j < e.value.length; j++)
           {
              var c = e.value.charAt(j);

              if ((c == '/'))
                 slashCount++;

              if (c != '/' &&
                 (c < '0' || c > '9') &&
                 addedError1 == false)
              {
                 addedError1 = true;
                 append += 
     "\n           - musí obsahovat pouze čísla " +
     "a lomítka";
              }
           }

           if (j != 10 || slashCount != 2)
              append += 
     "\n           - musí mít formát DD/MM/YYYY";
           if (slashCount != 2)
              append += 
     "\n           - musí obsahovat dvě lomítka";
           if (append)
              errors +=  "- Pole " + 
                         e.description + 
                         " musí: " + append;
        }

        // Now check for fields that are supposed 
        // not to have spaces
        if (e.nospaces)
        {
           var seenAt = false;
           var append = "";

           for(var j = 0; j < e.value.length; j++)
           {
              var c = e.value.charAt(j);
			  var x = false;
              if ((c == ' ') ||
                  (c == '\n') ||
                  (c == '\t'))
              var x = true;   
           }
				 if (x==true) {
				 errors += "- Pole " + e.description +
                           " nesmí obsahovat mezerové znaky \n";
				 }
        }

     } // if (type is text or textarea) and !optional
  } // for each character in field

  // Now, if there were any errors, then display the
  // messages, and return true to prevent the form from
  // being submitted.  Otherwise return false
  if (!empty_fields && !errors) 
     return true;

  msg  = "______________________________________________________\n\n"
  msg += "Formulář nebyl odeslán z důvodů těchto chyb:\n";
  msg += "Opravte prosím uvedené nedostatky a odešlete formulář znovu.\n";
  msg += "______________________________________________________\n\n"

  if (empty_fields)
  {
     msg += "- Tato vyžadovaná pole jsou prázdná:"
           + empty_fields + "\n";
     if (errors)
        msg += "\n";
  }
  msg += errors;
  alert(msg);
  return false;
}

//funkce na kontrolu rovnosti potvrzeni hesla nebo dvou poli
function thesame(value1, value2, description)
{
  if (((value1 != null) || 
       (value1 != "")) && 
       value2 != "" && 
       value1 != value2)
  {
       alert("The " + description + " musí být totožná.");
       return (false);
  }
  return (true);
}