var box_array  = new Array();
var input_page = new Object(); 
var fadeCount  = 0; 

window.onload = function() 
{
    setContent();
    setTimeout(showBox, 50); 
}

window.onresize = function() 
{
    setContent();
}

function getWindowHeight() 
{
    var windowHeight = 0;
    if (typeof(window.innerHeight) == 'number') 
    {
        windowHeight = window.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight) 
    {
        windowHeight = document.documentElement.clientHeight;
    }
    else if (document.body && document.body.clientHeight) 
    {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}

function getWindowWidth() 
{
    var windowWidth = 0;
    if (typeof(window.innerWidth) == 'number') 
    {
        windowWidth = window.innerWidth;
    }
    else if (document.documentElement && document.documentElement.clientWidth) 
    {
        windowWidth = document.documentElement.clientWidth;
    }
    else if (document.body && document.body.clientWidth) 
    {
        windowWidth = document.body.clientWidth;
    }
    return windowWidth;
}
        
function setContent() 
{
    if (document.getElementById) 
    {
        var windowHeight = getWindowHeight();
        var windowWidth  = getWindowWidth();
        if (windowHeight > 0) 
        {
            var contentElement = document.getElementById('enquete');
            var contentHeight = contentElement.offsetHeight;
            var contentWidth  = contentElement.offsetWidth;
            if (windowHeight - contentHeight > 0) 
            {
                contentElement.style.position = 'absolute';
                contentElement.style.top = ((windowHeight / 2) - (contentHeight / 2)) + 'px';  
                contentElement.style.left = ((windowWidth / 2) - (contentWidth / 2)) + 'px';  
            }
            else 
            {
                contentElement.style.position = 'static';
            }
        }
    }
}

function gotoPage(page) 
{
    for (var i = 0; i < box_array.length; i++)
    {
        var hide_box = document.getElementById(box_array[i]);
        hide_box.style.display = 'none';
        
        var nav = document.getElementById('nav_page_'+i);
        nav.className = 'pagebox';
    }
    var show_box = document.getElementById(box_array[page]);
    show_box.style.display = 'block'; 
    var nav = document.getElementById('nav_page_'+page);
    nav.className = 'pagebox active';
}

function sendEnqueteForm()
{
    var input = document.getElementsByTagName('textarea');
    var valid = true;
    for (var i = 0; i < input.length; i++)
    {
        if (input[i].value == "" && input[i].name.substr(0, 5) == "open_" && input[i].id.substr(0, 8) == "required")
        {
            valid = false;
            alert("U heeft niet alle vragen ingevuld.\r\nKlik op de knop hieronder om naar de niet ingevulde vraag te gaan.");
            gotoPage(input_page[input[i].name]);
            break;
        }
    }
    if (optionsChecked() != false && valid == true)
    {
        if (confirm('U staat op het punt de enquete te verzenden.\r\nWilt u doorgaan?'))
        {
            document.enqueteForm.submit();
        }
    }
}

function optionsChecked()
{
    var input = document.getElementsByTagName('input'); 
    var curRadio    = "";
    var checkRadio  = "";
    for (var i = 0; i < input.length; i++)
    {
        if (input[i].type == "radio" && input[i].name.substr(0, 7) == "closed_")
        {
            curRadio = input[i].name;
            if (curRadio != checkRadio)
            {
                if (checkSingleRadio(curRadio) == false)
                {
                    alert("U heeft niet alle vragen ingevuld.\r\nKlik op de knop hieronder om naar de niet ingevulde vraag te gaan.");
                    gotoPage(input_page[input[i].name]);   
                    return false;
                }
            }
            checkRadio = input[i].name;
        }       
    }  
    return true;   
}

function checkSingleRadio(radioName)
{
    var input = document.getElementsByTagName('input'); 
    for (var i = 0; i < input.length; i++)
    {
        if (input[i].type == "radio" && input[i].name == radioName)
        {
            if (input[i].checked) 
            {
                return true;
            }
        }       
    }  
    return false;   
}

function showBox()
{
    var box = document.getElementById('enquete');
    if (box != null)
    {
        if (fadeCount <= 100)
        {
            box.style.filter = "alpha(opacity="+fadeCount+")";
            box.style.opacity = fadeCount/100;
            //box.style.setProperty("-moz-opacity" , fadeCount);
            setTimeout(showBox, 50);
            fadeCount += 10; 
        }
    }
    else
    {
        setTimeout(showBox, 50);
    }
}

function hideBox()
{
    var box = document.getElementById('enquete');
    if (box != null)
    {
        box.style.display = 'none';
    }
}

function startEnquete()
{
    document.getElementById('intro_box').style.display = 'none'; 
    document.getElementById('question_box').style.display = 'block'; 
    gotoPage(0);
}
