﻿function currencyFormat(fld, milSep, decSep, e) {
var sep = 0;
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 == 13) return true;  // Enter
if (whichCode == 8) return true;  // Delete (Bug fixed)
	key = String.fromCharCode(whichCode);  // Get key value from key code
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) != '0') && (fld.value.charAt(i) != decSep)) break;
aux = '';
for(; i < len; i++)
if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);//alert(i);
//alert(fld.value.charAt(i));}
aux += key;
len = aux.length;
if (len == 0) fld.value = '';
if (len > 0) {
aux2 = '';
for (j = 0, i = len - 1; 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);
}
return false;
}

/*
function currencyFormat(fld, milSep, decSep, e) {
	var sep = 0;
	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 == 13) 
		return true;  // Enter
	if (whichCode == 8)
		return true;  // Delete (Bug fixed)
	key = String.fromCharCode(whichCode);  // Get key value from key code
	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) != '0') && (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 <=2 ) 
		fld.value = aux;
	if (len > 2) {
	aux == '1234';
	fld.value = '123';
		aux2 = '';
		for (j = 0, i = len - 1; 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);
		}
	return false;
}
*/
function commaSplit(srcNumber) {
var txtNumber = '' + srcNumber;
if (isNaN(txtNumber) || txtNumber == "") {
//alert("Oops!  That does not appear to be a valid number.  Please try again.");
//txtMadrak.select();
//txtMadrak.focus();
fieldName.select();
fieldName.focus();
}
else {
var rxSplit = new RegExp('([0-9])([0-9][0-9][0-9][,.])');
var arrNumber = txtNumber.split('.');
arrNumber[0] += '.';
do {
arrNumber[0] = arrNumber[0].replace(rxSplit, '$1,$2');
} while (rxSplit.test(arrNumber[0]));
if (arrNumber.length > 1) {
return arrNumber.join('');
}
else {
return arrNumber[0].split('.')[0];
      }
   }
}
//  End -->

function addCommas(nStr,id)
{
for (var n = 0; n < nStr.length; n++) {
onechar = nStr.substring(n, n+1);
//alert(onechar);
if (onechar == ",")
{
bl = false;
var t;
var s;
t = nStr.charAt(n).toString();
nStr = nStr.replace(t,"");
}
}
//alert(nStr);
nStr += '';
x = nStr.split('.');
x1 = x[0];
x3 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
document.getElementByid(id).value = x1 + x2;
}
function IsInteger(sText)
{
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;

		
	for (i = 0; i < sText.length && IsNumber == true; i++) 
		{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
			{
			IsNumber = false;
			}
		}
	return IsNumber;
	
}
function TestKeyPress( obj, event, Pattern)
{ 
	var curChar    = String.fromCharCode( event.keyCode ); 
	var inpStr     = obj.value + curChar;
	window.status  = '';
	obj.title      = '';

	result = inpStr.match( Pattern);
	if ( ! result )
	{
		window.status     = 'لطفا عدد وارد کنيد';
		obj.title         = window.status;
		event.returnValue = false;
		event.cancel      = true;
	}
}