﻿var jeVidet = false;
function valideEmail(email)
{
    var reEmail = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
    var regex = new RegExp(reEmail);
    if (!email.match(regex)){
	    return false;
    }
    return true;
}
function setSession(name, value)
{
    if (!send_xmlhttprequest(null, 'GET', 'set_session.php?name=' + name+'&value='+value)) {
        
        return false;
    }
    
    return true;
}

function testCh(d)
{
    dd = document.getElementById('let-all');
    dd2 = document.getElementById('strava-all')
    
    if(!dd.checked || !dd2.checked)
    {
       
        setSession('adv', 'true');
        var adv =  document.getElementById('adv');
        adv.value = true;
       
    }
}
function filtruj(name, id)
{
    if(!jeVidet)
    {
    
    /*dd = document.getElementById('let-all');
    dd2 = document.getElementById('strava-all')
    
    if(!dd.checked || !dd2.checked)
    {
        //alert('hfhg');
        setSession('adv', true);
    }*/
    //uncheckAll(dd, 'let[]');
    //uncheckAll(dd2 ,'str[]');
    //setSession('filtr-form-letiste',null);
    //setSession('filtr-form-strava', null);
    
    }
    document.getElementById('show1').style.display = (!jeVidet ? 'none' : '');
    document.getElementById(name).style.display = (jeVidet ? 'none' : '');
    var adv =  document.getElementById('adv');
    var upresnit =  document.getElementById(id);
    var upresnitImg = document.getElementById(id+'-img');
    upresnit.innerText = (!jeVidet? "Skrýt upřesnění " : "Upřesnit ");
    upresnitImg.setAttribute("src", (!jeVidet? "/css/img/f-up.png" : "/css/img/f-down.png")); 
    
    jeVidet = (jeVidet ? false : true);
    adv.value = jeVidet;
    
    setSession('adv', jeVidet);
   
   
    /*dd=document.location.href;
    index = dd.indexOf('#asdf');
    if(index!=-1)
    {
        dd= dd.substring(0, index);
        
    }*/
    /*
    if(jeVidet)
    {
    document.location.href = dd + "#asdf";
    }
    else
    {
        
        document.location.href = dd;}
    //alert(jeVidet);*/
    
}

function uncheck(id, group)
{
    var check = true;
    var f= document.getElementsByName(group);
    var str ="";
    for(i=0; i< f.length; i++)
    {
        if(f[i].checked)
        {
             check = false;
             
        }
        //str = str + f[i].value;
    }
    document.getElementById(id).checked = check;
   
    return true;
}
function uncheckAll(id, group)
{
    
    var check = id.checked;
    var f= document.getElementsByName(group);
    var str ="";
    if(check){
        for(i=0; i< f.length; i++)
        {
            f[i].checked = false;
        }
    }
    
    //alert(str);
    return true;
}
var _activeOld = 'tabDes';
function changeTab(id, active)
  {
    var elementyDiv = document.getElementsByTagName("div");
    for(i = 0; i < elementyDiv.length; i++)
        {
            if (elementyDiv[i].id.indexOf("divTab") == 0) 
            {
                elementyDiv[i].style.display = 'none'
            }
        }
    
    element = document.getElementById(id);
    element.style.display = 'block';
    tabDeactive();
    _activeOld= active;
    ac = document.getElementById(active);
    if(active=='tabDes')
    ac.className="active first";
    else
    ac.className = 'active';
    
  }
  function tabDeactive()
  {
    if(_activeOld =='tabDes')
    document.getElementById(_activeOld).className ="f2";
    else
    document.getElementById(_activeOld).className ="";
  }
  function fceDisableAdv( id)
  {
    element = document.getElementById(id);
    element.style.display = 'none';
  }
  
  function showElement(id)
  {
    element = document.getElementById(id);
    element.style.display = element.style.display=='none' ? "block" : "none";
    
  }
  
  function hideElement(id)
  {
    element = document.getElementById(id);
    element.style.display = 'none';
  }
  
  function validate(form)
  {
    var hotel = form.hotel;
    if (null!= hotel){
        if (hotel.value.length < 2){
                alert("Prosím zadejte název hotelu!")
                form.hotel.focus()
                return false;
        }
    }
    
    var name = form.vytvoril.value;
    if (name.length < 2){
	    alert("Prosím zadejte vaše jméno!")
	    form.vytvoril.focus()
	    return false;
    }
    
    var name = form.polohaPopis.value;
    if (name.length < 2){
	    alert("Prosím zadejte vaš popis polohy a pláže!")
	    form.polohaPopis.focus()
	    return false;
    }
    
    var name = form.vybaveniPopis.value;
    if (name.length < 2){
	    alert("Prosím zadejte vaš popis vybavení!")
	    form.vybaveniPopis.focus()
	    return false;
    }
    
    var name = form.sluzbyPopis.value;
    if (name.length < 2){
	    alert("Prosím zadejte vaš popis služeb!")
	    form.sluzbyPopis.focus()
	    return false;
    }
    
    var name = form.zabavaPopis.value;
    if (name.length < 2){
	    alert("Prosím zadejte vaš popis hodnocení zábavy, sportu, stravy!")
	    form.zabavaPopis.focus()
	    return false;
    }
    
    var name = form.popis.value;
    if (name.length < 2){
	    alert("Prosím zadejte vaš popis celkový dojem!")
	    form.popis.focus()
	    return false;
    }
    
    var name = form.jednou.value;
    if (name.length < 2){
	    alert("Prosím zadejte vaš popis jednou větou!")
	    form.jednou.focus()
	    return false;
    }
    return true;
  }
  
  function registerLm(form)
  {
    var arrayPageSize = getPageSize();
    var arrayPageScroll = getPageScroll();
    var objBody = document.getElementsByTagName("body").item(0);
	
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.onclick = function () {hideBox(); return false;}
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = '100%';
        objOverlay.style.height = (arrayPageSize[1] + 'px');
        opacity = 40;
	objOverlay.style.filter = "alpha(opacity:"+opacity+")"; // IE/Win
	objOverlay.style.KHTMLOpacity = opacity/100;  // Safari<1.2, Konqueror
	objOverlay.style.MozOpacity = opacity/101; // Older Mozilla and Firefox
	objOverlay.style.opacity = opacity/100; // Safari 1.2, newer Firefox and Mozilla, CSS3
	objBody.insertBefore(objOverlay, objBody.firstChild);
        
        var objBox = document.createElement("div");
	objBox.setAttribute('id','lm-box-reg');
	objBox.style.display = 'none';
	objBox.style.position = 'absolute';
	objBox.style.zIndex = '100';	
	objBody.insertBefore(objBox, objOverlay.nextSibling);
        
        var boxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - 100) / 2);
	var boxLeft = ((arrayPageSize[0] - 20 - 300) / 2);
        
        objBox.style.top = (boxTop < 0) ? "0px" : boxTop + "px";
	objBox.style.left = (boxLeft < 0) ? "0px" : boxLeft + "px";
        objBox.innerHTML = '<a style="position:absolute; top:8px;right:5px;cursor:pointer; z-index:95" onclick="hideBox()"><img src="/css/close.gif"></a><img id="loadingimg" style="position:absolute;top:55px; left:110px" src="/css/loading.gif"><h2 class="pad5" style="background-color:#EAEAEA;">Registrace lastminute</h2>';
        if(valideEmail(form.email.value)){
        checkLm(form.email.value);
        }
        else {
            showBox();
            var preloadimg = document.getElementById('loadingimg');
            preloadimg.style.display ='none';
            objBox.innerHTML += '<p>Vámi zadaná e-mailová adresa není validní. Zřejmě jste se překlepl(a), to se stane :-).</p>';
        }
  }
  
  function checkLm(email)
  {
    if (!send_xmlhttprequest(lmObsluha, 'GET', '/ajax/registerLastmin.php?reg_mail=' + email)) {
        
        return false;
    }
    
    return true;
  }
  function showBox()
  {
    var element = document.getElementById('overlay');
    element.style.display ='block';
    var e1 = document.getElementById('lm-box-reg');
    e1.style.display='block';
  }
  function lmObsluha(xmlhttp, content) {
    showBox();
    var e1 = document.getElementById('lm-box-reg');
    var preloadimg = document.getElementById('loadingimg');
    stav='';
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        preloadimg.style.display ='none';
         var status = xmlhttp.responseXML.getElementsByTagName('stav');
         if( status.length > 0 ){
            switch(status[0].firstChild.data)
            {
                case 'insert-false':
                    stav = '<p>Je nám líto, ale nepodařilo se uložit záznam do databáze.</p>';
                    break;
                case 'email-ok':
                    stav ='<p>Děkujeme. Na uvedenou e-mailovu adresu Vám bylo zasláno potvrzení o úspěšné registraci.</p>';
                    break;
                case 'email-false':
                    stav = '<p>Chyba při odesílání potvrzovacího e-mailu došlo k chybě.</p>';
                case 'exist':
                    stav ='<p>Zadaný e-mail již je registrován</p>';
                    break;
                default:
                    stav='<p>Nastal neočekávaná chyba</p>';
                    break;
            }
            
         }
         e1.innerHTML += stav;
    
    }
    
  }
  
  function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function hideBox()
{
    objOverlay = document.getElementById('overlay');
    objLightbox = document.getElementById('lm-box-reg');

	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function changeTextArea(area, max)
{
    str = area.value.length;
    //alert(str);
    if((str%max) ==0)
    {
        //alert(area.value);
        area.rows = (str/max) <1 ? 1 :(str/max);
        //alert(area.offsetWidth);
    }
}


