// MAIN FUNCTIONS

if (document.all) type = "IE";
if (document.layers) type = "NN";
if (!document.all && document.getElementById) type = "MO";


function goNav(val) {

    document.formdata.screen.value = val;
    if (val == 1) {
        document.formdata.action = "index.asp";
    }
    if (val == 2) {
        document.formdata.action = "selectcolumns.asp";
    }
    if (val == 3) {
        document.formdata.action = "refine.asp";
    }
    if (val == 4) {
        document.formdata.action = "output.asp";
    }
    if (val == 5) {
        document.formdata.action = "sortorder.asp";
    }
    if (val == 6) {
        document.formdata.action = "requested.asp";
    }
    if (val == 7) {
        document.formdata.action = "versions.asp";
    }
    if (val == 8) {
        document.formdata.action = "aboutdata.asp";
    }
    if (val == 9) {
        document.formdata.action = "help.asp";
    }
    if (val == 10) {
        document.formdata.action = "save.asp";
    }
    document.formdata.submit()
}

function goToHelp(val) {
    thisURL = "help.asp" + val;
    glossary = window.open(thisURL, "Help", "scrollbars=yes,top=0,left=0,screenX=0,screenY=0,width=660,height=560");

    //    strURL = "help.asp" + val;
    //    document.formdata.screen.value = "9";
    //    document.formdata.action = strURL;
    //    document.formdata.submit()
}


function ChangeContent(id, str) {

    if (type == "IE") {
        document.all[id].innerHTML = str;
    }
    if (type == "NN") {
        document.layers[id].document.open();
        document.layers[id].document.write(str);
        document.layers[id].document.close();
    }
    if (type == "MO" || type == "OP") {
        document.getElementById(id).innerHTML = str;
    }
}


function ChangeFilterContent(id, filID, str) {

    str = "<table class='userFilterList'><tr valign='top'><td nowrap><img src='images/update.gif' alt='Update This Filter' title='Update This Filter' onClick=\"EditFilter('" + filID + "');criteria.className='dispOn'\" style='cursor:pointer;' /> <img src='images/delete.gif' onClick=\"DropFilter('" + filID + "');result1.className='dispOn';criteria.className='dispOff'\" style='cursor:pointer;'  alt='Delete This Filter' title='Delete This Filter' /></td><td width='425'> " + str + "</td></tr></table>";

    if (type == "IE") {
        document.all[id].innerHTML = document.all[id].innerHTML + str;
    }
    if (type == "NN") {
        document.layers[id].document.open();
        document.layers[id].document.write(str);
        document.layers[id].document.close();
    }
    if (type == "MO" || type == "OP") {
        document.getElementById(id).innerHTML = document.getElementById(id).innerHTML + str;
    }
}


function SwitchStyle(id, disp) {
    if (type == "IE") {
        document.all[id].className = disp;
    }
    if (type == "NN") {
        document.layers[id].className = disp;
    }
    if (type == "MO" || type == "OP") {
        document.getElementById(id).className = disp;
    }
}
//-----------------------------------------------------------------------------------------------------
//these function applies to IE only, Iframe is used to hide controls behind a DIV containing innerHTML
function showIframe(id) {
    if (type == "IE") {
        document.all[id].style.visibility = "visible";
        document.all[id].style.height = document.all['criteria'].style.height;
    }
}


function hideIframe(id) {

    if (type == "IE") {
        document.all[id].style.visibility = "hidden";
    }
}
//-----------------------------------------------------------------------------------------------------

function ShowHelpDef(e) {

    var selectedValue = document.formdata.selLevel.options[document.formdata.selLevel.selectedIndex].value;
    if (selectedValue == "State") { stringText = document.formdata.helpState.value; }
    if (selectedValue == "County") { stringText = document.formdata.helpCounty.value; }
    if (selectedValue == "District") { stringText = document.formdata.helpDistrict.value; }
    if (selectedValue == "School") { stringText = document.formdata.helpSchool.value; }
    if (selectedValue == "CBSA") { stringText = document.formdata.helpCBSA.value; }
    if (selectedValue == "MSA") { stringText = document.formdata.helpMSA.value; }
    var arrText = stringText.split("|");
    var str = "<table cellspacing=0 cellpadding=2 border=0><tr><td align='center' class='helpexpheader'><font class='helpexpheadertext'>" + arrText[0] + "</font></td></tr><tr><td class='helpexpbody'><font class='helpexpbodytext'>" + arrText[1] + "</font></td></tr></table>";
    var scrollY = document.body.scrollTop;
    var scrollX = document.body.scrollLeft;

    if (type == "IE") {
        document.all['helpLevel'].innerHTML = "";
        document.all['helpLevel'].innerHTML = str;
        document.all['helpLevel'].style.visibility = "visible";
        //xhmtl reguires units (TG August 2008)
        document.all['helpLevel'].style.left = (e.clientX) + 15;
        document.all['helpLevel'].style.top = (e.clientY + scrollY);
    }
    if (type == "NN") {
        document.helplevel.document.open();
        document.helplevel.document.write(str);
        document.helplevel.document.close();
        document.helplevel.visibility = "visible";
    }
    if (type == "MO" || type == "OP") {
        document.getElementById('helpLevel').innerHTML = "";
        document.getElementById('helpLevel').innerHTML = str;
        document.getElementById('helpLevel').style.visibility = "visible";
        //xhmtl reguires units (TG August 2008)
        document.getElementById('helpLevel').style.left = (e.clientX) + 15 + 'px';
        document.getElementById('helpLevel').style.top = (e.clientY + scrollY) + 'px';
    }
    //check for popup proportion issues
    if (document.getElementById('helpLevel').offsetWidth < 300 && document.getElementById('helpLevel').offsetHeight > 300) {
        document.getElementById('helpLevel').style.width = '350px';
    }
    if (document.getElementById('helpLevel').offsetWidth < 200 && document.getElementById('helpLevel').offsetHeight > 200) {
        document.getElementById('helpLevel').style.width = '200px';
    }
    //move window if off bottom of screen - Adam Tarmy 1/1/07
    var helpY = e.clientY;
    var helpHeight = document.getElementById('helpLevel').offsetHeight;
    var windowHeight = window.innerHeight || document.body.offsetHeight;
    //if bottom scrollbar exists in mozilla
    var scrollXHeight = 0
    if (type == 'MO' && document.body.offsetWidth < document.body.scrollWidth) {
        scrollXHeight = 15
    }
    if (helpY + helpHeight > windowHeight) {
        document.getElementById('helpLevel').style.top = (scrollY + windowHeight - helpHeight - scrollXHeight) + 'px';
    }
}

// Added ShowHelpDef2 for popup help for the BAT 2 year labeling convention (YYYY-YY) CR 19 on 2/13/08 (DTa)
function ShowHelpDef2(e) {
    stringText = document.formdata.helpYear.value;
    var arrText = stringText.split("|");
    var str = "<table cellspacing=0 cellpadding=2 border=0><tr><td align='center' class='helpexpheader2'><font class='helpexpheadertext'>" + arrText[0] + "</font></td></tr><tr><td class='helpexpbody2'><font class='helpexpbodytext'>" + arrText[1] + "</font></td></tr></table>";
    var scrollY = document.body.scrollTop;
    var scrollX = document.body.scrollLeft;

    if (type == "IE") {
        document.all['helpLevel'].innerHTML = "";
        document.all['helpLevel'].innerHTML = "<div style='padding-bottom:18em; '> " + str + "</div>";
        document.all['helpLevel'].style.visibility = "visible";
        document.all['helpLevel'].style.left = (e.clientX) + 55 + 'px';
        document.all['helpLevel'].style.top = (e.clientY + scrollY) + 'px';
    }
    if (type == "NN") {
        document.helplevel.document.open();
        document.helplevel.document.write(str);
        document.helplevel.document.close();
        document.helplevel.visibility = "visible";
    }
    if (type == "MO" || type == "OP") {
        document.getElementById('helpLevel').innerHTML = '';
        document.getElementById('helpLevel').innerHTML = str;
        document.getElementById('helpLevel').style.visibility = "visible";
        document.getElementById('helpLevel').style.left = e.clientX + 15 + 'px';
        document.getElementById('helpLevel').style.top = e.clientY + scrollY + 'px';
    }

    //check for popup proportion issues
    if (document.getElementById('helpLevel').offsetWidth < 300 && document.getElementById('helpLevel').offsetHeight > 300) {
        document.getElementById('helpLevel').style.width = '365px';
    }
    if (document.getElementById('helpLevel').offsetWidth < 200 && document.getElementById('helpLevel').offsetHeight > 200) {
        document.getElementById('helpLevel').style.width = '365px';
    }
    //move window if off bottom of screen - Adam Tarmy 1/1/07
    var helpY = e.clientY;
    var helpHeight = document.getElementById('helpLevel').offsetHeight;
    var windowHeight = window.innerHeight || document.body.offsetHeight;

    //if bottom scrollbar exists in mozilla
    var scrollXHeight = 0
    if (type == 'MO' && document.body.offsetWidth < document.body.scrollWidth) {
        scrollXHeight = 15
    }

    if (helpY + helpHeight > windowHeight) {
        document.getElementById('helpLevel').style.top = scrollY + windowHeight - helpHeight - scrollXHeight + 'px';
    }
}


function HideHelpDef() {
    if (type == "NN") {
        document.helpLevel.visibility = "hide";
    }
    else if (type == "IE") {
        document.all['helpLevel'].style.visibility = "hidden";
    }
    if (type == "MO" || type == "OP") {
        document.getElementById('helpLevel').style.visibility = "hidden";
    }
}

function showInfo(val) {
    thisText = "";
    if (val == "new") {
        thisText = "Create customized tables using data from multiple years collected from five CCD surveys and a Census Special Tabulation. <br />"
        thisText = thisText + "For more information on creating tables, click the <a href=\"javascript:goToHelp(\'\');\">Help</a> tab."
    }
    else if (val == "save") {
        thisText = "To open a saved table, enter its ID and click the Go button."
    }
    else if (val == "requested") {
        thisText = "See a list of the most requested table selections, by category."
    }
    else if (val == "requestedBuild") {
        thisText = "Your requested table’s settings for rows, years, columns and filters have been loaded into BAT.  The settings for each screen can be changed to your preferences. Click the Next button on each screen to continue.  Once the table has been viewed, you can adjust settings, download the results or save the table."
    }
    else if (val == "available") {
        thisText = "Review CCD and Census data available using the Build A Table tool."
    }
    else if (val == "help") {
        thisText = "Get help on a particular feature or function for the Build a Table tool."
    }
    else if (val == "aboutdata") {
        thisText = "See information on CCD data and other CCD data sources."
    }

    thisText = '<table cellpadding=\'4\' cellspacing=\'0\' border=\'0\'><tr><td>' + thisText + '</td></tr></table>'
    ChangeContent('undernav', thisText)
}

function isPosInteger(inputVal) {
    inputStr = inputVal.toString()
    for (var i = 0; i < inputStr.length; i++) {
        var oneChar = inputStr.charAt(i);
        if (oneChar < "0" || oneChar > "9") {
            return false;
        }
    }
    return true
}

function isTooLarge(inputVal) {
    if (inputVal > 999999) {
        return true;
    } else {
        return false;
    }
}

function showWarning(thisMode, thisVal) {

    window.document.formdata.screen.value = thisVal;

    if (thisMode == "RETRIEVE") {
        thisID = document.formdata.savedID.value;

        if (isPosInteger(thisID) == false) {
            alert('The saved table ID must all be positive integers.')
            return false;
        }

        if (isTooLarge(thisID) == true) {
            alert('The saved table ID is invalid. Please enter a valid number between 1 - 999999.')
            return false;
        }

        if (thisID == '') {
            alert('You must enter the Saved Table ID to use this feature.  Click the Create a New Table button to create a new table.')
            return false;
        }

        window.document.formdata.action = "output.asp?SAVEDID=" + thisID;
    }
    else {
        unchecked = "FALSE"
        for (i = 0; i < document.formdata.year.length; i++) {
            if (document.formdata.year[i].checked == true) {
                unchecked = "TRUE";
            }
        }
        if (unchecked == "FALSE") {
            alert("You must select at least one Survey Year to continue.")
            return;
        }
    }

    if (window.document.formdata.ID.value == "") {
        thisURL = "nces_warning.asp";
        warnWindow = window.open(thisURL, "warnWindow", "scrollbars=yes,top=0,left=0,screenX=0,screenY=0,width=500,height=560");
    }
    else {
        window.document.formdata.processed.value = "";
        window.document.formdata.submit()
    }


}

function showGlossary(SID, selCol) {
    thisURL = "glossary.asp?sid=" + SID + "&selCol=" + selCol;
    glossary = window.open(thisURL, "warnWindow", "scrollbars=yes,top=0,left=0,screenX=0,screenY=0,width=500,height=560");
}


// SELECT ROWS

function SetYr(object) {
    var selectedValue = object.options[object.selectedIndex].value;
    window.document.formdata.level.value = selectedValue;
    if (selectedValue == "State") { EnableYr() }
    if (selectedValue == "County") { EnableYr() }
    if (selectedValue == "District") { EnableYr() }
    if (selectedValue == "School") { EnableYr() }
    if (selectedValue == "CBSA") { DisableCBSAYr(2002) }
    if (selectedValue == "MSA") { DisableMSAYr(2002) }
}

function DisableCBSAYr(yr) {
    for (i = 0; i < document.formdata.year.length; i++) {
        if (document.formdata.year[i].value >= yr) {
            document.formdata.year[i].disabled = false;
            if (i == 0) {
                document.formdata.year[i].checked = true;
            }
        }
        else if (document.formdata.year[i].value < yr) {
            document.formdata.year[i].disabled = true;
            document.formdata.year[i].checked = false;
        }
    }
}

function DisableMSAYr(yr) {
    for (i = 0; i < document.formdata.year.length; i++) {
        if (document.formdata.year[i].value >= yr) {
            document.formdata.year[i].disabled = true;
            document.formdata.year[i].checked = false;
        }
        else if (document.formdata.year[i].value < yr) {
            document.formdata.year[i].disabled = false;
        }
        if (document.formdata.year[i].value == "2001") {
            document.formdata.year[i].checked = true;
        }
    }
}

function EnableYr() {
    for (j = 0; j < document.formdata.year.length; j++) {
        document.formdata.year[j].disabled = false;
        if (j == 0) {
            document.formdata.year[j].checked = true;
        }
    }
}

function uncheck(yr) {
    if (document.layers) {
        var rowval;
        rowval == '';
        for (i = 0; i < document.formdata.level.length; i++) {
            if (document.formdata.level[i].checked) {
                rowval = document.formdata.level[i].value;
            }
        }
        if ((rowval == 'CBSA') && (yr.value == 2002)) {
        }
        else if ((rowval == 'CBSA') && (yr.value < 2002)) {
            yr.checked = false;
        }
        if ((rowval == 'MSA') && (yr.value > 2001)) {
            yr.checked = false
        }
        else if ((rowval == 'MSA') && (yr.value <= 2001)) {
        }
    }
}

function setLink(action, path) {
    if (action == 'Tip') {
        window1 = window.open('help.asp?HelpFile=2', 'window1', 'screenX=0,screenY=0,top=0,left=0,resizable=1,width=420,height=300,scrollbars=yes,menubar=no');
    }
    if (action == 'CatVar') {
        window1 = window.open('help.asp?HelpFile=14', 'window1', 'screenX=0,screenY=0,top=0,left=0,resizable=1,width=420,height=300,scrollbars=yes,menubar=no');
    }
    if (action == 'Def') {
        newpath = path + document.formdata.selLevel.options[document.formdata.selLevel.selectedIndex].value;
        window1 = window.open(newpath, 'window1', 'screenX=0,screenY=0,top=0,left=0,resizable=1,width=420,height=300,scrollbars=yes,menubar=no');
    }
    if (action == 'data') {
        window1 = window.open(path, 'window2', 'screenX=0,screenY=0,top=0,left=0,resizable=1,width=580,height=400,scrollbars=yes,menubar=no');
    }
    if (action == 'version') {
        window1 = window.open(path, 'window2', 'screenX=0,screenY=0,top=0,left=0,resizable=1,width=420,height=300,scrollbars=yes,menubar=no');
    }
}



// SELECT COLUMNS 

function selectColumns() {
    // CR 198: Removed alert for category 59 (CK 11/20/2008) 
    document.formdata.action = "selectcolumns.asp"
    document.formdata.submit();
}

function reviewColumns() {
    document.formdata.action = "selectcolumns.asp?DISP=REVIEW"
    document.formdata.submit();
}

function AddDropColumn(object, col) {
    var colsels = document.formdata.colselections.value;
    if (object.checked) {
        var countVals = document.formdata.colselections.value;
        var theseValues = countVals.split("|");
        if (theseValues.length >= 51) {
            alert("You cannot select more than 50 columns for a table.")
            object.checked = false;
        }
        else {
            var newcolsels = colsels + col;
            document.formdata.colselections.value = newcolsels;
            getCount = document.formdata.TotalColCount.value;
            getCount = getCount * 1;
            currentCount = getCount + 1;
            document.formdata.TotalColCount.value = currentCount;
            ChangeContent('TotalColumnCount', currentCount)
        }
    }
    else {
        var newcolsels = colsels.replace(col, "");
        document.formdata.colselections.value = newcolsels;
        getCount = document.formdata.TotalColCount.value;
        getCount = getCount * 1;
        currentCount = getCount - 1;
        document.formdata.TotalColCount.value = currentCount;
        ChangeContent('TotalColumnCount', currentCount)
    }
}

function unCheckAllCols(mode) {
    var colsels = document.formdata.colselections.value;
    for (var i = 0; i < document.formdata.ColumnList.length; i++) {
        if (document.formdata.ColumnList[i].checked == true && document.formdata.ColumnList[i].disabled == false) {
            document.formdata.ColumnList[i].checked = false;
            thisVal = document.formdata.ColumnList[i].value + "|";
            var colsels = colsels.replace(thisVal, "");
        }
    }
    newcolsels = colsels;
    document.formdata.colselections.value = newcolsels;
    if (mode != "DISP") {
        goNav('2')
    }
    else {
        currentCount = 0;
        for (var i = 0; i < document.formdata.ColumnList.length; i++) {
            if (document.formdata.ColumnList[i].checked == true) {
                currentCount = currentCount + 1;
            }
        }
        document.formdata.TotalColCount.value = currentCount;
        ChangeContent('TotalColumnCount', currentCount)
    }
}

function selectRow(colRSN) {
    for (var i = 0; i < document.formdata.ColumnList.length; i++) {
        checkCol = document.formdata.ColumnList[i].value;
        if (checkCol.length > 5) {
            if (checkCol.substring(4, 8) == colRSN) {
                if (document.formdata.ColumnList[i].checked == true) {
                    document.formdata.ColumnList[i].checked = false;
                }
                else {
                    document.formdata.ColumnList[i].checked = true;
                }
                AddDropColumn(document.formdata.ColumnList[i], checkCol + "|")
            }
        }
    }
}


// SELECT FILTERS
function showCriteria(obj, filterType) {
    // Set the optional parameter if needed
    if (filterType === undefined) {
        filterType = 'unspecified';
    }

    if (type == "IE") {
        obj = document.all[obj];
        if (filterType == 'edit' || document.formdata.columnselector.options[document.formdata.columnselector.selectedIndex].value != 'NULL') {
            obj.className = 'dispOn';
            //CR 427: Do not show Iframe (CK 9/28/2009)
            //showIframe('shim');
        }
        else {
            obj.className = 'dispOff';
            //CR 427: Do not show Iframe (CK 9/28/2009)
            //hideIframe('shim')
        }
    }
    if (type == "NN") {
        obj = document.layers[obj];
        if (document.formdata.columnselector.options[document.formdata.columnselector.selectedIndex].value != 'NULL') {
            obj.setAttribute('class', 'dispOn');
            //CR 427: Do not show Iframe (CK 9/28/2009)
            //showIframe('shim');

        }
        else {
            obj.setAttribute('class', 'dispOff');
            //CR 427: Do not show Iframe (CK 9/28/2009)
            //hideIframe('shim')
        }
    }
    if (type == "MO" || type == "OP") {
        obj = document.getElementById(obj);
        if (filterType == 'edit' || document.formdata.columnselector.options[document.formdata.columnselector.selectedIndex].value != 'NULL') {
            obj.setAttribute('class', 'dispOn');
        }
        else {
            obj.setAttribute('class', 'dispOff');
        }
    }
    LeftPosition = (document.body.clientWidth) ? (document.body.clientWidth - 380) / 2 : 0;
    TopPosition = (document.body.clientHeight) ? (document.body.clientHeight - 200) / 2 : 0;
    if (document.documentElement) {
        theLeft = document.documentElement.scrollLeft;
        theTop = document.documentElement.scrollTop;
    }
    else if (document.body) {
        theLeft = document.body.clientLeft;
        theTop = document.body.clientTop;
    }
    theLeft += LeftPosition + 80;
    /* xhtml (top position no longer adds 80 px)*/
    theTop += TopPosition;
    obj.style.left = theLeft + 'px';
    obj.style.top = '170px';
}


function UnselectDefault() {
    for (var i = 0; i < document.formdata.stateselect.length; i++) {
        if (document.formdata.stateselect[i].checked == true) {
            document.formdata.stateselect[i].checked = false;
        }
    }
}

function checkEmpty(val) {
    if (val == "") {
        alert("You must enter at least one column value for filters.")
        return false;
    }
    return true;
}

function checkInput(dtype, val) {

    if (dtype != "Text") {
        for (var i = 0; i < val.length; i++) {
            var mydigit = val.charAt(i);
            if (mydigit < "0" || mydigit > "9") {
                if (mydigit != ".") {
                    alert("Please enter only numerical data.")
                    return false;
                }
            }
        }
        countDecimal = 0;
        for (var i = 0; i < val.length; i++) {
            var myDot = val.charAt(i);
            if (myDot == ".") {
                countDecimal = countDecimal + 1;
            }
        }
        if (countDecimal > 1) {
            alert("Please enter only one decimal point.")
            return false;
        }
        return true;
    }
    else {
        for (var i = 0; i < val.length; i++) {
            var mychar = val.charAt(i);
            if (mychar == "`" || mychar == "!" || mychar == "#" || mychar == "^" || mychar == "+" || mychar == "=" || mychar == "<" || mychar == ">" || mychar == "?" || mychar == "|") {
                alert("Please enter alphanumeric characters only. Do not use symbols.");
                return false;
            }
        }
        return true;
    }
}

function SelectStates(val) {
    if (val == 0) {
        for (var x = 0; x < document.formdata.statelist.options.length; x++) {
            document.formdata.statelist.options[x].selected = true
        }
    } else {
        for (var x = 0; x < document.formdata.statelist.options.length; x++) {
            if (document.formdata.statelist.options[x].value == 'MP' || document.formdata.statelist.options[x].value == 'AS' || document.formdata.statelist.options[x].value == 'BI' || document.formdata.statelist.options[x].value == 'DD' || document.formdata.statelist.options[x].value == 'DO' || document.formdata.statelist.options[x].value == 'PR' || document.formdata.statelist.options[x].value == 'GU' || document.formdata.statelist.options[x].value == 'VI' || document.formdata.statelist.options[x].value == '00') {
                document.formdata.statelist.options[x].selected = false;
            }
            else {
                document.formdata.statelist.options[x].selected = true
            }

        }
    }
}

function ConvertChar(str, dir) {
    if (dir == "code") {
        str = str.replace(/</g, "&lt;");
        str = str.replace(/>/g, "&gt;");
    }
    else {
        str = str.replace(/&lt;/g, "<");
        str = str.replace(/&gt;/g, ">");
    }
    return str;
}

function ClearFilterVals() {
    //    document.formdata.columnselector.selectedIndex = 0;
    if (document.formdata.selYears) {
        document.formdata.selYears.options.length = 1;
        document.formdata.selYears.options[0] = new Option("Any Years", "ANY", false, false);
        document.formdata.selYears.options[1] = new Option("All Years", "ALL", false, false);
        document.formdata.selYears.selectedIndex = 0;
    }
    if (document.formdata.firstCriteria) {
        document.formdata.firstCriteria.selectedIndex = 0;
    }
    document.formdata.firstCriteriaValue.value = "";

    //CR 427: Exclude secondCriteriaValue for categorical variables (CK 9/10/2009)
    if (document.formdata.firstCriteriaValue.type != "select-multiple") {
        document.formdata.joinCriteria.selectedIndex = 0;
        if (document.formdata.secondCriteria) {
            document.formdata.secondCriteria.selectedIndex = 0;
        }
        document.formdata.secondCriteriaValue.value = "";
    }

    document.formdata.datatype.value = "";
    document.formdata.filterID.value = "";
}

function DrawRegularFilter(columnName) {
    var stringFilter = "";
    stringFilter = "<table class='criteriaBox'>";
    stringFilter = stringFilter + "<tr><td colspan=2 align='center'><h2>Column Filter Criteria</h2></td></tr>";
    stringFilter = stringFilter + "<tr><td colspan=2 align='center'><strong>" + columnName + "</strong></td></tr>";
    stringFilter = stringFilter + "<tr><td width='35%' align='right'><strong>Select Years</strong></td>";
    stringFilter = stringFilter + "<td style='text-align:left'><select name='selYears' class='iesTextEntry' size=1 >";
    stringFilter = stringFilter + "<option value='ANY'>Any Years</option>";
    stringFilter = stringFilter + "<option value='ALL'>All Years</option>";
    stringFilter = stringFilter + "</select></td></tr>";
    stringFilter = stringFilter + "<tr><td align='right'><strong>Column</strong></td>";
    stringFilter = stringFilter + "<td style='text-align:left'><select name='firstCriteria' class='iesTextEntry' size=1 >";
    stringFilter = stringFilter + "</select> <input class='iesTextEntry' type=text value='' size=20 maxLength=50 name='firstCriteriaValue' ></td></tr>";
    stringFilter = stringFilter + "<tr><td align='right'><strong>Or/And *</strong></td>";
    stringFilter = stringFilter + "<td style='text-align:left'><select class='iesTextEntry' name='joinCriteria' size=1>";
    stringFilter = stringFilter + "<option value='OR' selected='selected'>or</option>";
    stringFilter = stringFilter + "<option value='AND'>and</option>";
    stringFilter = stringFilter + "</select></td></tr>";
    stringFilter = stringFilter + "<tr><td align='right'><strong>Column *</strong></td>";
    stringFilter = stringFilter + "<td style='text-align:left'><select name='secondCriteria' class='iesTextEntry' size=1 >";
    stringFilter = stringFilter + "</select> <input class='iesTextEntry' type=text value='' size=20 maxLength=50 name='secondCriteriaValue' ></td></tr>";
    stringFilter = stringFilter + "<tr><td colspan=2 align=center>*Complete only when second filter is desired</td></tr>";
    stringFilter = stringFilter + "<tr><td colspan=2 align='center'><input type=hidden value='' name='datatype' ><input type=hidden value='' name='filterID' ><input type='button' class='inputstyleButton' title='Save Filter' value='Save Filter' onclick=\"AddCriteria();\" > <input type='button' class='inputstyleButton' title='Cancel' value='Cancel' onclick=\"CancelCriteria();\" ></td></tr>";
    stringFilter = stringFilter + "</table>";
    ChangeContent('criteria', stringFilter)
}

function DrawDataTypeFilter(columnName) {
    var stringFilter = "";
    stringFilter = "<table class='criteriaBox'>";
    stringFilter = stringFilter + "<tr>";
    stringFilter = stringFilter + "<td colspan=2 align='center'><h2>Column Filter Criteria</h2></td></tr>";
    stringFilter = stringFilter + "<tr><td colspan=2 align='center'><strong>" + columnName + "</strong></td></tr>";
    stringFilter = stringFilter + "<tr><td width='35%' align='right'><strong>Select Years</strong></td>";
    stringFilter = stringFilter + "<td style='text-align:left'><select name='selYears' class='iesTextEntry' size=1 >";
    stringFilter = stringFilter + "<option value='ANY'>Any Years</option>";
    stringFilter = stringFilter + "<option value='ALL'>All Years</option>";
    stringFilter = stringFilter + "</select></td></tr>";
    stringFilter = stringFilter + "<tr>";
    stringFilter = stringFilter + "<td align='right'><strong>First Criteria</strong></td>";
    stringFilter = stringFilter + "<td style='text-align:left'><select name='firstCriteriaValue' class='iesTextEntry' size=1 >";
    stringFilter = stringFilter + "</select> </td></tr>";
    stringFilter = stringFilter + "<tr>";
    stringFilter = stringFilter + "<td align='right'><strong>Or/And *</strong></td>";
    stringFilter = stringFilter + "<td style='text-align:left'><select class='iesTextEntry' name='joinCriteria' size=1>";
    stringFilter = stringFilter + "<option value='OR' selected='selected'>or</option>";
    stringFilter = stringFilter + "<option value='AND'>and</option>";
    stringFilter = stringFilter + "</select></td></tr>";
    stringFilter = stringFilter + "<tr>";
    stringFilter = stringFilter + "<td align='right'><strong>Second Criteria *</strong></td>";
    stringFilter = stringFilter + "<td style='text-align:left'><select name='secondCriteriaValue' class='iesTextEntry' size=1 >";
    stringFilter = stringFilter + "</select> </td></tr>";
    stringFilter = stringFilter + "<tr>";
    stringFilter = stringFilter + "<td colspan=2 align=center>*Complete only when second filter is desired</td></tr>";
    stringFilter = stringFilter + "<tr>";
    stringFilter = stringFilter + "<td colspan=2 align='center'><input type=hidden value='' name='datatype' ><input type=hidden value='' name='filterID' ><input type='button' class='inputstyleButton' title='Save Filter' value='Save Filter' onclick=\"AddCriteria();\" > <input type='button' class='inputstyleButton' title='Cancel' value='Cancel' onclick=\"CancelCriteria();\" ></td></tr>";
    stringFilter = stringFilter + "</table>";
    ChangeContent('criteria', stringFilter);
}

//CR 427: Show list box instead of dropdown to select categorical variables (CK 9/4/2009)
function DrawCategoricalFilter(columnName) {
    var stringFilter = "";
    stringFilter = "<table class='criteriaBox'>";
    stringFilter = stringFilter + "<tr>";
    stringFilter = stringFilter + "<td colspan=2 align='center'><h2>Column Filter Criteria</h2></td></tr>";
    stringFilter = stringFilter + "<tr><td colspan=2 align='center'><strong>" + columnName + "</strong></td></tr>";
    stringFilter = stringFilter + "<tr><td width='35%' align='right'><strong>Select Years</strong></td>";
    stringFilter = stringFilter + "<td style='text-align:left'><select name='selYears' class='iesTextEntry' size=1 >";
    stringFilter = stringFilter + "<option value='ANY'>Any Years</option>";
    stringFilter = stringFilter + "<option value='ALL'>All Years</option>";
    stringFilter = stringFilter + "</select></td></tr>";
    stringFilter = stringFilter + "<tr>";
    stringFilter = stringFilter + "<td align='right'><strong>Criteria</strong></td>";
    stringFilter = stringFilter + "<td style='text-align:left'><select name='firstCriteriaValue' class='iesTextEntry' multiple='multiple'>";
    stringFilter = stringFilter + "</select> </td></tr>";
    stringFilter = stringFilter + "<tr><td colspan=2 align='center'><input type=hidden value='' name='datatype' ><input type=hidden value='' name='filterID' ><input type='button' class='inputstyleButton' title='Save Filter' value='Save Filter' onclick=\"AddCriteria();\" > <input type='button' class='inputstyleButton' title='Cancel' value='Cancel' onclick=\"CancelCriteria();\" ></td></tr>";
    stringFilter = stringFilter + "</table>";
    ChangeContent('criteria', stringFilter);
}

function showOper(datatype, selector) {
    if (datatype != "Text") {
        selector.options.length = 0;
        selector.options[0] = new Option("=", "=", false, false);
        selector.options[1] = new Option(">", ">", false, false);
        selector.options[2] = new Option(">=", ">=", false, false);
        selector.options[3] = new Option("<=", "<=", false, false);
        selector.options[4] = new Option("<", "<", false, false);
    }
    else {
        selector.options.length = 0;
        selector.options[0] = new Option("contains", "like", false, false);
        selector.options[1] = new Option("is equal to", "=", false, false);
        selector.options[2] = new Option("begins with", "beginswith", false, false);
        selector.options[3] = new Option("ends with", "endswith", false, false);
    }
}

function showFilter(vals) {

    if (vals != "NULL") {
        var theseValues = vals.split("|");
        var colval = theseValues[0];
        var coltype = theseValues[1];

        //Prevent users from creating another filter with the same variable (CK 9/29/2009)
        var colfils = document.formdata.colfilters.value;
        var arrFilters = colfils.split("^");
        var blnNewVar = true;
        
        for (var x = 0; x < arrFilters.length; x++) {
            thisString = arrFilters[x];
            if (thisString.length >= 4) {
                var arrSelections = thisString.split("|");
                thisFilter = arrSelections[1];
                thisFilter = thisFilter.substring(7, arrSelections[1].length);

                if (thisFilter == colval) {
                    blnNewVar = false;
                }
            }                
        }

        if (blnNewVar == false) {
            SwitchStyle('criteria', 'dispOff')
            hideIframe('shim')
            alert("There is already a filter for this column. Please select another column.");
            document.formdata.columnselector.selectedIndex = 0;

        }  
        //End              
        else {
            if (coltype == "Text" || coltype == "Numeric" || coltype == "One Decimal" || coltype == "Whole Number" || coltype == "Percentage" || coltype == "Phone Number" || coltype == "Year") {
                DrawRegularFilter(document.formdata.columnselector.options[document.formdata.columnselector.selectedIndex].text)
                document.formdata.datatype.value = coltype;
                var theseSelections = document.formdata.colselections.value;
                var columns = theseSelections.split("|");
                var holderArray = new Array();
                var holderCounter = 0;
                for (var x = 0; x < columns.length; x++) {
                    thisColumnValue = columns[x];
                    if (thisColumnValue.length > 5) {
                        //Defect 173: Search for column_rsn after the year (e.g., 20011140) (CK 02/11/2009)
                        //if (columns[x].search(colval) > -1) {
                        if (columns[x].substring(9, 4).search(colval) > -1) {
                            holderArray[holderCounter] = columns[x].substring(0, 4);
                            holderCounter = holderCounter + 1;
                        }
                    }
                }
                holderArray.sort()
                for (var x = 0; x < holderArray.length; x++) {
                    var newYearOption = holderArray[x];

                    // Begin changes for the BAT 2 year labeling convention (YYYY-YY) CR 19 on 2/12/08 (DTa)

                    //document.formdata.selYears.options[document.formdata.selYears.options.length] = new Option(newYearOption, newYearOption, false, false);
                    var newYearLabel = newYearOption + '-' + String(parseInt(newYearOption) + 1).substr(2, 2)
                    document.formdata.selYears.options[document.formdata.selYears.options.length] = new Option(newYearLabel, newYearOption, false, false);
                }

                // End changes for CR 19 on 2/12/08 (DTa)

                showOper(coltype, document.formdata.firstCriteria)
                showOper(coltype, document.formdata.secondCriteria)
                var largestID = 1;
                var colfils = document.formdata.colfilters.value;
                var arrFilters = colfils.split("^");
                for (var x = 0; x < arrFilters.length; x++) {
                    thisString = arrFilters[x];
                    if (thisString.length >= 4) {
                        var arrSelections = thisString.split("|");
                        thisID = arrSelections[0];
                        if (parseInt(thisID) >= largestID) {
                            largestID = parseInt(thisID);
                        }
                    }
                }
                var currCount = largestID + 1;
                document.formdata.filterID.value = currCount;
            }
            else {
                //CR 427: Show list box instead of dropdown for categorical variables (CK 9/4/2009)
                if (colval == 1530 || colval == 2772 || colval == 1171 || colval == 3085 || colval == 4461 || colval == 1170 || colval == 1156 || colval == 2866 || colval == 2702 || colval == 1100 || colval == 2756 || colval == 4460 || colval == 3200 || colval == 1091 || colval == 3095 || colval == 1092 || colval == 3094 || colval == 3204) {
                    DrawCategoricalFilter(document.formdata.columnselector.options[document.formdata.columnselector.selectedIndex].text)
                }
                //End
                else {
                    DrawDataTypeFilter(document.formdata.columnselector.options[document.formdata.columnselector.selectedIndex].text)
                    document.formdata.secondCriteriaValue.options[document.formdata.secondCriteriaValue.options.length] = new Option('', '', false, false);
                }

                document.formdata.datatype.value = coltype;
                LoadDataSelector(colval)

                //CR 427: Default to "ALL" for a_type and s_type (CK 9/16/2009)
                if (colval == 1530 || colval == 2772 || colval == 2756) {
                    var selObj = document.formdata.firstCriteriaValue;
                    var i;
                    for (i = 0; i < selObj.options.length; i++) {
                        if (selObj.options[i].selected == false) {
                            selObj.options[0].selected = true;
                        }
                    }
                }
                //End

                var largestID = 1;
                var colfils = document.formdata.colfilters.value;
                var arrFilters = colfils.split("^");
                for (var x = 0; x < arrFilters.length; x++) {
                    thisString = arrFilters[x];
                    if (thisString.length >= 4) {
                        var arrSelections = thisString.split("|");
                        thisID = arrSelections[0];
                        if (parseInt(thisID) >= largestID) {
                            largestID = parseInt(thisID);
                        }
                    }
                }
                var currCount = largestID + 1;
                document.formdata.filterID.value = currCount;
            }
        }
    }
}



function AddCriteria() {
    var checkData = "";
    var ThisDataType = document.formdata.datatype.value;
    var ThisFilterID = document.formdata.filterID.value;

    if (document.formdata.firstCriteriaValue.type == "text") {
        if (checkEmpty(document.formdata.firstCriteriaValue.value) == false) {
            checkData = "FAIL";
        }
        if (checkInput(ThisDataType, document.formdata.firstCriteriaValue.value) == false) {
            checkData = "FAIL";
        }
        if (document.formdata.secondCriteriaValue.value != "") {
            if (checkInput(ThisDataType, document.formdata.secondCriteriaValue.value) == false) {
                checkData = "FAIL";
            }
        }
    }

    //Prevent 'All' from being selected with another value for a_type and s_type (CK 9/29/2009)   
    var ColumnValues = document.formdata.columnselector.options[document.formdata.columnselector.selectedIndex].value;
    var theseValues = ColumnValues.split("|");
    var ThisColumn = theseValues[0];

    if (document.formdata.firstCriteriaValue.type == "select-multiple" && (ThisColumn == 1530 || ThisColumn == 2772 || ThisColumn == 2756)) {
        var arrValue = new Array();
        var arrText = new Array();
        var selObj = document.formdata.firstCriteriaValue;
        var i;
        var count = 0;
        for (i = 0; i < selObj.options.length; i++) {
            if (selObj.options[i].selected) {
                arrValue[count] = selObj.options[i].value;
                arrText[count] = selObj.options[i].text;
                count++;
                             
                if (i != 0 && selObj.options[0].selected && selObj.options[i].selected) {
                    alert("You cannot select '" + selObj.options[0].text + "' with another value.");
                    selObj.options[0].selected = false;
                    checkData = "FAIL";
                    return;
                }
            }
        }
    }            
    
    if (document.formdata.columnselector.selectedIndex == 0) {
        alert("Please select a column name before adding filter values.")
        checkData = "FAIL";
    }
    if (checkData != "FAIL") {
        DropFilter(ThisFilterID,1);
        var ColumnValues = document.formdata.columnselector.options[document.formdata.columnselector.selectedIndex].value;
        var ColumnName = document.formdata.columnselector.options[document.formdata.columnselector.selectedIndex].text;
        var theseValues = ColumnValues.split("|");
        var ThisColumn = "COLRSN=" + theseValues[0];
        if (document.formdata.selYears) {
            if (document.formdata.selYears.selectedIndex == 0) {
                var ThisYear = "[ANY YEARS]";
            }
            if (document.formdata.selYears.selectedIndex == 1) {
                var ThisYear = "[ALL YEARS]";
            }
            if (document.formdata.selYears.selectedIndex != 0 && document.formdata.selYears.selectedIndex != 1) {
                ThisYear = "[" + document.formdata.selYears.options[document.formdata.selYears.selectedIndex].value + "]";
            }
        }
        else {
            ThisYear = "[ALL YEARS]";
        }
        ThisFirstCriteria = "=";
        ShowFirstCriteria = "=";
        ThisFirstCriteriaText = "";
        ThisSecondCriteria = "=";
        ShowSecondCriteria = "=";
        ThisSecondCriteriaText = "";
        if (document.formdata.firstCriteria) {
            ThisFirstCriteria = document.formdata.firstCriteria.options[document.formdata.firstCriteria.selectedIndex].value;
            // (Begin CR# 158)Added 'begins with' and 'ends with' options in the Column Filter Criteria window (DTa 12/14/07)
            //ShowFirstCriteria = ThisFirstCriteria.replace("like", "contains");

            ShowFirstCriteria = ThisFirstCriteria.replace("like", "contains");
            if (ThisFirstCriteria == "like") {
                ShowFirstCriteria = ThisFirstCriteria.replace("like", "contains");
            }
            else if (ThisFirstCriteria == "beginswith") {
                ShowFirstCriteria = ThisFirstCriteria.replace("beginswith", "begins with");
            }
            else if (ThisFirstCriteria == "endswith") {
                ShowFirstCriteria = ThisFirstCriteria.replace("endswith", "ends with");
            }
            ThisSecondCriteria = document.formdata.secondCriteria.options[document.formdata.secondCriteria.selectedIndex].value;
            //ShowSecondCriteria = ThisSecondCriteria.replace("like", "contains");
            ShowSecondCriteria = ThisSecondCriteria.replace("like", "contains");
            if (ThisSecondCriteria == "like") {
                ShowSecondCriteria = ThisSecondCriteria.replace("like", "contains");
            }
            else if (ThisSecondCriteria == "beginswith") {
                ShowSecondCriteria = ThisSecondCriteria.replace("beginswith", "begins with");
            }
            else if (ThisSecondCriteria == "endswith") {
                ShowSecondCriteria = ThisSecondCriteria.replace("endswith", "ends with");
            }

            // (End CR# 158)
        }
                
        if (document.formdata.firstCriteriaValue.type == "text") {
            ThisFirstCriteriaValue = document.formdata.firstCriteriaValue.value;
            ThisFirstCriteriaText = document.formdata.firstCriteriaValue.value;
            ThisSecondCriteriaValue = document.formdata.secondCriteriaValue.value;
            ThisSecondCriteriaText = document.formdata.secondCriteriaValue.value;
        }
        //CR 427: Get multiple values for categorical variables (CK 9/4/2009)
        else if (document.formdata.firstCriteriaValue.type == "select-multiple") {
            var arrValue = new Array();
            var arrText = new Array();
            var selObj = document.formdata.firstCriteriaValue;
            var i;
            var count = 0;
            for (i = 0; i < selObj.options.length; i++) {
                if (selObj.options[i].selected) {
                    arrValue[count] = selObj.options[i].value;
                    arrText[count] = selObj.options[i].text;
                    count++;
                }
            }
            ThisFirstCriteriaValue = arrValue;
            ThisFirstCriteriaText = arrText;
        }
        //End
        else {
            ThisFirstCriteriaValue = document.formdata.firstCriteriaValue.options[document.formdata.firstCriteriaValue.selectedIndex].value;
            ThisFirstCriteriaText = document.formdata.firstCriteriaValue.options[document.formdata.firstCriteriaValue.selectedIndex].text;
            ThisSecondCriteriaValue = document.formdata.secondCriteriaValue.options[document.formdata.secondCriteriaValue.selectedIndex].value;
            ThisSecondCriteriaText = document.formdata.secondCriteriaValue.options[document.formdata.secondCriteriaValue.selectedIndex].text;
        }

        //CR 427: Get multiple values for categorical variables (CK 9/4/2009)
        if (document.formdata.firstCriteriaValue.type != "select-multiple") {
            ThisJoinCriteria = document.formdata.joinCriteria.options[document.formdata.joinCriteria.selectedIndex].value;
            if (ThisSecondCriteriaValue == "") {
                ThisJoinCriteria = "";
                ThisSecondCriteria = "";
                ShowSecondCriteria = "";
                ThisSecondCriteriaText = "";
            }
            else {
                if (ThisFirstCriteriaValue == ThisSecondCriteriaValue) {
                    alert("You cannot insert two identical filter values. Please make each filter value unique.");
                    checkData = "FAIL";
                }
            }
        }
        //End

        if (checkData != "FAIL") {
            //CR 427: Get multiple values for categorical variables (CK 9/4/2009)
            if (document.formdata.firstCriteriaValue.type == "select-multiple") {
                fil = (ThisFilterID + "|" + ThisColumn + "|" + ThisYear + "|" + ThisFirstCriteria + "|" + ThisFirstCriteriaValue + "|" + ThisDataType + "|");
                ThisCriteria = (ColumnName + " " + ThisYear + " " + ShowFirstCriteria + " " + ThisFirstCriteriaText);
            }
            else {
                fil = (ThisFilterID + "|" + ThisColumn + "|" + ThisYear + "|" + ThisFirstCriteria + "|" + ThisFirstCriteriaValue + "|" + ThisJoinCriteria + "|" + ThisSecondCriteria + "|" + ThisSecondCriteriaValue + "|" + ThisDataType + "|");
                ThisCriteria = (ColumnName + " " + ThisYear + " " + ShowFirstCriteria + " " + ThisFirstCriteriaText + " " + ThisJoinCriteria + " " + ShowSecondCriteria + " " + ThisSecondCriteriaText);
            }
            
            colfils = document.formdata.colfilters.value;
            newfils = colfils + fil + ThisCriteria + "^";
            newfils = ConvertChar(newfils, 'code')
            document.formdata.colfilters.value = newfils;
            document.formdata.filterCount.value = ThisFilterID;
            ChangeFilterContent('result1', ThisFilterID, ThisCriteria)
            ClearFilterVals()
            SwitchStyle('result1', 'dispOn')
            SwitchStyle('criteria', 'dispOff')
            hideIframe('shim')
            document.formdata.action = 'refine.asp';
            document.formdata.submit()
        }
        //        else {
        //            ClearFilterVals();
        //            DropFilter(ThisFilterID);
        //            showFilter(document.formdata.columnselector.options[document.formdata.columnselector.selectedIndex].value)            
        //        }
    }
    //    else {
    //        ClearFilterVals();
    //        DropFilter(ThisFilterID);
    //        showFilter(document.formdata.columnselector.options[document.formdata.columnselector.selectedIndex].value)                
    //    }
}

function CancelCriteria() {
    ClearFilterVals()
    document.formdata.columnselector.selectedIndex = 0;
    SwitchStyle('criteria', 'dispOff')
    hideIframe('shim')
}

function DropFilter(filID, refresh) {
    ChangeContent('result1', '')
    var colfils = document.formdata.colfilters.value;
    var colfils = ConvertChar(colfils, 'null')
    var arrFilters = colfils.split("^");
    var newformfils = "";
    for (var x = 0; x < arrFilters.length; x++) {
        thisString = arrFilters[x];
        if (thisString.length >= 4) {
            var arrSelections = thisString.split("|");
            thisID = arrSelections[0];
            if (parseInt(thisID) != parseInt(filID)) {
                newformfils = newformfils + thisString + "^";
                //CR 427 (CK 9/14/2009)
                if (thisString.indexOf("COLRSN=1530") > -1 || thisString.indexOf("COLRSN=2772") > -1 || thisString.indexOf("COLRSN=1171") > -1 || thisString.indexOf("COLRSN=3085") > -1 || thisString.indexOf("COLRSN=4461") > -1 || thisString.indexOf("COLRSN=1170") > -1 || thisString.indexOf("COLRSN=1156") > -1 || thisString.indexOf("COLRSN=2866") > -1 || thisString.indexOf("COLRSN=2702") > -1 || thisString.indexOf("COLRSN=1100") > -1 || thisString.indexOf("COLRSN=2756") > -1 || thisString.indexOf("COLRSN=4460") > -1 || thisString.indexOf("COLRSN=3200") > -1 || thisString.indexOf("COLRSN=1091") > -1 || thisString.indexOf("COLRSN=3095") > -1 || thisString.indexOf("COLRSN=1092") > -1 || thisString.indexOf("COLRSN=3094") > -1 || thisString.indexOf("COLRSN=3204") > -1) {
                    ChangeFilterContent('result1', thisID, arrSelections[6])
                }
                else {
                    ChangeFilterContent('result1', thisID, arrSelections[9])
                }
                //End
            }
        }
    }
    newformfils = ConvertChar(newformfils, 'code')
    document.formdata.colfilters.value = "";
    document.formdata.colfilters.value = newformfils;
    //Refresh page after deleting (CK 9/30/2009)
    if (refresh != 1) {
        document.formdata.action = 'refine.asp';
        document.formdata.submit()
    }
}


function DeleteFilter(filID) {
    ChangeContent('result1', '')
    var colfils = document.formdata.colfilters.value;
    var colfils = ConvertChar(colfils, 'null')
    var arrFilters = colfils.split("^");
    var newformfils = "";
    for (var x = 0; x < arrFilters.length; x++) {
        thisString = arrFilters[x];
        if (thisString.length >= 4) {
            var arrSelections = thisString.split("|");
            thisID = arrSelections[0];
            if (parseInt(thisID) != parseInt(filID)) {
                newformfils = newformfils + thisString + "^";
                //CR 427 (CK 9/14/2009)
                if (thisString.indexOf("COLRSN=1530") > -1 || thisString.indexOf("COLRSN=2772") > -1 || thisString.indexOf("COLRSN=1171") > -1 || thisString.indexOf("COLRSN=3085") > -1 || thisString.indexOf("COLRSN=4461") > -1 || thisString.indexOf("COLRSN=1170") > -1 || thisString.indexOf("COLRSN=1156") > -1 || thisString.indexOf("COLRSN=2866") > -1 || thisString.indexOf("COLRSN=2702") > -1 || thisString.indexOf("COLRSN=1100") > -1 || thisString.indexOf("COLRSN=2756") > -1 || thisString.indexOf("COLRSN=4460") > -1 || thisString.indexOf("COLRSN=3200") > -1 || thisString.indexOf("COLRSN=1091") > -1 || thisString.indexOf("COLRSN=3095") > -1 || thisString.indexOf("COLRSN=1092") > -1 || thisString.indexOf("COLRSN=3094") > -1 || thisString.indexOf("COLRSN=3204") > -1) {
                    ChangeFilterContent('result1', thisID, arrSelections[6])
                }
                else {
                    ChangeFilterContent('result1', thisID, arrSelections[9])
                }
                //End
            }
        }
    }
    newformfils = ConvertChar(newformfils, 'code')
    document.formdata.colfilters.value = "";
    document.formdata.colfilters.value = newformfils;
}


function EditFilter(filID) {
    var colfils = document.formdata.colfilters.value;
    var colfils = ConvertChar(colfils, 'null')
    var arrFilters = colfils.split("^");
    var thisColumn = "";
    for (var x = 0; x < arrFilters.length; x++) {
        thisString = arrFilters[x];
        if (thisString.length >= 4) {
            var arrSelections = thisString.split("|");
            thisID = arrSelections[0];
            thisColumn = ""
            if (parseInt(thisID) == parseInt(filID)) {
                thisColumn = arrSelections[1];
                thisSelYears = arrSelections[2];
                thisFirstOper = arrSelections[3];
                thisFirstCriteria = arrSelections[4];
                thisJoinCrit = arrSelections[5];
                thisSecondOper = arrSelections[6];
                thisSecondCriteria = arrSelections[7];
                thisDataType = arrSelections[8];
                thisSelYears = thisSelYears.replace("[ANY YEARS]", "ANY");
                thisSelYears = thisSelYears.replace("[ALL YEARS]", "ALL");
                thisSelYears = thisSelYears.replace("[", "");
                thisSelYears = thisSelYears.replace("]", "");
                break
            }
        }
    }
    if (thisColumn != "") {
        thisRSN = thisColumn.substring(thisColumn.indexOf("=") + 1, thisColumn.length);
        if (thisDataType == "Text" || thisDataType == "Numeric" || thisDataType == "One Decimal" || thisDataType == "Whole Number" || thisDataType == "Percentage" || thisDataType == "Phone Number" || thisDataType == "Year") {
            var numOptions = document.formdata.columnselector.options.length;
            for (var i = 0; i < numOptions; i++) {
                thisOption = document.formdata.columnselector.options[i].value;
                if (thisOption.indexOf(thisRSN) != -1) {
                    document.formdata.columnselector.selectedIndex = i;
                    DrawRegularFilter(document.formdata.columnselector.options[document.formdata.columnselector.selectedIndex].text)
                    ClearFilterVals()
                    document.formdata.selYears.options.length = 1;
                    document.formdata.selYears.options[0] = new Option("Any Years", "ANY", false, false);
                    document.formdata.selYears.options[1] = new Option("All Years", "ALL", false, false);
                    var theseSelections = document.formdata.colselections.value;
                    var columns = theseSelections.split("|");
                    var holderArray = new Array();
                    var holderCounter = 0;
                    for (var x = 0; x < columns.length; x++) {
                        thisColumnValue = columns[x];
                        if (thisColumnValue.length > 5) {
                            //Defect 173: Search for column_rsn after the year (e.g., 20011140) (CK 02/11/2009)
                            //if (columns[x].search(thisRSN) > -1)                            
                            if (columns[x].substring(9, 4).search(thisRSN) > -1) {
                                holderArray[holderCounter] = columns[x].substring(0, 4);
                                holderCounter = holderCounter + 1;
                            }
                        }
                    }
                    holderArray.sort()
                    for (var x = 0; x < holderArray.length; x++) {
                        var newYearOption = holderArray[x];
                        // Begin changes for the BAT 2 year labeling convention (YYYY-YY) CR 19 on 2/12/08 (DTa)
                        //document.formdata.selYears.options[document.formdata.selYears.options.length] = new Option(newYearOption, newYearOption, false, false);
                        var newYearLabel = newYearOption + '-' + String(parseInt(newYearOption) + 1).substr(2, 2)
                        document.formdata.selYears.options[document.formdata.selYears.options.length] = new Option(newYearLabel, newYearOption, false, false);
                        // End changes for CR 19 on 2/12/08 (DTa)
                    }
                }
            }
            for (var j = 0; j < document.formdata.selYears.options.length; j++) {
                if (document.formdata.selYears.options[j].value == thisSelYears) {
                    document.formdata.selYears.selectedIndex = j;
                }
            }
            showOper(thisDataType, document.formdata.firstCriteria)
            var numOptions = document.formdata.firstCriteria.options.length;
            for (var i = 0; i < numOptions; i++) {
                thisOption = document.formdata.firstCriteria.options[i].value;
                if (thisOption == thisFirstOper) {
                    document.formdata.firstCriteria.selectedIndex = i;
                }
            }
            document.formdata.firstCriteriaValue.value = thisFirstCriteria;
            var numOptions = document.formdata.joinCriteria.options.length;
            for (var i = 0; i < numOptions; i++) {
                thisOption = document.formdata.joinCriteria.options[i].value;
                if (thisOption == thisJoinCrit) {
                    document.formdata.joinCriteria.selectedIndex = i;
                }
            }
            showOper(thisDataType, document.formdata.secondCriteria)
            var numOptions = document.formdata.secondCriteria.options.length;
            for (var i = 0; i < numOptions; i++) {
                thisOption = document.formdata.secondCriteria.options[i].value;
                if (thisOption == thisSecondOper) {
                    document.formdata.secondCriteria.selectedIndex = i;
                }
            }
            document.formdata.secondCriteriaValue.value = thisSecondCriteria;
        }
        else {
            var numOptions = document.formdata.columnselector.options.length;
            for (var i = 0; i < numOptions; i++) {
                thisOption = document.formdata.columnselector.options[i].value;
                if (thisOption.indexOf(thisRSN) != -1) {
                    document.formdata.columnselector.selectedIndex = i;
                }
            }
            
            //CR 427: Show list box instead of dropdown for categorical variables (CK 9/4/2009)
            if (thisRSN == 1530 || thisRSN == 2772 || thisRSN == 1171 || thisRSN == 3085 || thisRSN == 4461 || thisRSN == 1170 || thisRSN == 1156 || thisRSN == 2866 || thisRSN == 2702 || thisRSN == 1100 || thisRSN == 2756 || thisRSN == 4460 || thisRSN == 3200 || thisRSN == 1091 || thisRSN == 3095 || thisRSN == 1092 || thisRSN == 3094 || thisRSN == 3204) {
                DrawCategoricalFilter(document.formdata.columnselector.options[document.formdata.columnselector.selectedIndex].text)
                ClearFilterVals()
                LoadDataSelector(thisRSN)
                numOptions = document.formdata.firstCriteriaValue.options.length;

                var arrValue = new Array();
                arrValue = thisFirstCriteria.split(',');

                for (x = 0; x < arrValue.length; x++) {
                    for (y = 0; y < document.formdata.firstCriteriaValue.length; y++) {
                        if (document.formdata.firstCriteriaValue.options[y].value == arrValue[x]) {
                            document.formdata.firstCriteriaValue.options[y].selected = arrValue[x];
                        }
                    }
                }
            }
            //End
            else {
                DrawDataTypeFilter(document.formdata.columnselector.options[document.formdata.columnselector.selectedIndex].text)
                ClearFilterVals()
                document.formdata.secondCriteriaValue.options[document.formdata.secondCriteriaValue.options.length] = new Option('', '', false, false);
                LoadDataSelector(thisRSN)
                numOptions = document.formdata.firstCriteriaValue.options.length;
                for (var i = 0; i < numOptions; i++) {
                    thisOption = document.formdata.firstCriteriaValue.options[i].value;
                    if (parseInt(thisOption) == parseInt(thisFirstCriteria)) {
                        document.formdata.firstCriteriaValue.selectedIndex = i;
                    }
                }
                numOptions = document.formdata.secondCriteriaValue.options.length;
                for (var i = 0; i < numOptions; i++) {
                    thisOption = document.formdata.secondCriteriaValue.options[i].value;
                    if (parseInt(thisOption) == parseInt(thisSecondCriteria)) {
                        document.formdata.secondCriteriaValue.selectedIndex = i;
                    }
                }
            }
        }
        document.formdata.filterID.value = filID;
        document.formdata.datatype.value = thisDataType;
    }
}


function displayAllFilters() {
    var colfils = document.formdata.colfilters.value;
    var arrFilters = colfils.split("^");
    for (var x = 0; x < arrFilters.length; x++) {
        thisString = arrFilters[x];
        if (thisString.length >= 4) {
            var arrSelections = thisString.split("|");
            thisID = arrSelections[0];
            ChangeFilterContent('result1', thisID, arrSelections[9])
        }
    }
}


function showRefineWarning() {
    return confirm("Any changes made to the Select Filters screen will be lost. Continue?")
}


function showNavWarning(savedID) {
    if (document.formdata.ID.value != "") {
        input = confirm("Selecting a requested or saved table will clear your current table settings. Continue?");
        if (input == true) {
            document.formdata.savedID.value = savedID;
            showWarning('RETRIEVE', '4');
        }
    }
    else {
        document.formdata.savedID.value = savedID;
        showWarning('RETRIEVE', '4');
    }
}

// OUTPUT FUNCTIONS

function createNewTable() {
    input = confirm("This will clear your current table selections, do you want to continue?");
    if (input == true) {
        document.formdata.level.value = "State";
        document.formdata.colselections.value = "";
        document.formdata.colfilters.value = "";
        document.formdata.groupby.value = "";
        document.formdata.sortorder.value = "";
        document.formdata.requested.value = "";
        document.formdata.createNew.value = "TRUE";
        document.formdata.filterCount.value = "";
        document.formdata.processed.value = "";
        document.formdata.curpage.value = "";
        document.formdata.argcatflag.value = "";
        document.formdata.rowFilter.value = "";
        goNav('1')
    }
}

function SaveTable() {
    input = confirm("Do you want to save the settings of this table?");
    if (input == true) {
        goNav('10')
    }
}


 
 
