// ###########################################################
// Generic Javascript Functions Collection
// Martyn Green, June 2005
// ###########################################################

var isDHTML = 0;
var isID = 0;
var isAll = 0;
var isLayers = 0;

// Detect browser type
if (document.getElementById) {isID = 1; isDHTML = 1;}
else {
if (document.all) {isAll = 1; isDHTML = 1;}
else {
browserVersion = parseInt(navigator.appVersion);
if ((navigator.appName.indexOf('Netscape') != -1) && (browserVersion == 4)) {isLayers = 1; isDHTML = 1;}
}}

// ###########################################################
// Define Prototype functions
// ###########################################################
String.prototype.trim = function () {
	  return this.replace(/^\s+/,'').replace(/\s+$/,'');
 }
 
String.prototype.escapeHtml = function () {   
	  return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
}

String.prototype.startsWith = function (aStr) {
    if (aStr == null) return false;
    return this.substr(0, aStr.length) == aStr;
}
String.prototype.simpleReplaceAll = function (oldVal, newVal) {
	  var result = this
	  while (result.indexOf(oldVal) > -1) {
		   result = result.replace(oldVal, newVal);
	  }

	  return result
}

function findDOM(objectID,withStyle) {
// Locate object in Document Object Model
// ###########################################################
// objectID : [String] ID of object to locate
// withStyle : [boolean] whether to return style collection or entire object
// returns : located object
// ###########################################################

	if (withStyle == 1) {
		if (isID) { return (document.getElementById(objectID).style) ; }
		else {
			if (isAll) { return (document.all[objectID].style); }
		else {
			if (isLayers) { return (document.layers[objectID]); }
		};}
	}
	else {
		if (isID) { return (document.getElementById(objectID)) ; }
		else {
			if (isAll) { return (document.all[objectID]); }
		else {
			if (isLayers) { return (document.layers[objectID]); }
		};}
	}
}

function confirmDelete(name) {
// Request user confirm before action
// ###########################################################
// name: [String] text to display in alert message
// returns : [boolean] users response
// ###########################################################

	return confirm('Are you sure you want to delete ' + name + '?');

}

function showHide(itemName,state) {
// Show of hide a given ID
// ###########################################################
// itemname: [String] ID of target item
// state: ["display" | "none"] to show or hide the target item
// returns : [boolean] users response
// ###########################################################

	dom = findDOM(itemName,1);
	dom.display = state;

}

function switchClass(itemName, className) {
// Switch the class of a given ID
// ###########################################################
// itemname: [String] ID of target item
// className: [String] name of new class to set item to
// returns : [boolean] users response
// ###########################################################

	dom = findDOM(itemName,0);
	dom.className = className;

}

function autoGen(tempText, summaryLength) {
// remove all html tags from a supplied string and return a summary of fixed length
// ###########################################################
// tempText: [String] text to operate on
// summaryLength: [integer] max. number of characters to return (once tags are stripped)
// returns : [String] trimmed, de-tagged string of desired max. length.
// ###########################################################

	var newString = "";
	var inTag = false;

	if (tempText.length > 0) { 

		for(var i = 0; i < tempText.length; i++) {

			if(tempText.charAt(i) == '<') inTag = true;

			if(tempText.charAt(i) == '>') {
				inTag = false;
				continue;
			}

			if(!inTag)
				newString += tempText.charAt(i);

			if (summaryLength > 0 && newString.length == summaryLength) {
				 newString += "...";
				 break;
			}

		//Set summary text field
		}
		newString = newString.trim()

		newString = newString.simpleReplaceAll("&nbsp;", ' ')
		newString = newString.simpleReplaceAll("&amp;", '&')
		newString = newString.simpleReplaceAll("&lt;", '<')
		newString = newString.simpleReplaceAll("&gt;", '>')

	} else {

		newString = "";
	}

	return newString;

}

  // this function is needed to work around
  // a bug in IE related to element attributes
  function hasClass(obj) {
     var result = false;
     if (obj.getAttributeNode("class") != null) {
         result = obj.getAttributeNode("class").value;
     }
     return result;
  }

function stripe(refClass) {
// style tables with alternating row colors.
// ###########################################################
// refClass: all tables with this class in the main document will be styled.
// evenColor: first alternating row color (#hex)[default white]
// oddColor: second alternating row color (#hex)[default gray]
// returns: nothing.
// ###########################################################

	var evenColor = arguments[1] ? arguments[1] : "#fff";
	var oddColor = arguments[2] ? arguments[2] : "#eee";
	var table = arguments[3] ? arguments[3] : null;
	var styleTables = new Array();
	
	if (table==null)
		styleTables = document.getElementsByTagName("table");
	else {
		styleTables[0] = table;
	}
	

	// loop through all tables in the document
	for (var k = 0; k < styleTables.length; k++) {

		var even = false;

		if (styleTables[k].className == refClass) {

			var tbodies = styleTables[k].getElementsByTagName("tbody");
			for (var h = 0; h < tbodies.length; h++) {

				var trs = tbodies[h].children;
				for (var i = 0; i < trs.length; i++) {

					if (trs[i].tagName.toLowerCase() == "tr" && trs[i].style.display != "none") {
						
						if ( ! trs[i].style.backgroundColor) {
	
							var tds = trs[i].getElementsByTagName("td");
							for (var j = 0; j < tds.length; j++) {
	
								var mytd = tds[j];
								if ((! hasClass(mytd) || hasClass(mytd) == "mousehand") &&
								(! mytd.style.backgroundColor || table!=null)) {
	
								mytd.style.backgroundColor =
								even ? evenColor : oddColor;
								}
							}
						}
					
						even =  ! even;
				
					}
				  
				}
			}
		}
	}
}

function drawDrop(type, id, startVal, endVal, selectedIndex) {
// draw a drop down menu on the screen.
// ###########################################################
// type: datatype [number|day|month].
// id: id of control [string]
// startVal: beginning value of data (use 1==Monday etc.) [integer]
// endVal: end value of data (use 12==December etc) [integer]
// selectedIndex: choose which item is selected [integer]
// ###########################################################
	document.write('<select name="' + id + '">\n');

	if (type=="day") {

		dayOfWeek = new Array("", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");

		startVal = (startVal < 1) ? 1 : startVal;
		endVal = (endVal>dayOfWeek.length) ? dayofWeek.length : endVal;

		for (var i = startVal; i <= endVal; i++) {
			document.write('<option value=\"' + dayOfWeek[i] + '\" ');
			if (i==selectedIndex) document.write(' selected="selected" ');
			document.write('>');
			document.write( dayOfWeek[i] + '</option>\n');
		}

	} else if (type=="month") {

		monthOfYear = new Array("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

		startVal = (startVal < 1) ? 1 : startVal;
		endVal = (endVal>monthOfYear.length) ? monthOfYear.length : endVal;

		for (var i = 1; i < monthOfYear.length; i++) {
			document.write('<option value=\"' + i + '\" ');
			if (i==selectedIndex) document.write(' selected="selected" ');
			document.write('>');
			document.write( monthOfYear[i] + '</option>\n');
		}

	} else {

		for (var i = startVal; i <= endVal; i++) {
			document.write('<option value=\"' + i + '\" ');
			if (i==selectedIndex) document.write(' selected="selected" ');
			document.write('>');
			document.write( i + '</option>\n');
		}

	}

	document.write('</select>\n');

}

// ###########################################################
// Sticky DIV (sticks to top edge of page is scrolled down)
// ###########################################################
	function stickyHeaderCheckPos(headerObjName,lastRowName, originalHeaderPosition) 
	{
	   	var IfrRef = document.getElementById(lastRowName);
		var headerObj = document.getElementById(headerObjName);
		var pos1 = new positionInfo(headerObj);
		//var pos2 = new positionInfo(document.getElementById(lastRowName));
		
		var bodyY;
		
		if (self.pageYOffset) // all except Explorer
		{               
        		bodyY = self.pageYOffset;
		}               
		else if (document.documentElement && document.documentElement.scrollTop)   
        	// Explorer 6 Strict
		{
        		bodyY = document.documentElement.scrollTop;
		}
		else if (document.body) // all other Explorers
		{
        		bodyY = document.body.scrollTop;
		}       

		//window.status = "body position="+bodyY+", header original position="+originalYpos+", hreader current position="+headerObj.offsetTop;

		if(bodyY>originalYpos)
		{
			//alert(pos1.getElementHeight() + pos1.getElementTop() +" "+ pos2.getElementTop());
			//if ((pos1.getElementHeight() + pos1.getElementTop() < pos2.getElementTop()) || (bodyY<pos1.getElementTop()))
				//headerObj.style.top = bodyY-2;
				headerObj.style.top = bodyY-2;
			//else
			//	headerObj.style.top = pos2.getElementTop()-pos1.getElementHeight();
		}else
		{
			headerObj.style.top = originalHeaderPosition;
		}

	    headerObj.style.display = "block";

		showSubWindow(headerObj, 'fbiframe');

    	setTimeout("stickyHeaderCheckPos('"+headerObjName+"','"+lastRowName+"',"+originalHeaderPosition+")",50);
	}


