//User Name:  <input type=text name="username" size=10>  
// <a href="javascript:explain('User Name', 'opener.document.form.username.value', 
// 'The user name field is where you select a user name that you will use every time you access this site.  
// Pick something you can easily remember and that will easily identify you.');" 
// onMouseOver="window.status='Click for explanation...';return true;" 
// onMouseOut="window.status='';return true;">Help?</a>

function explain(name, output, msg) {
newwin = window.open('','','top=150,left=150,width=325,height=300');
if (!newwin.opener) newwin.opener = self;
with (newwin.document)
{
open();
write('<html>');
write('<body onLoad="document.form.box.focus()"><form name=form>' + msg + '<br>');
write('<p>You may enter your ' + name + ' here and it will be copied into the form for you.');
write('<p><center>' + name + ':  <input type=text name=box size=10 onKeyUp=' + output + '=this.value>');
write('<p><input type=button value="Click to close when finished" onClick=window.close()>');
write('</center></form></body></html>');
close();
   }
}


//Numbers Only Allowed Within
//onKeyPress="return KeyCheck(this,event);"
function KeyCheck(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;
if (((keycode>47) && (keycode<58) )  || (keycode==8)) { return true; }
else return false;
}

//Block multiple form submission script- By JavaScriptKit.com
//Enter error message to display if submit button has been pressed multiple times below.
//Delete below line if you don't want a message displayed:
//<input type="submit" value="Submit" onClick="return checksubmit(this)">
var formerrormsg="You\'ve attempted to submit the form multiple times.\n Please reload page if you need to resubmit form."

function checksubmit(submitbtn){
submitbtn.form.submit()
checksubmit=blocksubmit
return false
}

function blocksubmit(){
if (typeof formerrormsg!="undefined")
alert(formerrormsg)
return false
}

function
modifyEntry(elem)
{
 pn.setNumber(elem.value);
 elem.value =  pn.format();
}

function
resetNumPattern(sel)
{

 pn.setPattern(sel.options[sel.selectedIndex].value);
 sel.form.phone.value = "";
}

function preserveRadioGroup (evt) {
  this.checked = this.storedChecked;
  var rgb = this.form[this.name];
  if (!rgb.length && rgb.storedChecked)
    rgb.checked = true;
  else 
    for (var b = 0; b < rgb.length; b++)
      rgb[b].checked = rgb[b].storedChecked ? true : false;
}

function disableRadioGroup (radioGroup) {
  if (!radioGroup.disabled) {
    radioGroup.disabled = true;
    if (document.all || document.getElementById) {
      if (!radioGroup.length)
        radioGroup.disabled = true;
      else
        for (var b = 0; b < radioGroup.length; b++)
          radioGroup[b].disabled = true;
     }
    else {
      if (!radioGroup.length) {
        radioGroup.storedChecked = radioGroup.checked;
        radioGroup.oldOnClick = radioGroup.onclick;
        radioGroup.onclick = preserveRadioGroup;
      }
      else
        for (var b = 0; b < radioGroup.length; b++) {
          radioGroup[b].storedChecked = radioGroup[b].checked;
          radioGroup[b].oldOnClick = radioGroup[b].onclick;
          radioGroup[b].onclick = preserveRadioGroup;
        }
    }
  }
}

function enableRadioGroup (radioGroup) {
  if (radioGroup.disabled) {
    radioGroup.disabled = false;
    if (document.all || document.getElementById) {
      if (!radioGroup.length)
        radioGroup.disabled = false;
      else
        for (var b = 0; b < radioGroup.length; b++)
          radioGroup[b].disabled = false;
     }
    else {
      if (!radioGroup.length) {
        radioGroup.onclick = radioGroup.oldOnClick;
      }
      else
        for (var b = 0; b < radioGroup.length; b++) {
          radioGroup[b].onclick = radioGroup[b].oldOnClick;
        }
    }
  }
}

// Reset Radio Selected
function uncheckRadioGroup (radioButtonOrGroup) {
  if (radioButtonOrGroup.length) { // we have a group
    for (var b = 0; b < radioButtonOrGroup.length; b++)
      if (radioButtonOrGroup[b].checked) {
        radioButtonOrGroup[b].checked = false;
        break;
      }
  }
  else 
    radioButtonOrGroup.checked = false;
}

//Format Phone Number, SS, and Zip
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;
}
