﻿function checkChildParents(objCheck, federalId)
{       
    var mainDiv             = objCheck.parentNode.parentNode.parentNode.parentNode.parentNode;
    var inputMainDiv        = mainDiv.getElementsByTagName("input");
	var auxCheckBox         = false;
	var checkBoxYes		    = false;
	var auxCheckBoxYes      = false;
	var uncheckRadio        = false;
	var hispanicRadioButton = false;
	var ipedsAttribute      = false;

	//if any sub categories for Hispanic section are chosen then we will set a flag to deselect the Hispanic "no" radio button
	for (var j = 0; j < inputMainDiv.length; j++) 
	{
	    if (inputMainDiv[j].type == 'checkbox') 
	    {
	        ipedsAttribute = inputMainDiv[j].getAttribute("ipedsfederalcategoryid");
	        if (ipedsAttribute != null) {
	            if (inputMainDiv[j].ipedsfederalcategoryid == "1") 
	            {
	                if (inputMainDiv[j].checked == true) {
	                    uncheckRadio = true;
	                }
	            }
	        }
	    }
	}

	for (var i = 0; i < inputMainDiv.length; i++) 
	{
	    //If hispanic subcategories chosen then deselect the no radio
	    if (inputMainDiv[i].id.indexOf("HispanicNoRadio") > 0)
        {
            hispanicRadioButton = document.getElementById(inputMainDiv[i].id);
            if (uncheckRadio) 
            {
                hispanicRadioButton.checked = false;
            }
        }
        //If hispanic subcategories chosen then select the yes radio
        if (inputMainDiv[i].id.indexOf("HispanicYesRadio") > 0) 
        {
            hispanicRadioButton = document.getElementById(inputMainDiv[i].id);
            if (uncheckRadio) 
            {
                hispanicRadioButton.checked = true;
            }
        }
		
		//This part is for the checkboxes
        if (inputMainDiv[i].id.indexOf("_IpedsFederalCheckBox") > 0)
        {
            if (inputMainDiv[i].type == 'checkbox')
			{
			    if (objCheck.checked) 
			    {
				    //If the current child CheckBox is checked, then the parent checkbox must be checked		    
					inputMainDiv[i].checked = objCheck.checked;
					inputMainDiv[i].disabled = objCheck.checked;	
					auxCheckBox = true;				  
                }
                else 
                {
				    //If the current child CheckBox is not checked and at least other child checkbox in this 
				    //group is checked then the parent checkBox must be checked
                    var checkNodes = objCheck.parentNode.parentNode.childNodes;
                    for (j = 0; j < checkNodes.length; j++)
                    {
                        if (checkNodes[j].tagName == "LI")
                        {
                            var x = checkNodes[j].childNodes;
                            for (k = 0; k < x.length; k++)
                            {
                                if (x[k].type == 'checkbox') 
                                {
                                    if (x[k].checked)
                                    	auxCheckBox = true;
                                    else
                                        auxCheckBox = false;
                                }  
                                if (auxCheckBox)
                                    break;                                  
                            }
                        }
                        if (auxCheckBox)
                            break;
					}
					if (auxCheckBox) 
					{
                        inputMainDiv[i].checked = true;
                        inputMainDiv[i].disabled = true;
                    }
                    else 
                    {
                        inputMainDiv[i].checked  = false;
                        inputMainDiv[i].disabled = false;                         
                    }
                }                                       
            }
		}             
    }       
}

//If the Federal Category is unchecked, then you must find all the child sub-categories and uncheck them as well
function evaluateFederalCategory(obj, federalId)
 {
    if (obj.checked == false) 
    {
        var checkBoxList = document.getElementsByTagName('input');
        for (var i = 0; i < checkBoxList.length; i++) 
        {
            var element = checkBoxList[i];
            if (element.type == 'checkbox') 
            {
                ipedsAttribute = element.getAttribute("ipedsfederalcategoryid");
                if (ipedsAttribute == federalId) 
                {
                    element.checked = false;
                }
            }
        }
    }
}

//Evaluate the Hispanic section
//If the "No" radio button is chosen, deselect all sub category check boxes
function evaluateHispanic(objRadio) 
{
    var mainDiv         = objRadio.parentNode.parentNode.parentNode.parentNode.parentNode;
    var inputMainDiv    = mainDiv.getElementsByTagName("input");
    var ipedsAttribute  = false;
    var checksDone      = false;
	var radioNoFound    = false;

	for (var i = 0; i < inputMainDiv.length; i++) 
    {
        if (inputMainDiv[i].id.indexOf("HispanicNoRadioButton") > 0) 
        {
            if (document.getElementById(inputMainDiv[i].id).checked == true) 
            {
                radioNoFound = true;
            }

            if (radioNoFound) 
            {
                //Uncheck the sub categories
                //Look for all inputs that have "1" in ipedsfederalcategoryid tag
                for (var j = 0; j < inputMainDiv.length; j++) 
                {
                    if (inputMainDiv[j].type == 'checkbox') 
                    {
                        ipedsAttribute = inputMainDiv[j].getAttribute("ipedsfederalcategoryid");
                        if (ipedsAttribute != null) 
                        {
                            if (inputMainDiv[j].ipedsfederalcategoryid == "1") 
                            {
                                inputMainDiv[j].checked = false;
                            }
                        }
                        checksDone = true;
                    }
                }
            }
        }
        if (checksDone) break;
	}
}

//Clear all radio and checkbox selections.  Each of those types have an ipedsfederalcategoryid attribute
function clearSelections() 
{
    var elementList = document.getElementsByTagName('input');
    for (var i = 0; i < elementList.length; i++) 
    {
        var element = elementList[i];
        ipedsAttribute = element.getAttribute("ipedsfederalcategoryid");
        if (ipedsAttribute != null) {
            element.checked = false;
        }
        else {
            if (element.id.indexOf("_IpedsFederalCheckBox") > 0) {
                element.disabled = false;
                element.checked = false;
            }
        }
    }
}
