/*
	Function About Number 
	Create By Pakin R.
	On Jun 16 ,2007
*/
	var myAgent   = navigator.userAgent.toLowerCase();
	var myVersion = parseInt(navigator.appVersion);
	var is_ie   = ((myAgent.indexOf("msie") != -1)  && (myAgent.indexOf("opera") == -1));
	var is_nav  = ((myAgent.indexOf('mozilla')!=-1) 
					&& (myAgent.indexOf('spoofer')==-1)
					&& (myAgent.indexOf('compatible')== -1) 
					&& (myAgent.indexOf('opera')==-1)
					&& (myAgent.indexOf('webtv')==-1)       
					&& (myAgent.indexOf('hotjava')==-1)); 
	var is_win   =  ((myAgent.indexOf("win")!=-1) || (myAgent.indexOf("16bit")!=-1));
	var is_mac    = (myAgent.indexOf("mac")!=-1); 
	var Pos = 0;
	
/*
	Function Prepare Formatted
*/
	function getSeperate(cur){
		var sep = Array();		
		if(cur=='SEK'){
				sep[0] = ' ';
				sep[1] = '.'; 
		}else{
				sep[0] = ',';
				sep[1] = '.';			
		}
		return sep;
	}
	
	function doGetCaretPosition (obj) { 
		/* Get the cursor position. */
		var CaretPos = 0; 
		if (document.selection)
		{// IE Support
			obj.focus ();
			var Sel = document.selection.createRange ();
			Sel.moveStart ('character', -obj.value.length);
			CaretPos = obj.value.length-Sel.text.length;
		}  
		else if (obj.selectionStart || obj.selectionStart == '0')
		{// Firefox support
			CaretPos = obj.value.length - obj.selectionStart;
		}		
		return (CaretPos);
	} 	
	
	function setCaretTo(obj, pos) {   
		if(obj.createTextRange) 
		{// IE Support
			var range = obj.createTextRange();   
			range.move("character", pos);   
			range.select();   
		} 
		else if(obj.selectionStart) 
		{// Firefox support 
			obj.focus();   
			obj.setSelectionRange(pos, pos);   
		}   
	}

	function chkValidNumberic(number)
	{
		var len = number.length;
		var i = 0; var nSub = ''; var k = '';
		for(i=0;i<len;i++){ 
			k = number.charCodeAt(i); // Character on  i position 
			if(isNum (k)){ nSub += number.substr(i,1);} // If k is number
		}
		return nSub;
	}
	function isNum (charCode)
	{
	 	if (charCode >= 48 && charCode <= 57 )
	 		return true;
	 	else
	 		return false;
	} 
	function chkValidPhase(pharse,cur){ 
		alert(pharse)
		//CommaFormatted(pharse,1,cur);
	}
	
/*
	Function Retieve Position On KeyDown
*/
	function getPos(evt,obj,cur){  
		global = Pos;
		var pos = doGetCaretPosition(obj);
		var len = obj.value.length;
		var sep = getSeperate(cur);
		if(evt.keyCode==46){  // -> Delete Key
			if(obj.value.substr(len-pos,1)==sep[0]){  
				setCaretTo(obj, len-pos+1); // Move The Cursor   Before Delete
				pos=pos-1;  
			} else if(obj.value.substr(len-pos,1)==sep[1]){
				obj.value = obj.value+'0'; 
				setCaretTo(obj, len-pos+1); // Move The Cursor   Before Delete 
				pos=pos+1;  
			}			
			pos=pos-1; 
		}  else if(evt.keyCode==8){// -> Back Key
			if(obj.value.substr(len-pos-1,1)==sep[0]){  
				setCaretTo(obj, len-pos-1); // Move The Cursor  Before BackSpace
			} else if(obj.value.substr(len-pos-1,1)==sep[1]){
				setCaretTo(obj, len-pos-1); // Move The Cursor  Before BackSpace
			}
		}  
		Pos = pos;
	} 
/*
	Function Reteive Formatted On KeyUp
*/
	function getValue(evt,obj,type,cur){
		if(evt.keyCode >=37&&evt.keyCode <=40){   // Key <^v> 
			return true;
		} 
		var sep = getSeperate(cur);
		var comma = 0;
		var decim = 0; 
		var zero = ''; 
		if(type==0){
			comma = 0;
			decim = 0; 
		} else { 
			var t = type.split('d',2) ; 
			comma = t[0];
			decim = t[1]; 
		}   
		var keyDot =0;
		var len = obj.value.length;
		var posDot  = len - Pos;  // Check Last Key  
		if(obj.value.substr(posDot-1,1)==sep[1]){   keyDot = 1; }  //If Key Dot 
		obj.value =  CommaFormattedLimitDecimal(obj.value,comma,decim,cur);// Change Value 
		len = obj.value.length; 
		pos = len - Pos; // Get New Cursor Position
		if(keyDot){  // If Key Dot Sign 
			for(var k = 1; k<=3; k++){   // Find Dot Position 
				if(obj.value.substr(len-k,1)==sep[1]){
					pos= len-k+1;  // Get New Cursor Position 
				} // End Loop If
			}// End Loop for
		}  // End Loop If
		setCaretTo(obj, pos); // Move The Cursor
	}
/*
	Function Put Data OnBlur
*/
	function valueAfterBlur(obj,type,cur){ 
		var sep = getSeperate(cur);
		var comma = 0;
		var decim = 0; 
		var zero = ''; 
		if(type==0){
			comma = 0;
			decim = 0; 
		} else { 
			var t = type.split('d',2) ; 
			comma = t[0];
			decim = t[1]; 
		}   
		for(var i = 0 ; i<decim;i++){ zero += '0'; } 
		var amount = CommaFormattedLimitDecimal(obj.value,comma,decim,cur); 
		if(!amount){ amount='0';} 
		var a = amount.split(sep[1],2); 
		if(decim>0){  
			var d = ''
			if(!a[1]){a[1] = '0'}
			d =a[1]+zero;
			d = sep[1]+d.substr(0,decim); 
			if(a[0].length==0){a[0]=0;}
			amount = a[0] + d;
		}   
		obj.value = amount; 
	}
/*
	Function Format Number
*/
	function CommaFormattedLimitDecimal(amount,comma,decim,cur){  
		amount = CommaFormatted(amount,comma,cur); // Change Amount to Comma Format		
		var sep = getSeperate(cur);
		var a = amount.split(sep[1],2); 
		var d =''; 
		if(decim>0){ 
			if(a.length>1){  // If Have Decimal 
				d = a[1]; 
				d = d.substr(0,decim); // Limit Decimal Digit 
				d = sep[1]+d;
				amount = a[0]+d;
			}   
		} else{
			amount = a[0];
		}  
		return amount;
	}
	function CommaFormatted(amount,comma,cur)
	{   
		var sep = getSeperate(cur);
		var delimiter = ""; // replace comma if desired 
		if(comma==1){ delimiter = sep[0]; }
		else if(comma==99){  
			var a = amount.split(sep[0]);
			var len = a.length;
			amount = '';
			while(len > 1) // Loop While For Put  Comma(,)
			{
				len = len - 1;
				n = chkValidNumberic(a[len]);  
				amount = ',' + n + amount;
			} 
			n = chkValidNumberic(a[0]); 
			amount = n + amount;
			return amount;
		}
		var a = amount.split(sep[1],2)  
		var dot = 0;  
		var minus = ''; 
		var n = new String(a[0]); 
		var d = new String(a[1]); 
		if(n.substr(0,1)=='-'){ minus = '-'; }
		n =  chkValidNumberic(n); // Check Is Number
		if(a.length>1){  //  If Have Decimal
			dot = 1;
			d =  chkValidNumberic(d); // Check Is Number
			d = sep[1]+d;
		} 
		var a = []; 
		var nn = ''; 
		while(n.length > 3) // Loop While For Put  Comma(,)
		{
			nn = n.substr(n.length-3);
			a.unshift(nn);
			n = n.substr(0,n.length-3);
		} 
		if(n.length > 0) { a.unshift(n); }
		n = a.join(delimiter); 
		nn = n.substr(0,1); 
		while((nn=='0'||nn==sep[0])&&n!='0'){ // If First Digit is 0
			n = n.substr(1);
			nn = n.substr(0,1); 
		} 
		if(dot < 1) { amount = n; 
		} else {   
			if(n.length<1){ n = 0; }
			amount = n + d;
		} 
		amount = minus + amount; 
		return amount;  
	}
	function chkValue(limitVal,obj,alertVal){
		var val = obj.value; 
		var a = val.split(',');
		var len = a.length;
		var amount = '';
		var n = 0;
		while(len > 0) 
		{ 
			amount += a[n];
			n = n+1;
			len = len - 1; 
		} 
		val = amount; 
		if(val>limitVal){
			alert(alertVal);
			obj.value = '';
			obj.focus();
		} 
	}
	function chkLimitValue(obj,unitVal){
		var val = obj.value; 
		var len = val.length;
		if(unitVal=='H'){
			if(len>1){
				obj.value = obj.value.substr(0,1);
			}
		} else if(unitVal=='T'){
			if(len>3){
				obj.value = obj.value.substr(0,3);
			}
		} else if(unitVal!='M'){
			if(len>3){
				obj.value = obj.value.substr(0,3);
			}
		}
	}
	// Telephone Nunber
	function TelephoneNumber(telNum){
		var datas = '';
		if(telNum){ 
			var a = telNum.split('+',2); 
			var data1 = '' ; var data2 = '' ;
			var data3 = ''; var data4  = ''; var data5  = '';
			if(a[1]){
				var b = a[1].split('-',4);  
				if(b[0]){
					dataTemp = chkValidNumberic(b[0]); 
					data1 = "+"+dataTemp.substr(0,3);
					if(!b[1]){ b[1] = dataTemp.substr(3); }
				}
				if(b[1]){
					dataTemp = b[1];
					data2 = dataTemp.replace('(',''); 
					data2 = data2.replace(')',''); 
					data2 = chkValidNumberic(data2); 
					data2 = "-("+data2.substr(0,1)+")"
					if(!b[2]){ b[2] = dataTemp.substr(1)};
				}
				if(b[2]){ 
					dataTemp = b[2];
					data3 = chkValidNumberic(dataTemp); 
					data3 = "-"+data3.substr(0,4);
					if(!b[3]){
						b[3] = dataTemp.substr(4);
					}
				} 
				if(b[3]){ 
					dataTemp = b[3];
					data4 = chkValidNumberic(dataTemp); 
					data4 = "-"+data4.substr(0,4);
				}  
			}else{
				data1 = telNum;
			}
			datas = data1+data2+data3+data4+data5;
		}
		return datas; 
	} 

	function chkValidNumber(evt,obj){
		var posi = Pos; 
		if(evt.keyCode >=37&&evt.keyCode <=40){   // Key <^v> 
			return true;
		} 
		var strCheck = '-+()0123456789/'; 
		var len = obj.value.length;
		var newval = '';
		for(i = 0; i < len; i++){
			if (strCheck.indexOf(obj.value.charAt(i)) == -1) {
				posi = posi-1;
			}else{
				newval += obj.value.charAt(i);
			}
		}
		obj.value = newval;
		len = obj.value.length; 
		pos = len - Pos; 
		setCaretTo(obj, pos); 
	}
	
	function chkValidNumber2(evt,obj){
		var posi = Pos; 
		if(evt.keyCode >=37&&evt.keyCode <=40){   // Key <^v> 
			return true;
		} 
		var strCheck = '.0123456789'; 
		var len = obj.value.length;
		var newval = '';
		for(i = 0; i < len; i++){
			if (strCheck.indexOf(obj.value.charAt(i)) == -1) {
				posi = posi-1;
			}else{
				newval += obj.value.charAt(i);
			}
		}
		obj.value = newval;
		len = obj.value.length; 
		pos = len - Pos; 
		setCaretTo(obj, pos); 
	}
	
	function chkNumberOnly(evt,obj){
		var posi = Pos; 
		if(evt.keyCode >=37&&evt.keyCode <=40){   // Key <^v> 
			return true;
		} 
		var strCheck = '0123456789'; 
		var len = obj.value.length;
		var newval = '';
		for(i = 0; i < len; i++){
			if (strCheck.indexOf(obj.value.charAt(i)) == -1) {
				posi = posi-1;
			}else{
				newval += obj.value.charAt(i);
			}
		}
		obj.value = newval;
		len = obj.value.length; 
		pos = len - Pos; 
		setCaretTo(obj, pos); 
	}
	
	
 
 //From Old Project
function formatCurrency(fld, e) {
	var sep = 0;
	var milSep = "";
	var decSep = "";
	var key = '';
	var i = j = 0; 
	var len = len2 = 0;
	var strCheck = '-+()0123456789/';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode <= 34) return true;
	key = String.fromCharCode(whichCode); 
	if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
	len = fld.value.length;
	for(i = 0; i < len; i++){
		if (fld.value.charAt(i) != decSep) break;
	}
	aux = ''; 
	for(; i < len; i++){
		if (strCheck.indexOf(fld.value.charAt(i))!=-1) 
			aux += fld.value.charAt(i);
	}
	aux += key;
	len = aux.length;
	if(len>50){return false}
	//if (len == 0) fld.value = '';
	if (len == 1){ fld.value =  decSep + aux; }
	if (len == 2){fld.value =  decSep + aux;}
	if (len > 2) {
		aux2 = '';
		for (j = 0, i = len - 3; i >= 0; i--) {
			if (j == 3) {
				aux2 += milSep;
				j = 0;
			} 
			aux2 += aux.charAt(i);
			j++;
		}
		fld.value = '';
		len2 = aux2.length;
		for (i = len2 - 1; i >= 0; i--)
		fld.value += aux2.charAt(i); 
		fld.value += decSep + aux.substr(len - 2, len);
	}
	return false;
}
