function version()//skaiciuokles versija
{
	return '1.9 (2006.01.26)';
}

//koks dabar laikas ir data
function now()
{
	var date = new Date();
	var d  = date.getDate();
	var day = (d < 10) ? '0' + d : d;
	var m = date.getMonth() + 1;
	var month = (m < 10) ? '0' + m : m;
	var yy = date.getYear();
	var year = (yy < 1000) ? yy + 1900 : yy;
	var h=date.getHours();
	var hour = (h < 10) ? '0' + h : h;
	var i=date.getMinutes();
	var minutes = (i < 10) ? '0' + i : i;
	return (''+year+'-'+month+'-'+day+' '+hour+':'+minutes);
}

function Isperk_sumos_koeficientas(metai, terminas)
{
  if(metai == terminas) return 1; 
  if(metai == 1) return 0.5;
  if(metai == 2) return 0.75;
  if(metai >= 3 && metai <= 4) return 0.9;
  if(metai >= 5 && metai <= 9) return 0.95;
  return 0.99;
}

function Isperk_sumos_koeficientas2(metai, terminas)
{
    if (metai==terminas || metai >= 15) return 1;
    else if (metai == 1) return 0;
    else if (metai == 2) return 0.5;
    else if (metai >= 3 && metai <= 5) return 0.9;
    else if (metai >=6 && metai <= 10) return 0.95;
    else return 0.99;
}

//qx
MortalityInvest_m = new Array(101);
MortalityInvest_v = new Array(101);

MortalityInvest_v[0] = 0.020356;
MortalityInvest_v[1] = 0.00158;
MortalityInvest_v[2] = 0.00104;
MortalityInvest_v[3] = 0.0007;
MortalityInvest_v[4] = 0.00056;
MortalityInvest_v[5] = 0.0004116;
MortalityInvest_v[6] = 0.000392;
MortalityInvest_v[7] = 0.000392;
MortalityInvest_v[8] = 0.000378;
MortalityInvest_v[9] = 0.00035;
MortalityInvest_v[10] = 0.000336;
MortalityInvest_v[11] = 0.00035;
MortalityInvest_v[12] = 0.000392;
MortalityInvest_v[13] = 0.000462;
MortalityInvest_v[14] = 0.0006027;
MortalityInvest_v[15] = 0.0007497;
MortalityInvest_v[16] = 0.001023;
MortalityInvest_v[17] = 0.0013795;
MortalityInvest_v[18] = 0.001767;
MortalityInvest_v[19] = 0.001984;
MortalityInvest_v[20] = 0.002046;
MortalityInvest_v[21] = 0.002077;
MortalityInvest_v[22] = 0.0021235;
MortalityInvest_v[23] = 0.0021545;
MortalityInvest_v[24] = 0.0021855;
MortalityInvest_v[25] = 0.002294;
MortalityInvest_v[26] = 0.0024335;
MortalityInvest_v[27] = 0.0025575;
MortalityInvest_v[28] = 0.0026815;
MortalityInvest_v[29] = 0.002821;
MortalityInvest_v[30] = 0.0028518;
MortalityInvest_v[31] = 0.002912;
MortalityInvest_v[32] = 0.003164;
MortalityInvest_v[33] = 0.003472;
MortalityInvest_v[34] = 0.003808;
MortalityInvest_v[35] = 0.004186;
MortalityInvest_v[36] = 0.004606;
MortalityInvest_v[37] = 0.005068;
MortalityInvest_v[38] = 0.005187;
MortalityInvest_v[39] = 0.005707;
MortalityInvest_v[40] = 0.006266;
MortalityInvest_v[41] = 0.006348;
MortalityInvest_v[42] = 0.006936;
MortalityInvest_v[43] = 0.00756;
MortalityInvest_v[44] = 0.00822;
MortalityInvest_v[45] = 0.008916;
MortalityInvest_v[46] = 0.00966;
MortalityInvest_v[47] = 0.010476;
MortalityInvest_v[48] = 0.011364;
MortalityInvest_v[49] = 0.012336
MortalityInvest_v[50] = 0.01338;
MortalityInvest_v[51] = 0.014508;
MortalityInvest_v[52] = 0.015696;
MortalityInvest_v[53] = 0.016968;
MortalityInvest_v[54] = 0.017572;
MortalityInvest_v[55] = 0.018998;
MortalityInvest_v[56] = 0.019668;
MortalityInvest_v[57] = 0.021285;
MortalityInvest_v[58] = 0.023001;
MortalityInvest_v[59] = 0.024816;
MortalityInvest_v[60] = 0.026719;
MortalityInvest_v[61] = 0.02871;
MortalityInvest_v[62] = 0.030833;
MortalityInvest_v[63] = 0.033088;
MortalityInvest_v[64] = 0.035486;
MortalityInvest_v[65] = 0.038049;
MortalityInvest_v[66] = 0.040821;
MortalityInvest_v[67] = 0.043857;
MortalityInvest_v[68] = 0.047168;
MortalityInvest_v[69] = 0.050842;
MortalityInvest_v[70] = 0.052374;
MortalityInvest_v[71] = 0.056469;
MortalityInvest_v[72] = 0.0608895;
MortalityInvest_v[73] = 0.065583;
MortalityInvest_v[74] = 0.07056;
MortalityInvest_v[75] = 0.0759885;
MortalityInvest_v[76] = 0.081984;
MortalityInvest_v[77] = 0.0887775;
MortalityInvest_v[78] = 0.0964005;
MortalityInvest_v[79] = 0.1048215;
MortalityInvest_v[80] = 0.1139775;
MortalityInvest_v[81] = 0.1236375;
MortalityInvest_v[82] = 0.1334025;
MortalityInvest_v[83] = 0.143409;
MortalityInvest_v[84] = 0.153804;
MortalityInvest_v[85] = 0.15638;
MortalityInvest_v[86] = 0.16828;
MortalityInvest_v[87] = 0.18095;
MortalityInvest_v[88] = 0.19443;
MortalityInvest_v[89] = 0.20874; 
MortalityInvest_v[90] = 0.22391;
MortalityInvest_v[91] = 0.23994;
MortalityInvest_v[92] = 0.25685;
MortalityInvest_v[93] = 0.27464;
MortalityInvest_v[94] = 0.29332;
MortalityInvest_v[95] = 0.31287; 
MortalityInvest_v[96] = 0.33328;
MortalityInvest_v[97] = 0.35452;
MortalityInvest_v[98] = 0.37657;
MortalityInvest_v[99] = 0.39938;
MortalityInvest_v[100] = 0.42289;

MortalityInvest_m[0] = 0.017192;
MortalityInvest_m[1] = 0.00128;
MortalityInvest_m[2] = 0.00086;
MortalityInvest_m[3] = 0.00056;
MortalityInvest_m[4] = 0.00042;
MortalityInvest_m[5] = 0.0002793;
MortalityInvest_m[6] = 0.000252;
MortalityInvest_m[7] = 0.000238;
MortalityInvest_m[8] = 0.00021;
MortalityInvest_m[9] = 0.000196;
MortalityInvest_m[10] = 0.000196;
MortalityInvest_m[11] = 0.00021;
MortalityInvest_m[12] = 0.000224;
MortalityInvest_m[13] = 0.00028;
MortalityInvest_m[14] = 0.0003675;
MortalityInvest_m[15] = 0.000441;
MortalityInvest_m[16] = 0.0005115;
MortalityInvest_m[17] = 0.0005115;
MortalityInvest_m[18] = 0.0005115;
MortalityInvest_m[19] = 0.000527;
MortalityInvest_m[20] = 0.000527;
MortalityInvest_m[21] = 0.0005425;
MortalityInvest_m[22] = 0.000558;
MortalityInvest_m[23] = 0.0005735;
MortalityInvest_m[24] = 0.0005735;
MortalityInvest_m[25] = 0.000589;
MortalityInvest_m[26] = 0.00062;
MortalityInvest_m[27] = 0.0006665;
MortalityInvest_m[28] = 0.0007285;
MortalityInvest_m[29] =	0.0007905;
MortalityInvest_m[30] = 0.0008085;
MortalityInvest_m[31] = 0.00084;
MortalityInvest_m[32] = 0.000924;
MortalityInvest_m[33] = 0.001022;
MortalityInvest_m[34] = 0.001148;
MortalityInvest_m[35] = 0.001274;
MortalityInvest_m[36] = 0.001428;
MortalityInvest_m[37] = 0.001582;
MortalityInvest_m[38] = 0.001638;
MortalityInvest_m[39] = 0.001833;
MortalityInvest_m[40] = 0.002041;
MortalityInvest_m[41] =	0.002112;
MortalityInvest_m[42] = 0.002352;
MortalityInvest_m[43] = 0.002628;
MortalityInvest_m[44] = 0.002916;
MortalityInvest_m[45] = 0.003228;
MortalityInvest_m[46] = 0.00354;
MortalityInvest_m[47] = 0.003852;
MortalityInvest_m[48] = 0.004176;
MortalityInvest_m[49] = 0.004512;
MortalityInvest_m[50] = 0.004848;
MortalityInvest_m[51] = 0.005208;
MortalityInvest_m[52] = 0.00558;
MortalityInvest_m[53] = 0.005976;
MortalityInvest_m[54] = 0.0061525;
MortalityInvest_m[55] = 0.006624;
MortalityInvest_m[56] = 0.006875;
MortalityInvest_m[57] = 0.007513;
MortalityInvest_m[58] = 0.00825;
MortalityInvest_m[59] = 0.009075;
MortalityInvest_m[60] = 0.009988;
MortalityInvest_m[61] = 0.011;
MortalityInvest_m[62] = 0.012111;
MortalityInvest_m[63] = 0.013354;
MortalityInvest_m[64] = 0.014773;
MortalityInvest_m[65] = 0.016401;
MortalityInvest_m[66] = 0.018227;
MortalityInvest_m[67] = 0.020251;
MortalityInvest_m[68] = 0.022506;
MortalityInvest_m[69] = 0.025069;
MortalityInvest_m[70] = 0.026712;
MortalityInvest_m[71] = 0.0298305;
MortalityInvest_m[72] = 0.0333795;
MortalityInvest_m[73] = 0.0372645;
MortalityInvest_m[74] = 0.0414855;
MortalityInvest_m[75] = 0.046095;
MortalityInvest_m[76] = 0.0512295;
MortalityInvest_m[77] = 0.0570885;
MortalityInvest_m[78] = 0.063777;
MortalityInvest_m[79] = 0.0713265;
MortalityInvest_m[80] = 0.079779;
MortalityInvest_m[81] = 0.0889665;
MortalityInvest_m[82] = 0.098805;
MortalityInvest_m[83] = 0.109326;
MortalityInvest_m[84] = 0.1206555;
MortalityInvest_m[85] = 0.12642;
MortalityInvest_m[86] = 0.13928;
MortalityInvest_m[87] = 0.15316;
MortalityInvest_m[88] = 0.1681;
MortalityInvest_m[89] = 0.18413;
MortalityInvest_m[90] = 0.20128;
MortalityInvest_m[91] = 0.21955;
MortalityInvest_m[92] = 0.23896;
MortalityInvest_m[93] = 0.25951;
MortalityInvest_m[94] = 0.28119;
MortalityInvest_m[95] = 0.30398;
MortalityInvest_m[96] = 0.32784;
MortalityInvest_m[97] = 0.35272;
MortalityInvest_m[98] = 0.37857;
MortalityInvest_m[99] = 0.4053;
MortalityInvest_m[100] = 0.43282;

function qxInvest(x, lytis)
{
	var k=0;
	if ((lytis == 3 || lytis == 4) && (x>=0 && x<101))	{
		k = (lytis==3) ? MortalityInvest_v[x]:MortalityInvest_m[x];
	}
	return k;
}

//lx
MorbidityInvest_v = new Array(67);
MorbidityInvest_m = new Array(67);

MorbidityInvest_v[0] = 0.000646597906202237;
MorbidityInvest_v[1] = 0.000646597906202237;
MorbidityInvest_v[2] = 0.000646597906202237;	
MorbidityInvest_v[3] = 0.000646597906202237;
MorbidityInvest_v[4] = 0.000646597906202237;
MorbidityInvest_v[5] = 0.000646597906202237;
MorbidityInvest_v[6] = 0.000646597906202237;
MorbidityInvest_v[7] = 0.000646597906202237;
MorbidityInvest_v[8] = 0.000646597906202237;
MorbidityInvest_v[9] = 0.000646597906202237;
MorbidityInvest_v[10] = 0.000646597906202237;
MorbidityInvest_v[11] = 0.000646597906202237;
MorbidityInvest_v[12] = 0.000646597906202237;
MorbidityInvest_v[13] = 0.000646597906202237;
MorbidityInvest_v[14] = 0.000646597906202237;
MorbidityInvest_v[15] = 0.000646597906202237;
MorbidityInvest_v[16] = 0.000646597906202237;
MorbidityInvest_v[17] = 0.000646597906202237;
MorbidityInvest_v[18] = 0.000646597906202237;
MorbidityInvest_v[19] = 0.000646597906202237;	
MorbidityInvest_v[20] = 0.000646597906202237;
MorbidityInvest_v[21] = 0.000646597906202237;
MorbidityInvest_v[22] = 0.000646597906202237;
MorbidityInvest_v[23] = 0.000646597906202237;
MorbidityInvest_v[24] = 0.000646597906202237;
MorbidityInvest_v[25] = 0.000646597906202237;
MorbidityInvest_v[26] = 0.000670774657251839;
MorbidityInvest_v[27] = 0.00074366783549358;
MorbidityInvest_v[28] = 0.000850903138036779;
MorbidityInvest_v[29] = 0.000996130115190635;
MorbidityInvest_v[30] = 0.00120008284362401;
MorbidityInvest_v[31] = 0.00140322318519354;
MorbidityInvest_v[32] = 0.00162034873276064;
MorbidityInvest_v[33] = 0.00189567494941431;
MorbidityInvest_v[34] = 0.00214903801837663;
MorbidityInvest_v[35] = 0.00240314909797199;
MorbidityInvest_v[36] = 0.00274465246027964;
MorbidityInvest_v[37] = 0.00318613756879498;
MorbidityInvest_v[38] = 0.00377777552725341;	
MorbidityInvest_v[39] = 0.00448437053280314;
MorbidityInvest_v[40] = 0.00521653579204957;
MorbidityInvest_v[41] = 0.00603582833606074;
MorbidityInvest_v[42] = 0.00682621660377417;
MorbidityInvest_v[43] = 0.00747340541521676;
MorbidityInvest_v[44] = 0.00825083950151605;
MorbidityInvest_v[45] = 0.00917868339994654;
MorbidityInvest_v[46] = 0.0100755490163297;
MorbidityInvest_v[47] = 0.0110344306385284;
MorbidityInvest_v[48] = 0.0120907331375628;
MorbidityInvest_v[49] = 0.0131285137876426;
MorbidityInvest_v[50] = 0.0140973235159452;
MorbidityInvest_v[51] = 0.0150956228776193;
MorbidityInvest_v[52] = 0.0163729205579647;
MorbidityInvest_v[53] = 0.0177911719087263;
MorbidityInvest_v[54] = 0.0191866676660967;	
MorbidityInvest_v[55] = 0.0207584591716755;
MorbidityInvest_v[56] = 0.022500127483613;
MorbidityInvest_v[57] = 0.0241178105516794;
MorbidityInvest_v[58] = 0.0256066547644174;
MorbidityInvest_v[59] = 0.0270518130922051;
MorbidityInvest_v[60] = 0.0283743628104625;
MorbidityInvest_v[61] = 0.0283264037445645;
MorbidityInvest_v[62] = 0.0279997205648555;
MorbidityInvest_v[63] = 0.0287444713368195;
MorbidityInvest_v[64] = 0.0297163928312327;
MorbidityInvest_v[65] = 0.0308211215265323;

MorbidityInvest_m[0] = 0.000815136306106284;
MorbidityInvest_m[1] = 0.000815136306106284;
MorbidityInvest_m[2] = 0.000815136306106284;
MorbidityInvest_m[3] = 0.000815136306106284;
MorbidityInvest_m[4] = 0.000815136306106284;
MorbidityInvest_m[5] = 0.000815136306106284;
MorbidityInvest_m[6] = 0.000815136306106284;
MorbidityInvest_m[7] = 0.000815136306106284;
MorbidityInvest_m[8] = 0.000815136306106284;
MorbidityInvest_m[9] = 0.000815136306106284;
MorbidityInvest_m[10] = 0.000815136306106284;
MorbidityInvest_m[11] = 0.000815136306106284;
MorbidityInvest_m[12] = 0.000815136306106284;
MorbidityInvest_m[13] = 0.000815136306106284;
MorbidityInvest_m[14] = 0.000815136306106284;
MorbidityInvest_m[15] = 0.000815136306106284;
MorbidityInvest_m[16] = 0.000815136306106284;
MorbidityInvest_m[17] = 0.000815136306106284;
MorbidityInvest_m[18] = 0.000815136306106284;
MorbidityInvest_m[19] = 0.000815136306106284;
MorbidityInvest_m[20] = 0.000815136306106284;
MorbidityInvest_m[21] = 0.000815136306106284;
MorbidityInvest_m[22] = 0.000815136306106284;
MorbidityInvest_m[23] = 0.000815136306106284;
MorbidityInvest_m[24] = 0.000815136306106284;
MorbidityInvest_m[25] = 0.000815136306106284;
MorbidityInvest_m[26] = 0.000825428013998764;
MorbidityInvest_m[27] = 0.000872096033407586;
MorbidityInvest_m[28] = 0.00096254755977411;
MorbidityInvest_m[29] = 0.00104922116357028;
MorbidityInvest_m[30] = 0.00114253277864717;
MorbidityInvest_m[31] = 0.00127330509638506;
MorbidityInvest_m[32] = 0.00142845749456324;
MorbidityInvest_m[33] = 0.00162454856731738;
MorbidityInvest_m[34] = 0.00186915671263124;
MorbidityInvest_m[35] = 0.00215600980934012;
MorbidityInvest_m[36] = 0.00248785338379869;
MorbidityInvest_m[37] = 0.00281358467756804;
MorbidityInvest_m[38] = 0.00312714708030229;
MorbidityInvest_m[39] = 0.00347812183851159;
MorbidityInvest_m[40] = 0.00389539565909281;
MorbidityInvest_m[41] = 0.00434661876634081;
MorbidityInvest_m[42] = 0.00478544752781813;
MorbidityInvest_m[43] = 0.00525588126622408;
MorbidityInvest_m[44] = 0.00580457325574511;
MorbidityInvest_m[45] = 0.00641098478773043;
MorbidityInvest_m[46] = 0.00705479164946088;
MorbidityInvest_m[47] = 0.00771294675013199;
MorbidityInvest_m[48] = 0.00835811484705854;
MorbidityInvest_m[49] = 0.00894976977536897;
MorbidityInvest_m[50] = 0.00943783028844488;
MorbidityInvest_m[51] = 0.00996624851313799;
MorbidityInvest_m[52] = 0.0106154098355385;
MorbidityInvest_m[53] = 0.0112114261500557;
MorbidityInvest_m[54] = 0.0117492557231153;
MorbidityInvest_m[55] = 0.0123756598373868;
MorbidityInvest_m[56] = 0.0130990318563754;
MorbidityInvest_m[57] = 0.0138690429800052;
MorbidityInvest_m[58] = 0.01479530905403;
MorbidityInvest_m[59] = 0.0158649009805592;
MorbidityInvest_m[60] = 0.0169623294887976;
MorbidityInvest_m[61] = 0.016355959298137;
MorbidityInvest_m[62] = 0.0155816344396921;
MorbidityInvest_m[63] = 0.0162714833488468;
MorbidityInvest_m[64] = 0.0169617100235982;
MorbidityInvest_m[65] = 0.0177400176296477;
	
function lxInvest(x, lytis)
{
	var k=0;
	if ((lytis == 3 || lytis == 4) && (x>=0 && x<66))	{
		k = (lytis==3) ? MorbidityInvest_v[x]:MorbidityInvest_m[x];
	}
	return k;
}

function koefCI(lytis, sarasas)
{
	if (sarasas == false){
		koefCI1= (lytis==3) ? 1.59 : 1.95 ;
    } else {
		koefCI1= (lytis==3) ? 1.3 : 1.6 ;
	}
	return koefCI1
}

function format(number,isInt)
{
	var vv;
	
	if (isInt==true) 
    {
        vv=formatNumber(Math.round(number));
    }
	else {
	
		var sv=Math.round(number * 100);
		vv=""+(sv/100);
		
		if (sv % 100 == 0) 
        {
            vv=formatNumber(vv)+".00";
        }
		else if(sv % 10 == 0) 
        {
            var parts = vv.split('.'); 
            vv = formatNumber(parts[0]) + '.' + parts[1] + '0';
        }
		else {
            var parts = vv.split('.'); 
            vv = formatNumber(parts[0]) + '.' + parts[1];
        }
	}
	return (vv+" Lt");
}

function output(res)
{
	v.set_value('tabl',res);
	laikas.innerHTML=res;
}

Number.formatFunctions = {count:0};

// Constants useful for controlling the format of numbers in special cases.
Number.prototype.NaN         = 'NaN';
Number.prototype.posInfinity = 'Infinity';
Number.prototype.negInfinity = '-Infinity';

Number.prototype.numberFormat = function(format, context) {
    if (isNaN(this) ) {
        return Number.prototype.NaNstring;
    }
    else if (this == +Infinity ) {
        return Number.prototype.posInfinity;
    }
    else if ( this == -Infinity) {
        return Number.prototype.negInfinity;
    }
    else if (Number.formatFunctions[format] == null) {
        Number.createNewFormat(format);
    }
    return this[Number.formatFunctions[format]](context);
}

Number.createNewFormat = function(format) {
    var funcName = "format" + Number.formatFunctions.count++;
    Number.formatFunctions[format] = funcName;
    var code = "Number.prototype." + funcName + " = function(context){\n";

    // Decide whether the function is a terminal or a pos/neg/zero function
    var formats = format.split(";");
    switch (formats.length) {
        case 1:
            code += Number.createTerminalFormat(format);
            break;
        case 2:
            code += "return (this < 0) ? this.numberFormat(\""
                + String.escape(formats[1])
                + "\", 1) : this.numberFormat(\""
                + String.escape(formats[0])
                + "\", 2);";
            break;
        case 3:
            code += "return (this < 0) ? this.numberFormat(\""
                + String.escape(formats[1])
                + "\", 1) : ((this == 0) ? this.numberFormat(\""
                + String.escape(formats[2])
                + "\", 2) : this.numberFormat(\""
                + String.escape(formats[0])
                + "\", 3));";
            break;
        default:
            code += "throw 'Too many semicolons in format string';";
            break;
    }
    eval(code + "}");
}

Number.createTerminalFormat = function(format) {
    // If there is no work to do, just return the literal value
    if (format.length > 0 && format.search(/[0#?]/) == -1) {
        return "return '" + String.escape(format) + "';\n";
    }
    // Negative values are always displayed without a minus sign when section separators are used.
    var code = "var val = (context == null) ? new Number(this) : Math.abs(this);\n";
    var thousands = false;
    var lodp = format;
    var rodp = "";
    var ldigits = 0;
    var rdigits = 0;
    var scidigits = 0;
    var scishowsign = false;
    var sciletter = "";
    // Look for (and remove) scientific notation instructions, which can be anywhere
    m = format.match(/\..*(e)([+-]?)(0+)/i);
    if (m) {
        sciletter = m[1];
        scishowsign = (m[2] == "+");
        scidigits = m[3].length;
        format = format.replace(/(e)([+-]?)(0+)/i, "");
    }
    // Split around the decimal point
    var m = format.match(/^([^.]*)\.(.*)$/);
    if (m) {
        lodp = m[1].replace(/\./g, "");
        rodp = m[2].replace(/\./g, "");
    }
    // Look for %
    if (format.indexOf('%') >= 0) {
        code += "val *= 100;\n";
    }
    // Look for comma-scaling to the left of the decimal point
    m = lodp.match(/(,+)(?:$|[^0#?,])/);
    if (m) {
        code += "val /= " + Math.pow(1000, m[1].length) + "\n;";
    }
    // Look for comma-separators
    if (lodp.search(/[0#?],[0#?]/) >= 0) {
        thousands = true;
    }
    // Nuke any extraneous commas
    if ((m) || thousands) {
        lodp = lodp.replace(/,/g, "");
    }
    // Figure out how many digits to the l/r of the decimal place
    m = lodp.match(/0[0#?]*/);
    if (m) {
        ldigits = m[0].length;
    }
    m = rodp.match(/[0#?]*/);
    if (m) {
        rdigits = m[0].length;
    }
    // Scientific notation takes precedence over rounding etc
    if (scidigits > 0) {
        code += "var sci = Number.toScientific(val,"
            + ldigits + ", " + rdigits + ", " + scidigits + ", " + scishowsign + ");\n"
            + "var arr = [sci.l, sci.r];\n";
    }
    else {
        // If there is no decimal point, round to nearest integer, AWAY from zero
        if (format.indexOf('.') < 0) {
            code += "val = (val > 0) ? Math.ceil(val) : Math.floor(val);\n";
        }
        // Numbers are rounded to the correct number of digits to the right of the decimal
        code += "var arr = val.round(" + rdigits + ").toFixed(" + rdigits + ").split('.');\n";
        // There are at least "ldigits" digits to the left of the decimal, so add zeros if needed.
        code += "arr[0] = (val < 0 ? '-' : '') + String.leftPad((val < 0 ? arr[0].substring(1) : arr[0]), "
            + ldigits + ", '0');\n";
    }
    // Add thousands separators
    if (thousands) {
        code += "arr[0] = Number.addSeparators(arr[0]);\n";
    }
    // Insert the digits into the formatting string.  On the LHS, extra digits are copied
    // into the result.  On the RHS, rounding has chopped them off.
    code += "arr[0] = Number.injectIntoFormat(arr[0].reverse(), '"
        + String.escape(lodp.reverse()) + "', true).reverse();\n";
    if (rdigits > 0) {
        code += "arr[1] = Number.injectIntoFormat(arr[1], '" + String.escape(rodp) + "', false);\n";
    }
    if (scidigits > 0) {
        code += "arr[1] = arr[1].replace(/(\\d{" + rdigits + "})/, '$1" + sciletter + "' + sci.s);\n";
    }
    return code + "return arr.join('.');\n";
}

Number.toScientific = function(val, ldigits, rdigits, scidigits, showsign) {
    var result = {l:"", r:"", s:""};
    var ex = "";
    // Make ldigits + rdigits significant figures
    var before = Math.abs(val).toFixed(ldigits + rdigits + 1).trim('0');
    // Move the decimal point to the right of all digits we want to keep,
    // and round the resulting value off
    var after = Math.round(new Number(before.replace(".", "").replace(
        new RegExp("(\\d{" + (ldigits + rdigits) + "})(.*)"), "$1.$2"))).toFixed(0);
    // Place the decimal point in the new string
    if (after.length >= ldigits) {
        after = after.substring(0, ldigits) + "." + after.substring(ldigits);
    }
    else {
        after += '.';
    }
    // Find how much the decimal point moved.  This is #places to LODP in the original
    // number, minus the #places in the new number.  There are no left-padded zeroes in
    // the new number, so the calculation for it is simpler than for the old number.
    result.s = (before.indexOf(".") - before.search(/[1-9]/)) - after.indexOf(".");
    // The exponent is off by 1 when it gets moved to the left.
    if (result.s < 0) {
        result.s++;
    }
    // Split the value around the decimal point and pad the parts appropriately.
    result.l = (val < 0 ? '-' : '') + String.leftPad(after.substring(0, after.indexOf(".")), ldigits, "0");
    result.r = after.substring(after.indexOf(".") + 1);
    if (result.s < 0) {
        ex = "-";
    }
    else if (showsign) {
        ex = "+";
    }
    result.s = ex + String.leftPad(Math.abs(result.s).toFixed(0), scidigits, "0");
    return result;
}

Number.prototype.round = function(decimals) {
    if (decimals > 0) {
        var m = this.toFixed(decimals + 1).match(
            new RegExp("(-?\\d*)\.(\\d{" + decimals + "})(\\d)\\d*$"));
        if (m && m.length) {
            return new Number(m[1] + "." + String.leftPad(Math.round(m[2] + "." + m[3]), decimals, "0"));
        }
    }
    return this;
}

Number.injectIntoFormat = function(val, format, stuffExtras) {
    var i = 0;
    var j = 0;
    var result = "";
    var revneg = val.charAt(val.length - 1) == '-';
    if ( revneg ) {
       val = val.substring(0, val.length - 1);
    }
    while (i < format.length && j < val.length && format.substring(i).search(/[0#?]/) >= 0) {
        if (format.charAt(i).match(/[0#?]/)) {
            // It's a formatting character; copy the corresponding character
            // in the value to the result
            if (val.charAt(j) != '-') {
                result += val.charAt(j);
            }
            else {
                result += "0";
            }
            j++;
        }
        else {
            result += format.charAt(i);
        }
        ++i;
    }
    if ( revneg && j == val.length ) {
        result += '-';
    }
    if (j < val.length) {
        if (stuffExtras) {
            result += val.substring(j);
        }
        if ( revneg ) {
             result += '-';
        }
    }
    if (i < format.length) {
        result += format.substring(i);
    }
    return result.replace(/#/g, "").replace(/\?/g, " ");
}

Number.addSeparators = function(val) {
    return val.reverse().replace(/(\d{3})/g, "$1,").reverse().replace(/^(-)?,/, "$1");
}

String.prototype.reverse = function() {
    var res = "";
    for (var i = this.length; i > 0; --i) {
        res += this.charAt(i - 1);
    }
    return res;
}

String.prototype.trim = function(ch) {
    if (!ch) ch = ' ';
    return this.replace(new RegExp("^" + ch + "+|" + ch + "+$", "g"), "");
}

String.leftPad = function (val, size, ch) {
    var result = new String(val);
    if (ch == null) {
        ch = " ";
    }
    while (result.length < size) {
        result = ch + result;
    }
    return result;
}

String.escape = function(string) {
    return string.replace(/('|\\)/g, "\\$1");
}

String.prototype.numerize = function(string) {
    return this.replace(/\D/g, '') * 1;
}

function formatSum(field)
{
   field.value = field.value.numerize();
    
   if(field.value.length <= 3)
   {
       var format = '###';
   }
   else if(field.value.length > 3 && field.value.length <= 6)
   {
       var format = '### ###';
   }
   else
   {
       var format = '### ### ###';
   }
   
   field.value = new Number(field.value).numberFormat(format); 
}

function formatNumber(n)
{
   n = n + '';
   n = n.numerize();
    
   if(n.length <= 3)
   {
       var format = '###';
   }
   else if(n.length > 3 && n.length <= 6)
   {
       var format = '### ###';
   }
   else
   {
       var format = '### ### ###';
   }
   
   return new Number(n).numberFormat(format); 
}


