/*
   Builds college dropdown list - assumes the html is there for the <select> line (with id=selectname) and default option
   In:
      inExtension - this may be 50 for a login, or 30 for the reserves list
      inDropdownID - the id defined for the college dropdown
      inCollegeCode - the local_base from the querystring (if exists)
   Out: Populates the dropdownlist with the aleph code (and extension) of the 
   
   Note: You want to create the select like the following, if you want to generate a campus dropdown list as well:
      <select name="local_base" id="local_base" onchange="buildCampusDropdown(this.value, 'ddlcampus');" >
*/
function buildCollegeDropdown(inExtension, inDropdownID, inCurrBase) {
   if (!inExtension) {
      inExtension = '';
   }
   var inCollegeCode = inCurrBase;
   if ((inCollegeCode == '') || (inCollegeCode.match(/all/i))) {
      // if not in the parameter passed in, call the function in the commonfunctions library
      inCollegeCode = getLocalBase();
   }
   if (inCollegeCode.length > 3) {
      inCollegeCode = inCollegeCode.substring(0,3);
   }
   var key;
   var ddlcoll = getElement(inDropdownID);
   var cIndex = 0, indfound=false;
   if (ddlcoll) {
      for (key in collegelist) {
         cIndex = ddlcoll.length;
         ddlcoll.options[cIndex]=new Option(collegelist[key].getCollegename(), collegelist[key].getCodeExt(inExtension));
         var testRE = new RegExp(inCollegeCode, "i");
         if (!indfound && testRE.test(ddlcoll[cIndex].value)) {
            ddlcoll.selectedIndex = cIndex;
            indfound = true;
         }
      }
      if ((ddlcoll.selectedIndex < 0) && (ddlcoll.options.length > 0)) {
         ddlcoll.selectedIndex = 0;
      }
      if (inExtension == '') {
         ddlcoll.options[cIndex]=new Option('All Community Colleges','');
      }
   }
}; // buildCollegeDropdown

/*
   Given a college code, build a list of campuses.  Intended for use with the buildCollegeDropdown function
   In:
      inCollegeCode - the alephCode of the college (with or without extension)
      inDropdownID - the id of the select to populate with the campus list
   Out: Populates the dropdown list with the aleph code & name of the campus
*/
function buildCampusDropdown(inCollegeCode, inDropdownID) {
   var collCd = inCollegeCode;
   if (collCd.length > 3) {
      collCd = collCd.substring(0,3);
   }
   if (!collCd.match(/all/i) && (collCd != '') && getElement(inDropdownID) && (collegelist[collCd.toUpperCase()])) {
      var ddlCamp = getElement(inDropdownID);
      clearDropdown(ddlCamp);
      var campuslist = collegelist[collCd.toUpperCase()].getCampuslist(collCd);
      if ((campuslist) && (campuslist.length > 1)) {
         for (var i = 0; i < campuslist.length; i++) {
            var campuspair = campuslist[i].split(':');
            ddlCamp.options[ddlCamp.length]=new Option(campuspair[1], campuspair[0]);
            if (inCollegeCode.match(ddlCamp[ddlCamp.length - 1].value, 'i')) {
               ddlCamp.selectedIndex = ddlCamp.length - 1;
            }
         }
         showElement(inDropdownID);
      } else {
         hideElement(inDropdownID);
      }
   } else {
      hideElement(inDropdownID);
   }
}; // buildCampusDropdown

// Clears all but the first item of the dropdown list - except if the first element is '' or all
function clearDropdown(inDropdown) {
   if (inDropdown) {
      for (var i=inDropdown.length-1; i>0; i--) {
         inDropdown.options[i]=null;
      }
      if ((inDropdown.options[0]) && (inDropdown.options[0].value != '') && !(inDropdown.options[0].value.match(/all/i))) {
         inDropdown.options[0]=null;
      }
   }
}; // clearDropdown  
  
