// ccccccccccccccccccc copyright© 2005 Grevillea Software cccccccccccccccccccccc 

var SelectedDept;//=document.all["div1"]
function window_onload()
{	var b = document.body.getElementsByTagName("div");
	var el, loc = "home.php", pid = "home";
	//alert("loaded");//windowResize();
	for (i=0; i < b.length; i++)	{
		el = b[i];
		if (el.className == "mitem")   {
			el.onmouseover = mouseOver;
			el.onmouseout = mouseOut;
			el.onclick = menuHandler;
		}
	}	
	//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 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 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 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);

	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);
}//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 (window.frames.length != 0) {
				//pos = top.document.location.href.lastIndexOf("/");
				window.frames[0].document.location = loc; 
			}  else { 
				window.open(loc, "wBaywriteLinks")
			}
		}  else { // its probably an external link which would cause problems in the frame
			window.open(loc, "wBaywriteLinks")
		}
	}
	//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 ShopMenuHandler()
{	var m = window.event.srcElement;//doesn't work for Firefox
	var e; //var pos; var loc
	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");
	doSubDeptsOf("b" + m.deptid,"?")//shows or hides subdepts
	
	SelectedDept=m;	
	if (m.show != "")  {
		if (document.all("newwindowlink") != null) {
			document.all("newwindowlink").href = m.show; 
			document.all("newwindowlink").innerText = m.innerText; 
		}
		if (m.show.lastIndexOf(":") == -1)  {//check if its an absolute URL
		    //its relative to the current location
			if (top.window.frames.length != 0) {
				//pos = top.document.location.href.lastIndexOf("/");
				top.window.frames("fBody").document.location = m.show; 
			}  else { 
				window.open(m.show, "wBaywriteLinks")
			}
		}  else { // its probably an external link which would cause problems in the frame
			window.open(m.show, "wBaywriteLinks")
		}
	}
}//end menu handler----------------------------------

function doSubDeptsOf(d,how)//show or hide sub depts
{	var e, i, displayOn
	
	if(document.all)  {//detect the type of DOM model
		displayOn = 'block';//IE
	}  else  {   //Mozilla
		displayOn = 'table-row';//Mozilla
	}

	e = document.all(d);//ie all tags where id=d (the id of the tag contains the record id of the dept the subdept belongs to)
	if (e != null)
	{	if(e.length==null) e = new Array(e);//there's only one tag, and its not in an array, so create an array.
		{	for (i=0; i < e.length; i++)
			{	//alert("style " + e.item(i).style.display);
				if (e[i].style.display == "none" & how != "hide")
				{	//alert("block " + e.item(i).deptid);
					e[i].style.display = displayOn;
				}
				else
				{	e[i].style.display = "none";
					//alert("none " + e[i].deptid);
					doSubDeptsOf("b" + e[i].deptid,"hide")
				}
			}
		}
	}
}//end of function doSubDeptsOf

//---------------------------------------------------

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 m_click(dest) 
{
	window.navigate(dest);
}

/*
function handleMouseover() {
	eSrc = window.event.srcElement;
	eSrcTag=eSrc.tagName.toUpperCase();
	if (eSrcTag == "A") eSrc.style.textDecoration = "underline";
}

function handleMouseout() {
	eSrc = window.event.srcElement;
	eSrcTag=eSrc.tagName.toUpperCase();
	if (eSrcTag == "A") eSrc.style.textDecoration = "";
}
*/
//document.onmouseover=handleMouseover;
//document.onmouseout=handleMouseout;

//window.onload=window_onload;

function window_onloadShop()
{
	if (top.document.all)
	{
		if (top.document.title != "The Shop") top.window.navigate("Shop.asp");
		var ftitle = top.document.all("frameTitle");
		
		//alert(ftitle.innerHTML);
		if (ftitle != null)
		{
			ftitle.filters[0].Apply(); 
			ftitle.innerHTML = document.title; 
			ftitle.filters[0].Play(); 
		}
	}	
	//else alert("this is the top doc-" + document.title.innerText);
}
function ValidateQuantities()// used in products.asp
{	var e, i , result = true
	e = window.event.srcElement.elements("intQuantity").value;
	if (isNaN(parseInt(e))==true)		result = false;//handles empty and spaces
	else
	{	if (e > 0 )
		{	if (e != Math.floor(e))		result = false;}
		else  							result = false;
	}
	
	if ( result == false)
	{	alert("'" + e + "' is not a valid quantity. Use whole numbers, 1 or greater, for the new quantity.");
		window.event.returnValue = false;
	}
}//end of function ValidateQuantities

function ValidateQuantitiesNew() // used in Cart.asp
{	var e, i , result = true
	e = form1.elements("intQuantityNew");
	if (e != null)
	{	for (i=0; i < e.length; i++)
		{	result = true;
			if (isNaN(parseInt(e.item(i).value))==true) 				result = false;//handles empty and spaces
			else
			{	if (e.item(i).value >= 0 )//returns true if empty or spaces
				{	if (e.item(i).value != Math.floor(e.item(i).value)) 						result = false;}
				else  					result = false;
			}
			if ( result == false)
			{	alert("'" + e.item(i).value + "' is not a valid new quantity. Use whole numbers, 0 or greater, for the new quantity.");
				window.event.returnValue = false;
			}
		}
	}
}//end of function ValidateQuantitiesnew

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 contactsEmailList() // used in contacts.php
{	var e, i , result = true;
	var c, r, rows = document.getElementsByName("listRow");
	var emails = document.getElementsByName("rEmail");// all the email table cells
	var firstNames = document.getElementsByName("rFirstname");// all the name table cells
	var lastNames   = document.getElementsByName("rLastname");// all the name table cells
	//var rowText = document.all.tableContacts.rows("text").all("t"); // all the text boxes
	var list = "";
	//alert("name=" + document.all.contactsedit(0).name);
	//alert("padding=" + document.all.contactsedit(0).style.paddingBottom);
	//alert("margin=" + document.all.contactsedit(0).style.marginBottom);
	//alert("borderBottomWidth=" + document.all.contactsedit(0).style.borderBottomWidth);
	//alert("padding=" + document.all.contactsedit(0).paddingBottom);
	document.getElementById("textList").value = "";
	if (emails != null)
	{	for (c=0; c < emails.length; c++)
		//{	if (emails[c].innerHTML.length > 3  && rows[c].style.display != "none") 
		{	
			if (emails[c].innerHTML.length > 3 ) 
			{	list += firstNames[c].innerHTML + " " + lastNames[c].innerHTML + " <" + emails[c].innerHTML  + ">; " ;
			}
			//alert(rows(0).cells(1).innerHTML);
		}
		document.getElementById("textList").value = list;
		//alert(rows(2).cells.length);//27
	}
	
}//end of function contactsEmailList


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

function WhatsOnList() // used in contacts.php
//gets from non-hidden table rows
{	var e, i , result = true;
	var c, r, dat, loc, link, strDate;
	var rows = document.getElementsByName("listRow");
	var rName	= document.getElementsByName("rName");// all the email table cells
	var rWhere	= document.getElementsByName("rWhere");// all the name table cells
	var rWhen	= document.getElementsByName("rWhen");// all the name table cells
	var rDesc	= document.getElementsByName("rDesc");// all the name table cells
	var rLink	= document.getElementsByName("rLink");// all the name table cells
	var rType	= document.getElementsByName("rType");// all the name table cells
	//var rowText = document.all.tableContacts.rows("text").all("t"); // all the text boxes
	var list = "";
	document.getElementById("textList").value = "";
	c = window.location.href.indexOf("/admin/");
	loc = window.location.href.substring(0,c);//includes slash
	//alert(document.all.listTable.cells("rName").length + " rName rows");
	//alert(document.all.length + " elements");
	if (rName != null)
	{for (c=0; c < rName.length; c++)
		{if (rows[c].style.display != "none") 
			{	dat = convertDate(rWhen[c].innerHTML, "mySQL");
				if (isNaN(dat)) strDate = ""; 
					else strDate = dat.toLocaleDateString() + " " + dat.toLocaleTimeString() + ", ";
				
				list += rType[c].innerHTML.toUpperCase() + ": " + rName[c].innerHTML.toUpperCase();
				list += "\n" + strDate;
				if (rWhere[c].innerHTML != "") list += " at " + rWhere[c].innerHTML;
				link = rLink[c].innerHTML;
				if (link != "") 
				{	if (link.indexOf("://") == -1) link = loc + link;// link has an initial slash
					list += "\n" + link;				
				}
				if (rDesc[c].innerHTML != "")  list += "\n" + rDesc[c].innerHTML;
				list += "\n--------------------------------------------------------\n";

			}
			//alert(rows(0).cells(1).innerHTML);
		}
		document.getElementById("textList").value = list;
		//alert(rows(2).cells.length);//27
	}
	
}//end of function contactsEmailList
