var will_be_age_check_shown = 0; // priznak, jestli budu zobrazovat AgeCheck, pokud ano, vypnu TV

function createCookie(cookie_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 = cookie_name+"="+value+expires+"; path=/";
}
function readCookie(cookie_name) {
	var nameEQ = cookie_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 checkTrue(){
	createCookie("RadegastAgeCheck","true",0); // was "RadegastAgeCheck","true",0
	document.getElementById("age_check_screen").style.display = "none";
    document.getElementById("age_check_tab").style.display = "none";
    document.getElementById("age_check_header").style.display = "none";
	document.getElementById("age_check_tab").innerHTML= "";
    //window.location.reload(); // refresh stranky
}
function checkFalse(){
	//createCookie("RadegastAgeCheck","false",1);
	document.getElementById("age_check_nadpis").innerHTML="<p>Je nám líto, ale nesplnil jste podmínku věku minimálně 18 let.</p>";
	
	// posuneme pozadi pullitru a zmenime text
	//document.getElementById("age_check_bottle").style.backgroundPosition="0px -316px";
	//document.getElementById("age_check_nadpis").style.backgroundImage="url(/_clip/age-check/tit-jenamlito.gif)";
	
	//document.getElementById("ac-content").style.display = "none";
	//document.getElementById("ac-content").innerHTML= "";
	//document.getElementById("age_check_login").style.display = "none";
	//document.getElementById("age_check_login").innerHTML= "";
}
function eraseCookie(cookie_name) {
	createCookie(cookie_name,"",-1);
}
function resizeLayer(){
	var winH = document.documentElement.scrollHeight;
	document.getElementById("age_check_screen").style.height= winH+"px";
}
function startAgeCheck(){
	/* var server = getServer(); */
	var checked = readCookie("RadegastAgeCheck");

	if(checked == null || checked == "false"){
	    document.write('<div id="age_check_screen"></div><div id="age_check_header"><div id="age_check_bottle"></div></div>'+
				'<div id="age_check_tab"><div id="age_check_content">'+
				'<div id="age_check_main_holder"><p class="ac_txt">Pro vstup na stránky musíš být starší 18 let<br />nebo osoba podléhající právu USA starší 21 let.</p><div id="age_check_main">'+
				'<div id="ac-content"><p>Zadejte datum<br />Vašeho narození</p>'+
				'<div class="ac-inputs"><table class="form-box">'+
				'<tr><th>Den:</th><th>Měsíc:</th><th>Rok:</th></tr><tr>'+
				'<td><input maxlength="2" name="den-narozeni" id="den-narozeni" value="DD" type="text" class="year" onfocus="if(this.value==\'DD\'){this.value=\'\'}" onblur="if(this.value==\'\'){this.value=\'DD\'}" /></td>'+
				'<td><input maxlength="2" name="mesic-narozeni" id="mesic-narozeni" value="MM" type="text" class="year" onfocus="if(this.value==\'MM\'){this.value=\'\'}" onblur="if(this.value==\'\'){this.value=\'MM\'}" /></td>'+
				'<td><input maxlength="2" name="rok-narozeni" id="rok-narozeni" value="RR" type="text" class="year" onfocus="if(this.value==\'RR\'){this.value=\'\'}" onblur="if(this.value==\'\'){this.value=\'RR\'}" /></td>'+
				'</td></tr></table></div>'+
				'<div class="ac-enter"><a onclick="return checkAgeDate();" href="#" title="Vstoupit"></a></div>'+
				'<div id="age_check_nadpis"></div>'+
				'</div>'+
				'<div id="age_check_login"><div class="ac-login-text"><p>Vstup pro Členy Kmene Radegast</p></div><div class="ac-login-form"><div id="err_code"></div><div id="input-data"><input name="ach_login" id="ach_login" value="Login" onfocus="if(this.value==\'Login\'){this.value=\'\'}" onblur="if(this.value==\'\'){this.value=\'Login\'}" type="text" /><input name="ach_pswd" id="ach_pswd" type="password" value="123456" onfocus="if(this.value==\'123456\'){this.value=\'\'}" onblur="if(this.value==\'\'){this.value=\'123456\'}"/>'+
				'<input title="Přihlásit se do Radegast Kmene" class="ac-btn-login" onclick="login();" type="button" /></div></div> </div></div></div><a class="btn_napivo" title="ALKOHOL ZA VOLANT NEPATŘÍ - www.napivosrozumem.cz" href="http://www.napivosrozumem.cz" onclick="window.open(\'http://www.napivosrozumem.cz\'); return false;"></a></div><div class="ac-login-footer"><p>Pro správnou funkci stránek je nuté ve Vašem prohlížeci zapnout podporu cookies.</p></div></div>');
				
	    window.onresize=resizeLayer;
		window.onload=resizeLayer;
	}
	if (checked == "false") {
	    checkFalse();
	}
}
function getServer(){
	var host = window.location.toString();
	host = host.split('://');
	var protocol = host[0];
	host = window.location.host;

	return protocol+"://"+host;
}


function checkAgeDate()
{
	url = "&den=" + document.getElementById("den-narozeni").value;
	url += "&mesic=" + document.getElementById("mesic-narozeni").value;
	var rok = parseInt(document.getElementById("rok-narozeni").value);
	var theDate=new Date()
	if( rok <= (parseInt(theDate.getFullYear())%100) ){
	    rok += 2000;
	}
	else{
	    rok += 1900;
	}
	url += "&rok=" + rok ;
	
	document.getElementById('age_check_nadpis').innerHTML = "";
    var ajax = new EasyAjax('POST', url, '/smallapp/ajax/agecheckdate/');
    ajax.onSuccess("checkDateCallback");
    ajax.doRequest();
    return false;
}

function checkDateCallback(data)
{
	var res = eval("("+data+")");
	switch (res.status)
	{
		//case 'ERR_EMPTY':
		//	document.getElementById('age_check_nadpis').innerHTML = "Musite uvest mesic a rok narozeni";
		//	break;
		case 'ALL_OK':
			checkTrue();
			break;
		case 'ERR_TOOYOUNG':
			checkFalse();
			break;	
	}
}

function spinDecrement(item, min, max) {
	element = document.getElementById(item);
	if(isNaN(element.value)) {
		element.value = Math.round((max+min) / 2);
	} else {
		if(element.value <= min) {
			element.value = max;
		} else {
			element.value = Number(element.value) - 1;
		}
	}
}

function spinIncrement(item, min, max) {
	element = document.getElementById(item);
	if(isNaN(element.value)) {
		element.value = Math.round((max+min) / 2);
	} else {
		if(element.value >= Number(max)) {
			element.value = min;
		} else {
			element.value = Number(element.value) + 1;
		}
	}
}

function startDecrement(item, min, max) {
	spinDecrement(item, min, max);
	if( (window) && (window['spin'+item]) ) {
	} else {
		window['spin'+item] = setInterval("spinDecrement('"+item+"', '"+min+"', '"+max+"')", 200);
	}
}

function stopDecrement(item) {
	if( (window) && (window['spin'+item]) ) {
		clearInterval(window['spin'+item]);
		window['spin'+item] = false;
	}
}

function startIncrement(item, min, max) {
	spinIncrement(item, min, max);
	if( (window) && (window['spin'+item]) ) {
	} else {
		window['spin'+item] = setInterval("spinIncrement('"+item+"', '"+min+"', '"+max+"')", 200);
	}
}

function stopIncrement(item) {
	if( (window) && (window['spin'+item]) ) {
		clearInterval(window['spin'+item]);
		window['spin'+item] = false;
	}
}

startAgeCheck();

