/*---------@local storage for Forms-----------------
 *input or select, values store in local storage
 */

$(function(){

    var activate_local_storage = 0;

    if(activate_local_storage) {
        $("form input").focusout(function () {
            var element_name = $(this).attr('name');
            var value = $(this).val();
            saveUserInformation(element_name, value);
        })
        $("form select").change(function () {
            var element_name = $(this).attr('name');
            var value = $(this).val();
            saveUserInformation(element_name, value);
        });

        /*reload form data from local storage*/
        showUserInformation();
    }
});


/*
 * @save fill form information in local storage
 * */
function saveUserInformation(element_name, value){
    if($.trim(value) != ''){
        localStorage.setItem(element_name, value);
    }
}

/*
 * @load user information from local storage
 * */
function showUserInformation(){

    if(typeof(localStorage) !== "undefined"){
        $( "form input" ).each(function() {
            var this_element_name    = $(this).attr('name');
            var this_value           = $(this).val();

            if(localStorage.getItem(this_element_name) != ''){
                if(this_value == ''){
                    $(this).val(localStorage.getItem(this_element_name));
                }
            }
        });

        $( "form select" ).each(function() {
            var this_element_name    = $(this).attr('name');

            if(localStorage.getItem(this_element_name) != ''){
                $(this).find('option[value="'+localStorage.getItem(this_element_name)+'"]').prop('selected', true);
            }
        });
    }
}