﻿// <copyright file="Results.js" company="BioCentury Publications Inc."> 
// All contents Copyright © 2005-2009 BioCentury Publications, Inc. ALL RIGHTS RESERVED
// 
// http://www.biocentury.com 
// </copyright>

var id_hdSelectedCategories = 'ctl00_ContentPlaceHolder1_hdSelectedCategories';
var id_hdSelectedCategoriesCount = 'ctl00_ContentPlaceHolder1_hdSelectedCategoriesCount';
var id_hdShowOnly = 'ctl00_ContentPlaceHolder1_hdShowOnly';
var id_checkBoxSelectAllItems = 'ctl00_ContentPlaceHolder1_checkBoxSelectAllItems';
var id_hdIndividuallySelected = 'ctl00_ContentPlaceHolder1_hdIndividuallySelected'; 
var id_hdIndividuallyExcluded = 'ctl00_ContentPlaceHolder1_hdIndividuallyExcluded'; 
var hrefMergeSelected = 'ctl00_ContentPlaceHolder1_hrefMergeSelected';

var saveWindow = null;

function biocPageLoad()
{
    HideProgress();
    CheckTheCheckboxesFromThePage();
    ShowHideMergeSelectedItems();
    document.getElementById("ctl00_ContentPlaceHolder1_TreeHitsReport").className = "DivVisible";
}

function HideProgress()
{
    var element = document.getElementById('showProgress')
    if (element != null)
    {
        element.style.display = "none";
    }
}

function CheckboxChanged(sender, applyToChildWindow, popupSenderId, popupSenderIsChecked)
{
    if (applyToChildWindow == true)
    {
        CheckChildWindow(sender);    
    }
    var mySenderId = sender != null ? sender.id : popupSenderId;
    var myChecked = sender != null ? sender.checked : popupSenderIsChecked;
    if (myChecked)
    {
        CheckSummary(mySenderId);
    }
    else
    {
        UncheckSummary(mySenderId);
    }
    ShowHideMergeSelectedItems();
}
function clearSelection()
{
   document.getElementById(id_hdIndividuallySelected).value = '';
   document.getElementById(id_hdIndividuallyExcluded).value = '';
   document.getElementById(id_hdSelectedCategories).value = '';
   document.getElementById(id_hdSelectedCategoriesCount).value = '';
   document.forms[0].submit();
}
function CountTotal()
{
    var indSelected = parseInt(CountItemsNo(id_hdIndividuallySelected));
    var indExcluded = parseInt(CountItemsNo(id_hdIndividuallyExcluded));
    var selectedCat = 0;
    var cats = document.getElementById(id_hdSelectedCategories);
    var counts = ","+document.getElementById(id_hdSelectedCategoriesCount).value;
    for (var i=1;i<=CountItemsNo(id_hdSelectedCategories);i++)
    {
        var cat = cats.value.split(',')[i].toLowerCase();//othnews
        selectedCat += parseInt(counts.split(','+cat+'=')[1].split(',')[0]);
    }
    
    return indSelected-indExcluded+selectedCat;
}
function ShowHideMergeSelectedItems()
{
    if (document.getElementById('itemsNo'))
        document.getElementById('itemsNo').innerHTML = '(' + CountTotal() + ')';
        
    
    if (
        !IsEmpty(document.getElementById(id_hdIndividuallySelected).value) ||
        !IsEmpty(document.getElementById(id_hdIndividuallyExcluded).value)
        )    
    {
        DisableEnableAnchor(document.getElementById(hrefMergeSelected), 'false');
        DisableEnableAnchor(document.getElementById('hrefClear'), 'false');
        if (document.getElementById('itemsNo'))
            document.getElementById('itemsNo').style.color = "#ad3516";
        return;
    }
    if (!IsEmpty(document.getElementById(id_hdSelectedCategories).value))
    {
        DisableEnableAnchor(document.getElementById(hrefMergeSelected), 'false');
        DisableEnableAnchor(document.getElementById('hrefClear'), 'false');
        if (document.getElementById('itemsNo'))
            document.getElementById('itemsNo').style.color = "#ad3516";
        //document.getElementById('itemsNo').innerHTML = '(' + CountItemsNo('ctl00_ContentPlaceHolder1_hdIndividuallySelected') + ')';
        return;
    }
    DisableEnableAnchor(document.getElementById(hrefMergeSelected), 'true');
    DisableEnableAnchor(document.getElementById('hrefClear'), 'true');
    if (document.getElementById('itemsNo'))
    {
        document.getElementById('itemsNo').style.color = "Gray";
        document.getElementById('itemsNo').innerHTML = '(0)';
    }
}

function CheckSummary(checkboxId)
{
    var tokens = checkboxId.split('_');
    var collectionLeaf = tokens[0]+'_'+tokens[1];
    
    var listIndividuallyExcluded = document.getElementById(id_hdIndividuallyExcluded).value.split(',');
    
    if (ListContains(listIndividuallyExcluded, checkboxId))
    {
        RemoveFromList(listIndividuallyExcluded, checkboxId);
        document.getElementById(id_hdIndividuallyExcluded).value = listIndividuallyExcluded.join(',');
        
        if (!ListContainsSmthWithSufix(id_hdIndividuallyExcluded, collectionLeaf))
        {
            document.getElementById(id_checkBoxSelectAllItems).checked = true;
        }
    }
    else
    {
        var listIndividuallySelected = document.getElementById(id_hdIndividuallySelected).value.split(',');
        AddToList(listIndividuallySelected, checkboxId);
        document.getElementById(id_hdIndividuallySelected).value = listIndividuallySelected.join(',');
    }
}

function UncheckSummary(checkboxId)
{
    var pos_ = checkboxId.lastIndexOf('_');
    var collectionLeaf = checkboxId.substring(pos_ + 1);
    
    var listIndividuallySelected = document.getElementById(id_hdIndividuallySelected).value.split(',');
    
    if (ListContains(listIndividuallySelected, checkboxId))
    {
        RemoveFromList(listIndividuallySelected, checkboxId);
        document.getElementById(id_hdIndividuallySelected).value = listIndividuallySelected.join(',');
    }
    else
    {
        document.getElementById(id_checkBoxSelectAllItems).checked = false;
        var listIndividuallyExcluded = document.getElementById(id_hdIndividuallyExcluded).value.split(',');
        AddToList(listIndividuallyExcluded, checkboxId);
        document.getElementById(id_hdIndividuallyExcluded).value = listIndividuallyExcluded.join(',');
    }
}

function CheckChildWindow(sender)
{
    if (openeeHandler == null)
    {
        return;
    }
    if (openeeHandler.closed)
    {
        return;
    }
    var articleIdFromParentWindowHidden = openeeHandler.document.getElementById('ctl00_ContentPlaceHolderArticle_articleId');
    var collectionPrefix = sender.id.substring(0, sender.id.indexOf('_'));
    if (articleIdFromParentWindowHidden != null)
    {
        var articleIdFromParentWindow = articleIdFromParentWindowHidden.value;
        if (articleIdFromParentWindow.substring(0, collectionPrefix.length) == collectionPrefix)
        {
            openeeHandler.document.getElementById('ctl00_selectedArticle').checked = sender.checked;   
        }    
    }    
}

function AddExcluded(senderId)
{
    var hiddenInput = GetHiddenExcluded(senderId);
    var listValues = hiddenInput.value.split(',');
    listValues.push(senderId);
    hiddenInput.value = listValues.join(',');
    //ShowHideSelectLinks();
}

function AddChecked(senderId)
{
    var hiddenInput = GetHidden(senderId);
    var listValues = hiddenInput.value.split(',');
    listValues.push(senderId);
    hiddenInput.value = listValues.join(',');
    ShowHideSelectLinks();
}

function RemoveExcluded(senderId)
{
    var hiddenInput = GetHiddenExcluded(senderId);
    var listValues = hiddenInput.value.split(',');
    var posFound = GetPosition(senderId, listValues);
    if (posFound != -1)
    {
        listValues.splice(posFound, 1);
    }
    hiddenInput.value = listValues.join(',');
    //ShowHideSelectLinks();
}

function RemoveChecked(senderId)
{
    var hiddenInput = GetHidden(senderId);
    var listValues = hiddenInput.value.split(',');
    var posFound = GetPosition(senderId, listValues);
    if (posFound != -1)
    {
        listValues.splice(posFound, 1);
    }
    hiddenInput.value = listValues.join(',');
    ShowHideSelectLinks();
}

function GetPosition(what, where)
{
    for(var i=0; i<where.length; i++)
    {
        if (what == where[i])    
        {
            return i;
        }
    }
    return -1;
}

function GetHiddenExcluded(senderId)
{
    var hiddenId = GetHiddenIdExcluded(senderId);
    return document.getElementById( hiddenId );
}


function GetHidden(senderId)
{
    var hiddenId = GetHiddenId(senderId);
    return document.getElementById( hiddenId );
}

function GetHiddenIdExcluded(senderId)
{
    if (senderId.indexOf('biocb') != -1)
    {
        return idHiddenBiocbExcluded;
    }
    else
    {
        return idHiddenBiocxExcluded;
    }
}

function GetHiddenId(senderId)
{
    if (senderId.indexOf('biocb') != -1)
    {
        return idHiddenBiocbSelected;
    }
    else
    {
        return idHiddenBiocxSelected;
    }
}

function ShowHideSelectLinks()
{
    ShowHideSelectLink(idHiddenBiocbSelected);
    ShowHideSelectLink(idHiddenBiocxSelected);
    ShowHideShowAllSelectedPart2AndExtraLink();
}

function ShowHideShowAllSelectedPart2AndExtraLink()
{
    var hiddenInputB = document.getElementById( idHiddenBiocbSelected );
    var hiddenInputX = document.getElementById( idHiddenBiocxSelected );
    var valueB = hiddenInputB.value;
    var valueX = hiddenInputX.value;
    if (IsEmpty(valueB) && IsEmpty(valueX))
    {
        HideAllSelectedPart2AndExtraLink();
    }
    else
    {
        ShowAllSelectedPart2AndExtraLink();
    }
}

function HideAllSelectedPart2AndExtraLink()
{
    var myTd = GetCellForSelectedPart2AndExtraLink();
    if (myTd != null)
    {
        myTd.innerHTML = '';
    }
}

function GetCellForSelectedPart2AndExtraLink()
{
    var myTd = document.getElementById('ctl00_ContentPlaceHolder1_cellShowAllFromNode');
    var myHidden = document.getElementById('ctl00_ContentPlaceHolder1_hdShowOnly');
    if (myHidden.value != '' && myHidden.value != 'all')
    {
        myTd = document.getElementById('ctl00_ContentPlaceHolder1_cellShowSelected');
    }
    return myTd;    
}

function ShowAllSelectedPart2AndExtraLink()
{
    var myTd = GetCellForSelectedPart2AndExtraLink(); 
    if (myTd == null)
    {
        return;
    }
    myTd.innerHTML = '<a href="javascript:ShowSelected(allSelected)" class="smallLink">Show all selected Week In Review and The Daily Extra</a>';    
}

function IsEmpty(str)
{
    return str == '' || str == ',';
}

function ShowHideSelectLink(hiddenId)
{
    var hiddenInput = document.getElementById( hiddenId );
    var value = hiddenInput.value;
    if (IsEmpty(value))
    {
        HideLinks(hiddenId);
    }
    else
    {
        ShowLinks(hiddenId);
    }
}

function HideLinks(hiddenId)
{
    var elem = null;
    if (hiddenId.indexOf('biocb') != -1)
    {
        elem = document.getElementById('ctl00_ContentPlaceHolder1_part2ShowSelected');
        if (elem != null)
        {
            elem.innerHTML = '';
        }
        elem = document.getElementById('ctl00_ContentPlaceHolder1_part2Clear');
        if (elem != null)
        {
            elem.innerHTML = '';
        }
    }
    if (hiddenId.indexOf('biocx') != -1)
    {
        elem = document.getElementById('ctl00_ContentPlaceHolder1_extraShowSelected');
        if (elem != null)
        {
            elem.innerHTML = '';
        }
        elem = document.getElementById('ctl00_ContentPlaceHolder1_extraClear');
        if (elem != null)
        {
            elem.innerHTML = '';
        }
    }
}

function CountItemsNo(hiddenId)
{
    return document.getElementById(hiddenId).value.split(',').length - 1;
}

function ShowLinks(hiddenId)
{
    var elem = null;
    if (hiddenId.indexOf('biocb') != -1)
    {
        elem = document.getElementById('ctl00_ContentPlaceHolder1_part2ShowSelected');
        if (elem != null)
        {
            var itemsNo = CountItemsNo(idHiddenBiocbSelected);
            //elem.innerHTML = '<a href="javascript:createPopUp(5,\'Articles.aspx?action=showSelectedPart2\')" class="buttonLink">Show selected ('+ itemsNo + ')</a>';
            elem.innerHTML = '<a href="javascript:ShowSelected(biocb)" class="smallLink">Show selected ('+ itemsNo + ')</a>';
        }
        elem = document.getElementById('ctl00_ContentPlaceHolder1_part2Clear');
        if (elem != null)
        {
            elem.innerHTML = '<a href="javascript:ClearPart2()" class="smallLink">Clear</a>';
        }
    }
    if (hiddenId.indexOf('biocx') != -1)
    {
        elem = document.getElementById('ctl00_ContentPlaceHolder1_extraShowSelected');
        if (elem != null)
        {
            var itemsNo = CountItemsNo(idHiddenBiocxSelected);
            elem.innerHTML = '<a href="javascript:ShowSelected(biocx)" class="smallLink">Show selected (' + itemsNo + ')</a>';
        }
        elem = document.getElementById('ctl00_ContentPlaceHolder1_extraClear');
        if (elem != null)
        {
            elem.innerHTML = '<a href="javascript:ClearExtra()" class="smallLink">Clear</a>';
        }
    }
}

function ClearPart2()
{
    document.getElementById(idHiddenBiocbSelected).value = '';
    Uncheck('biocb');
    UncheckOpenee('biocb');
    ShowHideShowAllSelectedPart2AndExtraLink();
}

function ClearExtra()
{
    document.getElementById(idHiddenBiocxSelected).value = '';
    Uncheck('biocx');
    UncheckOpenee('biocx');
    ShowHideShowAllSelectedPart2AndExtraLink();
}

function UncheckOpenee(collectionPrefix)
{
    if (openeeHandler == null)
    {
        return;
    }
    if (openeeHandler.closed)
    {
        return;
    }
    var articleIdFromParentWindowHidden = openeeHandler.document.getElementById('ctl00_ContentPlaceHolderArticle_articleId');
    if (articleIdFromParentWindowHidden != null)
    {
        var articleIdFromParentWindow = articleIdFromParentWindowHidden.value;
        if (articleIdFromParentWindow.substring(0, collectionPrefix.length) == collectionPrefix)
        {
            openeeHandler.document.getElementById('ctl00_selectedArticle').checked = false;   
        }    
    }    
}

function Uncheck(prefix)
{
    var allInputs = document.getElementsByTagName('input')
    for(var i=0; i<allInputs.length; i++)
    {
        if (allInputs[i].id.substr(0, prefix.length) == prefix && allInputs[i].checked)
        {
            allInputs[i].checked=false;
        }
    }
    if (prefix=='biocb')   
    {
        HideLinks(idHiddenBiocbSelected);
    }
    else
    {
        HideLinks(idHiddenBiocxSelected);
    }
}

function ShowSelected()
{
    /*var form = document.forms[0];
    alert(form);
    var originalTarget = form.target;
    var originalAction = form.action;
    */
    window.open ('Articles.aspx', 'popUpContent', 'directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=yes');
    /*form.target = 'popUpContent';
    //form.action = 'Articles.aspx?show='+collectionName;
    form.action = 'Articles.aspx';
    form.submit();
    
    form.target = originalTarget;
    form.action = originalAction;
    */
}

function ShowSelectedNodeArticles()
{
    var form = document.forms[0]
    var originalTarget = form.target;
    var originalAction = form.action;
    
    window.open ('', 'popUpContent', 'directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=yes');
    form.target = 'popUpContent';
    form.action = 'Articles.aspx?show=node';
    form.submit();
    
    form.target = originalTarget;
    form.action = originalAction;
}

function NodeClick(nodeValue)
{
    document.getElementById('ctl00_ContentPlaceHolder1_hdShowOnly').value = nodeValue;
    document.forms[0].submit();
    return false;
}

function CheckUncheckSelectAll()
{
    var checkboxSelectAll = document.getElementById(id_checkBoxSelectAllItems);
    
    if (checkboxSelectAll.checked)
    {
        CheckAll();
    }
    else
    {
        UncheckAll();
    }
    //CheckUncheckInPopup(checkboxSelectAll.checked);
    ShowHideMergeSelectedItems();
}

//function CheckUncheckInPopup(checked)
//{
//    if (openeeHandler == null)
//    {
//        return;
//    }
//    if (openeeHandler.closed)
//    {
//        return;
//    }
//    var hiddenFromParentWindowHidden = openeeHandler.document.getElementById('ctl00_ContentPlaceHolderArticle_articleId');
//    if (hiddenFromParentWindowHidden!= null)
//    {
//        var articleIdFromParentWindowHidden = hiddenFromParentWindowHidden.value;
//        var pos_ = articleIdFromParentWindowHidden.lastIndexOf('_');
//        var articleCollectionLeaf = articleIdFromParentWindowHidden.substring(1 + pos_);
//        
//        var listSelected = document.getElementById(id_hdShowOnly).value.split(',');
//    
//        if (listSelected.length == 0)
//        {
//            listSelected.push( hdShowOnlyValue );  
//        }
//        if (ListContains(listSelected, articleCollectionLeaf))
//        {
//            openeeHandler.document.getElementById('ctl00_selectedArticle').checked = checked;      
//        }
//    }    
//}

function UncheckAll()
{
    var listHdShowOnly = document.getElementById(id_hdShowOnly).value.split(',');
    
    MaybeExpand(listHdShowOnly);
    
    for(var i=0; i<listHdShowOnly.length; i++)
    {
        if (listHdShowOnly[i] == "")
        {
            continue;
        }
        UncheckForCategory(listHdShowOnly[i]);
    
        RemoveAllEndingWithFromHidden( id_hdIndividuallySelected, '_' + listHdShowOnly[i] );
        RemoveAllEndingWithFromHidden( id_hdIndividuallyExcluded, '_' + listHdShowOnly[i] );
    }
    CheckAllSummaryChecboxes(false);
}

function UncheckForCategory(categoryName)
{
    var listSelectedCategories = document.getElementById(id_hdSelectedCategories).value.split(',');
    
    RemoveFromList(listSelectedCategories, categoryName);
    document.getElementById(id_hdSelectedCategories).value = listSelectedCategories.join(',');

    RemoveAllEndingWithFromHidden( id_hdIndividuallySelected, '_' + categoryName );
    RemoveAllEndingWithFromHidden( id_hdIndividuallyExcluded, '_' + categoryName );
}

function CheckAll()
{
    var listHdShowOnly = document.getElementById(id_hdShowOnly).value.split(',');
    
    MaybeExpand(listHdShowOnly);
    //hdShowOnly.value = listHdShowOnly.join(',');
    
    for(var i=0; i<listHdShowOnly.length; i++)
    {
        if (listHdShowOnly[i] == "")
        {
            continue;
        }
        CheckForCategory(listHdShowOnly[i]);
    }
    
    CheckAllSummaryChecboxes(true);
}

function GetExpandedList(collectionName)
{
    var myList = new Array();
    switch(collectionName)
        {
           case 'bioca':
            for (var i=0;i<biocaList.length;i++)
                myList.push(biocaList[i]);
//                myList.push('coverstory');
//                myList.push('regulation');
//                myList.push('productdev');
//                myList.push('analystpicks');
//                myList.push('strategy');
//                myList.push('toolstechniques');
//                myList.push('companyprofile');
//                myList.push('politicspolicy');
//                myList.push('finance');
                break;
           case 'biocb':
                return GetExpandedList('companynewsall').concat(GetExpandedList('clinicalnewsall')).concat(GetExpandedList('financialnewsall'));
                break;
           case 'companynewsall':
           for (var i=0;i<companyNewsList.length;i++)
                myList.push(companyNewsList[i]);
//                myList.push('deals');
//                myList.push('salesandmarketing');
//                myList.push('othnews');
//                myList.push('managementtrack');
                break;
           case 'clinicalnewsall':
            for (var i=0;i<clinicalNewsList.length;i++)
                myList.push(clinicalNewsList[i]);
//                myList.push('regulatory');
//                myList.push('clinicalresults');
//                myList.push('precliresults');
//                myList.push('clinicalstatus');
//                myList.push('othresearchnews');
                break;
           case 'financialnewsall':
           for (var i=0;i<financialNewsList.length;i++)
                myList.push(financialNewsList[i]);
//                myList.push('completedofferings');
//                myList.push('proposedofferings');
//                myList.push('withdrawnofferings');
//                myList.push('postponedofferings');
//                myList.push('amendedofferings');
//                myList.push('otherfinancialnews');
                break;
           case 'biocx':
           for (var i=0;i<biocxList.length;i++)
                myList.push(biocxList[i]);
//                myList.push('topstory');
//                myList.push('clinicalnews');
//                myList.push('financialnews');
//                myList.push('companynews');
//                myList.push('politics');
                break;
            case 'scibx':
                return GetExpandedList('analysis').concat(GetExpandedList('distillery'));
                break;
            case 'analysis':
                for (var i=0;i<analysisList.length;i++)
                myList.push(analysisList[i]);
//                myList.push('coverstoryscibx');
//                myList.push('targetsmechanisms');
//                myList.push('publicprivateinterface');
//                myList.push('drugplatforms');
//                myList.push('markers');
//                myList.push('tools');
//                myList.push('translationalnotes');
                break;
            case 'distillery':  
            for (var i=0;i<distileryList.length;i++)
                myList.push(distileryList[i]);              
//                myList.push('techniques');
//                myList.push('therapeutics');
                break;                
            case 'all':
                return GetExpandedList('bioca').concat(GetExpandedList('biocb')).concat(GetExpandedList('biocx')).concat(GetExpandedList('scibx'));
                break;
         }
     return myList;
}

function MaybeExpand(myList)
{
    var i = 0;
    while ( i < myList.length)
    {
        var currentItem = myList[i];
        var listToReplace = GetExpandedList(currentItem);
        
        if (listToReplace.length == 0)
        {
            ++i;
        }
        else
        {
                myList.splice(i, 1);//eliminate
                for(var j=0; j<listToReplace.length; j++)
                {
                   myList.push(listToReplace[j]); 
                }
        }
    }
}

function CheckForCategory(categoryName)
{
    var listSelectedCategories = document.getElementById(id_hdSelectedCategories).value.split(',');
    if (!ListContains(listSelectedCategories, categoryName))
    {
        AddToList(listSelectedCategories, categoryName);
    }
    document.getElementById(id_hdSelectedCategories).value = listSelectedCategories.join(',');

    RemoveAllEndingWithFromHidden( id_hdIndividuallySelected, categoryName );
    RemoveAllEndingWithFromHidden( id_hdIndividuallyExcluded, categoryName );
}

function RemoveAllEndingWithFromHidden(idHidden, suffix)
{
    var myHiddenValue = document.getElementById(idHidden).value;
    var myList = myHiddenValue.split(',');
    var i=0; 
    while(i<myList.length)
    {
        var currentListItem = myList[i];
        if (StringStartsWith(currentListItem, suffix))
        {
            myList.splice(i, 1);
        }
        else
        {
            ++i;
        }
    }
    document.getElementById(idHidden).value = myList.join(',');
}

function StringEndsWith(bigStr, smallStr)
{
    var pos_smallStr = bigStr.lastIndexOf(smallStr);
    if ( pos_smallStr == -1)
    {
        return false;
    }
    if (pos_smallStr + smallStr.length == bigStr.length)
    {
        return true;
    }
    else
    {
        return false;
    }
}

function StringStartsWith(bigStr, smallStr)
{
    var pos_smallStr = bigStr.lastIndexOf(smallStr);
    if ( pos_smallStr == -1)
    {
        return false;
    }
    if (pos_smallStr == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

function RemoveAllStartingWithFromHidden(idHidden, prefix)
{
    var myHidden = document.getElementById(idHidden);
    var myHiddenValue = myHidden.value;
    var myList = myHiddenValue.split(',');
    var i=0; 
    while(i<myList.length)
    {
        var currentListItem = myList[i];
        if (currentListItem.indexOf(prefix) == 0)
        {
            myList.splice(i, 1);
        }
        else
        {
            ++i;
        }
    }
    myHidden.value = myList.join(',');
}

function AddToList(myList, newItem)
{
    myList.push(newItem);    
}

function RemoveFromList(myList, myItem)
{
    var posFound = GetPosition(myItem, myList);
    if (posFound != -1)
    {
        myList.splice(posFound, 1);
    }
}

function CheckTheCheckboxesFromThePage()
{
    try
    {   
    
    var hdShowOnly = document.getElementById(id_hdShowOnly);
    
    var listSelected = GetExpandedList(hdShowOnly.value);
        
    if (listSelected.length == 0)
    {
        listSelected.push( hdShowOnly.value );  
    }
    
    for( var i=0; i<listSelected.length; i++)
    {
        CheckTheCheckboxesFromThePageForLeafCollection(listSelected[i]);   
    }
    
    //if (listSelected.length > 1)//super-category, not a leaf one
    {
        var listSelectedCategories = document.getElementById(id_hdSelectedCategories).value.split(',');
        UncheckSummariesForMissingCategories(listSelected, listSelectedCategories);
        UncheckSelectAllIfNotAllSubcategoriesAreEntirelySelected(listSelected, listSelectedCategories);
    }
    ModifyCheckboxes(id_hdIndividuallySelected, true);
    ModifyCheckboxes(id_hdIndividuallyExcluded, false);
    }
    catch(err){}
}

function UncheckSelectAllIfNotAllSubcategoriesAreEntirelySelected(listSelected, listSelectedCategories)
{
    try
    {
    for(var i = 0; i<listSelected.length; i++)
    {
        if (listSelected[i] == '')
        {
            continue;
        }
        if (!ListContains(listSelectedCategories,listSelected[i]))
        {
            document.getElementById(id_checkBoxSelectAllItems).checked = false;
            return;
        }
        if (ListContainsSmthWithSufix(id_hdIndividuallyExcluded, listSelected[i]))
        {
            document.getElementById(id_checkBoxSelectAllItems).checked = false;
            return;
        }
    }
    }
    catch(err){}
}

function UncheckSummariesForMissingCategories(listSelected, listSelectedCategories)
{
    for(var i = 0; i<listSelected.length; i++)
    {
        if (listSelected[i] == '')
        {
            continue;
        }
        if (!ListContains(listSelectedCategories,listSelected[i]))
        {
            var allSummaryChecboxes = document.getElementsByTagName('input');
            for(var j=0; j<allSummaryChecboxes.length; j++)
            {
                if (allSummaryChecboxes[j].type != "checkbox" 
                    || (!StringStartsWith(allSummaryChecboxes[j].id, 'bioc')
                    && !StringStartsWith(allSummaryChecboxes[j].id, 'scibx'))
                )
                {
                    continue;
                }
                if (StringEndsWith(allSummaryChecboxes[j].id, '_' + listSelected[i]))
                {
                    allSummaryChecboxes[j].checked = false;
                }
            }
        }
    }
}

function CheckTheCheckboxesFromThePageForLeafCollection(leafCollection)
{
    var listSelectedCategories = document.getElementById(id_hdSelectedCategories).value.split(',');
    if ( ListContains(listSelectedCategories, leafCollection) )
    {
        document.getElementById(id_checkBoxSelectAllItems).checked = true;
        CheckAllSummaryChecboxes(true);
    }
    if (ListContainsSmthWithSufix(id_hdIndividuallyExcluded, '_' + leafCollection))
    {
        document.getElementById(id_checkBoxSelectAllItems).checked = false;
    }
}

function CheckAllSummaryChecboxes(check)
{
    var allSummaryChecboxes = document.getElementsByName("result"); //document.getElementsByTagName('input');
    for(var i=0; i<allSummaryChecboxes.length; i++)
    {
        if (allSummaryChecboxes[i].type != "checkbox") 
            //|| (!StringStartsWith(allSummaryChecboxes[i].id, 'bioc')
            //&& !StringStartsWith(allSummaryChecboxes[i].id, 'scibx'))
            //)
        {
            continue;
        }
        allSummaryChecboxes[i].checked = check;
    }
}

function StringStartsWith(bigStr, smallStr)
{
    return bigStr.indexOf(smallStr) == 0;
}

function ListContainsSmthWithSufix(idHidden, sufix)
{
    var myHidden = document.getElementById(idHidden);
    var myHiddenValue = myHidden.value;
    var myList = myHiddenValue.split(',');
    for(var i=0; i<myList.length; i++)
    {
        if (StringEndsWith(myList[i], sufix))
        {
            return true;
        }
    }
    return false;
}

function ModifyCheckboxes(idHidden, check)
{
    var myHidden = document.getElementById(idHidden);
    var myHiddenValue = myHidden.value;
    var myList = myHiddenValue.split(',');
    for(var i=0; i<myList.length; i++)
    {
        if (myList[i].length == 0)
        {
            continue;
        }
        var myCheckbox = document.getElementById(myList[i]);
        if (myCheckbox != null)
        {
            myCheckbox.checked = check;
        }
    }
}

function ListContains(myList, myItem)
{
    var posFound = GetPosition(myItem, myList);
    if (posFound != -1)
    {
        return true;
    }
    else
    {
        return false;
    }
    //return posFound != -1
}

function Jump(jumpType, jumpValue)
{
    document.getElementById('ctl00_ContentPlaceHolder1_hdJumpType').value = jumpType;
    document.getElementById('ctl00_ContentPlaceHolder1_hdJumpValue').value = jumpValue;
    document.forms[0].submit();
}

function DisableEnableAnchor(obj,disable)
{
 if (obj == null)
 {
    return;
 }
 if (disable=="true")
  {
   obj.onclick=null;
   obj.removeAttribute('onclick');
   obj.disabled=true;
   obj.style.color="Gray";
   obj.style.cursor="default";
  }
 else
  {
  if (document.all)
   eval(obj.id+'.onclick =function() {'+obj.onclick_back+';}');
  else
   obj.setAttribute('onclick', obj.getAttribute("onclick_back"));
   
  obj.disabled=false;
  obj.style.color="#ad3516";
  obj.style.cursor="pointer";
  }
}

//-------------------------------------------------------------------------------
// "Save Search"
// used 
// ADD Bela Gaspar
//-------------------------------------------------------------------------------
function SaveCriteria()
{
	// if there is no pop-up already opened
	//if (saveWindow == null)
	//{
		leftVal = window.screenLeft;//(screen.width - 500) / 2;
		topVal = window.screenTop;//(screen.height - 150) / 2;
		saveWindow = window.open("CriteriaSave.aspx", "_blank", 'width=500,height=200,left='+leftVal+',top='+topVal+',toolbar=0,resizable=0');
		return false;
	//}
	/*else
	{
		saveWindow.close();
		return false;
	}*/
}
