Ext.onReady(function()
{
	if (Ext.get('flash_header'))
	{
		Ext.get('header').addClass('nobackground');
		Ext.get('flash_header').removeClass('noscreen');
	}
	
	/*Ext.select('a.frey').on('click', function(e){
		e.stopEvent();
		var thumbnail = Ext.get(e.target);
		var anchor = thumbnail.parent();
		var image = Ext.get('image').child('img');
		
		if (image)
		{
			image.dom.src = anchor.dom.href;
			image.dom.alt = thumbnail.dom.alt;
			image.dom.width = 476;
			if (anchor.dom.title !== '')
			{
				// vlozime popis fotky
				Ext.get('description').update(anchor.dom.title);
				// nastavime viditelnost
				Ext.get('description').replaceClass('noscreen', 'screen');
			}
			else
			{
				Ext.get('description').update();
				Ext.get('description').replaceClass('screen', 'noscreen');
			}
		}
		
	});*/
});
$(function() {
	/*$("#h_malevil").mouseover(function() {
		$("#d_malevil").show();
	}).mouseout(function() {
		$("#d_malevil").hide();
	});
	$("#h_benatky").mouseover(function() {
		$("#d_benatky").show();
	}).mouseout(function() {
		$("#d_benatky").hide();
	});
	$("#h_mstetice").mouseover(function() {
		$("#d_mstetice").show();
	}).mouseout(function() {
		$("#d_mstetice").hide();
	});
	$("#h_zbraslav").mouseover(function() {
		$("#d_zbraslav").show();
	}).mouseout(function() {
		$("#d_zbraslav").hide();
	});
	$("#h_motol").mouseover(function() {
		$("#d_motol").show();
	}).mouseout(function() {
		$("#d_motol").hide();
	});
	$("#h_hamry").mouseover(function() {
		$("#d_hamry").show();
	}).mouseout(function() {
		$("#d_hamry").hide();
	});
	$("#h_sokolov").mouseover(function() {
		$("#d_sokolov").show();
	}).mouseout(function() {
		$("#d_sokolov").hide();
	});
	$("#h_mlazne").mouseover(function() {
		$("#d_mlazne").show();
	}).mouseout(function() {
		$("#d_mlazne").hide();
	});
	$("#h_plzen").mouseover(function() {
		$("#d_plzen").show();
	}).mouseout(function() {
		$("#d_plzen").hide();
	});
	$("#h_bechyne").mouseover(function() {
		$("#d_bechyne").show();
	}).mouseout(function() {
		$("#d_bechyne").hide();
	});
	$("#h_hluboka").mouseover(function() {
		$("#d_hluboka").show();
	}).mouseout(function() {
		$("#d_hluboka").hide();
	});
	$("#h_krumlov").mouseover(function() {
		$("#d_krumlov").show('blind',{}, 200, "");
	}).mouseout(function() {
		$("#d_krumlov").hide();
	});*/
	
	$("#headersearchtext").click(function() 
	{
		if ($("#headersearchtext").val() == "Najdi hřiště, událost, výsledky...") 
		{
			$("#headersearchtext").val("");
		}
	});
});

var card3FirstClicked = false;
var basic_price = 0;
var year_price = 0;
var cardType = 2; // cislo vychozi karty (musi souhlasit se zaskrtnutym radiobuttonem)

function basicPrice(elementId, price) 
{
	$(document).ready(function() 
	{
		var radio = Ext.get(elementId);
		var price_container = Ext.get('final_price');
    var price_input = Ext.get('final_price_input');
		
		if (radio.dom.checked == true) 
		{
			basic_price = price;
		}
		cardId = elementId.split('card_type_');
		cardType = cardId[1];
		selectedCard = cardType;
		
		updatePrices(cardType, personAge($('#player_birthdate').value));
		$('input[name="order_person[services][1][clubs][]"]').attr('checked', false);
		$('input[id="services_1_club_none_"]').attr('checked', true);
		$(".card1").hide();
		$(".card2").hide();
		$(".card3").hide();
                $(".card4").hide();
		$(".card5").hide();
		$(".card6").hide();
		$(".card" + cardType.toString()).show();
                
                if (cardType == 1) 
		{
                    // skryje všechny resorty kromě mariny, neboť ostatní nejsou povoleny u karty 1
                    for (i = 2; i <= 7; i++){
                        $("#player_required_club_form" + i).hide();
                    }
                }else{
                    //zobrazí všechny resorty
                    for (i = 1; i <= 7; i++){
                        $("#player_required_club_form" + i).show();
                    }
                    
                }
                
		if (cardType == 3) 
		{
			if (document.getElementById('player_birthdate').value == "") 
			{
				price_container.dom.innerHTML = "Musíte nejprve zadat datum narození hráče";
			}
			else if (personAge(document.getElementById('player_birthdate').value) < 4) 
			{
				document.getElementById('player_age').innerHTML = "Neplatné datum narození";
				price_container.dom.innerHTML = "Musíte nejprve zadat správné datum narození hráče";
			}
			else 
			{
				price_container.dom.innerHTML = number_format(""+(price_regular_member_add + basic_price)+"", 0, ",", " ") + " Kč";
				cleanUp();
				dateChange();
			}
			
			$("#lifetimemember").css("display", "block");
			//$("#optional").css("display", "none");
			$("#customer_details").css("display", "block");
			//$("#firm_details").css("display", "none");
		}
		else 
		{
			price_container.dom.innerHTML = number_format(""+(basic_price)+"", 0, ",", " ") + " Kč";
                        price_input.dom.value = number_format(""+(basic_price)+"", 0, ",", "");
			$("#lifetimemember").css("display", "none");
			//$("#optional").css("display", "block");
			$("#clubreg").css("display", "block");
			$("#customer_details").css("display", "block");
			//$("#firm_details").css("display", "none");
			$("#card1").css("display", "none");
			$("#card2").css("display", "none");
			$("#card3").css("display", "none");
                        $("#card4").css("display", "none");
                        $("#card5").css("display", "none");
                        $("#card6").css("display", "none");

		}
		//alert(elementId);
	});
}

function personAge(birthDate) 
{
	//alert(selectedCard);
	var price_container = Ext.get('final_price');
	age = 0;
	today = new Date();
	if (birthDate) {
		birthDay = birthDate.split('-');
	}
	else {
		birthDay = new Array();
	}
	if (birthDay.length == 3) {
		//birth = new Date(birthDay[0], birthDay[1]*1-1, birthDay[2]);
		birth = new Date(birthDay[0], 1, 1);
		age = Math.floor((today.getTime() - birth.getTime()) / (365.25 * 24 * 60 * 60 * 1000));
		//alert('Věk: '+age);
		if (age < 4) {
			document.getElementById('player_age').innerHTML = "Neplatné datum narození";
			if (selectedCard == 3) {
				price_container.dom.innerHTML = "Musíte nejprve zadat správné datum narození hráče";
			}
		}
		else {
			document.getElementById('player_age').innerHTML = age;
		}
	}
	else {
		//document.getElementById('player_age').innerHTML = "Neplatné datum narození";
		$("#player_age").html("Neplatné datum narození");
		if (selectedCard == 3) {
			$("#price_container").html("Musíte nejprve zadat správné datum narození hráče");
		}
	}
	return age;
}


function countPrice(elementId, price){
	//console.log("\n");
	//console.log("countPrice("+elementId+", "+price+")");
	//alert("countPrice");
	var checkbox = Ext.get(elementId);
	var price_container = Ext.get('final_price');
	var price_input = Ext.get('final_price_input');
	
	if (selectedCard == 3) 
	{
		if (document.getElementById('player_birthdate').value == "") 
    {
			price_container.dom.innerHTML = "Musíte nejprve zadat datum narození hráče";
		}
		else if (personAge(document.getElementById('player_birthdate').value) < 4) 
    {
			document.getElementById('player_age').innerHTML = "Neplatné datum narození";
			price_container.dom.innerHTML = "Musíte nejprve zadat správné datum narození hráče";
		}
		else 
    {
			price_container.dom.innerHTML = number_format(""+(price_regular_member_add+basic_price)+"", 0, ",", " ") + " Kč";
			price_input.dom.value = number_format(""+(price_regular_member_add+basic_price)+"", 0, ",", "");
		}
	}
	else {
		price_container.dom.innerHTML = number_format(""+(basic_price)+"", 0, ",", " ") + " Kč";
		price_input.dom.value = number_format(""+(basic_price)+"", 0, ",", "");
	}
	updatePrices(cardType, personAge(document.getElementById('player_birthdate').value));
}

function updatePrices(cardType, age, elementId) 
{
	if (!elementId) 
	{
		elementId = 1;
	}
	if ($("#services_1_club_praha_").attr("checked") == true) {
		elementId = 1;
		price_life = getPrice1_(1);
	}
	if ($("#services_1_club_telc_").attr("checked") == true) {
		elementId = 2;
		price_life = getPrice2_(1);
	}
	if ($("#services_1_club_pisek_").attr("checked") == true) {
		elementId = 3;
		price_life = getPrice3_(1);
	}
	if ($("#services_1_club_usti_").attr("checked") == true) {
		elementId = 4;
		price_life = getPrice3_(1);
	}
	var add = 1;
	cardType = parseInt(cardType);
	
	if (age == 0) {
		add = 1;
		if (cardType == 3) {
			add = 2;
		}
	}
	else if (age <= 18 && cardType == 3) {
		add = 3;
	}
	else if (age > 64 && cardType == 3) {
		add = 4;
	}
	else {
		add = 1;
		if (cardType == 3) {
			add = 2;
		}
	}
	$(document).ready(function() {
		/*document.getElementById('club_price_1').innerHTML = club_price_1[1+add];
		document.getElementById('club_price_2').innerHTML = club_price_2[1+add];
		document.getElementById('club_price_3').innerHTML = club_price_3[1+add];
		document.getElementById('club_price_4').innerHTML = club_price_4[1+add];*/
		//$("#club_price_1_").html(club_price_1_[1+add]);
		//$("#club_price_2_").html(club_price_2_[1+add]);
		//$("#club_price_3_").html(club_price_3_[1+add]);
		//$("#club_price_4_").html(club_price_4_[1+add]);
		/*document.getElementById('club_price_1_').innerHTML = club_price_1_[1+add];
		document.getElementById('club_price_2_').innerHTML = club_price_2_[1+add];
		document.getElementById('club_price_3_').innerHTML = club_price_3_[1+add];
		document.getElementById('club_price_4_').innerHTML = club_price_4_[1+add];*/
		add++;
		$("#club_price_life").html(club_price_life[2*elementId+add*elementId]);
		//document.getElementById('club_price_life').innerHTML = club_price_life[2*elementId+add*elementId];
		//if (!price_life) {
			price_life = club_price_life[2*elementId+add*elementId];
		//}
		//alert(2*elementId+add*elementId);
		//alert(cardType+add);
	});
}


function dateChange() 
{
  if (selectedCard < 3) 
  {
    changePriceYear()  ;
  }
  
  if (selectedCard >= 4 && selectedCard <= 6) 
  {
    changePriceTP(null,null,selectedCard);
  }
  
	if (selectedCard == 3) 
	{

		basic_price = 0;
		var isempty = 0;
		var price_container = Ext.get('final_price');
		var price_input = Ext.get('final_price_input');
		price1_ = getPrice1_();
		price2_ = getPrice2_();
		price3_ = getPrice3_();
		price4_ = getPrice4_();
		price5_ = getPrice5_();
		price6_ = getPrice6_();
		price7_ = getPrice7_();
		
		/*if ($("#services_1_club_praha_").attr("checked") == true) {
			price_life = getPrice1_(1);
		}
		if ($("#services_1_club_telc_").attr("checked") == true) {
			price_life = getPrice2_(1);
		}
		if ($("#services_1_club_pisek_").attr("checked") == true) {
			price_life = getPrice3_(1);
		}
		if ($("#services_1_club_usti_").attr("checked") == true) {
			price_life = getPrice4_(1);
		}*/
		
		if ($('#services_1_club_praha_').attr('checked') == true) {
			countPriceRadio('services_1_club_praha_', price1_, 1);
		}
		else 
		{
			isempty++;
		}
		//alert(firm_optional_services_price);
		if ($('#services_1_club_telc_').attr('checked') == true) {
			countPriceRadio('services_1_club_telc_', price2_, 2);
		}
		else 
		{
			isempty++;
		}
		//alert(firm_optional_services_price);
		if ($('#services_1_club_pisek_').attr('checked') == true) {
			countPriceRadio('services_1_club_pisek_', price3_, 3);
		}
		else 
		{
			isempty++;
		}
		//alert(firm_optional_services_price);
		if ($('#services_1_club_usti_').attr('checked') == true) {
			countPriceRadio('services_1_club_usti_', price4_, 4);
		}
		else 
		{
			isempty++;
		}
		if (isempty == 4) 
		{
			price_container.dom.innerHTML = number_format(""+0+"", 0, ",", " ") + " Kč";
			price_input.dom.value = number_format(""+0+"", 0, ",", "");
		}
		if ($('#services_lifetime').attr('checked') == true) 
		{
			if (price_input.dom.value == "0") 
			{
				countPrice('services_lifetime', price_life);
			}
		}
		else 
		{
			isempty++;
		}
		//alert(firm_optional_services_price);
		
	  for (i = 1; i <= 4; i++)
			if ((ooo = document.getElementById("player_regular_club_" + i.toString())) !== null && ooo.checked)
				ooo.click();
	}
}

/*
 * Spouští se po kliknutí na radiobutton v bloku "Roční členství".
 * Autor: LUVR   Datum: 31.8.2011
 * o - id radiobuttonu
 * cislo - cislo radiobuttonu (1-4), pokud neni definovano, zjisti se samo
 */
function changePriceTP(o, cislo, tp)
{
  // pokud neni definovano cislo radiobuttonu, zjisti se samo
  if (cislo == null)
	{
	  if (document.getElementById("player_tp" + tp.toString() + "_club_1").checked)
		  cislo = 1;
	  if (document.getElementById("player_tp" + tp.toString() + "_club_2").checked)
		  cislo = 2;
		if (document.getElementById("player_tp" + tp.toString() + "_club_3").checked)
		  cislo = 3;
		if (document.getElementById("player_tp" + tp.toString() + "_club_4").checked)
		  cislo = 4;
	}
  
  var player_age = parseInt(document.getElementById("player_age").innerHTML); //alert(player_age);
  var price_container = Ext.get('final_price');
	var price_input = Ext.get('final_price_input');
  
  if (document.getElementById('player_birthdate').value == "") 
  {
    price_container.dom.innerHTML = "Musíte nejprve zadat datum narození hráče";
  }
  else if (isNaN(player_age) || player_age < 4) 
  {
    document.getElementById('player_age').innerHTML = "Neplatné datum narození";
    price_container.dom.innerHTML = "Musíte nejprve zadat správné datum narození hráče";
  }
  else 
  {
    if (player_age < 15)
    {
      //alert("jun");
      
      if (tp == 4)
        price = 3800;
      else
        if (tp == 5)
          price = 3370;
        else
          if (tp == 6)
            price = 2950;
    }
    else 
      price = 9000 - ((tp - 4) * 1000);
    
    price_container.dom.innerHTML = number_format(""+(price)+"", 0, ",", " ") + " Kč";
    price_input.dom.value =         number_format(""+(price)+"", 0, ",", "");
  }
}

/*
 * Spouští se po kliknutí na radiobutton v bloku "Roční členství".
 * Autor: LUVR   Datum: 31.8.2011
 * o - id radiobuttonu
 * cislo - cislo radiobuttonu (1-4), pokud neni definovano, zjisti se samo
 */
function changePriceYear(o, cislo)
{
  // pokud neni definovano cislo radiobuttonu, zjisti se samo
  if (cislo == null)
	{
	  if (document.getElementById("player_required_club_1").checked)
		  cislo = 1;
	  if (document.getElementById("player_required_club_2").checked)
		  cislo = 2;
		if (document.getElementById("player_required_club_3").checked)
		  cislo = 3;
		if (document.getElementById("player_required_club_4").checked)
		  cislo = 4;
	}
  
  var player_age = parseInt(document.getElementById("player_age").innerHTML);
  var price_container = Ext.get('final_price');
	var price_input = Ext.get('final_price_input');
  
  if (document.getElementById('player_birthdate').value == "") 
  {
    price_container.dom.innerHTML = "Musíte nejprve zadat datum narození hráče";
  }
  else if (isNaN(player_age) || player_age < 4) 
  {
    document.getElementById('player_age').innerHTML = "Neplatné datum narození";
    price_container.dom.innerHTML = "Musíte nejprve zadat správné datum narození hráče";
  }
  else 
  {
      if (document.getElementById('card_type_1').checked)
          price = 1990;
    /*if (cislo == 1)
      price = 1990;*/
    else if (player_age < 15)
      price = 8000;
    else 
      price = 19000;
    
    price_container.dom.innerHTML = number_format(""+(price)+"", 0, ",", " ") + " Kč";
    price_input.dom.value =         number_format(""+(price)+"", 0, ",", "");
  }
}

var price_regular_member_add = 7000;

/*
 * Spouští se po kliknutí na radiobutton v bloku "Řádné členství v klubu".
 * Autor: LUVR   Datum: 4.11.2010
 * o - id radiobuttonu
 * cislo - cislo radiobuttonu (1-4), pokud neni definovano, zjisti se samo
 */
function changePriceRegMem(o, cislo)
{
  var cisloByloNull = (cislo == null);

  // pokud neni definovano cislo radiobuttonu, zjisti se samo
  if (cislo == null)
	{
	  if (document.getElementById("player_regular_club_1").checked)
		  cislo = 1;
	  if (document.getElementById("player_regular_club_2").checked)
		  cislo = 2;
		if (document.getElementById("player_regular_club_3").checked)
		  cislo = 3;
		if (document.getElementById("player_regular_club_4").checked)
		  cislo = 4;
	}
	
	// v pripade vyberu prvniho radiobuttonu "Golfový areál Praha - Club Marina", se zaskrtne pouze ten prvni vstupni poplatek
	if (o == "player_regular_club_1")
	  document.getElementById("radioOrderPersonPlayerReqularFee_Regular").checked = true;

  // zmena ceny v textu v bloku "Vstupní poplatek do vybraného klubu"
	if (age > 0 && age <= 18)
	{
		document.getElementById("club_price_1_").innerHTML = club_price_1_[3];
		document.getElementById("club_price_2_").innerHTML = club_price_2_[3];
		document.getElementById("club_price_3_").innerHTML = club_price_3_[3];
		document.getElementById("club_price_4_").innerHTML = club_price_4_[3];
		document.getElementById("club_price_5_").innerHTML = club_price_5_[3];
		document.getElementById("club_price_6_").innerHTML = club_price_6_[3];
		document.getElementById("club_price_7_").innerHTML = club_price_7_[3];

	}
	else if (age > 64)
	{
		document.getElementById("club_price_1_").innerHTML = club_price_1_[4];
		document.getElementById("club_price_2_").innerHTML = club_price_2_[4];
		document.getElementById("club_price_3_").innerHTML = club_price_3_[4];
		document.getElementById("club_price_4_").innerHTML = club_price_4_[4];
		document.getElementById("club_price_5_").innerHTML = club_price_5_[4];
		document.getElementById("club_price_6_").innerHTML = club_price_6_[4];
		document.getElementById("club_price_7_").innerHTML = club_price_7_[4];
	}
	else
	{
		document.getElementById("club_price_1_").innerHTML = club_price_1_[2];
		document.getElementById("club_price_2_").innerHTML = club_price_2_[2];
		document.getElementById("club_price_3_").innerHTML = club_price_3_[2];
		document.getElementById("club_price_4_").innerHTML = club_price_4_[2];
		document.getElementById("club_price_5_").innerHTML = club_price_5_[2];
		document.getElementById("club_price_6_").innerHTML = club_price_6_[2];
		document.getElementById("club_price_7_").innerHTML = club_price_7_[2];
	}
	
  // zmena ceny v textu v bloku "Roční hrací příspěvek pro jedno hřiště"
	if (age > 0 && age <= 18)
	{
	  document.getElementById("span_regmem_price").innerHTML = regular_member_price_junior[cislo];
	}
	else if (age > 64)
	{
	  document.getElementById("span_regmem_price").innerHTML = regular_member_price_senior[cislo];
	}
	else
	{
		document.getElementById("span_regmem_price").innerHTML = regular_member_price_normal[cislo];
		document.getElementById("span_regmem_price_family").innerHTML = regular_member_price_normal_family[cislo];
	}
	
	// pokud je v konfiguraci v cene uvedena -1 u ceny pro rodinu, nebo je to senior ci junior, zneviditelni se ten radiobutton pro rodinu
	document.getElementById("regular_reg_family").style.display = ((regular_member_price_normal_family[cislo] == -1 || age > 64 || age <= 18) ? "none" : "block");
	
	// nastaveni promenne s cenou, ktera se pak pozdeji pouziva k vypoctu celkove ceny
	if (document.getElementById("radioOrderPersonPlayerReqularFee_Regular").checked)
		price_regular_member_add = parseInt(document.getElementById("span_regmem_price").innerHTML);
	if (document.getElementById("radioOrderPersonPlayerRegularFee_Family").checked)
		price_regular_member_add = parseInt(document.getElementById("span_regmem_price_family").innerHTML);
	
  // odškrtnutí všech radiobuttonu v části "Roční hrací poplatek pro jedno hřiště", aby nedošlo k tomu, že by byly zaškrtnuté neviditelné radiobuttony
	if (!cisloByloNull)
	  $("#lifetimemember input[type='radio']").attr("checked", false).hide(); // napřed se skryjí všechny checkboxy
	$("#lifetimemember label").hide(); // skrejou se vsechny labely
	$("#lifetimemember label:first-child").show(); // ale odkreje se prvni label, protoze to je nadpis pro cely blok
	
	// zviditelnění jen jedné příslušné skupiny radiobuttonů, spolu s nim i jeho label, v kazde casti se automaticky oznaci prvni radiobutton
	if (cislo == 1)
	{
            
	  $("#services_1_club_praha_").show().attr("checked", true);
		$("label[for='services_1_club_praha_']").show();
	}
	if (cislo == 2)
	{
	  $("#services_1_club_telc_").show();
		$("#services_1_club_telc2_").show();
		$("label[for='services_1_club_telc_']").show();
		$("label[for='services_1_club_telc2_']").show();
		
		if (!cisloByloNull)
		  $("#services_1_club_telc_").attr("checked", true);
	}
	if (cislo == 3)
	{
	  $("#services_1_club_pisek_").show();
		$("#services_1_club_pisek2_").show();
		$("label[for='services_1_club_pisek_']").show();
		$("label[for='services_1_club_pisek2_']").show();
		
		if (!cisloByloNull)
		  $("#services_1_club_pisek_").attr("checked", true);
	}
	if (cislo == 4)
	{
	  $("#services_1_club_usti_").show();
		$("#services_1_club_usti2_").show();
		$("label[for='services_1_club_usti_']").show();
		$("label[for='services_1_club_usti2_']").show();
		
		if (!cisloByloNull)
		  $("#services_1_club_usti_").attr("checked", true);
	}
		
	// vypocet ceny - klika se skriptem na checkboxy v bloku "Roční hrací poplatek pro jedno hřiště", aby fungoval princip vypoctu, ktery zde byl jiz drive
	if ((ooo = document.getElementById("services_1_club_praha_")) != null && ooo.checked)
	  setTimeout("document.getElementById(\"services_1_club_praha_\").click();", 100);
	if ((ooo = document.getElementById("services_1_club_praha2_")) != null && ooo.checked)
	  ooo.click();
  else if ((ooo = document.getElementById("services_1_club_telc_")) != null && ooo.checked)
	  ooo.click();
  else if ((ooo = document.getElementById("services_1_club_telc2_")) != null && ooo.checked)
	  ooo.click();
  else if ((ooo = document.getElementById("services_1_club_pisek_")) != null && ooo.checked)
	  ooo.click();
  else if ((ooo = document.getElementById("services_1_club_pisek2_")) != null && ooo.checked)
	  ooo.click();
  else if ((ooo = document.getElementById("services_1_club_usti_")) != null && ooo.checked)
	  ooo.click();
	else if ((ooo = document.getElementById("services_1_club_usti2_")) != null && ooo.checked)
	  ooo.click();
	//else
	  //countPriceRadio(o, 0, cislo); // pokud  zadny checkbox nebyl zaskrtnut, zavola se zmena ceny rucne
}

// defaultně má být zaškrtlý první radiobutton v bloku "Řádné členství v klubu:", toto souvisí s výchozí cenou v proměnné price_regular_member_add
//if (window.location.href.match("clenstvi"))
  //$(document).ready(function() {changePriceRegMem("player_regular_club_1", 1);});


/*
 * Původní výpočet ceny po kliku na radiobuttony (ale teď už jsou některé radiobuttony změněné na checkboxy).
 */
function countPriceRadio(elementId, price, number) 
{
	cleanUp();
	//console.log("\n");
	//console.log("countPriceRadio("+elementId+", "+price+", "+number+")");
	//alert("countPrice");
	var checkbox = Ext.get(elementId);
	var price_container = Ext.get('final_price');
	var price_input = Ext.get('final_price_input');
	year_price = price;
	//alert("year_price: " + year_price.toString());
	
	if (selectedCard == 3) 
	{
		if (document.getElementById('player_birthdate').value == "") 
		{
			price_container.dom.innerHTML = "Musíte nejprve zadat datum narození hráče";
		}
		else if (personAge(document.getElementById('player_birthdate').value) < 4) 
		{
			document.getElementById('player_age').innerHTML = "Neplatné datum narození";
			price_container.dom.innerHTML = "Musíte nejprve zadat správné datum narození hráče";
		}
		else 
		{
      if (selectedCard == 3)
      {
        //alert("price_regular_member_add = " + price_regular_member_add.toString() + "\bbasic_price = " + basic_price.toString() + "\nyear_price = " + year_price.toString());
        cena = price_regular_member_add + basic_price + year_price;
        if (cena == 0)
          cena = "-";

        price_container.dom.innerHTML = number_format(""+(cena)+"", 0, ",", " ") + " Kč";
        price_input.dom.value =         number_format(""+(cena)+"", 0, ",", "");
      }
		}
	}
	else 
	{
		price_container.dom.innerHTML = number_format(""+(basic_price)+"", 0, ",", " ") + " Kč";
		price_input.dom.value = number_format(""+(basic_price)+"", 0, ",", "");
	}
	
	//oldpricelife = price_life;
	
	if (typeof cardType != "undefined")
	  updatePrices(cardType, personAge(document.getElementById('player_birthdate').value), number);
	
	if ($("#services_lifetime").attr("checked") == true) 
	{
		countPrice('services_lifetime', price_life);
	}
}


function isNumberKey(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
	else
		return true;
}


function cleanUp() 
{
  // zruseni druheho z dvojice checkboxu v bloku "Roční hrací poplatek pro jedno hřiště"
  /*if ((ooo = document.getElementById("services_1_club_telc_")) != null && ooo.checked)
	  document.getElementById("services_1_club_telc2_").checked = false;
  if ((ooo = document.getElementById("services_1_club_telc2_")) != null && ooo.checked)
	  document.getElementById("services_1_club_telc_").checked = false;
  if ((ooo = document.getElementById("services_1_club_pisek_")) != null && ooo.checked)
	  document.getElementById("services_1_club_pisek2_").checked = false;
  if ((ooo = document.getElementById("services_1_club_pisek2_")) != null && ooo.checked)
	  document.getElementById("services_1_club_pisek_").checked = false;
  if ((ooo = document.getElementById("services_1_club_usti_")) != null && ooo.checked)
	  document.getElementById("services_1_club_usti2_").checked = false;
	if ((ooo = document.getElementById("services_1_club_usti2_")) != null && ooo.checked)
	  document.getElementById("services_1_club_usti2_").checked = false;*/

	/*
	document.getElementById("services_1_club_telc2_").disabled = document.getElementById("services_1_club_telc_").checked;
	document.getElementById("services_1_club_telc_").disabled = document.getElementById("services_1_club_telc2_").checked;
	
	document.getElementById("services_1_club_pisek2_").disabled = document.getElementById("services_1_club_pisek_").checked;
	document.getElementById("services_1_club_pisek_").disabled = document.getElementById("services_1_club_pisek2_").checked;
	
	document.getElementById("services_1_club_usti2_").disabled = document.getElementById("services_1_club_usti_").checked;
	document.getElementById("services_1_club_usti_").disabled = document.getElementById("services_1_club_usti2_").checked;
		*/

	//console.log("\ncleanUp();");
	previousPrice = 0;
	
	var price_container = Ext.get('final_price');
	var price_input = Ext.get('final_price_input');
	price_container.dom.innerHTML = number_format(""+0+"", 0, ",", " ") + " Kč";
	price_input.dom.value = number_format(""+0+"", 0, ",", "");
}



function loadContent(config) 
{
	if(config.autoGetParams && config.autoGetParams == true) 
	{
		var postParams = getParams(Ext.get(config.formId).dom);
	}
	else {	
		var postParams = config.params;
	}
	
	config.preloader = config.preloader || false;
	config.formId = config.formId || '';
	// zobrazime loading
	if (config.preloader) 
	{
		// vytvorime loading container
		Ext.get(config.targetId).createChild(
		{
			tag: 'div',
			id: 'loading'
		});
		Ext.get('loading').setOpacity(.5);
		// nastavime zpozdeni
		config.timeOut = config.timeOut || 1000;
	}
		
	setTimeout(function()
	{
		Ext.Ajax.request(
		{
			url: config.url
			,method: 'POST'
			,success: function(result,request)
			{
				var res = Ext.util.JSON.decode(result.responseText);
				// pokud nastala chyba
				if (res.success == false)
				{
					// odstranime predchozi elementy s chybovyma zpravama z DOMu
					Ext.select('p.message-bad').remove();
					
					// vlozime chybovou zpravu do DOMu
					Ext.DomHelper.insertHtml(
						'afterBegin', 
						Ext.get(config.targetId).dom, 
						'<p class="message-bad">' + res.error +'</p>'
					);
					
					// odebereme loading container
					Ext.get('loading').remove();
				}
				// po uspesnem zpracovani
				else
				{
					// vlozime vysledek vraceny skriptem
					Ext.DomHelper.overwrite(Ext.get(config.targetId).dom, res.data);
					if(config.callback)
					{
						eval(config.callback);
					}
				}
			}
			,failure: function()
			{
				// odstranime predchozi elementy s chybovyma zpravama z DOMu
				Ext.select('p.message-bad').remove();
				
				// vlozime chybovou zpravu do DOMu
				Ext.DomHelper.insertHtml(
					'afterBegin', 
					Ext.get(config.targetId).dom, 
					'<p class="message-bad">Nepodařilo se načíst data</p>'
				);
				
				// odebereme loading container
				Ext.get('loading').remove();
			}
			,params: ''+postParams+''
		});}
		,config.timeOut);
	//return false;
}


function deleteRecord(config)
{
	
	var postParams = config.params;
	
	Ext.Ajax.request(
	{
			url: config.url
			
			,method: 'POST'
			
			,success: function(result,request)
			{
				var res = Ext.util.JSON.decode(result.responseText);
				// pokud nastala chyba
				if (res.success == false)
				{
					Ext.get(config.targetId).dom.innerHTML = '<p class="message-bad">' + res.errors + '</p>' + Ext.get(config.targetId).dom.innerHTML;
				}
				// po uspesnem zpracovani
				else
				{
					if (config.hideElement && config.hideElement == true && config.hideId && config.hideId != '')
					{
						// skryjeme prvek
						Ext.get(config.hideId).fadeOut({remove:true});
					}
				}
			}
			,failure: function()
			{
				Ext.get(config.targetId).dom.innerHTML = '<p class="message-bad">Nepodařilo se načíst data</p>' + Ext.get(config.targetId).dom.innerHTML;
			}
			,params: ''+postParams+''
		});
}

function simple_tooltip(target_items, name)
{
	Ext.select(target_items+'.'+name).on('mouseover', function(e)
	{
		var anchor = Ext.get(e.target);
		var tooltip_id = "tooltip" + anchor.dom.id;
		//var my_tooltip = Ext.get(tooltip_id);
		//alert(my_tooltip.toSource());
                
                var my_tooltip = $("#" + tooltip_id);
                my_tooltip.parent().css("z-index", "999999");
                my_tooltip.fadeIn();
                
		//my_tooltip.applyStyles({display:"block"});
                //$(e.target).parent().css("z-index", "9999").css("filter", "alpha(opacity=100)");
		//alert($('#'+my_tooltip.id).width()); // margin-left: -179px;
	}).on('mouseout', function(e)
	{
		var anchor = Ext.get(e.target);
		var tooltip_id = "tooltip" + anchor.dom.id;
                
                var my_tooltip = $("#" + tooltip_id);
                my_tooltip.parent().css("z-index", "999");
                my_tooltip.fadeOut();
                
		//var my_tooltip = Ext.get(tooltip_id);
		//my_tooltip.applyStyles({display:"none"});
                //$(e.target).parent().css("z-index", "999");
	});
}

function getPosition(obj)
{
  var topValue = 0, leftValue = 0;
  while (obj)
  {
    leftValue += obj.offsetLeft;
    topValue += obj.offsetTop;
    obj = obj.offsetParent;
  }
  finalvalue = leftValue + "," + topValue;
  return finalvalue;
}

function toogleCheckbox(checkboxID)
{
	if(Ext.get(checkboxID).dom.checked)
	{
		Ext.get(checkboxID).dom.checked = false;
	}
	else
	{
		Ext.get(checkboxID).dom.checked = true;
	}
}


// zobrazi/skryje prvek
function toogleDisplay(config)
{
	if (!config.displayId)
	{
		alert('Nezadáno ID prvku, který se má skrýt/zobrazit');
	} 
	else
	{
		// pokud chceme menit ikonu prepinatka
		if (config.toogleIcon && config.toogleIcon == true)
		{
			if (!config.switcherId)
			{
				alert('Nezadáno ID přepínátka');
			}
			else
			{
				var switcher = Ext.get(config.switcherId).dom;
			}
		}
		
		var element = Ext.get(config.displayId).dom;
		
		switch(element.style.display)
		{
			case 'block':
				// skryjeme prvek
				element.style.display = 'none';
				// zmenime prepinatko
				if (config.toogleIcon && config.toogleIcon == true)
				{
					switcher.className = 'expand';
				}
				break;
				
			case 'none':
				element.style.display = 'block';
				// zmenime prepinatko
				if (config.toogleIcon && config.toogleIcon == true)
				{
					switcher.className = 'collapse';
				}
				break;
				
			default:
				element.style.display = 'block';
				// zmenime prepinatko
				if (config.toogleIcon && config.toogleIcon == true)
				{
					switcher.className = 'collapse';
				}
		}
	}
}


// zobrazi/skryje radek tabulky
function toogleRowDisplay(config)
{
	if (!config.displayId)
	{
		alert('Nezadáno ID prvku, který se má skrýt/zobrazit');
	} 
	else
	{
		// pokud chceme menit ikonu prepinatka
		if (config.toogleIcon && config.toogleIcon == true)
		{
			if (!config.switcherId)
			{
				alert('Nezadáno ID přepínátka');
			}
			else
			{
				var switcher = Ext.get(config.switcherId).dom;
			}
		}
		
		var element = Ext.get(config.displayId).dom;
		if (document.all)
		{
			displayValue = 'inline';
		}
		else
		{
			displayValue = 'table-row';
		}
		if (!element.style.display)
		{
			element.style.display = 'none';
		}
		
		switch(element.style.display)
		{
			case 'inline':
			case 'table-row':
				// skryjeme prvek
				element.style.display = 'none';
				// zmenime prepinatko
				if (config.toogleIcon && config.toogleIcon == true)
				{
					switcher.className = 'expand';
				}
				break;
			case 'none':
				element.style.display = displayValue;
				// zmenime prepinatko
				if (config.toogleIcon && config.toogleIcon == true)
				{
					switcher.className = 'collapse';
				}
				break;
			default:
				element.style.display = displayValue;
				// zmenime prepinatko
				if (config.toogleIcon && config.toogleIcon == true)
				{
					switcher.className = 'collapse';
				}
		}
	}
}


// zobrazi kalendar
function showDatePicker(targetID)
{
	
}


// fce sestavi retezec parametru z formularovych poli
function getParams(obj)
{
	var getstr = '';
	var ele = obj.elements;
	for ( i = 0; i < ele.length; i++ ) 
	{
		if ( ele[i].type && ele[i].type.length > 0 ) 
		{
			switch ( ele[i].type ) 
			{ 
				case "text" :
					getstr += ele[i].name + '=' + ele[i].value + '&';
					break;
				case "password" :
					getstr += ele[i].name + '=' + ele[i].value + '&';
					break;
				case "hidden" :
					getstr += ele[i].name + '=' + ele[i].value + '&';
					break;
				case "textarea" :
					getstr += ele[i].name + '=' + ele[i].value + '&';
					break;
				case "radio" :
					if ( ele[i].checked ) 
						getstr += ele[i].name + '=' + ele[i].value + '&';
					break;
				case "checkbox" :
					if ( ele[i].checked )
					{ 
						getstr += ele[i].name + '=' + ele[i].value + '&';
					}	
					break;
				case "select-one" :
					for ( var x = 0 ; x < ele[i].length; x++ ) 
					{
						if ( ele[i].options[x].selected ) 
							getstr += ele[i].name + '=' + ele[i].options[x].value + '&';
					}
					break;
				case "select-multiple" :
					for ( var x =0 ; x < ele[i].length; x++ ) 
					{
						if ( ele[i].options[x].selected ) 
							getstr += ele[i].name + '=' + ele[i].options[x].value + '&';
					}
					break;
			}
		}
	}
    return getstr;
}


/*var targetID;

function BrowseServer(fieldID)
{
	targetID = fieldID || 'xFilePath';
	// You can use the "CKFinder" class to render CKFinder in a page:
    //var finder = new CKFinder() ;
    //finder.BasePath = '/js/fckeditor/editor/filemanager/browser/ckfinder/' ;    // The path for the installation of CKFinder (default = "/ckfinder/").
    //finder.SelectFunction = SetFileField ;
    //finder.Popup() ;
    
    // It can also be done in a single line, calling the "static"
    // Popup( basePath, width, height, selectFunction ) function:
    CKFinder.popup('/libs/ckfinder/', null, null, SetFileField) ;
}*/

function BrowseServer(field)
{
  window.KCFinder = {};
  window.KCFinder.callBack = function(url) 
  {
    window.KCFinder = null;
    $(field).val(url);
  };
  window.open('/libs/kcfinder/browse.php?type=images&lang=cs', 'kcfinder');
}


// This is a sample function which is called when a file is selected in CKFinder.
function SetFileField(fileUrl)
{
	Ext.get(targetID).dom.value = fileUrl ;
}


// formatovani cisel
function number_format(a, b, c, d) 
{
	// number_format(number, decimals, comma, formatSeparator)
	var k = "";
	if(a.indexOf("-") == 0) {
		k = "-";
		a = a * -1;
	}
	a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
	var e = a + "";
	var f = e.split(".");
	if(!f[0]) f[0] = "0";
	if(!f[1]) f[1] = "";
	if(f[1].length < b){
		var g = f[1];
		for(var i = f[1].length + 1; i <= b; i++) {
			g += "0";
		}
		f[1] = g;
	}
	if(d != "" && f[0].length > 3) {
		var h = f[0];
		f[0] = "";
		for(var j = 3; j < h.length; j += 3) {
			i = h.slice(h.length - j, h.length - j + 3);
			f[0] = d + i +  f[0] + "";
		}
		j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
		f[0] = j + f[0];
	}
	c = (b <= 0) ? "" : c;
	return k + f[0] + c + f[1];
}
