URL = '/';

var calId = 'kalendar';
var calOnLoadingId = 'onloading';
var calCloseId = 'close';
var calFrameId = 'virtFrame';
var calInsetId = 'kalendarinset';

function calShow(e)
{
    /*if (e=getElement('itemForm'))
    {
        var sel = false;
        for (var i=0; i<e.rezervace_pokoj.length; i++)
        {
            if (e.rezervace_pokoj[i].checked)
                sel = e.rezervace_pokoj[i].value;
        }
        if (!sel)
        {
            alert('Vyberte prosím apartmán.');
            return;
        }
    }*/
    if (vf=getElement(calFrameId))
        vf.style.display = 'block';
    else
    {
        xy = getPageSizeWithScroll();
        vf = document.createElement('DIV');
        vf.id = calFrameId;
        vf.style.position = 'absolute';
        vf.style.zIndex = 9;
        vf.style.left = '0px';
        vf.style.top = '0px';
        vf.style.width = '100%';//xy[0]+'px';
        vf.style.height = xy[1]+'px';
        vf.onclick = function() {
            if (e=getElement(calId))
            {
                e.style.display = 'none';
            }
            this.style.display = 'none';            
        }
        document.getElementsByTagName('body')[0].appendChild(vf);
    }
    
    if (typeof(e)!='object')
        e = getElement(e);
    //xy = findPosition(e);
    
    if (!(e=getElement(calId)))
    {
        e = document.createElement('DIV');
        e.id = calId;
        onloading = document.createElement('DIV');
        onloading.id = calOnLoadingId;
        closer = document.createElement('DIV');
        closer.id = calCloseId;
        closer.onclick = calHide;
        einset = document.createElement('DIV');
        einset.id = calInsetId;
        e.appendChild(onloading);
        e.appendChild(closer);
        e.appendChild(einset);
        try
        {
            document.getElementsByTagName('body')[0].appendChild(e);
            //<div id="kalendar"><div id="onloading"></div><div id="close" onclick="calHide();">x</div><div id="kalendarinset"></div></div>
        }
        catch(e) {return};
    }

    e.style.left = tempMouseX+'px';
    e.style.top = tempMouseY+'px';
    e.style.display = 'block';
    cal_ajax.update();
}

function calHide()
{
    if (e=getElement(calId))
        e.style.display = 'none';
    if (vf=getElement('virtFrame'))
        vf.style.display = 'none';
}

function calMonth(m,y)
{
    if (e=getElement('itemForm'))
    {
        e.tm.value = m;
        e.ty.value = y;
        cal_ajax.update();
    }
}

function calSelect(d)
{
    if (e=getElement('itemForm'))
    {
        e.objednavka_datum.value = d;
        calHide();
        //dnu_ajax.update();
    }
}

var axTimer = null;

    var cal_ajax = new sack();
        cal_ajax.requestFile = URL+'includes/js.php';
        cal_ajax.method = 'POST';
        cal_ajax.element = calId;
        cal_ajax.reqType = false;
        cal_ajax.onLoading = function() {
            if (e=getElement(calOnLoadingId))
            {
                e.style.display = 'block';
                e.innerHTML = '';
            }
        };
        cal_ajax.onLoaded = function() {
        };
        cal_ajax.onCompletion = function() {
            if (e=getElement(calOnLoadingId))
                e.style.display = 'none';
            if (e=getElement(calInsetId))
            {
                e.innerHTML = this.response;
            }
        };
        cal_ajax.onError = function() {
            if (e=getElement(calOnLoadingId))
                e.style.display = 'none';
            if (e=getElement(calId))
                e.innerHTML = 'ERROR '+this.responseStatus[0]+': '+this.responseStatus[1];
        }
        cal_ajax.update = function ()
        {
            if (e=getElement('itemForm'))
            {
                /*var sel = false;
                for (var i=0; i<e.rezervace_pokoj.length; i++)
                {
                    if (e.rezervace_pokoj[i].checked)
                        sel = e.rezervace_pokoj[i].value;
                }
                if (!sel)
                {
                    alert('Vyberte prosím apartmán.');
                    return;
                }*/
                this.setVar('s', 'cal');
                //this.setVar('roubenkaId', e.roubenkaId.value);
                //this.setVar('pokoj', sel);
                this.setVar('tm', e.tm.value);
                this.setVar('ty', e.ty.value);
                this.runAJAX();
            }
        }
        
    var dnu_ajax = new sack();
        dnu_ajax.requestFile = URL+'includes/js.php';
        dnu_ajax.method = 'POST';
        dnu_ajax.element = calId;
        dnu_ajax.reqType = false;
        dnu_ajax.onLoading = function() {
            if (e=getElement('onloading'))
            {
                e.style.display = 'block';
                e.innerHTML = '';
            }
        };
        dnu_ajax.onLoaded = function() {
        };
        dnu_ajax.onCompletion = function() {
            if (e=getElement('onloading'))
                e.style.display = 'none';
            if (e=getElement('cal_result'))
            {
                e.innerHTML = this.response;
            }
        };
        dnu_ajax.onError = function() {
            if (e=getElement('onloading'))
                e.style.display = 'none';
            if (e=getElement('cal_result'))
                e.innerHTML = 'ERROR '+this.responseStatus[0]+': '+this.responseStatus[1];
        }
        dnu_ajax.update = function ()
        {
            if (e=getElement('itemForm'))
            {
                var sel = false;
                for (var i=0; i<e.rezervace_pokoj.length; i++)
                {
                    if (e.rezervace_pokoj[i].checked)
                        sel = e.rezervace_pokoj[i].value;
                }
                this.setVar('s', 'rezdatum');
                this.setVar('roubenkaId', e.roubenkaId.value);
                this.setVar('pokoj', sel);
                this.setVar('od', e.rezervace_od.value);
                this.setVar('dnu', e.rezervace_dnu.value);
                this.runAJAX();
            }
        }

var axTimer = null;

    var statdata = new sack();
        statdata.requestFile = URL+'stats.php';
        statdata.method = 'GET';
        statdata.element = false;
        statdata.fotoId = false;
        statdata.reqType = false;
        statdata.onLoading = function() {
        };
        statdata.onLoaded = function() {
        };
        statdata.onCompletion = function() {
            if (typeof(this.responseXML)!='undefined' && this.responseXML!=null)
            {
                if (e=getElement('celkem_zobrazeni'))
                    e.innerHTML = getXMLSingleValue(this.responseXML, 'celkem_navstev'); 
                if (e=getElement('tyden_zobrazeni'))
                    e.innerHTML = getXMLSingleValue(this.responseXML, 'tyden_navstev'); 
                if (e=getElement('den_zobrazeni'))
                    e.innerHTML = getXMLSingleValue(this.responseXML, 'den_navstev'); 
                if (e=getElement('online_zobrazeni'))
                    e.innerHTML = getXMLSingleValue(this.responseXML, 'online'); 
            }
            else
            {
                /*if (e=getElement('fotoMainLoading'))
                    e.innerHTML = '<span class="red">'+this.response+'</span>';*/
            }
        };
        statdata.onError = function() {
            //alert(this.responseStatus[0]);
        }
    statdata.update = function ()
    {
        this.runAJAX();
    }
    
function getXMLSingleValue(xml, tagName)
{
    values = xml.getElementsByTagName(tagName);
    if (values.length>0)
        try {
            value = (typeof(values[0].firstChild.nodeValue)!='undefined') ? values[0].firstChild.nodeValue : false;
            if (!value)
                value = (typeof(values[0].childNodes[0].text)!='undefined') ? values[0].childNodes[0].text : false;
        } catch(ex) {
            value = false;
        }
    else
        return false;
    return (value!='') ? value.unescapeHtml() : false
}

String.prototype.unescapeHtml = function () {
    var temp = document.createElement("div");
    temp.innerHTML = this;
    var result = temp.childNodes[0].nodeValue;
    temp.removeChild(temp.firstChild)
    return result;
}

function delay(cmd) {
    if (typeof(axTimer)!='undefined')
        clearTimeout(axTimer);
    axTimer = setTimeout(cmd, 200);
}

function gup( name )
{
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
        return "";
    else
        return results[1];
}
