/*==========================================================================
	GLOBAL FUNCTIONS
	Functions used all over the world :)
..........................................................................*/

//	=============================================
//	Cure IE6/Win Flicker
//	=============================================
//	Prevents images from flickering on hover and 
//	and increases page rendering speed in IE6/Win
//	---------------------------------------------
try {
	document.execCommand('BackgroundImageCache', false, true);
}
catch(e) {}

//	=============================================
//	Add Load Event
//	=============================================
function addLoadEvent(func){
	var oldonload = window.onload;
	if(typeof window.onload != 'function'){
		window.onload = func;
	}else{
		window.onload = function(){
			oldonload();
			func();
		}
	}
}

//	=============================================
//	Add Class
//	=============================================
//	by JEREMY KEITH 
//	http://www.adactio.com
//	---------------------------------------------
function addClass(element,value) {
	if (!element.className) {
		element.className = value;
	} else {
		newClassName = element.className;
		newClassName+= " ";
		newClassName+= value;
		element.className = newClassName;
	}
}

//	=============================================
//	Get Elements By Class Name
//	=============================================
//	by SCOTT SCHILLER
//	http://www.schillmania.com
//	---------------------------------------------
function getElementsByClassName(className,oParent) {
	var doc = (oParent||document);
	var matches = [];
	var nodes = doc.all||doc.getElementsByTagName('*');
	for (var i=0; i<nodes.length; i++) {
		if (nodes[i].className == className || nodes[i].className.indexOf(className)+1 || nodes[i].className.indexOf(className+' ')+1 || nodes[i].className.indexOf(' '+className)+1) {
			matches[matches.length] = nodes[i];
		}
	}
	return matches; // kids, don't play with fire. ;)
}

/*==========================================================================
	CUSTOM FUNCTIONS
..........................................................................*/

//	=============================================
//	Gray Initial Values
//	=============================================
//	Clear initial field values when clicked
//	---------------------------------------------
function greyInitialValues(){
	var filled = getElementsByClassName("filled");

	if(filled.length > 0){
		for(var i = 0; i < filled.length; i++){
			filled[i].className = "empty";
			filled[i].initialValue = filled[i].value;

			filled[i].onclick = filled[i].onfocus = function(){
				this.className = "filled";
				if(this.value == this.initialValue){
					this.value= "";
				}
			}
			
			filled[i].onblur = function(){
				if(this.value == this.initialValue || this.value == ""){
					this.className = "empty";
					this.value = this.initialValue;
				}else{
					this.className = "filled";
				}
			}
		}
	}else{ 
		return; 
	}
}
addLoadEvent(greyInitialValues);


//	=============================================
//	Change Input Type
//	=============================================
//	Set password field types
//	---------------------------------------------
function changeInputType(oldElm, iType,	iValue, blankValue,	noFocus) { 
	if(!oldElm || !oldElm.parentNode || (iType.length<4) || !document.getElementById || !document.createElement) return;
	var newElm = document.createElement('input');
	newElm.type = iType;
	if(oldElm.name) newElm.name = oldElm.name;
	if(oldElm.id) newElm.id = oldElm.id;
	if(oldElm.className) newElm.className = oldElm.className;
	if(oldElm.size) newElm.size = oldElm.size;
	if(oldElm.tabIndex) newElm.tabIndex = oldElm.tabIndex;
	if(oldElm.accessKey) newElm.accessKey = oldElm.accessKey;
	newElm.onfocus = function(){return function(){
		if(this.hasFocus) return;
		var newElm = changeInputType(this,'password',iValue,
		(this.value.toLowerCase()==iValue.toLowerCase())?true:false);
		if(newElm) newElm.hasFocus=true;
	}}();
	
	newElm.onblur = function(){return function(){
		if(this.hasFocus)
		if(this.value=='' || (this.value.toLowerCase()==iValue.toLowerCase())) {
			changeInputType(this,'text',iValue,false,true);
		}
	}}();
	
	// hasFocus is to prevent a loop where onfocus is triggered over and over again
	newElm.hasFocus=false;
	oldElm.parentNode.replaceChild(newElm,oldElm);
	if(!blankValue) newElm.value = iValue;
	if(!noFocus || typeof(noFocus)=='undefined') {
		window.tempElm = newElm;
		setTimeout("tempElm.hasFocus=true;tempElm.focus();",1);
	}
	return newElm;
}

addLoadEvent(function(){
	var ua = navigator.userAgent.toLowerCase();
	if(!((ua.indexOf('konqueror')!=-1) && (document.all || (ua.indexOf('khtml/3.4')!=-1))) && !(((ua.indexOf('safari')!=-1) && !window.print) || (document.defaultCharset && !window.print))) {
      changeInputType(document.getElementById("password"),'text','Password',false,true);
  }
});


//	=============================================
//	AUTO TAB - not needed because phone field is 1 long field*****************
//	=============================================
//	Once the maxlength is reached, focus on the 
//	next field
//	---------------------------------------------
function autoTab(){
	var tabbers = Array();
	if(document.getElementById("phone1")) {
		tabbers[0] = document.getElementById("phone1");
		if(document.getElementById("phone2")) {
			tabbers[0]["next"] = document.getElementById("phone2");
		}
	}
	if(document.getElementById("phone2")) {
		tabbers[1] = document.getElementById("phone2");
		if(document.getElementById("phone3")) {
			tabbers[1]["next"] = document.getElementById("phone3");
		}
	}
	
	if(tabbers.length > 0){
		for(var i = 0; i < tabbers.length; i++){
			tabbers[i].onkeyup = function(){
				var maxlength = this.getAttribute("maxlength");
				if(this.value.length >= this.getAttribute("maxlength")) {
					this["next"].focus();
				}
			}
		}
	}else{ 
		return; 
	}
}
//addLoadEvent(autoTab);


//	=============================================
//	ADD PRINT LINK
//	=============================================
function addPrintLink() {
	
	var utilityLists = getElementsByClassName("utility");
	
	if(utilityLists.length > 0){
		for(var i = 0; i < utilityLists.length; i++){
			var printLink = document.createElement("li");
			printLink.className = "print";
			var printLink_a = document.createElement("a");
			printLink_a.setAttribute("href", "javascript:void(0);");
			var printLink_a_text = document.createTextNode("Print Version");
			printLink_a.appendChild(printLink_a_text);
			printLink.appendChild(printLink_a);
		
			var newLink = utilityLists[i].appendChild(printLink);
			newLink.onclick = function(){
				window.print();
			}
		}
	} else {
		return;
	}
}
addLoadEvent(addPrintLink);


//	=============================================
//	READ/WRITE COOKIES
//	=============================================
function createCookie(name,value,days){
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else {
		expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name){
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

//	=============================================
//	ADD TEXT SIZE LINKS
//	=============================================
var TEXT_NORMAL = "100%";
var TEXT_LARGER = "120%";
var TEXT_LARGEST = "140%";

function addTextLinks(){
	var utilityLists = getElementsByClassName("utility");
	var linksArr = new Array();
	
	if(utilityLists.length > 0){
		for(var i = 0; i < utilityLists.length; i++){
			//NORMAL
			var dd_normal = document.createElement("dd");
				dd_normal.className = "normal";
			var dd_normal_a = document.createElement("a");
				dd_normal_a.setAttribute("href", "#");
				dd_normal_a.setAttribute("title", "Set text size to normal");
				dd_normal_a.onclick = function() { 
					setTextSize(TEXT_NORMAL);
					setCurrentStates("normal");
					return false;
				}
			var dd_normal_text = document.createTextNode("Normal");
			dd_normal_a.appendChild(dd_normal_text);
			dd_normal.appendChild(dd_normal_a);
			
			//LARGER
			var dd_larger = document.createElement("dd");
				dd_larger.className = "larger";
			var dd_larger_a = document.createElement("a");
				dd_larger_a.setAttribute("href", "#");
				dd_larger_a.setAttribute("title", "Set text size to larger");
				dd_larger_a.onclick = function() { 
					setTextSize(TEXT_LARGER);
					setCurrentStates("larger");
					return false;
				}
			var dd_larger_text = document.createTextNode("Larger");
			dd_larger_a.appendChild(dd_larger_text);
			dd_larger.appendChild(dd_larger_a);
			
			//LARGEST
			var dd_largest = document.createElement("dd");
				dd_largest.className = "largest";
			var dd_largest_a = document.createElement("a");
				dd_largest_a.setAttribute("href", "#");
				dd_largest_a.setAttribute("title", "Set text size to largest");
				dd_largest_a.onclick = function() { 
					setTextSize(TEXT_LARGEST);
					setCurrentStates("largest");
					return false;
				}
			var dd_largest_text = document.createTextNode("Largest");
			dd_largest_a.appendChild(dd_largest_text);
			dd_largest.appendChild(dd_largest_a);
			
			//CONSTRUCT AND INSERT DL
			var textLinks = document.createElement("li");
				textLinks.className = "text-size";
			var dl = document.createElement("dl");
			
			var dt = document.createElement("dt");
			var dt_text = document.createTextNode("Text Size");
			dt.appendChild(dt_text);
			
			dl.appendChild(dt);
			dl.appendChild(dd_normal);
			dl.appendChild(dd_larger);
			dl.appendChild(dd_largest);
			textLinks.appendChild(dl);
			
			utilityLists[i].appendChild(textLinks);
		}
		
		//If cookie exists, use saved text size. Else, use default
		var cookie = readCookie("robertwillens");
		if(cookie != null){
			setTextSize(cookie);
			if(cookie == TEXT_NORMAL){
				setCurrentStates("normal");
			} else if(cookie == TEXT_LARGER) {
				setCurrentStates("larger");
			} else if(cookie == TEXT_LARGEST) {
				setCurrentStates("largest");
			}
		} else {
			setTextSize(TEXT_NORMAL);
			setCurrentStates("normal");
		}
	} else {
		return;
	}
}

addLoadEvent(addTextLinks);

function setTextSize (textSize) {
	if(document.getElementById){
		document.getElementById("report").style.fontSize = textSize;
		createCookie("robertwillens", textSize, 365);
	}
}

function setCurrentStates (size) {
	var currentList = getElementsByClassName("current");
	for(var i=0; i < currentList.length; i++) {
		currentList[i].className = currentList[i].className.replace(/\s*current/, "");
	}
	
	var setList = getElementsByClassName(size);
	for(var i=0; i < setList.length; i++) {
		addClass(setList[i], "current");
	}
}





