var HTTP_OK = 200;
var HTTP_CREATED = 201;
var HTTP_ACCEPTED = 202;

var HTTP_NOT_MODIFIED = 304;

var HTTP_BAD_REQUEST = 400;
var HTTP_UNAUTHORIZED = 401;
var HTTP_FORBIDDEN = 403;
var HTTP_NOT_FOUND = 404;
var HTTP_TIMEOUT = 408;
var HTTP_VALIDATION_ERROR = 200;

var HTTP_INTERNAL_ERROR = 500;

// FIXME
// Remove for production
function addTestValues() {
    $('#firm_form input[name="name"]').val('ABC Law Firm');
    $('#firm_form input[name="phone1"]').val('408-555-6666');
    $('#firm_form input[name="fax"]').val('408-777-8888');
    $('#firm_form input[name="billingAddress1"]').val('123 Main St');
    $('#firm_form input[name="billingCity"]').val('New York');
    $('#firm_form input[name="billingState"]').val('NY');
    $('#firm_form input[name="billingZip"]').val('12345');
    $('#firm_form input[name="shippingAddress1"]').val('345 Broadway');
    $('#firm_form input[name="shippingCity"]').val('Walla Walla');
    $('#firm_form input[name="shippingState"]').val('WA');
    $('#firm_form input[name="shippingZip"]').val('97450');
    $('#firm_form input[name="firmSize"]').val('10-19');
    $('#firm_form input[name="practiceType"]').val('Type 4');
    $('#firm_form input[name="trainingDirector"]').val('Bruce Willis');
    
    $('#profile_form input[name="billingAddress1"]').val('345 Broadway');
    $('#profile_form input[name="billingCity"]').val('Walla Walla');
    $('#profile_form input[name="billingState"]').val('WA');
    $('#profile_form input[name="billingZip"]').val('97450');
    $('#profile_form input[name="shippingAddress1"]').val('345 Broadway');
    $('#profile_form input[name="shippingCity"]').val('Walla Walla');
    $('#profile_form input[name="shippingState"]').val('WA');
    $('#profile_form input[name="shippingZip"]').val('97450');
    $('#profile_form input[name="firstName"]').val('Jogn');
    $('#profile_form input[name="middleInitial"]').val('B');
    $('#profile_form input[name="lastName"]').val('Doe');
    $('#profile_form input[name="suffix"]').val('Jr.');
    $('#profile_form input[name="title"]').val('Director of Direction');
    //$('#profile_form input[name="email"]').val('test@test.com');
    $('#profile_form input[name="password"]').val('abc123');
    $('#profile_form input[name="password2"]').val('abc123');
    $('#profile_form input[name="phone"]').val('+1(303)413-0551');
    $('#profile_form input[name="phone2"]').val('7075556666');
    //$('#profile_form input[name="isAttendingClasses"]').val('Yes');
    $('#profile_form input[name="role"]').val('Doe');
    $('#profile_form input[name="badgeName"]').val('Doe');
    $('#profile_form input[name="requireAccessibility"]').val("0");
    $('#profile_form input[name="haveScholarship"]').val("0");
    $('#profile_form input[name="barId[]"]').val('1111111');
    $('#profile_form input[name="state[]"]').val('NY');
    $('#profile_form input[name="month[]"]').val('7');
    $('#profile_form input[name="year[]"]').val('1341');
}

function addBarRow() {
    var rowCells = $("#bar_row").html();
    var newRow = $('<div class="row bar_row"></div>').html(rowCells);
    newRow.find('input').each(function() {
        $(this).val('');
    });
    newRow.find('select').each(function() {
        $(this).attr('selectedIndex',0);
    });
    newRow.insertBefore("#bar_add");
}

function toggleIsAttendingDependentFields() {
    //console.log('wtf');
    $(".isAttendingDependent").slideToggle();
}

function tearDown() {
    $("#back").unbind('click');
    $("#continue").unbind('click');
    $("#login_submit").unbind('click');
    $(document).unbind('ajaxComplete');
    $("#content_reg_funnel").hide();
    $("#content_reg_form").hide();
    $("#firm_form").hide();
    $("#profile_form").hide();
    $("#user_type_box").hide();
    $('#error_container').html('');
    $('#response_message').html('');
}

function renderLogin() {
    tearDown();
    $('#login_form input[name="email"]').focus(function() {
        $(document).ajaxComplete(handleLoginComplete);
    });
    $("#login_submit").click(function() {
        $("#login_form").ajaxSubmit();
        return false;
    });
    $("#content_reg_funnel").show();
}

function renderPreFirmForm() {
    tearDown();
    $("#user_type_box").show();
    $("#content_reg_form").show();
    $("#continue").hide();
    $("#back").click(function() {
        renderLogin();
        return false;
    });
    $("#page_title").html("Create A New Account");
}

function renderFirmForm() {
    tearDown();
    $(document).ajaxComplete(handleFormComplete);
    $("#back").click(function() {
        renderPreFirmForm();
        return false;
    });
    $("#continue").show();
    $("#continue").click(function() {
        $("#firm_form").ajaxSubmit();
        return false;
    });
    var userType = $('input[name="userType"]').val();
    switch(userType) {
    case 'group':
        $('#instructions').html("(Firm info is <b>required</b> for group users)");
        break;
    case 'individual':
        $('#instructions').html("(Firm info is <b>optional</b> for individual users)");
        break;
    }

    $("#firm_form").show();
    $("#content_reg_form").show();
}

function renderProfileForm() {

    tearDown();
    $("#instructions").hide();
    $(document).ajaxComplete(handleFormComplete);
    $("#back").click(function() {
        renderFirmForm();
        return false;
    });
    $("#continue").click(function() {
        $("#profile_form").ajaxSubmit();
        return false;
    });
    var userType = $('input[name="userType"]').val();
    switch(userType) {
    case 'group':
        $(".isAttendingDependent").hide();
        $('input[name="isAttendingClasses"]').val('0');
        break;
    case 'individual':
        $(".isAttendingDependent").show();
        break;
    }
    $("[name='phone']").val($("[name='phone1']").val());
    $("#profile_form [name='billingAddress1']").val($("#firm_form [name='billingAddress1']").val());
    $("#profile_form [name='billingCity']").val($("#firm_form [name='billingCity']").val());
    $("#profile_form [name='billingState']").val($("#firm_form [name='billingState']").val());
    $("#profile_form [name='billingZip']").val($("#firm_form [name='billingZip']").val());
    $("#profile_form [name='shippingAddress1']").val($("#firm_form [name='shippingAddress1']").val());
    $("#profile_form [name='shippingCity']").val($("#firm_form [name='shippingCity']").val());
    $("#profile_form [name='shippingState']").val($("#firm_form [name='shippingState']").val());
    $("#profile_form [name='shippingZip']").val($("#firm_form [name='shippingZip']").val());
    $("#profile_form").show();
    $("#content_reg_form").show();
}

// Ajax Handlers

function handleLoginComplete(e, xhr, setting) {
    switch(xhr.status) {
        case HTTP_ACCEPTED :
            // Display a message to the user
            // Redirect them to referrer or profile page
            if(xhr.responseText == "/MyCart") {
                doPageLoad('/MyCart', false, true);
            } else {
                window.location.reload();
            }
            break;
        case HTTP_BAD_REQUEST :
            // The form info was invalid
            $("#error_container").html(xhr.responseText);
            break;
        case HTTP_UNAUTHORIZED :
            // The authorization failed
            $("#error_container").html(xhr.responseText);
            break;
        default : alert('error login');
    }
}

function handleFormComplete(e, xhr, setting) {
    switch(xhr.status) {
        case HTTP_ACCEPTED :
            $("#response_message").html(xhr.responseText);
            renderProfileForm();
            break;
        case HTTP_CREATED :
            if(xhr.responseText == "/MyCart") {
                doPageLoad('/MyCart', false, true);
            } else {
                window.location.reload();
            }
            break;
        case HTTP_VALIDATION_ERROR :
            $('#forms_container > #error_container').html(xhr.responseText);
            $('#forms_container > #error_container').show();
            break;
        default : alert('error forms');
    }
}

function handleShippingAddress(el) {
    if(el.is(":checked")) {
        $("[name='shippingAddress1']").val($("[name='billingAddress1']").val());
        $("[name='shippingCity']").val($("[name='billingCity']").val());
        $("[name='shippingState']").val($("[name='billingState']").val());
        $("[name='shippingZip']").val($("[name='billingZip']").val());
    } else {
        $("[name='shippingAddress1']").val('');
        $("[name='shippingCity']").val('');
        $("[name='shippingState']").val('');
        $("[name='shippingZip']").val('');
    }
}

function handlePickupMaterials(el) {
    if(el.is(":checked")) {
        $("#firmShippingSameAsBilling").hide();
        $("#firmShipping").hide();
        $("[name='shippingAddress1']").val('');
        $("[name='shippingCity']").val('');
        $("[name='shippingState']").val('');
        $("[name='shippingZip']").val('');
    } else {
        $("#firmShippingSameAsBilling").show();
        $("#firmShipping").show();
    }
}

