// openwin: open a new window  for all POSNA pages
function openwin(sURL) {
	windowTwo=open(sURL,"newwin", "status=yes,resizable=yes,scrollbars=yes,toolbar=yes,location=yes,width=640,height=480");
}

// Check values from the login form on the homepage
function chkloginform (){
	if (loginform.last_name.value == "") {
		alert( "Please enter your Last Name." );
		event.returnValue=false; 
	}
	if (loginform.customer.value == "") {
		alert( "Please enter your Password." );
		event.returnValue=false;
	}
}

// Add event handler to body when window loads
function addLoadEvent(func) {
	var oldonload = window.onload;
	
	if (typeof window.onload != "function") {
		window.onload = func;
	} else {
		window.onload = function () {
			oldonload();
			func();
		}
	}
}

addLoadEvent(function () {
	Callouts.fix();
	DocLinks.init();
	AnyFields.init();
	Skel.init();
});


/*---------------------------------------------------------------------------+
 | AnyFields - Add/remove "Any" value to fields that can accept blank values |
 +---------------------------------------------------------------------------*/
var AnyFields = {
	init : function () {
		// Check for functionality
		if (!document.getElementById || !document.getElementsByTagName) return false;
		
		// Get all inputs
		var fields = document.getElementsByTagName("input");
		
		for (var i = 0; i < fields.length; i++) {
			var theField = fields[i];
			
			// Check for class
			if (theField.className.indexOf("any") > -1) {
				// Only change value if it's initially empty
				if (theField.value == "") {
					theField.value = (theField.id == "q") ? "Search POSNA.org" : "Any";
				}
				
				// Add behaviors
				AnyFields.addBehaviors(theField);
			}
		}
	},
	
	addBehaviors : function(field) {
		// When focus is set to fields, remove default value and class so it looks normal
		field.onfocus = function() {
			if (this.value == "" || this.value == "Any" || this.value == "Search POSNA.org") {
				this.value = "";
			}
			
			this.className = this.className.replace(" any", "");
		};
		
		// When focus is removed, reset class and default value only if blank
		field.onblur = function() {
			if (this.value == "") {
				this.className += " any";
				
				this.value = (this.id == "q") ? "Search POSNA.org" : "Any";
			}
		};
	}
};


/*------------------------------------------------------------------------+
 | Callouts - Adjust widths of callouts depending on size of image within |
 +------------------------------------------------------------------------*/
var Callouts = {
	fix : function() {
		// Check for functionality
		if (!document.getElementById || !document.getElementsByTagName) return false;
		
		var bin = document.getElementById("content-primary");
		var arrBins = bin.getElementsByTagName("*");
		var classRE = /call-[lr]/gi;
		
		// Set div width = largest image width
		for (var i = 0; i < arrBins.length; i++) {
			if (classRE.test(arrBins[i].className)) {
				var images = arrBins[i].getElementsByTagName("img");
				
				if (images.length >= 1) {
					var maxWidth = images[0].offsetWidth;
					
					for (var j = 0; j < images.length; j++) {
						var curWidth = images[j].offsetWidth;
						if (curWidth > maxWidth) maxWidth = curWidth;
					}
					
					//if (maxWidth > 100) {
						arrBins[i].style.width = maxWidth + "px";
					//}
				}
			}
		}
		
		return false;
	}
};


/*----------------------------------------------+
 | DocLinks - Add icon after links to documents |
 +----------------------------------------------*/
var DocLinks = {
	init : function() {
		// Find all links
		var links = document.getElementsByTagName("a");
		
		for (var i = 0; i < links.length; i++) {
			var theLink = links[i];
			var address = theLink.href.toLowerCase();
			
			// Check if link points to files with common extensions
			var matches = address.match(/\.(doc|pdf|xls|ppt)/);
			
			if (matches) {
				// Using "match" always returns two results (not sure why)
				var ext = matches[0].substr(1, 3);
				
				// Create new image and insert it
				var newImg = document.createElement("img");
				newImg.alt = "(" + ext.toUpperCase() + ")";
				newImg.className = "icon";
				newImg.src = "/images/icon-" + ext + ".gif";
				newImg.title = newImg.alt;
				
				if (theLink.getElementsByTagName("img").length <= 0)
					theLink.parentNode.insertBefore(newImg, theLink);
				
				// Make link open in new window/tab
				theLink.onclick = function () {
					window.open(this.href);
					return false;
				};
			}
		}
	}
};
/*----------------------------------------------+
 | Skel - Add interactivity to skeleton diagram |
 +----------------------------------------------*/
var Skel = {
	init : function() {
		var skeleton = document.getElementById("skeleton");
		if (!skeleton) return false;
		var links = skeleton.getElementsByTagName("a");
		
		for (var i = 0; i < links.length; i++) {
			var theLink = links[i];
			var txt = theLink.childNodes[0];
			theLink.onmouseover = function() {
				document.getElementById("label").innerHTML = this.innerHTML;
			};
			theLink.onmouseout = function() {
				document.getElementById("label").innerHTML = "";
			};
		}
	}
};