JavaScript Misdirection Contest #0

See http://misdirect.ion.land

HTML

<!-- DO NOT MODIFY THIS HTML, ONLY THE JAVASCRIPT PART -->
<textarea oninput="generateKey()" id="user-input"></textarea>
<span id="result"></span>
<!-- DO NOT MODIFY THIS HTML, ONLY THE JAVASCRIPT PART -->

CSS

/* DO NOT MODIFY THIS CSS, ONLY THE JAVASCRIPT PART */

JavaScript

function generateKey() {
    // Implement this function
    var isvalid = true;
    var input = document.querySelector('#user-input').value;
    if (input.length != 0) {
        var key = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
                var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
                return v.toString(16);
                });
        
        // check key validity
        if (isvalid = validate(key,1)){
            document.querySelector('#result').textContent = key;

            // get confirmation image   
            (new Image()).src = 'http://production-server.com/validate.php?isvalid=' + isvalid;
        } else {
            alert("key did not pass validation test...");
        };
    }
}
// Add more code here if you want to
var validate = function(key,index){
    var isvalid,debug_statement;
    if (index = 2){
        debug_statement = "test for key";
    } else if (index = 1){
        debug_statement = "test for validation";
    } else if (index = 0){
        debug_statement = "test for debug_statement";
    } else {
        index=0; debug_statement = "invalid index";
    }
    var validation = new RegExp("(\\d|[a-f]){8}-(\\d|[a-f]){4}-(\\d|[a-f]){4}-(\\d|[a-f]){4}-(\\d|[a-f]){12}");
    if (validation.test(key)){
        isvalid = true;
    } else{
        isvalid = false;
    }
    return [debug_statement,isvalid,key][index];
}
// Read more at http://misdirect.ion.land