function validMail(string){var mail=/^.+@.+\..{2,4}$/; return mail.test(string);}
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function sendComment(pubid){	$('#odeslat-btn').attr("disabled","disabled");
	var name = $('#jmeno').val();
	var surname = $('#prijmeni').val();
	var comment = $('#komentar').val();
	if (jQuery.trim(comment) == ''){
		$('#comment-empty').show();
		$('#odeslat-btn').attr("disabled","");
		return false;
	}	
	$.post('/smallapp/send-comment/', {name:name, surname:surname, comment:comment, pubid:pubid}, sendCommentCallback, 'json');
}
function sendCommentCallback(data){
	switch (data.status){
		case 'ALL_OK':
			$('#komentar').val('');
			var ret = '<div class="koment-top"></div><div class="koment-text">'+data.comment+'</div><div class="koment-bottom">'+data.name+' | '+data.date+'</div>';
			var html = $('#komentare').html() + ret;
			$('#komentare').html(html);

			$('#odeslat-btn').attr("disabled","");
		break;
		case 'ERROR_EMPTY':
			$('#comment-empty').show();
			$('#odeslat-btn').attr("disabled","");
		break;
		case 'ERROR_DB':
			$('#comment-dberror').show();
			$('#odeslat-btn').attr("disabled","");
		break;
	}
}
function editComment(id){var comment = $("#koment_"+id).html();$("#koment_"+id).html('<textarea id="ta_'+id+'" cols="" rows="" style="width:100%">'+comment+'</textarea><br /><button onclick="saveComment('+id+'); return false;">Změnit</button> <button onclick="cancelComment('+id+'); return false;">Storno</button>');}
function cancelComment(id){$("#koment_"+id).html($("#ta_"+id).val());}
function saveComment(id){var c = $("#ta_"+id).val();$.post('/smallapp/save-comment/', {id:id, c:c}, saveCommentCallback, 'json')}
function saveCommentCallback(data){if (data.status == 'ALL_OK'){$("#koment_"+data.id).html(data.c);} else {alert('Error while saving comment');}}
function deleteComment(id){$.post('/smallapp/delete-comment/', {id:id}, deleteCommentCallback, 'json');}
function deleteCommentCallback(data){if (data.status == 'ALL_OK'){$("#koment_"+data.id).remove();$("#koment_t"+data.id).remove();$("#koment_b"+data.id).remove();} else {alert('Error while deleting comment');}}

function checkContactForm()
{
	var res = true;
	var name = $('#name').val();
	var question = $('#question').val();
	var hospoda = $('#hospoda').val();
	var email = $('#email').val();
	var telefon = $('#telefon').val();

	if (trim(name) == '')
	{
		res = false;
		$.jGrowl('Musíte zadat své jméno', { header:"Chyba", sticky: true, theme: "error" })
	}
	
	if (trim(question) == '')
	{
		res = false;
		$.jGrowl('Musíte zadat text dotazu', { header:"Chyba", sticky: true, theme: "error" })
	}
	
	if (trim(hospoda) == '')
	{
		res = false;
		$.jGrowl('Musíte zadat název restaurace', { header:"Chyba", sticky: true, theme: "error" })
	}
	
	if (trim(email) == '' && trim(telefon) == '')
	{
		res = false;
		$.jGrowl('Musíte zadat e-mail nebo telefon', { header:"Chyba", sticky: true, theme: "error" })
	} else {
		if (trim(email) != '' && !validMail(email))
		{
			res = false;
			$.jGrowl('Špatná e-mailová adresa', { header:"Chyba", sticky: true, theme: "error" })
		}		
	}
	
	return res;
}

/******** COOKIE METHODS FROM AGE-CHECK *********************/
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
/******************************************/
/**	MAPS **/
function getRegion(region, link, test)
{
	var flexApp = FABridge.flash.root();
	flexApp.centerMap(region);
	
	getPubList(region, test);
	$('.left-part').html('<h2>'+link.innerHTML+'</h2>');
}

function getPubList(region, test)
{
	test = (test == 1) ? 1 : 0;
	$.post('/smallapp/hospody/seznam/'+region+'/', {test:test}, getPubListCallback, 'json');	
}

function getPubListCallback(data)
{
	if (data.res == 'ALL_OK')
	{
		html = $('.left-part').html();
		$('.left-part').html(html + data.data);
	}
}

function getPubDetail(id, lat, lng)
{
	var flexApp = FABridge.flash.root();
	flexApp.centerMapOnPub(lat, lng);
	
	$('#p'+id).show();
}

function showTable(id)
{
	$('.table-provozovny').addClass('hide');
	$('#table-' + id).removeClass('hide');
}

/******************************************/
function newWin(URL) {
	okno=window.open(URL,'blank');
	okno.focus();
	return false;
}

function flash (file,width,height,flashvars,winmode,id,classs,alternative,will_be_age_check_shown) {
	if (will_be_age_check_shown==1) { // pro age check je reklama vypnuta
		return;
	}
	
	var shockmode = false;
	if(id!=''){
		id=" id=\""+id+"\"";
	}
	if(classs!=''){
		classs=' class="'+classs+'"';
	}
	var mimetype = 'application/x-shockwave-flash';

	if (navigator.mimeTypes) {
		if (navigator.mimeTypes[mimetype] != null) {
			if (navigator.mimeTypes[mimetype].enabledPlugin != null) {
				shockmode = true;
		      }
		}
	}
	if (!shockmode && navigator.appVersion.indexOf('MSIE') !=-1) {
		shockmode = true;
	}
	if (shockmode) {
		document.write("<object"+id+" type=\"application/x-shockwave-flash\" data=\""+file+"\""+classs+" width=\""+width+"\" height=\""+height+"\">");
		document.write("<param name=\"movie\" value=\""+file+"\" />");
		document.write("<param name=\"menu\" value=\"false\" />");
		document.write("<param name=\"wmode\" value=\""+winmode+"\" />");
		if(flashvars!=''){
			document.write("<param name=\"flashvars\" value=\""+flashvars+"\" />");
		}
		document.write(alternative);
		document.write("</object>");
	}
	else {
		document.write(alternative);
	}
}

function show_beer(show) {
	document.getElementById('pivo00').style.display = 'none';
	document.getElementById('pivo01').style.display = 'none';
	document.getElementById('pivo02').style.display = 'none';
	document.getElementById('pivo03').style.display = 'none';
	document.getElementById('pivo04').style.display = 'none';
	if(show) {
		document.getElementById(show).style.display = 'block';
	}
}

function checkPromoCode(promo)
{
	var code = $('#code').val();
	$.post('/smallapp/promo/checkcode/', {code:code, promo:promo}, checkPromoCodeCallback, 'json');
}

function checkPromoCodeCallback(data)
{
	switch (data.status)
	{
		case "PROMO_ENDED": $.jGrowl('Soutěž byla ukončena', { header:"Chyba", sticky: true, theme: "error" }); break;
		case "BAD_CODE": $.jGrowl('Špatný kód', { header:"Chyba", sticky: true, theme: "error" }); break;
		case "CODE_NOT_FOUND": $.jGrowl('Kód nebyl nalezen', { header:"Chyba", sticky: true, theme: "error" }); break;
		case "CODE_EXPIRED": $.jGrowl('Kód již byl použít', { header:"Chyba", sticky: true, theme: "error" }); break;
		default :
			//$.post('/smallapp/promo/showform/', {}, showFormCallback, 'json');
			$.fancybox({
				href: '/smallapp/promo/showform/',
				modal: true,
				showCloseButton:true,
				onComplete: function(){$('#fancybox-close').show();$.fancybox.resize();},
				scrolling: 'no',
				autoScale: false
			});
		break;

	}
}

function showFormCallback(data)
{
	$.fancybox({modal:true, showCloseButton:true, content:data.html});
}

function sendPromoForm()
{
	$('#sendButton').attr("disabled","disabled");
	var fields = ['jmeno', 'prijmeni', 'dn_den', 'dn_mesic', 'dn_rok', 'adresa', 'telefon', 'email', 'misto_nakupu', 'pocet', 'cislo_uctenky', 'otazka'];
	var fieldNames = ['Jméno', 'Příjmení', 'Den', 'Měsíc', 'Rok', 'Adresa', 'Telefon', 'E-mail', 'Místo nákupu', 'Počet zakoupených lahví', 'Číslo účtenky', 'Odpověď na soutěžní otázku'];
	var errors = Array();
	var values = {};
	var value = null;
	for (var i = 0; i < fields.length; i++)
	{
		value = $('#'+fields[i]).val();
		if (fields[i] == 'email')
		{
			if (!validMail(value))
			{
				errors.push('Špatná e-mailová adresa');
			}
		} else {
			if (trim(value) == '')
			{
				errors.push('Položka "'+fieldNames[i]+'" nesmí být prázdná');
			}
		}
		values[fields[i]] = value;
	}

	if (errors.length > 0)
	{
		$.jGrowl(errors.join('<br />'), { header:"Chyba", sticky: true, theme: "error" });
		$('#sendButton').attr("disabled","");
	} else {
		$.post('/smallapp/promo/saveform/', values, sendPromoFormCallback, 'json');
	}
}

function sendPromoFormCallback(data)
{
	if (data.status == 'ALL_OK')
	{
		switch (data.promo)
		{
			case 'spar' :
				window.location.replace("http://www.lobkowicz-premium.cz/soutez/garantovana-vyhra/");
			break;v
			case 'globus' :
				window.location.replace("http://www.lobkowicz-premium.cz/soutez-globus/garantovana-vyhra/");
			break;
		}
//		window.location.replace("http://www.lobkowicz-premium.cz/soutez/garantovana-vyhra/");
	} else {
		$('#sendButton').attr("disabled","");
		$.jGrowl(data.msg, { header:"Chyba", sticky: true, theme: "error" });
	}
}

function showGlobusMarkets()
{
	$('#fancybox-outer').css('width', '598px').css('height', '449px');
	var html = '<div id="provozovny_inline"><table class="table-provozovny" border="0"><tbody><tr><th class="textLeft">GLOBUS Liberec</th><td>Sousedská 600, 460 11 Liberec XI - Růžodol I.</td></tr>'
		+ '<tr><th class="textLeft">GLOBUS Trmice (Ústí nad Labem)</th><td>Tyršova 869, 400 04 Trmice</td></tr>'
		+ '<tr><th class="textLeft">GLOBUS Chomutov</th><td>Černovická 5430, 430 03 Chomutov</td></tr>'
		+ '<tr><th class="textLeft">GLOBUS Jenišov (Karlovy Vary)</th><td>Obchodní 30, 362 11 Jenišov u Karlových Varů</td></tr>'
		+ '<tr><th class="textLeft">GLOBUS Chotíkov (Plzeň)</th><td>Chotíkov u Plzně, 330 17 Plzeň</td></tr>'
		+ '<tr><th class="textLeft">GLOBUS Praha Čakovice</th><td>Kostelecká 823, 196 00 Praha 9</td></tr>'
		+ '<tr><th class="textLeft">GLOBUS Praha Černý Most</th><td>Chlumecká 765/6, 198 18 Praha 9</td></tr>'
		+ '<tr><th class="textLeft">GLOBUS Praha Zličín</th><td>Sárská 5/133, 155 00 Praha 5</td></tr>'
		+ '<tr><th class="textLeft">GLOBUS České Budějovice</th><td>České Vrbné, 370 11 České Budějovice</td></tr>' +
		'<tr><th class="textLeft">GLOBUS Pardubice</th><td>Poděbradská 293, 532 06 Pardubice</td></tr>' +
		'<tr><th class="textLeft">GLOBUS Brno</th><td>Hradecká 40, 621 00 Brno Ivanovice</td></tr>' +
		'<tr><th class="textLeft">GLOBUS Olomouc</th><td>Pražská ul. 39/248, 779 00 Olomouc</td></tr>' +
		'<tr><th class="textLeft">GLOBUS Opava</th><td>Těšínská 83, 746 01 Opava</td></tr>' +
		'<tr><th class="textLeft">GLOBUS Ostrava</th><td>Opavská 326/90, 725 27 Ostrava – Plesná</td></tr></tbody></table></div>';

	$.fancybox({
		content:html
	});
}