$(function () {
    $("#error").hide();
    //reveal_password("pwd");
});

function reveal_password(element) {

    var x = document.getElementById(element);
    if (x.type === "password") {
        x.type = "text";
    } else {
        x.type = "password";
    }
    return false;
}


function register(e) {
    $("input[type='submit']").attr("disabled", true).val("Please wait...");
    var error = $("#error");
    var url = window.location.protocol + "//" + window.location.host + "/scripts/short_queries.php";
    $.ajax({
        type: "POST",
        url: url,
        data: $('#register').serialize(e),
        dataType: "json",
        encode: true,
        success: function (data, textStatus, jqXHR) {

            var newURL = "";

            if (data.status === true) {
                error.show();
                error.html(data.front.register);
                error.addClass('alert alert-success');
                error.removeClass('alert-danger');
                setTimeout(function () {
                    if (data.should_i_verify != null && data.should_i_verify.toLowerCase() === 'yes') {
                        // for confirmation
                        newURL = window.location.protocol + "//" + window.location.host + '/v/confirm/' + data.confirm_user_id;
                    } else if (data.should_i_verify != null && data.should_i_verify.toLowerCase() === 'no') {
                        newURL = window.location.protocol + "//" + window.location.host + '/v/login';
                    } else {
                        // for confirmation
                        newURL = window.location.protocol + "//" + window.location.host + '/v/confirm/' + data.confirm_user_id;
                    }
                    window.location = newURL;
                }, 5000);
            } else if (data.status === false) {
                if (data.error.first_name) {
                    error.show();
                    error.html(data.error.first_name);
                    $('#first_name').addClass('error');
                    error.addClass('alert alert-danger');
                }

                if (data.error.last_name) {
                    error.show();
                    error.html(data.error.last_name);
                    $('#last_name').addClass('error');
                    error.addClass('alert alert-danger');
                }

                if (data.error.username) {
                    error.show();
                    error.html(data.error.username);
                    $('#username').addClass('error');
                    error.addClass('alert alert-danger');
                }

                if (data.error.email) {
                    error.show();
                    error.html(data.error.email);
                    $('#email').addClass('error');
                    error.addClass('alert alert-danger');
                }

                if (data.error.phone) {
                    error.show();
                    error.html(data.error.phone);
                    $('#phone').addClass('error');
                    error.addClass('alert alert-danger');
                }

                if (data.error.captcha) {
                    error.show();
                    error.html(data.error.captcha);
                    $('#captcha').addClass('error');
                    error.addClass('alert alert-danger');
                }

                if (data.error.emailcheck) {
                    error.show();
                    error.html(data.error.emailcheck);
                    $('#email').addClass('error');
                    error.addClass('alert alert-danger');
                }

                if (data.error.password) {
                    error.show();
                    error.html(data.error.password);
                    $('#pass_confirmation').addClass('error');
                    error.addClass('alert alert-danger');
                }

                if (data.error.registerfailure) {
                    error.show();
                    error.html(data.error.registerfailure);
                    error.addClass('alert alert-danger');
                }
                $("input[type='submit']").attr("disabled", false).val("Register");

            }


        },
        error: function (jqXHR, textStatus, errorThrown) {
            error.show();
            error.html('<pre><code class="prettyprint">AJAX Request Failed<br/> textStatus=' + textStatus + ', errorThrown=' + errorThrown + '</code></pre>');
            error.addClass('alert alert-danger');
            $("input[type='submit']").attr("disabled", false).val("Register");

        }
    });
    e.preventDefault();

}


function show_state(i) {
    $("#state_label").html("<i class='fa fa-spinner fa-spin fa-1x'></i>");
    var url = window.location.protocol + "//" + window.location.host + "/" + 'scripts/short_queries.php';
    $.get(url, {app: 'location_data', process: 'state_list', country_id: i})
        .done(function (data) {
            setTimeout(function () {
                $("#state_label").html("");
                $("#state_option").html('<select name="state" id="state" class="form-control state"><option value="" >==Select State==</option>' + data + '</select>');
            }, 1000);

        });
}

function show_city(i) {
    $("#city_label").html("<i class='fa fa-spinner fa-spin fa-1x'></i>");
    var url = window.location.protocol + "//" + window.location.host + "/" + 'scripts/short_queries.php';
    $.get(url, {app: 'location_data', process: 'city_list', state_id: i})
        .done(function (data) {
            setTimeout(function () {
                $("#city_label").html("");
                $("#city_option").html('<select name="city" id="city" class="form-control city"><option value="" >==Select City==</option>' + data + '</select>');
            }, 1000);

        });
}


$(function () {


    // this is the register page control
    $('#register').submit(function (e) {
        register(e);
        e.preventDefault();
    });

    // this is the country drop down snippet
    $(".country").on("change_off", function () {
        var countryId = $('#country').val();
        if (countryId != '') {
            show_state(countryId);
        } else {
            $(".state option:gt(0)").remove();
        }
    });

    $(document).on("change", ".state", function () {
        var stateId = $('#state').val();
        if (stateId != '') {
            show_city(stateId);
        } else {
            $(".city option:gt(0)").remove();
        }
    });


});