var animationSpeed = 600;
var results = new Array();
var boxWidth = 401;
var notify = true;

function getRandomNumber(min, max) {
    return Math.round((max - min) * Math.random() + min);
}

function sendResults() {
    var n1 = getRandomNumber(100, 999);
    var n2 = getRandomNumber(100, 999);
    var n3 = getRandomNumber(100, 999);
    var reference = n1 + " " + n2 + " " + n3;

    var data = "";
    for(var i = 0; i < results.length; i++) {
        data = data + results[i][0] + "\n- " + results[i][1].join("\n- ") + "\n\n";
    }

    var afid = 0;
    var url = window.location.toString();
    url.match(/\?(.+)$/);
    var params = (RegExp.$1).split("&");

    for(var i = 0; i < params.length; i++) {
        var parts = params[i].split("=");

        if(parts.length == 2 && parts[0] == "afid") {
            afid = unescape(parts[1]);
        }
    }

    $("#reference").val(reference);
    $("#results").val(data);
    $("#afid").val(afid);

    $("form").submit();
}

function selectAssociatedInput() {
    var parent = $(this).parent();
    var checkBox = parent.find("input[type=checkbox]:first");
    var radioButton = parent.find("input[type=radio]:first");

    if(checkBox.size() > 0) {
        checkBox.attr("checked", true);
    }
    else if(radioButton.size() > 0) {
        radioButton.attr("checked", true);
    }
}

$(document).ready(function()
{
    var i = 0;
    var boxes = $(".part").each(function() {
        $(this).css("left", (i * boxWidth) + "px");
        $(this).css("visibility", "visible");
        
        if(i > 0) {
            $(this).append('<a href="javascript:;" class="back">Back</a>');
        }
        
        i = i + 1;
    });

    $("input[type=text]").focus(selectAssociatedInput);
    $("textarea").focus(selectAssociatedInput);

    $("label").click(function() {
        var input = $(this).parent().find("input[type!=text]");

        if(input) {
            if(input.attr("checked")) {
                input.attr("checked", false);
            }
            else {
                input.attr("checked", true);

                if(input.attr("type") == "radio") {
                    input.click();
                }
            }
        }
    });

    $("input[type=radio]").click(function() {
        if($(this).attr("checked")) {
            var button = $(this).parents(".part").find(".button");
            if(button) {
                notify = false;
                button.click();
            }
        }
    });

    $("a.button").click(function() {
        var parent = $(this).parent();
        var question = parent.find("span:first").text();
        var details =  parent.hasClass("details");
        var answers = new Array();
        var notified = false;
        var count = 0;
        var valid = 0;

        if(details) {
            var errors = "";

            parent.find("input[type=text]").each(function() {
                var value = $(this).val();
                var label = $(this).parent().find("label");
                count = count + 1;

                if(value.length <= 0) {
                    if(!$(this).hasClass("optional")) {
                        var label = $(this).parent().find("label").text();
                        errors = errors + "Please complete " + label + ".\n";
                        valid = 0;
                    }
                    else {
                        valid = valid + 1;
                    }
                }
                else {
                    valid = valid + 1;
                    answers.push((label.size() == 1 ? $(label).text() + ": " : "") + value);
                }
            });;

            if(errors.length > 0) {
                if(notify) {
                    alert(errors);
                }
                notified = true;
            }
        }
        else {
            parent.find("input[type!=text]").each(function() {
                var input = $(this);
                count = count + 1;

                if(input.attr("checked")) {
                    var textbox = input.parent().find("input[type=text]");
                    var textarea = input.parent().find("textarea");
                    var label = input.parent().find("label");

                    if(textbox.size() > 0) {
                        if(textbox.attr("value").length <= 0) {
                            if(notify) {
                                alert("Please complete the textbox.");
                            }
                            count = count - 1;
                            notified = true;
                            textbox.focus();
                        }
                        else {
                            answers.push((label ? $(label).text() + ": " : "") + textbox.attr("value"));
                        }
                    }
                    else if(textarea.size() > 0) {
                        if(textarea.attr("value").length <= 0) {
                            if(notify) {
                                alert("Please complete the textarea.");
                            }
                            count = count - 1;
                            notified = true;
                            textarea.focus();
                        }
                        else {
                            answers.push((label ? $(label).text() + ": " : "") + textarea.attr("value"));
                        }
                    }
                    else {
                        answers.push(label ? label.text() : "Unknown");
                    }
                }
                else {
                    count = count - 1;
                }
            });
            
            parent.find("textarea").each(function() {
                count = count + 1;
                var label = $(this).parent().find("label");
                
                if($(this).attr("value").length <= 0) {
                    if(!$(this).hasClass("optional")) {
                        if(notify) {
                            alert("Please complete the textarea.");
                        }
                        count = count - 1;
                        notified = true;
                        $(this).focus();
                    }
                }
                else {
                    answers.push((label ? $(label).text() + ": " : "") + $(this).attr("value"));
                }
            });
        }

        if(details) {
            if(count != valid) {
                count = 0;
            }
        }

        if(count > 0 && !notified) {
            results.push(new Array(question, answers));

            if($(this).hasClass("final")) {
                sendResults();
            }
            else {
                $(".part").animate({
                    left: "-=" + boxWidth + "px"
                }, animationSpeed);
            }
        }
        else if(!notified && notify) {
            alert("Please select an option.");
        }

        notify = true;
    });
    
    $("a.back").click(function() {
        $(".part").animate({
            left: "+=" + boxWidth + "px"
        }, animationSpeed);
    });
});