function set_batch_size(batch_json, set_gallons){
    var mw = 0;
    var mw_total = 0;
    var mg = 0;
    var mg_total = 0;
    var mc = 0;
    var mc_total = 0;
    var voc_total = 0;
    var gallons = 0;
    var grams = 0;
    var batch = batch_json.size
    var unit = batch_json.unit
    
    switch (unit) {
        case 'pint':
            grams = formulation.density * batch / 8 * 454;
            break;
        case 'quart':
            grams = formulation.density * batch / 4 * 454;
            break;
        case 'gallon':
            grams = formulation.density * batch * 454;
            break;
        case '5gallon':
            grams = formulation.density * batch * 5 * 454;
            break;
        default: //grams
            grams = batch;
            break;
    }
    
    gallons = (grams / 454) / formulation.density;
    
    //get mixing weights
    for (i = 0; i < lines.length; i++) {
        mw = lines[i].weight / formulation.weight * grams;
        mw_total = mw_total + mw;
        mw = mw.toFixed(2);
        jq("td span[id^='mw']:eq(" + i + ")").html(mw);
        
        mg = lines[i].weight / formulation.weight * gallons;
        mg_total = mg_total + mg;
        mg = mg.toFixed(2);
        if (set_gallons != undefined && set_gallons == 1) {
            jq("td span[id^='mg']:eq(" + i + ")").html(mg);
            voc_total = voc_total + (mg * lines[i].voc);
        }
        
        mc = mg * lines[i].price / 100;
        mc_total = mc_total + mc;
        mc = mc.toFixed(2);
        jq("td span[id^='mc']:eq(" + i + ")").html(mc);
    }
    
    mw_total = mw_total.toFixed(0);
    jq("td span#mw_total").html(mw_total + 'g');
    if (set_gallons != undefined && set_gallons == 1) {
        mg_total = mg_total.toFixed(2);
        jq("td span#mg_total").html(mg_total);
        jq("td span#voc_pounds").html((voc_total * 0.00220462262).toFixed(2));
        jq("td span#voc_grams").html(voc_total.toFixed(1));
    }
    mc_total = mc_total.toFixed(2);
    jq("td span#mc_total").html('$ ' + mc_total);
}

function calc_custom_batch_size(density){
    mesh_factor = jq("#calculator_mesh_factor").val();
    print_area = jq("#calculator_print_area").val();
    coverage = jq("#calculator_coverage").val();
    prints = jq("#calculator_prints").val();
    batch_size = 0.00000432827 * mesh_factor * print_area * (coverage / 100) * prints * density * 454;
    jq("#calculator_ink_required").val(batch_size.toFixed(0));
}

function setup_batch_size_links(){
    var links_data = [{
        "size": 500,
        "unit": "gram",
        "text": ""
    }, {
        "size": 1000,
        "unit": "gram",
        "text": ""
    }, {
        "size": 4000,
        "unit": "gram",
        "text": ""
    }, {
        "size": 20000,
        "unit": "gram",
        "text": ""
    }, {
        "size": 1,
        "unit": "quart",
        "text": ""
    }, {
        "size": 1,
        "unit": "gallon",
        "text": ""
    }, {
        "size": 5,
        "unit": "gallon",
        "text": ""
    }];
    
    for (i in links_data) {
        links_data[i].text = links_data[i].size + "<br />" + links_data[i].unit + "<br />batch";
    }
    
    jq("#batch a.batchlink").each(function(i, o){
        jq(o).bind('click', function(){
            set_batch_size(links_data[i]);
            return false
        }).html(links_data[i].text);
    })
    
    jq("#batch a.apply").bind('click', function(){
        set_batch_size({
            "size": jq("#custom_grams").val(),
            "unit": "gram"
        });
        return false
    }).html("Custom");
    
    if (batch.size != 1000) {
        jq("#custom_grams").val(batch.size);
    }
}

function mixing_report(report_type){
    batch = jq("#mw_total").html();
    batch = batch.substring(0, batch.length - 1)
    window.open('/ultramix/mixing_' + report_type + '?batch=' + batch);
}

function cycle_label(label) {
    switch (label.className) {
        case 'container':
            label.className = 'container2'
            break;
        case 'container2':
            label.className = 'container3'
            break;
        case 'container3':
            label.className = 'container4'
            break;
        case 'container4':
            label.className = 'container5'
            break;
        case 'container5':
            label.className = 'container6'
            break;
        default:
            label.className = 'container'
            break;
    }
}
