﻿function wrk_round(ValToRnd, no_of_decimal){
	if(!no_of_decimal && no_of_decimal!=0)
		no_of_decimal=2;
	decimal_carpan = Math.pow(10,no_of_decimal);
	if(ValToRnd!=0) return (Math.round(ValToRnd*decimal_carpan)/decimal_carpan);
	else return 0;
}
function filterNum(str,no_of_decimal) 
{
	if (str.length == 0) return '';
	if(!no_of_decimal && no_of_decimal!=0) no_of_decimal=2;
	strCheck = '-0123456789,';
	newStr = '';
	for(var i=0; i < str.length; i++) if (strCheck.indexOf(str.charAt(i)) != -1) newStr += str.charAt(i);
	newStr = newStr.replace(',', '.');
	while(newStr.indexOf(',') > 0) newStr = newStr.replace(',','');
	return wrk_round(newStr,no_of_decimal);
}

function commaSplit(str,no_of_decimal,is_round)
{
	if(str.length==0) return '';
	if(!is_round && is_round != false) is_round=true;
	if(!no_of_decimal && no_of_decimal!=0)
		no_of_decimal=2;
	if(is_round) str = wrk_round(str,no_of_decimal);
	negatif_flag = 0;
	str = str.toString();
	if(parseFloat(str) < 0) {negatif_flag = 1;str = str.replace('-','');}
	textFormat_1='';
	textFormat_2='';
	temp_virgul = str.indexOf('.');
	virgul = '';
	if(temp_virgul >= 0 && no_of_decimal > 0)
		{
		virgul = ',';
		textFormat_2 = str.substr(temp_virgul+1,str.length);
		alert(textFormat_2);
		if(textFormat_2.length > no_of_decimal)
			textFormat_2 = textFormat_2.substr(0,no_of_decimal);
		else
			for (var txtf1 = textFormat_2.length+1;txtf1<=no_of_decimal;txtf1++)
				textFormat_2 = textFormat_2+'0';
		}
	else if(temp_virgul < 0 && no_of_decimal > 0)
		{
		virgul = ',';
		for (var txtf1=1;txtf1<=no_of_decimal;txtf1++)
			textFormat_2 = textFormat_2+'0';
		}
	nokta_koy = 0;
	if(temp_virgul==0)
		textFormat_1 = 0;
	else if(temp_virgul>3)
		for (var k = temp_virgul-1; k>=0 ; k--){
		textFormat_1 = str.substr(k,1) + textFormat_1;
		nokta_koy++;
		if (nokta_koy%3==0 && k!=0) textFormat_1 = '.'+textFormat_1;
		}
	else if(temp_virgul>0)
		textFormat_1 = str.substr(0,temp_virgul);
	else if(temp_virgul<0)
		for (var k=str.length-1; k>=0 ; k--){
		textFormat_1 = str.substr(k,1) + textFormat_1;
		nokta_koy++;
		if (nokta_koy%3==0 && k!=0) textFormat_1 = '.'+textFormat_1;
		}
	textFormat = textFormat_1+virgul+textFormat_2;
	if(negatif_flag) return '-'+textFormat;
	return textFormat;
}

function formatcurrency(fld,e) 
{
	return FormatCurrency(fld,e);
}
function FormatCurrency(fld,e) 
{	
	var milSep = '.'; /* binlik ayracı*/
	var perSep = ','; /* kuruş ayracı*/
	var strCheck = '0123456789,'; /* izin verilen karakterler*/
	var decimal = '';   /*Virgülden sonraki kısım*/
	var len = 0;
	var aux = '';
	if (e != undefined)
		var whichCode = (window.Event) ? e.which : e.keyCode; 
	else
		whichCode = ',';
		
	key = String.fromCharCode(whichCode); 		
		
	if((previous_process[0] != undefined) && 
	   (previous_process[0] == fld) &&
	   (previous_process[1] == '0') &&
	   (key != perSep) &&
	   (strCheck.indexOf(key) != -1))
			fld.value = key;
	previous_process[0] = fld;
	previous_process[1] = fld.value;				
	
	if (whichCode == 13) return true;		 		
	len = fld.value.length;	
	/* birden fazla noktaya izin yok	*/
	if ( (key == perSep) && (fld.value.indexOf(key) != -1) ) return false;
	/* noktadan sonra en fazla iki karakter olabilir*/
	if ( (fld.value.indexOf(perSep) != -1) && (fld.value.indexOf(perSep) == (len-3)) ) return false;
	/* soldaki sıfırlar atlanıyor*/
	var i = 0;
	if((fld.value.length > 1) && (fld.value.charAt(0) == '0')){
		if(fld.value.charAt(1) != perSep)
			for(i = 0; i < len; i++) if (fld.value.charAt(i) != '0' ) break;
	}
	/*0,12 için özel durum*/
	if ((fld.value.length == 1) && (key == perSep)) return true;
	if (fld.value.indexOf(perSep) == 1) i--;
	
	aux = '';
	/* tüm karakterler kontrol ediliyor*/
	for(; i < len; i++) if (strCheck.indexOf(fld.value.charAt(i)) != -1) aux += fld.value.charAt(i);	
	/*aux += key;  onkeypress'de kullaniyordu.Sayı silindiğinde tekrar formatlama yapılmadığı için onkeypress kaldırıldı.*/

	if(aux.indexOf(",") != -1){    /*Virgüden önceki ve sonraki ifadeler ayrı değişkenlere aktarılıyor*/
		decimal = aux.substr(aux.indexOf(",") , aux.length);
		aux = aux.substr(0,aux.indexOf(","));		
	}	
	len = aux.length;	
	if (len <= 3) fld.value = aux;	
	textFormat='';
	t=0;
	if (len > 2) 
		{
		/* virgüller yerleştirilir*/
		for (var k = aux.length-1; k>=0 ; k--)
			{
			t++;
			/* nokta için özel durum*/
			if (aux.substr(k,1) == perSep)
				{
				t = 0;
				textFormat =  aux.substr(k,1) + textFormat;
				}
			else if (t % 3 == 0)
				textFormat = milSep + aux.substr(k,1) + textFormat; 
			else 
				textFormat =  aux.substr(k,1) + textFormat;
			} 
		/* eğer ,123,123,123 gibi ise en baştaki virgül atılır*/
		if (textFormat.substr(0,1) == milSep)
			fld.value = textFormat.substr(1,textFormat.length-1);
		else 
			fld.value = textFormat;
		}
	fld.value += decimal;	/*virgülden sonraki kısım önceki parçayla birleştiriyor	*/
	if((key != '0') && (fld.value == (key + '0')))  /*Alan ilk seçildiğinde sayının '0'ın soluna yazılmasını engellemek için*/
		fld.value = key;						
	return false;
}

function FormatCurrency(fld,e,no_of_decimal) 
{
	if(!e) return false;
	if(!no_of_decimal && no_of_decimal!=0) no_of_decimal=2;
	var whichCode = (window.Event) ? e.which : e.keyCode;
	key_spec_codes = "'13','16','17','35','36','37','39','109','189'";
	if(key_spec_codes.search("'" + whichCode + "'")>=0) return true; 
	
	if(whichCode==32)
		{
			
		while(fld.value.indexOf(' ') > 0)
			fld.value = fld.value.replace(' ','');
		return false;
		}
		
	if(fld.value==',') {fld.value = '';return false;}
	if(fld.value.indexOf(',')>0)
		{
		

		var temp = fld.value.length-fld.value.indexOf(',')-1;
		
		if(temp == 0)
			fld.value = (whichCode==8 || whichCode==46 || no_of_decimal==0) ? commaSplit(filterNum(fld.value,temp),temp) : commaSplit(filterNum(fld.value,temp),temp)+',';
		else if(temp < no_of_decimal)
			fld.value = commaSplit(filterNum(fld.value,temp),temp);
		else if(temp > no_of_decimal)
			fld.value = commaSplit(filterNum(fld.value.substr(0,fld.value.indexOf(',')+1+no_of_decimal),no_of_decimal),no_of_decimal);
		else
			fld.value = commaSplit(filterNum(fld.value,no_of_decimal),no_of_decimal);
		}
	else
	
		fld.value = commaSplit(filterNum(fld.value),0);
		
	return false;
}


function trim(inputString) {
   /*Removes leading and trailing spaces from the passed string. Also removes
     consecutive spaces and replaces it with one space. If something besides
     a string is passed in (null, custom object, etc.) then return the input.*/
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { /*Check for spaces at the beginning of the string*/
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { /*Check for spaces at the end of the string*/
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { /*Note that there are two spaces in the string - look for multiple spaces within the string*/
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); /*Again, there are two spaces in each of the strings*/
   }
   return retValue; /*Return the trimmed string back to the user*/
}
function isNumber(nesne) 
{
	
	var inputStr=nesne.value;
	if(inputStr.length>0)
	{
		for(var i=0;i<inputStr.length;i++)
		{
			var oneChar = inputStr.substring(i,i+1);
			if (oneChar < "0" || oneChar > "9") 
			{
				nesne.value=inputStr.substring(0,i);
				return false;
			}
		}
	}
}
var upr = '0123456789.';
function isValid(parm,val)
{
  if (parm == "") return true;
  for (i=0; i<parm.length; i++) {
    if (val.indexOf(parm.charAt(i),0) == -1) return false;
  }
  return true;
}
function Check_Valid(obj)
{
  if(isValid(obj.value,upr))
  {return true;}
  else
  {obj.value= obj.value.substring(0,(obj.value.length-1));}
}





