
function validateForm(oForm) {
    if (document.getElementById) {
	var oDHTMLel = document.getElementById('dhtml_submit_message');

	if (oDHTMLel) {
	    var bShowMsg = true;
	    if (oForm.submitButton) {
		if (oForm.submitButton.name == 'loaddraft') {
		    bShowMsg = false;
		}
	    }
	    if (bShowMsg) oDHTMLel.innerHTML = '<p><strong>Please wait while your details are uploaded.</strong><br/>Do not navigate away until you receive confirmation!</p>';
	}
    }
}


var popupWindow; 
function openWin(name, url, w, h, opts) {
    if ((!popupWindow || popupWindow.closed) && window.name != name) { 
	// open new window
	popupWindow = window.open( url, name, 'width='+w+',height='+h+','+opts);
    } else if (window.name == name) {
	// re-use the current window
	window.resizeTo(w, h);
	window.location.href = url;
	window.focus();
    } else {
	// re-use the child window
	popupWindow.resizeTo(w, h);
	popupWindow.location.href = url;
	popupWindow.focus();
    }
}

/* Extendable tables */

function addRow(sID) {
    if (document.getElementById && document.getElementsByTagName) {
	// We have DOM.
	var oTableElement = document.getElementById(sID);
	var oTBodyElements = oTableElement.getElementsByTagName("tbody");
	var rows = oTBodyElements[0].getElementsByTagName("tr");
	var lastRowVisible = 0;
	for (var i=0; rows[i]; i++) { 
	    rowVisible = (rows[i].className == 'visible'); 
	    if ( rowVisible ) {lastRowVisible = i; } 
	}
	if (rows[lastRowVisible+1]) {
	    rows[lastRowVisible+1].className = 'visible';
	}
	return false;
    } else {
	// This will cause the server-side to work instead.
	return true;
    }
}

function tidyRows(sID) {
    if (document.getElementById && document.getElementsByTagName) {
	// We have DOM.
	var oTableElement = document.getElementById(sID);
	var oTBodyElements = oTableElement.getElementsByTagName("tbody");
	var rows = oTBodyElements[0].getElementsByTagName("tr");
	var nonemptyRows = new Array();
	var emptyRows = new Array();
	// sort into empty and non-empty rows
	for (var i = 0, inputs, rowEmpty; rows[i]; i++) { 
	    inputs = rows[i].getElementsByTagName("input");
	    rowEmpty = true;
	    for (var j = 0; j < inputs.length; j++) {
		if (inputs[j].value != '') rowEmpty = false;
	    }
	    if (rowEmpty) {
		emptyRows[emptyRows.length] = rows[i];
	    } else {
		nonemptyRows[nonemptyRows.length] = rows[i];
	    }
	}
	// add back to table in new order, with appropriate visibility.
	var oNewTBodyElement = document.createElement("tbody");

	for (var i = 0; nonemptyRows[i]; i++) { 
	    nonemptyRows[i].className="visible";
	    oNewTBodyElement.appendChild(nonemptyRows[i].cloneNode(true));
	}
	for (var i = 0; emptyRows[i]; i++) { 
	    if ( (i!=0 && nonemptyRows.length==0 ) || (nonemptyRows.length!=0)) {
		emptyRows[i].className="hidden";
	    }
	    oNewTBodyElement.appendChild(emptyRows[i].cloneNode(true));
	}
	oExistingTBody = oTableElement.getElementsByTagName("tbody");
	oTableElement.removeChild(oExistingTBody[0]);
	oTableElement.appendChild(oNewTBodyElement);

	return false;
    } else {
	// This will cause the server-side to work instead.
	return true;
    }

}

function tidyAll() {
    if (document.getElementById && document.getElementsByTagName) {
	oTables = document.getElementsByTagName("table")
	for (var i = 0, sID; oTables[i]; i++) { 
	    if (oTables[i].className == "extendable") {
		sID = oTables[i].getAttribute('id');
		tidyRows(sID);
	    }
	}
    }
}

function clearRow(sID, objAnchor) {
    if (document.getElementById && document.getElementsByTagName) {
	// We have DOM. Clear values, and any errors.
	inputs = objAnchor.parentNode.parentNode.getElementsByTagName("input");
	spans = objAnchor.parentNode.parentNode.getElementsByTagName("span");
	for (var j = 0; j < inputs.length; j++) {
	    inputs[j].value = '';
	    if (inputs[j].className == 'error') {
		inputs[j].className = '';
	    }
	}
	for (var j = 0; j < spans.length; j++) {
	    if (spans[j].className == 'error_msg') {
		spans[j].className = '';
		spans[j].innerHTML = '';
	    }
	}
	return false;
    } else {
	// This will cause the server-side to work instead.
	return true;
    }
}


/* General presentation */

function rollover(n)
{
  // Being for the benefit of Mr. Kite
  if(!n){return;}
  var lis=n.getElementsByTagName('li');
  for (var i=0;i<lis.length;i++)
  {
    lis[i].onmouseover=function()
    {
      this.className=this.className?'current':'over';
    }
    lis[i].onmouseout=function()
    {
       this.className=this.className=='current'?'current':'';
    }
  }
}



function addHandlers() {
  if(!document.getElementById || !document.createTextNode){return;}
  var n=document.getElementById('nav'); rollover(n);
  n=document.getElementById('col1'); rollover(n);

  n=document.getElementById('search');
  if (n) {
      n.onmouseover=function() {
	  this.style.backgroundPosition = 'bottom left';
      }
      n.onmouseout=function() {
	  this.style.backgroundPosition = 'top left';
      }
  }

  var n=document.getElementById('search_term');
  if (n) {
      n.onfocus=function() {
	  if(this.value=='Find an expert') this.value='';
      }
      n.onblur=function() {
	  if(this.value=='') this.value='Find an expert';
      }
  }

  var n=document.getElementById('searchform');
  if (n) { 
      n.onsubmit = function() {
	  var st=document.getElementById('search_term');
	  if(st.value=='Find an expert') {
	      alert("Please enter a search term before submitting your search.");
	      return false;
	  }
	  return true;
      }  
  }
  var n=document.getElementById('loaddraft');
  if (n) { 
      n.onclick = function() {
	  this.form.submitButton = this;
      }  
  }

}


/* For filterable tables */

function refreshFilterTypes(relEl) {
  // works in tandem with PHP to display only appropriate filters depending on the variable type.
  fieldType = mapFieldToType();
  filters = new Array('eq','ne','ct','nc','lt','gt','le','ge','ba','bn'); // All by default
  switch (fieldType) {
  case 'text': filters = new Array('eq','ne','ct','nc'); break;
  case 'numeric': filters = new Array('eq','ne','lt','gt','le','ge'); break;
  case 'logical': filters = new Array('eq','ne','lt','gt','le','ge','ba','bn'); break;
  }

  var first = -1; 
  for (i=0; i<relEl.length; i++) {
    found = false;
    for (j=0; j<filters.length; j++) {
      if (relEl[i].value == filters[j]) { 
	  found = true;
	  if (first==-1) first = i;
      }
    }
    if (first!=-1) relEl.selectedIndex = first;
    if (found) relEl[i].style.display='block'; else relEl[i].style.display='none';
  }
}

/* For collapsible elements */

function showAllCollapsible() {
    if (document.getElementById && document.getElementsByTagName) {
	oEls = document.getElementsByTagName("div")
	for (var i = 0, sID; oEls[i]; i++) { 
	    if (oEls[i].className == "collapsible") {
		oEls[i].lastChild.className = 'uncollapsed';
		oEls[i].firstChild.lastChild.innerHTML =  '<a onclick="hideCollapsible(this); return false;">Hide</a>'; // should be the 'control' div
	    }
	}
    }
}

function hideAllCollapsible() {
    if (document.getElementById && document.getElementsByTagName) {
	oEls = document.getElementsByTagName("div")
	for (var i = 0, sID; oEls[i]; i++) { 
	    if (oEls[i].className == "collapsible") {
		oEls[i].lastChild.className = 'collapsed';
		oEls[i].firstChild.lastChild.innerHTML =  '<a onclick="showCollapsible(this); return false;">Show</a>'; // should be the 'control' div
	    }
	}
    }
}

function toggleAllCollapsible(el) {
    if (el.expanded) {
	el.expanded = false;
	el.innerHTML = "(Expand all)";
	hideAllCollapsible();
    } else {
	el.expanded = true;
	el.innerHTML = "(Collapse all)";
	showAllCollapsible();
    }
}

function showCollapsible(el) {
    el.parentNode.parentNode.parentNode.lastChild.className = 'uncollapsed';
    el.parentNode.parentNode.parentNode.firstChild.lastChild.innerHTML =  '<a onclick="hideCollapsible(this); return false;">Hide</a>'; 
}

function hideCollapsible(el) {
    el.parentNode.parentNode.parentNode.lastChild.className = 'collapsed';
    el.parentNode.parentNode.parentNode.firstChild.lastChild.innerHTML =  '<a onclick="showCollapsible(this); return false;">Show</a>'; 
}


function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}


getElementsByClassName = function (needle)
{
  var         my_array = document.getElementsByTagName("*");
  var         retvalue = new Array();
  var        i;
  var        j;

  for (i = 0, j = 0; i < my_array.length; i++)
  {
    var c = " " + my_array[i].className + " ";
    if (c.indexOf(" " + needle + " ") != -1)
      retvalue[j++] = my_array[i];
  }
  return retvalue;
}


addEvent(window, 'load', function()
{
    hideAllCollapsible();
    tidyAll();
    addHandlers();
});


