var selectedState = "";
var currentCountryCode = "";
var selectedCountry = "";

function subscriptionsInit() {
  if (form = document.subForm) {
    termChanged();
    currentCountryCode = form.country.options[form.country.selectedIndex].value;
    countryChanged();
    paymentMethodChanged();
  }
}

function getTerm() {
  var form = document.subForm;
  if (form.term.length > 1) {
    for (var i=0;i<form.term.length;i++) {
      if (form.term[i].checked) {
        return form.term[i].value; 
      }
    }
  } else {
    return form.term.value;
  }
  return "";
}

function getPaymentMethod() {
  var form = document.subForm;
  for (var i=0;i<form.payment_method.length;i++) {
    if (form.payment_method[i].checked) {
      return form.payment_method[i].value;
    }
  }
  return "";
}

function termChanged() {
  var form = document.subForm;
  var term = getTerm();
  if (bonus_cd = form.bonus_cd) {
    for (var i=0;i<bonus_cd.length;i++) {
      bonus_cd[i].disabled = term != "annual" && term != "biennial";
    }
    if (term != "annual" && term != "biennial") {
      bonus_cd[0].checked = true;
    }
    for (var i=0;i<form.shipping_method.length;i++) {
      form.shipping_method[i].disabled = term != "annual" && term != "biennial";
    }
    if (term != "annual" && term != "biennial") {
      form.shipping_method[0].checked = true;
    }
  }
  toggleAutoRenewBlock(term!="biennial" && getPaymentMethod()=="Credit Card");
}

function countryChanged() {
  form = document.subForm;
  var countryCode;
  for (i=0;i<form.country.length;i++) {
    if (form.country[i].selected) {
      countryCode = form.country[i].value;
    }
  }
  if (currentCountryCode && (span = document.getElementById("state_"+currentCountryCode))) {
    span.style.display='none';
  }
  if (span = document.getElementById("state_"+countryCode)) {
    var state_o = form.state_o.value;
    document.getElementById("state_o").style.display='none';
    span.style.display='inline';
    if (state_o) {
      //attempt to auto select state in drop down
      for (var i=0;i<form["state_"+countryCode].length;i++) {
        if (form["state_"+countryCode][i].text.toLowerCase() == state_o.toLowerCase()) {
          form["state_"+countryCode].selectedIndex = i;
          break;
        }
      }
    }
    selectedState = form["state_"+countryCode].options[form["state_"+countryCode].selectedIndex].value;
    currentCountryCode = countryCode;
  } else {
    document.getElementById("state_o").style.display='inline';
    selectedState = "";
  }
  selectedCountry = countryCode;
}

function stateChanged(state) {
  selectedState = state;
}

function paymentMethodChanged() {
  var form = document.subForm;
  var payment_method = getPaymentMethod();
  form.card_type.disabled = payment_method != "Credit Card";
  form.card_num.disabled = payment_method != "Credit Card";
  form.exp_month.disabled = payment_method != "Credit Card";
  form.exp_year.disabled = payment_method != "Credit Card";
  form.card_code.disabled = payment_method != "Credit Card";
  toggleCCRows(payment_method=="Credit Card");
  toggleAutoRenewBlock(payment_method=="Credit Card" && getTerm() != "biennial");
}

function toggleCCRows(show) {
  var rows = new Array("cardType","cardNum","cardEXP","cardVer");
  for (var i=0;i<rows.length;i++) {
    document.getElementById(rows[i]+"Row").style.display = show ? "" : "none";
  }
}

function toggleAutoRenewBlock(show) {
  document.getElementById('autorenewBlock').style.display = show ? "block" : "none";
}

var detailsWin;
function bonusCDDetails(bonus) {
  var url = "subscribe-bonus-"+bonus+".htm";
  if (!detailsWin || detailsWin.closed) {
    detailsWin = window.open(url,'detailsWin','width=650,height=500,left=200,top=100,toolbar=no,location=no,status=no,resizeable=no,scrollbars=no')
  } else {
    detailsWin.document.location=url;
    detailsWin.focus();
  }
  return false;
}







