﻿///////////////////////////////////////////
// MULTIPLE ONLOAD EVENTS

var onloadEvents = new Array();

function setOnload(f)
{
	var i = onloadEvents.length;
	onloadEvents[i] = f;
}

function doOnload()
{
	if(onloadEvents.length == 0) return;

	for(var i = onloadEvents.length;i-->0;)
	{
		eval(onloadEvents[i] + "()");
	}
}

onload=doOnload;

///////////////////////////////////////////
// DROP DOWN MENUS

function dropMenus()
{
	if(document.getElementById)
	{
		// Get all the drop downs on the website
		var drop_downs = document.getElementsByTagName("select");

		// The parent div
		var menu_div = document.getElementById("p-menu");
		// The table with the links
		var menu_list = document.getElementById('ctl00_menu-list');
		// The width of the sub-menus (manually entered, look at the stylesheet (menu.css))
		var menu_width = 180;

		for (var i=0; i<menu_list.childNodes.length; i++)
		{
			var node = menu_list.childNodes[i];

			if(node.nodeName=="TD")
			{
			    if (node.childNodes.count > 1)
			    {
				node.onmouseover=function()
				{
					// Before we show the submenu, we have to hide all of the select boxes
					// FWIW, select boxes show THROUGH a layer --even if it has higher z-index
					// Loop through and hide 'em all
					for(var j=0; j<drop_downs.length; j++)
					{
						drop_downs[j].style.visibility = "hidden";
					}

					// Positioning the submenu
					// Get the TD
					var sub_menu = (this.childNodes[2].nodeName.toLowerCase()=="ul") ? this.childNodes[2] : this.childNodes[3];
					// Get the TD width
					var menu_item_width = this.offsetWidth;
						//alert(menu_item_width);
					// Get the distance from the edge of the left content
					var offset_left = parseInt(this.offsetLeft);
						//alert(offset_left);
					// Get the total distance from the left (-1 for the border);
					var from_left = parseInt(menu_div.offsetLeft) +  offset_left -1;
						//alert(from_left);

					// Either align the submenu to the left or the right of the main menu tab
					// If the the distance from the left of the menu plus the submenu is greater than
					// than the content area, align it to the right of the tab so it doesn't hang off the right edge
					// NOTE: We need an extra pixel to align correctly for Internet Explorer
					var pixel_offset_right = (document.all) ? 1 : 0;
					var pixel_offset_left = (document.all) ? 0 : -1;

					if((offset_left + menu_width) > menu_div.offsetWidth)
					{
						sub_menu.style.left = (from_left) - (menu_width - menu_item_width) + pixel_offset_right;
					}
					// If not, align it to the left of the tab
					else
					{
						//alert(from_left);
						sub_menu.style.left = (from_left + pixel_offset_left);
						//alert(from_left + pixel_offset_left);
					}

					// The hiding/showing is handled in soley by css for gecko browsers
					// We need this for Internet Explorer
					if(document.all)
					{
						this.className = this.className + " over";
						this.style.zIndex = "100";
					}
				}
				node.onmouseout=function()
				{
					// Now we need to show the hidden select boxes
					// Loop through and show 'em all
					for(var j=0; j<drop_downs.length; j++)
					{
						drop_downs[j].style.visibility = "visible";
					}

					// The hiding/showing is handled in soley by css for gecko browsers
					// We need this for Internet Explorer
					if(document.all)
					{
	  					this.className=this.className.replace(" over", "");
						this.style.zIndex = "50";
					}
				}
				}
			}
		}
	}
}

setOnload("dropMenus");

//////////////////////////////// TABLE ROW SHADING

function zebraTable()
{
	var ua = navigator.userAgent.toLowerCase();

	if(ua.indexOf("msie")>-1 && ua.indexOf("mac")>-1)
	{
		return;
	}
	else
	{
		var t = document.getElementsByTagName("table");
	
		for(var i=0; i<t.length; i++)
		{
			if(t[i].className && t[i].className=="listing-table")
			{
				var r = t[i].rows;
	
				for(var j=0; j<r.length; j++)
				{
					if(j%2 == 0) r[j].className = r[j].className + " shade";
				}
			}
		}
	}
}

setOnload("zebraTable");

//////////////////////////////// HIGHLIGHT TABLE ROW

function highlightRow()
{
	var ua = navigator.userAgent.toLowerCase();

	if(ua.indexOf("msie")>-1 && ua.indexOf("mac")>-1)
	{
		return;
	}
	else
	{
		var t = document.getElementsByTagName("table");
	
		for(var i=0; i<t.length; i++)
		{
			if(t[i].className && t[i].className.indexOf("listing-table") > -1)
			{
				var table = t[i];
	
				for(var j=0; j<table.rows.length; j++)
				{
					table.rows[j].onmouseover = function()
					{
							this.className += " lite";
					}
					table.rows[j].onmouseout = function()
					{
						this.className = this.className.replace("lite","");
					}
				}
			}
		}
	}
}

setOnload("highlightRow");

//////////////////////////////// PAGE TOOLS

function printPage()
{
	window.print();
	return;
}

function emailLink()
{
	var strURL = window.location.href;
	var strTitle = document.getElementsByTagName("h1");
	var strPageName = strTitle[0].innerText;
	var strSubject = "A Link To The Solebury Township Website";
	var strBody = "A link to the '" + strPageName + "' page at the Solebury Township website has been sent to you. %0D%0DYou can visit this page at: " + strURL + "%0D%0DThank you.%0D%0D------------------------%0DThis email was auto generated from the Solebury Township website.";
	window.open("mailto:?subject=" + strSubject + "&body=" + strBody);
	return;
}

function trim(s)
{
	return s.replace(/^\s+|\s+$/g, '');
}

function validEmail(ea)
{
	var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
	var regex = new RegExp(emailReg);
	return regex.test(ea);
  }

function validate(f,el)
{
	var address = trim(document.getElementById(el).value);
	var valid = validEmail(address);

	if(!valid)
	{
		alert("\"" + address + "\" is not a valid email address. A valid email address is required to submit this form.");
		f.focus();
		f[el].select();
		return false;
	}
	else return true;
}

//////////////// POPUPS ADDED BY SOLEBURY TOWNSHIP

function popitup(url) {
	newwindow=window.open(url,'name','height=610,width=550,top=100,left=200');
	if (window.focus) {newwindow.focus()}
	return false;
}

