// JavaScript Document
function Trim(TRIM_VALUE){
if(TRIM_VALUE.length < 1){
return"";
}
TRIM_VALUE = RTrim(TRIM_VALUE);
TRIM_VALUE = LTrim(TRIM_VALUE);
if(TRIM_VALUE==""){
return "";
}
else{
return TRIM_VALUE;
}
} //End Function

function RTrim(VALUE){
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function

function LTrim(VALUE) {
	var w_space = String.fromCharCode(32);
	var t_space = String.fromCharCode(9);
	
	
	
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 1){
		return"";
	}
	var iTemp = 0;
	
	while(iTemp < v_length) {
		if(VALUE.charAt(iTemp) == w_space || VALUE.charAt(iTemp) == t_space){
		}
		else{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While
	return strTemp;
} //End Function


//---------------------------------------------------------------------------------------------------------------
//This is for the COMMENT POPUP
//---------------------------------------------------------------------------------------------------------------
mousex = mousey = 0;
opacity = 100; timerid = -1; timeropenid = -1; timerfadeid = -1;
var maxheight = 0;
var skn = document.getElementById("popupdiv"); //There must be a div called popupdiv
var timerid = 0;

var innerHTML = "";
function get_mouse(e) {
	//Distinguish between internet explorer and firefox
	if (document.all) {
		mousex = event.x; mousey = event.y + document.body.scrollTop;
	}
	else {
		mousex = e.pageX; mousey = e.pageY;
	}
} 

function popup(msg, pos, slidein) {
  slidein = true;
  clearInterval(timerid);
  timerid = 0;
  
  if (!pos) pos = 'right';
  
  if (pos == 'right') {
	  skn.style.left = (mousex + 10) + "px";
	  skn.style.top = (mousey + 10) + "px";
  }
  
  
  skn.style.filter = 'alpha(opacity=100)';
  skn.style.opacity = '1.0';
  skn.style.display = "none";
  opacity = 100;
  alert(skn.style.top);
	skn.style.height = 'auto';
   skn.innerHTML = msg;
	 
  if (skn["offsetWidth"] > 400) {
	  skn.style.width = "400px";
  }
  if (pos == 'left') {
	  var oldoffset = skn["offsetWidth"];
	  skn.style.left = (mousex - oldoffset) + "px";
	  skn.style.width = oldoffset + "px";
	  skn.style.top = (mousey + 10) + "px";
  }
  
 alert(1);
  opened = true;
  if (slidein) {
	  skn.style.visibility = "hidden";  
	  maxheight = skn["offsetHeight"];
	  skn.style.height = "1px";
	  timeropenid = window.setInterval("openInterval()",25);
  }skn.style.display = "visible";
  skn.style.visibility = "visible";  
}

function kill() 
{
	if (!mousedown && timerid == 0 ) {
		opacity = 100;
	 	timerid = window.setInterval("closeinterval()",50);
	}
}
function openInterval() {
	if (timeropenid != -1) {
		skn.style.height = (parseInt(skn.style.height,10) + (maxheight - parseInt(skn.style.height,10))/3) + "px";
//		alert(maxheight);
		if (parseInt(skn.style.height,10) >= maxheight) {
			skn.style.height = skn.style.maxheight;
			clearInterval(timeropenid);
			timeropenid = -1;
			timerfadeid = window.setInterval("openInterval()",100);
		}
	}
	else if (timerfadeid != -1) {
		clearInverval(timerfadeid);
	}
	
}
function closeinterval() {
	opacity -= 10;
	
	skn.style.filter = 'alpha(opacity=' + opacity +')';
	skn.style.opacity = '' + opacity / 100 + '';
	
	if (opacity <= 1) {
		clearInterval(timerid);
		timerid = 0;
		
		skn.style.visibility = "hidden";
		
		skn.style.filter = 'alpha(opacity=100)';
		skn.style.opacity = '1.0';
		
		opacity = 100;
		
		opened = false;
	}
}
function setmessage(msg) {
	skn.innerHTML = msg;
}
//---------------------------------------------------------------------------------------------------------------
//This is for fancy opening of a div
//---------------------------------------------------------------------------------------------------------------
var _maxheight = 0;

function divEffect(divid, speed) {
	var div = document.getElementById(divid);
	
  if (speed < 0 && div.style.visibility != "visible") return;
  
  clearInterval(div._timeropenid);
  div._timerid = 0;  
  div._speed = speed;

  if (typeof div._startheight == "undefined") {
  	div._startheight = div["offsetHeight"];
  }
  div.maxheight = div._startheight;
 
 
	if ( div._speed > 0) {
		div.style.height = "1px";
	}
  div._timeropenid = window.setInterval("fancyDivInterval('" + divid + "')",50);
  div.style.visibility = "visible";  

}
var curh;
var maxh;
function fancyDivInterval(divid) {
	
	var div = document.getElementById(divid);
	if (div._timeropenid == -1) return;
	curh = parseInt(div.style.height,10);
	maxh = div.maxheight;
	if (div._speed > 0) {
		div.style.height = (curh + (maxh - curh) / div._speed) + "px";
		
		if (curh >= maxh-3) {
			div.style.height = maxh + "px";
			clearInterval(div._timeropenid);
			div._timeropenid = -1;
		}
	}
	else {
		//div.style.height = (parseInt(div.style.height,10) + (parseInt(div.style.height,10))/div._speed) + "px";
		div.style.height = (curh - (curh / -div._speed)) + "px";
		
		if (curh < 2) {
			div.style.height = "0px";
			div.style.visibility = "hidden";
			clearInterval(div._timeropenid);
			div._timeropenid = -1;
		}
	}
	
}

function setmessage(msg) {
	skn.innerHTML = msg;
}
//===========================================================================================================
function hidediv(div) {
	if (!div) {
		alert("JavaScript Page Error, parameter not passed into hidediv().");
		return;
	}
	document.getElementById(div).style.display='none';
}
function showdiv(div) {
	if (!div) {
		alert("JavaScript Page Error, parameter not passed into showdiv().");
		return;
	}
	document.getElementById(div).style.display='block';
}
function toggledivshow(div) {
	if (!div) {
		alert("JavaScript Page Error, parameter not passed into showdiv().");
		return;
	}
	mydiv = document.getElementById (div);
	if(mydiv.style.display != 'block') {
		mydiv.style.display='block';
	}
	else mydiv.style.display = 'none';
}
function correctPNG() // correctly handle PNG transparency in Win IE 5.5 or higher.
   {
	for(var i=0; i<document.images.length; i++)
      {
	  var img = document.images[i]
	  var imgName = img.src.toUpperCase()
	  if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
	     {
		 var imgID = (img.id) ? "id='" + img.id + "' " : ""
		 var imgClass = (img.className) ? "class='" + img.className + "' " : ""
		 var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
		 var imgStyle = "display:inline-block;" + img.style.cssText 
		 if (img.align == "left") imgStyle = "float:left;" + imgStyle
		 if (img.align == "right") imgStyle = "float:right;" + imgStyle
		 if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle		
		 var strNewHTML = "<span " + imgID + imgClass + imgTitle
		 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
	     + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		 + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
		 img.outerHTML = strNewHTML
		 i = i-1
	     }
      }
		
   }
	
function setDateBox(box,numdays, start) {
	//Creates a select box filled with dates from now until numdays in the future
	var months = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
	if (!start) {
		var date = new Date();
		var curday = date.getDate();
		var curmonth = date.getMonth() + 1; //from 1 - 12 (getMonth returns 0-11)
		var curyear = date.getFullYear(); //a four digit year
	}
	else {
		var curday = start.getDate();
		var curmonth = start.getMonth();
		var curyear = date.getFullYear();
	}
	
	var monthformat;
	var timestampformat;
	var curmonthstr;
	var curdaystr;
	
	for (var d = 0; d < numdays; d++) {
			if (curyear % 4 == 0 ) { //Leap year
				months[2] = 29;
			}
			else months[2] = 28;

		monthformat = curmonth + "/" + curday + "/" + curyear;
		
		if (curmonth < 10) curmonthstr = "0" + curmonth;
		else curmonthstr = "" + curmonth;
		
		if (curday < 10) curdaystr = "0" + curday;
		else curdaystr = "" + curday;
		
		timestampformat = "{ts '" + curyear + "-" + curmonthstr + "-" + curdaystr + " 00:00:00'}";
		box.options[d] = new Option(monthformat, timestampformat,false,false);
		
		curday++;
		if (curday > months[curmonth]) {
			curmonth++;
			curday = 1;
		}
		if (curmonth > 12) {
			curmonth = 1;
			curyear++;
		}
	}
}
function setTimeBox(box, starthour,endhour,res, addna) {
	var hour = starthour;
	var minute = 0;
	var strhour;
	var strmin;
	var totalstr;
	var strampm;
	var d = 0;
	if (typeof addna != undefined) {
		box.options[d++] = new Option("N/A","N/A",false,false);
	}
	for (var i = 0; i <= (endhour - starthour) * 60; i+= res) {
		if (minute >= 60) {
			minute = 0;
			hour ++;
		}

		if (hour >= 24) hour -= 24;
		if (hour == 0) {
			strhour = "12";
			strampm = "AM";
		}
		else if (hour > 12) {
			strhour = (hour-12).toString();
			strampm = "PM";
		}
		else {
			strhour = hour.toString();
			strampm = "AM";
		}
		
		strmin = minute.toString();
		if (minute < 10) strmin = "0" + strmin;
		
		totalstr = strhour  + ":" + strmin + " " + strampm;
		box.options[d++] = new Option(totalstr, totalstr,false,false);
		minute += res;
		
	}
}
//This function takes the current url including the current variables and just adds another variable to the
//end of it and reloads the page. Things are passed in like ("VARIABLE", "VALUE,...)
function addURLVariables() {
	//The reason this is complicated is because we have to search through the current variables in the URL
	//and make sure that this variable isn't already listed in the URL, because if it is, we want to replace it.
	if (arguments.length == 0) return;
	
	var isVariables = document.URL.split("?")[1];

	var URLsplit = document.URL.split("?");
	var URLString = URLsplit[0] + "?";
	
	var varfound = new Array(arguments.length/2);
	for (k = 0; k < arguments.length/2; k++) {
		varfound[k] = false;
	}
	if (typeof(isVariables) != "undefined") {
		var strvars = '' + URLsplit[1];
		strvar = strvars.split("&");
		
		for (i = 0; i < strvar.length;i++) {
			for (j = 0; j < arguments.length;j+=2) {
				if (strvar[i].split("=")[0] == arguments[j]) {
					
					varfound[j/2] = true;
					strvar[i] = arguments[j] + "=" + arguments[j+1];
					continue;
				}
			}
			if (i != 0) {
				URLString += "&" + strvar[i];
			}
			else URLString += strvar[i];
		}
	}
	for (j = 0; j < arguments.length;j+=2) {
		if (!varfound[j/2]) {
			if (isVariables || j != 0) {
				URLString += "&";
			}
			URLString += arguments[j] + "=" + arguments[j+1];
		}
	}
	document.location.assign(URLString);
	
}

// -------------------------------------------------------------------
// sortSelect(select_object)
//   Pass this function a SELECT object and the options will be sorted
//   by their text (display) values
// -------------------------------------------------------------------
function sortSelect(obj) {
	var o = new Array();
	if (!hasOptions(obj)) { return; }
	for (var i=0; i<obj.options.length; i++) {
		o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
		}
	if (o.length==0) { return; }
	o = o.sort( 
		function(a,b) { 
			if ((a.text+"") < (b.text+"")) { return -1; }
			if ((a.text+"") > (b.text+"")) { return 1; }
			return 0;
			} 
	);

	for (var i=0; i<o.length; i++) {
		obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
		}
}
// -------------------------------------------------------------------
// hasOptions(obj)
//  Utility function to determine if a select object has an options array
// -------------------------------------------------------------------
function hasOptions(obj) {
	if (obj!=null && obj.options!=null) { return true; }
	return false;
	}
//This code handles the variables and functions that determine whether the mouse is down or up
//on_MouseUp is called whenever the mouse button is clicked in the document
//on_MouseDown is called whenever the mouse button goes down anywhere in the document
//These two variables can be used by any function on the page to determine the mouses current state for whatever reason.

var mouseup = true;
var mousedown = false;

function on_MouseUp() {
	mousedown = false;
	mouseup = true;
}
function on_MouseDown() {
	mousedown = true;
	mouseup = false;
}

if (!document.all) {
	//FIREFOX IS BEING USED
	window.addEventListener("load",correctPNG,false);
	document.addEventListener("mouseup",on_MouseUp,false);
	document.addEventListener("mousedown",on_MouseDown,false);
	document.addEventListener("mousemove",get_mouse,false);
}
else {
	//INTERNET EXPLORER IS BEING USED
	window.attachEvent("onload", correctPNG);
	document.attachEvent("onmouseup", on_MouseUp);
	document.attachEvent("onmousedown", on_MouseDown);
	document.attachEvent("onmousemove",get_mouse);
}
//---------------------------------------------------------------------------------------------------------------
//This is for the scroll box
//---------------------------------------------------------------------------------------------------------------
var uptimer;
var downtimer;
var scrollspeed = 30;
function scrollbox_up() {
	
	div = document.getElementById("menu_dropdown_inside");
	if (parseInt(div.style.top,10) > 10) return;
	
	div.style.top = (parseInt(div.style.top,10) + 6) + "px";
	
}
function scrollbox_down() {
	div = document.getElementById("menu_dropdown_inside");
	odiv = document.getElementById("menu_dropdown_outside");
	if (-parseInt(div.style.top,10) > parseInt(div["offsetHeight"],10) - parseInt(odiv["offsetHeight"],10)) return;
	
	div.style.top = (parseInt(div.style.top,10) - 6) + "px";
}
function scrollbox_startUp() {
	window.clearInterval(uptimer);
	uptimer = window.setInterval("scrollbox_up()",scrollspeed);
}
function scrollbox_stopUp() {
	window.clearInterval(uptimer);
}
function scrollbox_startDown() {
	window.clearInterval(downtimer);
	downtimer = window.setInterval("scrollbox_down()",scrollspeed);
}
function scrollbox_stopDown() {
	window.clearInterval(downtimer);
}