function num_clearDot(obj)
{
	var s = "";
	for (var i=obj.value.length-1;i>=0;i--)
		if (!obj.value.substr(i,1).match("[^0123456789" + (obj.getAttribute("FDecimal")>0?DecimalSep:"") + "-]")) 
			s = obj.value.substr(i,1) + s;
	return s;
}

function replaceStr(str, eskiDeger, yeniDeger)
{
	while (str.indexOf(eskiDeger)>-1)
		str = str.substring(0, str.indexOf(eskiDeger)) + yeniDeger + str.substring(str.indexOf(eskiDeger)+1, str.length);
	return str;
}

function num_changeValue(obj)
{
	if ((obj.getAttribute("FDecimal") == 0) & (obj.value.indexOf(DecimalSep)> -1))
		obj.value = obj.value.substr(0, obj.value.indexOf(DecimalSep));

	obj.value = num_clearDot(obj);
	if (obj.getAttribute("FMaxValue")>0 & parseFloat(obj.value)>obj.getAttribute("FMaxValue"))
		obj.value = obj.getAttribute("FMaxValue");
		
	var l = obj.value.length;
	if (obj.value.lastIndexOf(DecimalSep)>-1) 
	{
		l = obj.value.lastIndexOf(DecimalSep);
		if (obj.value.length-l>obj.getAttribute("FDecimal"))
			obj.value = obj.value.substr(0,l+obj.getAttribute("FDecimal")*1+1);
		
		if (obj.trimZero="1" & obj.ilk == true)
		{
			for (m=obj.value.length-1;m>-1;m--)
				if (obj.value.substr(m, 1) == "0")
					obj.value = obj.value.substr(0, m);
				else break;
			if (obj.value.substr(obj.value.length-1) == DecimalSep)
				obj.value = obj.value.substr(0, obj.value.length-1);
		}
	}
			
	if (obj.getAttribute("FEditCode")==2)
	{
		while (true)
		{
			l -= 3;
			if (obj.value.substr(0,1)=="-" & l<=1) break;
			else if (l<=0) break;
			obj.value = obj.value.substr(0, l) + GroupSep + obj.value.substr(l);
		}
	}

	try{num_move(obj);}catch(e){}
}

function num_keyPress(e) 
{
	var keycode;
	if (window.event) 
		keycode = window.event.keyCode;
	else if (e) 
		keycode = e.which;

	if (keycode==45){  // - character
		if (this.value!="" & this.getAttribute("FNegatif")!=0){
			if (this.value.substr(0,1)=="-")
				this.value = this.value.substr(1);
			else
				this.value = "-" + this.value;
		}
		return false;
	}
	else if ((DecimalSep=="," & keycode==44) || (DecimalSep=="." & keycode==46)){  // , character
		if (this.getAttribute("FDecimal")==0){
			return false;
		}else {
			if (this.value.lastIndexOf(DecimalSep)>-1){
				return false;
			}else if (this.Fcursor!=0)
				this.Fcursor = -1*this.getAttribute("FDecimal");
		}
	}
	else if ((keycode>57 || keycode<48) & keycode!=45){
		keycode = 0;
	}
	else if (num_checkDigit(this)==true){
		keycode = 0;
	}
}  

function num_checkDigit(obj) 
{
	var v  = num_clearDot(obj);
	var l  = v.lastIndexOf(DecimalSep);
	var vl = v.length;
	
	if (l>-1 & vl-l-1>=obj.getAttribute("FDecimal") & vl+obj.Fcursor>=l+1)
		return true;
		
	if (obj.value.substr(0,1)=="-") 
	{
		l--;
		vl--;
	}
	
	if (l>=0 & l>=obj.getAttribute("FDigitLen")-obj.getAttribute("FDecimal") & vl+obj.Fcursor<l+1)
		return true;

	if (l<0 & vl>=obj.getAttribute("FDigitLen")-obj.getAttribute("FDecimal"))
		return true;

	return false;
}  

function num_keyDown(e) 
{
	var keycode;
	if (window.event) 
		keycode = window.event.keyCode;
	else if (e) 
		keycode = e.which;

	if (keycode==37){
		if (this.Fcursor>this.MDigitLen)
			this.Fcursor--;
	}else if (keycode==39 || keycode==46){
		if (this.Fcursor<0)
			this.Fcursor++;
	}else if (keycode==35) 
		this.Fcursor = 0;
	else if (keycode==36) 
		this.Fcursor = this.value.length * -1;
}

function num_keyUp(e) 
{
	var keycode;
	if (window.event) 
		keycode = window.event.keyCode;
	else if (e) 
		keycode = e.which;

	if (keycode!=35 & keycode!=36 & keycode!=37 & keycode!=39 & keycode!=0)
		num_changeValue(this);

	if ((keycode>47 & keycode<58) || (keycode>94 & keycode<106) || keycode==8 || keycode==46
		|| (DecimalSep=="," & keycode==188)
		|| (DecimalSep=="." & keycode==190))
	{
		this.chg = true;
		try{
			this.numChange(this.id);
		}catch(e){}
	}	
	
	if (this.Fcursor*-1 > this.value.length) 
		this.Fcursor = this.value.length*-1;
	if (this.Fcursor > 0) 
		this.Fcursor = 0;
}

function num_click()
{
	this.Fcursor = 0;
	num_move(this);
}

function num_move(obj)
{
	obj.r.moveStart("character", obj.value.length+obj.Fcursor);
	obj.r.moveEnd("character", obj.Fcursor);
	if (!obj.ilk) obj.r.select();
}

function num_blur()
{
	try
	{	
		if (this.chg == true) this.fireEvent("onchange");
		this.chg = false;
	}
	catch(e){}
}

function num_setup(obj_name) 
{
	var obj = ge(obj_name);
	if (obj==null) return;

	obj.Fcursor = 0;
	obj.onkeypress = num_keyPress;
	obj.onkeydown  = num_keyDown;
	obj.onkeyup = num_keyUp;
	obj.onclick = num_click;
	obj.onfocus = num_click;
	obj.onblur  = num_blur;	
	obj.style.textAlign = "right";
	obj.DecimalSep = DecimalSep;
	obj.GroupSep   = GroupSep;
	obj.MDigitLen = obj.getAttribute("FDigitLen");
	var i = obj.getAttribute("FDigitLen");
	
	if (obj.getAttribute("FDecimal")>0)
	{
		i-=obj.getAttribute("FDecimal");
		obj.MDigitLen++;
	}
	
	while (true)
	{
		i-=3;
		if (i<0) break;
		obj.MDigitLen++;
	}
	
	if (obj.getAttribute("FNegatif")!=0)
		obj.MDigitLen++;
		
	obj.MDigitLen *= -1;
	obj.r = obj.createTextRange();
	obj.ilk = true;
	num_changeValue(obj);
	obj.ilk = false;
	try{
		obj.numChange = eval(obj.onNumChange.toString());
	}catch(e){}
}
