/**
 * Extend native classes.
 */
String.prototype.trim = function(){
	return this.replace(/^\s+/, "").replace(/\s+$/, "");
};
String.prototype.isValidEmail = function(){
	return this.match( /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/ );
};

if(!Array.prototype.push){
	Array.prototype.push = function(){
		for(var i=0; i<arguments.length; i++){
			this[this.length] = arguments[i];
		}
		return this.length;
	};
}

/**
 * Replace image.
 *    @param id		id of image.
 *	  @parap src	location of new image.
 */
function swapImg(id, src){
	if(!document.getElementById) return;
	
	document.getElementById(id).src = src;
}

/**
 * Sets up image swap.  Looks for A tags with attibute "_isroll" and swaps out
 * images.  Assumes IMG is childNode[0] of A.  Images should be as follows:
 *    On State:		myimage.gif
 *    Off State:	myimage_over.gif
 **/
function initRollovers(){
	var OVER_SFX = "_over";
	
	var links = document.getElementsByTagName("a");
	var loadimg = [];
	for(var i=0; i<links.length; i++){
		lnk = links[i];
		
		// Skip links w/o attribute.
		if(!lnk.getAttribute("_isroll")) continue;
		
		var img = lnk.childNodes[0];
		img.id = "_" + i;
		
		// Get on and off state image paths.
		var src_off = img.src;
		var src_on = src_off.slice( 0, -4 ) 
					+ OVER_SFX
					+ src_off.slice( src_off.lastIndexOf(".") ) ;
		
		// Attach event handlers.
		lnk.onmouseover = Function("swapImg('" + img.id + "', '" + src_on + "')");
		lnk.onmouseout = Function("swapImg('" + img.id + "', '" + src_off + "')");
		
		// Preload image.
		loadimg[i] = new Image();
		loadimg[i].src = src_on;
		
	}
}

/**
 * Changes all links to *.pdf files to open in an IFRAME in /pdf.asp.
 **/
function initPdfLinks(){
	var links = document.links;
	for(var i=0; i<links.length; i++){
		var href = links[i].href;
		if(!href.match(/\.pdf$/i)) continue;
		links[i].href = ("/pdf.asp?file=/" + escape(links[i].pathname)).replace(/\/\//, "/");
	}
}

window.onload = function(){
	initRollovers();
	initPdfLinks();
}
