
///////////////// jQUERY /////////////////////////////////
$(document).ready(function(){
    $(function() {
        addEvents();
    });
});

//////////// JQUERY ADD EVENTS /////////////////////////////////////
function addEvents() {
    //create salt for hash page
    $("#aSalt").click(function(e){
        var salt = Math.uuid();
        $('#txtSalt').val(salt);
        e.preventDefault();
    })

    //confirm word to hash on hash page
    $("#txtConfirm").blur(function() {
        var word = $("#txtWord").val();
        var confirm = $(this).val();
        if (word != confirm) {
            alert("'Word' and 'Confirm Word' do not match!");
        }
    })

    //generate hash via ajax
    $("#btnGenerate").click(function() {
        var hash = $("#txtHash");

        $.ajax({
            type: 'GET',
            url: "hashAjax.php",
            data: {
                word: $("#txtWord").val(),
                salt: $("#txtSalt").val(),
                algo: $("#selAlgo").val()
            },
            beforeSend: function() {
                hash.html("Loading...");
            },
            success: function(data) {
                hash.html(data);
            },
            error: function() {
                hash.html("There was a problem - unable to create hash");
            }
        })
    })

}

///////////////// CONFIRM SUBMIT //////////////////////////////////////////
function confirmSubmit(type) {
    var agree = confirm("Are you sure you wish to " + type + "?");
    if (agree) {
            return true;
    }
    else {
            return false;
    }
}

//////////////// SET FOCUS ON DESIGNATED ELEMENT ////////////////////////
function setFocus(elementID) {
    if (elementID != "") {
        $("#" + elementID).focus();
    }
}

///////////////// MAINTAIN SCROLL POSITION /////////////////////////////
function saveScrollCoordinates() {
	var top = document.body.scrollTop;
	if (top == 0) {
            if (window.pageYOffset) {
                top = window.pageYOffset;
            }
            else {
                top = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
            }
	}
	$('#scrollY').val(top);
}

function scrollToCoordinates() {
	var y = $('#scrollY').val();
	window.scrollTo(0, y);
}





