
// [dFilter] - A Numerical Input Mask for JavaScript
// Written By Dwayne Forehand - March 27th, 2003
// Please reuse & redistribute while keeping this notice.

var dFilterStep

function dFilterStrip (dFilterTemp, dFilterMask)
{
    dFilterMask = replace(dFilterMask,'#','');
    for (dFilterStep = 0; dFilterStep < dFilterMask.length++; dFilterStep++)
		{
		    dFilterTemp = replace(dFilterTemp,dFilterMask.substring(dFilterStep,dFilterStep+1),'');
		}
		return dFilterTemp;
}

function dFilterMax (dFilterMask)
{
 		dFilterTemp = dFilterMask;
    for (dFilterStep = 0; dFilterStep < (dFilterMask.length+1); dFilterStep++)
		{
		 		if (dFilterMask.charAt(dFilterStep)!='#')
				{
		        dFilterTemp = replace(dFilterTemp,dFilterMask.charAt(dFilterStep),'');
				}
		}
		return dFilterTemp.length;
}

function dFilter (key, textbox, dFilterMask) {
    dFilterNum = dFilterStrip(textbox.value, dFilterMask);
    if (key==9) { // Tab
        return true;
    }
    else {
        if (key==8&&dFilterNum.length!=0) { // Backspace
            dFilterNum = dFilterNum.substring(0,dFilterNum.length-1);
        }
        else {
            if (key == 46) {
                dFilterNum = "";
            }
            if ( ((key>47&&key<58)||(key>95&&key<106)) && dFilterNum.length<dFilterMax(dFilterMask) ) {
                dFilterNum=dFilterNum+String.fromCharCode(key);
            }
        }
    }

    var dFilterFinal='';
    for (dFilterStep = 0; dFilterStep < dFilterMask.length; dFilterStep++) {
        if (dFilterMask.charAt(dFilterStep)=='#') {
            if (dFilterNum.length!=0) {
                dFilterFinal = dFilterFinal + dFilterNum.charAt(0);
                dFilterNum = dFilterNum.substring(1,dFilterNum.length);
            }
            else {
                dFilterFinal = dFilterFinal + "";
            }
        }
        else 
            if (dFilterMask.charAt(dFilterStep)!='#') {
                if (dFilterNum.length!=0) {
                    dFilterFinal = dFilterFinal + dFilterMask.charAt(dFilterStep); 			
                }
            }
//		    dFilterTemp = replace(dFilterTemp,dFilterMask.substring(dFilterStep,dFilterStep+1),'');
		}


		textbox.value = dFilterFinal;
    return false;
}

function replace(fullString,text,by) {
// Replaces text with by in string
    var strLength = fullString.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return fullString;

    var i = fullString.indexOf(text);
    if ((!i) && (text != fullString.substring(0,txtLength))) return fullString;
    if (i == -1) return fullString;

    var newstr = fullString.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(fullString.substring(i+txtLength,strLength),text,by);

    return newstr;
}
function printElementWithID(givenID)
{
   if (document.getElementById != null) {
      //  Create a new document string in the variable "html"
      //  and open a new window with just this content...
      var html = '<html>\n<head>\n';
      if (document.getElementsByTagName != null) {
         var headTags = document.getElementsByTagName("head");
         if (headTags.length > 0) {
            html += headTags[0].innerHTML;
         }
      }
      html += '\n</he' + 'ad>\n<body style="background-color:#FFFFFF;">\n';
      var printReadyElem = document.getElementById(givenID);
      if (printReadyElem != null) {
         html += printReadyElem.innerHTML;
      }
      else {
         alert("Could not find the printReady section in the HTML");
         return;
      }
      html += '\n</bo' + 'dy>\n</ht' + 'ml>';
      var printWin = window.open("","printSpecial");
      printWin.document.open();
      printWin.document.write(html);
      printWin.document.close();
      printWin.print();
   }
   else {
      alert("Sorry, the print ready feature is only available in modern browsers.");
   }
}

//  Move the currently-selected option in the source to the target.
function moveOption(aGivenSourceSelect, aGivenTargetSelect)
{
    var itemToMoveIndex = aGivenSourceSelect.options.selectedIndex;
    if (itemToMoveIndex > -1) {
        var itemToMove = aGivenSourceSelect.options[itemToMoveIndex];
        aGivenSourceSelect.options[itemToMoveIndex] = null;
        aGivenTargetSelect.options[aGivenTargetSelect.length] = itemToMove;
    }
    else {
        alert('Please select an element to be moved before pressing this button.');
    }
}

//  Return all option values (selected or not) of the given source select object in an array string
function getOptionValues(aGivenSourceSelect)  {
    var anArray = new Array();
    for (i = 0; i < aGivenSourceSelect.options.length; i++) {
        anArray[anArray.length] = aGivenSourceSelect.options[i].value;
    }
    return (anArray.toString());
}


// Given the name of a select element and the value of some option, 
// mark the option within that select as selected.
function selectOptionValued(selectElementName, optionValue) {
    var selectElement = document.getElementById(selectElementName);
    if (selectElement != null) {
        for (var i = 0; i < selectElement.children.length; i++) {
            var aCurrentNode = selectElement.children[i];
            if ((aCurrentNode.nodeName == 'OPTION') && (aCurrentNode.value == optionValue)) {
                aCurrentNode.selected = true;
                break;
            }
        }
    }
}

