// IE bug fix to override the horribly implemented getElementByID method
if (/msie/i.test (navigator.userAgent)) //only override IE
{
  document.nativeGetElementById = document.getElementById;
  document.getElementById = function(id)
  {
    var elem = document.nativeGetElementById(id);
    if(elem)
    {
      //make sure that it is a valid match on id
      if(elem.id == id)
      {
        return elem;
      }
      else
      {
        //otherwise find the correct element
        for(var i=1;i<document.all[id].length;i++)
        {
          if(document.all[id][i].id == id)
          {
            return document.all[id][i];
          }
        }
      }
    }
    return null;
  };
}

// basically, if a country in the countries array is selected, we need the applicable drop-down (select)
// list of states, otherwise we just want a text input field
function changeStateInputType(country_val, address_type) {
    
    // HACK ALERT: this is only here because IE doesn't honor disabled="disabled" for select boxes
    if (country_val.length == 0) {
      $(address_type + '_country').selectedIndex = 0;
      country_val = $(address_type + '_country').options[0].value;
    }
    
    var countries = ["USA", "CAN", "ITA", "NLD"];
    var country_strings = ["_us_", "_canadian_", "_italian_", "_netherland_"]

    var coming_from = null;

    for (var i=0; i < country_strings.length; i++) {
      var hidden_address_id = "hidden" + country_strings[i] + address_type + "_state";
      hidden_field = $(hidden_address_id);
      if (hidden_field == null) {
        coming_from = i;
        break;
      }
    }

    // this is what we will name the current shown element
    var hide_id;
    var hide_name;
    if (coming_from == null) {
      hide_id = "hidden_" + address_type + "_state";
      hide_name = "hidden_" + address_type + "[state]";
    }
    else {
      hide_id = "hidden" + country_strings[coming_from] + address_type + "_state";
      hide_name = "hidden_" + country_strings[coming_from] + address_type + "[state]";
    }

    // id of current shown element
    var shown_id = address_type + "_state";
    var shown_name = address_type + "[state]";

    // now we hide and rename the current shown element
    shown_field = $(shown_id);
    shown_field.style.display = 'none';
    shown_field.setAttribute('id', hide_id);
    shown_field.setAttribute('name', hide_name);

    var going_to = null;
    for (var i=0; i < countries.length; i++) {
      if (countries[i] == country_val) {
        going_to = i;
      }
    }

    // now we show and rename the new shown element
    var hidden_id;
    if (going_to == null) {
      hidden_id = "hidden_" + address_type + "_state";
    }
    else {
      hidden_id = "hidden" + country_strings[going_to] + address_type + "_state";
    }

    hidden_field = $(hidden_id);
    hidden_field.style.display = '';
    hidden_field.setAttribute('id', shown_id);
    hidden_field.setAttribute('name', shown_name);
}

function promoCodeChanged(){
	if ($F('promotional_code').length > 0) {
		$('referred_by').disable();
		$('referred_by').value = '';
	}
	else{
		$('referred_by').enable();
	}
}

function referredByChanged(){
	if ($F('referred_by').length > 0) {
		$('promotional_code').disable();
		$('promotional_code').value = '';
	}
	else{
		$('promotional_code').enable();
	}
}
