// JavaScripts for the tabbed panel in Library homepage


// check if enter (key code=13) is pressed
function isEnterPressed(evtEvent) {
	var keyPressed = (window.event) ? evtEvent.keyCode : evtEvent.which;
	return (keyPressed==13);
}  //  function isEnterPressed

// functions for search boxes begin
function validate_required(field,alerttxt)
{
with (field)
  {
  if (value==null||value=="")
    {
    alert(alerttxt);return false;
    }
  else
    {
    return true;
    }
  }
}

// for WebPAC keyword search, not being used
function validate_form_webpacSearch()
{
   if (validate_required(document.webpacSearch.searcharg,"Please enter keyword(s) to search!")==false) {
     document.webpacSearch.searcharg.focus();
	 return false;
	}
  else { return true; }
}

function validate_form_encoreSearch()
{
   if (validate_required(document.encoreSearch.target,"Please enter keyword(s) to search!")==false) {
     document.encoreSearch.target.focus();
	 return false;
	}
  else { return true; }
}

// for ebook search in WebPAC, not being used
function validate_form_ebkSearch()
{
   if (validate_required(document.ebkSearch.SEARCH,"Please enter keyword(s) to search!")==false) {
     document.ebkSearch.SEARCH.focus();
	 return false;
	}
  else { return true; }
}

function validate_form_jnlSearch()
{
   if (validate_required(document.jnlSearch1.SearchTerm,"Please enter title or subject keyword(s) to search!")==false)
     document.jnlSearch1.SearchTerm.focus()
  else {
  	document.jnlSearch2.SEARCH.value = "t:(" + document.jnlSearch1.SearchTerm.value + ") or s:(" + document.jnlSearch1.SearchTerm.value + ")";
	document.jnlSearch2.submit();
	}
}

function validate_form_dbSearch()
{
   if (validate_required(document.dbSearch1.SearchTerm,"Please enter title or subject keyword(s) to search!")==false)
     document.dbSearch1.SearchTerm.focus()
  else {
  	document.dbSearch2.SEARCH.value = '(t:(' + document.dbSearch1.SearchTerm.value + ') or (s:' + document.dbSearch1.SearchTerm.value + ')) and s:("databases online" or "databases standalone")';
	document.dbSearch2.submit();
	}
}

function validate_form_siteSearch()
{
   if (validate_required(document.siteSearch.q,"Please enter keyword(s) to search this site!")==false) {
     document.siteSearch.q.focus();
	 return false; 
	} 
	else { return true; }
}
//functions for search boxes end


// hide the Search Tips box when user clicks CLOSE in the box
function closeTips() {
	document.getElementById('tips-box').style.display="none";
	return void(0);
}  // function closeTips

// hide/show the Search Tips box when user clicks the Tips link
function searchTips(paraSearchBox) {
	var tipsBox = document.getElementById('tips-box').style
	if (tipsBox.display=="none") {
		// show the tips box in a position below the search box
		tipsBox.left = $(paraSearchBox).cumulativeOffset().left;   // function defined in prototype.js library
		tipsBox.top = $(paraSearchBox).cumulativeOffset().top+30;  // function defined in prototype.js library
		tipsBox.display="block" 
	}
	else
		tipsBox.display="none";
	return void(0);
}  // function searchTips;


// functions controlling the behaviour of the tabs begin
      function showTabContent(tabID, imgID) {
        var imgFile = document.getElementById(imgID).src;
        imgFile = imgFile.replace("-over","-on");

		closeTips();  // close any Search Tips box
		        
        // ON the selected tab and SHOW its tab content
        document.getElementById(imgID).src = imgFile;
        document.getElementById(tabID).style.display = "block";
        lib_currentTab = imgID;   // save the Image ID selected (i.e. ACTIVE) 
              
        // traverse the image list, OFF other tabs and HIDE their contents 
        for (var i=0;i<arrImageID.length;i++) {
			if (arrTabID[i]!=tabID) { 
				imgFile = document.getElementById(arrImageID[i]).src;
				imgFile = imgFile.replace("-on","-off");
				document.getElementById(arrTabID[i]).style.display= "none";
				document.getElementById(arrImageID[i]).src = imgFile;
          } // if
        } // for
		// switch focus to the 1st textbox in a tab
		var div_elements = $(tabID).getElementsByTagName("*")
		for (var i=0, max_elms=div_elements.length;i<max_elms;i++) {
			if (div_elements[i].type=="text") { div_elements[i].focus(); break; }        
		}
      }  // function ShowTabContent

      function rollOver(imgID) {
        var imgFile = document.getElementById(imgID).src;

		// do not RollOver for current ACTIVE tab
        if (lib_currentTab!=imgID) {    
          imgFile = imgFile.replace("-off","-over");
          imgFile = imgFile.replace("-on","-over");
          document.getElementById(imgID).src = imgFile;
        }                   
      } // function rollOver
      
      function rollOut(imgID) {
        var imgFile = document.getElementById(imgID).src;
        if (lib_currentTab == imgID)
          imgFile = imgFile.replace("-over","-on")
        else
          imgFile = imgFile.replace("-over","-off") ;
        
        document.getElementById(imgID).src = imgFile;                   
      } // function rollOut
      
      function preloadImage() {
		// image files: XXXX-on.jpg  XXXX-off.jpg  XXXX-over.jpg (where XXXX is the prefix)
		var arrImgFile = new Array('book', 'journal', 'database', 'sitesearch');  // list of image file prefix	  
		
        if (document.images)  {
          imgPreload = new Image();
          for (var i=0;i<arrImgFile.length;i++) {
			imgPreload = new Image();
            imgPreload.src = "http://www.cityu.edu.hk/lib/images/tabs/" + arrImgFile[i] + "-on.jpg";
			imgPreload = new Image();
            imgPreload.src = "http://www.cityu.edu.hk/lib/images/tabs/" + arrImgFile[i]  + "-off.jpg";
			imgPreload = new Image();
            imgPreload.src = "http://www.cityu.edu.hk/lib/images/tabs/" + arrImgFile[i]  + "-over.jpg";
          }  // for
        }  // if
      }  // function preloadImage

      var lib_currentTab = 'imgBook';  // set the image ID of default ACTIVE Tab
      var arrImageID = new Array('imgBook', 'imgJournal', 'imgDatabase', 'imgSearch')  // image ID of each tab
      var arrTabID = new Array('tabC1', 'tabC2', 'tabC3', 'tabC4')   // tab ID
      preloadImage();
// functions controlling the behaviour of the tabs end


