/* =Onload Functions
-------------------------------------------------- */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload !== 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		};
	}
}

/* =Disclaimers
-------------------------------------------------- */
function emailDisc(passedAddress) {
	if (confirm("Messages sent to Hills Bank via email are sent over the Internet and are not transmitted in an encrypted or secure form. Do not email account numbers, social security numbers, or other confidential information.")) {
		window.location = passedAddress;
	}
}

function linkDisc(passedWebsite) {
	if (confirm("HillsBank.com provides links to other web sites for convenience and informational purposes only.  Users should be aware that all web sites are not protected.  Hills Bank and Trust Company is not responsible for the contents of any linked site or any link contained in a linked site, or any changes or updates to such sites.  Hills Bank and Trust Company is not responsible for any transmission received from a linked site.")) {
		var oWin = window.open(passedWebsite,"newWindow");
		if (oWin == null || typeof(oWin) == "undefined") {
			window.location.href = passedWebsite;
		} else {
			return true;
		}
	}
}

/* =Bookmark
-------------------------------------------------- */
function createBookmarkLink() {
	if (!document.getElementsByTagName || !document.getElementById || !document.getElementById("bookmark")) { return false; }
	var linkContainer = document.getElementById("bookmark");
	if (navigator.platform.indexOf("Win") != -1) {
		if (document.all) {
		linkContainer.innerHTML = "<a href=\"javascript:void window.external.AddFavorite(location.href, document.title);\">Add to Favorites</a>";
		} else if (!document.all) {
			linkContainer.innerHTML = "<a href=\"javascript:void window.sidebar.addPanel(document.title, location.href, '');\">Bookmark Page</a>";
		} else {
			linkContainer.innerHTML = "<a href=\"javascript:void alert('Please press Controll/Ctrl + D to bookmark our site.');\">Bookmark Page</a>";
		}
	} else if (navigator.platform.indexOf("Mac") != -1) {
		if (navigator.vendor.indexOf("Apple") != -1) {
			linkContainer.innerHTML = "<a href=\"javascript:void alert('Please press Command/Cmd + D to bookmark our site.');\">Bookmark Page</a>";
		} else {
			linkContainer.innerHTML = "<a href=\"javascript:void window.sidebar.addPanel(document.title, location.href, '');\">Bookmark Page</a>";
		}
	}
}
addLoadEvent(createBookmarkLink);

/* =Show/Hide Sections
-------------------------------------------------- */
// Toggle individual sections to expand/collapse, including changing display of ad images.
//To simulate the expand/collapse effect, we're changing the height of the section. 30px is default, or collapsed. Auto allows the section to expand to the size of the content inside it.

function toggleSection(section) {
	if (!document.getElementsByTagName || !document.getElementById || !document.getElementById("productList") || !document.getElementById("defaultSideImage")) { return false; }
	var el = document.getElementById(section); // The section we're in.
	var sideImages = el.getElementsByTagName("div"); // Get all <div> tags in this section.
	var defaultSideImage = document.getElementById("defaultSideImage"); // Get the default image.
	
	if (el.style.height !== "auto") { // If this section's height is not set to auto.
		for (i = 0; i < sideImages.length; i++) { // Loop through all of the <div> tags in the section.
			if (sideImages[i].className.match("sideImage")) { // Get all the <div> tags with a class of sideImage.
			  sideImages[i].style.display = "block"; // Set those items to display: block so they'll show.
			}
		}
		el.style.height = "auto"; // Set the height of the secton to auto so it'll expand.
		el.className = "expand"; // Change the section's class to expand for minor layout changes.
		defaultSideImage.style.display = "none"; // Hide the default ad image.
		
	} else { // If this section's height is set to auto.
		for (i = 0; i < sideImages.length; i++) { // Loop through all of the <div> tags in the section.
			if (sideImages[i].className.match("sideImage")) { // Get all the <div> tags with a class of sideImage.
			  sideImages[i].style.display = "none"; // Set those items to display: none so they won't show.
			}
		}
		el.style.height = "30px"; // Set the height of the secton to 30px so it'll contract.
		el.className = "contract"; // Change the section's class to contract for minor layout changes.
		
		// Here is where we try to determine if ALL images are hidden. If they are, we'll need to show the default side image.
		
		var productList = document.getElementById("productList"); // Get the product list section.
		var newSideImages = productList.getElementsByTagName("div"); // Get all <div> tags in product list section.
		counter = 0; // Count all side images.
		hiddenCounter = 0; // Count only side images that are currently hidden.
		
		for (i = 0; i < newSideImages.length; i++) { // Loop through all of the <div> tags in the section.
			if (newSideImages[i].className.match("sideImage")) { // Get all the <div> tags with a class of sideImage.
				counter += 1; // Add to the standard counter.
				if (newSideImages[i].style.display == "none") { //Check to see if the side images are hidden.
					hiddenCounter += 1; // Add to the hidden counter.
				}
			}
		}
		if (hiddenCounter == counter) { // If the 2 counters are equal, then all of the side images are hidden.
			defaultSideImage.style.display = "block"; // So show the default side image.
		}
	}
}

function mailConfirm(passedAddress,disclaimerText) {
	if (confirm(disclaimerText))
		window.location = passedAddress;
}


function showSection() {
	if (!document.getElementById || !document.getElementById("defaultSideImage")) { return false; }
	var thisURL = window.location.href;
	var defaultSideImage = document.getElementById("defaultSideImage");
	
	if ( thisURL.indexOf("#") != -1 && thisURL.indexOf( document.URL ) != -1 ) {
		target = thisURL.substring( thisURL.indexOf("#")+1 );	
		var el = document.getElementById(target);
		
		if (target.length) {
			el.style.height = "auto";
			defaultSideImage.style.display = "none";
			
			var sideImages = el.getElementsByTagName("div");
			for (i = 0; i < sideImages.length; i++) {
				if (sideImages[i].className.match("sideImage")) {
				  sideImages[i].style.display = "block";
				}
			}
		}
	}
}