var select = document.getElementById('existing-type');
var input = document.getElementById("bottype");
var channel = document.getElementById("channel");
var username = document.getElementById("username");
var description = document.getElementById("type");
var form = document.getElementById("submit-form");
var stNew = document.getElementById("new-bot");
var stCorrect = document.getElementById("correction");
var chanGroup = document.getElementById("channel-group");

// Conditionally show description field if the type is a new one.
function update() {
    if(parseInt(select.value, 10) == 0) {
        input.removeAttribute("hidden");
        description.required = true;
        description.disabled = false;
    }
    else {
        input.setAttribute("hidden", true);
        description.required = false;
        description.disabled = true;
    }
}
select.addEventListener("change", update);
update();

// Validation
function formChecker() {
    if(channel.value.toLowerCase() == username.value.toLowerCase() && stNew.checked)
        channel.setCustomValidity("The bot user has to be different from the channel it is for.");
    else
        channel.setCustomValidity("");

   return channel.validity.valid && username.validity.valid;
}
formChecker();

channel.addEventListener("keyup", formChecker);
username.addEventListener("keyup", function() {
    formChecker();
    username.setCustomValidity("");
});
form.addEventListener("submit", function(e) {
    if(!formChecker())
        e.preventDefault();

    //TODO also track errors/successes
    var _paq = _paq || [];
    _paq.push(['trackEvent', 'Submission', stNew.checked ? "Submit" : "Correct", username.value]);
});

// User checking
function checkBot(username, cbk) {
    var url = "https://api.twitchbots.info/v1/bot/" + username;

    var xhr = new XMLHttpRequest();

    xhr.open("HEAD", url, true);
    xhr.onreadystatechange = function(e) {
        if(xhr.readyState === 2)
            cbk(xhr.status !== 404);
    };

    xhr.send();
}

function checkTwitchUser(username, cbk) {
    var url = "https://api.twitch.tv/kraken/users/" + username;

    var xhr = new XMLHttpRequest();

    xhr.open("HEAD", url, true);
    xhr.setRequestHeader('Client-ID', form.dataset.clientid);
    xhr.setRequestHeader('Accept', 'application/vnd.twitchtv.v3+json');
    xhr.onreadystatechange = function(e) {
        if(xhr.readyState === 2) {
            // Only reject if the status is 404 not found.
            cbk(xhr.status !== 404);
        }
    };

    xhr.send();
}

function validateFieldContent(field, shouldExist) {
    field.checkValidity();
    if(field.validity.valid && field.value.length) {
        if(shouldExist && !stNew.checked) {
            checkBot(field.value, function(exists) {
                if(exists)
                    field.setCustomValidity("");
                else
                    field.setCustomValidity("Only known bots can be corrected.");
            });
        }
        else {
            checkTwitchUser(field.value, function(exists) {
                if(exists)
                    field.setCustomValidity("");
                else
                    field.setCustomValidity("Must be an existing Twitch user.");


                if(shouldExist & field.validity.valid) {
                    checkBot(field.value, function(exists) {
                        if(exists)
                            field.setCustomValidity("We already know about this bot.");
                        else
                            field.setCustomValidity("");
                    });
                }
            });
        }
    }
}

function stListener() {
    username.setCustomValidity("");
    for(var i = 0; i < fields.length; ++i) {
        validateFieldContent(fields[i].field, fields[i].shouldExist);
    }

    if(stNew.checked) {
        chanGroup.removeAttribute("hidden");
    }
    else {
        chanGroup.setAttribute("hidden", true);
    }
}

var fields = [
    {
        field: channel,
        shouldExist: false
    },
    {
        field: username,
        shouldExist: true
    }
];

for(var i = 0; i < fields.length; ++i) {
    fields[i].field.addEventListener("blur", validateFieldContent.bind(null, fields[i].field, fields[i].shouldExist));
    validateFieldContent(fields[i].field, fields[i].shouldExist);
}

stNew.addEventListener("change", stListener);
stCorrect.addEventListener("change", stListener);

@@ -2088,21 +2088,25 @@
 .tv/
-kraken/users/
+helix/users?login
 %22 + 
@@ -2159,36 +2159,35 @@
 %0A%0A    xhr.open(%22
-HEAD
+GET
 %22, url, true);%0A 
@@ -2259,70 +2259,29 @@
 xhr.
-setRequestHeader('Accept', 'application/vnd.twitchtv.v3+
+responseType = %22
 json
-')
+%22
 ;%0A  
@@ -2288,32 +2288,20 @@
   xhr.on
-readystatechange
+load
  = funct
@@ -2321,98 +2321,119 @@
     
-if(xhr.readyState === 2) %7B%0A            // Only reject if the status is 404 not found.%0A
+var body = xhr.response;%0A        if(typeof body === %22string%22) %7B%0A            body = JSON.parse(body);%0A    
     
+%7D%0A
     
@@ -2444,45 +2444,47 @@
 cbk(
-xhr.status !== 404);%0A        %7D%0A    %7D;
+!body.data %7C%7C !body.data.length);%0A    %7D
 %0A%0A  

upgrade to helix on client-side