/*
$.ajaxSetup({
	contentType: "text/html; charset=UTF-8"
});
$.ajaxSetup({
	'mimeType' : "text/html; charset=UTF-8",
    'beforeSend' : function(xhr) {
        xhr.overrideMimeType('text/html; charset=UTF-8');
    }
});
*/

function calculate_cost() {
	
  if($('#checkout-form').length) {
	
	var calculateTrademark = function(msg) {
		var oamiCheckboxes = $('#checkout-form .oami:checked');
		var oepmCheckboxes = $('#checkout-form .oepm:checked');
		var oamiCount = oamiCheckboxes.length;
		var oepmCount = oepmCheckboxes.length;
		var cost = 0;
		
		if(oamiCount) {
			cost += (Math.ceil(oamiCount / 3.0) * $(oamiCheckboxes[0]).data('price'))
			cost += 400
		}
		/*
		oamiCheckboxes.each(function(index) {
			if(index == 0) {
				cost += $(this).data('price'); 
			} else {
				cost += $(this).data('price2'); 
			}
		});
		*/
		oepmCheckboxes.each(function(index) {
			if(index == 0) {
				cost += $(this).data('price'); 
			} else {
				cost += $(this).data('price2'); 
			}
			cost += 75
		});
		
		total_msg = msg.replace(":count", (oamiCount + oepmCount)).replace(":cost", cost.toFixed(2))
		total_id = 'trademark-totals'

		$('#' + total_id).html(total_msg);
		return cost;
	}
	
	var calculateCorporate = function(msg) {

		var checkboxes = $('#checkout-form .corporate:checked');
		var count = checkboxes.length;
		var cost = 0;
		
		if(count > 0) {
			cost = $('.corporate:first').data('price');
			cost += 20
		}
		total_msg = msg.replace(":count", count).replace(":cost", cost.toFixed(2));
		total_id = 'corporate-totals'
		$('#' + total_id).html(total_msg);
		return cost;
	}
	
	var calculateDomain = function(msg) {
	  var checkboxes = $('#checkout-form .domain:checked');
	  var count = checkboxes.length;
	  var cost = 0;
	  checkboxes.each( function(){ cost += $(this).data('price'); } );
	  total_msg = msg.replace(":count", count).replace(":cost", cost.toFixed(2))
	  total_id = 'domain-totals'
	  $('#' + total_id).html(total_msg);
	  return cost;
	}

	var cost =
		calculateTrademark("Registro en :count clases <span>:cost€</span>") +
		calculateDomain("Registro de :count dom. por 1 año <span>:cost€</span>") +
		calculateCorporate("Registro de :count denominaciones <span>:cost€</span>");
	$('#total').html("Total: <strong>" + cost.toFixed(2) + "€</strong>");
  }
}


$(document).ready(function(){
	/*
  $('#country').change(function() {
	alert("Se ha cambiado el país. Nuevo valor: " + this.value);
  });
  
  $('#locale').change(function() {
	alert("Se ha cambiado el idioma. Nuevo valor: " + this.value);
  });
	*/
  
  // activate placeholders
  $('input[placeholder], textarea[placeholder]').placeholder();
  
  // activate togglers
  $('a[data-toggle]').live('click',function() {
	var id = $(this).data('toggle');
	$("#" + id).slideToggle("fast"); 
  });

  // calculate costs and interactivity on request form
  calculate_cost();
  $('#checkout-form :checkbox').live('change', calculate_cost);
  
  // manage ajax search buttons
  $('[data-ajax-text]').click(function() {
	var $this = $(this);
	if(! $this.hasClass('loading')){
		$this.addClass('loading');
		var text = $('#'+ $this.data('ajax-text')).val();
		var update = $this.data('ajax-update');

		$('#' + update).load( $this.data('ajax-url'), { q : text }, function(){
			calculate_cost();
			$this.removeClass('loading');
		});
		//{} + "?q=" + encodeURIComponent()
	}
  });

	$('.trademark-suggestion').live('click', function(e) {
		e.preventDefault();
		var me = $(this);
		var update = me.data('ajax-update');
		$('#' + update).load( me.attr('href'), function(){
			calculate_cost();
		});
	});
  	var activityAutoComplete = $('.home-search input.activity');
	if(activityAutoComplete.length) {
		var autoCompleteUrl = activityAutoComplete.data('autocomplete-url')
		$('.home-search input.activity').autocomplete({
			//source: '${createLink(action:"activityTextAutocomplete")}',
			source : autoCompleteUrl,
			minLength : 1
		});
	}
	

});
























