/*
************************************************************
- THE STUFF YOU PROBABLY NEED TO CHANGE
- IMPORTANT: Uses teaCommerce_Advanced.js functionality
- This is the simple stuff
************************************************************
*/
/*
When something goes completely wrong on the server
this is where you will know.
*/
function teaCommerceError(error) {
  //alert("Cart Error: \n" + error.responseText);
}
/*
When the minicart needs to be updated this method is called.
You get the mini cart as a jQuery object and the order.
Here you update the mini carts html or info.
*/
function updateMiniCartUI(miniCart, order) {
  miniCart.find("#miniCartItemPrice").text(order.TotalPriceWithoutFeesFormatted);
  miniCart.find("#miniCartItemCount").text(order.TotalQuantity);
}
/*
Will update one product element.
We only do this because the variants have different stock
and prices than the main product.
*/
function updateProduct(productEle) {
  var variant = productEle.find('.productVariants select').val(),
      chosenProductId = variant ? variant : productEle.attr('productid'),
      xsltFile = productEle.parent().hasClass('productList') ? 'productList-AjaxProduct.xslt' : 'product.xslt',
      htmlFromServer = TeaCommerce.invokeXSLT(xsltFile, chosenProductId, false);
  productEle.before(htmlFromServer).remove();
}
/*
This is a light weight way to update the total amounts
of the cart. This way we do not have to update the entire UI html
*/
function updateCartUI(order) {
  //Update the cart content
  jQuery('#totalExVAT .money').text(order.TotalPriceWithoutFeesWithoutVATFormatted);
  jQuery('#totalShipping .money').text(order.Shipping.FeeFormatted);
  jQuery('#totalPayment .money').text(order.Payment.FeeFormatted);
  jQuery('#totalWithoutVAT .money').text(order.TotalPriceWithoutVATFormatted);
  jQuery('#totalVAT .money').text(order.TotalVATWithoutFeesFormatted);
  jQuery('#totalWithVAT .money').text(order.TotalPriceWithoutFeesFormatted);
  jQuery('#orderQuantity').text(order.TotalQuantity);
}
/*
Removes one orderline from the UI
*/
function removeOrderLineFromUI(orderlineEle) {
  /*
  We just remove the orderline tr element
  You could instead do it with a nice animation
  */
  orderlineEle.remove();
}
/*
Updates one orderline in the UI
*/
function updateOrderLineInUI(orderlineEle, orderLine) {
  if (orderLine.Quantity > 0) {
    //The orderline has changed and we need to update the UI
    orderlineEle.find('.productQuantity').val(orderLine.Quantity);
    orderlineEle.find('.productUnitPrice').text(orderLine.UnitPriceFormatted);
    orderlineEle.find('.productTotalPrice').text(orderLine.TotalPriceFormatted);
  } else {
    //The orderline has been removed and we need to do the same in the UI
    removeOrderLineFromUI(orderlineEle);
  }
}

/*
CLICK ON STEP 2 NEXT: The next step event on step 2 of the cart
*/
function sendCustomerInformation() {
  var paymentInformation = jQuery("#paymentInformation"), 
      shippingInformation = jQuery("#shippingInformation"),
      country = paymentInformation.find("#country option:selected");

  /*
  We fetch the information from the form and creates
  an object that can be sent to the server as a form
  POST submit
  */
  var formObj = {
    company: paymentInformation.find("#company").val(),
    firstName: paymentInformation.find("#firstName").val(),
    lastName: paymentInformation.find("#lastName").val(),
    city: paymentInformation.find("#city").val(),
    zipCode: paymentInformation.find("#zipCode").val(),
    streetAddress: paymentInformation.find("#streetAddress").val(),
    country: country.text(),
    ISOCountryCode: country.attr("ISOCountryCode"),
    telephone: paymentInformation.find("#telephone").val(),
    email: paymentInformation.find("#email").val(),
    comments: jQuery("#comments").val(),
    shipping_firstName: shippingInformation.find("#shippingFirstName").val(),
    shipping_lastName: shippingInformation.find("#shippingLastName").val(),
    shipping_streetAddress: shippingInformation.find("#shippingStreetAddress").val(),
    shipping_zipCode: shippingInformation.find("#shippingZipCode").val(),
    shipping_city: shippingInformation.find("#shippingCity").val(),
    shipping_ISOCountryCode: country.attr("ISOCountryCode")
  };

  //Simple validation
  var pageValidateText = '';
  if (formObj.firstName === '') {
    pageValidateText += '\nFornavn';
  }
  if (formObj.lastName === '') {
    pageValidateText += '\nEfternavn';
  }
  if (formObj.city === '') {
    pageValidateText += '\nBy';
  }
  if (formObj.zipCode === '') {
    pageValidateText += '\nPostnr.';
  }
  if (formObj.streetAddress === '') {
    pageValidateText += '\nAdresse';
  }
  if (formObj.telephone === '') {
    pageValidateText += '\nTelefon';
  }
  if (formObj.email === '') {
    pageValidateText += '\nE-mail';
  }

  if (pageValidateText != '') {
    //The form does not validate and we pop up the result
    pageValidateText = 'Husk at udfylde' + pageValidateText;
    alert(pageValidateText);
    return false; //Return false and the browser will not send the user to the next step
  }
  /*
  Update the country from the dropdown value
  */
  TeaCommerce.setCurrentCountry(paymentInformation.find('#country').val(), { async: false });
  /*
  The properties is sent to the server with a syncronous call
  This way we lock the UI and can redirect the user.
  */
  TeaCommerce.updateOrderProperties(formObj, { async: false });
  return true; //Return true and the browser will send the user to the next step
}


jQuery('.stepProgress03 #paymentInformation input').live('click', function(){
  if(this.id === 'payment3'){
    jQuery('#shipping2').click();
  }
});

jQuery('.stepProgress03 #shippingInformation input').live('click', function(){
  var jQEle = jQuery(this);
  if(this.id === 'shipping1'){
    if(jQuery('#payment3')[0].checked){
      alert('Skal du have leveret med Post Danmark skal du vælge en anden betalingsmetode');
      jQuery('#shipping2').click();
      return false;
    }
  }
});

