  $(document).ready(function() {
 	$("form.niceForm").each(function() {
		$(this).find("input[type=submit]").hide();
		$(this).append("<img src='/images/submit_button.gif' class='submitButton' alt='submit'>");
		$(this).find("img.submitButton").click(function(){
			$(this).parent().submit();
		});
		if($(this).attr('id') == "login"){
			$(this).find("input[type=text],input[type=password]").each(function(){
				$(this).attr('defaultValue',$(this).value);
				$(this).click(function(){
					if($(this).attr('defaultValue') == $(this).attr('value')){
						$(this).attr('value',"");
					}
				});
			});
		}
	});
 });

 $(document).ready(function() {
	$("form.niceForm input#password").live('keypress', function (e) {
    if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
    	$("form.niceForm").submit();
    }
	});
	$("form.niceForm input#loginName").live('keypress', function (e) {
    if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
    	$("form.niceForm").submit();
    }
	});
 });

/* pro search blok */
 $(document).ready(function() {
 	$("form.niceFormSearch").each(function() {
		$(this).find("input[type=submit]").hide();
		$(this).append("<img src='/images/submit_button_search.gif' class='submitButton' alt='submit'>");
		$(this).find("img.submitButton").click(function(){
			$(this).parent().submit();
		});
	});
 });


/*	$("form#signup").each(function(){
		$(this).find("input,select").each(function(){
			$(this).blur(function(){

				var url = "http://ignum-dev-kamil.cz/webcontrol/signup/ajax/?";
				var name = $(this).attr('name');
				var val = $(this).attr('value');

				if(val == 'undefined'){
					var val = "";
				}
				var obj = new Object();
				eval("obj."+name+" = '"+val+"';");

				$.getJSON(url,obj,function(json){
					$("#error"+name).remove();
					if(json != 'true'){
						$.each(json[name],function(k,v){
							$("#"+name).after("<p id='error"+name+"'>" + v + "</p>");
						});
					}
				});
			});
		});
	});*/

$(document).ready(function(){
	var type = "showme"
	$("."+type).each(function(){
		var blockID = type+"-"+$(this).attr('id');

		$(this).click(function(){
			if($("div#"+blockID).is(":hidden")){
				$("div#"+blockID).slideDown('fast');
			}else{
				$("div#"+blockID).slideUp('fast');
			}
		});

		$("div#"+blockID).hide();
	});
});

$(document).ready(function(){
	var type = "enableme";
	$("."+type).each(function(){
		var blockClass = type + "-" + $(this).attr('id');
		$(this).click(function(){
			$("input."+blockClass+",select."+blockClass).each(function(){
				if($(this).parent().is(':checked')){
					$(this).attr('disabled','');
				}else{
					$(this).attr('disabled','disable');
				}
			});
		});
		$("input."+blockClass).attr('disabled','disable');
	});
});

$(document).ready(function(){
	var typeClass = 'date-calendar';
	$("input."+typeClass).each(function(){
		Date.firstDayOfWeek = 1;
		Date.format = 'yyyy-mm-dd';
		$(this).datePicker({clickInput:true});
	});
});

$(document).ready(function(){
	var type = 'toggler';
	$("."+type).each(function(){
		var i = 0;
		$(this).children().each(function(){
		/*	$(this).has('tr'){
			}*/
		});
	});
});

$(document).ready(function(){
	var type = 'select_del_block';
	$("#"+type).each(function(){
		if(true == $("#antispam_delete_emails-no").attr("checked")){
			$(this).hide();
		}
	});
});

// Voucher
/*
$(document).ready(function() {
	var handler = function() {
		$input = $(this);
		var coupon = $.trim($input.val());
		$input.val(coupon);

		if (coupon.length == 6) {
			$.getJSON("/webcontrol/orderhd/ajax_voucher/", {hash:coupon}, function(data) {
				if (data) {
					$.each(data, function(key, value) {
						if (key == 'hash_ok' && value == '1') {
							$input.unbind('keyup');
							$input.attr("readonly", "readonly");

							$("input#domain_name").attr("readonly", "readonly");
							$("#order-form>input[name='stayOnStep1']").val('1');
							$("#order-form>input[name='voucherValidated']").val('1');
							$("#order-form").submit();

							// Disable submit
							$("#order-form").bind("submit", function() { return false; });
						}
					});
				}
			});
		}
	};
	$('#couponCodeOrder').bind('keyup', handler);

});
*/

function setAntispamDelVisibility(){
	if(true == $("#antispam_delete_emails-yes").attr("checked")){
		$("#select_del_block").show();
	}
	else{
		$("#select_del_block").hide();
	}
}


function showSegment(el)
{
	var prefix = $(el).attr('value');

	$("div.segment").each(function(){
		if($(this).attr('id') == prefix + "Segment"){
			$(this).show('fast');/*css({display:"block"});*/
		}else{
			$(this).hide('fast');/*css({display:"none"});*/
		}
	});
	return;
}

/* Disable item */
function disableItem(elmDisableID,elm)
{
	if(false == $("#"+elmDisableID).is(":disabled") && $(elm).is(":checked")){
		$("#"+elmDisableID).attr('disabled','disable');
	}else{
		$("#"+elmDisableID).attr('disabled','');
	}
}

/* Hide item */
function hide(elementID)
{
	$("#"+elementID).hide();
}

function disableInput(elObj)
{
	var name = $(elObj).attr('id');
	var actualId = name.replace(/Radio$/i,"");

	$("input#"+name).attr('checked','checked');

	$("input.owner,select.owner").each(function(){
		if($(this).attr('type') != "submit"){
			if($(this).attr('id') != actualId){
					$(this).attr('disabled','disabled');
			}else{
				$(this).attr('disabled','');
			}
		}
	});
	return;
}

function disableSelectOptions(oElm,idDisElm)
{
	var state = false;
	$("#"+idDisElm).children().each(function(){
		if(Number($(this).val()) <= Number($(oElm).val())){
			$(this).attr('disabled','disable');
		}else{
			if(state == false){
				$(this).parent().val($(this).val());
				state = true;
			}
			$(this).attr('disabled','');
		}
	});

}

function showBlock(elm)
{
	var prefix = 'show-';
	var elmID = $(elm).attr('id');

	if($("div."+prefix+elmID).is(":hidden")){
		$("div."+prefix+elmID).show();
	}else{
		$("div."+prefix+elmID).hide();
	}
}


function showLoginBox(elm){
	alert('kvak');

	$(elm).is(':checked')(function(){

		var url = "http://ignum-dev-kamil.cz/webcontrol/login/ajax/?";
		var name = $(this).attr('name');
		var val = $(this).attr('value');

		if(val == 'undefined'){
			var val = "";
		}

		var obj = new Object();
		eval("obj."+name+" = '"+val+"';");

		$.getJSON(url,obj,function(json){
			$("#error"+name).remove();
			if(json != 'true'){
				$.each(json[name],function(k,v){
					$("#"+name).after("<p id='error"+name+"'>" + v + "</p>");
				});
			}
		});
	});
}

function changeOS(value){
	$("#os").val(value);
}

function changeFormWH1(ctype){
	clearWH1Values(ctype);
	getWH1Values(ctype, false);
}

function clearWH1Values(ctype){
	switch(ctype){
		case 'product_group':
			$("#product").html("");
		case 'product':
			$("#h_product_name").html("-");
			$("#period").html("");
			var os = $("#os").val();
			$("#os").val("");
			if(os == "VPSL" || os == "VDSW"){
				$("#os_type").html("");
				$("#os_type").attr("disabled", "disabled");
			}
			$("#scripting-L").removeAttr("checked");
			$("#scripting-W").removeAttr("checked");
			$("#scripting-L").attr("disabled", "disabled");
			$("#scripting-W").attr("disabled", "disabled");
			$("#product_recap").html("");
			$("#submitButton1").attr("disabled", "disabled");
		case 'period':
			$("#h_payment_period").html("-");
			$("#h_price_month").html("-");
			$("#h_price_all").html("-");
			$("#h_discount_for_payment_period").html("-");
			break;
		default:
			break;
	}
}

function getWH1Values(ctype, bAjax){
	if(true == bAjax){
		var productID = $("#product").val();
		var productGroupID = $("#product_group").val();
		switch(ctype){
			case 'product_group':
				$.getJSON("/webcontrol/orderhd/ajax/",{type:'product_group_change', productGroup:productGroupID}, function(data){
					if(data){
						setWH1Values(data, ctype);
					}
				});
				break;
			case 'product':
				$.getJSON("/webcontrol/orderhd/ajax/",{type:'product_change', product:productID}, function(data){
					if(data){
						setWH1Values(data, ctype);
					}
				});
				break;
			case 'period':
				var periodID = $("#period").val();
				$.getJSON("/webcontrol/orderhd/ajax/",{type:'period_change', product:productID, period:periodID}, function(data){
					if(data){
						setWH1Values(data, ctype);
					}
				});
				break;
			default:
				break;
		}
	}
	else{ // cteme z pripraveneho pole
		data = {}; // zalozime objekt, ktery postupne plnime ve switchi
		var productGroupID = $("#product_group").val();
		var productID = $("#product").val();
		var periodID = $("#period").val();
		switch(ctype){
			case 'product_group':
				data.product = pdata[productGroupID][0].product;
				productID = pdata[productGroupID][0].product_selected
			case 'product':
				data.period = pdata[productGroupID][productID][0].period;
				data.platform = pdata[productGroupID][productID][0].platform;
				if(data.platform[0] == 'VPSL' || data.platform[0] == 'VDSW'){
					data.os_type = pdata[productGroupID][productID][0].os_type;
				}
				data.product_name = pdata[productGroupID][productID][0].product_name;
				periodID = pdata[productGroupID][productID][0].period_selected;
			case 'period':
				data.payment_period = pdata[productGroupID][productID][periodID].payment_period;
				data.price_month = pdata[productGroupID][productID][periodID].price_month;
				data.price_all = pdata[productGroupID][productID][periodID].price_all;
				data.discount_for_payment_period = pdata[productGroupID][productID][periodID].discount_for_payment_period;
				break;
			default:
				break;
		}
		setWH1Values(data, ctype);
	}
}

function setWH1Values(data, ctype){
	switch(ctype){
		case 'product_group':
			$("#product").html(data.product);
		case 'product':
			$("#h_product_name").html(data.product_name);
			$("#period").html(data.period);
			$("#product_recap").html(data.product_recap);
			if(data.platform.length >= 2){
				$("#scripting-L").removeAttr("disabled");
				$("#scripting-W").removeAttr("disabled");
				$("#scripting-" + data.platform[0]).attr("checked", "checked");
				$("#os").val(data.platform[0]);
				$("#scripting_radio").show();
			}
			else{
				$("#scripting_radio").hide();
				$("#os").val(data.platform[0]);
			}
			if(data.platform[0] == 'VPSL' || data.platform[0] == 'VDSW'){
				$("#os_type").html(data.os_type);
				$("#os_type").removeAttr("disabled");
			}
		case 'period':
			$("#h_payment_period").html(data.payment_period);
			$("#h_price_month").html(data.price_month);
			$("#h_price_all").html(data.price_all);
			$("#h_discount_for_payment_period").html(data.discount_for_payment_period);
			break;
		default:
			break;
	}
	$("#submitButton1").removeAttr("disabled");
}


function changeFormSH1(ctype){
	clearSH1Values(ctype);
	getSH1Values(ctype);
}

function clearSH1Values(ctype){
	switch(ctype){
		case 'product_group':
			$("#product").html("");
			$("#fixed").hide();
		case 'product':
			$("#h_product_name").html("-");
			$("#h_price_month").html("-");
			$("#h_discount_for_fixed_period").html("-");
			$("#h_discount_for_fixed_period").hide();
			$("#l_discount_for_fixed_period").hide();
			break;
		default:
			break;
	}
}

function getSH1Values(ctype){
	data = {}; // zalozime objekt, ktery postupne plnime ve switchi
	var productGroupID = $("#product_group").val();
	var productID = $("#product").val();
	switch(ctype){
		case 'product_group':
			data.product = pdata[productGroupID][0].product;
			productID = pdata[productGroupID][0].product_selected
		case 'product':
			data.product_name = pdata[productGroupID][productID].product_name;
			data.price_month = pdata[productGroupID][productID].price_month;
			data.discount_for_fixed_period = pdata[productGroupID][productID].discount_for_fixed_period;
			if(pdata[productGroupID][productID].price_month_discounted){
				data.price_month_discounted = pdata[productGroupID][productID].price_month_discounted;
			}
			break;
		default:
			break;
	}
	setSH1Values(data, ctype);
}

function setSH1Values(data, ctype){
	switch(ctype){
		case 'product_group':
			$("#product").html(data.product);
		case 'product':
			$("#h_product_name").html(data.product_name);
			if(data.discount_for_fixed_period){
				$("#fixed").show();
				$("#h_discount_for_fixed_period").show();
				$("#l_discount_for_fixed_period").show();
			}
			var contract_selected = $("#contract_selected").val();
			if(contract_selected && contract_selected == 'fixed24' && data.price_month_discounted){
				$("#h_price_month").html(data.price_month_discounted);
				$("#h_discount_for_fixed_period").html(data.discount_for_fixed_period);
			}
			else{
				$("#h_price_month").html(data.price_month);
			}
			break;
		default:
			break;
	}
}

function changeSHCFix(value){
	var productGroupID = $("#product_group").val();
	var productID = $("#product").val();
	switch(value){
		case 'fixed24':
			$("#h_discount_for_fixed_period").html(pdata[productGroupID][productID].discount_for_fixed_period);
			$("#h_price_month").html(pdata[productGroupID][productID].price_month_discounted);
			$("#contract_selected").val('fixed24');
		break;
		default:
			$("#h_discount_for_fixed_period").html("-");
			$("#h_price_month").html(pdata[productGroupID][productID].price_month);
			$("#contract_selected").val('indefinitely');
		break;
	}
}

function fnumber(number) {
	return Math.max(0, number).toFixed(0).replace(/(?=(?:\d{3})+$)(?!^)/g, ' ');
}


// SSL certifikaty
$(document).ready(function() {
	// Pouze pokud jsme na prvnim kroku objednavky SSL certifikatu
	if ($('#add_domains_section').length != 0) {
		
		// Pred odeslanim formulare nastavime nektere hodnoty
		$("#order-form").bind("submit", function() {
			if ($('#add_domains_section').css('display') == 'none') {
				$('#add_domains').removeAttr('checked');
			}
			
			if ($('#licence_count_section').css('display') == 'none') {
				$('#licence_count').val('1');
			}

			if ($('#server_type_section').css('display') == 'none') {
				$('#server_type_section').html('');
			}
			
			if ($('#hosting_provider_section').css('display') == 'none') {
				$('#hosting_provider').val('ignum');
			}
			
			if ($('#period_section>#period').length == 0) {
				$('#period_section').html($periodSection);
				$('#period_section').css('display', 'none')
				$('#period').val('1');
			}
			
			return true;
		});
		
		// Perioda je podminene zobrazovana, proto si ulozime jeji HTML kod pro pozdejsi pouziti
		$periodSection = $('#period_section').html();
		
		// Zavolame change funkci pro schovani dynamickych elementu
		changeFormSSL1('product_group', false);
	}
});

function changeFormSSL1(ctype, setSelecboxes) {
	if (typeof(setSelecboxes) == 'undefined') {
		setSelecboxes = true;
	}
	clearSSL1Values(ctype, setSelecboxes);
	getSSL1Values(ctype, setSelecboxes);
}

function clearSSL1Values(ctype, setSelecboxes){
	if (!setSelecboxes) return;
	switch(ctype){
		case 'product_group':
			$("#product").html("");
		case 'product':
			$("#period").html("");			
		case 'period':
		case 'licence_count':
		case 'add_domains':
			$("#h_ssl_cert_name").html("-");
			$("#h_payment_period").html("-");
			$("#h_price_year").html("-");
			$("#h_price_all").html("-");
			$("#h_discount_for_payment_period").html("-");
			break;
	}
}

function getSSL1Values(ctype, setSelecboxes){
	data = {}; // zalozime objekt, ktery postupne plnime ve switchi
	var productGroupID = $("#product_group").val();
	var productID = $("#product").val();
	var periodID = $("#period").val();
	var addDomains = ($("#add_domains:checked").val() == 1)?1:0;
	// TODO: Do configu
	if (productGroupID != 26 && productGroupID != 27) {
		$('#licence_count').val('1');
	}
	var licenceCount = $("#licence_count").val();
	
	switch (ctype) {
		case 'product_group':
			data.product = pdata[productGroupID][0].product;
			data.product_group = productGroupID;
			if (setSelecboxes) {
				productID = pdata[productGroupID][0].product_selected
			}
		case 'product':
			if (setSelecboxes) {
				data.periods_options = '';
				if (typeof(pdata[productGroupID][productID]['periods_options']) != "undefined") {
					data.periods_options = pdata[productGroupID][productID]['periods_options'].html;
				}
				if (typeof(pdata[productGroupID][productID]['periods']) != "undefined") {
					periodID = pdata[productGroupID][productID]['periods'].selected;
				}
			}
		case 'period':
		case 'licence_count':
		case 'add_domains':
			data.product_name = pdata[productGroupID][productID].product_name;
			data.price_all = pdata[productGroupID][productID].price_all;
			if (periodID != null) {
				// TODO: Predgenerovat pomoci PHP
				switch (periodID) {
					case '1':
						data.payment_period = periodID + ' rok';
						break;
					case '2':
					case '3':
					case '4':
						data.payment_period = periodID + ' roky';
						break;
					default:
						data.payment_period = periodID + ' let';
				}
				// Vyjimka pro Ignum cert
				if (productGroupID == 22) {
					data.payment_period = '-';
				}

				var oneYearPrice;
				var oneYearAddDomainPrice = 0;
				$.each(pdata[productGroupID][productID]['periods_prices'], function (currentPeriodID, currentPeriodPrice) {
					if (currentPeriodID == 1) {
						oneYearPrice = currentPeriodPrice;
					}
					if (currentPeriodID == periodID) {
						var addDomainPrice = 0;
						data.addomains = false;
						if (typeof(pdata[productGroupID][productID]['add_domains']) != 'undefined') {
							data.addomains = true;
							$.each(pdata[productGroupID][productID]['add_domains'], function (currentAddDomainPeriodID, currentAddDomainPrice) {
									if (currentAddDomainPeriodID == 1) {
										oneYearAddDomainPrice = currentAddDomainPrice;
									}
									if (currentAddDomainPeriodID == periodID) {
									addDomainPrice = currentAddDomainPrice;
								}
							});
						}
						
						var vat = pdata[productGroupID][productID]['vat'];
						var price = ((Math.round(currentPeriodPrice) + addDomains * Math.round(addDomainPrice)) * licenceCount);
						var priceYear = price / periodID;
						var discountForPaymentPeriod = (Math.round(oneYearPrice) + addDomains * Math.round(oneYearAddDomainPrice)) * periodID * licenceCount - price;
						//alert (oneYearPrice+' - '+price+' - '+oneYearPrice+' - '+periodID+' - '+discountForPaymentPeriod);
						
						data.price_all = fnumber(price) + ' Kč bez DPH<br/>' + fnumber(price * (1 + vat / 100)) + ' Kč s DPH';
						data.price_year = fnumber(priceYear) + ' Kč bez DPH';
						data.discount_for_payment_period = fnumber(discountForPaymentPeriod) + ' Kč bez DPH';
					}
				});
			}
			break;
	}
	setSSL1Values(data, ctype, setSelecboxes);
}

function setSSL1Values(data, ctype, setSelecboxes){
	switch(ctype){
		case 'product_group':
			// TODO: Hodit do configu - zatim je to tu z casovych duvodu natvrdo (26 = Thawte, 27 = VerySign, 22 = Ignum)
			// Nabizet nastaveni poctu licenci a typ WEB serveru?
			if (data.product_group == 26 || data.product_group == 27) {
				$('#licence_count_section').css('display', 'block');

				if ($('#server_type_section').css('display') == 'none') {
					$('#server_type_section').css('display', 'block');
					if ($('input[name="server_type"]:checked').length == 0) {
						$('#server_type-iis').attr('checked', 'checked');
					}
				}
			}
			else {
				$('#licence_count_section').css('display', 'none');
				$('#server_type_section').css('display', 'none');
			}
			
			// Vyjimka pro Ignum cert
			if (data.product_group == 22) {
				$('#period_section').html('');
				$('#hosting_provider_section').css('display', 'none');
				$('#can_hide').css('display', 'none');
			}
			else {
				$('#period_section').html($periodSection);
				$('#period_section').css('display', 'block');
				$('#hosting_provider_section').css('display', 'block');
				$('#can_hide').css('display', 'block');
			}
			
			if (setSelecboxes) {
				$("#product").html(data.product);
			}
		case 'product':
			if (setSelecboxes) {
				$("#period").html(data.periods_options);
			}
			if (data.addomains != true) {
				$("#add_domains").removeAttr('checked');
				$('#add_domains_section').css('display', 'none');
			}
			else {
				$('#add_domains_section').css('display', 'block');
			}
		case 'period':
		case 'licence_count':
		case 'add_domains':
			if (setSelecboxes) {
				$("#h_ssl_cert_name").html(data.product_name);
				$("#h_payment_period").html(data.payment_period);
				$("#h_price_year").html(data.price_year);
				$("#h_price_all").html(data.price_all);
				$("#h_discount_for_payment_period").html(data.discount_for_payment_period);
			}
			break;
	}
}

function additionalVisibility(){
	if($('#additional').is(':visible')){
		$('#additional').hide();
		$('#additional_show').show();
		$('#additional_hide').hide();
	}
	else{
		$('#additional').show();
		$('#additional_show').hide();
		$('#additional_hide').show();
	}
}

function getAddonParams(el){
	var serviceID = $(el).attr('name').match(/\d+/);
	if($(el).is(":checked") && 0 == $("#id"+serviceID).parent().find("#" + serviceID + "-params").length){
		// Add
		$.get("/webcontrol/orderhd/ajax/", {type:'addonparams', product:serviceID}, function(data){
			if(data){
				$("#id" + serviceID).parent().append("<p id='" + serviceID + "-params'>" + data + "</p>");
			}
		});
	}else{
		if(1 == $("#id"+serviceID).parent().find("#" + serviceID + "-params").length){
			// Remove
			$("#id"+serviceID).parent().find("#" + serviceID + "-params").remove();
		}
	}
}

function mAddressVisibility(el){
	var madisplayb = $(el).attr("checked");
	if(true == madisplayb){
		$("#fieldset-mailing_address").hide();
	}
	else{
		$("#fieldset-mailing_address").show();
	}
}
