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;
function launchHelp(helpURL, size) {
	remote = window.open(helpURL, "help", size+",scrollbars=1,resizable=1");
	remote.focus();
}

var remote;
function launchWin(url, 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));
	
	var xPos = (screen.height - h) / 2;
	var yPos = (screen.width - w) / 2;
	remote = window.open(url, "remoteWindow", size + ",scrollbars=1,resizable=1,left=" + yPos + ",top=" + xPos);
	remote.focus();
	
	// Do not need this return stmt, because parent window is refreshing when calling this function.
	//return false;
}
	
var imgWin;
function fileBrowse(field, fileTypeID, hiddenField) {
	var w = 770;
	var h = 485;
	var destination = document.getElementById(field);
	var leftX = (screen.width - w) / 2;
	var topY = (screen.height - h) / 3;
	
	if(!hiddenField) hiddenField = "";
	
	imgWin = window.open("/extranet/fileBrowse.asp?field=" + field + "&fileTypeID=" + fileTypeID + "&hiddenField=" + hiddenField, "fileBrowse", "width=" + w + ",height=" + h + ",scrollbars=0,resizable=1,left=" + leftX + ",top=" + topY + ";");
	imgWin.focus();
}

function validateDate(theDate) {
	if(theDate.indexOf("/") != -1 || theDate.indexOf("-") != -1){
		// Get date parts
		var theDateParts;
		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 validateTime(theTime) {
	if (theTime.indexOf(":") != -1 && theTime.indexOf(" ") != -1) {
		// Get time parts
		var theTimeParts = theTime.split(":");
		
		// Not correct length?
		if (theTimeParts.length != 2) {
			return false;
		}
		
		for (i = 0; i < theTimeParts.length; i++) {
			if (theTimeParts[i].length < 1 || isNaN(theTimeParts[i])) {
				return false;
			}
		}
		
		var theHour = parseInt(theTimeParts[0]);
		var theMinute = parseInt(theTimeParts[1]);
		
		if (theHour == 0) {
			theTimeParts[0].replace("0", "");
			theHour = parseInt(theTimeParts[0]);
		}
		
		if (theHour < 1 || theHour > 12) {
			return false;
		}
		
		if (theMinute < 0 || theMinute > 59) {
			return false;
		}
		
		return true;
	} else {
		return false;
	}
}

var savedField;
function setTheFieldVal(val) {
	if(savedField)
		savedField.value = val;
}

function calcHeight(iframeID) {
	var the_height=document.getElementById(iframeID).contentWindow.document.body.scrollHeight;//find the height of the internal page
	document.getElementById(iframeID).style.height=the_height + 20;//change the height of the iframe
}

function moveOption(fromSelectBxID, toSelectBxID)
{
	var fromSelectBx = document.getElementById(fromSelectBxID);
	var toSelectBx   = document.getElementById(toSelectBxID);
	
	if(fromSelectBx && toSelectBx)
	{
		if(fromSelectBx.selectedIndex != -1)
		{
			// Move MULTIPLE option 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(selectBxID)
{
	var theSelectBx = document.getElementById(selectBxID);
	
	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(selectBxID)
{
	var theSelectBx = document.getElementById(selectBxID);
	
	if(theSelectBx.selectedIndex != -1 && theSelectBx.length > 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;
		}
	}
}

