/*
 * Leaseweb.com
 * @author skl
 */

function updateItem (target, data) {

  jQuery.post(cartUrl + '/update', data, function (response) {
    if(data.property != 'domain')
    {
      jQuery.post(cartUrl + '/updateOverview', null, function (json)
      {
        jQuery('.cart_totalAmount').html(json.totalAmount);
        jQuery('.cart_totalMonthlyAmount').html(json.totalMonthlyAmount);
        jQuery('.cart_subtotalAmount').html(json.subtotalAmount);
        jQuery('.cart_subtotalMonthlyAmount').html(json.subtotalMonthlyAmount);
        jQuery('.cart_vatAmount').html(json.vatAmount);
        jQuery('.cart_vatMonthlyAmount').html(json.vatMonthlyAmount);
        jQuery('.cart_finalAmount').html(json.finalAmount);
        jQuery('.cart_finalMonthlyAmount').html(json.finalMonthlyAmount);
        jQuery('.cart_itemCount').html(json.itemCount);
        jQuery('#numDomains').val(json.domains);
        jQuery('#numWebhost').val(json.webhosting);
        if (json.webhosting == 0 && (json.domains > json.webhosting))
        {
          jQuery('#wrapper_webhost_promo').show();
        }
        else
        {
          jQuery('#wrapper_webhost_promo').hide();
        }

        if (json.itemCount)
        {
          jQuery('#cart_prices_footer').show();
          jQuery('#cart_submit').show();
        }
        else
        {
          jQuery('#cart_prices_footer').hide();
          jQuery('#cart_submit').hide();
        }

        jQuery('.cart_itemCount').html(json.itemCount);

        if (data.property == 'remove')
        {
          jQuery('#cart_item_' + data.pid).remove();
        }

      jQuery('#cart_item_' + data.pid).html(response);

      }, 'json');

    jQuery('#cart_item_' + data.pid).html(response);

    }
  });

}

function displayCustom(pid)
{
  var sel = jQuery("#domain_dropdown_"+pid+" option:selected").val();
  if(sel == "custom")
  {
    domainName = jQuery("#domain_custom_"+pid).val();
    jQuery("#domain_custom_"+pid).css("visibility", "visible");
  }
  else
  {
    domainName = sel;
    jQuery("#domain_custom_"+pid).css("visibility", "hidden");
    jQuery("#domain_custom_"+pid).val("");
  }
  if (domainName && domainName.match(/^[a-zA-Z0-9]{2}.*(\.[a-zA-Z]{2,6}){1}$/))
  {
    saveDomain(domainName, pid);
  }

  return false;
}

function saveDomain(domainName, pid)
{
    var data = {
    property: 'domain',
    value: domainName,
    pid: pid
  }
  
  
  updateItem(jQuery('#cart_item_' + pid), data);
}

jQuery(document).ready(function()
{
  var numWebhost = jQuery('#numWebhost').val();
  var numDomains = jQuery("#numDomains").val();

  if( numWebhost == 0 && (numDomains > numWebhost))
  {
    jQuery('#wrapper_webhost_promo').show();
  }
  else
  {
    jQuery('#wrapper_webhost_promo').hide();
  }
  if (jQuery('input[name=method]:checked').val() != 'debit') {
    jQuery('#payment_debit').slideUp('slow');
  }
  if (jQuery('input[name=method]:checked').val() != 'paypal') {
    jQuery('#payment_paypal').slideUp('slow');
  }

  if (jQuery('.domainSelect > option:selected').val() == 'custom')
  {
    //jQuery('.domainSelect > option:selected').change();
  }

  var lastKeyUp;
  jQuery('input[id^=domain_custom_]').keyup(function(){
    displayCustom(jQuery(this).attr('id').substr(14));
  });

  jQuery('input[id^=domain_]').keyup(function(){
    var domainId = jQuery(this).attr('id');
    var split = domainId.split("_");
    var last = split.length - 1;
    var pid = split[last];
    domainName = jQuery(this).val();
    clearTimeout(lastKeyUp);
    var self = this;
    lastKeyUp = setTimeout(function(){
      if (domainName && domainName.match(/^[a-zA-Z0-9]{2}.*(\.[a-zA-Z]{2,6}){1}$/))
      {
        saveDomain(domainName, pid);
      }
    }, 500);
  });

  // change billing cycle
  jQuery('.switch_billing_cycle').live('click', function () {
    var data = {
      property: 'billing_cycle',
      value: jQuery(this).val(),
      pid: jQuery(this).attr('pid')
    }
    updateItem(jQuery('#cart_item_' + data.pid), data);
  });
  
  // change quantity
  jQuery('.update_quantity').live('click', function () {
    var data = {
      property: 'quantity',
      value: jQuery('#' + jQuery(this).attr('id') + '_value').val(),
      pid: jQuery(this).attr('pid')
    }
    updateItem(jQuery('#cart_item_' + data.pid), data);
    return false;
  });

  // remove from cart
  jQuery('.remove_item').live('click', function (e) {
    var data = {
      property: 'remove',
      pid: jQuery(this).attr('pid')
    }

    if (confirm(message_delete))
    {
      updateItem(jQuery('#cart_item_' + data.pid), data);
    }
    return false;
  });

  // Auto update quantity
  jQuery('.numberSrv').live('keyup', function (e) {
    var charCode = e.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
      // NaN
      return false;
    }

    if (jQuery(this).val())
    {
      var data = {
        property: 'quantity',
        value: jQuery(this).val(),
        pid: jQuery(this).attr('name')
      }
      updateItem(jQuery('#cart_item_' + data.pid), data);
    }
  });
  
  // allow only numbers in quantity field
  jQuery('.numberSrv').live('keydown', function (e) {
    var charCode = e.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
      // NaN
      return false;
    }
  });

  jQuery('input[name=method]').click(function () {
    jQuery('input[name=method]').attr('checked', false);
    jQuery(this).attr('checked', 'checked');
    if (jQuery(this).val() == 'debit') {
      jQuery('#payment_debit').slideDown('slow');
      jQuery('#payment_paypal').slideUp('slow');
    } else if (jQuery(this).val() == 'paypal') {
      jQuery('#payment_debit').slideUp('slow');
      jQuery('#payment_paypal').slideDown('slow');
    } else {
      jQuery('#payment_debit').slideUp('slow');
      jQuery('#payment_paypal').slideUp('slow');
    }
  });

  // submit selected payment type
  jQuery('#payment_type_form').submit(function () {
    var method = jQuery('input[name=method]:checked').val();
    if (!method)
    {
      method = jQuery('input[name=method]').val();
    }
    jQuery('#payment_' + method).submit();
    return false;
  });

  //check if webhosting package
  jQuery("#cart_submit").click(function () {
    var cust_input = jQuery("[name=domain]");
    var invalid = false;

    cust_input.each(function(i){
      var domainId = jQuery(this).attr('id');
      var split = domainId.split("_");
      var last = split.length - 1;
      var pid = split[last];

      if(jQuery(this).val() == "")
      {
        invalid = true;
      }
      else
      {
        var domainName = jQuery(this).val();
      }

      if(jQuery(this).val() == "custom")
      {
        if(jQuery("#domain_custom_"+pid).val() == "")
        {
          invalid = true;
        }
        else
        {
          var domainName = jQuery("#domain_custom_"+pid).val();
        }
      }

      var data = {
        property: 'domain',
        value: domainName,
        pid: pid
      }

     if (!(domainName && domainName.match(/^[a-zA-Z0-9]{2}.*(\.[a-zA-Z]{2,6}){1}$/)))
     {
       alert(error_empty_domain);
       invalid = true;
     }
     else
     {
       updateItem (jQuery('#cart_item_' + pid), data);
     }

    });

    if (invalid)
    {
      return false;
    }
    else
    {
      return true;
    }
  });

});

