// hide existing foto popups
function closeFotoPopup(e)
{
    if ($('foto_overlay'))
        $('foto_overlay').remove();
    if (e) Event.stop(e);
}

function openFotoPopup(e)
{
    Event.stop(e);
    closeFotoPopup();
    var a = Event.element(e);
    if (a.nodeName != 'A')
        a = a.up('a');
    if (a) {
        var img = a.getAttribute('src');
        // create image popup
        var popup = '<div id="foto_overlay"><img src="' + img + '"></div>';
        a.insert(popup);
        $('foto_overlay').observe('click', closeFotoPopup);
    }
}

// build fotocol by moving images from content
function buildFotoCol()
{
    $$('img.foto_rechts').each(function(i) {
        var img = i.remove();
        img.show();
        $('fotocol').insert({bottom: img});
    });
}

// clear all content of an input on focus
function clearContent(e)
{
    Event.element(e).clear();
}

// validate game forms
function validateGame(e)
{
    var elm = Event.element(e);
    var stop = false;
    // check mandatory elements
    $$('#gameform .mandatory').each(function(i)
    {
        if ((i.type == 'checkbox' && !i.checked) || (i.value.length == 0)) {
            i.up('tr').addClassName('missing');
            stop = true;
        }
        else {
            i.up('tr').removeClassName('missing');
        }
    });
    if (stop) {
        $('validateerror').show();
        Event.stop(e);
        return false;
    }
    else {
        $('validateerror').hide();
        elm.send.name = 'register';
    }
}

// validate recipe mailing
function validateMailing(e)
{
    var elm = Event.element(e);
    var stop = false;
    // check mandatory elements
    $$('#tafform .mandatory').each(function(i)
    {
        if ((i.type == 'checkbox' && !i.checked) || (i.value.length == 0)) {
            i.up('label').addClassName('missing');
            stop = true;
        }
        else {
            i.up('label').removeClassName('missing');
        }
    });
    if (stop) {
        $('validateerror').show();
        Event.stop(e);
        return false;
    }
    else {
        $('validateerror').hide();
        elm.to.name = 'recipient';
    }
}

function validateProfile(e)
{
    var elm = Event.element(e);
    // check password
    var pass1 = $F('pass1');
    var pass2 = $F('pass2');
    if (pass1 && (pass1 != pass2)) {
        alert('De wachtwoorden zijn niet identiek.');
        Event.stop(e);
        return false;
    }
    return true;
}

// validate registration
function validateRegister(e)
{
    var elm = Event.element(e);
    var stop = false;
    // check mandatory elements
    $$('#veeregisterform .mandatory').each(function(i)
    {
        if ((i.type == 'checkbox' && !i.checked) || (i.value.length == 0)) {
            i.up('tr').addClassName('missing');
            stop = true;
        }
        else {
            i.up('tr').removeClassName('missing');
        }
    });
    if (stop) {
        $('registererror').show();
        Event.stop(e);
        return false;
    }
    else {
        $('registererror').hide();
        elm.send.name = 'register';
    }
}

// spamproof subscribe
function changeSubscribe(e)
{
    Event.element(e).changeme.name = 'subscribe';
}

// initialise page elements
function initPage()
{
    if ($('subscribeform')) {
        $('subscribeform').observe('submit', changeSubscribe);
    }
    var sponsorcontent = $('sponsorcontent');
    if (sponsorcontent) {
        $('sponsors').update(sponsorcontent.innerHTML);
    }
    if ($('fotocol')) {
        buildFotoCol();
    }
    $$('input.autoselect').each(function(i) {
        Event.observe(i, 'focus', clearContent, false);
    });
    $$('#gameform').each(function(f) {Event.observe(f, 'submit', validateGame);});
    $$('#tafform').each(function(f) {Event.observe(f, 'submit', validateMailing);});
    $$('#profile').each(function(f) {Event.observe(f, 'submit', validateProfile);});
    $$('#veeregisterform').each(function(f) {Event.observe(f, 'submit', validateRegister);});
    $$('.autosubmit').each(function(elm) {Event.observe(elm, 'change', function(e){Event.element(e).form.submit()})});
    $$('a.fotopopup').each(function(a) {a.observe('click', openFotoPopup);});
}

Event.observe(window, 'load', initPage);
