


/*
 * JavaScript functions. All kept in the same file for performance reasons.
 * This file is actually a JSP!
 *
 * $Id: tobacco.js 4563 2008-02-16 08:09:01Z waldura $
 */


/*###########################################################################
 * Utility functions.
 */
 
var common_console = null;

/****************************************************************************
 * common_debug
 *
 * Print the message in a new window.
 * 
 * @param mesg string to print to the window
 */
function common_debug(mesg)
{
	if (common_console == null || common_console.closed) 
	{
		// initialize a new console
		common_console = window.open("", "console", "width=600,height=300,resizable");
		common_console.document.open("text/plain");
		common_console.focus();
	}
	
	common_console.document.writeln(mesg);
}

/**************************************************************************** 
 * common_array2hash
 *
 * Turn an array into a hash. Each value in the array is converted to a
 * key/value pair. The key is the value from the array, and the value is
 * "true". 
 *
 * @param a an array
 * @return a hash
 */ 
function common_array2hash(a)
{
	var h = {};
	
	for (var i = 0; i < a.length; i++)
	{
		h[a[i]] = true;
	}
	
	return h;
}

/**************************************************************************** 
 * common_checkBoxes
 *
 * Check/uncheck an array of checkboxes. 
 *
 * @param boxes an array of checkboxes
 * @param flag whether to check or uncheck the boxes in the array
 * @param the list of values to check/uncheck
 */ 
function common_checkBoxes(boxes, flag, values)
{
	var hvalues;
	if (values)
	{
		// turn the "values" array into a hash for easy lookup
		hvalues = common_array2hash(values);
	} 
	 
	if (boxes.length)
	{
		for (var i = 0; i < boxes.length; i++)
		{
			if (hvalues)
			{
				boxes[i].checked = flag && hvalues[boxes[i].value];
			}
			else
			{
				boxes[i].checked = flag;
			}
		}
	}
}

/**************************************************************************** 
 * showElement
 *
 * Show an element. 
 *
 * @param id element ID on the page
 */ 
function common_showElement(id)
{
    common_showHideElement(id, true);
}

/**************************************************************************** 
 * common_hideElement
 *
 * Hide an element. 
 *
 * @param id element ID on the page
 */ 
function common_hideElement(id)
{
    common_showHideElement(id, false);
}

/**************************************************************************** 
 * common_showHideElement
 *
 * Show/hide an element depending on the passed flag. 
 *
 * @param id element ID on the page
 * @param flag true to show the element, false to hide it
 */ 
function common_showHideElement(id, flag)
{
	if (flag)
	{
		$(id).show();
	}
	else
	{
		$(id).hide();
	}
}


/*###########################################################################
 * Bookbag functions.
 * Please refer to <i>TN113 -- Bookbag</i> for more information.
 */
 
/****************************************************************************
 * bookbag_addOrRemoveAllDocuments
 *
 * Check/uncheck all bookbag checkboxes found on the current page. Used when 
 * adding an entire page of search results to the bookbag.
 */
function bookbag_addOrRemoveAllDocuments(flag)
{
    // check all bookbag checkboxes
	for (var i = 0; i < document.forms.length; i++)
	{
		var form = document.forms[i];
		 
		if (form.add)
		{
			form.add.checked = flag;
		}	
	}

	// show/hide all "annotate" links
	for (var i = 0; i < document.links.length; i++)
	{
	    var link = document.links[i];

	    if (link.id.match(/^annotateLink-/))
	    {
	        common_showHideElement(link.id, flag);
	    }
	}
}

/****************************************************************************
 * bookbag_resetUI
 *
 * Reset UI elements according to bookbag status.
 *
 * @param bookbag associative array of TIDs  
 */
function bookbag_resetUI(bookbag)
{
	// match the state of checkboxes and annotate links to the bookbag
	for (var i = 0; i < document.forms.length; i++)
	{
		var form = document.forms[i];
		 
		if (form.add && form.tid)
		{
			if (form.tid.value)
			{
			    form.add.checked = bookbag[form.tid.value];
			    common_showHideElement('annotateLink-' + form.tid.value, bookbag[form.tid.value]);
		    }
		    else	// "add all" box
		    {
		    	form.add.checked = false;
		    }
		}
	}
}

/****************************************************************************
 * bookbag_refreshBookbagUI
 *
 * Get the bookbag from the server and reset all checkboxes and annotate
 * links according to the bookbag. Called by onLoad trigger.
 * Please refer to <i>TN113 -- Bookbag</i> for more information.   
 */
function bookbag_refreshBookbagUI()
{
    // get bookbag from server & reset UI according to it
    new Ajax.Request(
        '/action/bookbag/view', 
        { 
        	method: 'get', parameters: { format: 'js' }, 
            onSuccess: function(transport, bookbag) { if (bookbag) bookbag_resetUI(bookbag) }
            // BUG #526: swallow exceptions -- Firefox generates them on network errors 
            //onException: function(req, e) { alert("Bookbag error: " + e.name + " - " + e.message) } 
		} 
	);
}


/*###########################################################################
 * Functions used by search pages. 
 */
 
/****************************************************************************
 * search_submitOnEnter
 *
 * Called on keypress event: submit the form if the Enter key was pressed.
 *
 * @param evt the UI event triggered by the keypress
 * @return true if the key pressed was not the Enter key
 */
function search_submitOnEnter(evt) 
{
	var keyCode;
	
	if (window.event) // IE
	{
		keyCode = window.event.keyCode;	
	}
	else if (evt && evt.which) // Mozilla
	{
		keyCode = evt.which;
	}
	
	if (keyCode == 13)
	{
        document.searchQuery.submitButton.disabled = true; 
        document.searchQuery.submitButton.value = 'Searching...';			
		document.searchQuery.submit();
		return false;
	}
	
	return true;
}

/****************************************************************************
 * search_setCaretTo
 *
 * Move the cursor to position "pos" in the given element.
 *
 * @param el the element where the cursor should be positioned
 * @param pos integer position
 */
function search_setCaretTo(el, pos) 
{
	if (el.createTextRange) // IE
	{
		var r = el.createTextRange();
		r.moveStart('character', pos);
		r.select();
	}
	else if (el.setSelectionRange) // Mozilla
	{
		el.setSelectionRange(pos, el.value.length);
	}
}

/****************************************************************************
 * search_setCaretToEnd
 *
 * Move the cursor to the very end of a text field element.
 *
 * @param el text field element to alter
 */
function search_setCaretToEnd(el) 
{
	search_setCaretTo(el, el.value.length);
}

/****************************************************************************
 * search_focusQueryOnError
 *
 * Focus the search query field and move the cursor to the error.
 * See bugs #129 and #168.
 *
 * @param errorColumn integer position of the error
 */
function search_focusQueryOnError(errorColumn)
{
	document.searchQuery.q.focus();

    if (errorColumn == 0)
    {
		// leave cursor where it is by default
    }
    else if (errorColumn > 1)
    {
		search_setCaretTo(document.searchQuery.q, errorColumn);    
    }		
    else // undefined
    {
		search_setCaretToEnd(document.searchQuery.q);			
    }
}


/*###########################################################################
 * Functions created DreamWeaver and used by the UI.
 */

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
