$(document).ready(function() {
  
  var form = $("#userRegistration");
  
  form.find("select[name='question']").change(function() {
    if ($(this).find("option:selected").val() == "qfree") {
      $("#ownquestioncontainer").show().find("input").focus();
    }
    else {
      $("#ownquestioncontainer").hide().find("input").val("");
    }
  });
  
  $("#idchecker").click(function() {
    var eMsg = $("#actionmessage");
    if (isEmpty($("#sunid").val())) {
      eMsg.removeClass("success").addClass("error").text("Please enter your Sun ID").show(); $("#sunid").focus(); return;
    }
    var btn = $(this).val("Please wait...").attr("disabled", true);
    $.ajax({
      url: 'check_sun_id.php?id=' + $("#sunid").val(),
      dataType: 'json',
      type: 'GET',
      success: function(result) {
        if (result.exist) {
          eMsg.removeClass("success").addClass("error").text("This ID is not available.").show();
        }
        else {
          eMsg.removeClass("error").addClass("success").text("This ID is available.").show();
        }
        btn.val("Check ID").removeAttr("disabled");
      }
    });
  });
  
  form.submit(function() {
    var eMsg = $("#actionmessage").removeClass("success").addClass("error");
    var firstname = $(this).find("input[name='firstname']").val();
    var lastname = $(this).find("input[name='lastname']").val();
    var phonenumber = $("#phonenumber").val();
    var broadbandnumber = $("#broadbandnumber").val();
    var personalemail = $(this).find("input[name='personalemail']").val();
    //var sunemail = $(this).find("input[name='sunemail']").val();
    var sunid = $(this).find("input[name='sunid']").val();
    var mobilepostpaidnumber = $("#mobilepostpaidnumber").val();
    var broadbandpostpaidnumber = $("#broadbandpostpaidnumber").val();
    var question = $(this).find("select[name='question'] option:selected").val();
    var ownquestion = $(this).find("input[name='ownquestion']").val();
    var answer = $(this).find("input[name='answer']").val();
    
    if (isEmpty(firstname)) {
      eMsg.text("Please fill in your firstname.").show(); return false;
    }
    if (isEmpty(lastname)) {
      eMsg.text("Please fill in your lastname.").show(); return false;
    }
    if (isEmpty(phonenumber) && isEmpty(broadbandnumber)) {
      eMsg.text("Please fill in mobile number postpaid and/or broadband.").show(); return false;
    }
    if (!isEmpty(phonenumber) && !isNumeric(phonenumber)) {
      eMsg.text("Please fill in your valid mobile number (postpaid).").show(); return false;
    }
    if (!isEmpty(broadbandnumber) && !isNumeric(broadbandnumber)) {
      eMsg.text("Please fill in your valid mobile number (broadband).").show(); return false;
    }
    if (isEmpty(personalemail) || !isValidEmail(personalemail)) {
      eMsg.text("Please fill in your valid personal email.").show(); return false;
    }
    //if (isEmpty(sunemail)) {
    //  eMsg.text("Please fill in your valid SUN email.").show(); return false;
    //}
    if (isEmpty(sunid)) {
      eMsg.text("Please fill in your desired SUN ID.").show(); return false;
    }
    if (!isEmpty(phonenumber) && (isEmpty(mobilepostpaidnumber) || !isNumeric(mobilepostpaidnumber))) {
      eMsg.text("Please fill in your valid mobile postpaid account number.").show(); return false;
    }
    if (!isEmpty(broadbandnumber) && (isEmpty(broadbandpostpaidnumber) || !isNumeric(broadbandpostpaidnumber))) {
      eMsg.text("Please fill in your valid broadband postpaid account number.").show(); return false;
    }
    if (isEmpty(question) || (question == "qfree" && isEmpty(ownquestion))) {
      eMsg.text("Please fill in security question.").show(); return false;
    }
    if (isEmpty(answer)) {
      eMsg.text("Please fill in answer.").show(); return false;
    }

    return true;
  });
  
});