/* 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;
}
function getQueryVariable(varName) {
  var queryString = window.location.search.substring(1);
  var vars = queryString.split("&");
  for(var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == varName)
      return pair[1];
  } 
  return "";
}

/* String functions */
function trim(str) {
	var returnStr = str;
	if(returnStr == null)
		returnStr = "";
	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; 
}
function padLeft(stringObj, length, padChar) {
	var result = "" + stringObj;
	while(result.length < length)
		result = padChar + result;
	return result;
}
function padRight(stringObj, length, padChar) {
	var result = "" + stringObj;
	while(result.length < length)
		result = result + padChar;
	return result;
}
function startsWith(wholeString, compareString) {
	var index = wholeString.indexOf(compareString);
	return index == 0;
}
function endsWith(wholeString, compareString) {
	var index = wholeString.indexOf(compareString);
	return index >= 0 && index == (wholeString.length - compareString.length);
}
function getIntFromString(stringObj, startAt, endAt, minValue, maxValue, defaultValue) {
	var possibleNum = stringObj.substring(startAt, endAt);
	if(isNaN(possibleNum))
		return defaultValue;
	if(possibleNum < minValue || possibleNum > maxValue)
		return defaultValue;
	return Number(possibleNum);
}

/* Email functions */
function emailCount(emailString) {
	var result = 0;
	for(var i=0;i<emailString.length;i++) {
		if(emailString.charAt(i) == "@")
			result++;
	}
	return result;
}
function checkForValidEmail(emailString) {
	var result = true;
	if(emailString == "")
		return result;
	var emails = emailString.split(",");
	var curEmail;
	for(var 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;
}

/* Form related functions */
function scrollToFirstSelected(selectObj) {
	var bottomIndex = selectObj.selectedIndex + selectObj.size - 1;
	if(bottomIndex >= selectObj.options.length)
		bottomIndex = selectObj.options.length-1;
	//alert(selectObj.selectedIndex + ":" + bottomIndex);
	var bottomSelected = selectObj.options[bottomIndex].selected;
	selectObj.options[bottomIndex].selected = true;
	selectObj.options[bottomIndex].selected = bottomSelected;
}


function addOption(selectObj, optionObj, index) {
	if(index == -1) {
		try {
			selectObj.add(optionObj, null);
		} catch(ex) {
			selectObj.add(optionObj); //IE only
		}
	} else {
		try {
			selectObj.add(optionObj, selectObj.options[index]);
		} catch(ex) {
			selectObj.add(optionObj, index); //IE only
		}
	}
}
function removeOption(selectObj, index) {
	selectObj.remove(index);
}


/* Popup related functions */
var dialogWin = new Object();
		
function popupWindow(theURL,winName, w, h, forceClose) {
	var horz = screen.width/2 - w/2;
	var vert = screen.height/2 - h/2;
	var windowProperties = "resizable=yes, scrollbars=yes, width=" + w + ", height=" + h + ", left=" + horz + ", top=" + vert;
	if(forceClose) {
		dialogWin.forceWin = window.open(theURL, winName, windowProperties);
		dialogWin.forceWin.focus();
	} else {
		dialogWin.win = window.open(theURL, winName, windowProperties);
		dialogWin.win.focus();
	}
}
function checkPopup() {
	if(dialogWin.forceWin && !dialogWin.forceWin.closed)
		dialogWin.forceWin.focus();
}

var ieBody = (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
var modalName = null;

function resetModal() {
	if(modalName == null) {
		hideModalBackground();
		return;
	}
		
	var modalBackground = findObj("shadeDiv");
	modalBackground.className = "showModal";
	var pageSize = findObj("pageTable");
	var windowHeight = document.all ? ieBody.clientHeight : window.innerHeight;
	modalBackground.style.width = pageSize.offsetWidth + "px";
	modalBackground.style.height = pageSize.offsetHeight + "px";
	if(windowHeight > pageSize.offsetHeight)
		modalBackground.style.height = windowHeight + "px";
	modalBackground.style.top = pageSize.offsetTop + "px";
	modalBackground.style.left = pageSize.offsetLeft + "px";
	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 + "px";
	objectToPlace.style.top = positionTop + "px";
}

/* Browser window size */
function getBrowserWidth() {
	var width = 600;
	if (parseInt(navigator.appVersion) > 3 && navigator.appName == "Netscape")
  	width = window.innerWidth - 16;
	else if (navigator.appName.indexOf("Microsoft") != -1)
  	width = document.body.offsetWidth - 20;
	return width;
}
function getBrowserHeight() {
	var height = 400;
	if (parseInt(navigator.appVersion) > 3 && navigator.appName == "Netscape")
  	height = window.innerHeight - 16;
	else if (navigator.appName.indexOf("Microsoft") != -1)
  	height = document.body.offsetHeight - 20;
	return height;
}

/* Object position
		created by Peter-Paul Koch & Alex Tingle: http://blog.firetree.net/2005/07/04/javascript-find-position/
*/
function findPosX(obj) {
	var curleft = 0;
	if(obj.offsetParent)
		while(1) {
			curleft += obj.offsetLeft;
			if(!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}
function findPosY(obj) {
	var curtop = 0;
	if(obj.offsetParent)
		while(1) {
			curtop += obj.offsetTop;
			if(!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}