/**
 * SUBMODAL v1.4
 * Used for displaying DHTML only popups instead of using buggy modal windows.
 *
 * By Seth Banks (webmaster at subimage dot com)
 * http://www.subimage.com/
 *
 * Contributions by:
 * 	Eric Angel - tab index code
 * 	Scott - hiding/showing selects for IE users
 *	Todd Huss - inserting modal dynamically and anchor classes
 *
 * Up to date code can be found at http://www.subimage.com/dhtml/subModal
 * 
 *
 * This code is free for you to use anywhere, just keep this comment block.
 */

// Popup code
var gPopupMask = null;
var gPopupContainer = null;
//var gPopFrame = null;
var gPopDIV = null;
//var gReturnFunc;
var gPopupIsShown = false;

var gHideSelects = false;


var gTabIndexes = new Array();
// Pre-defined list of tags we want to disable/enable tabbing into
var gTabbableTags = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");	

// If using Mozilla or Firefox, use Tab-key trap.
if (!document.all) {
	document.onkeypress = keyDownHandler;
}



/**
 * Initializes popup code on load.	
 */
function initPopUp() {
	// Add the HTML to the body
	theBody = document.getElementsByTagName('BODY')[0];
	popmask = document.createElement('div');
	popmask.id = 'popupMask';
	popcont = document.createElement('div');
	popcont.id = 'popupContainer';
	popcont.innerHTML = '' +
		'<div id="popupInner">' +
			'<div id="popupTitleBar">' +
				'<div id="popupTitle">test</div>' +
				'<div id="popupControls">' +
					'<img src="images/close.gif" onclick="hidePopWin(false);" id="popCloseBox" />' +
				'</div>' +
			'</div>' +
			'<div style="visibility:hidden" id="popupDIV" name="popupDIV" width="100%" height="100%"></div>' +
		'</div>';
	theBody.appendChild(popmask);
	theBody.appendChild(popcont);
	
	gPopupMask = document.getElementById("popupMask");
	gPopupContainer = document.getElementById("popupContainer");
	//gPopFrame = document.getElementById("popupFrame");
	gPopDIV = document.getElementById("popupDIV");	

	
	// check to see if this is IE version 6 or lower. hide select boxes if so
	// maybe they'll fix this in version 7?
	var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
	if (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1) {
		gHideSelects = true;
	}
	
	// Add onclick handlers to 'a' elements of class submodal or submodal-width-height
	var elms = document.getElementsByTagName('a');
	for (i = 0; i < elms.length; i++) {
		if (elms[i].className.indexOf("submodal") == 0) { 
			// var onclick = 'function (){showPopWin(\''+elms[i].href+'\','+width+', '+height+', null);return false;};';
			// elms[i].onclick = eval(onclick);
			elms[i].onclick = function(){
				// default width and height
				var width = 400;
				var height = 200;
				// Parse out optional width and height from className
				params = this.className.split('-');
				if (params.length == 3) {
					width = parseInt(params[1]);
					height = parseInt(params[2]);
				}
				showPopWin(this.href,width,height,null); return false;
			}
		}
	}
}
addEvent(window, "load", initPopUp);

 /**
	* @argument width - int in pixels
	* @argument height - int in pixels
	* @argument url - url to display
	* @argument returnFunc - function to call when returning true from the window.
	* @argument showCloseBox - show the close box - default true
	*/

function showPopWin(width, height, cellnum, title, agendaORminutes, showCloseBox) {
	
	//set title bar for modal dialog
	if (title != null) {
		document.getElementById("popupTitle").innerHTML = title+" - "+agendaORminutes;
	} else {
		document.getElementById("popupTitle").innerHTML = "Harborcreek Township";
	}
	// show or hide the window close widget		
	if (showCloseBox == null || showCloseBox == true) {
		document.getElementById("popCloseBox").style.display = "block";
	} else {
		document.getElementById("popCloseBox").style.display = "none";
	}
	gPopupIsShown = true;
	disableTabIndexes();
	gPopupMask.style.display = "block";
	gPopupContainer.style.display = "block";
	// calculate where to place the window on screen
	centerPopWin(width, height);
	
	var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);


	gPopupContainer.style.width = width + "px";
	gPopupContainer.style.height = (height+titleBarHeight) + "px";
	
	setMaskSize();

	// need to set the width of the iframe to the title bar width because of the dropshadow
	// some oddness was occuring and causing the frame to poke outside the border in IE6
	gPopDIV.style.width = parseInt(document.getElementById("popupTitleBar").offsetWidth, 10) + "px";
	gPopDIV.style.height = (height) + "px";
	
	ret = "";

	ret += "<form onsubmit=\'return formValidator()\' method=\'post\' enctype=\'multipart/form-data\' action=\'http://www.harborcreektownship.org/cgi-bin/meetings/vtup.pl?"+cellnum+"+"+agendaORminutes+"\'>";
		ret += "<center>&nbsp;";
		
		ret += "<table bgcolor='#FFFFFF' width=\'375\' border=\'0\' cellpadding=\'0\' cellspacing=\'0\' height=\'10\'>";
			ret += "<tr>";
				ret += "<td height=\'8\' width=\'8\'><img src=\'http://www.harborcreektownship.org/format_images/tl_white.gif\'></td>";
				ret += "<td style=\'border-top:1px solid #666666\' height=\'8\' width=\'100%\'><img src=\'http://www.harborcreektownship.org/format_images/spacer.gif\' height=\'8\'></td>";
				ret += "<td height=\'8\' width=\'8\'><img src=\'http://www.harborcreektownship.org/format_images/tr_white.gif\'></td>";
			ret += "</tr>";
		ret += "</table>";
		ret += "<table bgcolor='#FFFFFF' width=\'375\' border=\'0\' cellspacing=\'0\' cellpadding=\'0\'>";
			ret += "<tr>";						
				ret += "<td style=\"padding-left:6px; border-left:1px solid gray; border-right:1px solid gray\">";						
					ret += "<P style=\"margin-left: 0px; margin-bottom: 0px; margin-top: 0px; line-height: 11pt; font-weight: normal; font-size: 10pt; color: #000066; font-family: Arial, Goudy Old Style, Verdana;\">";
						ret += "1. Browse to the file you want to upload.<br>2. Highlite the file and click open.<br>3. Click the UPLOAD button.<br>4. Wait for the Meetings page to reload.<br><br><center><font color=\"#800000\">**</font> FILENAME FORMAT for Minutes<font color=\"#800000\">**</font><br><font color=\"#800000\">yy_mm_dd.ext <font size=2>(ext = doc, pdf, or htm)</font></center><br>";
					ret += "</p>";
				ret += "</td>";
			ret += "</tr>";			
		ret += "</table>";
		ret += "<table bgcolor='#FFFFFF' width=\'375\' border=\'0\' cellspacing=\'0\' cellpadding=\'0\'>";
			ret += "<tr>";						
				ret += "<td style=\"padding-left:10px; border-left:1px solid gray; border-right:1px solid gray\">";
					ret += "<P style=\"margin-left: 0px; margin-bottom: 0px; margin-top: 0px; line-height: 10pt; font-weight: normal; font-size: 8pt; color: #000066; font-family: Arial, Goudy Old Style, Verdana;\">";
						ret += "File to be uploaded";
					ret += "</p>";
				ret += "</td>";
			ret += "</tr>";
			ret += "<tr>";						
				ret += "<td style=\"padding-left:10px; border-left:1px solid gray; border-right:1px solid gray\"><input id=\'upload_file\' type=\'file\' name=\'upload_file\' size=\'40\'><br></td>";
			ret += "</tr>";
			ret += "<tr>";
				ret += "<td style=\"padding-left:10px; border-left:1px solid gray; border-right:1px solid gray\" colspan=\'2\'>";
						ret += "<input type=\'submit\' value=\'Upload\' name=\'upload\'>";
						ret += "<input type=\'reset\' value=\'Reset\' name=\'b2\'>";
				ret += "</td>";
			ret += "</tr>";
		ret += "</table>";		
		ret += "<table bgcolor='#FFFFFF' width=\'375\' border=\'0\' cellpadding=\'0\' cellspacing=\'0\'>";
			ret += "<tr>";
				ret += "<td height=\'8\' width=\'8\' valign=\'bottom\'><img src=\'http://www.harborcreektownship.org/format_images/bl_white.gif\'></td>";
				ret += "<td style=\'border-bottom:1px solid #666666\' height=\'10\' width=\'100%\' valign=\'bottom\'><img src=\'http://www.harborcreektownship.org/format_images/spacer.gif\' height=\'8\'></td>";
				ret += "<td height=\'8\' width=\'8\' valign=\'bottom\'><img src=\'http://www.harborcreektownship.org/format_images/br_white.gif\'></td>";
			ret += "</tr>";
		ret += "</table></center>";				
	ret += "</form>";

	// set the html
	gPopDIV.innerHTML = ret;
	
	gPopDIV.style.visibility = "visible";
	//gReturnFunc = returnFunc;
	// for IE
	if (gHideSelects == true) {
		hideSelectBoxes();
	}
	
	//window.setTimeout("setPopTitle();", 600);
}

function showPopSearchWin(width, height, cellnum, showCloseBox) {
	
	//set title bar for modal dialog	
	document.getElementById("popupTitle").innerHTML = "SEARCH MEETING MINUTES";
	
	// show or hide the window close widget		
	if (showCloseBox == null || showCloseBox == true) {
		document.getElementById("popCloseBox").style.display = "block";
	} else {
		document.getElementById("popCloseBox").style.display = "none";
	}
	gPopupIsShown = true;
	disableTabIndexes();
	gPopupMask.style.display = "block";
	gPopupContainer.style.display = "block";
	// calculate where to place the window on screen
	centerPopWin(width, height);
	
	var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);


	gPopupContainer.style.width = width + "px";
	gPopupContainer.style.height = (height+titleBarHeight) + "px";
	
	setMaskSize();

	// need to set the width of the iframe to the title bar width because of the dropshadow
	// some oddness was occuring and causing the frame to poke outside the border in IE6
	gPopDIV.style.width = parseInt(document.getElementById("popupTitleBar").offsetWidth, 10) + "px";
	gPopDIV.style.height = (height) + "px";	
	
	ret = "";
		ret += "<center>&nbsp;";
		
		ret += "<table bgcolor='#FFFFFF' width=\'375\' border=\'0\' cellpadding=\'0\' cellspacing=\'0\' height=\'10\'>";
			ret += "<tr>";
				ret += "<td height=\'8\' width=\'8\'><img src=\'http://www.harborcreektownship.org/format_images/tl_white.gif\'></td>";
				ret += "<td style=\'border-top:1px solid #666666\' height=\'8\' width=\'100%\'><img src=\'http://www.harborcreektownship.org/format_images/spacer.gif\' height=\'8\'></td>";
				ret += "<td height=\'8\' width=\'8\'><img src=\'http://www.harborcreektownship.org/format_images/tr_white.gif\'></td>";
			ret += "</tr>";
		ret += "</table>";
				
		ret += "<table bgcolor='#FFFFFF' width=\'375\' border=\'0\' cellspacing=\'0\' cellpadding=\'0\'>";
			ret += "<tr>";						
				ret += "<td style=\"border-left:1px solid gray; border-right:1px solid gray\">";
					ret += "<P style=\"margin-left: 6px; margin-bottom: 0px; margin-top: 0px; line-height: 10pt; font-weight: normal; font-size: 8pt; color: #CD5C5C; font-family: Arial, Goudy Old Style, Verdana;\">";
						ret += "There are approximately 24 months of minutes available here to search.<br><br>";
					ret += "</p>";
				ret += "</td>";
			ret += "</tr>";
			ret += "<TR>";
				ret += "<TD bgcolor=\"#FFFFFF\" STYLE=\"border-left: 1px solid #666666; border-right: 1px solid #666666;\">";
					ret += "<FORM STYLE=\"margin-top: 0; margin-bottom: 0\" ACTION=\"http://www.harborcreektownship.org/cgi-bin/minuteSearch/ksearch.pl\" METHOD=\"GET\" NAME=\"search\">";
						ret += "<p align=\"left\" style=\"line-height: 100%; margin-top: 0; margin-bottom: 0; margin-left: 6; font-size: 8pt; color: #000066; font-family: Arial, Goudy Old Style, Verdana;\">";
							ret += "Supervisors Meeting Minutes&nbsp;<IMG SRC=\"format_images/magnify.gif\" WIDTH=\"16\" HEIGHT=\"16\">";
						ret += "</p>";
						ret += "<p align=\"left\" style=\"line-height: 100%; margin-top: 0; margin-bottom: 0;margin-left: 6; font-size: 7pt; color: #000000; font-family: Arial, Goudy Old Style, Verdana;\">";
							ret += "<INPUT TYPE=\"text\" SIZE=\"40\" NAME=\"terms\">&nbsp;<INPUT TYPE=\"submit\" VALUE=\"Search\">";
						ret += "</p>";
					ret += "</FORM>";
				ret += "</TD>";
			ret += "</TR>";
		ret += "</table>";		
		ret += "<table bgcolor='#FFFFFF' width=\'375\' border=\'0\' cellpadding=\'0\' cellspacing=\'0\'>";
			ret += "<tr>";
				ret += "<td height=\'8\' width=\'8\' valign=\'bottom\'><img src=\'http://www.harborcreektownship.org/format_images/bl_white.gif\'></td>";
				ret += "<td style=\'border-bottom:1px solid #666666\' height=\'10\' width=\'100%\' valign=\'bottom\'><img src=\'http://www.harborcreektownship.org/format_images/spacer.gif\' height=\'8\'></td>";
				ret += "<td height=\'8\' width=\'8\' valign=\'bottom\'><img src=\'http://www.harborcreektownship.org/format_images/br_white.gif\'></td>";
			ret += "</tr>";
		ret += "</table></center>";				

	// set the html
	gPopDIV.innerHTML = ret;
	
	gPopDIV.style.visibility = "visible";
	//gReturnFunc = returnFunc;
	// for IE
	if (gHideSelects == true) {
		hideSelectBoxes();
	}
	
	//window.setTimeout("setPopTitle();", 600);
}

//
var gi = 0;
function centerPopWin(width, height) {
	if (gPopupIsShown == true) {
		if (width == null || isNaN(width)) {
			width = gPopupContainer.offsetWidth;
		}
		if (height == null) {
			height = gPopupContainer.offsetHeight;
		}
		
		//var theBody = document.documentElement;
		var theBody = document.getElementsByTagName("BODY")[0];
		theBody.style.overflow = "hidden";
		
		var scTop = parseInt(theBody.scrollTop,10);
		var scLeft = parseInt(theBody.scrollLeft,10);
		
		gPopupMask.style.top = scTop + "px";
		gPopupMask.style.left = scLeft + "px";
	
		setMaskSize();
		
		//window.status = gPopupMask.style.top + " " + gPopupMask.style.left + " " + gi++;
		
		var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
		
		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();
		
		gPopupContainer.style.top = (scTop + ((fullHeight - (height+titleBarHeight)) / 2)) + "px";
		gPopupContainer.style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";
		//alert(fullWidth + " " + width + " " + gPopupContainer.style.left);
	}
}
addEvent(window, "resize", centerPopWin);
//addEvent(window, "scroll", centerPopWin);
window.onscroll = centerPopWin;

/**
 * Sets the size of the popup mask.
 *
 */
function setMaskSize() {
	var theBody = document.getElementsByTagName("BODY")[0];
			
	var fullHeight = getViewportHeight();
	var fullWidth = getViewportWidth();
	
	// Determine what's bigger, scrollHeight or fullHeight / width
	if (fullHeight > theBody.scrollHeight) {
		popHeight = fullHeight;
	} else {
		popHeight = theBody.scrollHeight;
	}
	
	if (fullWidth > theBody.scrollWidth) {
		popWidth = fullWidth;
	} else {
		popWidth = theBody.scrollWidth;
	}
	
	gPopupMask.style.height = popHeight + "px";
	gPopupMask.style.width = popWidth + "px";
}

/**
 * @argument callReturnFunc - bool - determines if we call the return function specified
 * @argument returnVal - anything - return value 
 */
function hidePopWin(callReturnFunc) {
	gPopupIsShown = false;
	var theBody = document.getElementsByTagName("BODY")[0];
	theBody.style.overflow = "";
	restoreTabIndexes();
	if (gPopupMask == null) {
		return;
	}
	gPopupMask.style.display = "none";
	gPopupContainer.style.display = "none";
	//if (callReturnFunc == true && gReturnFunc != null) {
		//gReturnFunc(window.frames["popupFrame"].returnVal);
	//}
	//gPopFrame.src = 'loading.html';
	// display all select boxes
	if (gHideSelects == true) {
		displaySelectBoxes();
	}
}

/**
 * Sets the popup title based on the title of the html document it contains.
 * Uses a timeout to keep checking until the title is valid.
 */
/*function setPopTitle() {
	return;
	if (window.frames["popupFrame"].document.title == null) {
		window.setTimeout("setPopTitle();", 10);
	} else {
		document.getElementById("popupTitle").innerHTML = window.frames["popupFrame"].document.title;
	}
}*/

// Tab key trap. iff popup is shown and key was [TAB], suppress it.
// @argument e - event - keyboard event that caused this function to be called.
function keyDownHandler(e) {
    if (gPopupIsShown && e.keyCode == 9)  return false;
}

// For IE.  Go through predefined tags and disable tabbing into them.
function disableTabIndexes() {
	if (document.all) {
		var i = 0;
		for (var j = 0; j < gTabbableTags.length; j++) {
			var tagElements = document.getElementsByTagName(gTabbableTags[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				gTabIndexes[i] = tagElements[k].tabIndex;
				tagElements[k].tabIndex="-1";
				i++;
			}
		}
	}
}

// For IE. Restore tab-indexes.
function restoreTabIndexes() {
	if (document.all) {
		var i = 0;
		for (var j = 0; j < gTabbableTags.length; j++) {
			var tagElements = document.getElementsByTagName(gTabbableTags[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				tagElements[k].tabIndex = gTabIndexes[i];
				tagElements[k].tabEnabled = true;
				i++;
			}
		}
	}
}


/**
* Hides all drop down form select boxes on the screen so they do not appear above the mask layer.
* IE has a problem with wanted select form tags to always be the topmost z-index or layer
*
* Thanks for the code Scott!
*/
function hideSelectBoxes() {
	for(var i = 0; i < document.forms.length; i++) {
		for(var e = 0; e < document.forms[i].length; e++){
			if(document.forms[i].elements[e].tagName == "SELECT") {
				document.forms[i].elements[e].style.visibility="hidden";
			}
		}
	}
}

/**
* Makes all drop down form select boxes on the screen visible so they do not reappear after the dialog is closed.
* IE has a problem with wanted select form tags to always be the topmost z-index or layer
*/
function displaySelectBoxes() {
	for(var i = 0; i < document.forms.length; i++) {
		for(var e = 0; e < document.forms[i].length; e++){
			if(document.forms[i].elements[e].tagName == "SELECT") {
			document.forms[i].elements[e].style.visibility="visible";
			}
		}
	}
}