/// check that the given element has a non-empty (actually that it has a minimal length) value
/// @param elem - the element
/// @param minlen - the minimal length required
/// @param name - the name of the element for the message to the user
/// @returns a message string if the element did not meet the requirements, otherwise an empty string
function checkNotEmpty(elem, minlen, name)
{
  if(trim(elem.value).length<minlen)
  {
    return name+" muss mindestens "+minlen+" Zeichen lang sein\n";
  }
  return "";
}

/// check if the value of the given element is an email address
/// currently this is pretty simplistic
/// @param elem - the element
/// @returns a message string if the element did not meet the requirements, otherwise an empty string
function checkIsEmail(elem)
{
  if(!elem.value.match(/.+@.+\..+/))
  {
    return "Bitte geben Sie eine gültige E-Mailadresse ein\n";
  }
  return "";
}

/// check if the values of the given elements are equal
/// @param elem1 - first element to be compared
/// @param elem2 - the element elem1 is compared to
/// @param text - the text returned if elem1 and elem2 are not equal
/// @returns text if the elements were not equal
function checkIsEqual(elem1,elem2,text)
{
  if(elem1.value!=elem2.value)
  {
    return text+"\n";
  }
  return "";
}


/// check the input in the given form (which should be the new user form)
/// @param form - the html form to be checked
/// @returns - shows a message and returns false if one of the test fails, returns true otherwise
function checkinput(form)
{
  // this one is fairly stupid, but i am too tired now to write proper code
  var msg="";
  for(ii=0;ii<form.elements.length;ii++)
  {
    if(form.elements[ii].name=="data[User][login_name]")
    {
      msg+=checkNotEmpty(form.elements[ii],3,"Benutzername");
    }
    if(form.elements[ii].name=="data[User][first_name]")
    {
      msg+=checkNotEmpty(form.elements[ii],3,"Vorname");
    }
    if(form.elements[ii].name=="data[User][name]")
    {
      msg+=checkNotEmpty(form.elements[ii],3,"Nachname");
    }
    if(form.elements[ii].name=="data[User][email]")
    {
      msg+=checkIsEmail(form.elements[ii]);
      //msg+=checkIsEqual(form.elements[ii],form.elements[ii+1],"Bitte geben Sie zweimal die gleiche E-Mailadresse an");
    }
    if(form.elements[ii].name=="data[User][pwd]")
    {
      msg+=checkNotEmpty(form.elements[ii],3,"Passwort");
      //msg+=checkIsEqual(form.elements[ii],form.elements[ii+1],"Bitte geben Sie zweimal das gleiche Passwort an");
    }
    if(form.elements[ii].name=="data[User][bonus_partner_validation1]")
    {
        msg+=checkBonusParam1(form.elements[ii-1], form.elements[ii]);
    }
    if(form.elements[ii].name=="data[User][bonus_partner_validation2]")
    {
        msg+=checkBonusParam2(form.elements[ii-2], form.elements[ii]);
    }
    if(form.elements[ii].name=="data[User][agb]")
    {
      if(form.elements[ii].checked!=true)
      {
        msg+="Sie müssen die AGBs akzeptieren. Bitte setzen Sie einen Haken in die Checkbox\n";
      }
    }
  }

  if(msg!="")
  {
    alert(msg);
    return false;
  }
  else
  {
    //form.submit();
    return true;
  }
}


/// i think this checks the username and password for a normal user
/// shows a message if any checks fail
/// @param form - the form to be checked
/// @returns false if a check failed, true in the other case
function checkinput2(form)
{
  // this one is fairly stupid, but i am too tired now to write proper code
  var msg="";
  for(ii=0;ii<form.elements.length;ii++)
  {
    if(form.elements[ii].name=="data[User][login_name]")
    {
      msg+=checkNotEmpty(form.elements[ii],3,"Benutzername");
    }
    if(form.elements[ii].name=="data[User][pwd]")
    {
      msg+=checkNotEmpty(form.elements[ii],3,"Passwort");
    }
  }
  if(msg!="")
  {
    alert(msg);
    return false;
  }
  else
  {
    //form.submit();
    return true;
  }
}

/// check the input when a new owner is registered
/// shows a message if any checks fail
/// @param form - the form to be checked
/// @returns false if a check failed, true in the other case
function checkNewOwner(form)
{
    // this one is fairly stupid, but i am too tired now to write proper code
    var msg="";
    for(ii=0;ii<form.elements.length;ii++)
    {
        // note the following elements are not part of the
        // newownerbox and are therefore not really needed
        // but i think the newownerbox and new_entry should be
        // merged, so I don't delete then (this is a copy of the
        // new_entry code)
        // 30.8.07 jc
        if(form.elements[ii].name=="firmenname")
        {
            msg+=checkNotEmpty(form.elements[ii],3,"firmenname");
        }
        if(form.elements[ii].name=="branche")
        {
            msg+=checkNotEmpty(form.elements[ii],3,"branche");
        }
        if(form.elements[ii].name=="strasse")
        {
            msg+=checkNotEmpty(form.elements[ii],3,"strasse");
        }
        if(form.elements[ii].name=="hausnummer")
        {
            msg+=checkNotEmpty(form.elements[ii],1,"hausnummer");
        }
        if(form.elements[ii].name=="plz")
        {
            msg+=checkNotEmpty(form.elements[ii],5,"plz");
        }
        if(form.elements[ii].name=="ort")
        {
            msg+=checkNotEmpty(form.elements[ii],3,"ort");
        }
        if(form.elements[ii].name=="vorwahl")
        {
            msg+=checkNotEmpty(form.elements[ii],3,"vorwahl");
        }
        if(form.elements[ii].name=="rufnummer")
        {
            msg+=checkNotEmpty(form.elements[ii],3,"rufnummer");
        }

        // these are really used by the newownerbox
        // 30.8.07 jc
        if(form.elements[ii].name=="data[User][login_name]")
        {
            msg+=checkNotEmpty(form.elements[ii],3,"Benutzername");
        }
        if(form.elements[ii].name=="data[User][first_name]")
        {
            msg+=checkNotEmpty(form.elements[ii],3,"Vorname");
        }
        if(form.elements[ii].name=="data[User][name]")
        {
            msg+=checkNotEmpty(form.elements[ii],3,"Nachname");
        }
        if(form.elements[ii].name=="data[User][email]")
        {
            msg+=checkIsEmail(form.elements[ii]);
            msg+=checkIsEqual(form.elements[ii],form.elements[ii+1],"Bitte geben Sie zweimal die gleiche E-Mailadresse an");
        }
        if(form.elements[ii].name=="data[User][pwd]")
        {
            msg+=checkNotEmpty(form.elements[ii],3,"Passwort");
            msg+=checkIsEqual(form.elements[ii],form.elements[ii+1],"Bitte geben Sie zweimal das gleiche Passwort an");
        }
        if(form.elements[ii].name=="data[User][agb]")
        {
            if(form.elements[ii].checked!=true)
            {
                msg+="Sie müssen die AGBs, die Allgemeinen Nutzungsbedingungen und die Datenschutzerklärung akzeptieren. Bitte setzen Sie einen Haken in die Checkbox\n";
            }
        }
    }

    if(msg!="")
    {
        alert(msg);
        return false;
    }
    else
    {
        return true;
    }
}


