
function showBox(elementID, boxClassname, boxContent) {
  var newDiv = $(document.createElement('div'));
  newDiv.hide();
  newDiv.addClassName(boxClassname);
  newDiv.innerHTML = '<p>' + boxContent + '</<p>';
  $(elementID).parentNode.appendChild(newDiv);
  new Effect.Appear(newDiv, { duration: 0.6 });
}

function hideBox(elementID, boxClassname) {
  try {
    var foo = $($(elementID).parentNode.lastChild);
    if (foo.hasClassName(boxClassname)) {
      foo.remove();
    }
  }
  catch (e) {}
}

function toggleOK(elementID, action) {
  if ($(elementID).parentNode.hasClassName('status-ok') && action == 'focus') {
    $($(elementID).parentNode).removeClassName('status-ok');
  }
  else if (!$(elementID).parentNode.hasClassName('status-ok') && (action == 'blur')) {
    $($(elementID).parentNode).addClassName('status-ok');
  }
  else if (!$(elementID).parentNode.hasClassName('status-ok') && (action == 'click')) {
    $($(elementID).parentNode.parentNode.parentNode).addClassName('gender-status-ok');
  }
}

function checkUsername() {
  var usernameIsValid = $F('reg_username_id').match(/^[A-Za-z0-9-]+$/) == $F('reg_username_id');
  var usernameExists = false;
  if (usernameIsValid && $F('reg_username_id').length > 2) { //Benutzername gueltig
    var myAjax = new Ajax.Request( '/registration/',  { //pruefen ob es den Benutzernamen schon gibt
      asynchronous: false,
      method: 'post',
      parameters: 'command=checkusername&searchusername=' + $F('reg_username_id'),
      onSuccess: function(t) {
        if (t.responseText == 'username_exists') {
          usernameExists = true;
        }
      }
    });
    if (usernameExists) {
      return txtError['username_exists'];
    }
    else {
      return true;
    }
  }
  else if ($F('reg_username_id').length >= 0) {
    return txtError['username_invalid'];
  }
}

function checkPassword() {
  if ($F('reg_password_id').match(/\S/)) {
    return true;
  }
  else if ($F('reg_password_id').length >= 0) {
    return txtError['password_invalid'];
  }
}

function checkPassword2() {
  if ($F('reg_password_id').length > 0 && $F('reg_password2_id').length > 0
         && $F('reg_password_id') == $F('reg_password2_id')) {
    return true;
  }
  else if ($F('reg_password_id').length >= 0 && $F('reg_password2_id').length > 0) {
    return txtError['password2_mismatch'];
  }
  else {
    return txtError['password2_empty'];
  }
}

function checkEmail() {
  var emailIsValid = $F('reg_email_id').match(/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/) == $F('reg_email_id');
  var emailStatus = false;
  if (emailIsValid) {
    var myAjax = new Ajax.Request( '/registration/', {
      asynchronous: false,
      method: 'post',
      parameters: 'command=checkemail&searchemail=' + $F('reg_email_id'),
      onSuccess: function(t) {
        emailStatus = t.responseText;
       }
    });
    if (emailStatus == 'email_invalid') {
      return txtError['email_invalid'];
    }
    else if (emailStatus == 'email_exists') {
      return txtError['email_exists'];
    }
    else if (emailStatus == 'email_in_robinsonlist') {
      return txtError['email_blocked'];
    }
    else {
      return true;
    }
  }
  else if ($F('reg_email_id').length >= 0) {
    return txtError['email_invalid'];
  }
}

function checkSecurityCode() {
  if ($F('reg_security_code_id').length > 0) {
    return true;
  }
  else {
    return txtError['security_code'];
  }
}

function checkEmpty(value, errCode) {
  if (value.match(/\S/)) {
    return true;
  }
  else if (value.length >= 0) {
    return txtError[errCode];
  }
}

function checkDigits(value, errCode) {
  if (value.match(/\d+/) &&  value != 0) {
    return true;
  }
  else {
    return txtError[errCode];
  }
}

function checkStreet() {
  var streetCheck = $F('reg_street_id').match(/\S/);
  var streetnrCheck = $F('reg_streetnr_id').match(/\S/);
  if (streetCheck && streetnrCheck) {
    return true;
  }
  else {
    return txtError['street_invalid'];
  }
}

function checkZip() {
  var zipCheck = $F('reg_zip_id').match(/\S/);
  var cityCheck = $F('reg_city_id').match(/\S/);
  if (zipCheck && cityCheck) {
    return true;
  }
  else {
    return txtError['zip_invalid'];
  }
}

function checkBirthdate() {
  var bdayCheck = $F('reg_bday_id').match(/\d+/);
  var bmonthCheck = $F('reg_bmonth_id').match(/\d+/);
  var byearCheck = $F('reg_byear_id').match(/\d+/);
  if (bdayCheck && bmonthCheck && byearCheck && $F('reg_bday_id') > 0 && $F('reg_bmonth_id') > 0 && $F('reg_byear_id') > 0) {
    return true;
  }
  else {
    return txtError['birthdate_invalid'];
  }
}


/**
 *  RegistrationCheck Klasse stellt die Javascript Funktionen fuer die Registrierungsforumlare bereit
 *
 *  Die initialize Funktion, also die instanziierung der Klasse, MUSS unmittelbar hinter dem input Feld aufgerufen werden!
 */

var RegistrationCheck = Class.create();

RegistrationCheck.prototype = {
  initialize: function( inputObject ) {
    inputObject = $(inputObject);

    /**
     * 1st Page registration Checks
     */
    if (inputObject.id == 'reg_username_id') {
      Event.observe('reg_username_id', 'blur', function() {
        hideBox('reg_username_id', 'box-info');
        checkVal = checkUsername();
        if (checkVal != true) {
          hideBox('reg_username_id', 'box-error'); //falls in diesem Feld schon Fehler aufgetreten sind
          showBox('reg_username_id', 'box-error', checkVal);
        }
        else {
          //status OK
          toggleOK('reg_username_id', 'blur');
        }
      }, false);
      Event.observe('reg_username_id', 'focus', function() {
        hideBox('reg_username_id', 'box-error');
        showBox('reg_username_id', 'box-info', txtInfo['username']);
        toggleOK('reg_username_id', 'focus');
      }, false);
    }

    if (inputObject.id == 'reg_password_id') {
      Event.observe('reg_password_id', 'blur', function() {
        hideBox('reg_password_id', 'box-info');
        checkVal = checkPassword();
        if (checkVal != true) {
          hideBox('reg_password_id', 'box-error'); //falls in diesem Feld schon Fehler aufgetreten sind
          showBox('reg_password_id', 'box-error', checkVal);
        }
        else {
          //status OK
          toggleOK('reg_password_id', 'blur');
        }
      }, false);
      Event.observe('reg_password_id', 'focus', function() {
        hideBox('reg_password_id', 'box-error');
        showBox('reg_password_id', 'box-info', txtInfo['password']);
        toggleOK('reg_password_id', 'focus');
      }, false);
    }

    if (inputObject.id == 'reg_password2_id') {
      Event.observe('reg_password2_id', 'blur', function() {
        hideBox('reg_password2_id', 'box-info');
        checkVal = checkPassword2();
        if (checkVal != true) {
          hideBox('reg_password2_id', 'box-error'); //falls in diesem Feld schon Fehler aufgetreten sind
          showBox('reg_password2_id', 'box-error', checkVal);
        }
        else {
          //status OK
          toggleOK('reg_password2_id', 'blur');
        }
      }, false);
      Event.observe('reg_password2_id', 'focus', function() {
        hideBox('reg_password2_id', 'box-error');
        showBox('reg_password2_id', 'box-info', txtInfo['password2']);
        toggleOK('reg_password2_id', 'focus');
      }, false);
    }

    if (inputObject.id == 'reg_email_id') {
      Event.observe('reg_email_id', 'blur', function() {
        hideBox('reg_email_id', 'box-info');
        checkVal = checkEmail();
        if (checkVal != true) {
          hideBox('reg_email_id', 'box-error'); //falls in diesem Feld schon Fehler aufgetreten sind
          showBox('reg_email_id', 'box-error', checkVal);
        }
        else {
          //status OK
          toggleOK('reg_email_id', 'blur');
        }
      }, false);
      Event.observe('reg_email_id', 'focus', function() {
        hideBox('reg_email_id', 'box-error');
        showBox('reg_email_id', 'box-info', txtInfo['email']);
        toggleOK('reg_email_id', 'focus');
      }, false);
    }

    if (inputObject.id == 'reg_cb_interests_love') {
      Event.observe('reg_cb_interests_love', 'click', function() {
        if ($('reg_cb_interests_love').checked == true) {
          hideBox('reg_interests_love', 'box-error');
        }
      }, false);
    }

    if (inputObject.id == 'reg_cb_interests_flirt') {
      Event.observe('reg_cb_interests_flirt', 'click', function() {
        if ($('reg_cb_interests_flirt').checked == true) {
          hideBox('reg_interests_love', 'box-error');
        }
      }, false);
    }

    if (inputObject.id == 'reg_cb_interests_contact') {
      Event.observe('reg_cb_interests_contact', 'click', function() {
        if ($('reg_cb_interests_contact').checked == true) {
          hideBox('reg_interests_love', 'box-error');
        }
      }, false);
    }

    if (inputObject.id == 'reg_cb_interests_fun') {
      Event.observe('reg_cb_interests_fun', 'click', function() {
        if ($('reg_cb_interests_fun').checked == true) {
          hideBox('reg_interests_love', 'box-error');
        }
      }, false);
    }

    if (inputObject.id == 'reg_cb_interests_hobby') {
      Event.observe('reg_cb_interests_hobby', 'click', function() {
        if ($('reg_cb_interests_hobby').checked == true) {
          hideBox('reg_interests_love', 'box-error');
        }
      }, false);
    }

    if (inputObject.id == 'reg_cb_interests_lookaround') {
      Event.observe('reg_cb_interests_lookaround', 'click', function() {
        if ($('reg_cb_interests_lookaround').checked == true) {
          hideBox('reg_interests_love', 'box-error');
        }
      }, false);
    }

    if (inputObject.id == 'reg_cb_interests_gender_love_male') {
      Event.observe('reg_cb_interests_gender_love_male', 'click', function() {
        if ($('reg_cb_interests_gender_love_male').checked == true) {
          hideBox('reg_interests_love_gender', 'box-error');
        }
      }, false);
    }

    if (inputObject.id == 'reg_cb_interests_gender_love_female') {
      Event.observe('reg_cb_interests_gender_love_female', 'click', function() {
        if ($('reg_cb_interests_gender_love_female').checked == true) {
          hideBox('reg_interests_love_gender', 'box-error');
        }
      }, false);
    }

    if (inputObject.id == 'reg_cb_interests_gender_love_both') {
      Event.observe('reg_cb_interests_gender_love_both', 'click', function() {
        if ($('reg_cb_interests_gender_love_both').checked == true) {
          hideBox('reg_interests_love_gender', 'box-error');
        }
      }, false);
    }

    if (inputObject.id == 'reg_cb_interests_gender_flirt_male') {
      Event.observe('reg_cb_interests_gender_flirt_male', 'click', function() {
        if ($('reg_cb_interests_gender_flirt_male').checked == true) {
          hideBox('reg_interests_flirt_gender', 'box-error');
        }
      }, false);
    }

    if (inputObject.id == 'reg_cb_interests_gender_flirt_female') {
      Event.observe('reg_cb_interests_gender_flirt_female', 'click', function() {
        if ($('reg_cb_interests_gender_flirt_female').checked == true) {
          hideBox('reg_interests_flirt_gender', 'box-error');
        }
      }, false);
    }

    if (inputObject.id == 'reg_cb_interests_gender_flirt_both') {
      Event.observe('reg_cb_interests_gender_flirt_both', 'click', function() {
        if ($('reg_cb_interests_gender_flirt_both').checked == true) {
          hideBox('reg_interests_flirt_gender', 'box-error');
        }
      }, false);
    }

    if (inputObject.id == 'reg_cb_terms') {
      Event.observe('reg_cb_terms', 'click', function() {
        if ($('reg_cb_terms').checked == true) {
          hideBox('reg_cb_terms', 'box-error');
        }
        else {
          showBox('reg_cb_terms', 'box-error', txtInfo['accept_agb']); //schaltet beim Wegklicken den Fehler ein
        }
      }, false);
    }

    if (inputObject.id == 'reg_security_code_id') {
      Event.observe('reg_security_code_id', 'blur', function() {
        hideBox('reg_security_code_id', 'box-info');
        checkVal = checkSecurityCode();
        if (checkVal != true) {
          hideBox('reg_security_code_id', 'box-error'); //falls in diesem Feld schon Fehler aufgetreten sind
          showBox('reg_security_code_id', 'box-error', checkVal);
        }
      }, false);
      Event.observe('reg_security_code_id', 'focus', function() {
        hideBox('reg_security_code_id', 'box-error');
        showBox('reg_security_code_id', 'box-info', txtInfo['security_code']);
      }, false);
    }

    /**
     * 2nd Page registration Checks
     */

    if (inputObject.id == 'reg_rb_gender_female') {
      Event.observe('reg_rb_gender_female', 'click', function() {
        if ($('reg_rb_gender_female').checked == true) {
          hideBox('reg_rb_gender_female', 'box-error');
          toggleOK('reg_rb_gender_female', 'click');
        }
      }, false);
    }

    if (inputObject.id == 'reg_rb_gender_male') {
      Event.observe('reg_rb_gender_male', 'click', function() {
        if ($('reg_rb_gender_male').checked == true) {
          hideBox('reg_rb_gender_female', 'box-error');
          toggleOK('reg_rb_gender_female', 'click');
        }
      }, false);
    }

    if (inputObject.id == 'reg_firstname_id') {
      Event.observe('reg_firstname_id', 'blur', function() {
        hideBox('reg_firstname_id', 'box-info');
        checkVal = checkEmpty($F('reg_firstname_id'), 'firstname_invalid');
        if (checkVal != true) {
          hideBox('reg_firstname_id', 'box-error'); //falls in diesem Feld schon Fehler aufgetreten sind
          showBox('reg_firstname_id', 'box-error', checkVal);
        }
        else {
          //status OK
          toggleOK('reg_firstname_id', 'blur');
        }
      }, false);
      Event.observe('reg_firstname_id', 'focus', function() {
        hideBox('reg_firstname_id', 'box-error');
        showBox('reg_firstname_id', 'box-info', txtInfo['firstname']);
        toggleOK('reg_firstname_id', 'focus');
      }, false);
    }

    if (inputObject.id == 'reg_lastname_id') {
      Event.observe('reg_lastname_id', 'blur', function() {
        hideBox('reg_lastname_id', 'box-info');
        checkVal = checkEmpty($F('reg_lastname_id'), 'lastname_invalid');
        if (checkVal != true) {
          hideBox('reg_lastname_id', 'box-error'); //falls in diesem Feld schon Fehler aufgetreten sind
          showBox('reg_lastname_id', 'box-error', checkVal);
        }
        else {
          //status OK
          toggleOK('reg_lastname_id', 'blur');
        }
      }, false);
      Event.observe('reg_lastname_id', 'focus', function() {
        hideBox('reg_lastname_id', 'box-error');
        showBox('reg_lastname_id', 'box-info', txtInfo['lastname']);
        toggleOK('reg_lastname_id', 'focus');
      }, false);
    }

    if (inputObject.id == 'reg_country_id') {
      Event.observe('reg_country_id', 'blur', function() {
        hideBox('reg_country_id', 'box-info');
        checkVal = checkDigits($F('reg_country_id'), 'country_invalid');
        if (checkVal != true) {
          hideBox('reg_country_id', 'box-error'); //falls in diesem Feld schon Fehler aufgetreten sind
          showBox('reg_country_id', 'box-error', checkVal);
        }
        else {
          //status OK
          toggleOK('reg_country_id', 'blur');
        }
      }, false);
      Event.observe('reg_country_id', 'focus', function() {
        hideBox('reg_country_id', 'box-error');
        showBox('reg_country_id', 'box-info', txtInfo['country']);
        toggleOK('reg_country_id', 'focus');
      }, false);
    }

    if (inputObject.id == 'reg_province_id') {
      Event.observe('reg_province_id', 'blur', function() {
        hideBox('reg_province_id', 'box-info');
        checkVal = checkDigits($F('reg_province_id'), 'province_invalid');
        if (checkVal != true) {
          hideBox('reg_province_id', 'box-error'); //falls in diesem Feld schon Fehler aufgetreten sind
          showBox('reg_province_id', 'box-error', checkVal);
        }
        else {
          //status OK
          toggleOK('reg_province_id', 'blur');
        }
      }, false);
      Event.observe('reg_province_id', 'focus', function() {
        hideBox('reg_province_id', 'box-error');
        showBox('reg_province_id', 'box-info', txtInfo['province']);
        toggleOK('reg_province_id', 'focus');
      }, false);
    }

    if (inputObject.id == 'reg_street_id') {
      Event.observe('reg_street_id', 'blur', function() {
        hideBox('reg_street_id', 'box-info');
      }, false);
      Event.observe('reg_street_id', 'focus', function() {
        hideBox('reg_street_id', 'box-error');
        showBox('reg_street_id', 'box-info', txtInfo['street']);
      }, false);
    }

    if (inputObject.id == 'reg_streetnr_id') {
      Event.observe('reg_streetnr_id', 'blur', function() {
        hideBox('reg_streetnr_id', 'box-info');
        checkVal = checkStreet();
        if (checkVal != true) {
          hideBox('reg_street_id', 'box-error'); //falls in diesem Feld schon Fehler aufgetreten sind
          showBox('reg_street_id', 'box-error', checkVal);
        }
        else {
          //status OK
          toggleOK('reg_street_id', 'blur');
        }
      }, false);
      Event.observe('reg_streetnr_id', 'focus', function() {
        hideBox('reg_streetnr_id', 'box-error');
        showBox('reg_streetnr_id', 'box-info', txtInfo['streetnr']);
        toggleOK('reg_streetnr_id', 'focus');
      }, false);
    }

    if (inputObject.id == 'reg_zip_id') {
      Event.observe('reg_zip_id', 'blur', function() {
        hideBox('reg_zip_id', 'box-info');
      }, false);
      Event.observe('reg_zip_id', 'focus', function() {
        hideBox('reg_zip_id', 'box-error');
        showBox('reg_zip_id', 'box-info', txtInfo['zip']);
      }, false);
    }

    if (inputObject.id == 'reg_city_id') {
      Event.observe('reg_city_id', 'blur', function() {
        hideBox('reg_city_id', 'box-info');
        checkVal = checkZip();
        if (checkVal != true) {
          hideBox('reg_city_id', 'box-error'); //falls in diesem Feld schon Fehler aufgetreten sind
          showBox('reg_city_id', 'box-error', checkVal);
        }
        else {
          //status OK
          toggleOK('reg_city_id', 'blur');
        }
      }, false);
      Event.observe('reg_city_id', 'focus', function() {
        hideBox('reg_city_id', 'box-error');
        showBox('reg_city_id', 'box-info', txtInfo['city']);
        toggleOK('reg_city_id', 'focus');
      }, false);
    }

    if (inputObject.id == 'reg_bday_id') {
      Event.observe('reg_bday_id', 'blur', function() {
        hideBox('reg_bday_id', 'box-info');
      }, false);
      Event.observe('reg_bday_id', 'focus', function() {
        hideBox('reg_bday_id', 'box-error');
        showBox('reg_bday_id', 'box-info', txtInfo['birthdate']);
      }, false);
    }

    if (inputObject.id == 'reg_bmonth_id') {
      Event.observe('reg_bmonth_id', 'blur', function() {
        hideBox('reg_bmonth_id', 'box-info');
      }, false);
      Event.observe('reg_bmonth_id', 'focus', function() {
        hideBox('reg_bmonth_id', 'box-error');
        showBox('reg_bmonth_id', 'box-info', txtInfo['birthdate']);
      }, false);
    }

    if (inputObject.id == 'reg_byear_id') {
      Event.observe('reg_byear_id', 'blur', function() {
        hideBox('reg_byear_id', 'box-info');
        checkVal = checkBirthdate();
        if (checkVal != true) {
          hideBox('reg_byear_id', 'box-error'); //falls in diesem Feld schon Fehler aufgetreten sind
          showBox('reg_byear_id', 'box-error', checkVal);
        }
        else {
          //status OK
          toggleOK('reg_byear_id', 'blur');
        }
      }, false);
      Event.observe('reg_byear_id', 'focus', function() {
        hideBox('reg_byear_id', 'box-error');
        showBox('reg_byear_id', 'box-info', txtInfo['birthdate']);
        toggleOK('reg_byear_id', 'focus');
      }, false);
    }

  }
}
