function toggleBox (bar) {
	var box = bar.id+'Box';
	Element.toggle(box);
	id_name = bar.id+'_arrow'
	img_el = document.getElementById(id_name);
	//alert("hi - the id is "+id_name+" the src is "+img_el.src);
	if (Element.visible(box)) {
		Element.addClassName(bar, 'barOn');
		//Flip the arrow
		//img_id = getElementById(id_name);
		img_el.src = '../images/white-arrow-down.gif';
		//alert ("hi in visible");
	}
	else {
		Element.removeClassName(bar, 'barOn');
		//Flip the arrow
		//img_id = getElementById(id_name);
		img_el.src = '../images/white-arrow-right.gif';
		//alert ("hi from remove");
		}
	
}

function toggleBoxOn (bar) {
	var box = bar.id+'Box';
	Element.show(box);
	Element.addClassName(bar, 'barOn');
}

function show_toc(id, image) {
	var table = document.getElementById(id);
	var image = document.getElementById(image);
	if (table.style.display == 'none') {
		var current_page_id = getQueryVariable('id');
		if (current_page_id == null) {
			current_page_id = 1;
		}
		var page_str = 'index.php?id=' + current_page_id;
		setCookie("toc", 1);
		location.href = page_str;
	} else {
		table.style.display = 'none';
		image.src = '../images/contents_button_down.jpg';		
		setCookie("toc", 2);
	}
}

function load_toc_state(id, image, toc_state) {	
	var toc_state = getCookie(toc_state);
	var table = document.getElementById(id);
	var image = document.getElementById(image);	
	if (toc_state == 1) {
		table.style.display = '';
		image.src = '../images/contents_button_up.jpg';		
	} else if (toc_state == 2)   {
		table.style.display = 'none';
		image.src = '../images/contents_button_down.jpg';			
	}
	
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
}

function expandTree(folderObj) {
    var childObj;
    var i;

    //Open folder
    if (!folderObj.isOpen)
      clickOnNodeObj(folderObj)

    //Call this function for all folder children
    for (i=0 ; i < folderObj.nChildren; i++)  {
      childObj = folderObj.children[i]
      if (typeof childObj.setState != "undefined") {//is folder
        expandTree(childObj)
      }
    }
}

function collapseTree()
{
	//hide all folders
	clickOnNodeObj(foldersTree)
	//restore first level
	clickOnNodeObj(foldersTree)
}

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function getCookieValue(name) { 
	 var arg = name + "=";
	 var startName = document.cookie.indexOf(arg);
	 var start = startName + arg.length;
	 if (start != -1) // if cookie name exists
	 {
	  var end = 
		  document.cookie.indexOf (";", start);
	  if (end == -1)
		  end = document.cookie.length;
	  // the value from within the cookie
	  return document.cookie.substring(start,end);
	 } 
	 else
	  return null;
}


function deleteCookie(name) { 
   	var exp = getGMT(-1); 
   	var cval = getCookieValue (name); 
   	document.cookie = name + "=" + cval + 
            "; expires=" + exp + ";";
}

function getGMT (xDays){
   var exp = new Date();  // get current date 
   // get current time and add xDays*24hrs/day
   // *60min/hr*60sec/min*1000mSec/sec
   exp.setTime(exp.getTime() +  
              (xDays*24*60*60*1000));
   return exp.toGMTString();
}


// check that search form isn't blank
function validate_search_form() {
	if (document.getElementById('query').value == "" || document.getElementById('query').value == "Enter search terms") {
		return false;
	} else {
		return true;
	}
}

function dynamicSelect(id1, id2) {
	// Browser and feature tests to see if there is enough W3C DOM support
	var agt = navigator.userAgent.toLowerCase();
	var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	var is_mac = (agt.indexOf("mac") != -1);
	if (!(is_ie && is_mac) && document.getElementById && document.getElementsByTagName) {
		// Obtain references to both select boxes
		var sel1 = document.getElementById(id1);
		var sel2 = document.getElementById(id2);
		// Clone the dynamic select box
		var clone = sel2.cloneNode(true);
		// Obtain references to all cloned options 
		var clonedOptions = clone.getElementsByTagName("option");
		// Onload init: call a generic function to display the related options in the dynamic select box
		refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
		// Onchange of the main select box: call a generic function to display the related options in the dynamic select box
		sel1.onchange = function() {
			refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
		};
	}
}

function refreshDynamicSelectOptions(sel1, sel2, clonedOptions) {
	// Delete all options of the dynamic select box
	while (sel2.options.length) {
		sel2.remove(0);
	}
	// Create regular expression objects for "select" and the value of the selected option of the main select box as class names
	var pattern1 = /( |^)(select)( |$)/;
	var pattern2 = new RegExp("( |^)(" + sel1.options[sel1.selectedIndex].value + ")( |$)");
	// Iterate through all cloned options
	for (var i = 0; i < clonedOptions.length; i++) {
		// If the classname of a cloned option either equals "select" or equals the value of the selected option of the main select box
		if (clonedOptions[i].className.match(pattern1) || clonedOptions[i].className.match(pattern2)) {
			// Clone the option from the hidden option pool and append it to the dynamic select box
			sel2.appendChild(clonedOptions[i].cloneNode(true));
		}
	}
}

/*
Prototype and Scriptaculous extensions and mods
*/

Ajax.Responders.register({
  onCreate: function() {
    document.body.style.cursor = 'wait';
  },
  onComplete: function() {
    document.body.style.cursor = 'default';
  }
});
