/*---------------------- Order Form Logic ----------------*/

function showBlock(id) { 
  var element = $(id);

  if (element == null) {
    return;
  }

  element.style.display = "block";
}

function initOrderForm() {
  refreshOrderForm();
}

function refreshOrderForm() {
  handleRecipientOptionVisibility();
  handleShippingAddressVisibility();
  handlePaymentMethodVisibility();
  handleVATZeroRatingVisibility();
  handleCD2GOShippingVisibility();
}

function handlePaymentMethodVisibility() {
  var display = document.getElementById('creditCardDiv').style.display;
  var isChecked = false;

  if ( typeof(document.order.PAYMENT_DELIVERY_METHOD) != "undefined" &&
       typeof(document.order.PAYMENT_DELIVERY_METHOD.length) == "undefined" &&
       document.order.PAYMENT_DELIVERY_METHOD.value.substring(0,2) == 'CC'
  ) {
    isChecked = document.order.PAYMENT_DELIVERY_METHOD.checked;   // single value variable
  } else if (typeof(document.order.PAYMENT_DELIVERY_METHOD) != "undefined") {
    for (i=0; i < document.order.PAYMENT_DELIVERY_METHOD.length; i++){
      var radio = document.order.PAYMENT_DELIVERY_METHOD[i];
      if (radio.value.substring(0,2) == 'CC' && radio.checked == true) {
        isChecked = true;
        break;
      }
    }
  }

  if (isChecked && (display == 'none')) {
    new Effect2.BlindDown('creditCardDiv', { fps: 35, duration: 0.7 });
  } else if (!isChecked && (display != 'none')) {
    new Effect2.BlindUp('creditCardDiv', { fps: 35, duration: 0.7 });
  }
}

function handleRecipientOptionVisibility() {
  var showOption = !(document.order.IS_GIFT.value || document.order.BUY_AS_GIFT.checked);

  if (showOption) {
    showBlock('recipientOptionDiv');
  } else {
    hideElement('recipientOptionDiv');
  }
}

function hideShippingPhysicalAddress() {
  hideElement('shippingCompanyTr');
  hideElement('shippingPhoneTr');
  hideElement('shippingStreetTr');
  hideElement('shippingCityTr');
  hideElement('shippingStateTr');
  hideElement('shippingZipTr');
  hideElement('shippingCountryTr');
}

function showShippingPhysicalAddress() {
  showElement('shippingCompanyTr');
  showElement('shippingPhoneTr');
  showElement('shippingStreetTr');
  showElement('shippingCityTr');
  showElement('shippingStateTr');
  showElement('shippingZipTr');
  showElement('shippingCountryTr');
}

// Clears the Scale (used by BlindDown/Up) effect's absolute
// dimensions so the element can be resized properly 
function clearDimensions(elem) {
  elem.style.top = '';
  elem.style.left = '';
  elem.style.height = '';
  elem.style.width = '';
  elem.style.position = 'relative';

}

function handleShippingAddressVisibility() {
  var display = document.getElementById('shippingDiv').style.display;
  var useBillAddress = document.order.SHIP_TO_BILLING_ADDRESS.checked;
  var isGift = document.order.IS_GIFT.value;
  var giftChecked = document.order.BUY_AS_GIFT.checked;
  var showShipping = isGift || giftChecked || !useBillAddress;

  var cdChecked;
  if (document.order.ACCEPT_CD2GO) {
    cdChecked = document.order.ACCEPT_CD2GO.checked;
  }
  else if (document.order.ACCEPT_CUSTOM_CD_SOLUTION) {
    cdChecked = document.order.ACCEPT_CUSTOM_CD_SOLUTION.checked;
  }

  var hasCD = document.order.HAS_CD.value == 1;

  if (isGift || giftChecked) {
    if (hasCD || cdChecked) {
      showShippingPhysicalAddress();
    } else {
      hideShippingPhysicalAddress();
    }

    showElement('shippingGiftNoteTr');
  } else {
    showShippingPhysicalAddress();
    hideElement('shippingGiftNoteTr');
  }

  if (showShipping && display == 'none') {
    /*new Effect2.BlindDown('shippingDiv', { fps: 35,
                                           duration: 0.7,
                                           afterFinish: function() { clearDimensions($('shippingDiv')) }
                                         });*/
    $('shippingDiv').style.display = 'block';
  } else if (!showShipping && display != 'none') {
    //new Effect2.BlindUp('shippingDiv', { fps: 35, duration: 0.7 });
    $('shippingDiv').style.display = 'none';
  }

}

function handleVATZeroRatingVisibility() {
  var country = document.order.COUNTRY.options[document.order.COUNTRY.selectedIndex].value;

  switch(country) {
    case 'GB':
    case 'AT':
    case 'BE':
    case 'CY':
    case 'CZ':
    case 'DK':
    case 'EE':
    case 'FI':
    case 'FR':
    case 'FX':
    case 'DE':
    case 'GR':
    case 'HU':
    case 'IE':
    case 'IT':
    case 'LV':
    case 'LT':
    case 'LU':
    case 'MT':
    case 'NL':
    case 'PL':
    case 'PT':
    case 'SI':
    case 'SK':
    case 'ES':
    case 'SE':
    case 'BG':
    case 'RO':
      showElement('vatRow');
      break;
    default:
      hideElement('vatRow');
      document.order.VAT_NUMBER.value = '';
  }
}

function handleCD2GOShippingVisibility()
{
  var buttons = document.order && document.order.cd2goShippingGroup ? document.order.cd2goShippingGroup : undefined;
  if (!buttons) return;

  var accept_cd2go = document.getElementById('ACCEPT_CD2GO');
  var useCd2goPlus = document.getElementById('useCd2goPlus');

  var is_hidden = accept_cd2go && accept_cd2go.checked && useCd2goPlus && useCd2goPlus.value == 1 ? 0 : 1;

  var j = 0;
  for (var i = 0; i < buttons.length; i++) {
    if (!buttons[i].checked) continue;

    is_hidden ? buttons[i].checked = 0 : j++;
    
    break;
  } 

  if (!j && !is_hidden && buttons.length > 0) buttons[0].checked = ++j;

  var cd2goShippingDiv = document.getElementById('cd2goShippingDiv');
  if (cd2goShippingDiv) cd2goShippingDiv.style.display = j ? 'block' : 'none';
}

/*----------------------- Onload Logic ------------------*/

addEvent(window, "load", initOrderForm);


/* The quantity box/drop-down logic */
function onChangeQty(obj) {

    if (obj) {
      var v = obj[obj.selectedIndex].value;

      var itemID = obj.id;
      itemID = itemID.replace(/^SELECT_QUANTITY_/, "");

      var i = document.getElementById("ITEM_QUANTITY_" + itemID);

      if (v == "other") {
        var div0 = document.getElementById("DIV_SELECT_QUANTITY_" + itemID),
            div1 = document.getElementById("DIV_ITEM_QUANTITY_" + itemID);

        if (div0 && div1) {
          var disp0 = div0.style.display, disp1 = div1.style.display;

          div0.style.display = disp1;
          div1.style.display = disp0;

          obj.selectedIndex--;
        }
      }
      else {
        i.value = v;
      }
    }
}


