﻿var TotalCubic = 0;
var boxValues = new Object;
var ExtraOption = false;

function CollectCubicFeet(name, value)
{
    var InitialBox = document.getElementsByName(name);
    var ValueBox = document.getElementsByName(name + "_val");
    var Cubic = 0;
    
    InitialBox = InitialBox[0];
    ValueBox = ValueBox[0];
    
    value = parseInt(value);
    Cubic = parseInt(ValueBox.value);
    
    if(value == null)
    {
        value = 0;
    }
    
    if(value % 1 != 0)
    {
        InitialBox.style.backgroundColor = 'red';
        alert("Please enter a valid number");
        return;
    }
    else
    {
        InitialBox.style.backgroundColor = '#FFF';
    }
    
    if(value == null || isNaN(value))
    {
        value = 0        
    }
    
    value = value * Cubic;
    AddRemoveBoxValue(value, name);
}

function AddRemoveBoxValue(value, name)
{
    if(value > 0)
    {
        boxValues[name] = value;
    }
    else
    {
        if(boxValues[name])
        {
            delete boxValues[name];
        }
    }
    CalculateAdd();
}

function CalculateAdd()
{
    var value = 0;
    var EstimatorContainer = document.getElementById("EstimatorContainer");
    var SelcetedIndexvalue = 0;
    Estimator = EstimatorContainer.getElementsByTagName("select");
    Estimator = Estimator[0];
    var OptionLength = Estimator.options.length;
    
    for(ii in boxValues)
    {
        value = value + parseInt(boxValues[ii]);
    }    
    
    //Estimator    
    if(value > 0)
    {
        
        if(ExtraOption == false)
        {
            Estimator.options[OptionLength] = new Option("Custom Cubic Feet - "+ value, value);
            SelcetedIndexvalue = OptionLength;
        }
        else
        {
            Estimator.options[OptionLength -1] = new Option("Custom Cubic Feet - "+ value, value);
            SelcetedIndexvalue = OptionLength - 1;
        }
        var valholder = document.getElementById("ctl00_ContentArea_EstimatorCustomVal");
        valholder.value = value;
        
        Estimator.selectedIndex = SelcetedIndexvalue;
        
        ExtraOption = true;
    }
    else
    {
        Estimator.remove(OptionLength - 1) = null;
        ExtraOption = false;
    }
}
