// the elements currently selected
var selectedElements = new Array ();
// elements for which there is no info in the Index file
var missingElementsArray = new Array ();
// elements which don't have a common system whith the selected ones
var disabledElementsArray = new Array ();
// the elements available for selection
var enabledElements = new Array ();
//Controls the behavior when allSys are selected
var allSystemsSelected = false;
//The category option which is selected
var catOptionSelected = -1;

var openedDocuments = new Array();
// the colors for the different element categories, taken from the css classes

//blue
var color0 = "#AFB9D7";
var color0Dis = "#E8F0FF";
var textColor0 = "#234567";
var textColor0Dis = "#8baac9";

//red
var color1 = "#FBAD7E";
var color1Dis = "#FFE7DF";
var textColor1 = "#8D0D0D";
var textColor1Dis = "#d5a69b";

//orange
var color2 = "#EDD59B"; 
var color2Dis = "#FFFED9";
var textColor2 = "#B05B00";
var textColor2Dis = "#dba973";

//green
var color3 = "#AEC796"; 
var color3Dis = "#DFFFD5"; 
var textColor3 = "#314E00";
var textColor3Dis = "#95a667";

var colorFrame="#000000";
var colorWhite="#FFFFFF";

//prevents selecting another element before the result is retrieved from the server
var selectionEnabled = 1;

// The result document counts

var ternaryCount;
var binaryCount;
var diagramsAsPubCount;
var diagramCount;
var researchCount;
var linksCount;
var defaultElsDisabled = false;
var formReset = false;

/* Taken from /www.hunlock.com */
Array.prototype.find = function(searchStr) {
  var returnArray = false;
  for (i=0; i<this.length; i++) {
    if (typeof(searchStr) == 'function') {
      if (searchStr.test(this[i])) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    } else {
      if (this[i]===searchStr) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    }
  }
  return returnArray;
}


/*
* Called when the page is loaded
*/
function init () {
    submitSearchForm();
};

 function goTop() {
    if (window.location.hash != "#topLine" || window.location.hash.length == 0) {
        loc = window.location;
        window.location = loc + '#topLine'; 
    }
 } 

 var debugString;
 
function getFormData (formId) {
    var getstr = "?";
    var el;
    obj = document.getElementById(formId);

    for (i = 0; i < obj.elements.length; i ++) {
        el = obj.elements[i];
//        console.log(el.id);
 //       if (obj.childNodes[i].tagName == "INPUT") {
            
            if (el.type == "text") {
                getstr += el.name + "=" + el.value + "&";
            }
            if (el.type == "checkbox") {
                if (el.checked) {
                    getstr += el.name + "=" + el.value + "&";
                } else {
                    //getstr += el.name + "=&";
                }
            }
            if (el.type == "radio") {
                if (el.checked) {
                    getstr += el.name + "=" + el.value + "&";
                }
            }
//        }
/*        
        if (obj.childNodes[i].tagName == "SELECT") {
            var sel = obj.childNodes[i];
            getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
        }
*/        
    }
    debugString = getstr;
    return getstr;
}


function submitSearchForm() {
//    alert('in submitForm');
    query = getFormData('searchForm');
    jx.load('/read-online/controller.jsp' + query, displayResults,'json');
}



// from disableElements.jsp


var res;

function displayResults (data) {
    
    res = data;
    if (res.tableInShop == true) {
        setResults();
    }
    else {
        setResultsFront();
    }
}

function setResults() {    
//    source = document.hiddenForm;
/*    cats = document.searchForm['allCats'];
    tmp = "";
    for (i = 0; i < cats.length; i ++) {
        if (cats[i].checked == true) {
            tmp += " 1 ";
        }
        else {
            tmp += " 0 ";
        }
    }*/
    //       alert("selectedCats: " + tmp);
    //     alert('waiting');
    
    catOpt = res.catOpt;    
    
/*
* Inits the values in the document counts table
* The parameters should be passed with '' 
*/
    var cell;
    for (i = 0; i < 7; i++) {
        id = 'cat' + i;
        cell = document.getElementById (id);
        cell.innerHTML = res.docsCount[i];
    }
    cell = document.getElementById ('allNumbers');
    cell.innerHTML = res.docsCount[7];
    
    list = res.selectedElements;
    //       alert ('frontVisited: ' + source.frontVisited.value);
    
    if (res.frontVisited == 1) {
        selectedElements = list.split('-');
        //            alert('list: ' + list + ', parentArray: ' + parent.selectedElements.toString());
    }
    if (formReset == true) {
        formReset = false;
        restoreColors();
    }    
    
    list = res.elsToDisable;
    //       alert (list);
    disabledElementsArray = list.split('-');
    if (disableElements) {
//        console.log('els to disable: ' + list);
        disableElements(list);
    }       

    //if allSys is selected, there are now elements to show, everything is disabled
    if (document.searchForm && catOpt != 0) {
//        console.log('Sel elements list: ' + res.selectedElements);
        showSelectedElements(res.selectedElements);
    }
    else {
        //set the flag in the parent because it doesn't keep its value after refresh
        allSystemsSelected = true;
    }
    parent.selectionEnabled = 1;
    
    if (document.searchForm) {
        //            alert(catOpt);
        if (catOpt == -1) {
            //when there is no category selected, set it to 1 (As sel + any other)
            catOpt = 1;
        }
        // 0 and 3 was replaced
        if (document.searchForm.categoryOpt) {
            if (catOpt == 0) {
                catOpt = 3;
            }
            else if (catOpt == 3) {
                catOpt = 0;
            }
            
            document.searchForm.categoryOpt[catOpt].checked = true;
        }
        //setup the categories
        //cats = document.searchForm['allCats'];
        for (i = 0; i < document.searchForm['allCats'].length; i ++) {
            if (res.allCats[i] == 1) {
                document.searchForm['allCats'][i].checked = true;
            }
            else {
                document.searchForm['allCats'][i].checked = false;
            }
        }
        
//!!!!!! to remove when Property links come

        document.searchForm['allCats'][6].checked = false;
        document.searchForm['allCats'][6].disabled = true;
//!!!!!!!!!!     
    }
    //       alert(parent.document.getElementById('internalColorMt').style.backgroundColor);
    document.getElementById('docsTableDiv').innerHTML = res.docsTableContent;
    document.getElementById('systemsDiv').innerHTML = res.systemsToDisplay;
    
    selectionEnabled = 1;
/*    
    initPrice();
    
    //console.log('setParent: selEnabled = 1');
    if (document.getElementById('barTable')) {
        document.getElementById('barTable').title = res.priceBarTitle;
        document.getElementById('priceTable').title = res.priceBarTitle;
        document.getElementById('priceMessageDiv').innerHTML = res.priceBarTitle;
    }     
 */
};


function setResultsFront() {
    
    list = res.selectedElements;
    
    if (res.frontVisited == 1) {
        selectedElements = list.split('-');
        //            alert('list: ' + list + ', parentArray: ' + parent.selectedElements.toString());
    }
    if (formReset == true) {
        formReset = false;
        restoreColors();
    }    
    
    list = res.elsToDisable;
    //       alert (list);
    disabledElementsArray = list.split('-');
    if (disableElements) {
//        console.log('els to disable: ' + list);
        disableElements(list);
    }       

    //if allSys is selected, there are now elements to show, everything is disabled
    if (document.searchForm/* && catOpt != 3*/) {
//        console.log('Sel elements list: ' + res.selectedElements);
        showSelectedElements(res.selectedElements);
    }
    else {
        //set the flag in the parent because it doesn't keep its value after refresh
        allSystemsSelected = true;
    }
    parent.selectionEnabled = 1;
    selectionEnabled = 1;
}


function resetSearchForm() {

    if (document.getElementById("newPriceBar")) {
        document.getElementById("newPriceBar").style.width = "0em";
    }
    if (document.getElementById("divFirstCol")) {
        document.getElementById("divFirstCol").style.backgroundColor = "white";
    }
    
    for (i = 0; i < selectedElements.length; i ++) {
        id = "td" + selectedElements[i];
        if (document.getElementById (id)) {
           el = document.getElementById (id);
           cl = el.className;
           if (cl == "red") {
               el.style.backgroundColor = color1;
           }
           else {
              if (cl == "blue") {
                  el.style.backgroundColor = color0;
              }
              else {
                 if (cl == "orange") {
                     el.style.backgroundColor = color2;
                 }
                 else {
                     el.style.backgroundColor = color3;
                 }
              }
           }
        }
    }
    
    selectedElements = new Array();
    document.searchForm.selectedElements.value = "";
//    restoreColors();
    
    if (document.searchForm.allCats) {
        el = document.searchForm.allCats;
        for (i = 0; i < el.length; i ++) {
            el[i].checked = "true";
        }
        document.searchForm.allSelected.checked = "true";
    }
    
    if (document.searchForm.categoryOpt[1]) {
        document.searchForm.categoryOpt[1].checked = "true";
    }
    
    selectionEnabled = 1;
    formReset = true;
    submitSearchForm();    
//    window.location.reload();
};

function initPrice() {
    source = document.searchForm;
    target = document.shoppingForm;
    target.totalPrice.value = res.newPrice;
    target.itemsCount.value = res.itemsCount;
    target.itemsPrice.value = res.cartPrice;
    //    alert("docsTable: " + document.debugForm.catOpt.value);
    
    //init the price bars 
    
    target.newBarLength.value = res.newBarLength;
    target.cartBarLength.value = res.cartBarLength;
    target.maxBarLength.value = res.maxBarLength;
    parent.initPriceBars();
}

//from docsTable.jsp

var columnToSort;

function onColumnClick(colNumb) {    
    target = document.searchForm;
    target.showAllEntries.value = "0";
    target.columnToSort.value = colNumb;
    target.oldColumnToSort.value = res.oldColumnToSort;
    target.sortDirection.value = res.sortDirection;
    target.columnClicked.value = "1";
    columnToSort = colNumb;
    //    alert ("in the func");
    submitSearchForm();
}

function showOnClickError() {
    alert("You can't select documents here. Please, narrow your choice using the Periodic Table.");
}


function showAll() {
    
//    target = parent.document.catSelectForm;
    document.searchForm.showAllEntries.value = "1";
//    target.showAllEntries.value = source.showAllEntries.value;
    submitSearchForm();
}

function mouseTableOver(id) {
    el = document.getElementById("headText" + id);
    el.style.color = "#E66517";
    //   el.style.textDecoration = "underline";
}

function mouseTableOut(id) {
    el = document.getElementById("headText" + id);
    el.style.color = "black";
    //   el.style.textDecoration = "none";
}    


/*
* Changes the style of the cell in the Periodic table with the element selected
* Moves the element from the array with active elements to the array with the selected elements
*/

function selectElement (element, color) {    
   
    var system = "";
    var el;
    var processed = false;
    var disabled = false;
    
    //console.debug('selectElement: selectionEnabled = ' + selectionEnabled);
    
    if (selectionEnabled == 1) {
        selectionEnabled = 0;
        //console.debug('selectElement: selectionEnabled = 0');
    }
    else {
        //console.debug('selectElement: return false');
        selectionEnabled = 1;
        return false;
    }    
    
    if (document.searchForm.showAllEntries) {
        document.searchForm.showAllEntries.value = "0";
    }

//    alert (disabledElementsArray[0]);
//    alert(selectionEnabled);
//alert(document.getElementById('internalColorMt').style.backgroundColor);

    //disabled elements should not be processed
    for (i = 0; i < disabledElementsArray.length; i ++) {
        if (element == disabledElementsArray[i]) {
//            alert (element);
            disabled = true;
        }        
    }
    
    //if the element is disabled and not selected, exit
    if (disabled == true) {
        //console.debug('selectElement(): exit condition on line 300');
        selectionEnabled = 1;
        return false;
    }
    
    //check if the element is already selected:              
    for (i = 0; i < selectedElements.length; i ++) {        
            
        if (element == selectedElements[i]) {
            selectedElements.splice(i,1,"0"); //remove the element[i] and insert 0 at the end
            selectedElements.sort();
            selectedElements.shift();
            elName = "td" + element;
//            alert (elName);  
            intEl = "internalColor" + element;
            el = document.getElementById (elName);
            el2 = document.getElementById (intEl);    
           
//            alert ("color " + color);
            switch (color) {
                case 0: el.style.backgroundColor = color0;
                        el2.style.backgroundColor = color0;
//                        el2.style.color = textColor0;
                        break; 
                case 1: el.style.backgroundColor = color1;
                        el2.style.backgroundColor = color1;
//                        el2.style.color = textColor1;
                        break;
                case 2: el.style.backgroundColor = color2;
                        el2.style.backgroundColor = color2;
//                        el2.style.color = textColor2;
                        break;
                case 3: el.style.backgroundColor = color3;
                        el2.style.backgroundColor = color3;
//                        el2.style.color = textColor3;
                        break;                   
            } 
           
            processed = true;
            restoreColors();
            el.style.fontWeight = "bold";
            
        }        
    }

    
    //the element is not selected and not disabled: here!!!!!
    if (processed == false) {
	
        selectedElements.push(element);
        selectedElements.sort();
        enabledElements.pop(element);

        elName="td" + element;
        intEl="internalColor"+ element;

        el = document.getElementById (elName);
        el2 = document.getElementById (intEl);    
        
        cl = el.className;
         
       if (cl == "red") {
           el.style.backgroundColor = colorFrame;
           el2.style.backgroundColor = color1;
//           el2.style.color = textColor1Dis;
       }
       else {
          if (cl == "blue") {
              el.style.backgroundColor = colorFrame;
              el2.style.backgroundColor = color0;
//              el2.style.color = textColor0Dis;
          }
          else {
             if (cl == "orange") {
                 el.style.backgroundColor = colorFrame;
                 el2.style.backgroundColor = color2;
//                 el2.style.color = textColor2Dis;
             }
             else {
                 el.style.backgroundColor = colorFrame;
                 el2.style.backgroundColor = color3;
//                 el2.style.color = textColor3Dis;
             }
          }
       }//first else          
        el.style.backgroundColor = colorFrame; //frame of a selected element
      
//        el.style.fontWeight="normal";
    }        
    
    //When the user comes from the frontPage version of the periodicTable,
    //and hasn't selected any elements, there is an empty entry ("") at the
    //first position of selectedElements array. It should be removed.

    if (selectedElements[0] == '' || selectedElements[0] == ' ') {
         //remove the first value of the array
         selectedElements.shift();
    }

    //display the current system
    for (i = 0; i < selectedElements.length; i ++) {        
        if (i == 0 ) {
            system += selectedElements[i];
        }
        else {
            system += "-" + selectedElements[i];
        }    
    }
    document.searchForm.selectedElements.value = "";
    document.searchForm.selectedElements.value = system;
//    alert(system);

    browsername = navigator.appName;
    if (browsername.indexOf("Microsoft") == -1) { //not IE
        window.stop();
    }
    
//    alert(document.searchForm.selectedElements.value);
//    alert('length: ' + selectedElements.length);
    //console.debug('selectElement(): before submitting the searchForm');
    submitSearchForm();
};


//enables all disabled elements and leaves only the selected ones
function restoreColors () {    
   var disabled;
   var selected;
   var tmp;
   var elNumb;
   
   //keep the current disabledElementsArray. Here the elements from the missingElementsArray
   //will be added, but then are not needed any more there
   var disOriginal = disabledElementsArray;
   
   
//alert(missingElementsArray.toString());
//alert('dis: ' + disabledElementsArray.toString());   
   
/*        for (j = 0; j < missingElementsArray.length; j ++) {
           disabledElementsArray.push(missingElementsArray[j]) 
        }  
*/   

   for (i = 0; i < disabledElementsArray.length; i ++) {
       disabled = false;
       selected = false;
       
       //Missing elements should always be disabled
       for (j = 0; j < missingElementsArray.length; j ++) {
           if (disabledElementsArray[i] == missingElementsArray[j]) {
               disabled = true;
               break;
           }
       }

       //Currently selected elements should stay disabled
       for (j = 0; j < selectedElements.length; j ++) {
           if (disabledElementsArray[i] == selectedElements[j]) {
               selected = true;
               break;
           }
       }
//       alert(disabledElementsArray[i] + ' sel: ' + selected + ' dis: ' + disabled);
       
       if (!disabled && !selected) {
           
           id = "td" + disabledElementsArray[i];
           intDiv = "internalColor" + disabledElementsArray[i];
           if (document.getElementById (id)) {
               el = document.getElementById (id);         
               el.style.cursor = "pointer";               
           }
           if (document.getElementById (intDiv)) {
               el2 = document.getElementById (intDiv);         
               el2.style.cursor = "pointer";
               el2.style.fontWeight = "bold";
           }           
           cl = el.className;
//alert("classname: " + cl);
 
           el.style.color = "black";  
          
           //change the color of the element's number
           id = "numb" + disabledElementsArray[i];
           if (document.getElementById(id)) {
               elNumb = document.getElementById(id);
           }            
           
           if (cl == "red") {
               el.style.backgroundColor = color1;
               el2.style.backgroundColor = color1;
               el2.style.color = textColor1;
               elNumb.style.color = textColor1;
           }
           else {
              if (cl == "blue") {
                  el.style.backgroundColor = color0;
                  el2.style.backgroundColor = color0;
                  el2.style.color = textColor0;
                  elNumb.style.color = textColor0;
              }
              else {
                 if (cl == "orange") {
                     el.style.backgroundColor = color2;
                     el2.style.backgroundColor = color2;
                     el2.style.color = textColor2;
                     elNumb.style.color = textColor2;  
                 }
                 else { //green
                     el.style.backgroundColor = color3;
                     el2.style.backgroundColor = color3;
                     el2.style.color = textColor3;
                     elNumb.style.color = textColor3;
                 }
              }
           }     
            
       }
  }//for
   
  disabledElementsArray = disOriginal; 
};

//Implements the "hover" effect
function mouseOver (element) {
    //prevent selecting another element before the result is retrieved
    if (selectionEnabled == 0) {
    	return false;
    }
    
    elName = "td" + element;
 //           alert (elName);  
    intEl = "internalColor" + element;
    el = document.getElementById (elName);
    el2 = document.getElementById (intEl);    
   
    disabled = false;
//    alert (disabledElementsArray[0]);

    //disabled elements should not be processed
    for (i = 0; i < disabledElementsArray.length; i ++) {
        if (element == disabledElementsArray[i]) {
//            alert (element);
            disabled = true;
        }        
    }
    selected = false;
    for (j = 0; j < selectedElements.length; j ++) {
       if (element == selectedElements[j]) {
           selected = true;
           break;
       }
    }    
    
    if (!disabled && !selected) {    
       el.style.backgroundColor = colorFrame; 
    }
};


function mouseOut (element) {

    //prevent selecting another element before the result is retrieved
    if (selectionEnabled == 0) {
        return false;
    }


    elName = "td" + element;
 //           alert (elName);  
    intEl = "internalColor" + element;
    el = document.getElementById (elName);
    el2 = document.getElementById (intEl);
    disabled = false;
    
    //disabled elements should not be processed
    for (i = 0; i < disabledElementsArray.length; i ++) {
        if (element == disabledElementsArray[i]) {
//            alert (element);
            disabled = true;
        }        
    }
    
   selected = false;
   for (j = 0; j < selectedElements.length; j ++) {
       if (element == selectedElements[j]) {
           selected = true;
           break;
       }
   }     
    
    if (!disabled && !selected) {   
        cl = el.className;
        if (cl == "red") {
            el.style.backgroundColor = color1;
            el2.style.backgroundColor = color1;
        }
        else {
           if (cl == "blue") {
              el.style.backgroundColor = color0;
              el2.style.backgroundColor = color0;
           }
           else {
             if (cl == "orange") {
                 el.style.backgroundColor = color2;
                 el2.style.backgroundColor = color2;
             }
             else {
                 el.style.backgroundColor = color3;
                 el2.style.backgroundColor = color3;
             }
           }
        }   
    } 
};

/*
    Transfers the current element selection of the 'FrontPage' version to the OnlineShop version.
    The function is called from disableElements.jsp
*/

function showSelectedElements(elString) {        
    elParam = "";
//    alert('allSys: ' + allSystemsSelected + ' ' + 'cat: ' + catOptionSelected);    
    if (allSystemsSelected == true) {
        if (catOptionSelected != 3) { 
           allSystemsSelected = false;
//           alert('in the if');
//To change that later, so not the whole form is reset
//           resetSearchForm();
//           document.searchForm.categoryOpt[catOptionSelected].checked = true;
           disabledElementsArray = new Array();
           disabledElementsArray = missingElementsArray;
        }    
        return;
    }

//    alert(elementString);
    
//    console.log('The els string: ' + elString);
    
    if (elString == 'null' || elString == '') {
//        alert(null);
        return;
    }    
    
//    elArray = elString;
    elArray = elString.split('-');

    //Initialize the selectedElements array. In this way an additionally selected elelements
    //is appended to the current selection
    selectedElements = elArray;
//    alert(selectedElements.toString());
    
    for (i = 0; i < elArray.length; i ++) {
       elParam += elArray[i] + '-';
       elName="td" + elArray[i];
       intEl="internalColor"+ elArray[i];

       el = document.getElementById (elName);
       el2 = document.getElementById (intEl);    
       
       cl = el.className;
         
       if (cl == "red") {
           el.style.backgroundColor = colorFrame;
           el2.style.backgroundColor = color1;
//           el2.style.color = textColor1Dis;
       }
       else {
          if (cl == "blue") {
              el.style.backgroundColor = colorFrame;
              el2.style.backgroundColor = color0;
//              el2.style.color = textColor0Dis;
          }
          else {
             if (cl == "orange") {
                 el.style.backgroundColor = colorFrame;
                 el2.style.backgroundColor = color2;
//                 el2.style.color = textColor2Dis;
             }
             else {
                 el.style.backgroundColor = colorFrame;
                 el2.style.backgroundColor = color3;
//                 el2.style.color = textColor3Dis;
             }
          }
       }//first else          
       el.style.backgroundColor = colorFrame; //frame of a selected element
       //el.style.fontWeight = "normal";
    }
    document.searchForm.selectedElements.value = "";    
    document.searchForm.selectedElements.value = elParam.substr(0,elParam.length - 1);
};

function addAllSubsystems (subsysArray) {
   document.searchForm.allSystems.options.length = 0; 
   for (i = 0; i < subsysArray; i ++) { 
     document.searchForm.allSystems.value += 
         subsysArray[i] + "\n";         
   }
};

/*
* Inits the values in the document counts table
* The parameters should be passed with '' 
*/

function initDocumentCounts (teCount, beCount, daPCount, dCount, rrCount, llCount, thrdCount, allNumbers) {
    var cell;
    cell = document.getElementById ('cat0'); //teCount
    cell.innerHTML = teCount;
    cell = document.getElementById ('cat1'); //beCount
    cell.innerHTML = beCount;
    cell = document.getElementById ('cat2'); //daPCount
    cell.innerHTML = daPCount;
    cell = document.getElementById ('cat3'); //dCount
    cell.innerHTML = dCount;     
    cell = document.getElementById ('cat4'); //rrCount
    cell.innerHTML = rrCount;
    cell = document.getElementById ('cat5'); //llCount
    cell.innerHTML = llCount;
    cell = document.getElementById ('cat6'); //thrdCount
    cell.innerHTML = thrdCount;    
    cell = document.getElementById ('allNumbers');
    cell.innerHTML = allNumbers;
};



//recieves a list of elements, which should be deactivated

function disableElements (elList) {

   var id;
   var intDiv;
   var el2;
   var cl;
   var selected;
   var el;

//   alert('elList: ');
   
   var elements = elList.split("-");      
   disabledElementsArray = elements;

//   alert('disableElements: selElements.length: ' + selectedElements.length);

   if (defaultElsDisabled == false && res.selectedElements.length == 0 
            && selectedElements.length == 0) {
        //check this in case the periodicTable is in the frontPage - there we have 
        //only 1 categoryOpt
        if (document.searchForm.categoryOpt[0] && !document.searchForm.categoryOpt[0].checked) {
        //        alert('defaultElsDisabled = false');
            missingElementsArray = disabledElementsArray;
//        console.debug('selElementsLength: ' + selectedElements.length + 
//            ', resSelElementsLength: ' + res.selectedElements.length);
//        console.debug('missingElementsArray set, length: ' + missingElementsArray.length);
            defaultElsDisabled = true;
        }
   }
   
   for (i = 0; i < elements.length; i ++) {
       selected = false;
       for (j = 0; j < selectedElements.length; j ++) {
           if (elements[i] == selectedElements[j]) {               
               selected = true;
               break;
           }
       }
       if (!selected) {
           id = "td" + elements[i];
           intDiv="internalColor" + elements[i];
           if (document.getElementById (id)) {
               el = document.getElementById (id);
               el.style.cursor = "default";
           }
           if (document.getElementById (intDiv)) {
               el2 = document.getElementById (intDiv);
               el2.style.cursor = "default";
           }
           //exit from the for loop in case there is no Periodic table
           if (!el) {
               break;
           }
           cl = el.className;
           if (cl == "red") {
               el.style.backgroundColor = color1Dis;
               el2.style.backgroundColor = color1Dis;
               el2.style.color = textColor1Dis;
           }
           else {
              if (cl == "blue") {
                  el.style.backgroundColor = color0Dis;
                  el2.style.backgroundColor = color0Dis;
                  el2.style.color = textColor0Dis;
              }
              else {
                 if (cl == "orange") {
                     el.style.backgroundColor = color2Dis;
                     el2.style.backgroundColor = color2Dis;
                     el2.style.color = textColor2Dis;
                 }
                 else {
                     el.style.backgroundColor = color3Dis;
                     el2.style.backgroundColor = color3Dis;
                     el2.style.color = textColor3Dis;
                 }
              }
           }
           //el.style.color = "#CCEEFF";
           el.style.color = "#999999";
//           el.style.fontWeight = "normal";
//           el2.style.fontWeight = "normal";
           
           //change the color of the element's number
           id = "numb" + elements[i];
           if (document.getElementById(id)) {
               el3 = document.getElementById(id);
               el3.style.color = "#999999";               
           }
       }
  }
//  selectionEnabled = 1; 
    
};

function categorySelection(option) {

    //for subsystems no elements should be disabled
    catOptionSelected = option;
    if (option == 2) { //sub-systems
        restoreColors();
    }
    if (option == 3) { //all systems
       allSystemsSelected = true;
       //remove all elements from the arrays
       delete selectedElements;
       delete disabledElementsArray;
       disabledElementsArray = new Array ();
       selectedElements = new Array ();
       restoreColors();
       //when selecting one of the other cats, no elements should be pre-selected
       document.searchForm.selectedElements.value = "";
    }
    else {
        if (selectedElements.length == 0) {
            restoreColors();
        }
    }
    submitSearchForm();
};

function getSubsystems (system) {
    var allSys = Math.pow(2, system.length) - 1;
    var subsys = new Array();
    var temp = new Array();
    var k;
    var p;
    
    for (i = 1; i < allSys + 1; i ++) {
        p = 1;
        for (j = 0; j < system.length; j ++) {
            if ((i & p) != 0) {                
                temp.push(system[j]);
            }
            p = p << 1;    
        }
        temp.sort();
        subsys.push(temp);
//        alert (temp);
        document.searchForm.allSystems.value += '\n' + temp;      
        for (f = 0; f < temp.length; f ++) {
            temp.pop();
        }
    }
    return subsys;        
};

function checkCategories (cat) {
//    alert (document.catSelectForm.allCats.length);

    //holds the number of currently selected categories
    var checkedCount = 0;
//    alert ("allCats length: " + document.searchForm.allCats.length);
    for (i = 0; i < document.searchForm.allCats.length; i ++) {
//        alert("search: " + document.searchForm.allCats[i].value + " " + document.searchForm.allCats[i].checked);
        if (document.searchForm.allCats[i].checked) {
           // document.catSelectForm.allCats[i].checked = true;
            checkedCount ++;
        }
        else {
      //      document.catSelectForm.allCats[i].checked = false;    
        }

//        alert("hidden: " + document.catSelectForm.allCats[i].value + " " + document.catSelectForm.allCats[i].checked);
    }
    
    if (checkedCount != document.searchForm.allCats.length) {
        document.searchForm.allSelected.checked = false;
    }
    else {
        document.searchForm.allSelected.checked = true;
    }
    
    if (checkedCount == 0) {
        alert ("At least one category must be selected!");    
        document.searchForm.allCats[cat].checked = true;
        return false;        
    }
    //in "All systems" mode only 100 result items must be displayed, when new
    //categories are checked/unchecked
    if (document.searchForm.categoryOpt[0].checked) {
        document.searchForm.showAllEntries.value = 0;
    }
 
    browsername=navigator.appName;
    if (browsername.indexOf("Microsoft")==-1) { //not IE
        window.stop();
    }
    submitSearchForm();
};

function checkAllCategories () {
    
    if (document.searchForm.allSelected.checked) {
       for (i = 0; i < document.searchForm.allCats.length; i ++) {
           document.searchForm.allCats[i].checked = true;                     
       }
    }
    else {
       alert("You can't deselect all categories!");       
       document.searchForm.allSelected.checked = true;
       return false;
    }
    checkCategories(); 
};

function onAddToCart () {
    document.cartForm.submit();
    
//    query = getFormData('cartForm');    
//    jx.load('ShoppingCartServlet' + query, displayResults,'json','get');    
};

function initPriceBars () {
    
    target = document.shoppingForm;
    target.totalPrice.value = res.newPrice;
    target.itemsCount.value = res.itemsCount;
    target.itemsPrice.value = res.cartPrice;    
    
    if (res.newBarLength != '0em' && res.cartBarLength == '0em') {
        document.getElementById("firstColumnCell").style.backgroundColor = "#D5DDF3";
    }
    else {
        if (res.cartBarLength != '0em') {
            document.getElementById("firstColumnCell").style.backgroundColor = "#A8BAF3";
        }
        else {
            document.getElementById("firstColumnCell").style.backgroundColor = "#FFFFFF";
        }
    }
 
    document.getElementById("newPriceBar").style.width = res.newBarLength;
    document.getElementById("cartPriceBar").style.width = res.cartBarLength;
    document.getElementById("maxPriceBar").style.width = res.maxBarLength;
    disBar = document.getElementById("disabledPriceBar").style.backgroundColor = "#FFFFFF";
};


//functions for the 'Front page' version

function resetPeriodicTable () {

    selectedElements = new Array();
    document.searchForm.selectedElements.value = "";
    
    selectionEnabled = 1;
    formReset = true;
    submitSearchForm();
    s = window.location;
    window.location = s;
};

function viewDocsDetails () {
   document.showDetailsForm.elementsToShow.value = document.searchForm.selectedElements.value;
//   alert(document.searchForm.selectedElements.value);
   document.showDetailsForm.submit();
}

function addDoc(pid) {
    addDocToSession(pid);
}

function addDocToList(pid,image,title,urlPart) {
    
    if (findDoc(pid,openedDocuments) == false) {
        
        openedDocuments.push(pid);
        tmp = document.getElementById('recentDocs').innerHTML;
        tmp += "<a href=\"preview-html/" + pid + "/" + urlPart + "\" target=\"" +  
            pid + "\"><img src=\"images/" + image + "\" />" + title + "</a>"; 
        document.getElementById('recentDocs').innerHTML = tmp;        
    }
}

function addDocToSession(docPid) {

    document.getElementById("docPid").value = docPid;
    query = getFormData('docSessionForm');
    jx.load('/read-online/DocSessionServlet' + query,showRes,'json');
}

function showRes(data) {
    res = data;
    addDocToList(res.docPid,res.docImg,res.docName,res.urlPart);
}

function findDoc(pid,arr) {
    
    var res = false;
    
    for (i = 0; i < arr.length; i ++) {
        if (arr[i] == pid) {
            res = true;
            break;
        }
    }
    return res;
}














