// Form Compendium f20 (06-12-2005)
// Format Number
// by Vic Phillips http://www.vicsjavascripts.org.uk

// Format a number, such as a date or phone number as required.

// Options to verify each input 'onblur'
// or a number of specified inputs on Submit or other event.

// Date formats of mm/dd/yyyy and dd/mm/yyyy
// may be checked for valid day, month and year.

// There may be as many applications on a page as required.

// Application Notes

// The format is specified as a string
// and passed to the function 'f20FormatNumber()' on the onkeyup event of a text box
// e.g.
// <input name="" size="10" value="Phone Number" onkeyup="f20FormatNumber(this,'(~~~) ~~~~~~'),'Blur or All';" >
// Parameter 0 may be the FormatNumber input object (this)
// or the unique ID name of the FormatNumber input element.

// The sting charactors '~' will be replaced by the numbers typed in the text box
// Parameter 2 'Blur or All' will dictate if the input is verified on blur or  on Submit or other event.
// 'Blur' will verify the input 'onblur'
// 'All'  will verify the input on Submit or other event.
// 'BlurAll' will verify the input on both 'blur' and Submit or other event.
// If the parameter is not specified verification will not occur.

// The Check All Function f20CheckAll()
// Calling f20CheckAll() will verify all FormatNumber input which inclue 'All'
// The function will return 'true' if all are correct or 'false' if any are incorrect.

// The facility would normally be initialise from the first 'keyup' event of the FormatNumber input
// However it may also be initialise from a <BODY> or window onload event calling  function 'f20InitFormatNumber('
// e.g.
// <body onload="f20InitFormatNumber('*ID*','~~/~~/~~~~','All');" >
// where
// *ID* = the unique ID name of FormatNumber input element (string)

// Verification of Date
// The f20FormatNumber() call requires an additional parmeter
// e.g.
// <input name="" size="15" value="mm/dd/yyyy" style="color:gray;" onkeyup="f20FormatNumber(this,'~~/~~/~~~~','Blur',['mm/dd/yyyy',2000,2008]);" >
// Parameter 4 is an array
// parameter 4 field 0 = the required date format 'mm/dd/yyyy' or 'dd/mm/yyyy'  (string)
// parameter 4 field 1 = (optional) the minimum permitted year                  (digits)
// parameter 4 field 2 = (optional) the maximum permitted year                  (digits)

// If field 1 is omitted the min and max years will default to custonising variables f20MinYear & f20MaxYear
// There can be any number of applications on a page each with a unique format

// All variable, function etc. names are prefixed with 'f20' to minimise conflicts with other JavaScripts

// The functional code(about 3K) is best as an external JavaScript.

// Tested with IE6 and Mozilla FireFox


// Customising Variables
var f20InitialColor='gray';
var f20TypingColor='blue';
var f20CompleteColor='black';
var f20WarningColor='RED';
var f20MinYear=1973;
var f20MaxYear=3000;

// Functional Code

// NO NEED to Change
var f20Temp,f20Lgth,f20War,f20CkAllCk;
var f20CkAllAry=new Array();

function f20InitFormatNumber(f20obj,f20tem,f20m,f20date){
 if (typeof(f20obj)=='string'){ f20obj=document.getElementById(f20obj); }
 f20FormatNumber(f20obj,f20tem,f20m,f20date);
 f20obj.style.color=f20InitialColor;
 f20obj.value=f20obj.txt;
}

function f20FormatNumber(f20obj,f20tem,f20m,f20date){
	if (f20obj.value.length == 0 ) {
		return 0;
	}
 if (typeof(f20obj)=='string'){ f20obj=document.getElementById(f20obj); }
 f20obj.txt=f20obj.value;
 f20obj.style.color=f20TypingColor;
 f20re = /\D/g;
 f20obj.value=f20obj.value.replace(/\D/g,'');
 f20Temp=f20tem;
 for (f200=0;f200<f20obj.value.length;f200++){
  f20Lgth=f20Temp.indexOf('~');
  f20Temp=f20Temp.replace('~',f20obj.value.charAt(f200));
 }
 if (f20obj.value.length>0&&(f20Lgth<0||f20obj.value.length==f20tem.match(/~/g).length)){
  f20obj.value=f20Temp.substring(0,f20tem.length);
  f20obj.style.color=f20CompleteColor;
 }
 if (f20Lgth>=0&&f20obj.value.length>0){
  f20obj.value=f20Temp.substring(0,f20Lgth+1);
 }
 f20obj.date=f20date;
 f20obj.lgth=f20tem.length;
 if (f20m){
  if (f20m.toLowerCase().match('b')){ f20AddBlur(f20obj); }
  if (f20m.toLowerCase().match('a')&&!f20obj.ckall){ f20obj.ckall=true; f20CkAllAry[f20CkAllAry.length]=f20obj; }
 }
 f20AddFocus(f20obj);
}

function f20Blur(f20,f20obj){
 if (f20obj){ f20=f20obj; }
 else { f20=this; }
 f20.style.color=f20CompleteColor;
 if (f20.value.length > 0 && f20.value.length < f20.lgth){
  //if ( f20.value.length < 8 ){ f20.value='Please Complete'; }
  f20CkAllCk=false
  f20Warning(f20,'Please Complete the');
 }
 if (!f20.date){ return; }
 f20s=f20.date[0].charAt(2);
 f20y=parseInt(f20.value.split(f20s)[2]);

 
 if (f20.date[0].charAt(0)=='m'){
  f20d=parseFloat(f20.value.split(f20s)[1]);
  f20m=parseFloat(f20.value.split(f20s)[0]);
 }else {
  f20d=parseFloat(f20.value.split(f20s)[0]);
  f20m=parseFloat(f20.value.split(f20s)[1]);
 }
 
 f20mess='';
 f20md=new Date(f20y,f20m,1,-1).getDate();

 
 if (!f20.date[1]){
  f20.date[1]=f20MinYear;
  f20.date[2]=f20MaxYear;
 }
 if (f20y<f20.date[1]||f20y>f20.date[2]){
   f20mess='Incorrect Year';
 }
 else if (f20m<1||f20m>12){
  f20mess='Incorrect Month';
 }
 else if (f20d<1||f20d>f20md){
  f20mess='Incorrect Day';
 }
 if (f20mess!=''){
  f20Warning(f20,f20mess);
 }
}

function f20CheckAll(){
 f20CkAllCk=true;
 for (f200=0;f200<f20CkAllAry.length;f200++){
  f20Blur(null,f20CkAllAry[f200]);
 }
 return f20CkAllCk;
}

function f20Warning(f20obj,f20mess){
 f20obj.style.color=f20WarningColor;
 alert(f20mess+'\nsee the '+f20WarningColor+'\nField');
 f20War=true;
 f20CkAllCk=false
 f20obj.focus();
}

function f20Color(){
 if (!f20War){ this.style.color=f20TypingColor; }
 f20War=false;
}

function f20EventAdd(f20o,f20t,f20f) {
 if ( f20o.addEventListener ){ f20o.addEventListener(f20t, function(e){ f20o[f20f](e);}, false); }
 else if ( f20o.attachEvent ){ f20o.attachEvent('on'+f20t,function(e){ f20o[f20f](e); }); }
 else {
  var f20Prev=f20o["on" + f20t];
  if (f20Prev){ f20o['on'+f20t]=function(e){ f20Prev(e); f20o[f20f](e); }; }
  else { f20o['on'+f20t]=f20o[f20f]; }
 }
}

function f20AddBlur(f20){
 if (f20.addblur){ return; }
 f20.addblur=f20Blur;
 f20EventAdd(f20,'blur','addblur');
}

function f20AddFocus(f20){
 if (f20.addfocus){ return; }
 f20.addfocus=f20Color;
 f20EventAdd(f20,'focus','addfocus');
}

