/* Basic javascript functions */
function findObj(objName) {
	var x = null;
	if(document.getElementById)
		x = document.getElementById(objName);
	else if(document.all)
		x = document.all[objName];
	else if(document.layers)
		x = document.layers[objName];
	return x;
}

/* String functions */
function trim(str) {
	var returnStr = str;		
	while(returnStr.length > 0 && returnStr.charAt(0) == ' ')
		returnStr = returnStr.substring(1, returnStr.length);
	while(returnStr.length > 0 && returnStr.charAt(returnStr.length-1) == ' ')
		returnStr = returnStr.substring(0, returnStr.length-1);		
	return returnStr;
}
function trimAndReplace(textFieldObject) {
	var newText = trim(textFieldObject.value);
	textFieldObject.value = newText;
	return newText; 
}

/* Pseudo popup related functions */
var ieBody = (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
var modalName = null;

function resetModal() {
	if(modalName == null)
		return;	
		
	var modalBackground = findObj("shadeDiv");
	modalBackground.className = "showModal";
	var pageSize = findObj("pageTable");
	modalBackground.style.width = pageSize.offsetWidth;
	modalBackground.style.height = pageSize.offsetHeight;
	modalBackground.style.top = pageSize.offsetTop;
	modalBackground.style.left = pageSize.offsetLeft;
	centerObjectOnScreen(modalName);
}
function hideModalBackground() {
	var modalBackground = findObj("shadeDiv");
	modalBackground.className = "hideModal";
}

function centerObjectOnScreen(objName) {
	var objectToPlace = findObj(objName);
	var windowWidth = document.all ? ieBody.clientWidth : window.innerWidth;
	var windowHeight = document.all ? ieBody.clientHeight : window.innerHeight;
	var offsetLeft = document.all ? ieBody.scrollLeft : window.pageXOffset;
	var offsetTop = document.all ? ieBody.scrollTop : window.pageYOffset;
	var objectWidth = objectToPlace.offsetWidth;
	var objectHeight = objectToPlace.offsetHeight;
	
	var positionLeft = offsetLeft + (.5*windowWidth) - (.5*objectWidth);
	var positionTop = offsetTop + (.5*windowHeight) - (.5*objectHeight);
	if(positionLeft < offsetLeft)
		positionLeft = offsetLeft + 5;
	if(positionTop < offsetTop)
		positionTop = offsetTop + 5;
	
	objectToPlace.style.left = positionLeft;
	objectToPlace.style.top = positionTop;
}

/* Brings up a Pip Info box */
function showPipInfo(boxName) {
	modalName = boxName;
	var objectToPlace = findObj(modalName);
	objectToPlace.className = "pipInfoShow";
	resetModal();
	return false;
}
function hidePipInfo() {
	var objectToPlace = findObj(modalName);
	objectToPlace.className = "pipInfoHide";
	modalName = null;
	hideModalBackground();
	return false;
}

/* Email functions */
function sendMessage(userName) {
	document.message_form.user_name.value = userName;
	document.message_form.submit();
	return false;
}
function displayAddress(selectObject) {
	var index = selectObject.selectedIndex;
	var value = selectObject.options[index].value;
	var address = value;
	if(value != "")
		address = value + "@chsra.wisc.edu <span class='footnote'><a href='/chsra/create_message.asp?user_name=" + value + "'>send a message</a></span>";
	var addressDiv = findObj("addressSection");
	addressDiv.innerHTML = address;
}
function emailCount(emailString) {
	var result = 0;
	for(j=0;j<emailString.length;j++) {
		if(emailString.charAt(j) == "@")
			result++;
	}
	return result;
}
function checkForValidEmail(emailString) {
	var result = true;
	if(emailString == "")
		return result;
	var emails = emailString.split(",");
	var curEmail;
	for(i=0;i<emails.length;i++) {
		curEmail = trim(emails[i]);
		
		if(!isEmailFormat(curEmail))
			result = false;
	}
	return result;
}
function isEmailFormat(emailString) {
	var result = true;
	
	var emailExpression = /^[\w]{1}[\w\.]*@[\w]{1}[\w\.]*$/;
	result = emailExpression.test(emailString);
	
	return result;
}
