/*
	Author:	Charles McPhate, JFM
	Date:	11/08/02
	This script inserts the first day of the current month and
	the last day of the current month into Range forms in NetLink.

	To use this script:
		1. Name the form on your page "Ranges".
		2. Name the field to contain the first day of the month "DATE_START".
		3. Name the field to contain the last day of the month "DATE_END."
		4. Insert the following line into the <head> section of your page:
			<script src="custom/insertdates.js" language="javascript"
				type="text/javascript"></script>
		5. Make sure the src location and filename in step 4 are correct for this file.
		6. Insert the following attribute into the <body> tag:
			onLoad="insertDates();"
			Example: <body onLoad="insertDates();">
*/

function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function insertDates() {
	today = new Date();
	month = today.getMonth()+1;
	year = Right(today.getYear(), 2);

	var lastDay;
	if (month == 4 || month == 6 || month == 9 || month == 11)
		lastDay = 30;
	else if (month == 2) {
		if (year % 400 == 0)
			lastDay = 29;
		else if (year % 100 == 0)
			lastDay = 28;
    	else if (year % 4 == 0)
    		lastDay = 29;
    	else
    		lastDay = 28;
    	}
	else
		lastDay = 31;

	document.Ranges.DATE_START.value = month + "01" + year;
	document.Ranges.DATE_END.value = month + "" + lastDay + "" + year;
}

var str;
insertDates2()
function insertDates2() {
	today = new Date();
	month = today.getMonth()+1;
	year = today.getYear();

	if (year < 1900) {
		year = year + 1900
	}

	var lastDay;
	if (month == 4 || month == 6 || month == 9 || month == 11)
		lastDay = 30;
	else if (month == 2) {
		if (year % 400 == 0)
			lastDay = 29;
		else if (year % 100 == 0)
			lastDay = 28;
	else if (year % 4 == 0)
		lastDay = 29;
	else
		lastDay = 28;
	}
	else
		lastDay = 31;


	str = "<a href='http://@VAR_FORM_ACTION_URL_@?SESSION_NO=@VAR_SESSION_NO_@&REQUEST_ID=JFSVCDAY&TECH_NO_START=@VAR_TECH_NO_@&TECH_NO_END=@VAR_TECH_NO_@&DATE_START=" + month + "/01/" + year + "&DATE_END=" + month + "/" + lastDay + "/" + year +"' class='black'>";
}

// Alternating Row Colors

function getElementsByClassName(ClassName,tagName,parentElement)
{
	var elements=new Array();
	var d=parentElement ? parentElement : document;
	var allElements;

	if(tagName)
		allElements=d.all && d.all.tags(tagName) || d.getElementsByTagName && d.getElementsByTagName(tagName);
	else
		allElements=d.all || d.getElementsByTagName("*");

	for(var i=0,len=allElements.length; i<len; i++)
		if(allElements[i].className==ClassName)
			elements[elements.length]=allElements[i];

	return elements;
}

function altrows(classname,firstcolor,secondcolor)
{

	firstcolor = "#E8F3FF";
	secondcolor = "#FFFFFF";
	var tableElements = getElementsByClassName(classname) ;
	var table = tableElements[tableElements.length - 1] ;

	var exists = table;
	if (exists)
	{
		rows = table.getElementsByTagName("TR") ;
		var counter = 0
		var numRows = rows.length;

		for( i = 0; i <= numRows; i=i+2)
		{
			if (rows[i])
				rows[i].bgColor = firstcolor ;
			if (rows[i+1])
				rows[i+1].bgColor = secondcolor ;
		}
	}
}

// New Alternating Row Colors Method

function alternateRowColors() {
	var className = 'default';
	var rowcolor = '#E8F3FF';
	var rows, arow;
	var tables = document.getElementsByTagName("table");
	var rowCount = 0;
	for(var i=0;i<tables.length;i++) {
		if(tables.item(i).className == className) {
			atable = tables.item(i);
			rows = atable.getElementsByTagName("tr");
			for(var j=0;j<rows.length;j++) {
				arow = rows.item(j);
				if(arow.nodeName == "TR") {
					if(rowCount % 2)
					{
						arow.style.backgroundColor = tables.item(i).style.backgroundColor;
					}
					else
					{
						arow.style.backgroundColor = rowcolor;
					}
					rowCount++;
				}
			}
			rowCount = 0;
		}
	}
}

function alternateRowColors2() {
	var className = 'default2';
	var rowcolor = '#E8F3FF';
	var rows, arow;
	var tables = document.getElementsByTagName("table");
	var rowCount = 0;
	for(var i=0;i<tables.length;i++) {
		if(tables.item(i).className == className) {
			atable = tables.item(i);
			rows = atable.getElementsByTagName("tr");
			for(var j=0;j<rows.length;j++) {
				arow = rows.item(j);
				if(arow.nodeName == "TR") {
					if(rowCount % 2)
					{
						arow.style.backgroundColor = tables.item(i).style.backgroundColor;
					}
					else
					{
						arow.style.backgroundColor = rowcolor;
					}
					rowCount++;
				}
			}
			rowCount = 0;
		}
	}
}

// Highlight on Rollover effect

var highlightcolor = "yellow"

var ns6 = document.getElementById && !document.all
var ie = document.all

var myOriColor
var myElement

function changeto(e)
{

	source = ie ? event.srcElement : e.target

	if (source.tagName == "TABLE" || source.tagName == "TR" || source.tagName == "P" || source.tagName == "HTML" || source.tagName == "" || source.tagName == "CAPTION" || source.tagName == "H6")
		return

	while (source.tagName != "TD" && source.tagName != "TH" && source.tagName != "HTML")
		source = ns6 ? source.parentNode : source.parentElement

	if (ie && source.parentElement.style.backgroundColor != highlightcolor)
		myOriColor = source.parentElement.style.backgroundColor

	if (ns6 && source.parentNode.style.backgroundColor != highlightcolor)
		myOriColor = source.parentNode.style.backgroundColor

	if (ns6)
		source.parentNode.style.backgroundColor = highlightcolor
	else
		source.parentElement.style.backgroundColor = highlightcolor

}

function contains_ns6(master, slave)
{ //check if slave is contained by master
	while (slave.parentNode)
		if ((slave = slave.parentNode) == master)
			return true;

	return false;
}

function changeback(e)
{
	if (ie && (event.fromElement.contains(event.toElement) || source.contains(event.toElement)) || source.tagName == "TABLE")
		return
	else if (ns6 && e.relatedTarget && (contains_ns6(source, e.relatedTarget)))
		return

	if (ie)
		source.parentElement.style.backgroundColor = myOriColor
	else
		source.parentNode.style.backgroundColor = myOriColor
}


//
// global variables
//
var tbody=null;
var theadrow=null;
var colCount = null;


var reverse = false;
var lastclick = -1;					// stores the object of our last used object

var oTR = null;
var oStatus = null;
var none = 0;

function init() {

	// get TBODY - take the first TBODY for the table to sort
	tbody = element.tBodies(0);
	if (!tbody) return;

	//Get THEAD
	var thead = element.tHead;
	if (!thead)  return;

	theadrow = thead.children[0]; //Assume just one Head row
	if (theadrow.tagName != "TR") return;

	theadrow.runtimeStyle.cursor = "hand";

	colCount = theadrow.children.length;

	var l, clickCell;
	for (var i=0; i<colCount; i++)
	{
		// Create our blank gif
		l=document.createElement("IMG");
		l.src="images/dude07232001blank.gif";
		l.id="srtImg";
		l.width=25;
		l.height=11;

		clickCell = theadrow.children[i];
		clickCell.selectIndex = i;
		clickCell.insertAdjacentElement("beforeEnd", l)
		clickCell.attachEvent("onclick", doClick);
	}

}

//
// doClick handler
//
//
function doClick(e)
{
	var clickObject = e.srcElement;

	while (clickObject.tagName != "TH")
	{
		clickObject = clickObject.parentElement;
	}


	// clear the sort images in the head
	var imgcol= theadrow.all('srtimg');
	for(var x = 0; x < imgcol.length; x++)
		imgcol[x].src = "images/dude07232001blank.gif";

	if(lastclick == clickObject.selectIndex)
	{
		if(reverse == false)
		{
			clickObject.children[0].src = "images/dude07232001down.gif";
		      reverse = true;
		}
		else
		{
			clickObject.children[0].src = "images/dude07232001up.gif";
			reverse = false;
		}
	}
	else
	{
		reverse = false;
		lastclick = clickObject.selectIndex;
		clickObject.children[0].src = "images/dude07232001up.gif";
	}

	insertionSort(tbody, tbody.rows.length-1,  reverse, clickObject.selectIndex);
	altrows('default','#99CCFF','#FFFFFF')
}

function insertionSort(t, iRowEnd, fReverse, iColumn)
{


    var iRowInsertRow, iRowWalkRow, current, insert;
    for ( iRowInsert = 0 + 1 ; iRowInsert <= iRowEnd ; iRowInsert++ )
    {
        if (iColumn) {
		if( typeof(t.children[iRowInsert].children[iColumn]) != "undefined")
     		      textRowInsert = t.children[iRowInsert].children[iColumn].innerText;
		else
			textRowInsert = "";
        } else {
           textRowInsert = t.children[iRowInsert].innerText;
        }

        for ( iRowWalk = 0; iRowWalk <= iRowInsert ; iRowWalk++ )
        {
            if (iColumn) {
			if(typeof(t.children[iRowWalk].children[iColumn]) != "undefined")
				textRowCurrent = t.children[iRowWalk].children[iColumn].innerText;
			else
				textRowCurrent = "";
            } else {
			textRowCurrent = t.children[iRowWalk].innerText;
            }

		//
		// We save our values so we can manipulate the numbers for
		// comparison
		//
		current = textRowCurrent;
		insert  = textRowInsert;


		//  If the value is not a number, we sort normally, else we evaluate
		//  the value to get a numeric representation
		//
		if ( !isNaN(current) ||  !isNaN(insert))
		{
			current= eval(current);
			insert= eval(insert);
		}
		else
		{
			current	= current.toLowerCase();
			insert	= insert.toLowerCase();
		}


            if ( (   (!fReverse && insert < current)
                 || ( fReverse && insert > current) )
                 && (iRowInsert != iRowWalk) )
            {
		    eRowInsert = t.children[iRowInsert];
                eRowWalk = t.children[iRowWalk];
                t.insertBefore(eRowInsert, eRowWalk);
                iRowWalk = iRowInsert; // done
            }
        }
    }
}

function showIframe(tid){
	document.getElementById(tid).style.display = "";
}

function hideIframe(tid){
	document.getElementById(tid).style.display = "none";
}