var discountRate = 1;

function updateSelect(url, data, select) {
  var gif = $J('#gif');
  select.empty();
  select.attr("disabled", "disabled");

  $J.ajax({
    url: baselink + url,
    data: data,
    type: 'post',
    dataType: 'json',
    success: function (j) {
      for (i = 0; i < j.length; i++) {
        // required to get around IE bug (http://support.microsoft.com/?scid=kb%3Ben-us%3B276228)
        var o = document.createElement("OPTION");
        o.value = typeof j[i] == 'object' ? j[i]["id"] : j[i];
        o.text = typeof j[i] == 'object' ? j[i]["label"] : j[i];
        select.get(0).options[i] = o;
      }
      if(j.length > 0)
        select.removeAttr("disabled");

      // hand control back to browser for a moment
      setTimeout(function () {
        select
          .find('option:first')
          .attr('selected', 'selected')
          .parent('select')
          .trigger('change');
      }, 0);
      gif.hide();
    },
    error: function (xhr, desc, er) {
      alert("an error occurred");
    }
  });
}

function checkDiscount() {
  var discount = $J('#banner_order_banner_discount_code');
  var data = 'discount_code=' + discount.val();
  var gif = $J('#gif');
  gif.show();

  $J.ajax({
    url: host + '/get-banner-discount',
    data: data,
    type: 'post',
    dataType: 'json',
    success: function (j) {
      discountRate = j.discount;
      updateTotalPrice();
      gif.hide();
      $J('#tick').show();
      showdiscountvalue = 100 -(discountRate * 100);
      $J('#discountconfirmed').text('-' + showdiscountvalue + '%');
    },
    error: function (xhr, desc, er) {
      gif.hide();
      $J('#tick').hide();
      $J('#discountconfirmed').text("Sorry, not a valid discount code");
    }
  });
}

function updatePageDetails() {
  var data = 'landing_page=' + $J('#banner_order_landing_page').val();
  var title = $J('#banner_order_meta_title');
  var description = $J('#banner_order_meta_description');
  var gif = $J('#gif2');
  gif.show();

  $J.ajax({
    url: host + '/update-page-details',
    data: data,
    type: 'post',
    dataType: 'json',
    success: function (j) {
      if(j.title) {
        title.val(j.title);
        description.val(j.description);
      }
      else {
        alert(j.error);
      }
      gif.hide();
      updatePreview();
    },
    error: function (xhr, desc, er) {
      gif.hide();
      alert("Please enter a valid url");
    }
  });

}

function updatePageImages() {
  var data = 'landing_page=' + $J('#banner_order_landing_page').val();

  $J.ajax({
    url: host + '/update-page-images',
    data: data,
    type: 'post',
    dataType: 'json',
    success: function (j) {
    },
    error: function (xhr, desc, er) {
      alert("an error occurred");
    }
  });
}

function updateTotalPrice() {
  var totalprice = $J('#totalPrice');
  var currency = $J('#banner_order_currency_id :selected').text();
  var totalcurrency = $J('#totalCurrency');
  var quantity = parseFloat($J('#banner_order_pricing_quantity_id :selected').text());
  var price = parseFloat($J('#banner_order_banner_price_id :selected').text());

  var total = price * quantity * discountRate;
  if(isNaN(total)) {
    totalprice.text('');
    totalcurrency.text('');
    }
  else {
    totalprice.text(total);
    totalcurrency.text(currency);
    $J('#totalPrice').formatCurrency();
  }
}

function updatePreview() {
  var titlepreview = $J('#title_preview');
  var descriptionpreview = $J('#description_preview');
  var urlpreview = $J('#url_preview');
  var url = $J('#banner_order_landing_page').val();
  var title = $J('#banner_order_meta_title').val();
  var description = $J('#banner_order_meta_description').val();

  titlepreview.text(title);
  descriptionpreview.text(description);
  urlpreview.text(url);
}

$J(function ($) {
  var geotarget = $('#banner_order_geotarget_id');
  var country = $('#banner_order_destination_country_id');
  var administrativeArea = $('#banner_order_destination_administrative_area_id');
  var locality = $('#banner_order_destination_locality_id');
  var businesstype = $('#banner_order_business_type_id');
  var bannerplacement = $('#banner_order_banner_placement_id');
  var bannertype = $('#banner_order_banner_type_id');
  var pricingmodel = $('#banner_order_pricing_model_id');
  var pricingquantity = $('#banner_order_pricing_quantity_id');
  var currency = $('#banner_order_currency_id');
  var bannerprice = $('#banner_order_banner_price_id');
  var gif = $('#gif');
  var gif2 = $('#gif2');
  var size468 = $J('#size468');
  var size300 = $J('#size300');
  var size301 = $J('#size301');
  var size125 = $J('#size125');
  var size100 = $J('#size100');
  var size = $J('#size');
  gif.hide();
  gif2.hide();
  size.hide();
  size100.hide();
  size300.hide();
  size301.hide();
  size125.hide();
  size468.hide();
  $J('#preview').hide();
  $J('#125w125h').hide();
  $J('#300w250h').hide();
  $J('#300w100h').hide();
  $J('#468w60h').hide();
  $J('#tick').hide();
  $J("#noclick").hide();

  var title = $J('#banner_order_meta_title');
  title.keypress(function () {
    updatePreview()
  });

  var description = $J('#banner_order_meta_description');
  description.keypress(function () {
    updatePreview()
  });

  administrativeArea.attr("disabled", "disabled");
  locality.attr("disabled", "disabled");
  bannerplacement.attr("disabled", "disabled");
  bannertype.attr("disabled", "disabled");
  country.attr("disabled", "disabled");
  pricingmodel.attr("disabled", "disabled");
  pricingquantity.attr("disabled", "disabled");
  currency.attr("disabled", "disabled");
  bannerprice.attr("disabled", "disabled");

  updateTotalPrice();

  geotarget.change(function () {
    gif.show();
    var data = 'country_id=' + country.val() +
      '&' + 'geotarget_id=' + geotarget.val() +
      '&' + 'currency_id=' + currency.val() +
      '&' + 'banner_type_id=' + bannertype.val() +
      '&' + 'banner_placement_id=' + bannerplacement.val() +
      '&' + 'pricing_model_id=' + pricingmodel.val() +
      '&' + 'pricing_quantity_id=' + pricingquantity.val();
    updateSelect('/list-banner-prices', data, bannerprice);
  });

  country.change(function () {
    gif.show();
    var data = 'country_id=' + country.val() +
      '&' + 'geotarget_id=' + geotarget.val() +
      '&' + 'currency_id=' + currency.val() +
      '&' + 'banner_type_id=' + bannertype.val() +
      '&' + 'banner_placement_id=' + bannerplacement.val() +
      '&' + 'pricing_model_id=' + pricingmodel.val() +
      '&' + 'pricing_quantity_id=' + pricingquantity.val();
    updateSelect('/list-administrative-areas', data, administrativeArea);
    updateSelect('/list-banner-prices', data, bannerprice);
  });

  administrativeArea.change(function () {
    gif.show();
    var data =  'country_id=' + country.val() +
      '&' + 'administrative_area_id=' + administrativeArea.val() +
      '&' + 'currency_id=' + currency.val() +
      '&' + 'geotarget_id=' + geotarget.val() +
      '&' + 'banner_type_id=' + bannertype.val() +
      '&' + 'banner_placement_id=' + bannerplacement.val() +
      '&' + 'pricing_model_id=' + pricingmodel.val() +
      '&' + 'pricing_quantity_id=' + pricingquantity.val();
    updateSelect('/list-localities', data, locality);
    updateSelect('/list-banner-prices', data, bannerprice);
  });

  locality.change(function () {
    gif.show();
    var data =  'country_id=' + country.val() +
      '&' + 'administrative_area_id=' + administrativeArea.val() +
      '&' + 'locality_id=' + locality.val() +
      '&' + 'geotarget_id=' + geotarget.val() +
      '&' + 'currency_id=' + currency.val() +
      '&' + 'banner_type_id=' + bannertype.val() +
      '&' + 'banner_placement_id=' + bannerplacement.val() +
      '&' + 'pricing_model_id=' + pricingmodel.val() +
      '&' + 'pricing_quantity_id=' + pricingquantity.val();
    updateSelect('/list-banner-prices', data, bannerprice);
  });

  businesstype.change(function () {
    gif.show();
    var data = 'business_type_id=' + businesstype.val();

    if(businesstype.val()) {
      country.removeAttr("disabled");
      currency.removeAttr("disabled");
      updateSelect('/list-banner-placements', data, bannerplacement);
      }
    else {
      administrativeArea.attr("disabled", "disabled");
      locality.attr("disabled", "disabled");
      bannerplacement.attr("disabled", "disabled");
      bannertype.attr("disabled", "disabled");
      country.attr("disabled", "disabled");
      pricingmodel.attr("disabled", "disabled");
      pricingquantity.attr("disabled", "disabled");
      currency.attr("disabled", "disabled");
      bannerprice.attr("disabled", "disabled");
      gif.hide();
      updateTotalPrice();
      alert("Please select your business");
    }
  });

  bannerplacement.change(function () {
    gif.show();
    var data = 'banner_placement_id=' + bannerplacement.val();
    if (bannerplacement.val() == 1) {
        $J('.destination_targeting').show();
      } else if (bannerplacement.val() == 3) {
        $J('.destination_targeting').show();
      } else {
        $J('.destination_targeting').hide();
      }
    updateSelect('/list-banner-types', data, bannertype);
  });

  bannertype.change(function () {
    gif.show();
    var data = 'banner_type_id=' + bannertype.val();
    var image = $J('#banner_order_image_url');
    var size100 = $J('#size100');
    var size468 = $J('#size468');
    updateSelect('/list-pricing-models', data, pricingmodel);
      if (bannertype.val() == 6) {
        $J("#metatoggle").hide("slow");
        image.addClass("required");
        image.show();
        $J('#preview').hide();
        $J('#125w125h').show();
        $J('#300w250h').hide();
        $J('#300w100h').hide();
        $J('#468w60h').hide();
        $J('#preview').css("padding", "0");
        $J('#preview').css("width", "125px");
        $J('#preview').css("height", "125px");
        $J('#uploadlabel').show();
        size468.hide();
        size300.hide();
        size301.hide();
        size125.show();
        size100.hide();
        $J('#updateok').hide();
        $J("#clickok").hide();
        $J("#noclick").show();
      } else if (bannertype.val() == 2){
        $J("#metatoggle").show("slow");
        title.val('Please enter a title');
        description.val('Please enter a description');
        image.removeClass("required");
        image.hide();
        $J('#preview').show();
        $J('#125w125h').hide();
        $J('#sampleimage').hide();
        $J('#300w250h').hide();
        $J('#300w100h').hide();
        $J('#468w60h').hide();
        $J('#preview').css("width", "468px");
        $J('#preview').css("height", "60px");
        $J('.adtext').css("padding-left", "0");
        $J('.adtitle').css("padding-bottom", "0");
        updatePreview();
        $J('#uploadlabel').hide();
        size468.hide();
        size300.hide();
        size301.hide();
        size125.hide();
        size100.hide();
        $J('#updateok').show();
        $J("#clickok").show();
        $J("#noclick").hide();
      } else if (bannertype.val() == 4){
        $J("#metatoggle").hide("slow");
        title.val('Please enter a title');
        description.val('Please enter a description');
        image.addClass("required");
        image.show();
        $J('#preview').hide();
        $J('#125w125h').hide();
        $J('#300w250h').show();
        $J('#300w100h').hide();
        $J('#468w60h').hide();
        $J('#preview').css("width", "300px");
        $J('#preview').css("height", "250px");
        $J('#uploadlabel').show();
        size468.hide();
        size300.show();
        size301.hide();
        size125.hide();
        size100.hide();
        $J('#updateok').hide();
        $J("#clickok").hide();
        $J("#noclick").show();
      } else if (bannertype.val() == 5){
        $J("#metatoggle").hide("slow");
        title.val('Please enter a title');
        description.val('Please enter a description');
        image.addClass("required");
        image.show();
        $J('#preview').hide();
        $J('#125w125h').hide();
        $J('#300w250h').hide();
        $J('#468w60h').hide();
        $J('#300w100h').show();
        $J('#preview').css("width", "300px");
        $J('#preview').css("height", "100px");
        $J('#uploadlabel').show();
        size468.hide();
        size300.hide();
        size301.show();
        size125.hide();
        size100.hide();
        $J('#updateok').hide();
        $J("#clickok").hide();
        $J("#noclick").show();
      } else if (bannertype.val() == 3){
        $J("#metatoggle").hide("slow");
        title.val('Please enter a title');
        description.val('Please enter a description');
        image.addClass("required");
        image.show();
        $J('#preview').hide();
        $J('#125w125h').hide();
        $J('#300w250h').hide();
        $J('#300w100h').hide();
        $J('#468w60h').show();
        $J('#preview').css("width", "468px");
        $J('#preview').css("height", "60px");
        $J('#uploadlabel').show();
        size468.show();
        size300.hide();
        size301.hide();
        size125.hide();
        size100.hide();
        $J('#updateok').hide();
        $J("#clickok").hide();
        $J("#noclick").show();
      } else {
        $J("#metatoggle").show("slow");
        title.val('Please enter a title');
        description.val('Please enter a description');
        image.removeClass("required");
        image.show();
        $J('#preview').show();
        $J('#sampleimage').show();
        $J('#125w125h').hide();
        $J('#300w250h').hide();
        $J('#300w100h').hide();
        $J('#468w60h').hide();
        $J('#preview').css("padding", "7px");
        $J('#preview').css("width", "300px");
        $J('#preview').css("height", "125px");
        $J('.adtext').css("padding-left", "7px");
        $J('.adtitle').css("padding-bottom", "7px");
        updatePreview();
        $J('#uploadlabel').show();
        size468.hide();
        size300.hide();
        size301.hide();
        size125.hide();
        size100.show();
        $J('#updateok').show();
        $J("#clickok").show();
        $J("#noclick").hide();
      }
  });

  pricingmodel.change(function () {
    gif.show();
    var data = 'pricing_model_id=' + pricingmodel.val();
    updateSelect('/list-pricing-quantities', data, pricingquantity);
  });

  pricingquantity.change(function () {
    gif.show();
    var data = 'country_id=' + country.val() +
      '&' + 'administrative_area_id=' + administrativeArea.val() +
      '&' + 'locality_id=' + locality.val() +
      '&' + 'geotarget_id=' + geotarget.val() +
      '&' + 'currency_id=' + currency.val() +
      '&' + 'banner_type_id=' + bannertype.val() +
      '&' + 'banner_placement_id=' + bannerplacement.val() +
      '&' + 'pricing_model_id=' + pricingmodel.val() +
      '&' + 'pricing_quantity_id=' + pricingquantity.val();
    updateSelect('/list-banner-prices', data, bannerprice);
    updateTotalPrice();
  });

  currency.change(function () {
    gif.show();
    var data = 'country_id=' + country.val() +
      '&' + 'administrative_area_id=' + administrativeArea.val() +
      '&' + 'locality_id=' + locality.val() +
      '&' + 'geotarget_id=' + geotarget.val() +
      '&' + 'currency_id=' + currency.val() +
      '&' + 'banner_type_id=' + bannertype.val() +
      '&' + 'banner_placement_id=' + bannerplacement.val() +
      '&' + 'pricing_model_id=' + pricingmodel.val() +
      '&' + 'pricing_quantity_id=' + pricingquantity.val();
    updateSelect('/list-banner-prices', data, bannerprice);
  });

  bannerprice.change(function () {
    updateTotalPrice();
  });

  $J('#banner_discount_code_1').hide();
  $J('#banner_order_discount_code_select_0').change(function() {
    $J("#banner_discount_code_1").toggle();
  });

  $J('textarea.expanding').autogrow({minHeight: 30, lineHeight: 16});
  $J('input[type=text]').simpleWaterMark('watermark');
  $J("#advertiseForm").validate();
  $J(':input[type=text]').textCounting();
  $J(':input[type=textarea]').textCounting();

  $J("#banner_order_start_date").datepicker({
    showOn: 'button',
    showOtherMonths: true,
    buttonImage: '/images/calendar-icon2.png',
    buttonImageOnly: true,
    dateFormat: 'dd/mm/yy',
    minDate: 0
  });

  $J("#tabMenu ul.menu a").corner('top 8px');
  $J("#tabMenu ul.menu .selected").corner('top 8px');
  $J(".corner").corner('bottom right 8px');
});
