
// do not allow this page to be displayed in a frame
if (parent.frames.length != 0) parent.location = window.location;

// do not allow abbreviated domain use (to avoid the broken cookie problem)
var currentHost = (new String(window.location)).split('/')[2];
var currentSubdomain = 'blink-prod';
if (currentHost != currentSubdomain + '.ucsd.edu') window.location = (new String(window.location)).replace(currentSubdomain,currentSubdomain + '.ucsd.edu');

var MSIE = (navigator.userAgent.toLowerCase().search(/msie/) != -1);

function staffPopup(staff_id,h,w) {
	tidyPopup('/Blink/External/Browse/Staff_View/1,1414,'+staff_id+',00.html',h,w);
}

function staffPopupSmall(staff_id) {
	tidyPopup('/Blink/External/Browse/Staff_View/1,1414,'+staff_id+',00.html',320,475);
}

function staffPopupMedium(staff_id) {
	tidyPopup('/Blink/External/Browse/Staff_View/1,1414,'+staff_id+',00.html',600,485);
}

function staffPopupLarge(content_url) {
	var randName = 'staff' + parseInt(100 * Math.random());
	window.open(content_url,randName,centeredPopupCoords((screen.availWidth / 2), (screen.availHeight / 2)));
}

// Collapse and Expand Text (taken from http://iacuc.ucsd.edu/)
function blocking(tag) {
	if (document.getElementById) 	{
	//  Netscape, Mozilla, etc.
		var state = document.getElementById(tag).style.display;
	} else if (document.all) {
	     //  IE, Konqueror, etc.
		var state = document.all[tag].style.display;
	}
	var newState = "";
	if ( state == "block") {
		newState = "none";
	} else {
		newState = "block";
	}
	if (document.getElementById) {
		document.getElementById(tag).style.display = newState;
	} else if (document.all) {
		document.all[tag].style.display = newState;
	}
}


function centeredPopupCoords(xsize, ysize) {
	var positY = (screen.availHeight / 2) - (ysize / 2);
	var positX = (screen.availWidth / 2) - (xsize / 2); 
	var position;
	if (window.MSIE) position = "left=" + positX + ",top=" + positY + ",";
	else position = "screenX=" + positX + ",screenY=" + positY + ",";
	return position;
}


function emllnk (ct) {
	var alphabet = " !\"#$%'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz{|}~&";
	document.write('<a href="mailto:');
	for (i = 0; i < String(ct).length; i += 2) {
		k = String(ct).substring(i, i+2);
		document.write(String(alphabet).substring(k, parseInt(k)+1));
	}
	document.write('">');
}


function setStatus(text) {
	text = text.replace(/\'/g, "\\'");
	var statusSet = 'window.status=\''+text+'\'';
	setTimeout(statusSet,1);
	return true;
}


function standBy(message) {
	window.loading = true;
	var standByURL = "/Blink/StandBy/1,2498,,00.html?msg="+escape(message);
	var xsize = 200;
	var ysize = 100;
	var standByOptions = centeredPopupCoords(xsize, ysize);
	standByOptions += "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width="+xsize+",height="+ysize;
	window.open(standByURL,'standByWin',standByOptions);
}




var myuniquepopupwin;
function tidyPopup(theUrl,height,width) {
//begin original code
            var tpopupOptions = centeredPopupCoords(width, height);
            tpopupOptions += "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width="+width+",height="+height;
            var randName = 'tidyPopup' + parseInt(100 * Math.random());
//end original code                                  
            if(window.MSIE){
                      height+=140;
                        width+=40;
                       var positY = (screen.availHeight / 2) - (height / 2);
                       var positX = (screen.availWidth / 2) - (width / 2); 
                       myuniquepopupwin=window.open(theUrl,randName,'');
                        setTimeout("myuniquepopupwin.resizeTo("+width+","+height+");",50);
                       setTimeout("myuniquepopupwin.moveTo("+positX+","+positY+");",50);
                        }
//open window using old functionality
            else{
                        window.open(theUrl,randName,tpopupOptions);
                        }
}




setTimeout('setCursor();',100);

function setCursor(cType) {
	if (typeof(cType) == 'undefined' || cType == '') cType = 'default';
	if (document.body) {
		if (document.body.style) {
			if (document.body.style.cursor) document.body.style.cursor = cType;
		} else {
			if (document.body.cursor) document.body.cursor = cType;
		}
	}
}


function openGallery(picField) {
	window.open("/Blink/Internal/Gallery/Chooser/1,1165,,00.html?formName=contentForm&picField="+picField, "chooser", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=400,height=400");
	return false;
}


function sidebarGallery(pthField, destForm) {
	window.open("/Blink/Internal/Gallery/Chooser/1,1165,,00.html?formName="+destForm+"&pthField="+pthField, "sb_chooser", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=400,height=400");
	return false;
}


function openTopicChooser(titleField, urlField, destForm) {
	window.open("/Blink/External/Topics/Chooser/1,1176,,FF.html?formName="+destForm+"&topicTitle="+titleField+"&topicURL="+urlField, "topicChooser", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=700,height=300");
	return false;
}


function liveArticleChooser(titleField, urlField, destForm, siteField) {
	window.open("/Blink/External/Topics/Chooser/1,1176,,FF.html?topicSite="+siteField+"&formName="+destForm+"&topicTitle="+titleField+"&topicURL="+urlField,"topicChooser","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=700,height=300");
	return false;
}


function openHelpWindow(urlField) {
	window.open(urlField,"helpWindow","width=370,height=150,scrollbars=yes");
	return false;
}


function centerPopup(url, xDim, yDim, winAttributes) {
	var position = centeredPopupCoords(xDim, yDim);
    var windowName = window.open(url,'newWin',winAttributes+position+',height='+yDim+',width='+xDim);
    return(windowName);
}


var changed = false;
function promptUser() {
	return (!changed)||(confirm('Is it OK to abandon your changes?'));
}


function no_local_cache(old_url) {
	var new_url = old_url
	var re0 = /\/0,/;
	var re1 = /\/1,/;
	if (old_url.match(re0) == null) {
		new_url = old_url.replace(re1, "/0,");
	} else {
		new_url = old_url.replace(re0, "/1,");
	}
	return new_url;
}


function updateParentWindow() {
	if (typeof(opener) != 'undefined') {
		var idx = opener.location.toString().indexOf("#");
		if (idx != -1) var parent_address = opener.location.toString().substring(0, idx);
		else var parent_address = opener.location.toString();
		opener.location = no_local_cache(parent_address);
	}
}


function getMenuItem(lyrItem) {
    return getLayerItem(lyrItem);
}


function getLayerItem(lyrItem) {
    if (document.getElementById) {
	    return document.getElementById(lyrItem)
    } else if (document.layers) {
		// ns 4.x
	    return document.layers[lyrItem]
    } else if (document.all) {
	    return eval('document.all.' + lyrItem)
    }
}


function getObject(id) {
	var handle;
	if (document.getElementById) {
		handle = document.getElementById(id);
		handle = handle.style;
	} else if (document.all){
		handle = eval('document.all.' + id);
		handle = handle.style;
	} else if (document.images){
		handle = document.images[id];
	}
	return handle;
}


function getValue(name) {
	var params = (new String(window.location)).split('?')[1];
	if (params) {
		params = params.split('&');
		var identifier;
		for (p in params) {
			identifier = params[p].split('=')[0];
			if (identifier == name) return params[p].split('=')[1];
		}
	}
	return "";
}


function noEmptySearch(formName) {
	var form = eval('document.'+formName);
	if (form && form.google_query && form.google_query.value == '') return false;
	else return true;
}




function logoutOver(src) {
	if (typeof(window.jlink_active) != 'undefined' && window.jlink_active) {
		src.style.cursor = 'pointer';
		setStatus('Logout off Single Sign-on and Content Manager');
	}
}

function logoutOut(src) {
	if (typeof(window.jlink_active) != 'undefined' && window.jlink_active) src.style.cursor = 'default';
}

function completeLogout(logoutType) {
	// attempt logout and reattempt later if page is still rendering
	if (typeof(window.FULLLOGOUT) != 'undefined') FULLLOGOUT(logoutType);
	else setTimeout('completeLogout(\''+logoutType+'\');',10);
}

// TritonBlink supplement

	//
	// Functions related to the search forms
	//
	var defaultSiteSearch = 'Search Blink';
	var defaultDirSearch = 'Find People';

	// ensures a user is not attempting to initiate a search without a search phrase of some kind
	function validateSearch(formName,fieldName) {
		if (eval('document.'+formName)) {
			if (eval('document.'+formName+'.'+fieldName)) {
				var value = eval('document.'+formName+'.'+fieldName+'.value');
				if (typeof(value) != 'undefined') {
					if (value != '' && value != window.defaultSiteSearch && value != window.defaultDirSearch) return true;
					else alert('You must enter a search phrase.');
				}
			}
		}
		return false;
	}
	// invoked from search text onclick and onblur events
	// sets to or clears the value 'Search for:' as appropriate
	function fieldSet(formName,fieldName,newText) {
		if (eval('document.'+formName)) {
			if (eval('document.'+formName+'.'+fieldName)) {
				eval('document.'+formName+'.'+fieldName+'.value = \''+newText+'\';');
			}
		}
	}
	function defaultField(formName,fieldName) {
		if (eval('document.'+formName)) {
			if (eval('document.'+formName+'.'+fieldName)) {
				if (eval('document.'+formName+'.'+fieldName+'.value') == '') {
					var newText = '';
					if (formName == 'phone') newText = window.defaultDirSearch;
					if (formName == 'sitesearch') newText = window.defaultSiteSearch;
					eval('document.'+formName+'.'+fieldName+'.value = \''+newText+'\';');
				}
			}
		}
	}
	function clearDefault(formName,fieldName) {
		if (eval('document.'+formName)) {
			if (eval('document.'+formName+'.'+fieldName)) {
				if (eval('document.'+formName+'.'+fieldName+'.value') == window.defaultDirSearch || eval('document.'+formName+'.'+fieldName+'.value') == window.defaultSiteSearch) eval('document.'+formName+'.'+fieldName+'.value = \'\';');
			}
		}
	}

// AJAX for the snippet popup stuff

	
	function popupSnippet(url,label,height,width) {
		theUrl = '/popupSnippet?url=' + escape(url) + '&label=' + label;
		tidyPopup(theUrl,height,width);
		return false;
	}
	
	var snippetStatus = 'closed';
	var snippetHeight = '0px';
	var snippetWidth = '0px';

	// Load the ajax XML document
	function loadXMLDocPost(strURL,strQuery) {
showText(strURL + '?' + strQuery);
		var xmlHttpReq = false;
		if (window.XMLHttpRequest) {
			// Mozilla / Safari
			xmlHttpReq = new XMLHttpRequest();
			xmlHttpReq.overrideMimeType('text/xml');
		} else {
			// IE
			if (window.ActiveXObject) xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (xmlHttpReq) {
			xmlHttpReq.open('POST', strURL, true);
			xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			xmlHttpReq.onreadystatechange = function() {if (xmlHttpReq.readyState == 4) updateSnippet(xmlHttpReq.responseText);}
			xmlHttpReq.send(strQuery);
		}
	}
	// Accept XML text for parsing and formatting to the 'snippet' HTML div element
	function updateSnippet(xml){
		var snippet = document.getElementById("snippet");
		if (snippet) {
			var title = xml.match("(<title>)(.*)(</title>)")[2];
			var snip = xml.match("(<snippet>)(.*)(</snippet>)")[2];
			var topval = parseInt(getScrollY() + (document.body.clientHeight / 2) - (parseInt(window.snippetHeight) / 2)) + 'px';
			var leftval = parseInt(getScrollX() + (document.body.clientWidth / 2) - (parseInt(window.snippetWidth) / 2)) + 'px';
			if (snippet.style.posTop) snippet.style.posTop = topval;
			else if (snippet.style.top) snippet.style.top = topval;
			if (snippet.style.posLeft) snippet.style.posLeft = leftval;
			else if (snippet.style.left) snippet.style.left = leftval;
			snippet.style.height = window.snippetHeight;
			snippet.style.width = window.snippetWidth;
			snippet.style.visibility = 'visible';
			snippet.innerHTML = '<b>' + title + '</b>';
			snippet.innerHTML += '<p>' + snip + '</p>';
		}
	}
	// Make the 'snippet' div invisible again when a user clicks it.
	function closeSnippet() {
		var snippet = document.getElementById("snippet");
		if (snippet) {
			var topval = 0 + 'px';
			var leftval = 0 + 'px';
			window.snippetHeight = '0px';
			window.snippetWidth = '0px';
			snippet.style.visibility = 'hidden';
			snippet.innerHTML = '';
			snippet.style.height = window.snippetHeight;
			snippet.style.width = window.snippetWidth;
			if (snippet.style.posTop) snippet.style.posTop = topval;
			else if (snippet.style.top) snippet.style.top = topval;
			if (snippet.style.posLeft) snippet.style.posLeft = leftval;
			else if (snippet.style.left) snippet.style.left = leftval;
			window.snippetStatus = 'closed';
		}
		return false;
	}
	// Prepare to call AJAX service for the remote snippet content
	function openSnippet(url,label,height,width) {
		if (window.snippetStatus == 'open') return closeSnippet();
		window.snippetStatus = 'open';
		window.snippetHeight = parseInt(height) + 'px';
		window.snippetWidth = parseInt(width) + 'px';
		theUrl = '/AJAX-Snippet';
		theQuery = 'url=' + escape(url) + '&label=' + label;
		loadXMLDocPost(theUrl,theQuery);
		return false;
	}
	// determine X offset via scroll
	function getScrollX() {
		var scrOfX = 0;
		//Netscape compliant
		if( typeof( window.pageYOffset ) == 'number' ) scrOfX = window.pageXOffset;
		else {
			//DOM compliant
			if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) scrOfX = document.body.scrollLeft;
			else {
				//IE6 standards compliant mode
				if( document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) scrOfX = document.documentElement.scrollLeft;
			}
		}
		return scrOfX;
	}
	// determine Y offset via scroll
	function getScrollY() {
		var scrOfY = 0;
		//Netscape compliant
		if( typeof( window.pageYOffset ) == 'number' ) scrOfY = window.pageYOffset;
		else {
			//DOM compliant
			if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) scrOfY = document.body.scrollTop;
			else {
				//IE6 standards compliant mode
				if( document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) scrOfY = document.documentElement.scrollTop;
			}
		}
		return scrOfY;
	}
	
	function showText(txt) {
		var bod = document.getElementsByTagName('body').item(0);
		if (bod) {
			var t = document.createTextNode(txt);
			bod.appendChild(t);
		}
	}
	
	
