/*
	written by nu-design/meshnet
	----------------------------
	11-10-1006 * Version 1.1  - moved shipping to shipping array to allow different shipping costs per item
	
	4-10-2006  *  Version 1.0
	
	calculates order total with tax in texas and processes numbers for decimal display
	Products On Orderpage (with shipping) :
	1) Fire/EMS-FDNY Tribute $17.88 + 4.95 Shipping
	2) Fire/EMS- "Hot Squad" $17.88 + 4.95 Shipping
	
	SUPERVISOR SECURITY SERIES : SUPERVISOR EXCELLENCE : THE SERIES.
	3-9) Part 1 - 8 each listed as a separate item each $250 with $4.95 shipping each
	10) order entire set discounted at $1,500 plus 4.95 shipping for set. 
*/


function nu_calculate(){
	var name_array = new Array('fdny', 'hot_squad', 'se_complete', 'se_1', 'se_2', 'se_3', 'se_4', 'se_5', 'se_6', 'se_7', 'se_8', 'm_sub');
	var product_array = new Array();
	product_array['fdny'] = '17.88';
	product_array['hot_squad'] = '17.88';
	product_array['se_complete'] = '1500';
	product_array['se_1'] = '250';
	product_array['se_2'] = '250';
	product_array['se_3'] = '250';
	product_array['se_4'] = '250';
	product_array['se_5'] = '250';
	product_array['se_6'] = '250';
	product_array['se_7'] = '250';
	product_array['se_8'] = '250';
	product_array['m_sub'] = '1800';
	
	var ship_array = new Array();
	ship_array['fdny'] = 4.95;
	ship_array['hot_squad'] = 4.95;
	ship_array['se_complete'] = 4.95;
	ship_array['se_1'] = 4.95;
	ship_array['se_2'] = 4.95;
	ship_array['se_3'] = 4.95;
	ship_array['se_4'] = 4.95;
	ship_array['se_5'] = 4.95;
	ship_array['se_6'] = 4.95;
	ship_array['se_7'] = 4.95;
	ship_array['se_8'] = 4.95;
	ship_array['m_sub'] = 0;
	
	
	
	
	var f = document['hwct_orderForm'];
	var tax_array = new Array();
	if(f.x_state.options[f.x_state.selectedIndex].value == 'TX'){// 8.25% from http://www.ci.hurst.tx.us/Departments/Finance/Factsheet.asp
		tax_rate = '.0825';
	}else{
		tax_rate = '0';
	}
	
	//var item_cost = 19.95;
	//var ship_cost = 4.95; //moved below to get per item shipping in ver 1.1
	var subt = 0;
	var ship = 0;
	var tax = 0;
	var tot = 0;
	var tot_quantity = 0;
	var items = name_array.length;
	
	for(var i=0; i<items; i++){
		var item_id = name_array[i];
		var item_cost = product_array[item_id];
		var ship_cost = ship_array[item_id];//added in ver 1.1
		
		//var item_tax = tax_array[item_cost];
		var item_formElementName = "quantity['" + item_id + "']";
		//alert(item_formElementName);
		var item_quantity = f[item_formElementName].value;
		var item_total = 0;
		item_total = Number(item_quantity) * Number(item_cost);
		subt = Number(subt) + Number(item_total);
		//alert (item_quantity + ' ' + item_cost + ' ' + item_total + ' ' + subt);
		//break;
		ship = Number(ship) + (Number(item_quantity) * Number(ship_cost));
		tot_quantity = Number(tot_quantity) + Number(item_quantity);
	
	}//end of for

	tax_raw = (subt + ship) * tax_rate;
	tax =  round_decimals(tax_raw, 2)
	tot = dp(subt + ship + tax);
	subt = dp(subt);
	ship = dp(ship);
	
	
	
	/*
		subt = dp(f.order_quantity.value * item_cost);
		ship = dp(f.order_quantity.value * ship_cost);
		tax = dp(f.order_quantity.value * tax_cost);
		tot = dp(eval(eval(subt) + eval(ship) + eval(tax)));
	*/	
		f.order_subtotal.value = subt;
		f.order_shipping.value = ship;
		f.order_tax.value = tax;		
		f.x_amount.value = tot;
		
		f['show_quantity'].value = tot_quantity;
		f['show_total'].value = tot;
		
}//end of nu_calculate



function dp(price){
   string = "" + price;
   number = string.length - string.indexOf('.');
   if (string.indexOf('.') == -1)
      return string + '.00';
   if (number == 1)
      return string + '00';
   if (number == 2)
      return string + '0';
   if (number > 3)
      return string.substring(0,string.length-number+3);
return string;
}

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals);
    var result2 = Math.round(result1);
    var result3 = result2 / Math.pow(10, decimals);
    return result3;
}