var amt_inc=1,amt_min=1,amt_max=50;//config value for amount

var price_inc=10.00,price_min=20.00,price_max=200.00;//config value for price

var rch_inc=100.00, rch_min=300.00, rch_max=2000.00;//config value for price

var d10000up = 5,d5000up = 4,d3000up = 3,d1000up = 0,d500up = 0,d300up = 0;
//var d200up=5 ,d100up=4 ,d60up=3 ,d40up=2;//config value discount in percentage

var bonus_d40up=1 ,bonus_d50up=3 ,bonus_d60up=3,bonus_d80up=5 ,bonus_d100up=5 ,bonus_d200up=6;//config discount value in dollar

function updatePinCard(This,e_price,e_amt,e_subt,type,product,carddis)
{
	//alert(round("10.0055",2));
	if(type=='plus')
	{
		if(parseFloat(e_amt.value) < amt_max)
		{
			//alert(e_amt.value+ '-' + amt_max);
			e_amt.value = parseFloat(e_amt.value) + amt_inc;//update amount
			e_subt.value = round(e_amt.value * parseFloat(e_price.value));//update subtotal for each row
			This.amt.value = parseFloat(This.amt.value) + amt_inc;//update total amount
			This.subt.value = round(parseFloat(This.subt.value) + parseFloat(e_price.value));//update sum subtotal
		
			//increase discount in discount field and decrease total
			dis = parseFloat(This.dis.value) + parseFloat(carddis);
		}
		else//if over limit then discount field not change
			dis = parseFloat(This.dis.value);
	}
	else if(type=='minus')
	{
		if(e_amt.value > amt_min)//if amount element > amount minimum then update
		{
			e_amt.value = parseFloat(e_amt.value) - amt_inc;
			e_subt.value = round(e_amt.value * parseFloat(e_price.value));
			This.amt.value = parseFloat(This.amt.value) - amt_inc;
			This.subt.value = round(parseFloat(This.subt.value) - parseFloat(e_price.value));
		
			//decrease discount in discount field and increase total
			dis = parseFloat(This.dis.value) - parseFloat(carddis);
		}
		else//if over limit then discount field not change
			dis = parseFloat(This.dis.value);
	}
	//updateTotal(This);
	if(product == 'NewPin')
		dis = normalDiscount(This,This.subt.value);

	/*This.dis.value = round(dis);
	This.total.value = round(This.subt.value - dis);*/

	This.dis.value = "N/A";
	This.total.value = "N/A";

	This.updateChange.value = '1';//when update item on page
}
function updateRechargeCard(This,e_price,e_subt,e_bonus,pintype,type)
{
	if(type=='plus')
	{
		if(e_price.value < rch_max)
		{
			new_subt = round(parseFloat(e_price.value) + rch_inc);
			e_subt.value = e_price.value = new_subt;

			pintype == 'Recharge' ? bonus = round(bonusDiscount(This,new_subt)) : bonus = round(0);
			//e_bonus.value = bonus;
			
			This.subt.value = round(parseFloat(This.subt.value) + rch_inc);
		}
	}
	else if(type=='minus')
	{
		if(e_price.value > rch_min && e_subt.value > rch_min)
		{
			new_subt = round(parseFloat(e_price.value) - rch_inc);
			e_subt.value = e_price.value = new_subt;
			
			pintype == 'Recharge' ? bonus = round(bonusDiscount(This, new_subt)) : bonus = round(0);			
			//e_bonus.value = bonus;
			
			This.subt.value = round(parseFloat(This.subt.value) - rch_inc);
		}
	}
	//updateTotal(This);
	dis = normalDiscount(This,This.subt.value);

	/*This.dis.value = round(dis);
	This.total.value = round(This.subt.value - dis);*/

	This.dis.value = "N/A";
	This.total.value = "N/A";

	This.updateChange.value='0';//when update item on page
}
function updateTotal(This)
{
	/*dis = normalDiscount(This,This.subt.value);
	This.dis.value = round(dis);
	This.total.value = round(This.subt.value - dis);*/
}
function normalDiscount(This,subt)//calculate discount
{
	if(subt >= 10000)
		return (d10000up/100)*subt;
	else if(subt >= 5000)
		return (d5000up/100)*subt;
	else if(subt >= 3000)
		return (d3000up/100)*subt;
	else if(subt >= 1000)
		return (d1000up/100)*subt;
	else if(subt >= 500)
		return (d500up/100)*subt;
	else if(subt >= 300)
		return (d300up/100)*subt;
	/*else //if less than 40 or 1 card use unique card discount
		return (This.carddis.value*This.amt.value);*/
}
function bonusDiscount(This,subt)//calculate discount
{
	if(subt >= 200)
		return bonus_d200up;
	else if(subt >= 100)
		return bonus_d100up;
	else if(subt >= 80)
		return bonus_d80up;
	else if(subt >= 60)
		return bonus_d60up;
	else if(subt >= 50)
		return bonus_d50up;
	else if(subt >= 40)
		return bonus_d40up;
	else
		return 0;
}
function flush_decimal(value,X)
{
	//return round(value,2);
	value = ''+value+'';
	pos = value.indexOf('.');
	num = value.length - (pos+1);//count of decimal such as 3.0990 then num is 4
	
	if(pos != -1)//if find (.) position
	{
		if(num == 1)
			ans = value + "0";
		else if(num <= X)
			ans = value;
		else if(num > X)
			ans = value.substr(0,pos+X-1);
	}
	else
		ans = value + ".00";
	
	return ans;
}
function round(amount,X)
{
	// rounds number to X decimal places, defaults to 2
	amount= ''+amount+'';

	amount.replace("$","")
	amount.replace("?","")

	X = (!X ? 2 : X);
	return flush_decimal(Math.round(amount*Math.pow(10,X))/Math.pow(10,X),X);
}
