/**
 * Some javascript functions used in forms for Silverback
 * Make sure you include prototype.js!
**/

/**
 * This function toggles state and region form fields depending on if the country selected is domestic or international
 * @param country_selector the id of the country selector dropdown form field
 * @param state_selector the id of the state selector dropdown form field
 * @param state_div the id of the div element that the state dropdown is in
 * @param region_textbox the id of the region textbox form field
 * @param region_div the id of the div element that the region textbox is in
**/

function toggleStateRegion(country_selector, state_selector, state_div, region_textbox, region_div){
  if ($(country_selector).value != 'US' && $(country_selector).value != ""){
    Element.hide(state_div);
    $(state_selector).value = "";
    Element.show(region_div);
  }else{
    Element.hide(region_div);
    $(region_textbox).value = "";
    Element.show(state_div);
  }
}


function toggleStateAndRegion(country_selector, state_label, region_label, state_br, region_br){
    if ($(country_selector).value != 'US' && $(country_selector).value != ""){
        Element.hide(state_label);
        Element.hide(state_br);
        $(state_label).childNodes[1].value = "";
        Element.show(region_label);
        Element.show(region_br);
    }
    else{
        Element.hide(region_label);
        Element.hide(region_br)
        $(region_label).childNodes[1].value = "";
        Element.show(state_label);
        Element.show(state_br);
    }
}

function addActivity(name, select) {
    selOption = $(select).options[$(select).selectedIndex]
    if (selOption.value != 0) {
        option_text = selOption.text;
        name = name + selOption.value;
        form_div = $(select).parentNode.parentNode;
        group_div = $(form_div).parentNode;
        if ($(name)) {
            $(name).select();
            $(name).focus();
        } else {
            addTextField(name, name, group_div, form_div, option_text);
        }
    }
}

function addContact(name, select) {
    selOption = $(select).options[$(select).selectedIndex]
    if (selOption.value != 0) {
        form_div = $(select).parentNode.parentNode;
        group_div = $(form_div).parentNode;
        name = name + "[" + selOption.value + "][]";
        node_id = name + new Date().getTime();
        addTextField(name, node_id, group_div, form_div, selOption.text);
    }
}

function addTextField(name, node_id, group_div, form_div, option_text) {
    label = document.createElement("label");
    em = document.createElement("em");
    em.appendChild(document.createTextNode(option_text));

	input = document.createElement("input");
	input.setAttribute("type", "text");
	input.setAttribute("name", name);
    input.setAttribute("id", node_id);
    small = document.createElement("small");
    a = document.createElement("a");
    a.setAttribute("href", "javascript:;");
    a.setAttribute("onclick", "if(confirm('Are you sure?')){ removeTextField('" + node_id + "')}");
    a.appendChild(document.createTextNode("Remove"));

    label.appendChild(em);
    label.appendChild(input);
    label.appendChild(small);
    small.appendChild(a);

    br = document.createElement("br");

    group_div.insertBefore(label, form_div);
    group_div.insertBefore(br, form_div);
    $(node_id).focus();
}

function removeTextField(node_id) {
  label = $(node_id).parentNode;
  br = label.nextSibling;
  div = label.parentNode;
  div.removeChild(br);
  div.removeChild(label);
}

function addressOnLoad(address_type) {
  toggleStateAndRegion(address_type + '_country',
                       address_type + '_state_label',
                       address_type + '_region_label',
                       address_type + '_state_br',
                       address_type + '_region_br');
  $(address_type + '_country').onchange = function() {
    toggleStateAndRegion(address_type + '_country',
                         address_type + '_state_label',
                         address_type + '_region_label',
                         address_type + '_state_br',
                         address_type + '_region_br');
  }
}

function updateOption2sForOption1s(select_id, merchandise_option_id, selected_id) {
  new Ajax.Updater(select_id, '/checkout/list_package_option2s?merchandise_option_id=' + merchandise_option_id + '&selected_id=' + selected_id, {asynchronous:true});
  return false;
}


