﻿// JScript File
var PageIsValid = false;

function atleastoneyear()
{
    var isValid = false;

    var years = document.getElementById('yr').getElementsByTagName('input');

    for (i = 0; i < years.length; i++)
    {
        if (years[i].checked)
            isValid = true;
    }
    if (isValid)
    {
        document.getElementById("vldYear").style.display = 'none';
    }
    else
    {
        document.getElementById("vldYear").style.display = 'inline';
    }
} //done


//may not be in use
function caSelect(source, arguments)
{
    var isValid = true;

    var control = document.getElementById(source.controltovalidate);


    var theForm = control.form;

    for (i = control.options.selectedIndex + 1; i < control.options.length; i++)
        if (control.options[i].selected)
        isValid = false;
    arguments.IsValid = isValid || theForm.col[6].checked || theForm.row[6].checked;
}

function caSelect2(source, arguments)
{
    var isValid = true;
    var control = document.getElementById(source.controltovalidate);

    //alert(control.form);
    var theForm = control.form;
    for (i = control.options.selectedIndex + 1; i < control.options.length; i++)
        if (control.options[i].selected)
        isValid = false;
    arguments.IsValid = isValid || theForm.col[8].checked || theForm.row[8].checked;
}

function maxCols(theControl, theLabel)
{
    var control = document.getElementById(theControl);

    var label = document.getElementById(theLabel);


    if (control == null || label == null)
        return;
    var columnCount = 1;

    var theForm = control.form;

    for (i = control.options.selectedIndex + 1; i < control.options.length; i++)
    {
        if (control.options[i].selected)
            columnCount++;
    }
    var gte8 = (columnCount >= 8);
    var colSelected = (control.id == "ca" ? theForm.col[5].checked : theForm.col[4].checked);
    var isValid = !(gte8 && colSelected);
    if (isValid)
    {
        label.style.display = 'none';
    }
    else
    {
        label.style.display = 'inline';
    }
}

//theForm: document.getElementById('frmId')
function ResetForm(theForm)
{
    atleastoneyear();
    maxCols("rg", "vldRgMaxCols");
    maxCols("ca", "vldCaMaxCols");
    DisableConfidenceIntervals(theForm, document.getElementById("geo"));
    ResetGeoType(theForm);

}

//theForm: document.getElementById('frmId')
function ResetGeoType(theForm)
{

    if (theForm.id == 'frmPopulation')
        return;
    var levels = document.getElementById('level').getElementsByTagName('input');


    if (levels[0].checked)
    {
        var geoOptions = document.getElementById("geo").getElementsByTagName('input');
        var geoLabels = document.getElementById("geo").getElementsByTagName('label');

        if (document.getElementById("ci") == null)
            return;
        var ciOptions = document.getElementById("ci").getElementsByTagName('input');

        for (j = 0; j < geoOptions.length - 1; ++j)
        {
            if ((geoLabels[j].innerHTML.toUpperCase()).indexOf("RATES") != -1)
            {
                for (i = 0; i < ciOptions.length; i++)
                {
                    ciOptions[i].disabled = true;
                }

                geoOptions[j].disabled = true;
            }
        }
    }
}

function selectStep3InMap()
{
    
    var levels = document.getElementById('level').getElementsByTagName('input');
    var step5Options = document.getElementById('geo').getElementsByTagName('input');

    if (levels[0].checked == true)
    {

        for (var i = 0; i < step5Options.length; i++)
        {
            if (i == 0)
                step5Options[i].checked = true;
            else
            {
                step5Options[i].checked = false;
                step5Options[i].disabled = true;
                
            }
        }
    }
    if (levels[1].checked == true)
    {
        document.getElementById('geo').selectedValue = "-1";

        for (var i = 0; i < step5Options.length; i++)
        {

            step5Options[i].disabled = false;

        }
    }
}

function ResetControls(theForm)
{
    //alert('1');
    var control;

    //1. set default row variable
    control = document.getElementById("row_0");
    if (control != null)
        control.checked = true;

    //2. set default col variable
    control = document.getElementById("col_1");
    if (control != null)
        control.checked = true;

    //3. clear years (a) and select last year (b)
    var years = document.getElementById('yr').getElementsByTagName('input');

    for (i = 0; i < years.length; i++)
    {
        years[i].checked = false;
        if (i == years.length - 1)
            years[i].checked = true;
    }

    //4a. Select "All races"
    //    control = document.getElementById("rc");
    //    if (control != null)
    //        control.selectedIndex = 0;

    //4b. Select "All ages"
    control = document.getElementById("ag");
    if (control != null)
        control.selectedIndex = 0;

    control = document.getElementById("ma");
    if (control != null)
        control.selectedIndex = 0;

    control = document.getElementById("sx");
    if (control != null)
        control.selectedIndex = 0;

    control = document.getElementById("sortBy_0");
    if (control != null)
        control.checked = true;

    control = document.getElementById("rg");
    if (control != null)
        control.selectedIndex = 0;

    control = document.getElementById("sortCauseBy_0");
    if (control != null)
        control.checked = true;

    var control2 = document.getElementById("ca2");
    var control1 = document.getElementById("ca1");
    var control0 = document.getElementById("ca0");

    control = document.getElementById("ca");
    if (control2 != null)
        control = control2;
    else if (control1 != null)
        control = control1;
    else if (control0 != null)
        control = control0;

    if (control != null)
        control.selectedIndex = 0;

    control = document.getElementById("geo_0");
    if (control != null)
        control.checked = true;
    control = document.getElementById("ci_0");
    if (control != null)
        control.checked = true;
    control = document.getElementById("br_0");
    if (control != null)
        control.checked = true;
    control = document.getElementById("color_0");
    if (control != null)
        control.checked = true;

    selectAllCategoriesInStep4();
}

//Fires when an Output preference is selected
//Add by Mark Massy 8/14/2009 - Replaces DisableConfidenceIntervals_old
function DisableConfidenceIntervals(theForm, radiobutton)
{

    if (theForm.id == 'frmPopulation')
        return;

    var selectedstr = "";

    var rbOptions;
    var rbLabels;

    if (radiobutton != null)
    {
        rbOptions = radiobutton.getElementsByTagName('input');
        rbLabels = radiobutton.getElementsByTagName('label');

        for (i = 0; i < rbOptions.length; i++)
        {
            if (rbOptions[i].checked == true)
            {
                selectedstr = rbLabels[i].innerHTML;
            }
        }
    }
    else
    {
        var geoOptions = document.getElementById('geo').getElementsByTagName('input');
        var geoLabels = document.getElementById('geo').getElementsByTagName('label');

        for (i = 0; i < geoOptions.length; i++)
        {
            if (((geoOptions[i].innerHTML).toUpperCase()).indexOf("RATE") != -1)
            {
                if (geoOptions[i].checked)
                    selectedstr = geoLabels[i].innerHTML;
            }
        }

    }

    if (document.getElementById('ci') == null)
        return;
    var ciOptions = document.getElementById('ci').getElementsByTagName('input');

    if ((selectedstr.toUpperCase()).indexOf('RATE') != -1)
    {
        for (i = 0; i < ciOptions.length; i++)
        {
            if (ciOptions[i] != null)
                ciOptions[i].disabled = false;
        }
    }
    else
    {
        ciOptions[0].checked = true;

        for (i = 0; i < ciOptions.length; i++)
        {
            if (ciOptions[i] != null)
                ciOptions[i].disabled = true;
        }
    }
}


function setImage(action, control, image)
{
    switch (action)
    {
        case 'out':
            control.src = '../images/' + image + '_up.gif';
            break;
        case 'over':
            if (currentControl == control.id)
                control.src = '../images/' + image + '_down.gif';
            break;
        case 'down':
            currentControl = control.id;
            control.src = '../images/' + image + '_down.gif';
            break;
        case 'up':
            currentControl = '';
            control.src = '../images/' + image + '_up.gif';
            break;
        default:
            currentControl = '';
            break;
    };
}

function atleastone()
{
    args = atleastone.arguments;

    var displayerror = false;

    for (i = 0; i < args.length - 2; i++)
    {
        control = document.getElementById(args[i]);

        if (control != null)
        {
            if (control.selectedIndex == -1)
            {
                displayerror = true;
            }
        }
    }
    if (displayerror)
    {
        document.getElementById(args[args.length - 2]).innerHTML = args[args.length - 1];
        document.getElementById(args[args.length - 2]).style.display = 'inline';
    }
    else
    {
        document.getElementById(args[args.length - 2]).style.display = 'none';
    }
}

//********** Added by Amar/US Computing **************
var MaxSiteValue = 15;
var Column = -1;
var Row = -1;
var Output = -1;


function setRowVariable()
{
    //modified by Mark Massy 8/11/2009
    var i = 0;

    var rowVariables = document.getElementById('row').getElementsByTagName('input');

    for (i = 0; i < rowVariables.length; ++i)
    {
        if (rowVariables[i].checked == true)
        {
            Row = i;
            break;
        }
    }
}

function setColumnVariable()
{
    //modified by Mark Massy 8/11/2009
    var i = 0;

    var colVariables = document.getElementById('col').getElementsByTagName('input');

    for (i = 0; i < colVariables.length; ++i)
    {
        if (colVariables[i].checked == true)
        {
            Column = i;
            break;
        }
    }
}

/*Added by Amit; 3/5/2008*/
function IsGroupHeadingSelected(objSelect)
{
    for (var i = 0; i < objSelect.length; i++)
    {
        if ((objSelect[i].selected))
        {
            if (objSelect[i].value == "grpheading")
            {
                objSelect[0].selected = true;
                event.returnValue = false;
            }
        }
    }
}

/* returns 
-1 (failure) if "All" category is selected and selected options is more than 1
1 (success) if only "All" Category is selected or others are selected
*/
function AreSelectedOptionsValid(objSelect)
{
    var i = 0;
    var AllSelected = -1;

    for (var intLoop = 0; intLoop < objSelect.length; intLoop++)
    {
        if ((objSelect[intLoop].selected))
        {
            if (objSelect[intLoop].value == "grpheading")
            {
                return -1;
            }

            if (intLoop == 0)
                AllSelected = 1;
            ++i;
        }
    }

    if ((AllSelected == 1 && i > 1) || (AllSelected == -1 && i == 0))
        return -1;
    else
        return 1;
}

//for row and column variables, makes sure that no. of variables of interest are less than 15
function validateStep4ChartOptions(source, arguments)
{
    var i;
    var RetVal = -1;
    var rowVariables = document.getElementById('row').getElementsByTagName('input');
    var colVariables = document.getElementById('col').getElementsByTagName('input');

    var args;

    //if Birth
    if (document.getElementById('frmBirth'))//(document.forms[0].id == "frmBirth")
    {
        args = new Array("rc", "eth", "ag", "ma", "bw", "ga");
    } // if Population
    else if (document.getElementById("frmPopulation"))
    {
        args = new Array("rc", "eth", "sx", "ag");
    } //if Death
    else if (document.getElementById("frmDeath"))
    {
        args = new Array("rc", "eth", "sx", "ag");
    } //if Death2
    else if (document.getElementById("frmDeath2"))
    {
        args = new Array("rc", "eth", "sx", "ag");
    }

    //set the Output Value first
    var options = document.getElementById('output').getElementsByTagName('input');
    for (i = 0; i < options.length; ++i)
    {
        if (options[i].checked == true)
        {
            Output = i;
            break;
        }
    }

    for (i = 0; i < args.length; ++i)
    {
        control = document.getElementById(args[i]);

        if (control != null && typeof (control) != "undefined")
        {   //validate selections for trendline and bar chart
            //make sure that not more than 15 items are selected when variable is row or column
            var NumSelectedItems = getNumSelectedOptions(control.getElementsByTagName('option'));

            if (args[i] == "rc")
            {
                if ((rowVariables[Row].value == "race" || colVariables[Column].value == "race")
		                && (Output == 1 || Output == 2 || Output == 3)
		                 && NumSelectedItems > 15)
                {
                    arguments.IsValid = false;
                    return;
                }
            }
            else if (args[i] == "eth")
            {
                if ((rowVariables[Row].value == "ethnicity" || colVariables[Column].value == "ethnicity")
		                && (Output == 1 || Output == 2 || Output == 3)
		                 && NumSelectedItems > 15)
                {
                    arguments.IsValid = false;
                    return;
                }
            }
            else if (args[i] == "ag")
            {
                //alert(args[i] + ' ' + rowVariables[Row].value + ' ' + colVariables[Column].value + ' ' + NumSelectedItems + ' ' + Output)

                if ((rowVariables[Row].value == "age" || colVariables[Column].value == "age")
		             && (Output == 1 || Output == 2 || Output == 3)
		             && NumSelectedItems > 15)
                {
                    arguments.IsValid = false;
                    return;
                }
            }
            else if (args[i] == "ma")
            {
                if ((rowVariables[Row].value == "status" || colVariables[Column].value == "status")
		                && (Output == 1 || Output == 2 || Output == 3)
		                 && NumSelectedItems > 15)
                {
                    arguments.IsValid = false;
                    return;
                }
            }
            else if (args[i] == "bw")
            {
                if ((rowVariables[Row].value == "birthweight" || colVariables[Column].value == "birthweight")
		                && (Output == 1 || Output == 2 || Output == 3)
		                 && NumSelectedItems > 15)
                {
                    arguments.IsValid = false;
                    return;
                }
            }
            else if (args[i] == "ga")
            {
                if ((rowVariables[Row].value == "gestage" || colVariables[Column].value == "gestage")
		                && (Output == 1 || Output == 2 || Output == 3)
		                 && NumSelectedItems > 15)
                {
                    arguments.IsValid = false;
                    return;
                }
            }
            else if (args[i] == "sx")
            {
                if ((rowVariables[Row].value == "sex" || colVariables[Column].value == "sex")
		                && (Output == 1 || Output == 2 || Output == 3)
		                 && NumSelectedItems > 15)
                {
                    arguments.IsValid = false;
                    return;
                }
            }


        }

    }
}

/* Re-sets the selected options if required*/
// if user selects over-arching items like All Sex and Male Items when that variable is not row or column
// the following code unselects male and selects  "All"
function validateStep4Selections()
{
    var i;
    var RetVal = -1;
    var rowVariables = document.getElementById('row').getElementsByTagName('input');
    var colVariables = document.getElementById('col').getElementsByTagName('input');

    args = validateStep4Selections.arguments;

    for (i = 0; i < args.length; ++i)
    {
        control = document.getElementById(args[i]);

        if (control != null && typeof (control) != "undefined")
        {
            if (args[i] == "rc" && rowVariables[Row].value != "race" && colVariables[Column].value != "race" && AreSelectedOptionsValid(control) == -1)
                control.selectedIndex = 0;
            else if (args[i] == "eth" && rowVariables[Row].value != "ethnicity" && colVariables[Column].value != "ethnicity" && AreSelectedOptionsValid(control) == -1)
                control.selectedIndex = 0;
//            else if (args[i] == "eth" && rowVariables[Row].value == "ethnicity" && colVariables[Column].value == "ethnicity" && AreSelectedOptionsValid(control) == -1)
//            {
//                var NumSelectedItems = getNumSelectedOptions(control.getElementsByTagName('option'));
//                if(control[0].selected == false)
//                    control[0].selected = true; 
//            }
            else if (args[i] == "ag" && rowVariables[Row].value != "age" && colVariables[Column].value != "age" && AreSelectedOptionsValid(control) == -1)
                control.selectedIndex = 0;
            else if (args[i] == "ma" && rowVariables[Row].value != "status" && colVariables[Column].value != "status" && AreSelectedOptionsValid(control) == -1)
                control.selectedIndex = 0;
            else if (args[i] == "bw" && rowVariables[Row].value != "birthweight" && colVariables[Column].value != "birthweight" && AreSelectedOptionsValid(control) == -1)
                control.selectedIndex = 0;
            else if (args[i] == "ga" && rowVariables[Row].value != "gestage" && colVariables[Column].value != "gestage" && AreSelectedOptionsValid(control) == -1)
                control.selectedIndex = 0;
            else if (args[i] == "sx" && rowVariables[Row].value != "sex" && colVariables[Column].value != "sex" && AreSelectedOptionsValid(control) == -1)
                control.selectedIndex = 0;                
        }       
        
    }

    return RetVal;
}


/* validates the selections in Step Four of Tables*/
function validateStep4(source, arguments)
{
    var i = 0;

    if (Row == -1)
        setRowVariable();

    if (Column == -1)
        setColumnVariable();    
        
    //if Birth
    if (document.getElementById('frmBirth'))//(document.forms[0].id == "frmBirth")
    {
        validateStep4Selections("rc", "eth", "ag", "ma", "bw", "ga");
    } // if Population
    else if (document.getElementById("frmPopulation"))
    {
        validateStep4Selections("rc", "eth", "sx", "ag");        
    } //if Death
    else if (document.getElementById("frmDeath"))
    {
        validateStep4Selections("rc", "eth", "sx", "ag");
    } //if Death2
    else if (document.getElementById("frmDeath2"))
    {
        validateStep4Selections("rc", "eth", "sx", "ag");
    }       
    
}

/* Re-sets the selected options if required*/
function validateMapStep2Selections()
{
    var i;
    var RetVal = -1;

    args = validateMapStep2Selections.arguments;

    for (i = 0; i < args.length; ++i)
    {
        control = document.getElementById(args[i]);

        if (control != null && typeof (control) != "undefined")
        {
            if (AreSelectedOptionsValid(control) == -1)
                control.selectedIndex = 0;
        }
    }

    return RetVal;
}

/* validates the selections in Step Two of Maps*/
function validateMapStep2(source, arguments)
{
    if (document.getElementById('frmBirth'))
    {
        validateMapStep2Selections("rc", "eth", "ag", "ma", "bw", "ga");
    }
    else if (document.getElementById("frmPopulation"))
    {
        validateMapStep2Selections("rc", "eth", "sx", "ag");
    }
    else if (document.getElementById("frmDeath"))
    {
        validateMapStep2Selections("rc", "eth", "sx", "ag");
    }

}

// Added by Amit and Amar Apr 25 2006
// Checks if the user selected Map with "All Births" Rates
// prompts user that they are not available
function validateMapStep5(source, arguments)
{
    var rateSelected = false;
    control = (document.getElementById("geo").getElementsByTagName('input'))[1];
    if (control != null && control.checked == true)
        rateSelected = true;

    if (document.getElementById("frmBirth"))
    {
        if (document.getElementById('ca').options[0].selected == true
			&& rateSelected == true)
        {
            //alert("Rates are not available for All Births. Please select another birth characteristic and try again.");
            arguments.IsValid = false;
            return;
        }
    }

}



//
function validateOutput(source, arguments)
{
    var options = document.getElementById('output').getElementsByTagName('input');
    var i;
    for (i = 0; i < options.length; ++i)
    {
        if (options[i].checked == true)
        {
            Output = i;
            break;
        }
    }

    var geoOptions = document.getElementById('geo').getElementsByTagName('input');
    if (Output == 1 || Output == 2 || Output == 3)
    {
        if (geoOptions[0].checked == true)
        {
            arguments.IsValid = false;
            return;
        }
    }
}



function validateYear(source, arguments)
{
    var i;
    var numYears;
    var YearsArray;
    var j;

    var colVariables = document.getElementById('col').getElementsByTagName('input');

    if (Output == 1 || Output == 3)	// Trend-line is selected
    {
        if (colVariables[0].checked == false)
        {
            arguments.IsValid = false;
            return;
        }

        var yrs = document.getElementById('yr').getElementsByTagName('input');
        var yearCount = 0;
        for (i = 0; i < yrs.length; i++)
        {
            if (yrs[i].checked == true)
                yearCount++;

        }
        if (yearCount <= 1)
        {
            arguments.IsValid = false;
            return;
        }

        for (i = 0; i < yrs.length; i++)
        {
            if (yrs[i].checked == true)
            {
                for (var j = 1; j < yearCount; j++)
                {

                    if (yrs[i + j].checked == false)
                    {
                        arguments.IsValid = false;
                        return;
                    }
                }
                break;
            }

        }

    }
}



function validateRegion(source, arguments)
{
    var i = 0;
    var rowVariables = document.getElementById('row').getElementsByTagName('input');
    var colVariables = document.getElementById('col').getElementsByTagName('input');

    var NumRegions = getNumSelectedOptions(document.getElementById('rg').getElementsByTagName('option'));

    if (Row == -1)
        setRowVariable();

    if (Column == -1)
        setColumnVariable();

    if ((rowVariables[Row].value == "region"
		|| colVariables[Column].value == "region")
		&& (Output == 1 || Output == 2 || Output == 3)
		 && NumRegions > 15)
    {
        arguments.IsValid = false;
        return;
    }
}


function validateRegion_old(source, arguments)
{
    var i = 0;
    var rowVariables = document.getElementById('row').getElementsByTagName('input');
    var colVariables = document.getElementById('col').getElementsByTagName('input');
    var NumRegions = getNumSelectedOptions(document.forms[0].rg);

    if (Row == -1)
        setRowVariable();

    if (Column == -1)
        setColumnVariable();


    if ((rowVariables[Row].value == "region"
		|| document.forms[0].col[Column].value == "region")
		&& (Output == 1 || Output == 2 || Output == 3)
		 && NumRegions > 15)
    {
        arguments.IsValid = false;
        return;
    }
}

function validateCharacteristic(source, arguments)
{
    var i = 0;
    var rowVariables = document.getElementById('row').getElementsByTagName('input');
    var colVariables = document.getElementById('col').getElementsByTagName('input');
    var objChar = document.getElementById('ca').getElementsByTagName('option');

    for (var intLoop = 0; intLoop < objChar.length; intLoop++)
    {
        if ((objChar[intLoop].selected) && objChar[intLoop].value == "grpheading")
        {
            objChar[intLoop].selected = false;
        }
    }


    var NumChars = getNumSelectedOptions(document.getElementById('ca').getElementsByTagName('option'));

    if (NumChars == 0)
        objChar[0].selected = true;

    if (rowVariables[Row].value == "characteristic"
			|| colVariables[Column].value == "characteristic"
			&& objChar[0].selected == false)
    //&& document.forms[0].id="frmBirth") //Amit; 5/18/2006; No need of this check 
    //as this function is only being used in the 'Birth' module.
    {
        objChar[0].selected = true;
    }

    if (Row == -1)
        setRowVariable();

    if (Column == -1)
        setColumnVariable();

    if ((rowVariables[Row].value == "characteristic"
		|| colVariables[Column].value == "characteristic")
		&& (Output == 1 || Output == 2 || Output == 3)
		 && NumChars > 15)
    {
        arguments.IsValid = false;
        return;
    }

}

//Added by Amit; 08/16/2007
//If the user selects a group, this method clears that selection and selects 'All Births' instead.
function validateGroupedCharacteristic(source, arguments)
{
    var caList = document.getElementById('ca');

    for (var intLoop = 0; intLoop < caList.options.length; ++intLoop)
    {
        if ((caList.options[intLoop].selected) && caList.options[intLoop].value == "grpheading")
        {
            caList.options[0].selected = true;
        }
    }
}

function validateCharacteristic2(source, arguments)
{
    var i = 0;

    if (Row == -1)
        setRowVariable();

    if (Column == -1)
        setColumnVariable();

    var NumChars0 = getNumSelectedOptions2('ca0');
    var NumChars1 = getNumSelectedOptions2('ca1');
    var NumChars2 = getNumSelectedOptions2('ca2');

    var rowVariables = document.getElementById('row').getElementsByTagName('input');
    var colVariables = document.getElementById('col').getElementsByTagName('input');

    var ca0 = document.getElementById('ca0');
    /* Added By Amar/ Amit; 4/26/2006
    When the Row or Column Variable is 'Causes Of Death', and the characteristic selected
    is other than 'All Causes', then 'All Causes' should also get selected.
    */
    if (rowVariables[Row].value == "characteristic"
			|| colVariables[Column].value == "characteristic"
			&& ca0.options[0].selected == false)
    {
        ca0.options[0].selected = true;
    }

    if ((rowVariables[Row].value == "characteristic"
          || colVariables[Column].value == "characteristic")
         && (Output == 1 || Output == 2 || Output == 3)
           && (NumChars0 > 15 || NumChars1 > 15 || NumChars2 > 15))
    {
        arguments.IsValid = false;
        return;
    }

}

function getNumSelectedOptions(objSelect)
{
    var i = 0;

    for (var intLoop = 0; intLoop < objSelect.length; intLoop++)
    {
        if ((objSelect[intLoop].selected))
        {
            ++i;
        }
    }
    return i;
}

function getNumSelectedOptions2()
{
    var i = 0;

    args = getNumSelectedOptions2.arguments;

    control = document.getElementById(args[0]);

    if (control != null && typeof (control) != "undefined")
    {
        for (var intLoop = 0; intLoop < control.length; intLoop++)
        {
            if ((control[intLoop].selected))
            {
                ++i;
            }
        }
    }
    return i;
}

function checkAllYearsSelection()
{
    var isValid = false;

    var years = document.getElementById('yr').getElementsByTagName('input');

    for (i = 0; i < years.length; i++)
    {
        if (document.getElementById("allyr").checked == true)
        {
            years[i].checked = true;
            isValid = true;
        }
        else
        {
            years[i].checked = false;
            isValid = false;
        }
    }
    if (isValid)
    {
        document.getElementById("vldYear").style.display = 'none';
    }
    else
    {
        document.getElementById("vldYear").style.display = 'inline';
    }
}



var CharsOnlyOld;
var CharsBoth;
var CharsOnlyNew;

var CharsOnlyOld2;
var CharsBoth2;
var CharsOnlyNew2;

var bYear2003OrPreviousSelected = false;
var bYear2004OrLaterSelected = false;

//Adds an item to list box
//Added By Amit; 5/4/2006
function addOption2(object, text, value)
{
    var defaultSelected = false;
    var selected = false;
    var optionName = new Option(text, value, defaultSelected, selected)
    object.options[object.length] = optionName;

}

function SetCa()
{
    objSortByGroup = document.getElementById("sortCaByGroupName");
    objSortByDesc = document.getElementById("sortCaByDescription");
    if (objSortByGroup.checked == true)
    {
        SetCaOrderedByGroupName();
    }
    else if (objSortByDesc.checked == true)
    {
        SetCaOrderedByDescription();
    }
}

//***This function was added by Amit; 5/1/2006
function SetCaOrderedByGroupName()
{
    var CharsOnlyOldRow;
    var CharsBothRow;
    var CharsOnlyNewRow;
    var objElement = document.getElementById("ca");

    window.status = "Please wait while the characteristics listbox is filled...";

    //First remove all the rows from the Characteristics drop-down list
    for (var j = objElement.options.length - 1; j > -1; j--)
    {
        objElement.options[j] = null;
    }

    CharsOnlyOld = unescape(CharsOnlyOld);
    CharsBoth = unescape(CharsBoth);
    CharsOnlyNew = unescape(CharsOnlyNew);

    CharsOnlyOldRow = CharsOnlyOld.split("|");
    CharsBothRow = CharsBoth.split("|");
    CharsOnlyNewRow = CharsOnlyNew.split("|");

    //Get all the selected years
    GetSelectedYears();

    if (bYear2004OrLaterSelected == true)
    {
        if (bYear2003OrPreviousSelected == true)
        {
            FillListBox(objElement, CharsBothRow);
        }
        else
        {
            FillListBox(objElement, CharsOnlyNewRow);
        }
    }
    else if (bYear2003OrPreviousSelected == true)
    {
        FillListBox(objElement, CharsOnlyOldRow);
    }

    //Ask Amar if this should be here, or not
    if (document.getElementById('ca').options[0] != null)
    {
        document.getElementById('ca').options[0].selected = true;
    }

    window.status = "Done";

}

//***This function was added by Amit; 5/4/2006
function SetCaOrderedByDescription()
{
    var CharsOnlyOldRow2;
    var CharsBothRow2;
    var CharsOnlyNewRow2;
    var objElement = document.getElementById("ca");

    window.status = "Please wait while the characteristics listbox is filled...";

    //First remove all the rows from the Characteristics drop-down list
    for (var j = objElement.options.length - 1; j > -1; j--)
    {
        objElement.options[j] = null;
    }

    CharsOnlyOld2 = unescape(CharsOnlyOld2);
    CharsBoth2 = unescape(CharsBoth2);
    CharsOnlyNew2 = unescape(CharsOnlyNew2);

    CharsOnlyOldRow2 = CharsOnlyOld2.split("|");
    CharsBothRow2 = CharsBoth2.split("|");
    CharsOnlyNewRow2 = CharsOnlyNew2.split("|");

    //Get all the selected years
    GetSelectedYears();

    if (bYear2004OrLaterSelected == true)
    {
        if (bYear2003OrPreviousSelected == true)
        {
            FillListBox2(objElement, CharsBothRow2);
        }
        else
        {
            FillListBox2(objElement, CharsOnlyNewRow2);
        }
    }
    else if (bYear2003OrPreviousSelected == true)
    {
        FillListBox2(objElement, CharsOnlyOldRow2);
    }

    //Ask Amar if this should be here, or not
    if (document.getElementById('ca').options[0] != null)
    {
        document.getElementById('ca').options[0].selected = true;
    }

    window.status = "Done";

}

//This function was added by Amit; 5/1/2006
function FillListBox(objElement, objArrayList)
{
    var objArrayListItem;
    var lastGroupName = "";
    var currGroupName = "";

    for (var i = 0; i < objArrayList.length; i++)
    {
        if (objArrayList[i] != null && objArrayList[i] != "")
        {
            objArrayListItem = objArrayList[i].split("?");

            currGroupName = objArrayListItem[02];

            if (currGroupName != null && currGroupName != "")
                currGroupName = currGroupName;

            if (lastGroupName != null && lastGroupName != "")
                lastGroupName = lastGroupName;

            if (currGroupName != lastGroupName && currGroupName != "")
            {
                lastGroupName = currGroupName;
                addOption2(objElement, objArrayListItem[02], "grpheading");
            }

            if (currGroupName != null && currGroupName != "")
            {
                addOption2(objElement, "..." + objArrayListItem[00], objArrayListItem[01]);
            }
            else
            {
                addOption2(objElement, objArrayListItem[00], objArrayListItem[01]);
            }
        }
    }
}

//***This function was added by Amit; 5/4/2006    
function FillListBox2(objElement, objArrayList)
{
    var objArrayListItem;

    for (var i = 0; i < objArrayList.length; i++)
    {
        if (objArrayList[i] != null && objArrayList[i] != "")
        {
            objArrayListItem = objArrayList[i].split("?");
            addOption2(objElement, objArrayListItem[00], objArrayListItem[01]);
        }
    }
}

/*
Removes all items from an object.
Added By Amit; 5/1/2006
*/
function removeAll(fromObject)
{
    if (fromObject.options.length == 0)
    {
        return;
    }
    for (var i = fromObject.options.length - 1; i > -1; i--)
    {
        deleteOption(fromObject, i);
    }
}

/*
Gets the selected items from the 'Years' checkbox control.
Added By Amit; 5/1/2006 
*/
function GetSelectedYears()
{

    var maxx = (document.getElementById("yr").innerHTML); //M.M  5-13-2008
    var max = 0;
    bYear2003OrPreviousSelected = false;
    bYear2004OrLaterSelected = false;

    while (true)
    {
        var obj = eval("document.getElementById('yr_' + max)");
        if (obj == null)
        {
            break;

        }
        max++;
    }
    for (i = 0; i < max; i++)
    {
        if (eval("document.getElementById('yr_' + i)").checked == true)
        {
            if (parseInt(eval("document.getElementById('yr_' + i)").id.substring(3, 5)) <= 13)
            {
                bYear2003OrPreviousSelected = true;
            }
            else
            {
                bYear2004OrLaterSelected = true;
            }
        }
    }
}
function selectAllCategoriesInStep4()
{
    // if Previously selected Row/Col variable is not
    // Year, Region, Cause of Death then Unselect everything except "All" category

    var rowVariables = document.getElementById('row').getElementsByTagName('input');
    var colVariables = document.getElementById('col').getElementsByTagName('input');

    if (document.getElementById("frmBirth"))
    {
        if (Row != -1 && Column != -1)
        {
            if (rowVariables[Row].value == "race" || colVariables[Column].value == "race")
                UnSelectOptions("rc");
            if (rowVariables[Row].value == "ethnicity" || colVariables[Column].value == "ethnicity")
                UnSelectOptions("eth");
            if (rowVariables[Row].value == "age" || colVariables[Column].value == "age")
                UnSelectOptions("ag");
            if (rowVariables[Row].value == "status" || colVariables[Column].value == "status")
                UnSelectOptions("ma");
            if (rowVariables[Row].value == "birthweight" || colVariables[Column].value == "birthweight")
                UnSelectOptions("bw");
            if (rowVariables[Row].value == "gestage" || colVariables[Column].value == "gestage")
                UnSelectOptions("ga");
        }

        setRowVariable();
        setColumnVariable();

        //select all categories available for current row/col variable
        if (rowVariables[Row].value == "race" || colVariables[Column].value == "race")
            SelectOptions("rc");
        if (rowVariables[Row].value == "ethnicity" || colVariables[Column].value == "ethnicity")
            SelectOptions("eth");
        if (rowVariables[Row].value == "age" || colVariables[Column].value == "age")
            SelectOptions("ag");
        if (rowVariables[Row].value == "status" || colVariables[Column].value == "status")
            SelectOptions("ma");
        if (rowVariables[Row].value == "birthweight" || colVariables[Column].value == "birthweight")
            SelectOptions("bw");
        if (rowVariables[Row].value == "gestage" || colVariables[Column].value == "gestage")
            SelectOptions("ga");
    }
    else if (document.getElementById("frmPopulation") || document.getElementById("frmDeath") || document.getElementById("frmDeath2"))
    {
        if (Row != -1 && Column != -1)
        {
            if (rowVariables[Row].value == "race" || colVariables[Column].value == "race")
                UnSelectOptions("rc");
            if (rowVariables[Row].value == "ethnicity" || colVariables[Column].value == "ethnicity")
                UnSelectOptions("eth");
            if (rowVariables[Row].value == "sex" || colVariables[Column].value == "sex")
                UnSelectOptions("sx");
            if (rowVariables[Row].value == "age" || colVariables[Column].value == "age")
                UnSelectOptions("ag");
        }

        setRowVariable();
        setColumnVariable();

        //select all categories available for current row/col variable
        if (rowVariables[Row].value == "race" || colVariables[Column].value == "race")
            SelectOptions("rc");
        if (rowVariables[Row].value == "ethnicity" || colVariables[Column].value == "ethnicity")
            SelectOptions("eth");
        if (rowVariables[Row].value == "sex" || colVariables[Column].value == "sex")
            SelectOptions("sx");
        if (rowVariables[Row].value == "age" || colVariables[Column].value == "age")
            SelectOptions("ag");



    }
}


function UnSelectOptions(ObjId)
{
    var control = document.getElementById(ObjId);

    if (control != null && typeof (control) != "undefined")
    {
        for (var i = 0; i < control.length; ++i)
            control[i].selected = false;

        //select default option All
        control.selectedIndex = 0;
    }
}


//Replaces _old 
function SelectOptions(ObjId)
{
    control = document.getElementById(ObjId);

    if (control != null && typeof (control) != "undefined")
    {
        for (var i = control.options.length; i > 0; --i)
            control[i - 1].selected = true;
    }
}


	
