
//==============================================
 function kp_setup() {
     this.integer = kp_integer;
     this.numeric = kp_numeric;
     this.character = kp_character;
     this.convertUpper = kp_convert_upper;
     this.convertLower = kp_convert_lower;
     
     this.validateNumber= kp_validateNumber;
     this.validNumber=kp_validNumber;
     this.formatNumber=kp_formatNumber;
     
     var seperators=".,";
    
     //choose!
    
     //decimal point is a dot
     //this.decimal_point=seperators.charCodeAt(0);	
     //this.decimal_thousands=seperators.charCodeAt(1);
     
     //decimal point is a comma
     this.decimal_point=seperators.charCodeAt(1);	
     this.decimal_thousands=seperators.charCodeAt(0);
         
     return this;



// This set of functions are for processing the key press event
// Used to restrict input on numerics and pure textual fields 
 
 function kp_integer(ev) {
 	 ev=evSelf(ev);	 
     if ((ev.keyCode < 48 || ev.keyCode > 57))
         ev.returnValue = false;
 }
 
 function kp_numeric(ev) {
  
 	 ev=evSelf(ev); 
     if ((ev.keyCode != decimal_thousands) && (ev.keyCode != decimal_point) && (ev.keyCode < 48 || ev.keyCode > 57)) ev.returnValue = false;
     if (ev.keyCode == decimal_point) {
         if (evSource(ev).value.indexOf(String.fromCharCode(decimal_point)) > -1) ev.returnValue = false;
     }
 } 
 
 function kp_character(ev) { 	
 	 ev=evSelf(ev);
     if ((ev.keyCode < 65 || ev.keyCode > 90) && (ev.keyCode < 97 || ev.keyCode > 122))
         ev.returnValue = false;
 }
 
 function kp_convert_upper(ev) {
 	 ev=evSelf(ev); 	 
     if ((ev.keyCode >= 97 && ev.keyCode <= 122)) ev.keyCode -= 32;
 }
 
 function kp_convert_lower(ev) {
 	 ev=evSelf(ev); 
     if ((ev.keyCode >= 65 && ev.keyCode <= 90)) ev.keyCode += 32;
 } 



function kp_validNumber(v) {
var num = String.fromCharCode(decimal_point)+String.fromCharCode(decimal_thousands)+"0123456789";
	
     for (var intLoop = 0; intLoop < v.length; intLoop++) {
         if (num.indexOf(v.charAt(intLoop)) == -1) {
             return false;
         }
     }
     if (v.indexOf(String.fromCharCode(decimal_point))!=v.lastIndexOf(String.fromCharCode(decimal_point))) {
         return false;
     }
     return true;
	
}

function kp_formatNumber(v) {
	var n=v;
	
	
	//ignore the thousands
	while (n.indexOf(String.fromCharCode(decimal_thousands))!=-1) 
	 	n=n.replace(String.fromCharCode(decimal_thousands),"");
	
	if (n==String.fromCharCode(decimal_point)) n=0;
	
	
	if (String.fromCharCode(decimal_point)==",") {					
		n=n.replace(",","!");		
		n=n.replace(".",",");		
		n=n.replace("!",".");		
	}
	
	n=Number(n);
	if (n!=0) {
		p1=Math.floor(n);
		p2=n-p1;
		p2=p2*100;
		p2=Math.round(p2);
		
		if (p2==100) {
			p1++;
			p2=0;		
		}
		
		if (p2<10) p2="0"+p2;
		
		var p3="";
		if (p1>1000) {
			while (p1>=1000) {
				var p4="000"+(p1%1000);	
			
				p3=String.fromCharCode(decimal_thousands)+p4.substring(p4.length-3)+p3;
				p1=Math.floor(p1/1000);
			}
			p3=p1+p3;	
		} else
			p3=p1;
		
		//if (n<1000)
		return p3+String.fromCharCode(decimal_point)+p2;
		//else
		//return p3.toString();
	} else return "";	
}

function kp_validateNumber(ev) {
	ev=evSelf(ev);
	e=evSource(ev);	
	
	if (!validNumber(e.value)) {				
		e.style.color="red";
		e.focus();		
		//e.select();
	} else {		
		e.style.color="black";
		e.value=formatNumber(e.value);
	}
}


  
} 
 
 var keyPressInput = new Object;
 keyPressInput = kp_setup();
