﻿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 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;
	}
}
// Removes leading whitespaces
function LTrim(value) {

    var re = /\s*((\S+\s*)*)/;
    return value.replace(re, "$1");

}

// Removes ending whitespaces
function RTrim(value) {

    var re = /((\s*\S+)*)\s*/;
    return value.replace(re, "$1");

}

// Removes leading and ending whitespaces
function trim(value) {

    return LTrim(RTrim(value));

}

function moneyCommaSep(ctrl)
{
  var separator = ",";
var int = ctrl.value.replace(/[a-zA-Z]/g, "") 
//alert(int);
  int = int.replace ( new RegExp ( separator, "g" ), "" );
  var regexp = new RegExp ( "\\B(\\d{3})(" + separator + "|$)" );
//alert(int);
//alert(regexp);
  do
  {
    int = int.replace ( regexp, separator + "$1" );
  }
  while ( int.search ( regexp ) >= 0 )
  ctrl.value = int;
  //document.getElementById(id).value = int;
}
function remove(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;
//alert(whichCode);
if (whichCode == 13) return true;  // Enter
if (whichCode == 46) return true;  //Delete
if (whichCode == 37) return true;  //->
if (whichCode == 39) return true;  //<-
if (whichCode == 35) return true; //End
if (e.ctrlKey && (whichCode == 65 ||whichCode == 67||whichCode == 86)) return true;
if (whichCode == 8) return true;  //BackSpace (Bug fixed)
if (whichCode == 17 || whichCode == 36 || whichCode == 9 ||  whichCode == 16) return true;  //Home
if (whichCode >= 96 && whichCode <=105) return true; 

	key = String.fromCharCode(whichCode);  // Get key value from key code
//alert(key);
//alert(strCheck.indexOf(key));
if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
return true;
}
function addCommas(nStr,id,sep,e)
{
var whichCode = (window.Event) ? e.which : e.keyCode;
//alert(whichCode);
if (whichCode == 17 || whichCode == 9 ) return;  //Home

	//alert(nStr);alert(id);
for (var n = 0; n < nStr.length; n++) {
onechar = nStr.substring(n, n+1);
//alert(onechar);
if (onechar == sep)
{
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) && sep!='') {
x1 = x1.replace(rgx, '$1' + sep + '$2');
}
//alert(x1 + x2);
document.getElementById(id).value = x1 + x2;
}