function strchr (str,chr)
{
	var i;

	for(i=0; i<str.length; i++)
		if(str.charAt(i) == chr)
			return i;

	return -1;
}

function CheckPromoCode()
{
	var validChars = "0123456789ABCDEFGHJKLMNPRSTUVWXYZ";
	var promocode;
	var n = 0;
	var m = 0;
	var i = 0;
	
	if(document.form.promoField)
	{
		promoCode = document.form.promoField.value.toUpperCase();
		
		if(promoCode.length != 6) return 0;

		for(i=0;i<promoCode.length; i++)
		{
			p = strchr(validChars,promoCode.charAt(i));
		
			if(p == -1) return 0;
			else	    n += p;
		}
	
		n = n % 11;

		if(n==0)	return 1;
		return 0;
	}
	return 0;
}

function GreenPromoField()
{
	if(document.form.promoField)
	{
		if(CheckPromoCode())
		{
			document.form.promoField.style.backgroundColor = "#A7FDAF";
		}
		else
		{
			document.form.promoField.style.backgroundColor = "#FFB7B7";
		}
	}
}

function InsertPromoField()
{
	document.getElementById("promoPrompt").innerHTML = "What is your promotion code?";
	document.getElementById("promoField").style.visibility  = "visible";
	CalculateTotal();
}

function AddCommas(n)
{
	n += '';
	x = n.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function CalculateTotal ()
{
	// globals: unit_price, promo_discount, unit_discount, item_name, max_discount

	// internals
	var ccyrate  = document.form.selectCcy.value;
	var units    = document.form.txtQuantity.value;	
	var items    = (unit_price*units)*ccyrate;
	var shipping = (document.form.selectDestination.value*units)*ccyrate;
	var total    = 0;
	var vat      = 0;
	var vatrate  = 20/100;
	var discount = 0;

	// Reduce shipping
	if((units>1)&(units<=4))
	{
		     if(document.form.selectDestination.selectedIndex == 0)
			shipping = Math.min(max_uk_shipping,shipping);
		else if(document.form.selectDestination.selectedIndex == 1)
			shipping = Math.min(max_eu_shipping,shipping);
		else if(document.form.selectDestination.selectedIndex == 2)
			shipping = Math.min(max_us_shipping,shipping);
		else
			shipping = Math.min(max_ww_shipping,shipping);
	}
		

	// 0. Calculate VAT Rate
	if(document.form.selectDestination.selectedIndex > 1) vatrate = 0;

	// 1. calculate size of discount
	discount = Math.max(promo_discount*CheckPromoCode(),(units-1)*unit_discount);
	discount = Math.min(discount,max_discount);
	
	// 2. apply discount
	items    = (units > 0)*items*(1-discount/100);
	shipping = (units > 0)*shipping*(1-discount/100);
	vat      = (units > 0)*(items+shipping)*vatrate;
	total    = items+shipping+vat;
	
	// 3. round
	items    = AddCommas(items.toFixed(2));
	shipping = AddCommas(shipping.toFixed(2));
	vat      = AddCommas(vat.toFixed(2));
	total    = AddCommas(total.toFixed(2));

	// 4. Order Strings
	if(units) 	order = units+"x "+item_name;
	else		order = "0x "+ item_name;
	
	// 5. Print Strings
	document.getElementById("billQuantity").innerHTML = order;
	document.getElementById("billItems").innerHTML = items;
	document.getElementById("billShipping").innerHTML = shipping;
	document.getElementById("billVAT").innerHTML = vat;
	document.getElementById("billTotal").innerHTML = total;
	
	GreenPromoField();
}
