function window_maximize() {
	window.moveTo(0, 0);
	window.resizeTo(screen.width, screen.height);
}

function instructor_win(id){
	window.open('http://www.tabory.cz/vedouci-popup.php?id='+id, 'instructor_window', 'width=240,height=100');
}

function instructor_win_adjust() {
	cont = document.getElementById('whole_content');
	contW = cont.offsetWidth;
	contH = cont.offsetHeight;
	window.resizeBy(0, contH - 94);
	window.moveTo((screen.width / 2) - (contW / 2), (screen.height / 2) - (contH / 2));
	window.focus();
}

function show_instructor_questions() {
	quest_box = document.getElementById('instructor_questions_id');
	hlavas = document.getElementById('hlavni_vedouci_id');
	programovy = document.getElementById('programovy_vedouci_id');
	oddilak = document.getElementById('oddilovy_vedouci_id');

	if(hlavas.checked || programovy.checked || oddilak.checked) {
		quest_box.style.display = 'block';
	}
	else {
		quest_box.style.display = 'none';
	}
}

function displayEmployerFields(eleId, flds) {
	var selBox = document.getElementById(eleId);
	var elem = document.getElementById(flds);

	if (selBox.options[selBox.options.selectedIndex].value == 'p2') {
		elem.style.display = 'block';
	}
	else {
		elem.style.display = 'none';
	}
}

function copy_order_data(copy_from, copy_to) {
	var fields = new Array('firstname', 'lastname', 'email', 'phone');

	for(i = 0; i < fields.length; i++) {
		from = document.getElementById('frm_'+copy_from+'_'+fields[i]);
		to = document.getElementById('frm_'+copy_to+'_'+fields[i]);

		if(from.value.length != 0 && to.value.length == 0) {
			to.value = from.value;
		}
	}
}

function orderer_to_participant(copy_from, copy_to) {
	from = document.getElementById('frm_'+copy_from);
	to = document.getElementById('frm_'+copy_to);

	if(from.value.length != 0 && to.value.length == 0) {
		to.value = from.value;
	}
}

function check_req_values(formName, fillinMsg) {
	var to_return = true;
	var alerted = false;
	var focused = false;

	if (window.to_check_array) {
		for(i = 0; i < to_check_array.length; i++) {
			element = 'document.forms[\''+formName+'\'].'+to_check_array[i];

			if (eval(element)) {
				var ww_el = false;
				eval_str = element+'.value';

				if (eval(eval_str) === '') {
					if (!alerted) {
						alert(fillinMsg);
						alerted = true;
						to_return = false;
					}
					if (!focused) {
						try {
							eval(element+'.focus()');
							focused = true;
						}
						catch(oException) {
							//alert(element+'.focus() - '+oException.msg);
						}
					}

					try {
						eval(element+'.style.background = \'#c5c5c5\'');
					}
					catch(oException) {
						//alert(element+'.style.background - '+oException.msg);
					}
				}
				else {
					try {
						eval(element+'.style.background = \'#ffffff\'');
					}
					catch(oException) {
						//alert(element+'.style.background - '+oException.msg);
					}
				}
			}
		}
	}
	return to_return;
}

function order_submit() {
	var conditions_box = document.getElementById("conditions_agreement_id");

	reqFilled = check_req_values('order_form', 'Vyplňte prosím všechna zvýrazněná pole.');
	if (reqFilled) {
		if (conditions_box.checked) {
			document.forms['order_form'].submit();
		}
		else {
			alert('Pro odeslání formuláře je vyžadován souhlas se všeobecnými podmínkami.');
		}
	}
}

function show(eleId) {
	if(document.getElementById(eleId)) {
		document.getElementById(eleId).style.display = 'block';
	}
}

function hide(eleId) {
	if(document.getElementById(eleId)) {
		document.getElementById(eleId).style.display = 'none';
	}
}

function anketa_init() {
	var options = {
        target: '#anketa_button_ln',
        url: 'http://www.tabory.cz/save_vote.php'
    };

	$("#anketa_submit").bind("click", function(){
		if($(":radio:checked[name='anketa_radio']").val()) {
			$("#id_anketa_form").ajaxSubmit(options);
		}
		else {
			alert('Vyberte prosím jednu z možných odpovědí.');
		}
    });
}

function order_form_init(type) {
    $("#id_optional_extras select, #payment_type_id").bind("change", function(){
		total_price_count(type);
	});
	$("#id_dep_places input").bind("click", function(){
		total_price_count(type);
	});
	$("#insurance_chbox").bind("click", function(){
		total_price_count(type);
	});

	if(type == 'tabor') {
		if (!$("#frm_children_rc1").attr('value') || !$("#frm_children_rc2").attr('value') || $("#frm_children_rc1").attr('value').length != 6 || $("#frm_children_rc2").attr('value').length < 3) {
			$("#id_order_form input[id!='frm_children_rc1'][id!='frm_children_rc2']").attr('readonly', 'readonly').bind("click", alert_rc_fill);
		}

		$("#frm_children_rc1").bind("keyup", function(){
			if ($("#frm_children_rc1").attr('value').length == 6 && $("#frm_children_rc2").attr('value').length > 3) {
				$("#id_order_form input").removeAttr('readonly').unbind("click", alert_rc_fill);
				total_price_count(type);
				show_dps(type);
			}
		});
		$("#frm_children_rc2").bind("keyup", function(){
			if ($("#frm_children_rc1").attr('value').length == 6 && $("#frm_children_rc2").attr('value').length > 3) {
				$("#id_order_form input").removeAttr('readonly').unbind("click", alert_rc_fill);
				total_price_count(type);
				show_dps(type);
			}
		});
    }
    else if(type == 'rekreace') {
    	$("#id_participants input.rcinput1").bind("keyup", function(){
			if ($(this).attr('value').length == 6) {
				total_price_count(type);
			}
		});
    	$("#id_participants input.rcinput2").bind("keyup", function(){
			if ($(this).attr('value').length > 2) {
				total_price_count(type);
			}
		});
    }

    $("#frm_discount_code").bind("keyup", function(){
		var $len = $("#frm_discount_code").attr('value').length;
    	if ($len == 11 || $len == 12 || $len == 0) {
			total_price_count(type);
		}
	});

    $("#id_mf_contact_link").bind("click", function(){
		$("#id_mf_contact").toggle();
	});


	/*
	total_price_count(type);

	if(type == 'tabor') {
		show_dps(type);
	}
	*/
}

function alert_rc_fill() {
	alert('Vyplňte prosím nejprve rodné číslo přihlašovaného dítěte.');
}

function total_price_count(type) {
	var options = {
        target: '#id_total_price_box',
        beforeSubmit: showCountingMsg,
        url: 'get_total_price.php?type='+type
    };

	$("#id_order_form").ajaxSubmit(options);
}
function show_dps(type) {
	var options = {
        target: '#id_dp_box',
        beforeSubmit: showDpMsg,
        url: 'get_dps.php?type='+type
    };

	$("#id_order_form").ajaxSubmit(options);
}

function showCountingMsg() {
	var html = '<br /><fieldset class="mt18 order1"><legend class="red2 b">&nbsp;Výpočet celkové ceny:&nbsp;</legend><div class="acenter red2 b" style="padding: 12px 0 16px 0;">Cena se počítá...</div></fieldset>';

	$("#id_total_price_box").html(html);
}

function showDpMsg() {
	var html = '<br /><fieldset class="mt18 order1"><legend class="red2 b">&nbsp;Odjezdová místa:&nbsp;</legend><div class="acenter red2 b" style="padding: 12px 0 16px 0;">...Načítá se...</div></fieldset>';

	$("#id_dp_box").html(html);
}

function show_mm_section(section_id) {
	sec = $("#"+section_id);
	sec_sign = $("#"+section_id+"-sign");

	$(".mm_section[id!='"+section_id+"']").slideUp("fast");
	$(".sign_minus[id!='"+section_id+"-sign']").attr("class", "sign_plus");
	if (sec_sign.hasClass("sign_plus")) {
		sec_sign.attr("class", "sign_minus");
	}
	else {
		sec_sign.attr("class", "sign_plus");
	}

	if (sec.is(":hidden")) {
		sec.slideDown("fast");
	}
	else {
		sec.slideUp("fast");
	}
}

function restyle_ww_tables() {
	$(".wysiwyg2 table tr:first td").attr("class", "th");
}
