// ccccccccccccccccccc copyright© 2005 Grevillea Software cccccccccccccccccccccc 

var SelectedDept;//=document.all["div1"]
var NewWindowName = "wGSlinks";
var SubPageCurrent = "";
function window_onload()
{	var b = document.body.getElementsByTagName("div");
	var el, loc = "home.htm", pid = "home";
	////alert("loaded");//windowResize();
	document.getElementById("fBody").height = (document.body.clientHeight - 80) 
	
	for (i=0; i < b.length; i++)	{
		el = b[i];
		if (el.className == "mitem")   {
			el.onmouseover = mouseOver;
			el.onmouseout = mouseOut;
			el.onclick = menuHandler;
		}
	}	
    //window.frames[0].DocState = "Loading";
    //window.frames[0].AppState  = "Ready";
	
	//load the appropriate doc into the frame
	pid = pidGet(); //gets the pid from the URL eg ?pid=hots06 Defaults to 'home' if not supplied
	//get the location from the menu item with id=pid	
	loc = document.getElementById(pid).attributes["show"].value;
	//alert(loc);
	if (window.parent != window)  {
		//apparently trying to load this index page into the frame
		//alert("index as subpage");
		window.location = loc;
	}  else  {
		//load this page into the frame
		//alert("loc as subpage");
		showSubPage(loc);//
	}
	//document.getElementById(pid).click();
//	if(document.all)  {//detect the the type of DOM model
//		document.getElementById(pid).click();//IE
	//	m = e.srcElement;
//	}  else  {   //Mozilla
	//showSubPage();//IE
		//m = e.target;
//	}
//	pid = document.getElementById("fBody").attributes["pid"].value;
//	if (document.getElementById(pid))   {
//	    document.getElementById(pid).click();
//	}  else  {
//	    document.getElementById("home").click();
//	}
}

function frameUnLoad(e)  {
	var m;
	if(document.all)  {//detect the the type of DOM model
		e = window.event;//IE
		m = e.srcElement;
	}  else  {   //Mozilla
		m = e.target;
	}
    alert("unloading - framed " + m.document.location);
   // alert("unloading...");

}
function windowResize() {
	var fwidth, url;	//alert("Resize");
	if (top.window.frames.length != 0) {
		//alert("Resizing----");
		document.getElementById("fBody").height = (document.body.clientHeight - 80) //+ "px"; 
		
		if (window.frames[0].document.body.attributes["framesize"])   {
			if (window.frames[0].document.body.attributes["framesize"].value == "max")  {
				fwidth = (document.body.clientWidth - 165);
			}  else  {  fwidth = 540;  }
		}  else  {  fwidth = 540;  }
		if(document.all)  {//detect the type of DOM model
			fwidth = fwidth + 5;
		}
		document.getElementById("fBody").width = fwidth;
	}	//alert("Resized=====" + window.frames[0].document.location);//body.clientWidth);
}
function pidGet(){//gets the pid from the url if its a GET var
   var pid = "home";
   var sLocation;
	if(document.all)  {//detect the type of DOM model
		sLocation = window.location.href;
	}  else  {   //Mozilla
		sLocation = window.location.href;
	}
   var sData = sLocation.substring(sLocation.indexOf("?") + 1,sLocation.length);
   aData = sData.split("&");
   for(var i = 0;i < aData.length;i++){
      var sName = aData[i].substring(0,aData[i].indexOf("="));
      var sValue = aData[i].substring(aData[i].indexOf("=")+1,aData[i].length);
      //alert(sName + ' = "' + unescape(sValue));
      if (sName == "pid")  {  pid = sValue;  }
   }
   return( pid );
}

function menuHandler(e)//handles click on menu items in index.htm
{	var m;
	if(document.all)  {//detect the the type of DOM model
		e = window.event;//IE
		m = e.srcElement;
	}  else  {   //Mozilla
		m = e.target;
	}
	e.cancelBubble = true;
//	if (m.className != "mitem" & m.className != "mheading") return(true);
	if (m.className == "mitem" || m.className == "mheading")  {

	    var loc = m.attributes["show"].value;

	    if (typeof(SelectedDept)=="undefined");
	    else//reset font to normal on previous selection
	    {	//SelectedDept.style.fontWeight=400;//normal
		    SelectedDept.style.color = "#FFFFFF";
	    }	
	    //m.style.fontWeight=700;//bold
	    m.style.color = "#006699";
	    //alert("calling doSubDeptsOf");
	    doSubmenusOf(m,"?");//shows or hides subdepts
    	
	    SelectedDept = m;	
	    showSubPage(loc);
    }    
    return(true);
}//end menu handler----------------------------------
	
function showSubPage(loc)  {	
	if (loc != "")  {
		if (document.getElementById("newwindowlink") != null) {
			document.getElementById("newwindowlink").attributes["href"].value = loc; //m.attributes["show"].value; 
			//document.getElementById("newwindowlink").innerText = mID;  //m.innerText; 
		}
		if (loc.lastIndexOf(":") == -1)  {//check if its an absolute URL
		    //its relative to the current location
            if (!(loc == gpPageNameIntro || loc == gpPageNameInit || loc == gpPageNameProgress || loc == gpPageNameLatest || loc == gpPageNameChart))  {
                //its not a gp page so pause if gp is running
                if (window.document.getElementById("AppState").innerHTML != 'Ready')  {
                    window.document.getElementById("AppState").innerHTML = 'Paused';
                    PendingRequest = "";
                }
            }
			if (window.frames.length != 0) {
				//pos = top.document.location.href.lastIndexOf("/");
                window.document.getElementById("DocState").innerHTML = 'Loading';
				window.frames[0].document.location = loc; 
			}  else { 
				window.open(loc, NewWindowName)
			}
		    SubPageCurrent = loc;
		}  else { // its probably an external link which would cause problems in the frame
			window.open(loc, NewWindowName)
		}
	}
	//return(false);
}//end showSubPage ----------------------------------


function doSubmenusOf(m,how)//show or hide sub depts
{	var e, i 
	//if (how == "hide")			e = m.all("DIV");
	//else						e = m.children.tags("DIV");
	e = m.getElementsByTagName("div");
	
	for (i=0; i < e.length; i++)	{
		if (e[i].style.display == "none" & how != "hide" & e[i].parentNode == m)   {
			e[i].style.display = "block"; //alert("display " + e[i].style.display); e[i].style.display = "block"; alert("display " + e[i].style.display);
		}else   {
			e[i].style.display = "none";
			//alert("none " + e[i].deptid);
			//doSubmenusOf(e[i],"hide")
		}
	}
}//end of function doSubmenusOf



//---------------------------------------------------
function mouseOver(e)   {
	var m;
	if(document.all)  {//detect the type of DOM model
		e = window.event;//IE
		m = e.srcElement;
	}  else  {   //Mozilla
		m = e.target;
	}
	e.cancelBubble = true;
	//m.style.color = "#000000";
	//m.style.cursor = "hand";
}
function mouseOut(e)   {
	var m;
	if(document.all)  {//detect the type of DOM model
		e = window.event;//IE
		m = e.srcElement;
	}  else  {   //Mozilla
		m = e.target;
	}
	e.cancelBubble = true;
	//m.style.color = "#FFFFFF";
	//m.style.cursor = "auto";
}

function hiliter(h)
{	var el = window.event.srcElement;
	if (el.tagName=="SPAN")
	{	if (h==1)
			el.style.color = "#000000";
		else
		{	if (typeof(SelectedDept)=="undefined")
				el.style.color = "#ffffff";
			else
			{	if (SelectedDept==el)
					el.style.color = "#006699";
				else
					el.style.color = "#ffffff";
			}
		}
	}
}// end hilite ---------------------------------

function m_over(x) 
{
	if(x == null)
		x= window;
	x.event.srcElement.style.background="#003399";
	x.event.srcElement.style.color = "yellow";
	x.event.srcElement.style.cursor = "hand";
}

function m_out(x) 
{
	if(x == null)
		x= window;
	x.event.srcElement.style.background="#CC9900";
	x.event.srcElement.style.color = "black";
	x.event.srcElement.style.cursor = "default";
}

function tableFilter(tableName) // used in contacts.php
{	var e, i, cells, result = true;
	var c, r, rows = document.getElementById(tableName).rows;
	var rowUse = document.getElementById("filterUse").getElementsByTagName("input");// all the check boxes
	var rowText = document.getElementById("filterText").getElementsByTagName("input"); // all the text boxes
	var displayOn, displayHow, F, rowCount = 0, rowCountFiltered = 0;
	
	if(document.all)  {//detect the type of DOM model
		displayOn = 'block';//IE
	}  else  {   //Mozilla
		displayOn = 'table-row';//Mozilla
	}
	
	//var c, r, rows = document.all.tableContacts.rows;
	//var rowUse = document.all.tableContacts.rows("use").all("u");// all the check boxes
	//var rowText = document.all.tableContacts.rows("text").all("t"); // all the text boxes
	//var c, cellsCount;
	//alert(rowUse.length);
	document.getElementById("RowCount").innerHTML = "No Records";
	if (rows != null)
	{	rowCount = rows.length - 4; 
	    rowCountFiltered = rowCount;	
		for (r=0; r < rows.length; r++)
		{	//alert(rows[r].childNodes[1].innerHTML);
			//rows[r].style.display = displayOn;//default to displaying row 
			displayHow = displayOn;//default to displaying row 
			if (rows[r].id == "listRow")   {
				for (c=0; c < rowUse.length; c++)//for (c=1; c < rows(2).cells.length; c++)
				//{ if ( c < rowUse.length )
				{	if (rowUse[c].checked == true )
					{	cells = rows[r].getElementsByTagName("TD");
						F = rowText[c].value.toLowerCase()
						if (F == '_')  {  //looking for empty fields
						    if (cells[c + 1].innerHTML != '')   {
								displayHow = 'none';//rows[r].style.display = 'none';
							}
						} else {
						//alert(rows[r].childNodes[c+3].innerHTML + " " + rows[r].childNodes[c].tagName);//alert(" checked");
						//alert(rowText[c].value);
						//if (rows[r].childNodes[c+3].innerHTML.toLowerCase().indexOf(rowText[c].value.toLowerCase()) == -1)
						if (cells[c + 1].innerHTML.toLowerCase().indexOf(F) == -1)
						//contacts cell does not contain the value so hide it
								displayHow = 'none';//rows[r].style.display = 'none';
						}
					}
				}
				if (displayHow == 'none')  rowCountFiltered += -1;
				rows[r].style.display = displayHow;
				//}
			}			//alert(rows(0).cells(1).innerText);
		}
		//alert(rows(2).cells.length);//27
	}
	if (rowCount > rowCountFiltered)  {
		document.getElementById("RowCount").innerHTML = rowCountFiltered + " records, filtered";
	} else {
		document.getElementById("RowCount").innerHTML = rowCountFiltered + " records";
	}
}//end of function contactsFilter

function convertDate(strDate, how)//returns a javascript Date object, or Nan if invalid
	//just does YYYY-MM-DD HH:MM at the moment
{	var re, s,  msg = "";
	var y, m, d, t, h, n, dat;
	re = / /g; s = strDate.replace(re, "");//get rid of spaces
	//alert("'" + s + "' from '" + str + "'");
	if (s.length < 13) 
	{   //alert(s + "- too short");//msg += "'" + strDate + "' is too short. Must be YYYY-MM-DD HH:MM.";
		return NaN;
	}
	else
	{	y = s.substr(0,4); m = s.substr(5,2); d = s.substr(8,2); t = s.substr(10);h = t.substr(0,2);n = t.substr(3,2);
		if (y < 1970 | y > 2100)  msg += "'" + strDate + "' is not a valid year. ";
		if (m < 1 | m > 12)       msg += "'" + strDate + "' is not a valid date. Must be YYYY-MM-DD HH:MM.";
		if (d < 1 | d > 31)       msg += "'" + strDate + "' is not a valid date. Must be YYYY-MM-DD HH:MM.";
		dat = m + "-" + d + "-" + y + " " + t;
		//alert("dat=" + dat);
		return new Date(y,m,d,h,n);
	}
}//end function convertDate
