﻿// <copyright file="CheckBoxes.js" company="BioCentury Publications Inc."> 
// All contents Copyright © 2005-2009 BioCentury Publications, Inc. ALL RIGHTS RESERVED
// 
// http://www.biocentury.com 
// </copyright>
function GetAllCheckedCheboxes(param, searchYears)
{
    //get all checkboxes, except years
    var listCheckboxes;
    listCheckboxes = document.getElementsByTagName('input');
    var listValues = new Array();
    for(var i=0; i<listCheckboxes.length; i++)
    {
        if (listCheckboxes[i].type != "checkbox")
        {
            continue;
        }
        if (listCheckboxes[i].checked)
        {
            if (listCheckboxes[i].id.indexOf(param)>=0)
            {
                listValues.push( listCheckboxes[i].value ); continue;
            }
            if (searchYears)
            {
                if (listCheckboxes[i].id == 'ctl00_ContentPlaceHolder1_allYears')
                    listValues.push( listCheckboxes[i].value );
                if (listCheckboxes[i].id == 'ctl00_ContentPlaceHolder1_twoYears')
                    listValues.push( listCheckboxes[i].value );
                if (parseInt(listCheckboxes[i].value,10))
                    listValues.push( listCheckboxes[i].value );
            }
        }
    }
    document.getElementById('ctl00_ContentPlaceHolder1_selectedCheckboxes').value = listValues.join();
    return true;
}

//used in DiseaseClass, Mechanism of Action and Moleculat Target to set right side summary for a specific checkbox
function setSummary(cb) 
{
    if (cb.checked) 
        addNode(cb);
    else 
        deleteNode(cb);
}
//used in MOA, MT and DC - removes an item's childs from right side summary
function removeChilds(cb) {
    var summary = document.getElementById(divSummaryId);
    var span = null;
    var cbChilds = document.getElementsByName(cb.id);
    for (var i = 0; i < cbChilds.length; i++) {//remove childs
        if (cbChilds[i].id != cb.id) {
            span = document.getElementById('summary_' + cbChilds[i].id);
            if (span != null) {
                summary.removeChild(span);

                // Add by Bela Gaspar 06/21/2006
                // keep selection in a temporary Hidden element; used before to click on Search
                document.getElementById(SearchSelectionSetId).value =
                    document.getElementById(SearchSelectionSetId).value.split(cbChilds[i].id + ",")[0] + 
                    document.getElementById(SearchSelectionSetId).value.split(cbChilds[i].id + ",")[1];
            }
            var childs = document.getElementsByName(cbChilds[i].id);
            if (childs.length > 1)
                removeChilds(cbChilds[i]);
        }
    }
}
//used in MOA, MT and DC - adds an item into right side summary
function addNode(cb) {
    var summary = document.getElementById(divSummaryId);
    var span = null;
    var isSearch = false;
    var isDisease = false;
    if (divSummaryId.indexOf('diseaseCategory')>=0) isDisease = true;
    if (document.getElementById('s_'+cb.id).name == 'result') isSearch = true;
    
    if (!isSearch && !isDisease)
        removeChilds(cb);

    //check brothers
    var cbBrothers = (cb.attributes['name']) ? document.getElementsByName(cb.attributes['name'].value) : '';
    var allChecked = true;
    for (var i = 0; i < cbBrothers.length; i++) {
        if (cbBrothers[i].id != cb.attributes['name'].value) {
            if (!cbBrothers[i].checked) {
                allChecked = false;
                break;
            }
        }
    }
    var parent = (cb.attributes['name']) ? document.getElementById(cb.attributes['name'].value) : null;
    if (allChecked && parent != null && !isSearch && !isDisease) {//check parent
        addNode(parent);
    }
    else {//add node
        span = document.createElement('span');
        span.setAttribute('id', 'summary_' + cb.id);
        span.setAttribute('name', 'summary');
        span.setAttribute('class', 'smallText');
        span.setAttribute('style', 'float: left;width:100%;');
        var strWith = "";
        if (cb.attributes["with"] != null)
            strWith = "<strong> with " + cb.attributes["with"].value + "</strong>";

        var strDel = '<div style="float:right;"><a title="Remove selection" onclick="delSummaryItem(\'' + cb.id + '\');" ' +
					 'href="#"><img alt="" style="border:none;" src="./images/icon_delete.gif"></a></div><div class="clear"></div>';

        span.innerHTML = '<div style="float:left">'+SetForDisplay([document.getElementById('s_' + cb.id).innerHTML]) + strWith+'</div>'+strDel;
        summary.appendChild(span);
        
        //var clear = document.createElement('div');
        //clear.setAttribute('class', 'clear');
        //summary.appendChild(clear);

        // Add by Bela Gaspar 06/21/2006
        // keep selection in a temporary Hidden element; used before to click on Search
        document.getElementById(SearchSelectionSetId).value += cb.id + ',';
    }
}
function delSummaryItem(cbID) {
    if (document.getElementById(cbID))
        document.getElementById(cbID).click();
    else {
        var summary = document.getElementById(divSummaryId);
        var span = null;
        //remove Node
        span = document.getElementById('summary_' + cbID);
        if (span != null) {
            summary.removeChild(span);

        }
        var selections = document.getElementById(SearchSelectionSetId).value;
        document.getElementById(SearchSelectionSetId).value =
			selections.split(cbID + ',')[0] + selections.split(cbID + ',')[1];
    }
}
//used in DoDiseaseClassSubStringSelectionExtraction
function RemoveNodes(aSearchSet) {
    for (var i = 0; i < aSearchSet.split(',').length - 1; i++) {
        var str = aSearchSet.split(',')[i];
        var cb = document.getElementById(str);
        cb.checked = false;
        deleteNode(cb);
    }
}
//used in MOA, MT and DC - removes an item from right side summary
function deleteNode(cb) {
    var summary = document.getElementById(divSummaryId);
    var isDisease = false;
    if (divSummaryId.indexOf('diseaseCategory')>=0) isDisease = true;
    var span = null;
    //remove Node
    span = document.getElementById('summary_' + cb.id);
    if (span != null) {
        summary.removeChild(span);

        // Add by Bela Gaspar 06/21/2006
        // keep selection in a temporary Hidden element; used before to click on Search
        document.getElementById(SearchSelectionSetId).value = document.getElementById(SearchSelectionSetId).value.split(cb.id + ",")[0] + document.getElementById(SearchSelectionSetId).value.split(cb.id + ",")[1];
    }
    //check brothers
    var cbBrothers = (cb.getAttribute('name')) ? document.getElementsByName(cb.getAttribute('name')) : '';
    var allChecked = true;
    for (var i = 0; i < cbBrothers.length; i++) {
        if (cbBrothers[i].id != cb.attributes['name'].value &&
			cbBrothers[i].id != cb.id) {
            if (!cbBrothers[i].checked) {
                allChecked = false;
                break;
            }
        }
    }
    if (allChecked) {//add brothers
        for (var i = 0; i < cbBrothers.length; i++) {
            if (cbBrothers[i].id != cb.attributes['name'].value &&
				cbBrothers[i].id != cb.id &&
				!document.getElementById('summary_'+cbBrothers[i].id)) 
			{
                span = document.createElement('span');
                span.setAttribute('id', 'summary_' + cbBrothers[i].id);
                span.setAttribute('name', 'summary');
                span.setAttribute('class', 'smallText');
                span.setAttribute('style', 'float:left;width:100%;');

                var strDel = '<div style="float:right;"><a title="Remove selection" onclick="delSummaryItem(\'' + cb.id + '\');" ' +
					 'href="#"><img alt="" style="border:none;" src="./images/icon_delete.gif"></a></div><div class="clear"></div>';

                span.innerHTML = '<div style="float:left">'+SetForDisplay([document.getElementById('s_' + cbBrothers[i].id).innerHTML]) + '</div>' + strDel;
                summary.appendChild(span);
                
                //var clear = document.createElement('div');
                //clear.setAttribute('class', 'clear');
                //summary.appendChild(clear);

                // Add by Bela Gaspar 06/21/2006
                // keep selection in a temporary Hidden element; used before to click on Search
                document.getElementById(SearchSelectionSetId).value += cbBrothers[i].id + ',';
            }
        }
    }
    var parent = document.getElementById(cb.attributes['name'].value);
    if (parent != null && !isDisease) deleteNode(parent);
    cb.checked = false;
}

//-------------------------------------------------------------------------------
// SetForDisplay - returns a formatted string for use in displaying summary information about a widget
//-------------------------------------------------------------------------------
function SetForDisplay(anArray) {
    // if "anArray" is string, then make it an Array
    if (!(anArray instanceof Array))
        anArray = anArray.split('_');

    var retval = "";
    for (var i = 0; i < anArray.length; i++) {
        if (anArray[i] != "" && anArray[i] != null) {
            retval += (i > 0) ? "<br />" : "";
            retval += "&nbsp;&nbsp;&#149;&nbsp;" + anArray[i];

        }
    }
    return retval;
}

function ToggleParent(check) {
    if (check.name != "" && document.getElementById(check.name) != null) {
        document.getElementById(check.name).checked = checkChilds(check.name) && check.checked;
        ToggleParent(document.getElementById(check.name));
    }

}

function ShowDiseaseClassIndications(diseaseClassId)
{
	if (document.getElementById("img_DiseaseClassID=" + diseaseClassId))
	{
		if (document.getElementById("s_DiseaseClassID=" + diseaseClassId).title=="Show Indications")
			document.getElementById("img_DiseaseClassID=" + diseaseClassId).src = 'images/b_up.gif';
		else
			document.getElementById("img_DiseaseClassID=" + diseaseClassId).src = 'images/b_down.gif';
	    document.getElementById("img_DiseaseClassID=" + diseaseClassId).title = (document.getElementById(diseaseClassId).style.display == 'block') ? 'Show Indications' : 'Hide Indications';
	}		
	document.getElementById("s_DiseaseClassID=" + diseaseClassId).title = (document.getElementById(diseaseClassId).style.display == 'block') ? 'Show Indications' : 'Hide Indications';
	document.getElementById(diseaseClassId).style.display = (document.getElementById(diseaseClassId).style.display == 'block') ? "none" : "block";
}

function checkChilds(name) {
    var allChecked = true;
    for (var i = 0; i < document.getElementsByName(name).length; i++) {
        if (document.getElementsByName(name)[i] != document.getElementById(name) && !document.getElementsByName(name)[i].checked)
            allChecked = false;
    }
    return allChecked;
}

function selectAll(check, param) 
{
    var theCheckBoxValue = check.id.split("=")[1];
    // for Search
    var cbs = document.getElementsByName(check.id);
    for (var i = 0; i < cbs.length; i++) {
        if (cbs[i].type &&
			cbs[i].type == 'checkbox' &&
			cbs[i].style.display != 'none' &&
			cbs[i].id != check.id) {
            cbs[i].checked = check.checked;
            if (param)
                setSummary(cbs[i]);
        }
        
    }
}
