// Move an option from one select list to another and set the selection
// in the source list.  This makes it easier for the user to move several items.	
function moveOptionSetSelection(sourceSelectList, destSelectList) {
	var sourceSelected = sourceSelectList.selectedIndex;
	
	moveOption(sourceSelectList, destSelectList);
	
	if(sourceSelected < sourceSelectList.options.length)
		sourceSelectList.selectedIndex = sourceSelected;
	else
		sourceSelectList.selectedIndex = sourceSelected - 1;	
}

// Move a select list item from one select list to another
function moveOption(sourceSelectList, destSelectList) {
	// Make sure that something is selected
	if(sourceSelectList.selectedIndex > -1) {
		// Get a new reference to the selected option
		// Loop through all options and move selected items.
		for (var i = 0;i < sourceSelectList.options.length;i++){
			//check if item is selected
			if(sourceSelectList.options[i].selected){
				var selectedItem = sourceSelectList.options[i];
			
				// Remove the selected option from source select list
				sourceSelectList.options[i] = null;
				
				// Add option to destination select list
				destSelectList.options[destSelectList.options.length] = selectedItem;
				
				// Re-sort the two select lists
				//sortSelectList(sourceSelectList);
				sortSelectList(destSelectList);
				//compensate for the removed item.
				i--;
			}
		}
		// De-Select all items.
		for (var i = 0;i < destSelectList.options.length;i++){
			destSelectList.options[i].selected=false;
		}
		for (var i = 0;i < sourceSelectList.options.length;i++){
			sourceSelectList.options[i].selected=false;
		}
	}
}

// Move a select list item from one select list to another
function moveAllOption(sourceSelectList, destSelectList) {
	var setlength = sourceSelectList.options.length;

	for(var x = 0; x < setlength; x++){
		var selectedItem = sourceSelectList.options[0];
		sourceSelectList.options[0] = null;	
		destSelectList.options[destSelectList.options.length] = selectedItem;					
		}
	
		// Re-sort the two select lists
		sortSelectList(sourceSelectList);
		sortSelectList(destSelectList);
		selectAllOptions(destSelectList);

}	

// Sort the items in a select list alphabetically
function sortSelectList(sel) {
	var optionArray = new Array(sel.options.length);
	
	// For some reason I can't just sort the .options array that's part of the 
	// select object, so copy that array into a new array called optionArray
	for(var x = 0; x < sel.options.length; x++) 
		optionArray[x] = sel.options[x];
	
	// Then sort optionArray using the orderOptionsByText function to determine
	// the sort order.	
	optionArray.sort(orderOptionsByText);
	
	// Clear out the options in the select list
	sel.options.length = 0;
	
	// Finally, copy the sorted optionArray back into the select control
	for(var x = 0; x < optionArray.length; x++) 
		sel.options[x] = optionArray[x];
}

// Helper function for sortSelectList to generate alphabetical sort
function orderOptionsByText(a, b) {
	var aValue = a.text;
	var bValue = b.text;
	
	// A comes before B
	if(aValue < bValue)
		return -1;
	
	// A comes after B
	if(aValue > bValue)
		return 1;
	
	// A is the same as B	
	return 0;
}

// Selects all options in a select list (so that they all get submitted)
function selectAllOptions(sel) {
	for(var x = 0; x < sel.options.length; x++)
		sel.options[x].selected = "true";
}

function trim(str) {
	var elements = str.split("");
	var trimmedStr = "";
	var firstTextIndex = -1;
	var lastTextIndex = -1;
	
	for(var x = 0; x < elements.length; x++) {
		if(elements[x].search(/\s+/) == -1) {
			lastTextIndex = x;
			if(firstTextIndex == -1) {
				firstTextIndex = x;		
			}
		}			
	}
	
	if(firstTextIndex > -1)
		for(var x = firstTextIndex; x <= lastTextIndex; x++)
			trimmedStr += elements[x];
	
	return trimmedStr;
}

function LimitTextarea(fieldObj,maxChars) {
	var result = true;
	if (fieldObj.value.length > maxChars) {
		fieldObj.value = fieldObj.value.substr(0,maxChars)	;
		result = false;
	}
	
	if (window.event)
		window.event.returnValue = result;
	return result;
}

function bNumeric(field) {
	var number = /^[0-9]+$/;
	var found = field.search(number);
	if (found == -1)
		return false;
	else
		return true;
}

function bPositiveInteger(field) {
	var number = /[0-9]+/;
	var found = field.search(number);
	if (found == -1)
		return false;
	else {
		if (field > 0)
			return true;
		else
			return false;
	}
}

function bZipcode(field) {
	var zipcode = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
	var intzipcode = /^\D{1}\d{1}\D{1}\-?\d{1}\D{1}\d{1}$/;
	var singaporezipcode = /(^\d{6}$)/;
	field = trim(field);
	if (field.search(zipcode) == -1) {
		if (field.search(intzipcode) == -1) {
			if (field.search(singaporezipcode) == -1)
				return false;
			else
				return true;
		}
		else
			return true;
	}
	else
		return true;
}

function bNotBlank(field) {
	var notblank = /\w{1,}/;
	if (field.search(notblank) == -1)
		return false;
	else
		return true;
}

function bEmail(field) {
	if (field.length == 0) {
		return true;
	}
	else {
//		var email = /\w{1,}@\w{1,}\.\w{2,}/;
		var email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var found = field.search(email);
		if (found == -1)
			return false;
		else
			return true;
	}
}

/*
function bEmail(field) {
	var email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var found = field.search(email);
	if (found == -1)
		return false;
	else
		return true;
}
*/

function bDate(field) {
	var date = /\d{2}\/\d{2}\/\d{4}/;
	var found = field.search(date);
	if (found == -1)
		return false;
	else
		return true;
}

function bRadioChecked(field) {
	for (i=0; i < field.length; i++) {
		if (field[i].checked) {
			return true;
		}
	}
	return false;
}

var _height=null;
function sh(_d)
{
	var _y = document.getElementById(_d);
	if(_height==null)_height=_y.style.height;
	_y.style.visibility=_y.style.visibility=="hidden"?"visible":"hidden";
	_y.style.overflow=_y.style.overflow=="hidden"?"visible":"hidden";
	_y.style.height=_y.style.height=="1px"?"8em":"1px";
}

function PreviewPDF(url) {
	var windowprops = "top=100,left=100,resizable=yes,width=640,height=480";
	window.open(url, "popupPage", windowprops);
}

function DetailPopUp(url) {
	var windowprops = "top=200,left=200,resizable=yes,width=630,height=440,scrollbars";
	window.open(url, "popupPage", windowprops);
}

function rollOn (imgName) {
   if (document.images) {
      document[imgName].src = eval(imgName + "on.src");
   }
}

function rollOff (imgName) {
   if (document.images) {
      document[imgName].src = eval(imgName + "off.src");
   }
}

// Processing Window Def's
var popupWin = 2;

function popupClose()
{
	if(popupWin!=2) popwin.close();
}

function popupOpen()
{
	popupWin = 1;
	var winopts = "toolbar=no,width=300,height=320,directories=no,status=no,scrollbars=no,resize=no,menubar=no,";
	if (navigator.appName.substring(0,8) == "Netscape") {
		var xlocation = window.screenX + 325;
		var ylocation = window.screenY + 225;
		// alert('(' + xlocation + ', ' + ylocation + ')');
		winopts = winopts + "screenx=" + xlocation + ",screeny=" + ylocation;
	}
	if (navigator.appName.substring(0,9) == "Microsoft") {
		var xlocation = window.screenLeft + 350;
		var ylocation = window.screenTop + 75;
		// alert('(' + xlocation + ', ' + ylocation + ')');
		winopts = winopts + "left=" + xlocation + ",top=" + ylocation;
	}
	popwin=window.open("Resources/Layouts/processing.htm","Processing",winopts);	
}
function setimage(selectfield, imgname, prepend, append) {
	var currentrow = 0;
	for (i = 0; i < selectfield.options.length; i++)
	{
		if (selectfield.options[i].selected)
		{
			document[imgname].src = prepend + selectfield.options[i].value + append;
		}
	}
}
