﻿function is_valid_email(email) {
    return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email);
}

function checkSpace(str){   
    var result = true;
    if (str.indexOf(" ") != -1) {   
        result = false;
    }
    return result;
}
  
  
$(document).ready(function() {
    $('#username').blur(function() {
        if ($(this).val() == '') {
            $(this).next().next().html('Please enter your username.');
        } else if ($(this).val().length > 14 ) {
            $(this).next().next().html('Username must be less than 14 letters.');
        } else if (!checkSpace($(this).val())) {
            $(this).next().next().html('Don\'t let space in your username.');
        } else {  
            $(this).next().next().html('');
        }
    });

    $('#password').blur(function() {
        if ($(this).val() == '') {
            $(this).next().next().html('Please enter your password.');
        } else if ($(this).val().length < 6 || $(this).val().length > 14) {
        $(this).next().next().html('Must have at least 6 characters and less than 14 characters.');
        } else if (!checkSpace($(this).val())) {
            $(this).next().next().html('Don\'t let space in your password.');
        } else {
            $(this).next().next().html('');
        }
    });

    $('#re_password').blur(function() {
        if ($(this).val() == '') {
            $(this).next().next().html('Please enter your password again.');
        } else if ($(this).val() != $('#password').val()) {
            $(this).next().next().html('Please match your password.');
        } else {
            $(this).next().next().html('');
        }
    });

    $('#secret_question').change(function() {
        if ($(this).val() == '') {
            $(this).next().next().html('Please select your secret question.');
        } else {
            $(this).next().next().html('');
        }
    });

    $('#secret_answer').focus(function() {
        if ($('#secret_question').val() == '') {
            $('#secret_question').next().next().html('Please select your secret question.');
        } else {
            $(this).next().next().html('');
        }
    });

    $('#secret_answer').blur(function() {
        if ($(this).val() == '') {
            if ($('#secret_question').val() == '') {
                $('#secret_question').next().next().html('Please select your secret question.');
            } else {
                $(this).next().next().html('Please enter your secret answer.');
            }
        } else {
            $(this).next().next().html('');
        }
    });


    $('#register_form').submit(function() {
        var flag = true;
        var username = $('#username').val();
        var email = $('#email').val();
        var pass = $('#password').val();
        var re_pass = $('#re_password').val();

        if (username == '') {
            $('#username').next().next().html('Please enter your username.');
            flag = false;
        } else if (username.length > 14 ) {
            $('#username').next().next().html('Username must be less than 14 letters.');
            flag = false;
        } else if (!checkSpace(username)){
            $('#username').next().next().html('Don\'t let space in your username.');
            flag = false;
        }
        
        if ($('#password').val() == '') {
            $('#password').next().next().html('Please enter your password.');
            flag = false;
        } else if ($('#password').val().length < 6 || $('#password').val().length > 14) {
            $('#password').next().next().html('Must have at least 6 characters and less than 14 characters.');
            flag = false;
        } else if (!checkSpace($('#password').val())) {
            $('#password').next().next().html('Don\'t let space in your password.');
            flag = false;
        }

        if ($('#re_password').val() == '') {
            $('#re_password').next().next().html('Please enter your password again.');
            flag = false;
        } else if ($('#re_password').val() != $('#password').val()) {
            $('#re_password').next().next().html('Please match your password.');
            flag = false;
        }

        if ($('#secret_question').val() == '') {
            $('#secret_question').next().next().html('Please select your secret question.');
            flag = false;
        }

        if ($('#secret_answer').val() == '') {
            if ($('#secret_question').val() != '') {
                $('#secret_answer').next().next().html('Please enter your secret answer.');
                $('#secret_question').next().next().html('');
            } else {
                $('#secret_answer').next().next().html('');
            }
            flag = false;
        }

        if ($('#read_check').attr('checked') == false) {
            $('#check_notice').html('Please check above option.');
            flag = false;
        }else{
			$('#check_notice').html('');
		}

        return flag;
    });
});