String.prototype.trim = function() 
{
	return this.replace(/^\s+|\s+$/g,"");
}

function moveOptionSetSelectionNoSort(sourceSelectList, destSelectList)
{
	var sourceSelected = sourceSelectList.selectedIndex;
	moveOptionNoSort(sourceSelectList, destSelectList);
	if(sourceSelected < sourceSelectList.options.length)
		sourceSelectList.selectedIndex = sourceSelected;
	else
		sourceSelectList.selectedIndex = sourceSelected - 1;	
}

// 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 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.
// Re-sort by type
function moveOptionSetSelectionReSortByType(sourceSelectList, destSelectList, selection) { 
	var sourceSelected = sourceSelectList.selectedIndex;
	
	moveOptionReSortByType(sourceSelectList, destSelectList, selection);
	
	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;
				
				//compensate for the removed item.
				i--;
			}
		}
		// Re-sort the two select lists
		//sortSelectList(sourceSelectList);
		sortSelectList(destSelectList);
		// 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 moveOptionNoSort(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;
				
				//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
// Re-sort by type
function moveOptionReSortByType(sourceSelectList, destSelectList, selection) {
	// 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;
				
				//compensate for the removed item.
				i--;
			}
		}	
		// Re-sort the two select lists
		//sortSelectList(sourceSelectList);
		sortSelect(destSelectList, selection)
		// 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);

}	

// Move a select list item from one select list to another
function moveAllOptionNoSort(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;					
		}
		selectAllOptions(destSelectList);

}	

// Move a select list item from one select list to another
// Re-sort by type
function moveAllOptionReSortByType(sourceSelectList, destSelectList, selection) {
	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
		sortSelect(sourceSelectList, selection);
		sortSelect(destSelectList, selection);
		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;
	
	// This zipcode stuff is for the zipcodeselection page, it takes off the zipcode and sorts it by remaining characters (county)
	var zipcode = /^\d{5}$ - /;
	
	if (aValue.search(zipcode) != -1)
	{
		aValue = aValue.slice(7, aValue.length - 1);
		bValue = bValue.slice(7, bValue.length - 1);
	}
	
	// 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]*[\.]?[0-9]*$/;
	var found = field.search(number);
	if (found == -1)
		return false;
	else
		return true;
}

function bNumericComma(field) {
	var number = /^[0-9]+[,]*[0-9]+[\.]?[0-9]*$/;
	var found = field.search(number);
	if (found == -1)
		return false;
	else
		return true;
}

function bNotNumericComma(field) {
	var number = /^[0-9]+[,]*[0-9]+[\.]?[0-9]*$/;
	var found = field.search(number);
	if (found == -1)
		return true;
	else
		return false;
}

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 bNonNegativeInteger(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 bHasAsterisk(field) 
{
	var number = /[\*]+/;
	var found = field.search(number);
	if (found == -1)
		return false;
	else
		return true;
}
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 bXX(field) {
	var notblank = /\w{1,}/;
	var XX = /\XX/;
	if (field.search(notblank) == -1 || !(field.search(XX) == -1))
		return true;
	else
		return false;
}

function Blank(field) {
	var notblank = /\w{1,}/;
	if (field.search(notblank) == -1)
		return true;
	else
		return false;
}

function NoValue(field) {
	var notblank = /\w{1,}/;
	var val = /\$XXX/
	if (field.search(notblank) == -1 || !(field.search(val) == -1))
		return true;
	else
		return false;
}

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 bDate(field) {
   var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/
   if (re.test(field)) {
      var dArr = field.split("/");
      var d = new Date(field);
      return d.getMonth() + 1 == dArr[0] && d.getDate() == dArr[1] && d.getFullYear() == dArr[2];
   }
   else {
      return false;
   }
}

function bRadioChecked(field) {
	for (i=0; i < field.length; i++) {
		if (field[i].checked) {
			return true;
		}
	}
	return false;
}

function checkinteger(object_value)
{
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is an integer defined as
    //   having an optional leading + or -.
    //   otherwise containing only the characters 0-9.
	var decimal_format = ".";
	var check_char;

    //The first character can be + -  blank or a digit.
	check_char = object_value.indexOf(decimal_format);
    //Was it a decimal?
    if (check_char < 1)
		return checknumber(object_value);
    else
		return false;
}
		
function checknumber(object_value)
{
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is a number defined as
    //   having an optional leading + or -.
    //   having at most 1 decimal point.
    //   otherwise containing only the characters 0-9.
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

    //The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(object_value.charAt(0))
    //Was it a decimal?
	if (check_char == 1)
	    decimal = true;
	else if (check_char < 1)
		return false;
        
	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < object_value.length; i++)
	{
		check_char = number_format.indexOf(object_value.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits)	
				trailing_blank = true;
        // ignore leading blanks

		}
	        else if (trailing_blank)
			return false;
		else
			digits = true;
	}	
    //All tests passed, so...
    return true
}

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 BigPreviewPDF(url) {
	var windowprops = "top=10,left=10,resizable=yes,width=900,height=600";
	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;
		}
	}
}

function TextareaMaxLen(prm_oTextarea_Obj)
{
	var var_sKey_Code, var_aKey_Special, var_bResult,
	var_oTextarea_TxtRng;
	
	var_oTextarea_TxtRng = prm_oTextarea_Obj.createTextRange();
	var_aKey_Special = [8,17,18,27,33,34,35,36,37,38,39,40,45,46,114];
	var_bResult = true;
	var_sKey_Code = event.keyCode;
	
	if (var_sKey_Code == 86)
	{ 
		if (event.ctrlKey) var_bResult = fun_mTextarea_Paste(prm_oTextarea_Obj); 
	}

	if(prm_oTextarea_Obj.value.length >= prm_oTextarea_Obj.maxLen)
	{ 
		var_bResult = false;
	
		if (var_oTextarea_TxtRng.queryCommandState('OverWrite') && (prm_oTextarea_Obj.value.length == prm_oTextarea_Obj.maxLen))
		{ 
			var_bResult = true; 
		}
		else
		{
			for (i=0; i<var_aKey_Special.length; i++)
			{ 
				if (var_sKey_Code == var_aKey_Special[i]) 
				{
					var_bResult = true;
					break;
				} 
			}
		}
	}
	
	return var_bResult;
}

function fun_mTextarea_Paste(prm_oTextarea_Obj)
{ 
	var var_sClipboard_Text;
	var_sClipboard_Text = window.clipboardData.getData("Text");
	prm_oTextarea_Obj.TxtRng = document.selection.createRange().duplicate();

	if (prm_oTextarea_Obj.TxtRng && prm_oTextarea_Obj.createTextRange)
	{ 
		prm_oTextarea_Obj.TxtRng.text = prm_oTextarea_Obj.TxtRng.text.charAt(prm_oTextarea_Obj.TxtRng.text.length - 1) == ' ' ? var_sClipboard_Text + ' ' : var_sClipboard_Text;
	}
	else
	{ 
		prm_oTextarea_Obj.TxtRng.text = var_sClipboard_Text;
	}
	
	prm_oTextarea_Obj.value = prm_oTextarea_Obj.value.substring(0, prm_oTextarea_Obj.maxLen);
	
	return false;
}

function sortSelect(select, sortType) 
{		
	//if (!compareFunction)
	//  compareFunction = compareText;
	  
	var options = new Array (select.options.length);
	
	for (var i = 0; i < options.length; i++)	
	{		
		options[i] = 
	    	new Option (
		    	select.options[i].text,
		        select.options[i].value,
		        select.options[i].defaultSelected,
		        select.options[i].selected
			);
	}		
						
	options.sort(
					function (option1,option2)
					{
							if (sortType == 'ZipCode')
							{
								return option1.value < option2.value ? -1 : option1.value > option2.value ? 1 : 0;
							}
							else if (sortType =='County')
							{			
								var option1String = option1.text.substring(option1.text.indexOf('-'),option1.text.indexOf(','));
								var option2String = option2.text.substring(option2.text.indexOf('-'),option2.text.indexOf(','));
								
								return option1String < option2String ? -1 : option1String > option2String ? 1 : 0;
							}		
							else if (sortType == 'City')
							{							    
								var option1String = option1.text.substring(option1.text.indexOf(',') + 1);
								var option2String = option2.text.substring(option2.text.indexOf(',') + 1);
								
								return option1String < option2String ? -1 : option1String > option2String ? 1 : 0;
							}	
							else if (sortType == 'Date')
							{															
								var option1ms = Date.parse(option1.text);
								var option2ms = Date.parse(option2.text);
								return option1ms < option2ms ? -1 : option1ms > option2ms ? 1 : 0;
							}										
					}
				);
						
	select.options.length = 0;		

	for (var i = 0; i < options.length; i++)
	  select.options[i] = options[i];
	
}

function Sort(selection)
{									
	sortSelect(document.MailSelection.availablezips, selection);
	sortSelect(document.MailSelection.selectedzips, selection);
}

function formatCurrency(num)
{
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
        num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function parseCurrency(num)
{
    return parseFloat(num.toString().replace(/[,\$]/g, ''));
}

// This gets the upper right coordinates for an element
function getDim(el)
{
	for (var lx=0,ly=0;el!=null;
		lx+=el.offsetLeft,ly+=el.offsetTop,el=el.offsetParent);
	return {x:lx,y:ly}
}

function b5DigitZipcode(field) {
	var notblank = /^\d{5}$/;
	if (field.search(notblank) == -1)
		return false;
	else
		return true;
}
