// $Id: profiles_scripts.js 6123 2008-10-08 14:37:14Z lexa $

jQuery.profiles = {
	copy_fields : function(status)
	{
		var b_country_id = $('label.cm-country.cm-location-billing').attr('for');
		var b_state_id = $('label.cm-state.cm-location-billing').attr('for');
		var s_country_id = $('label.cm-country.cm-location-shipping').attr('for');
		var s_state_id = $('label.cm-state.cm-location-shipping').attr('for');

		var elements = [];

		for (k in field_groups)	{
			elements[elements.length] = document.getElementById(field_groups[k]);

			// Skip country/state
			if (b_country_id == k || b_state_id == k) {
				continue;
			}
			if (status) {
				this.copy_field(k, field_groups[k]);
			}
		}

		// Copy country state manually
		if (s_state_id && b_state_id) {
			if (status) {
				var tag_name = document.getElementById(s_state_id + '_d').tagName;	// get value from same element (checking tag)
				document.getElementById(s_state_id).value = (tag_name == document.getElementById(b_state_id + '_d').tagName) ? document.getElementById(b_state_id).value : document.getElementById(b_state_id + '_d').value;
			}
			elements[elements.length] = document.getElementById(s_state_id + '_d');
		}
		if (s_country_id && b_country_id) {
			if (status) {
				document.getElementById(s_country_id).value = document.getElementById(b_country_id).value;
			}
			elements[elements.length] = document.getElementById(s_country_id);
		}
		if (status && b_state_id) {
			default_state['shipping'] = document.getElementById(b_state_id).value;
		}

		for(var i = 0; i < elements.length; i++) {
			if (elements[i]) {
				elements[i].disabled = status;
			}
		}

		if (status) {
			this.rebuild_states('shipping', false);
		}
	},

	copy_field : function(src_id, target_id)
	{
		if (document.getElementById('seqb') && document.getElementById('seqb').checked == true && document.getElementById(src_id) && document.getElementById(target_id)) {
			elm = document.getElementById(src_id);
			if (elm.type == 'checkbox' || elm.type == 'radio') {
				document.getElementById(target_id).checked = elm.checked;
			} else {
				document.getElementById(target_id).value = elm.value;
			}
		}
	},

	rebuild_states : function(section, rebuild_shipping)
	{
		var country_id = $('.cm-country.cm-location-' + section).attr('for');
		var elm = $('#' + $('.cm-state.cm-location-' + section).attr('for') + ':visible').attr('id');

		if (typeof(this.classes[elm]) == 'undefined') {
			this.classes[elm] = $('.cm-state.cm-location-' + section).hasClass('cm-required');
		}

		active_elm = document.getElementById(elm);

		if (!active_elm) {
			return;
		}

		var country_code;
		if (document.getElementById(country_id)) {
			country_code = document.getElementById(country_id).value;
		} else {
			country_code = default_country;
		}

		sbox = (active_elm.tagName == 'SELECT') ? active_elm : document.getElementById(elm+'_d');
		inp = (active_elm.tagName == 'SELECT') ? document.getElementById(elm+'_d') : active_elm;

		if ((typeof(rebuild_shipping) == 'undefined') || rebuild_shipping == 'check') {
			rebuild_shipping = (section == 'billing' && document.getElementById('seqb') && document.getElementById('seqb').checked) ? true : false;
		}
		var i = 0;
		var tag_switched = false;

		if (states && states[country_code]) { // Populate selectbox with states
			sbox.options.length = 1;
			for (k in states[country_code]) {
				i++;
				sbox.options[i] = new Option(states[country_code][k],k);
				if (k == default_state[section]) {
					sh_addr = document.getElementById('sa');
					if (sh_addr && sh_addr.style.display == 'none')	{
						sh_addr.style.display = '';
						tag_switched = true;
					}
					sbox.selectedIndex = i;
					sbox.options[i].selected = true;
					if (tag_switched)	{
						sh_addr.style.display = 'none';
					}
				}
			}

			if(sbox.style.display != '') {
				sbox.disabled = false;
			}
			$(sbox).show();
			sbox.id = elm;

			inp.disabled = true;
			$(inp).hide();
			inp.id = elm+'_d';

			// Add required class if needle
			(this.classes[elm] == true) ? $('label[@for=' + elm + ']').addClass('cm-required') : $('label[@for=' + elm + ']').removeClass('cm-required');

		} else { // Disable states
			sbox.disabled = true;
			$(sbox).hide();
			sbox.id = elm + '_d';

			inp.disabled = document.getElementById(country_id).disabled;
			$(inp).show();
			inp.id = elm;

			// Remove required class
			$('label[@for=' + elm + ']').removeClass('cm-required');
		}
		default_state[section] = (sbox.disabled) ? inp.value : sbox.value;

		if (rebuild_shipping == true) {
			default_state['shipping'] = default_state[section];
			this.rebuild_states('shipping', false);
		}
	},

	classes : []
}