function OpenPage(URL,width,height)
{
    var scbr = 'no';
    var w = width;
    var h = height;
    var left = Math.round(screen.width/2-w/2);
    var top = Math.round(screen.height/2-h/2);


    if (width>=screen.width)
    {
        w = screen.width-25;
        scbr = 'yes';
    }

    if (height>=screen.height)
    {
        h = screen.height-70;
        scbr = 'yes';
    }

    window.open(URL,'','left='+left+', top='+top+', menubar=no, toolbar=no, width='+w+', height='+h+', resizable=no, scrollbars='+scbr+', status=no');
}

function OpenImg(src, alt) 
{
  image = window.open('','image','left=50, top=50, width=400, height=400, toolbar=no, menubar=no, status=no, resizable=no, scrollbars=no');
  image.focus();
  image.document.write('<html><head><title>Kentaurus Praha</title>');
  image.document.write('<script language="javascript"> var NS = (navigator.appName=="Netscape") ? true:false; function FitPic() { iWidth = (NS)?window.innerWidth:document.body.clientWidth; iHeight = (NS)?window.innerHeight:document.body.clientHeight; iWidth = document.images[0].width - iWidth; iHeight = document.images[0].height - iHeight; window.resizeBy(iWidth, iHeight); self.focus(); } </script>');
  image.document.write('</head><body onload="FitPic();" topmargin="0" leftmargin="0" marginwidth="0" marginheight="0" align="center"><a href="javascript: window.close();"><img src="'+src+'" alt="'+alt+'" border="0" /></a></body></html>');
  image.document.close();
}

function recountEndDate()
{
    var dateStElementName = 'fs_dateSt';
    var prefix = dateStElementName.substr(0,3);
    
    var dateStElement = document.getElementById(dateStElementName);
    var nightsElement = document.getElementById(prefix + 'nights');
    var dateEndElement = document.getElementById(prefix + 'dateEnd');
    
    if(dateStElement == undefined || nightsElement == undefined || dateEndElement == undefined) return;
    
    var f_dS_value = String(dateStElement.value);

    var f_nights_value = nightsElement.value;

    var regexpDate = new RegExp("^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}$");
    var regexpNights = new RegExp("^[0-9]+$");

    if(f_dS_value != '' && regexpDate.test(f_dS_value) && regexpNights.test(f_nights_value))
    {
        var f_dS_splitted = f_dS_value.split('/');
        var dS_day = f_dS_splitted[0];
        var dS_month = f_dS_splitted[1];
        var dS_year = f_dS_splitted[2];
        
        var dS = new Date(dS_year, dS_month-1, dS_day, 12, 0, 0, 0); // months in Javascript: 0,1,2,.....,11
        var dS_stamp = dS.getTime() / 1000;
        
        var dE_stamp = dS_stamp + (f_nights_value * 86400);
        var dE = new Date(dE_stamp * 1000);
        
        var dE_day = dE.getDate();
        if(dE_day < 10) dE_day = "0" + dE_day;
        var dE_month = dE.getMonth() + 1; // months in Javascript: 0,1,2,.....,11
        if(dE_month < 10) dE_month = "0" + dE_month;

        
        var dE_string = dE_day + '/' + dE_month + '/' + dE.getFullYear();
        dateEndElement.value = dE_string;
    }
    else
    {
        dateEndElement.value = '';
    }
}

/*
 * DISABLING PAST DATES IN CALENDAR
 */

function dateStatus(date) {
	var now = new Date();
	if (date.getTime() <= now.getTime())
		return true; // true says "disable"
	else
		return false; // leave other dates enabled
}

/*
 * FLOATING HELPS
 */

function viewFloatingHelp(helpText)
{
    var fpTitleDiv = document.getElementById('floatingHelp');
    fpTitleDiv.innerHTML = helpText;
    
    var fpDiv = document.getElementById('floatingHelp');
    mX = mouseX + document.body.scrollLeft - 250;
    mY = mouseY + document.body.scrollTop;
    
    fpDiv.style.left = (mouseX + 10) + 'px';
    //fpDiv.style.left = (screen.width/2 - 54) + 'px';
    fpDiv.style.top = (mouseY + 10) + 'px'; 
    fpDiv.style.visibility = 'visible';
}

function hideFloatingHelp()
{
    var fpDiv = document.getElementById('floatingHelp');
    fpDiv.style.visibility = 'hidden';
}

function confirmAction(text) {
    return window.confirm(text);
}


// AJAX - anketa

var quizID = 0;

function send_xmlhttprequest(handler, protocol, url, content, headers)
{
    var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
    if (!xmlhttp) {
        return false;
    }
    xmlhttp.open(protocol, url);
    xmlhttp.onreadystatechange = function() {
        handler(xmlhttp);
    };
    if (headers) {
        for (var key in headers) {
            xmlhttp.setRequestHeader(key, headers[key]);
        }
    }
    xmlhttp.send(content);
    return true;
}

function quiz_vote(answer, lang)
{
    if (!send_xmlhttprequest(quiz_handler, 'GET', '/quiz.php?quiz=' + quizID + '&answer=' + answer + '&lang=' + lang)) 
    {
        return false;
    }

    // informace o ukladani hlasovani
    return true;
}

function quiz_handler(xmlhttp)
{
    if (xmlhttp.readyState == 4) 
    {
        var quiz = xmlhttp.responseXML.getElementsByTagName('quiz');
        quizID = quiz[0].getAttribute('id');

        var question = xmlhttp.responseXML.getElementsByTagName('question');
        document.getElementById('quiz_question').innerHTML = question[0].firstChild.data;
        
        var answers = xmlhttp.responseXML.getElementsByTagName('answer');
        for (var i=0; i < answers.length; i++) 
        {
            document.getElementById('quiz_vote' + answers[i].getAttribute('id')).innerHTML = answers[i].firstChild.data + '  ' + answers[i].getAttribute('perc') + '%';
            document.getElementById('quiz_bar' + answers[i].getAttribute('id')).style.width = Math.round(200 * answers[i].getAttribute('perc') / 100) + 'px';
        }
    }
}

