// the number of shippings
// salex TAX
var salesTax = 9.75;

// subtotal and total calculus
var sum_subtotal = 0;
var sum_total = 0;
var sum_shipping = 0;
var vTotalShipping = 8;


// Shipping quantity: ... from ... to
var storageS_qty = new Array (
1,
16,
25,
35,
50,
75,
100,
150
);


// Shipping price
var storageS_val = new Array (
6.50,
7.50,
8.50,
9.50,
10.50,
11.50,
12.50,
13.50
);




// Function to calculate a row
function f_input(index)
{
fieldPrice = 'price_' + index;
fieldQuant = 'qty_' + index;
fieldExt = 'ext_' + index;
str = document.order[fieldQuant].value;
if (trim(str)=="") 
{
	document.order[fieldQuant].value = "";
    document.order[fieldExt].value = "";                
}
else
{
	if (containsOnlyDigits(str)) 
    {
		tmp = document.order[fieldPrice].value * document.order[fieldQuant].value; 
		document.order[fieldExt].value = tmp; 
		sum_subtotal = sum_subtotal + tmp;
	}
	else 
 	{
    	document.order[fieldQuant].value = "";
        document.order[fieldExt].value = "";
   	}
}

if(document.order.check_sales_tax.checked)
{
	tmp1 = sum_subtotal * salesTax / 100;
	document.order.salestax_input.value = formatNR(tmp1,2);
}

if(sum_subtotal)
{
	document.order.subtotal_input.value = formatNR(sum_subtotal,2);
}
}


// Function to calculate sub-total
function f_subtotal()
{
var vTotalProducts = document.order.vTotalProducts.value;
tmp_sum = 0;
for(index=0; index<vTotalProducts; index++)
{
	fieldPrice = 'price_' + index;
	fieldQuant = 'qty_' + index;
	fieldExt = 'ext_' + index;
	str = document.order[fieldQuant].value;
	if (trim(str)=="") 
	{
        document.order[fieldQuant].value = "";
        document.order[fieldExt].value = "";                
	}
	else
	{
        if (containsOnlyDigits(str)) 
        {
			tmp = document.order[fieldPrice].value * document.order[fieldQuant].value; 
			document.order[fieldExt].value = tmp; 
			tmp_sum = tmp_sum + tmp;		
		}
		else 
        {
     		document.order[fieldQuant].value = "";
          	document.order[fieldExt].value = "";
        }
	}
}
sum_subtotal = tmp_sum;

if(document.order.check_sales_tax.checked)
{
	tmp1 = sum_subtotal * salesTax / 100;
	document.order.salestax_input.value = formatNR(tmp1,2);
}

if(sum_subtotal)
{
	document.order.subtotal_input.value = formatNR(sum_subtotal,2);
}
}


// Function to calculate TOTAL
function f_total()
{
var vTotalProducts = document.order.vTotalProducts.value;
f_subtotal();

tmp = sum_subtotal;	

if(document.order.check_sales_tax.checked)
{
	tmp1 = sum_subtotal * salesTax / 100;
	document.order.salestax_input.value = formatNR(tmp1,2);
	tmp = tmp + tmp1;
}
	
if(sum_subtotal)
{	
str = document.order.salestax_input.value;
if(trim(str)=="" )
{
	document.order.salestax_input.value = "";
}
else
{
	if (!containsOnlyDigits(str)) 
    {
    	document.order.salestax_input.value = ''
   	}
}


idx = 0;
for(i=vTotalShipping - 1; i>=0; i--)
{
	if(tmp >= storageS_qty[i])
	{
		idx = i;
		i=-1;
	}
}
sum_shipping = storageS_val[idx];
document.order.shipping_input.value = sum_shipping;
tmp = tmp + storageS_val[idx];
sum_total = tmp;
document.order.total_input.value = formatNR(sum_total,2);
}
else
{
	document.order.subtotal_input.value = '';
}
}



// Function to print order
function f_print()
{
window.open("order_form_print2.htm","print_win",'width=650,height=500,toolbar=1,scrollbars=1');
}





// Function to display MIVA product and to add to basket
function f_sales_tax()
{
f_subtotal();
	if(document.order.check_sales_tax.checked)
	{
		tmp = sum_subtotal * salesTax / 100;
		document.order.salestax_input.value = formatNR(tmp,2);
	}
	else
	{
		document.order.salestax_input.value = '';	
	}
f_total();
}




// Display price
function formatNR(nr, dec)
{
str = "" + Math.round(eval(nr) * Math.pow(10,dec));
while(str.length < dec)
	str = "0" + str;
decidx = str.length - dec;
return(str.substring(0,decidx) + '.' + str.substring(decidx, str.length));
}

// Trim
function trim(inString) 
{
if(inString == null) return "";
var first;
var last;

for(first = 0; first < inString.length && inString.charAt(first) == ' '; first++);
for(last = inString.length - 1; last > 0 && inString.charAt(last) == ' '; last--);

if(last < first) return "";				
return inString.substring(first, last + 1);
}


// Test number
function containsOnlyDigits(inString) 
{
var theString = trim(inString);
for(i = 0; i < theString.length; i++) 
{
	if(theString.charAt(i) != '0' &&
	theString.charAt(i) != '1' &&
	theString.charAt(i) != '2' &&
	theString.charAt(i) != '3' &&
	theString.charAt(i) != '4' &&
	theString.charAt(i) != '5' &&
	theString.charAt(i) != '6' &&
	theString.charAt(i) != '7' &&
	theString.charAt(i) != '8' &&
	theString.charAt(i) != '\.' &&	
	theString.charAt(i) != '9') 
		return false;
}
return true;
}	 

