// ---------------------------------------------------------------------
// Loads a drop down list with the xml node list received
// ---------------------------------------------------------------------
function LoadCombo(comboId, nodeList, dataTextField, dataValueField, firstItemText, firstItemValue)
{
	var i = 0;
	var combo = document.getElementById(comboId);
	
	combo.visible = false;
	
	ClearCombo(comboId);
	
	if (firstItemText != "")
	{
		var firstOption = document.createElement("OPTION");
		
		combo.options.add(firstOption, 0);
		firstOption.innerText = firstItemText;
		firstOption.value = firstItemValue;
		combo.selectedIndex = 0;
	}
	
	for (i = 0; i < nodeList.length; i++)
	{
		var option = document.createElement("OPTION");
		
		combo.options.add(option);
		option.innerText = nodeList[i].attributes.getNamedItem(dataTextField).value;
		option.value = nodeList[i].attributes.getNamedItem(dataValueField).value;
	}
	

	combo.disabled = false;
	combo.visible = true;
	combo.focus();
}

// ---------------------------------------------------------------------
// Clear the contenst of a drop down list
// ---------------------------------------------------------------------
function ClearCombo(comboId)
{
	var i = 0;
	var combo = document.getElementById(comboId);
	
	for (i = combo.options.length; i >= 0; i--)
		combo.options.remove(i);
		
	combo.disabled = true;
}

function ClearSearchCombo(comboId)
{
	var combo = document.getElementById(comboId);
	var firstOption = document.createElement("OPTION");
	
	ClearCombo(comboId);

	combo.options.add(firstOption, 0);
	firstOption.innerText = "< ALL >";
	firstOption.value = "0";
	combo.selectedIndex = 0;
		
	combo.disabled = true;
}

// ---------------------------------------------------------------------
// Clear the contenst of a data grid
// ---------------------------------------------------------------------
function ClearDataGrid(dtgId, hasNew, hasHeader, hasFooter)
{
	var table = document.getElementById(dtgId);
	var firstIndex = 0;
	var lastIndex = table.rows.length - 1;

	if (hasHeader) firstIndex++;
	if (hasNew) firstIndex++;
	if (hasFooter) lastRow--;
	
	for (i = lastIndex; i >= firstIndex; i--)
		table.deleteRow(i);
		
	table.disabled = true;
}

// ---------------------------------------------------------------------
// Selects an option in the combo based on its value
// ---------------------------------------------------------------------
function SelectComboValue(comboId, value)
{
	var i = 0;
	var combo = document.getElementById(comboId);
	
	if (value == null)
		return;
	
	for (i = 0; i < combo.options.length; i++)
		if (combo.options[i].value == value)
		{
			combo.selectedIndex = i;
			break;
		}
}

// ---------------------------------------------------------------------
// Focus
// ---------------------------------------------------------------------
function SetFocus(elementId)
{
	element = document.getElementById(elementId);
	
	if (element != undefined)
		if (!element.disabled)
			element.focus();
}

function Select(elementId)
{
	element = document.getElementById(elementId);
	
	if (element != undefined)
		if (!element.disabled)
			element.select();
}

// ---------------------------------------------------------------------
// Enablig / Disabling
// ---------------------------------------------------------------------
function DisableTextBox(textboxId)
{
	txt = document.getElementById(textboxId).readOnly = true;
}

function EnableTextBox(textboxId)
{
	txt = document.getElementById(textboxId).readOnly = false;
}

function Disable(elementId)
{
	var element = document.getElementById(elementId);

	if (element.disabled == false)
	{
		element.attachEvent("onclick", DisabledFunction);
		element.disabled = true;
		
		if (document.getElementById('calendar_' + elementId) != null)
		{
			Disable('calendar_' + elementId);
			SetImage('img_calendar_' + elementId, 'img_calendar_' + elementId + '_I');
		}
		else
		{
			SetImage('img_' + elementId, 'img_' + elementId + '_I');
		}
	}
}

function Enable(elementId)
{
	var element = document.getElementById(elementId);

	if (element.disabled == true)
	{
		element.detachEvent("onclick", DisabledFunction);
		element.disabled = false;

		if (document.getElementById('calendar_' + elementId) != null)
		{
			Enable('calendar_' + elementId);
			SetImage('img_calendar_' + elementId, 'img_calendar_' + elementId + '_N');
		}
		else
		{
			SetImage('img_' + elementId, 'img_' + elementId + '_N');
		}
	}
}

function SetImage(destination, source)
{
	if (document[destination] != undefined)
		document[destination].src = eval(source + '.src');
}
				
function DisabledFunction()
{
	return false;
}

function SetLinkPopup(linkId, href)
{
	var link = document.getElementById(linkId.replace('$', '_'));
	
	link.href = "javascript:ButtonPopup('" + href + "', '" + linkId + "', '', true);";
}

// ---------------------------------------------------------------------
// Show / Hide objects
// ---------------------------------------------------------------------
function Show(id)
{
	var element = document.getElementById(id);
	element.style.visibility = "visible";
}

function Hide(id)
{
	var element = document.getElementById(id);
	element.style.visibility = "hidden";
}

function DisplayBlock(id)
{
	var element = document.getElementById(id);
	element.style.display = "block";
}

function DisplayNone(id)
{
	var element = document.getElementById(id);
	element.style.display = "none";
}


// ---------------------------------------------------------------------
// Functions used in the Tab control
// ---------------------------------------------------------------------
function mOvr(src,clrOver) 
{
    if (!src.contains(event.fromElement)) 
    {
	  src.style.cursor = 'hand';
	}
}

function mOut(src,clrIn) 
{
	if (!src.contains(event.toElement)) 
	{
	  src.style.cursor = 'default';	
	}
}

function mClk(src) 
{
    if(event.srcElement.tagName=='TD')
    {
	  src.children.tags('A')[0].click();
    }
}


// ---------------------------------------------------------------------
// Popups
// ---------------------------------------------------------------------
function ResizeWindow(width, height)
{
	var left;
	var top;

	left = screen.width / 2 - width / 2;
	top = screen.height / 2 - height / 2;

	window.moveTo(left, top);
	window.resizeTo(width, height);
}

function ResizeDialog(width, height)
{
	var left;
	var top;

	left = screen.width / 2 - width / 2;
	top = screen.height / 2 - height / 2;

	window.dialogLeft = left + 'px';
	window.dialogTop = top + 'px';
	window.dialogWidth = width + 'px';
	window.dialogHeight = height + 'px';
}

// ---------------------------------------------------------------------
// TextBox currency & percent formatting
// ---------------------------------------------------------------------
function FormatTextBoxCurrency(txt)
{
	// If the user leaves this field blank, then just exit
	if (txt.value == "")
		return false;
		
	// Validate number
	if (!IsValidNumber(txt.value))
	{
		alert("Entry must be numeric.");
		txt.value = "";
		txt.select();
		return false;
	}
	else
	{
		txt.value = FormatCurrency(GetNumberFromString(txt.value));
		return true;
	}
}

function FormatTextBoxPercent(txt)
{
	// If the user leaves this field blank, then just exit
	if (txt.value == "")
		return false;

	// Validate commission percent
	if (IsValidNumber(txt.value))
	{
		var prct = GetNumberFromString(txt.value);
		
		if (prct < 0 || prct > 100)
		{
			alert("Percent should not be less than 0 or greater than 100.");
			txt.value = "";
			txt.select();
			return false;
		}
		else
		{
			txt.value = FormatPercent(GetNumberFromString(txt.value));
			return true;
		}
	}
	else
	{
		alert("Entry should be a numeric percent.");
		txt.value = "";
		txt.select();
		return false;
	}
}


/******************************** Mask ************************************/

var reOneOrMoreDigits = /[\d+]/;
var reNoDigits = /[^\d]/gi;

function doMask(textBox)
{
	var keyCode = event.which ? event.which : event.keyCode;

	// enter, backspace, delete and tab keys are allowed thru
	if(keyCode == 13 || keyCode == 8 || keyCode == 9 || keyCode == 46)
		return true;

	// get character from keyCode....dealing with the "Numeric KeyPad" 
	// keyCodes so that it can be used
	var keyCharacter = cleanKeyCode(keyCode);

	// grab the textBox value and the mask
	var val = textBox.value;
	var mask = textBox.mask;

	// simple Regex to check if key is a digit
	if(reOneOrMoreDigits.test(keyCharacter) == false)
		return false;

	// get value minus any masking by removing all non-numerics
	val = val.replace(reNoDigits,'');			

	// add current keystroke
	val += keyCharacter;

	// mask it...val holds the existing TextBox.value + the current keystroke
	textBox.value = val.maskValue(mask);

	setCaretAtEnd(textBox);

	return false;
}

// puts starting chars in field
function onFocusMask(textBox) 
{
	var val = textBox.value;
	var mask = textBox.mask;

	if(val.length == 0 || val == null) 
	{
		var i = mask.indexOf('#');

		textBox.value = mask.substring(0,i);
	}

	setCaretAtEnd(textBox);

	// set just in case.
	textBox.maxlength = mask.length;
}

// blank field if no digits entered
function onBlurMask(textBox) 
{
	var val = textBox.value;
	
	// if no digits....nada entered.....blank it.
	if(reOneOrMoreDigits.test(val) == false) 
	{
		textBox.value = '';
	}
}

String.prototype.maskValue = function(mask) 
{
	var retVal = mask;
	var val = this;

	//loop thru mask and replace #'s with current value one at a time

	// better way of doing this ???
	for(var i=0;i<val.length;i++) 
	{
		retVal = retVal.replace(/#/i, val.charAt(i));
	}

	// get rid of rest of #'s
	retVal = retVal.replace(/#/gi, "");

	return retVal;
}

// The Numeric KeyPad returns keyCodes that ain't all that workable.
// ie: KeyPad '1' returns keyCode 97 which String.fromCharCode converts to an 'a'.
// This cheesy way allows the Numeric KeyPad to be used
function cleanKeyCode(key)
{
	switch(key)
	{
		case 96: return "0"; break;
		case 97: return "1"; break;
		case 98: return "2"; break;
		case 99: return "3"; break;
		case 100: return "4"; break;
		case 101: return "5"; break;
		case 102: return "6"; break;
		case 103: return "7"; break;
		case 104: return "8"; break;
		case 105: return "9"; break;
		default: return String.fromCharCode(key); break;
	}

}

function setCaretAtEnd (field) 
{
  if (field.createTextRange) 
  {
    var r = field.createTextRange();

    r.moveStart('character', field.value.length);

    r.collapse();

    r.select();
  }
}
var tnglobalwin;
function popOpen(URL, winFeat, winName) {
var availL, availT, testit, winH, winW;
if (!(URL)) URL = "";
if (!(winFeat)) winFeat = "";
if (!(winName)) winName = "_blank";
availL = (screen.availLeft) ? screen.availLeft :
	Math.floor((screen.width - screen.availWidth) / 2);
availT = (screen.availTop) ? screen.availTop :
	Math.floor((screen.height - screen.availHeight) / 2);
if (winFeat.search(/scrollbars/i) == -1) {
   if (winFeat.length != 0) winFeat += ",";
   winFeat += "scrollbars=yes";
}
if (winFeat.search(/resizable/i) == -1)
   winFeat += ",resizable=yes";
testit = winFeat.match(/height=(\d+)/i);
if (testit) {
   if (testit[1] > screen.availHeight) {
		winH = screen.availHeight;
		winFeat = winFeat.replace(/height=(\d+)/i, "height=" + winH);
	}
	else { if (testit[1] < 50) {
		winH = Math.floor(screen.availHeight * 2 / 3);
		winFeat = winFeat.replace(/height=(\d+)/i, "height=" + winH);
		}
		else
		winH = Number(testit[1]);
	}
}
else {
	winH = Math.floor(screen.availHeight * 2 / 3);
	winFeat += ",height=" + winH;
}
testit = winFeat.match(/width=(\d+)/i);
if (testit) {
   if (testit[1] > screen.availWidth) {
		winW = screen.availWidth;
		winFeat = winFeat.replace(/width=(\d+)/i, "width=" + winW);
	}
	else { if (testit[1] < 50) {
		winW = Math.floor(screen.availWidth * 2 / 3);
		winFeat = winFeat.replace(/width=(\d+)/i, "width=" + winW);
		}
		else
		winW = Number(testit[1]);
	}
}
else {
	winW = Math.floor(screen.availWidth * 2 / 3);
	winFeat += ",width=" + winW;
}
testit = winFeat.match(/top=(\d+)/i);
if (testit) {
   if (Number(testit[1]) + winH > screen.availHeight) {
		winFeat = winFeat.replace(/top=(\d+)/i, "top=" +
			(screen.availHeight + availT - winH));
	}
}
else {
   winFeat += ",top=" + Math.floor((screen.availHeight -
		 winH) / 2 + availT);
}
testit = winFeat.match(/left=(\d+)/i);
if (testit) {
   if (Number(testit[1]) + winW > screen.availWidth) {
		winFeat = winFeat.replace(/left=(\d+)/i, "left=" +
			(screen.availWidth + availL - winW));
	}
}
else {
   winFeat += ",left=" + Math.floor((screen.availWidth -
		winW) / 2 + availL);
}
tnglobalwin = window.open(URL, winName, winFeat);
if (tnglobalwin.opener==null)tnglobalwin.opener=self;
setTimeout('tnglobalwin.focus()',250);
return tnglobalwin;
}
