// [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)
		{
		    return true;
		}
		else if (key==8&&dFilterNum.length!=0)
		{
		 	 	dFilterNum = dFilterNum.substring(0,dFilterNum.length-1);
		}
 	  else 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)!='#')
				{
				    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 isEmail(who) {
	var email=/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
	return(email.test(who));
}

function valButton(btn) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) return btn[cnt].value;
    else return null;
}



//tweak color of radio button broup for highlight

function colButton(btn,color) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        //color button
		btn[i].style.backgroundColor = color;
    }
}

//toggle element enable disable	

function toogleElement(elementName,togType){
	
	document.getElementById(elementName).disabled = togType;
	//should set enabled and disabled colors here
	if (togType == true) {
		document.getElementById(elementName).style.backgroundColor = "#CCCCCC" ;
	}
	else {
		document.getElementById(elementName).style.backgroundColor = "#FFFFFF";
	}
	
	
}

//yes no radios, then select and hightlight appropriate control or text box
function checkBoolText(optBtnObj,optTextObj,txtResponse,txtResponse2,cSelectCol,cUnselectCol) {
	var btn = valButton(optBtnObj);

	if (btn == null && optTextObj.value.length==0 ) {
		alert(txtResponse);
		colButton(optBtnObj,cSelectCol);
		optBtnObj[0].focus();
		return false;
	}
	else {
		//check offendernames
			if(optBtnObj[0].checked && optTextObj.value.length==0){
			alert(txtResponse2);
			//optTextObj.style.backgroundColor = cSelectCol;
			optBtnObj[0].focus()
			optTextObj.focus();
			return false;
		}
		else if (btn == null && optTextObj.value.length==0){
		//condition to let them out of selecting if they already put text in
			optBtnObj[0].checked = true;
			colButton(optBtnObj,cUnselectCol);
			return true;

		}
		else {
			//uncolor
			colButton(optBtnObj,cUnselectCol);
			return true;
		}
		//
	}

}

//val button(s), select and highlight
//on form load date and some defaults
//val select and highlight
//val select other text and hilight
//val text and highlight
