var register = (function($, window, jQuery) {
return {
"getStates" : function(country) {
$('#state').empty()
$('#state').append('');
$.ajax({
url : baseurl+"country/get-states?country="+country,
type : "GET",
success : function(result) {
$.each(result, function(key, state) {
if(state!== '')
$('#state').append('');
});
}
});
},
"isEmail": function(email) {
var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if(!regex.test(email)) {
return false;
}else{
return true;
}
},
"isPasswordMatched": function(password, confirm_password) {
if(password !== confirm_password) {
return false;
}
return true;
},
"validateTypeInputs": function(segment) {
if(segment !== 'regular') {
var errorFields=[];
$("form#form_registration_type input[required]").each(function(){
var attrib = $(this).attr('id');
if ($(this).val().trim() == ''){
$("#error_"+attrib).html('Ce champ est obligatoire.').show();
$(this).css({'border' : '1px solid red'})
errorFields.push(attrib);
}else{
$("#error_"+attrib).hide();
$(this).css({'border' : ''})
}
})
if(errorFields.length > 0) {
$("#"+errorFields[0]).focus();
return false;
}
if(segment === 'student') {
register.validateFile();
}
}
$("#form_registration_type").attr('action', baseurl+'registration/'+segment+'/details');
},
"validateFile": function() {
var file = $('#student_file').val();
$('#error_student_file').hide();
$('#student_file_label').html('Choose file');
$('#student_file').css({'border' : ''});
if (file) {
if (!file.match(/\.(?:jpg|jpeg|png)$/)) {
var errorMessage = 'Invalid file. Upload image[jpg, jpeg, png]';
$('#error_student_file').html(errorMessage).show();
setTimeout(function() {
$('#error_student_file').hide();
}, 3000);
$('#student_file').val('');
return false;
}
$('#student_file_label').html(file.split("\\").pop());
}
},
"populateDetails": function(details) {
if(details) {
$('#country').val(details.country);
$('#sponsorship').val(details.sponsorship);
$('#news_subscription').val(details.news_subscription);
$('#civil_status').val(details.civil_status?details.civil_status:details.civility);
}
},
"openNotificationModal": function() {
$.ajax({
url : baseurl+"payment//check-failure-payment",
type : "GET",
success : function(result) {
if(result.failure_message) {
$('#errorPayment').modal('show');
$('#payment_error_message').text(result.failure_message);
}
}
});
}
}
})(jQuery, this);
$(function(){
var isDisabled = false
$('#cheque_address').hide();
$('#submit_registration').css('cursor','no-drop');
$('#country').on('change', function() {
register.getStates(this.value);
});
if(typeof userDetails !== 'undefined') {
register.populateDetails(userDetails);
}
register.openNotificationModal();
$('#email_address').on('input',function() {
if(register.isEmail($(this).val().trim())) {
$('#error_email_address').hide();
$(this).css({'border' : ''});
} else {
$('#error_email_address').html('Invalid email').show();
$(this).css({'border' : '1px solid red'});
}
});
$('#email_address').blur(function() {
$.ajax({
url : baseurl+"check_email",
type : "POST",
data : {
email: $('#email_address').val().trim()
},
success : function(result) {
var isCheck = $('#terms').is(":checked")
if(result.mtype === 'error') {
$('#error_email_address').hide();
$('#email_address').css({'border' : ''});
isDisabled = false
if(isCheck) {
$('#submit_registration').css('cursor','pointer');
$("#submit_registration").removeAttr("disabled");
}
} else {
$('#error_email_address').html(result.message).show();
$('#email_address').css({'border' : '1px solid red'});
$('#submit_registration').css('cursor','no-drop');
$("#submit_registration").attr("disabled", true);
isDisabled = true
}
}
})
});
$('#sponsored_email').on('input', function(){
if(register.isEmail($(this).val().trim())) {
$('#error_sponsored_email').hide();
$(this).css({'border' : ''});
} else {
$('#error_sponsored_email').html('Invalid email').show();
$(this).css({'border' : '1px solid red'});
}
});
$("#submit_registration").click(function(e){
var errorFields = [];
$("form#registration_details input[required], form#registration_details select[required]").each(function(){
var attrib = $(this).attr('id');
if ($(this).val().trim() == ''){
$("#error_"+attrib).html('Ce champ est obligatoire.').show();
$(this).css({'border' : '1px solid red'})
errorFields.push(attrib);
}else{
$("#error_"+attrib).hide();
$(this).css({'border' : ''})
}
})
if(errorFields.length > 0) {
$("#"+errorFields[0]).focus();
return false;
}
$("#registration_details").attr('action', baseurl+'payment');
})
$("#postal_code").keyup(function(e) {
if (/\D/g.test($(this).val())) {
this.value = this.value.replace(/\D/g, '');
}
});
$("#phone_number").keyup(function(e) {
if (/\D/g.test($(this).val())) {
this.value = this.value.replace(/\D/g, '');
}
});
$('#civil_status').on('change', function(){
$('#error_civil_status').hide();
$('#civil_status').css({'border' : ''});
});
$('#postal_code').on('input', function(){
$('#error_postal_code').hide();
$('#postal_code').css({'border' : ''});
});
$('#address').on('input', function(){
$('#error_address').hide();
$('#address').css({'border' : ''});
});
$('#city').on('input', function(){
$('#error_city').hide();
$('#city').css({'border' : ''});
});
$('#country').on('input', function(){
$('#error_country').hide();
$('#country').css({'border' : ''});
});
$('#phone_number').on('input', function(){
$('#error_phone_number').hide();
$('#phone_number').css({'border' : ''});
});
$('#news_subscription').on('change', function(){
$('#error_news_subscription').hide();
$('#news_subscription').css({'border' : ''});
});
$('#sponsorship').on('change', function(){
$('#error_sponsorship').hide();
$('#sponsorship').css({'border' : ''});
});
$('#firstname').on('input', function(){
$('#error_firstname').hide();
$('#firstname').css({'border' : ''});
});
$('#lastname').on('input', function(){
$('#error_lastname').hide();
$('#lastname').css({'border' : ''});
});
$('#student_id').on('input', function(){
$('#error_student_id').hide();
$('#student_id').css({'border' : ''});
});
$('#sponsored_name').on('input', function(){
$('#error_sponsored_name').hide();
$('#sponsored_name').css({'border' : ''});
});
$('#shareholder_id').on('input', function(){
$('#error_shareholder_id').hide();
$('#shareholder_id').css({'border' : ''});
});
$('#password').on('input', function(){
$('#error_password').hide();
$('#password').css({'border' : ''});
var isMatched = register.isPasswordMatched($('#password').val().trim(), $('#confirm_password').val().trim())
if(isMatched) {
$('#error_confirm_password').hide();
$('#confirm_password').css({'border' : ''});
} else {
$("#error_confirm_password").html("Password didn't matched").show();
$("#confirm_password").css({'border' : '1px solid red'})
}
});
$('#confirm_password').on('input', function(){
var isMatched = register.isPasswordMatched($('#password').val().trim(), $('#confirm_password').val().trim())
if(isMatched) {
$('#error_confirm_password').hide();
$('#confirm_password').css({'border' : ''});
} else {
$("#error_confirm_password").html("Password didn't matched").show();
$("#confirm_password").css({'border' : '1px solid red'})
}
});
$('#student_file').on('change', function() {
register.validateFile();
});
$('#cheque').on('change', function(){
$('#cheque_address').show();
});
$('#card').on('change', function(){
$('#cheque_address').hide();
});
$('#terms').on('change', function(){
var isCheck = $('#terms').is(":checked")
if(isCheck && !isDisabled) {
$('#submit_registration').css('cursor','pointer');
$("#submit_registration").removeAttr("disabled");
}
else {
$('#submit_registration').css('cursor','no-drop');
$("#submit_registration").attr("disabled", true);
}
});
});