function ascendDOMToTag(el, target)	{
	while( el.nodeName.toLowerCase() != target && el.nodeName.toLowerCase() != 'html')
		el = el.parentNode;
	return (el.nodeName.toLowerCase() == 'html') ? null : el;
	}

function ascendDOMToId(el, target)	{
	while( el.id != target && el.nodeName.toLowerCase() != 'html')
		el = el.parentNode;
	return (el.nodeName.toLowerCase() == 'html') ? null : el;
	}

function findEl(e, id, tag)	{
	var el;
	if (window.event && window.event.srcElement)
		el = window.event.srcElement;
	if (e && e.target)
		el = e.target;
	if (!el) 
		return;
	if(id)	{
		el = ascendDOMToId(el, id);
		}
	else	{
		el = ascendDOMToTag(el, tag);
		}
	return el;
	}
	
function cancel(e) {
	if (e && e.preventDefault)
    	e.preventDefault(); // DOM style
  	return false; // IE style
	}

function switchTabs(e) {
	el = findEl(e, null, 'li');
	if(el && el.id != '') {
		li_id = el.id;
		last_score = li_id.lastIndexOf('_');
		base = li_id.substring(0, last_score);
		if(!(new_active = document.getElementById(base)))
			return false;
		var top = document.getElementById('sc_tabs_container');
		var divs = top.getElementsByTagName('div');
		for(d = 0; d < divs.length; d++) {
			if(divs[d].className == 'active_tab_content')
				divs[d].className = 'tab_content';
			}
		new_active.className = 'active_tab_content';
		}
	}
	
function setupTabs(list_ref, div_array) {
	if(!list_ref.getElementsByTagName('li'))
		return false;
	var lis = list_ref.getElementsByTagName('li');
	for(s = 0; s < lis.length; s++) {
		addEvent(lis[s], 'click', switchTabs);
		}
	for(a = 0; a < div_array.length; a++) {
		if(a == 0) {
			div_array[a].className = 'active_tab_content';
			}
		else {
			div_array[a].className = 'tab_content';
			}
		}
	}
	
function shipToBill() {
	var ids_array = new Array('co_billing_name', 'co_billing_address', 'co_billing_city', 'co_billing_state', 'co_billing_zip', 'co_shipping_first', 'co_shipping_last', 'co_shipping_address', 'co_shipping_city', 'co_shipping_state', 'co_shipping_zip', 'co_ship_is_bill');
	
	// check for the elements, not much point if some are missing and we would just throw an error
	for(id in ids_array) {
		if(el = document.getElementById(id)) 
			if(!el)
				return;
		}
		
	var cb = document.getElementById('co_ship_is_bill');
	
	var b_name = document.getElementById('co_billing_name');
	var b_address = document.getElementById('co_billing_address');
	//var b_address2 = document.getElementById('co_billing_address2');
	var b_city = document.getElementById('co_billing_city');
	var b_state = document.getElementById('co_billing_state');
	var b_zip = document.getElementById('co_billing_zip');
	
	
	if(cb && cb.checked) {
		var s_first = document.getElementById('co_shipping_first');
  		var s_last = document.getElementById('co_shipping_last');
  		var s_address = document.getElementById('co_shipping_address');
  	//var s_address2 = document.getElementById('co_shipping_address2');
  		var s_city = document.getElementById('co_shipping_city');
  		var s_state = document.getElementById('co_shipping_state');
  		var s_zip = document.getElementById('co_shipping_zip');	
		
		b_name.value = s_first.value + ' ' + s_last.value;
		b_address.value = s_address.value;
		//b_address2.value = s_address2.value;
		b_city.value = s_city.value;
		b_state.value = s_state.value;
		b_zip.value = s_zip.value;
		}
	else {
		b_name.value = '';
		b_address.value = '';
		//b_address2.value = '';
		b_city.value = '';
		b_state.value = '';
		b_zip.value = '';
		}
	}

// hide shipping is used when the customer elects to ship directly to a registrant's mailing address
function hide_show_shipping(status) {
	ids_to_toggle = new Array('co_shipping_first', 'co_shipping_last', 'co_company', 'co_shipping_address', 'co_shipping_city', 'co_shipping_state', 'co_shipping_zip');
	
	if(document.getElementById('co_ship_to_registrant[0]')) {
		if(status==undefined && document.getElementById('co_ship_to_registrant[0]').checked){
			status = 1;
		}
		hide = (status==1)? false:true;
		for(id in ids_to_toggle) {
			if(el = document.getElementById(ids_to_toggle[id])) {
				if(hide)
					hideElement(el);
				else
					showElement(el);
				if(label = ascendDOMToTag(el, 'label')) { 
					if(hide)
						hideElement(label);
					else
						showElement(label);
					}
				}
			}
		if(hide) {
			if(document.getElementById('co_registrant_zip') && document.getElementById('co_shipping_zip')) {
				var reg_zip = document.getElementById('co_registrant_zip');
				var ship_zip = document.getElementById('co_shipping_zip');
				ship_zip.value = reg_zip.value;
				}
			if(document.getElementById('co_registrant_state') && document.getElementById('co_shipping_state')) {
				var reg_state = document.getElementById('co_registrant_state');
				var ship_state = document.getElementById('co_shipping_state');
				var opts = ship_state.getElementsByTagName('option');
				for(o = 0; o < opts.length; o++) {
					if(opts[o].value == reg_state.value) {
						ship_state.selectedIndex = o;
						}
					}
				}
			
			updateTotals();
			}
		}
	}
	
function checkGiftWrap() {
	if(gw_options = document.getElementById('gw_options')) {
		
		// set hidden field that carries the information to the checkout page
		// ok to use javascript here because these options will only be available
		// when javascript is enabled, otherwise the customer always has the 
		// opportunity to select these options later
		if(sc_gw_type = document.getElementById('sc_gw_type')) {
			sc_gw_type.value = gw_options.value;
			}
		}
	}

function selectShipOption(e) {
	if(ups_options_list = document.getElementById('ups_responses')) {
		ups_options = ups_options_list.getElementsByTagName('li');
		if(e) {
			for(uo = 0; uo < ups_options.length; uo++) {
				ups_options[uo].className = '';
				}	
			var option = findEl(e, null, 'li');
			option.className = 'selected';
			}
		else {
			var found = false;
			for(uo = 0; uo < ups_options.length; uo++) {
				if(ups_options[uo].className == 'selected') {
					option = ups_options[uo];
					found = true;
					}
				}
			if(!found) //if none of the options are selected, don't continue
				return false;
			}
		
		// set hidden field that carries the information to the checkout page
		// ok to use javascript here because these options will only be available
		// when javascript is enabled, otherwise the customer always has the 
		// opportunity to select these options later
		var id = option.id;
		var code = id.substring(id.lastIndexOf('_') + 1);
		if(sc_shipping_type = document.getElementById('sc_shipping_type')) {
			sc_shipping_type.value = code;
			}
		}
	}

function init() {
  	if(!document.getElementById || !document.getElementsByTagName || !document.createElement || !document.createTextNode) {
		return false; }
  	if(s2b = document.getElementById('co_ship_is_bill'))	{
		addEvent(s2b, 'click', shipToBill);
		}
		// TUCKER: commenting this one out and using inline "onclick" event
//	if(ship_to_registrant = document.getElementById('co_ship_to_registrant')) {
//		addEvent(ship_to_registrant, 'click', hide_show_shipping);
//		}
	if(document.getElementById('sc_tabs') && document.getElementById('sc_tabs_container')) {
		var tabs_container = document.getElementById('sc_tabs_container');
		tabs_container.className = '';
		var div_ids = new Array('sc_shipping', 'sc_gift_wrap');
		var div_array = new Array();
		for(d = 0; d < div_ids.length; d++) {
			if(document.getElementById(div_ids[d]))
				div_array[div_array.length] = document.getElementById(div_ids[d]);
			}
		if(div_array.length > 0)
			setupTabs(document.getElementById('sc_tabs'), div_array);
		}
	if(gw_options = document.getElementById('gw_options')) {
		addEvent(gw_options, 'change', checkGiftWrap);
		}
	if(ups_options_list = document.getElementById('ups_responses')) {
		ups_options = ups_options_list.getElementsByTagName('li');
		for(uo = 0; uo < ups_options.length; uo++) {
			addEvent(ups_options[uo], 'click', selectShipOption);
			}
		}
	hide_show_shipping();	
	checkGiftWrap();
	selectShipOption();
	}

window.onload = init;