var user_agent = navigator.userAgent.toLowerCase();

function setTextSize(size) {
	if(document.getElementsByTagName) {
		var bodyTag = document.getElementsByTagName("body");
		var textsize = document.getElementById('textsize');
		
		if(bodyTag && textsize) {			
			switch(size) {
				case "small": 
					document.getElementById('medium').className = "medium";
					document.getElementById('large').className = "large";
					bodyTag.item(0).style.lineHeight = "16px";
					bodyTag.item(0).style.fontSize = "14px";			
					break;
				case "medium": 
					document.getElementById('small').className = "small";
					document.getElementById('large').className = "large";
					bodyTag.item(0).style.lineHeight = "16px";
					bodyTag.item(0).style.fontSize = "16px";
					break;
				case "large": 
					document.getElementById('small').className = "small";
					document.getElementById('medium').className = "medium";
					bodyTag.item(0).style.lineHeight = "18px";
					bodyTag.item(0).style.fontSize = "18px";
					break;
			}
			document.getElementById(size).className += " selected";
						
			// Set code to add this setting to a cookie; Invalidate the cookie after session?
			var today = new Date();
			var zero_date = new Date(0,0,0);
			today.setTime(today.getTime() - zero_date.getTime());
			var cookie_expire_date = new Date(today.getTime() + (8 * 7 * 86400000)); 
			setCookie("textsize", size, cookie_expire_date, "/", "", null);
		}
	}
}

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function ieRefresh() {
	if (document.all && user_agent.indexOf("Opera") == -1) {
		history.go(0);
	}
}
  
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


function acs_init() {
	
	//check for textsize	
	if(getCookie("textsize")) {		
		setTextSize(getCookie("textsize"));
	}
	
}


function trim(str) {
return( ("" + str).replace(/^\s+/,'').replace(/\s+$/,'') );
}

function checkFields(fArray, sButton) {
var missinginfo = "";
	for (var i=0; i < fArray.length; i++) {
		if ( trim(fArray[i].value) == null || trim(fArray[i].value) == "" ) {
			missinginfo += "- " + document.getElementById(fArray[i].name).getAttribute('title') + "\n";
			fArray[i].value = "";
			fArray[i].style.backgroundColor = '#ffcccc';
		} else {
			fArray[i].style.backgroundColor = '#ffffff';
		}
	}
	
	if (missinginfo != "") {
		alert("Please complete the required fields before submitting!\n\n" + missinginfo);
		return false;
	} else {
		sButton.disabled = true;
		return true;
	}
}



function selectChange ( url, formName ) {
	var queryName = formName.name;
	var queryValue = formName.options[formName.selectedIndex].value;
		if ( queryValue == "ALL" ) {
			queryString = '?' + queryName + '=';
		}
		else if ( queryValue != "" ) {
			queryString = '?' + queryName + '=' + queryValue;
		}
	location.href =  url + queryString;
}


var startList = function() {
	if ( document.all && document.getElementById ) {
		var navRoot = document.getElementById("navlist");
		for ( var i=0; i < navRoot.childNodes.length; i++) {
			var node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover = function() {
					this.className+= " over";
				}
				node.onmouseout = function() {
					this.className = this.className.replace(" over", "");
				}
			}
		}
	}
}


function popupWin(link,attribs) {
    var popupWin = null;
    popupWin = window.open(link,'winPopup',attribs);  
}

function popupWindows() {
    if( !document.getElementsByTagName ) {
         return;
    }
    var scrW = screen.availWidth;
    var scrH = screen.availHeight;
    var anchors = document.getElementsByTagName("a");
    for ( var i = 0; i < anchors.length; i++ ) {
         var anchor = anchors[i];
         var linkDest = anchor.getAttribute("href");
         var relIndex = anchor.getAttribute("rel");
         var relSplit = relIndex.split("|");
         var windowAttributes = "";
         if( relSplit[0] == "popup" ) {
			if (relSplit[1] > scrW) {
				var pW = scrW - 10;
			} else {
				var pW = relSplit[1];
			}
			
			if (relSplit[2] > scrH) {
				var pH = scrH - 40;
			} else {
				var pH = relSplit[2];
			}
			var scrX = (scrW - pW - 10) * .5;
			var scrY = (scrH - pH - 30) * .5;
			windowAttributes = "width=" + pW + ",height=" + pH + ",left=" + scrX + ",top=" + scrY + ",screenX=" + scrX + ",screenY=" + scrY;
			windowAttributes += ",location=" + relSplit[3] + ",resizable=" + relSplit[4] + ",scrollbars=" + relSplit[5] + ",status=1,menubar=1";
			anchor.setAttribute("href", "javascript:popupWin('" + linkDest + "','" + windowAttributes + "')");
         } else if ( relSplit[0] == "external" ) {
			anchor = anchors[i];
			linkDest = anchor.getAttribute("href");
			anchor.setAttribute("href", "javascript:popupWin('" + linkDest + "','');");
		 }
    }
}

function startMultipleOnload() {
	startList();
	acs_init();
	popupWindows();
}

window.onload = startMultipleOnload;