// File: const_js_library.js
// Author: Shehzaad Nakhoda
// Written for: The Constitution of Pakistan at www.pakistani.org/pakistan/constitution

var thisPageName= "";

// thisPageName should be declared at the page level
function const_onload()
{
    selectThisOption(thisPageName, document.getElementsByName('const_part'));
    // remove the Go buttons from the drop down navigation menu from the DOM tree
    removeElemsWithName('menuGoSubmit');
}

function const_onunload() 
{
   // this is so that when the back button is pressed the right menu
   //  item is still selected
   selectThisOption(thisPageName, document.getElementsByName('const_part'));
}

function selectThisOption(thisOptionValue, selectElementsList) {
   for(var j=0; j < selectElementsList.length; j++) {
     var selectList = selectElementsList[j];
     var options = selectList.getElementsByTagName("option");
     for (var i = 0; i < options.length; i++) { 
       thisValue = options[i].value; 
       if(thisOptionValue == thisValue) {
         options[i].selected = true;
         break;
       }
    }
  }
}

function removeElemsWithName(theName) {

    var elemsToDie = document.getElementsByName(theName);
    // trying to make sure the implementation can handle NodeLists that morph
    // when a node is removed AS WELL AS NodeLists that don't.
    var numElemsToDie = elemsToDie.length;
    var i = 0;
    while(i < numElemsToDie && elemsToDie.length > 0) {
         elemsToDie[i].parentNode.removeChild(elemsToDie[i]);
	 if(elemsToDie.length == numElemsToDie) {
	   // we know the NodeList is not shrinking
           i++;
         }
    }
}