
// We can't get to onload, since it's tucked away in the header. Instead, we'll 
// just attach the event, below.

// The normal (Firefox, Opera) way
if(window.addEventListener)
	window.addEventListener('load',doSomething,false);

// For IE, which has to be different
if(window.attachEvent)
	window.attachEvent('onload',doSomething);

function doSomething() {
	document.forms[2].elements[1].focus();
	resetAll(document);

}



// jump to search page
function jump(val) {

	window.location = val;

}




// flips the search tabs on index.php

function select(stubid) {

	var STUB_MAX = 3;

	var num = parseInt(stubid.charAt(4));
	
	document.getElementById("tab"+num).className = "tab seeit";
	document.getElementById("stub"+num).className = "pushbutton front";

	for(var i = 1; i <= STUB_MAX; i++) {
		
		if(!(i == num)) {
			document.getElementById("stub"+i).className = "pushbutton back";
			document.getElementById("tab"+i).className = "tab dont";
		}
	}

	document.forms[1+num].elements[1].focus();
}



// This function is used to show/hide the second entry field for ints and dates
// (When you select "between" as the comparison operator)
function report2(val)
{
	var attStat = document.forms['query2'].elements["field_"+val+"_match"].value;

	//alert(attStat);

	if(attStat == "range") {
		document.getElementById("show_"+val).style.display = "inline";
		if(val == "date")
			document.getElementById("date_prefix").style.display = "inline";
	} else {
		document.getElementById("show_"+val).style.display = "none";
		if(val == "date")
			document.getElementById("date_prefix").style.display = "none";

	}
}




// This function is used to show/hide the second entry field for ints and dates
// (When you select "between" as the comparison operator)
function report(val)
{
	var attStat = document.forms['query'].elements["field_"+val+"_match"].value;
	var attTxt = document.forms['query'].elements["field_"+val+"_match"].value;
	//alert(attTxt);

	//alert(attStat);

	if(attStat == "range" || attStat == "rangem" || attStat == "rangey") {
		document.getElementById("show_"+val).style.display = "inline";
		if(val == "date") {
			document.getElementById("date_prefix").style.display = "inline";

			switch(attStat) {
				case "range":
					document.getElementById('month1_' + val).style.display = "inline";
					document.getElementById('day1_' + val).style.display = "inline";
					document.getElementById('month2_' + val).style.display = "inline";
					document.getElementById('day2_' + val).style.display = "inline";
					break;
				case "rangem":
					document.getElementById('month2_' + val).style.display = "inline";
					document.getElementById('month1_' + val).style.display = "inline";
					document.getElementById('day1_' + val).style.display = "none";
					document.getElementById('day1_' + val).value = '1';
					document.getElementById('day2_' + val).style.display = "none";
					document.getElementById('day2_' + val).value = '28';
					break;
				case "rangey":
					document.getElementById('month2_' + val).style.display = "none";
					document.getElementById('month2_' + val).value = '12';
					document.getElementById('month1_' + val).style.display = "none";
					document.getElementById('month1_' + val).value = '1';
					document.getElementById('day1_' + val).style.display = "none";
					document.getElementById('day1_' + val).value = '1';
					document.getElementById('day2_' + val).style.display = "none";
					document.getElementById('day2_' + val).value = '28';
					break;
			}
			
		}
	} else {
		document.getElementById("show_"+val).style.display = "none";
		if(val == "date") {
			document.getElementById("date_prefix").style.display = "none";
			document.getElementById('month1_' + val).style.display = "inline";
			document.getElementById('day1_' + val).style.display = "inline";
			document.getElementById('month2_' + val).style.display = "inline";
			document.getElementById('day2_' + val).style.display = "inline";
		}
	}
}



// This function highlights the rows of the results table as check their checkboxes
function flip(num)
{
	
	if(document.getElementById("chk"+num).checked)
		document.getElementById("row"+num).className = "selected";
	else
		document.getElementById("row"+num).className = "unselected";
}


// When we reload the page, we want to make sure that any rows with checked boxes
// are immediately highlighted, as the 'onChange' event will not trigger.
function resetAll(n) {                         // n is a Node 
  if (n.nodeType ==1 && n.id.indexOf("row") == 0) {
    if(document.getElementById("chk"+(n.id.substring(3))).checked) {
	  n.className = "selected";
	  return;
	}
  }

  var children = n.childNodes;                // Now get all children of n
  for(var i=0; i < children.length; i++) {    // Loop through the children
    resetAll(children[i]);      // Recurse on each one
  }
  return;
}




// Shortcut function
function check() {
	checkall(document,true);
}

function uncheck() {
	checkall(document,false);
}

// Provides functionality for the "Check All" button at the bottom of the search page
function checkall(n,val) {
  if (n.id && n.id.indexOf("chk") == 0) {
	n.checked = val;
  }
  if (n.id && n.id.indexOf("row") == 0) {
	n.className = (val ? "selected" : "unselected");
  }

  var children = n.childNodes;                // Now get all children of n
  for(var i=0; i < children.length; i++) {    // Loop through the children
    checkall(children[i],val);      // Recurse on each one
  }
  return;
}


