function validateEmail(theAddress)
{
	var returnValue = true;
	var AtSym       = theAddress.indexOf('@');
	var Period      = theAddress.lastIndexOf('.');
	var Space       = theAddress.indexOf(' ');
	var Length      = theAddress.length - 1;  // Array is from 0 to length-1

	// '@' cannot be in first position, Must be at least one valid char btwn '@' and '.'
	// Must be at least one valid char after '.', No empty spaces permitted
	if((AtSym < 1) || (Period <= AtSym+1) || (Period == Length ) || (Space  != -1))
		returnValue = false;

	return returnValue;
}

var remote;
var windowCnt = 0;
function launchWin(helpURL, size)
{
	// size string should have the format of 'width=#,height=#'
	// this avoids having to change all the function calls to launchHelp()
	var firstEqual = size.indexOf("=")+1;
	var comma = size.indexOf(",")+1;
	var secondEqual =  size.lastIndexOf("=")+1;
	var sizeLen = size.length;
	
	var w = parseInt(size.substring(firstEqual, comma));
	var h = parseInt(size.substring(secondEqual, sizeLen));
	
	// This ensures that every open popup window has a unique name
	windowCnt += 1;
	
	var xPos = (screen.height-h)/2;
	var yPos = (screen.width-w)/2;
	if (helpURL.indexOf('viewFile.asp') < 1 )
	{
		remote = window.open(helpURL, "Artemis"+windowCnt, size+",scrollbars=1,resizable=1,left="+yPos+",top="+xPos);
	}
	else
	{
		remote = window.open(helpURL, "Artemis"+windowCnt, size+",scrollbars=1,resizable=1,left="+yPos+",top="+xPos);
	}		
	remote.focus();
}

function launchPrintWin(helpURL, size)
{
	// size string should have the format of 'width=#,height=#'
	// this avoids having to change all the function calls to launchHelp()
	var firstEqual = size.indexOf("=")+1;
	var comma = size.indexOf(",")+1;
	var secondEqual =  size.lastIndexOf("=")+1;
	var sizeLen = size.length;
	
	var w = parseInt(size.substring(firstEqual, comma));
	var h = parseInt(size.substring(secondEqual, sizeLen));
	
	// This ensures that every open popup window has a unique name
	windowCnt += 1;
	
	var xPos = (screen.height-h)/2;
	var yPos = (screen.width-w)/2;
	remote = window.open(helpURL, "Pierce"+windowCnt, size+",toolbar=1,scrollbars=1,resizable=1,left="+yPos+",top="+xPos);
	remote.focus();
}

function launchCalendar(theURL)
{
	var w = 200;
	var h = 170;
	
	var xPos = (screen.height-h)/2;
	var yPos = (screen.width-w)/2;
	var calendar = window.open(theURL, "Calendar", "width=" + w + ",height=" + h + ",scrollbars=1,resizable=1,left="+yPos+",top="+xPos);
	calendar.focus();
}
/*
function validateDate(theDate)
{
	if(theDate.indexOf("/") != -1 || theDate.indexOf("-") != -1){
		theDate = date(theDate);
		
		// Get date parts
		var theDateParts;
		if(theDate.indexOf("-") != -1)
			theDateParts = theDate.split("-");
		else if(theDate.indexOf(".") != -1)
			theDateParts = theDate.split(".");
		else
			theDateParts = theDate.split("/");
		
		// Not correct length?
		if(theDateParts.length != 3)
			return false;
		
		for(i=0; i<theDateParts.length; i++)
			if(theDateParts[i].length < 1 || isNaN(theDateParts[i]))
				return false;
		
		var theMonth = parseInt(theDateParts[0]);
		if(theMonth == 0)
		{
			theDateParts[0].replace("0", "");
			theMonth = parseInt(theDateParts[0]);
		}
		var theDay	 = parseInt(theDateParts[1]);
		if(theDay == 0)
		{
			theDateParts[1].replace("0", "");
			theDay = parseInt(theDateParts[1]);
		}
		
		if(theMonth < 1 || theMonth > 12)
			return false;
		if(theDay < 1 || theDay > 31)
			return false;
		
		return true;
	}
	else
		return false;
}*/

function formatDate(theDate)
{
	var regExpObj = new RegExp("\\D", "g"); // \D = any non-digit character
	
	// If no non-digits, then this is an invalid date
	if(theDate.search(regExpObj) == -1)
		return false;
	else if(theDate.match(regExpObj).length != 2) // user must be trying to search mm/yyyy
		return false;
		
	if(theDate.length <= 8)
	{
		var dateParts = theDate.split(regExpObj);
		if(dateParts.length == 3)
		{
			var theMonth 	= dateParts[0];
			var theDay 		= dateParts[1];
			var theYear		= dateParts[2];
			
			if(theMonth.length == 1)
				theMonth = "0" + theMonth;
			if(theDay.length == 1)
				theDay = "0" + theDay;
			if(theYear.length == 1)
				theYear = "0" + theYear;
			else if(theYear.length == 4)
				theYear = theYear.slice(2, 4);
				
			theDate = theMonth + theDay + theYear;
		}
	}
	
	theDate = theDate.replace(regExpObj, "");
	
	if(theDate.length < 6)
		return false;
	else if(theDate.length == 8 && theDate.charAt(4) == "2") // check year, might have entered YYYY
		theDate = theDate.charAt(0) + theDate.charAt(1) + "/" + theDate.charAt(2) + theDate.charAt(3) + "/" + theDate.charAt(6) + theDate.charAt(7);
	else if(theDate.length > 6)
		return false;
	else
	{
		if(theDate.charAt(0) != "0")
			theMonth = theDate.charAt(0) + theDate.charAt(1);
		else
			theMonth = theDate.charAt(1);
		if(parseInt(theMonth) < 1 || parseInt(theMonth) > 12)
			return false;
		
		theYear = parseInt("20" + theDate.charAt(4) + theDate.charAt(5));
		
		if(theDate.charAt(2) != "0")
			theDay = theDate.charAt(2) + theDate.charAt(3);
		else
			theDay = theDate.charAt(3);
		if(theDay < 1 || theDay > 31)
			return false;
		else if(!validateMonthDate(theMonth, theDay, theYear))
			return false;
		
		theDate = theDate.charAt(0) + theDate.charAt(1) + "/" + theDate.charAt(2) + theDate.charAt(3) + "/" + theDate.charAt(4) + theDate.charAt(5);
	}
	
	return theDate;
}

function validateMonthDate(m, d, y)
{
	months = new Array(13);
	
	// Store max date for each month
	months[1] = 31;
	
	// Treat Feb differently
	if(y%4 == 0 && ((y%100==0 && y%400==0) || y%100!=0))
		months[2] = 29;
	else
		months[2] = 28;
		
	months[3] = 31;
	months[4] = 30;
	months[5] = 31;
	months[6] = 30;
	months[7] = 31;
	months[8] = 31;
	months[9] = 30;
	months[10] = 31;
	months[11] = 30;
	months[12] = 31;
	
	if(months[m] >= d)
		return true;
	else
		return false;
}

/*function addOption(selectBxID, optionVal, optionTx)
{
	var theSelectBx = eval("document.objectForm." + selectBxID);
	theSelectBx.options[theSelectBx.length] = new Option(optionTx, optionVal, false, false);
}*/

function addOption(selectBxID, optionVal, optionTx)
{
	var theSelectBx = eval("document.objectForm." + selectBxID);
	
	// Need to store select bx options in an array to add new option at the top
	var optionsArray = new Array(theSelectBx.length+1);
	
	// Create a 2D arry
	for(i=0; i<optionsArray.length; i++)
		optionsArray[i] = new Array(3);
	
	for(i=0; i<theSelectBx.length+1; i++)
	{
		if(i == 0)	// Add new option to top
		{
			optionsArray[i][0] = optionVal; // Value
			optionsArray[i][1] = optionTx;  // Text
			optionsArray[i][2] = true;		// Selected
		}
		else
		{
			optionsArray[i][0] = theSelectBx.options[i-1].value; // Value
			optionsArray[i][1] = theSelectBx.options[i-1].text;  // Text
			
			if(theSelectBx.options[i-1].selected)
				optionsArray[i][2] = true;						 // Selected
			else
				optionsArray[i][2] = false;
		}
	}
	
	// Clear select box
	for(i=0; i<theSelectBx.length; i++)
		theSelectBx.options[i] = null;
		
	// Add options back in
	for(i=0; i<optionsArray.length; i++)
		theSelectBx.options[i] = new Option(optionsArray[i][1], optionsArray[i][0], optionsArray[i][2], false);
}

function updateOption(selectBxID, optionVal, optionTx)
{
	var theSelectBx = eval("document.objectForm." + selectBxID);
	
	for(i=0; i<theSelectBx.length; i++)
		if(theSelectBx.options[i].value == optionVal)
			if(theSelectBx.options[i].text != optionTx)
				theSelectBx.options[i].text = optionTx;
}

function deleteOption(selectBxID, optionVal)
{
	var theSelectBx = eval("document.objectForm." + selectBxID);
	//alert(theSelectBx);
	for(i=0; i<theSelectBx.length; i++)
		if(theSelectBx.options[i].value == optionVal)
			theSelectBx.options[i] = null;
}

function moveOption(fromSelectBxID, toSelectBxID, enableSubmit)
{
	var fromSelectBx = eval("document.objectForm." + fromSelectBxID);
	var toSelectBx   = eval("document.objectForm." + toSelectBxID);
	
	if(fromSelectBx && toSelectBx)
	{
		if(fromSelectBx.selectedIndex != -1)
		{
			if(enableSubmit)
				enableBtns();
			
			// Move ONE option item over
			/*var selectedOption = fromSelectBx.options[fromSelectBx.selectedIndex];
			toSelectBx.options[toSelectBx.length] = new Option(selectedOption.text, selectedOption.value, false, false);
			
			// Remove option item
			fromSelectBx.options[fromSelectBx.selectedIndex] = null;*/
			
			// Move MULTIPLE options items
			var fromSelectBxLen = fromSelectBx.length;
			
			for(i=0; i<fromSelectBxLen; i++)
				if(fromSelectBx.options[i].selected)
					toSelectBx.options[toSelectBx.length] = new Option(fromSelectBx.options[i].text, fromSelectBx.options[i].value, false, false);
			
			// Remove option items
			for(i=fromSelectBxLen-1; i>=0; i--)
				if(fromSelectBx.options[i].selected)
					fromSelectBx.options[i] = null;
		}
	}
}

function moveOptionUp(theSelectBx){
	if(theSelectBx.selectedIndex != -1)
	{
		var selectedIdx  = theSelectBx.selectedIndex;				// tracks selected option index
		var optionID     = theSelectBx.options[selectedIdx].value;  // tracks unique ID for the option
		var optionsArray = new Array(theSelectBx.length);
		
		// Create a 2D arry
		for(i=0; i<optionsArray.length; i++)
			optionsArray[i] = new Array(2);
			
		for(i=0; i<theSelectBx.length; i++){
			if(i == selectedIdx-1)
			{
				optionsArray[i][0] = theSelectBx.options[i+1].value;
				optionsArray[i][1] = theSelectBx.options[i+1].text;
				
				optionsArray[i+1][0] = theSelectBx.options[i].value;
				optionsArray[i+1][1] = theSelectBx.options[i].text;
				
				i++;
			}
			else
			{
				optionsArray[i][0] = theSelectBx.options[i].value;
				optionsArray[i][1] = theSelectBx.options[i].text;
			}
		}
		
		// Clear select box
		for(i=0; i<theSelectBx.length; i++)
			theSelectBx.options[i] = null;
			
		for(i=0; i<optionsArray.length; i++)
		{
			theSelectBx.options[i] = new Option(optionsArray[i][1], optionsArray[i][0], false, false);
			if(optionsArray[i][0] == optionID)
				theSelectBx.options[i].selected = true;
		}
	}
}

function moveOptionDown(theSelectBx)
{
	if(theSelectBx.selectedIndex != -1)
	{
		var selectedIdx  = theSelectBx.selectedIndex;				// tracks selected option index
		var optionID     = theSelectBx.options[selectedIdx].value;  // tracks unique ID for the option
		var optionsArray = new Array(theSelectBx.length);
		
		// Create a 2D arry
		for(i=0; i<optionsArray.length; i++)
			optionsArray[i] = new Array(2);
			
		for(i=0; i<theSelectBx.length; i++)
			{
			if(i == selectedIdx){
				optionsArray[i][0] = theSelectBx.options[i+1].value;
				optionsArray[i][1] = theSelectBx.options[i+1].text;
				
				optionsArray[i+1][0] = theSelectBx.options[i].value;
				optionsArray[i+1][1] = theSelectBx.options[i].text;
				
				i++;
			}
			else
			{
				optionsArray[i][0] = theSelectBx.options[i].value;
				optionsArray[i][1] = theSelectBx.options[i].text;
			}
		}
		
		// Clear select box
		for(i=0; i<theSelectBx.length; i++)
			theSelectBx.options[i] = null;
			
		for(i=0; i<optionsArray.length; i++)
		{
			theSelectBx.options[i] = new Option(optionsArray[i][1], optionsArray[i][0], false, false);
			if(optionsArray[i][0] == optionID)
				theSelectBx.options[i].selected = true;
		}
	}
}
	
function showElement(elemID)
{
	document.getElementById(elemID).style.visibility = "visible";
}
	
function hideElement(elemID)
{
	document.getElementById(elemID).style.visibility = "hidden";
}

function toggleElement(theSelectBx, elemID)
{
	var theElem = document.getElementById(elemID);
	
	if(theSelectBx.selectedIndex != -1)
		theElem.style.visibility = "visible";
	else
		theElem.style.visibility = "hidden";
}

function showInfo(theSelectBx, iframeID)
{
	// iframeID is also the page name
	if(theSelectBx.selectedIndex != -1)
		document.getElementById(iframeID).src = iframeID + ".asp?ID=" + theSelectBx.options[theSelectBx.selectedIndex].value;
}

function hideEdit(selectBxName, elemID)
{
	// All edit forms are named objectForm
	var theSelectBx = eval("document.objectForm." + selectBxName);
	
	if(theSelectBx.selectedIndex == -1)
		document.getElementById(elemID).style.visibility = "hidden";
}

function deselectAllOptions(theSelectBx)
{
	theSelectBx.selectedIndex = -1;
}

function selectOption(selectBxID, optionVal)
{
	var theSelectBx = eval("document.objectForm." + selectBxID);
	for(i=0; i<theSelectBx.length; i++)
		if(theSelectBx.options[i].value == optionVal)
			theSelectBx.options[i].selected = true;
}

function showViewCnt(objectID)
{
	divX = event.x;
	divY = event.y;
	linkStr = "/shared/ShowViewCount.asp?objectID=" + objectID
	launchWin(linkStr, "width=500,height=400")
	//alert(linkStr);
}

function showLinkCnt(linkID)
{
	divX = event.x;
	divY = event.y;
	linkStr = "/shared/ShowLinkCount.asp?linkID=" + linkID
	launchWin(linkStr, "width=500,height=400")
	//alert(linkStr);
}


