// JavaScript Document
var airmail=true;
	function writePItem(a_id, a_class, item_id, item_name, item_price, item_save, c_value, in_class, ip_class, is_class, w_save, is_selected, pn_class, pack_name){ // Write Product Item
		if(is_selected == 1){
			is_selected_text = ' checked="checked"';
		}else{
			is_selected_text = '';
		}
		document.write('<a href="javascript:select_item('+"'"+item_id+"'"+');" id="'+a_id+'" onclick="s_item_a('+"'"+a_id+"'"+')" class="'+a_class+'">');
		document.write('<span class="'+pn_class+'">'+pack_name+'</span><br />');
		document.write('<input type="radio" name="cc" id="'+item_id+'" value="'+c_value+'" onclick="click_prod('+"'"+item_id+"'"+');"'+is_selected_text+' /><span class="regular_text_smaller">SELECT THIS QUANTITY</span><br />');
		document.write('<span class="'+in_class+'">'+item_name+'</span> FOR ONLY <span class="'+ip_class+'">$'+item_price+'</span>');
		if(w_save == 1){
			document.write('<br /><span class="'+is_class+'">'+item_save+'</span>');
		}
		document.write('</a>');
	}

	function writeSItem(shipping_id, shipping_type, shipping_amount, is_checked,it_num){ // Write Shipping Choices
		if(is_checked == 1){
			is_checked_text = ' checked="checked"';
		}else{
			is_checked_text = '';
		}
		document.write('<input type="radio" name="shipping" id="'+it_num+'shipping'+shipping_id+'" value="'+shipping_id+'" onclick="click_ship('+it_num+','+shipping_id+');"'+is_checked_text+' /> <strong onclick="click_ship('+it_num+','+shipping_id+'); document.getElementById('+"'"+it_num+'shipping'+shipping_id+"'"+').checked = true;">'+shipping_type+' $'+shipping_amount+'</strong> ');
	}

	function writeDEFCompute(def_st_value, def_at_value){
		document.write('<br /><br />Sub Total: <input type="text" name="amount_or" id="amount_or" class="text_embeded" readonly="YES" value="'+def_st_value+'" /><br />');
		document.write('Total Amount of Order: <input type="text" name="amount_total" id="amount_total" class="text_embeded" readonly="YES" value="'+def_at_value+'" /><br /><br />');
		document.write('<input type="submit" name="checkout" value="  Checkout  " class="checkout" />');
	}

	function select_item(pvx_id){

		var ctr = 1;

		while(ctr <= 4){
			document.getElementById('item_0'+ctr).checked = false;
			ctr+=1;
		}

		document.getElementById(pvx_id).checked = true;
		pvxValue = "p"+document.getElementById(pvx_id).value;
		document.getElementById('amount_or').value = document.getElementById(pvxValue).value;
		document.getElementById('c').value = document.getElementById(pvx_id).value;
		compute_all();
	}

	function s_item_a(a_id){
		var ctr = 1;

		while(ctr <= 4){
			document.getElementById('pvx_item0'+ctr).style.border = "1px solid #CCC";
			document.getElementById('pvx_item0'+ctr).style.background = '#FFFFFF';
			ctr+=1;
		}

		document.getElementById(a_id).style.border = '1px dashed #333333';
		document.getElementById(a_id).style.background = '#f8f9e8';
	}

	function compute_all(){
		var se_it = document.getElementById('c').value;
		var am_or = parseFloat(document.getElementById('p'+se_it).value);
		
		if(airmail == true){
			
			document.getElementById('amount_total').value = number_format(am_or + 14.95,2,'.','');
			document.getElementById('shipping_type').value = "AIRMAIL";
			document.getElementById('amount_shipping').value = 14.95;

		}
		if(airmail == false){

			document.getElementById('amount_total').value = number_format(am_or + 29.95,2,'.','');
			document.getElementById('shipping_type').value = "FEDEX";
			document.getElementById('amount_shipping').value = 29.95;

		}
		update_total(se_it);

	}

	function number_format(a, b, c, d) {
		
		a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
		e = a + '';
		f = e.split('.');
		
		if (!f[0]) {
			f[0] = '0';
		}
		
		if (!f[1]) {
			f[1] = '';
		}
		
		if (f[1].length < b){
			g = f[1];
				for (i=f[1].length + 1; i <= b; i++){
					g += '0';
				}
			f[1] = g;
		}
		
		if(d != '' && f[0].length > 3){
			h = f[0];
			f[0] = '';

			for(j = 3; j < h.length; j+=3){
				i = h.slice(h.length - j, h.length - j + 3);
				f[0] = d + i +  f[0] + '';
			}

			j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
			f[0] = j + f[0];
		}

		c = (b <= 0) ? '' : c;
		return f[0] + c + f[1];

	}

	function privacy(pageName){
		
		window.open(pageName, 'ProCalisX', 'toolbar=no,location=no,directories=no,status=yes,menub ar=no,scrollbars=1,resizable=yes,copyhistory=yes,width=450,height=500');
		
	}
	
	// Merged Shipping Choice & DEF Compute
	// Still relies on writeSItem but not writeDEFCompute
	function writeNSItem(dv_class,sp_class,def_at_value,it_num){ 
		document.write('<div class="'+dv_class+'"><span class="'+sp_class+'">Shipping Type:');
		writeSItem('0', 'Air Mail', '14.95', 0,it_num);
		writeSItem('1', 'FEDEX', '29.95', 0,it_num);

		document.write('<br />Total: <input type="text" name="amount_total0'+it_num+'" id="amount_total0'+it_num+'" class="text_embeded" readonly="YES" value="'+def_at_value+'" /><br /><br />');
		document.write('<input type="submit" name="checkout" value="  Checkout  " class="checkout" />');
		document.write('</span></div>');
	}
	
	function writeAOr(def_st_value, def_at_value){ //Kludge to make it work with old script
	document.write('<input type="hidden" name="amount_or" id="amount_or" value="'+def_st_value+'" />');
	document.write('<input type="hidden" name="amount_total" id="amount_total" value="'+def_at_value+'" />');
	KShip('0','0.00'); // Airmail
	KShip('1','29.95'); //Fedex
	}
	
	function KShip (shipping_id,shipping_amount){
		document.write('<input type="hidden" name="shipping'+shipping_id+'_price" id="shipping'+shipping_id+'_price" value="'+shipping_amount+'" />');
	}
	
	//Compute total and move selection
	function click_ship(it_num,shipping_id){
	var si;
		if (shipping_id=='0') {
			airmail= true;
		}
		if (shipping_id=='1'){
			airmail=false;
		}
		ai='pvx_item0'+it_num;
		si='item_0'+it_num;
		select_item(si);
		s_item_a(ai);
		compute_all();
	}
	
	function click_prod(item_id){
	var ii;
	select_item(item_id);
	airmail=true;
	ii=item_id.substr(6,1)+'shipping0';
	document.getElementById(ii).checked=true;
}	
	
	function update_total(item_no){
		document.getElementById('amount_total0'+item_no).value=document.getElementById('amount_total').value;
	}