/**
 * Contains the core javascript functions that can be used
 * in a variety of pages.
 * 
 * @author Craig Morris <craig.michael.morris@gmail.com>
 * @version 2.0
 * @package ParadigmJavascript
 */

/**
 * Finds the first parent of an element
 * matching the tagname
 * @param Element element The lonely child element
 * @param string tagname Type of paren to look for eg. table
 * @return Element The first matching parent matching tagname
 */
function find_ancestor(element, tagname)
{
	if (element.tagName=='BODY') return false;
	if (element.parentNode.tagName==tagname.toUpperCase())
	{
		return element.parentNode;
	}
	else
	{
		return find_ancestor(element.parentNode, tagname);
	}
}
	
/**
 * Sets all checboxes that are children of parent to be 
 * the same state as the control_box
 * @param Input control_box The checkbox to copy state from
 * @param Element parent The element containing child checbkoxes
 */
function select_all(control_box, parent) 
{
	var boxes = parent.getElementsByTagName('input');
	var control_state = control_box.checked;
	
	for (i=0; i < boxes.length; i++) 
	{
		if (boxes[i].type == 'checkbox' && boxes[i]!=control_box && boxes[i].name.indexOf(exception) == -1) 
		{
			if (boxes[i].checked != control_state) 
			{
				boxes[i].click();
			}
		}
	}
}
	
/**
 * Toggles the display of an element. If the object
 * is shown it is hidden, if it is hidden it is shown
 * @param Element element The object to hide / show
 */
function toggle_display(element) 
{
	if ( element.style.display=="none")
	{
		element.style.display == "";
	}
	else
	{
		element.style.display = "none";
	}
}

/**
 * Displays a simple dialog box confirming an action
 * before forwarding to a URL.
 * @param string url The url to forward to IF 'OK' is clicked
 */
function confirm_delete(url) 
{
	if (window.confirm('Are you sure?')) 
	{
		location.href = url;
	}
}

/**
 * Loads options into a select element. The values
 * and labels should be a comma delimited strings
 * ( with matching positions ). Replaces the existing
 * options, and then fires the onchange of the destination
 * select box.
 
 * @param string selectID ID of the select element
 * @param string values Comma delimitered list of values
 * @param string labels Comma delimitered list of labels
 */
function loadOptions(selectID, values, labels)
{
	// Delete all existing options (except 1st which is a description)
	//alert("loadOptions('" + selectID + "', '" + values + "', '" + labels + "')");
	
	var objSelect = document.getElementById(selectID);
	var arrValues = values.split(",");
	var arrLabels = labels.split(",");
	
	objSelect.options.length = 0;
	for (i=0; i < arrValues.length; i++)
	{
	  var objOption = new Option(arrLabels[i], arrValues[i]);
	  objSelect.options.add(objOption);
	}
	
	// we have to fire the on change event manually
	objSelect.onchange();
}

/**
 * Select a value in a select element by its value
 * @param Select dest The element to change selection
 * @param string value The value to select if present.
 */
function select_from_value(dest, value)
{
	for (i=0; i < dest.options.length; i++)
	{
		if ( dest.options[i].value == value )
		{
			dest.selectedIndex = i;
			return;
		}
	}
}

/**
 * Popups a new a window with width and height
 * with default parameters. No location, menubar or toolbars.
 * Can be resized and scrolled.
 * @param string url The url of the popup window
 * @param int width The width of the popup window
 * @param int height The height of the popup window
 */
function popup(url, width, height)
{
	var win = window.open(url, 'popup', 'width='+width+',height='+height+',location=no,menubar=no,toolbar=no,resizable=yes');
	win.focus();
}

/**
 * Returns true if a series of checkbox or radio button is 
 * selected
 * Usually pass it document.form.checkboxes which will be an array.
 * @param array arrElements Array of elements to check.
 * @return bool True if any are selected, false otherwise.
 */
function is_one_selected(arrElements)
{
	if (arrElements.checked)
		return true;
		
	for (i=0; i < arrElements.length; i++)
	{
		if ( arrElements[i].checked )
			return true;
	}
	
	return false;
}

/**
 * Macromedia's bulit in restore image function
 */
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;
}

/**
 * Macromedia's bulit in prelaod function
 */
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];}}
}

/**
 * Macromedia's bulit in find obj function
 */
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;
}

/**
 * Macromedia's bulit in swap image function
 */
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];}
}