// $Id: profiles_scripts.js 2122 2006-09-18 13:46:25Z zeke $ 

function switch_profile(profile_id)
{
    window.location=index_script+'?'+target_name+'=profiles&'+mode_name+'=update&profile_id='+profile_id;
}

function create_profile()
{
    window.location=index_script+'?'+target_name+'=profiles&'+mode_name+'=update&'+action_name+'=add_profile';
}

function delete_profile(profile_id)
{
    window.location=index_script+'?'+target_name+'=profiles&'+mode_name+'=update&'+action_name+'=delete_profile&profile_id='+profile_id;
}

function fn_copy_fields(status)
{
	if (status == true) {
		for (k in field_groups)	{
			fn_copy_field(k, field_groups[k]);
		}
	}
}

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 = document.getElementById(src_id).checked;
		} else {
			document.getElementById(target_id).value = document.getElementById(src_id).value;
		}
	}
}

function fn_switch_required_field(elm, status)
{

	if (status == 'required') {
		if (required_fields[elm] == 'N') {
			document.getElementById('mark_'+elm).innerHTML = "*";
			required_fields[elm] = 'Y';
		}
	} else {
		if (required_fields[elm] == 'Y') {
			document.getElementById('mark_'+elm).innerHTML = "";
			required_fields[elm] = 'N';
		}
	}
}

function fn_rebuild_states(section, selected_state, rebuild_shipping)
{
	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 = (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 == selected_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';

		//fn_switch_required_field(elm, 'required');
	} else { // Disable states
		sbox.disabled = true;
		sbox.style.display = 'none';
		sbox.id = elm+'_d';

		inp.disabled = false;
		inp.style.display = '';
		inp.id = elm;

		//fn_switch_required_field(elm, 'non-required');
	}

	if (rebuild_shipping == true) {
		fn_rebuild_states('S', selected_state, false);
	}
}

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;
}

