function trim(s) {
    var l=0; var r=s.length -1;
    while(l < s.length && s[l] == ' ')
    {	l++; }
    while(r > l && s[r] == ' ')
    {	r-=1;	}
    return s.substring(l, r+1);
}

function setValidStatus(name, status) {
    document.getElementById(name + "GoodIcon").style.display =
        (status == "good") ? "inline"
                           : "none";
    document.getElementById(name + "BadIcon").style.display =
        (status == "bad") ? "inline"
                          : "none";    
}

function checkStuID() {
    var stuId = document.getElementById("stuid");
    var val = stuId.value;
    var nextButton = document.getElementById("nextButton");
    nextButton.disabled = true;

    if (val.length == 0) {
        setValidStatus("stuid", "none");
        return;
    }

    if (val.length < 9) {
        setValidStatus("stuid", "bad");
        return;
    }

    var numCount = 0;
    for (var i = 0; i < val.length; i++) {
        var c = val.charAt(i);
        if (c == '-') continue;
        if (!(c >= '0' && c <= '9')) {
            setValidStatus("stuid", "bad");
            return;
        }
        numCount++;
        if (numCount > 9) {
            setValidStatus("stuid", "bad");
            return;
        }
    }
    if (numCount != 9) {
        setValidStatus("stuid", "bad");
        return;
    }

    setValidStatus("stuid", "good");
    nextButton.disabled = false;
}

function isNumeric(val) {
    for (var i = 0; i < val.length; i++) {
        var c = val.charAt(i);
        if (!(c >= '0' && c <= '9')) return false;
    }
    return true;
}

var defaultBdayText = "mm/dd/yyyy";
var bdayHasFocus = false;
function checkQuestions() {
    var allowNext = true;
    var nextButton = document.getElementById("nextButton");

    // check last four
    var val = document.getElementById("lastFour").value;
    if (val.length == 0) {
        setValidStatus("lastFour", "none");
        allowNext = false;
    } else if (val.length != 4 || !isNumeric(val)) {
        setValidStatus("lastFour", "bad");
        allowNext = false;
    } else {
        setValidStatus("lastFour", "good");
    }


    // check birthday    
    var bday = document.getElementById("bday");
    var bdayRegex = new RegExp("^[01]?\\d[/-]?[0-3]?\\d[/-]?(19|20)?\\d{2}$");
    val = bday.value;
    if (val.length == 0) {
        setValidStatus("bday", "none");
        allowNext = false;
        if (!bdayHasFocus) {
            bday.value = defaultBdayText;
            bday.style.color = "gray";
        }
    } else if (val == defaultBdayText) {
        setValidStatus("bday", "none");
        allowNext = false;
    } else if (!bdayRegex.test(val)) {
        setValidStatus("bday", "bad");
        allowNext = false;
    } else {
        setValidStatus("bday", "good");
    }

    // check security question
    val = document.getElementById("secAnswer").value;
    if (val.length == 0) {
        setValidStatus("secAnswer", "none");
        allowNext = false;
    } else {
        setValidStatus("secAnswer", "good");
    }

    nextButton.disabled = !allowNext;
}

function onBdayFocus() {
    bdayHasFocus = true;
    var bday = document.getElementById("bday");
    if (bday.value == defaultBdayText) {
        bday.value = "";
        bday.style.color = "black";
    }
}

function onBdayBlur() {
    bdayHasFocus = false;
    checkQuestions();
}

var passThroughCaptchaCheck = false;
function onCaptchaSubmit() {
    if (passThroughCaptchaCheck) return true;

    Asirra_CheckIfHuman(onAsirraComplete);

    return false;
}

function onAsirraComplete(isHuman) {
    if (!isHuman) {
        document.getElementById("wrongCats").style.display = "block";
        return;
    }

    passThroughCaptchaCheck = true;
    document.getElementById("captchaForm").submit();
}

function toggleHandicap() {
    text = document.getElementById("handicapText");
    if (text.style.visibility == "hidden") {
        text.style.visibility = "visible";
    } else if (text.style.visibility == "visible") {
        text.style.visibility = "hidden";
    } else {
        text.style.visibility = "visible";
    }
}