// $Id: profiles_scripts.js 4408 2007-12-07 15:41:25Z brook $ 

function switch_profile(profile_id)
{
    fn_redirect(index_script+'?'+target_name+'=profiles&'+mode_name+'=update&profile_id='+profile_id);
}

function create_profile()
{
     fn_redirect(index_script+'?'+target_name+'=profiles&'+mode_name+'=update&'+action_name+'=add_profile');
}

function delete_profile(profile_id)
{
    fn_redirect(index_script+'?'+target_name+'=profiles&'+mode_name+'=update&'+action_name+'=delete_profile&profile_id='+profile_id);
}

function fn_copy_fields(status , object)
{
	var elements = [];
	for (k in field_groups)	{
		// Skip country/state
		if (country_state.B.country_id == k || country_state.B.state_id == k) {
			continue;
		}
		if (status) {
			fn_copy_field(k, field_groups[k]);
		}
		elements[elements.length] = document.getElementById(field_groups[k]);
	}

	// Copy country state manually
	if (country_state.S.state_id && country_state.B.state_id) {
		if (status) {
			var tag_name = document.getElementById(country_state.S.state_id + '_d').tagName;	// get value from same element (checking tag)
			document.getElementById(country_state.S.state_id).value = (tag_name == document.getElementById(country_state.B.state_id + '_d').tagName) ? document.getElementById(country_state.B.state_id).value : document.getElementById(country_state.B.state_id + '_d').value;
		}
		elements[elements.length] = document.getElementById(country_state.S.state_id + '_d');
	}		
	if (country_state.S.country_id && country_state.B.country_id) {
		if (status) {
			document.getElementById(country_state.S.country_id).value = document.getElementById(country_state.B.country_id).value;
		}
		elements[elements.length] = document.getElementById(country_state.S.country_id);
	}
	if (status && country_state.B.state_id) {
		country_state.S.state = document.getElementById(country_state.B.state_id).value;
	}
	
	for(var i = 0; i < elements.length; i++) {
		if (elements[i]) {
			elements[i].disabled = status;
		}
	}

	if (status) {
		fn_rebuild_states('S', false , object);
	}
}

function fn_copy_field(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;
		}
	}
}

function fn_rebuild_states(section, rebuild_shipping, object)
{
	if (typeof(country_state[section]) == 'undefined') {
		return;
	}

	var country_id = country_state[section]['country_id'];
	var elm = country_state[section]['state_id'];

	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 == 'B' && document.getElementById('seqb') && document.getElementById('seqb').checked) ? true : false;
	}
	var i = 0;
	var tag_switched = false;

	if (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 == country_state[section].state) {
				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';
				}
			}
		}
		
		sbox.disabled = false;
		sbox.style.display = '';
		sbox.id = elm;

		inp.disabled = true;
		inp.style.display = 'none';
		inp.id = elm+'_d';
		
		if (typeof(object) !== 'undefined') {
			if (object.required_fields && object.required_fields[elm]) {
				object.required_fields[elm] = 'Y';
				if (document.getElementById('mark_' + elm)) {
					document.getElementById('mark_' + elm).innerHTML = '*';
				}
			}
		}
		
	} else { // Disable states
		sbox.disabled = true;
		sbox.style.display = 'none';
		sbox.id = elm+'_d';

		inp.disabled = false;
		inp.style.display = '';
		inp.id = elm;

		if (typeof(object) !== 'undefined') {
			if (object.required_fields && object.required_fields[elm]) {
				object.required_fields[elm] = 'N';
				if (document.getElementById('mark_' + elm)) {
					document.getElementById('mark_' + elm).innerHTML = '&nbsp;';
				}
			}
		}
	}
	country_state[section].state = (sbox.disabled) ? inp.value : sbox.value;

	if (rebuild_shipping == true) {
		country_state['S'].state = country_state[section].state;
		fn_rebuild_states('S', false, object);
	}
}

function fn_check_if_shipping(elm)
{
	if (typeof(field_groups) == 'undefined') {
		return false;
	}

	for (k in field_groups) {
		if (field_groups[k] == elm) {
			return true;
		}
	}

	return false;
}
