/*
  JS for registration flow
  made by Claudiu (claudiuj@gmail.com)
*/

function do_submit(theForm)
{
  if (validate_reg_form(theForm))
    theForm.submit();
}

function validate_reg_form(theForm)
{
  if (typeof(theForm) == 'undefined')
    return false;

  // first name
  if (!check_first_name(theForm)) return false;
  // last name
  if (!check_last_name(theForm)) return false;
  // address
  if (!check_address(theForm)) return false;
  // city
  if (!check_city(theForm)) return false;
  // email
  if (!check_email(theForm)) return false;
  // age
  if (!check_age(theForm)) return false;
  // smoker_age
  if (!check_smoker_age(theForm)) return false;

  return true;
}

function reg_form_errmsg(form, field, msg)
{
  if (typeof(form) != 'undefined' &&
      typeof(form.elements[field]) != 'undefined')
  {
    alert(msg);
    form.elements[field].focus();
  }
  return false;
}

// first name
function check_first_name(theForm)
{
  if (typeof(theForm.first_name) == 'undefined')
    return false;

  var first_name = theForm.first_name.value;

  if (typeof(first_name) == 'undefined')
    return false;

  if (first_name == "")
  {
    alert("Va rugam sa completati campul \"Nume\".");
    theForm.first_name.focus();
    return false;
  }

  if (first_name.length < 3)
  {
    alert("Va rugam sa introduceti un nume valid.");
    theForm.first_name.focus();
    return false;
  }
  return true;
}

// last name
function check_last_name(theForm)
{
  if (typeof(theForm.last_name) == 'undefined')
    return false;

  var last_name = theForm.last_name.value;

  if (typeof(last_name) == 'undefined')
    return false;

  if (last_name == "")
  {
    alert("Va rugam sa completati campul \"Prenume\".");
    theForm.last_name.focus();
    return false;
  }

  if (last_name.length < 3)
  {
    alert("Va rugam sa introduceti un prenume valid.");
    theForm.last_name.focus();
    return false;
  }

  return true;
}

// address
function check_address(theForm)
{
  if (typeof(theForm.address) == 'undefined')
    return false;

  var address = theForm.address.value;

  if (typeof(address) == 'undefined')
    return false;

  if (address == "")
  {
    alert("Va rugam sa completati campul \"Adresa\".");
    theForm.address.focus();
    return false;
  }

  if (address.length < 6)
  {
    alert("Va rugam sa introduceti adresa dvs. postala completa.");
    theForm.address.focus();
    return false;
  }

  return true;
}

// city
function check_city(theForm)
{
  if (typeof(theForm.city) == 'undefined')
    return false;

  var city = theForm.city.value;

  if (typeof(city) == 'undefined')
    return false;

  if (city == "")
  {
    alert("Va rugam sa completati campul \"Oras\".");
    theForm.city.focus();
    return false;
  }

  if (city.length < 3)
  {
    alert("Va rugam sa introduceti un oras valid.");
    theForm.city.focus();
    return false;
  }

  return true;
}

function check_email(theForm)
{
  if (typeof(theForm.email) == 'undefined')
    return false;

  var email = theForm.email.value;

  if (typeof(email) == 'undefined')
    return false;

  if (email == "")
  {
    alert("Va rugam sa completati campul \"Email\".");
    theForm.email.focus();
    return false;
  }

  if (email.length < 5)
  {
    alert("Adresa de email ne este necesara pentru a va putea contacta; va rugam sa o introduceti corect in " +
            "campul corespunzator. Va multumim pentru incredere.");
    theForm.email.focus();
    return (false);
  }

  // check for correct email address
  var filter = /^[A-Za-z0-9][A-Za-z0-9\_\-\.]*\@[A-Za-z0-9][A-Za-z0-9\_\-\.]*\.[A-Za-z]{2,3}$/;
  var filter2 = /^.*[\.\@\_\-][\.\@\_\-]+.*$/;
  if( (!filter.test( theForm.email.value ))  || ( filter2.test( theForm.email.value )) )
  {
    alert("Adresa de email ne este necesara pentru a va putea contacta; va rugam sa o introduceti corect in " +
            "campul corespunzator. Va multumim pentru incredere.");
    theForm.email.focus();
    return (false);
  }

  return true;
}

// age
function check_age(theForm)
{
  if (typeof(theForm.age) == 'undefined')
    return false;

  var age = theForm.age.value;

  if (typeof(age) == 'undefined')
    return false;

  if (age == "")
  {
    alert("Va rugam sa completati campul \"Varsta\".");
    theForm.age.focus();
    return false;
  }

  var tmp = parseInt(age);
  if (isNaN(tmp) || tmp < 10)
  {
    alert("Sunteti cam tanar pentru un fumator. Probabil doriti sa inscrieti la acest curs un membru al familiei; " +
            "va rugam ca in acest caz sa completati datele de contact ale persoanei pe care doriti sa o inscrieti, " +
            "inclusiv ... varsta. Va multumim.");
    theForm.age.focus();
    return false;
  }

  return true;
}

function check_smoker_age(theForm)
{
  if (typeof(theForm.smoker_age) == 'undefined')
    return false;

  var smoker_age = theForm.smoker_age.value;

  if (typeof(smoker_age) == 'undefined')
    return false;

  if (smoker_age == "")
  {
    alert("Va rugam sa ne spuneti de cati ani fumati.");
    theForm.smoker_age.focus();
    return false;
  }

  var tmp = parseInt(smoker_age);
  if (isNaN(tmp) || tmp < 1)
  {
    alert("Va rugam sa ne spuneti de cati ani fumati, in campul corespunzator.");
    theForm.smoker_age.focus();
    return false;
  }

  return true;
}

