
function format_integer(element) {
	
	if (element.value=='')
		return;
	
	if (isNaN(element.value))
		element.value = "0";
	else
		 element.value = parseInt(element.value);

}


	function select_tab(id) {
		item_types = get_item_types();
		item_types.each(function(e){	
			if (id==e) {
				$('items_' + e).show();
				$('tab_' + e).removeClassName('tab-down');
				$('tab_' + e).addClassName('tab-up');				
			} else {
				$('items_' + e).hide();
				$('tab_' + e).removeClassName('tab-up');
				$('tab_' + e).addClassName('tab-down');	
			}
		});
		
	}
	function calculateTotal(type_id) {
		var total = $('total_number_' + type_id).value;
		var inputs = $$('input.item_type_' + type_id);
			inputs.each(function(e){			
			if ((!e.value) || (isNaN(e.value))) {
				x = 0;
			} else {
				x = parseInt(e.value);
			}
			
			if (x > 0) {
				total = total - x;
			}
		});
		show_alert = false;
		if (total < 0) {
			total = 0;
			color = 'pink';
			show_alert=true;
		} else {
			color = '#FFF';
		}
		
		inputs.each(function(e){			
				e.style.background=color;
			});
		
		$('number_remaining_' + type_id).style.background=color;
		$('number_remaining_' + type_id).innerHTML = total;
		
		if (show_alert==true) {
			alert("You've selected too many items.");
		}
		
		return total;
				
	}
function copyShippingAddressToBilling()
{
var tf=document.forms[0];
tf.bill_name.value=tf.ship_name.value;
tf.bill_addr1.value=tf.ship_addr1.value;
tf.bill_city.value=tf.ship_city.value;
tf.bill_state.value=tf.ship_state.value;
tf.bill_zip.value=tf.ship_zip.value;
tf.bill_prefix.value=tf.ship_prefix.value;
tf.bill_suffix.value=tf.ship_suffix.value;
tf.bill_areacode.value=tf.ship_areacode.value;
tf.bill_ext.value=tf.ship_ext.value;
}

function copyBillingAddressToHome()
{
var tf=document.forms[0];
tf.home_addr1.value=tf.bill_addr1.value;
tf.home_city.value=tf.bill_city.value;
tf.home_state.value=tf.bill_state.value;
tf.home_zip.value=tf.bill_zip.value;
tf.home_prefix.value=tf.bill_prefix.value;
tf.home_suffix.value=tf.bill_suffix.value;
tf.home_areacode.value=tf.bill_areacode.value;
tf.home_ext.value=tf.bill_ext.value;
}


function clearBillingAddress()
{
var tf=document.forms[0];
tf.bill_name.value="";
tf.bill_addr1.value="";
tf.bill_city.value="";
tf.bill_state.value="";
tf.bill_zip.value="";
tf.bill_areacode.value="";
tf.bill_prefix.value="";
tf.bill_suffix.value="";
tf.bill_ext.value="";
}

function clearHomeAddress()
{
var tf=document.forms[0];
tf.home_addr1.value="";
tf.home_city.value="";
tf.home_state.value="";
tf.home_zip.value="";
tf.home_areacode.value="";
tf.home_prefix.value="";
tf.home_suffix.value="";
tf.home_ext.value="";
}

var phone_field_length=0;
function TabNext(obj,event,len,next_field) {
	if (event == "down") {
		phone_field_length=obj.value.length;
		}
	else if (event == "up") {
		if (obj.value.length != phone_field_length) {
			phone_field_length=obj.value.length;
			if (phone_field_length == len) {
				next_field.focus();
				}
			}
		}
	}
	