function notnz(selected) {
    if (selected != '111' && selected != '0') {
        $('#nztowns').hide();
        $('#entertown').show();
        $('#errorcountry').hide();
        $("#validcountry").removeClass('incorrect');
        $("#validcountry").addClass('ticked');
    } else if (selected == '111') {
        $('#nztowns').show();
        $('#entertown').hide();
        $('#errorcountry').hide();
        $("#validcountry").removeClass('incorrect');
        $("#validcountry").addClass('ticked');
    } else {
        $('#nztowns').hide();
        $('#entertown').hide();
        $("#errorcountry").html("Please choose country");
        $('#errorcountry').show();
        $("#validcountry").removeClass('ticked');
        $("#validcountry").addClass('incorrect');
    }
}
function emailValidate() {
    email = $("#email").val();
    $.ajax({
        url: "/ajax/checkemail.cfm?email=" + email,
        success: function (resp) {
            if (resp == "missing") {
                $("#erroremail").html("Please enter email");
                $('#erroremail').show();
                $("#validEmail").removeClass('ticked');
                $("#validEmail").addClass('incorrect');
            } else if (resp == "invalid") {
                $("#erroremail").html("Invalid email address");
                $('#erroremail').show();
                $("#validEmail").removeClass('ticked');
                $("#validEmail").addClass('incorrect');
            } else if (resp == "exists") {
                $("#erroremail").html("Email already exists");
                $('#erroremail').show();
                $("#validEmail").removeClass('ticked');
                $("#validEmail").addClass('incorrect');
            } else if (resp == "success") {
                $('#erroremail').hide();
                $("#validEmail").removeClass('incorrect');
                $("#validEmail").addClass('ticked');
            }
        }
    });
}
function usernameValidate() {
    username = $("#username").val();
    $.ajax({
        url: "/ajax/checkusername.cfm?username=" + username,
        success: function (resp) {
            if (resp == "missing") {
                $("#errorusername").html("Please enter username");
                $('#errorusername').show();
                $("#validusername").removeClass('ticked');
                $("#validusername").addClass('incorrect');
            } else if (resp == "length") {
                $("#errorusername").html("Length must between 4-40 characters");
                $('#errorusername').show();
                $("#validusername").removeClass('ticked');
                $("#validusername").addClass('incorrect');
            } else if (resp == "invalid") {
                $("#errorusername").html("Invalid username");
                $('#errorusername').show();
                $("#validusername").removeClass('ticked');
                $("#validusername").addClass('incorrect');
            } else if (resp == "exists") {
                $("#errorusername").html("username already been taken");
                $('#errorusername').show();
                $("#validusername").removeClass('ticked');
                $("#validusername").addClass('incorrect');
            } else if (resp == "success") {
                $('#errorusername').hide();
                $("#validusername").removeClass('incorrect');
                $("#validusername").addClass('ticked');
            }
        }
    });
}
function pwdValidate() {
    pwd = $("#pwd").val();
    if (pwd.trim() == "") {
        $("#errorpwd").html("Please choose a password");
        $('#errorpwd').show();
        $("#validpwd").removeClass('ticked');
        $("#validpwd").addClass('incorrect');
    } else if (pwd.trim().length < 6) {
        $("#errorpwd").html("Must be longer than 6 characters");
        $('#errorpwd').show();
        $("#validpwd").removeClass('ticked');
        $("#validpwd").addClass('incorrect');
    } else if (pwd.trim().length > 50) {
        $("#errorpwd").html("Must be shorter than 50 characters");
        $('#errorpwd').show();
        $("#validpwd").removeClass('ticked');
        $("#validpwd").addClass('incorrect');
    } else {
        $('#errorpwd').hide();
        $("#validpwd").removeClass('incorrect');
        $("#validpwd").addClass('ticked');
    }
}
function bdValidate() {
    day = $("#day").val();
    month = $("#month").val();
    year = $("#year").val();
    $.ajax({
        url: "/ajax/checkbd.cfm?d=" + day + "&m=" + month + "&y=" + year,
        success: function (resp) {
            if (resp == "missing") {
                $("#validbd").removeClass('ticked');
                $("#validbd").addClass('incorrect');
            } else if (resp == "invalid") {
                $("#errorbd").html("Invalid date of birth");
                $('#errorbd').show();
                $("#validbd").removeClass('ticked');
                $("#validbd").addClass('incorrect');
            } else if (resp == "success") {
                $('#errorbd').hide();
                $("#validbd").removeClass('incorrect');
                $("#validbd").addClass('ticked');
            }
        }
    });
}
function locationValidate(selected, scope) {
    if (selected.trim() == "") {
        $("#valid" + scope).removeClass('ticked');
        if (scope == 'region') $("#valid" + scope).addClass('incorrect');
    } else {
        $("#valid" + scope).removeClass('incorrect');
        $("#valid" + scope).addClass('ticked');
    }
}

function townValidate() {
    reg = new RegExp("<[^>]*>");
    town = $("#city").val();
    if (town.trim() == "") {
        $("#validtown").removeClass('ticked');
        $("#validtown").addClass('incorrect');
        $('#errortown').hide();
    } else if (reg.test(town.trim())) {
        $("#validtown").removeClass('ticked');
        $("#validtown").addClass('incorrect');
        $("#errortown").html("Invalid city name");
        $('#errortown').show();
    } else {
        $("#validtown").removeClass('incorrect');
        $("#validtown").addClass('ticked');
        $('#errortown').hide();
    }
}

function validateall() {
    emailValidate();
    usernameValidate();
    pwdValidate();
    bdValidate();
}
