// ******Attaching Mapping Events to IDs*******

$ = function(id) { return document.getElementById(id) };

if (document.addEventListener) {
	addEvent = function(id, event, func) {$(id).addEventListener(event, func, false)};
} else if(document.attachEvent){
	addEvent = function(id, event, func){
	    $(id)["e"+event+func] = func;
	    $(id)[event+func] = function() { $(id)["e"+event+func]( window.event ); };
	    $(id).attachEvent('on'+event, $(id)[event+func]);
	};
}

// ******Attaching Onload Events***************
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
		  if (oldonload) {
		    oldonload();
		  }
		  func();
	}
	}
}

// ************************************************
// Javascript Functions

// ******Site Search Functions*******
clearText = function() {
	if(this.value == "Search or Tracking") {
		this.value = ""
	}
};

addDefaultText = function() {
	if(this.value == "") {
		this.value = "Search or Tracking"
	}
};


// ***** Login Functions ************
clearUsernameText = function()	{
	if(this.value == "Username") {
		this.value = ""
	}
};

clearPasswordText = function()	{
	if(this.value == "Password") {
		this.value = ""
	}
};

addUsernameText = function() {
	if(this.value == "") {
		this.value = "Username"
	}
};

addPasswordText = function() {
	if(this.value == "") {
		this.value = "Password"
	}
};

// ************************************************

displayContactForm = function()	{
	var enquiry = this.value
	if(enquiry!=""){
		for(var i = 1; i<document.forms['contact_form'].enquiry_selected.options.length; i++) {
			document.getElementById(document.forms['contact_form'].enquiry_selected.options[i].value).className = "hidden"
		}
		document.getElementById(enquiry).className = "reveal"
	}
};

// **************Image Mouseover Functions***************

changePrintImage = function() {
	var image = document.getElementById("print-icon")
	image.onmouseover = function() { changeImage(this); }
	image.onmouseout = function() { changeImage(this); }
	
};

changeImage = function(image) {
	var src = image.src
	
	if(/_lo/.test(src)) {
		src = src.replace("_lo","_hi")
	} else {
		src = src.replace("_hi","_lo")
	}
	
	image.src = src
};

//*********Add New Window Links to page**************

externalLinks = function() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
		 anchor.target = "_blank";
	}
};

// ************************************************

// ******** Login Cookie show/hide ****************

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function showLoginStatus() {
	if(getCookie("UMSParceline")) {
		if($("logout-sidebar")) {
			$("logout-sidebar").style.display = "block";
		}
		$("logout-ultity").style.display = "inline";
	} else {
		if($("login-sidebar")) {
			$("login-sidebar").style.display = "block";
		}
		$("login-ultity").style.display = "inline";
	}
}
