Date.prototype.lastWeek = function() {
	var datum = new Date();
	datum.setFullYear(this.getFullYear(),'11','28') ;
	var determinedate = new Date();
	determinedate.setFullYear(datum.getFullYear(), datum.getMonth(), datum.getDate());
	var D = determinedate.getDay();
	if(D == 0) D = 7;
	determinedate.setDate(determinedate.getDate() + (4 - D));
	var YN = determinedate.getFullYear();
	var ZBDoCY = Math.floor((determinedate.getTime() - new Date(YN, 0, 1, -6)) / 86400000);
	var WN = 1 + Math.floor(ZBDoCY / 7);
	return WN;
}

String.prototype.replaceAll = function(strA,strB)
{
    var returnValue ;
    returnValue = this ;
    while ( returnValue.indexOf(strA) != -1)
    {
        returnValue = returnValue.replace(strA,strB);
    }
    return returnValue;
//    console.log(this) ;
}

function addItem(el,id,root)
{
	
	// Fotoboek
	if (id == '' || id == 7) {
       	window.location = 'fotoboek.html';
       	return;
    }
	
    new_item = prompt('Geef een naam voor het nieuwe item') ;
    if(new_item)
    {
        new_item_href = new_item ;
        var newLi = document.createElement('li');
        
        var newA = document.createElement('a');
        new_item_href = new_item_href.toLowerCase()
        new_item_href = new_item_href.replaceAll(' ' , '_')
        newA.href = new_item_href + '.html' ;
        newA.innerHTML = new_item ;
		
        newLi.appendChild(newA) ;
        myEll = el.parentNode.parentNode ;
        
        // myEll.getElementsByTagName('span')[0].appendChild(newLi) ;
        new Ajax.Request('./ajax/addmenuitem.php',{
            postBody : 'menu_id='+id+'&name='+new_item+'&href='+new_item_href+ '&user=' + user + '&login=' + login,
            onComplete : function()
            { 
                alert('Het item is toegevoegd.\nU wordt direct doorgestuurd naar de nieuwe pagina.');
                
                // Toevoegen 'h' argument vanwege correct ophalen pagina / 35 = Dagboek
                var extra = (root == 35) ? '?h=' + id : '';
                
                window.location = new_item_href + '.html' + extra ;
            }

        }) ;
    }
}

function remove_item(el , id)
{
    if(confirm('Weet u zeker dat u dit menu en de gehele inhoud wilt verwijderen?'))
    {
        el = el.parentNode ;
        el = el.parentNode ;
        el.parentNode.removeChild(el);
        new Ajax.Request('./ajax/removemenu.php',{
        postBody : 'menu_id='+id+ '&user=' + user + '&login=' + login ,
        onComplete : function()
        {
            alert('Het item is verwijderd') ;
        }
    }) ;
    }
}

function edit_item(el , id)
{
    var newContent = prompt('Geef een nieuwe naam op voor het item', '');
    
    // Geen content ? return
    if (!newContent) {
    	return;
    }
    
    el = el.parentNode;
    el = el.parentNode.getElementsByTagName('a')[0]; 
    el.innerHTML = newContent;
    new_item_href = newContent.toLowerCase();
    new_item_href = newContent.replace(' ' , '_');
    el.href = newContent + '.html';
    new Ajax.Request('./ajax/editmenu.php',{
        postBody : 'menu_id='+id+'&name='+newContent+'&href='+new_item_href+ '&user=' + user + '&login=' + login,
        onComplete : function()
        {
            alert('Het item is gewijzigd');
        }
    }) ;
}

function gbRemove(el,id)
{
    if(confirm('Weet u zeker dat u dit bericht verwijderen wil?'))
    {
        el = el.parentNode.parentNode.parentNode ;
        el.style.display = 'none'
        new Ajax.Request('./ajax/gbRemovePost.php',{
            postBody : 'id=' + id + '&user=' + user + '&login=' + login,
            onComplete : function(t)
            {
                if(t.responseText == 1)
                {
                    alert('Bericht is verwijderd');
                    el.parentNode.removeChild(el) ;
                }
                else
                {
                    el.style.display = '';
                    alert('Verwijderen mislukt') ;
                }
            }
        })
    }
}

function edit_adres(el)
{
    if(document.getElementById('is_text').value == '0')
    {
        var old_inner = el.innerHTML ;
        old_inner = old_inner.replaceAll('<br>','') ;
        el.innerHTML = old_inner ;
        document.getElementById('is_text').value = 1;
    }
}

function calc()
{
	
	var val = document.getElementById('bedrag').value ;
	// Geen nummer, return
    if (val.toString().search(/^[0-9]+$/) != 0) {
    	return;
    }

    if($('valuta_1').value != $('valuta_2').value)
    {
        var rates = document.getElementById('rate').value ;
        var new_val ;

        switch(document.getElementById('valuta_1').value)
        {
            case 'czk' :
            {
                new_val = val / rates ; 
                // new_val = (new_val*100)/100 ;
                
                new_val += '';
                var parts = new_val.split('.');
                if (typeof(parts[1]) != 'undefined') {
                	if (parts[1].length > 2) { 
                		// new_val = Math.round(new_val * 100) / 100;
                		new_val = parts[0] + '.' + parts[1].slice(0, 2); 
                	}
                }
                
            }
            break;
            case 'eur' :
            {
                new_val = val * rates ; 
                new_val += '';
                var parts = new_val.split('.');
                if (typeof(parts[1]) != 'undefined') {
                	if (parts[1].length > 2) { 
                		// new_val = Math.round(new_val * 100) / 100;
                		new_val = parts[0] + '.' + parts[1].slice(0, 2); 
                	}
                }
                
            }
            break;
        }
        document.getElementById('omgerekend').innerHTML = new_val ;
    }
}

function setSession(session_name , value)
{
    new Ajax.Request('ajax/setSession.php' , {
        postBody : 'session_name=' + session_name + '&session_value=' +value ,
        onComplete : function()
        {
            window.location.href = window.location.href;
        }
    })
}

function changeValuta(sender)
{
    tarId = (sender.id == 'valuta_1') ? 'valuta_2' : 'valuta_1' ;
    new_select = (sender.options[sender.selectedIndex].id == 'eur') ? 'czk' : 'eur' ;
    var mySel = document.getElementById(tarId) ;
    var opts = mySel.options ;
    for(var i = 0; i < opts.length ; i++)
    {
        if(opts[i].id == new_select)
        {
            mySel.options[i].selected = 'selected'
        }
    }
}


function download(file)
{
    new Ajax.Request('ajax/download.php' , {
        postBody : 'file=' + file ,
        onComplete : function()
        {
            //window.location.href = window.location.href;
        }
    })
}

function ShowFotoBlock()
{
    if($('select_album').value > 0)
    {
        new Ajax.Request('ajax/get_fotos.php' , {
            postBody : 'album_id=' + $('select_album').value ,
            onComplete : function(t)
            {
                $('foto_container').innerHTML = t.responseText ;
                $('album_naam').innerHTML = $('select_album').options[$('select_album').selectedIndex].innerHTML.split(' - ')[1] ;
            }
        }) ;
        $('foto_container').style.display = '' ;
        $('album_id').value = $('select_album').options[$('select_album').selectedIndex].value ;

        if($('select_album').value == 42)
        {

        }

    }
    else
    {
        $('foto_container').style.display = 'none' ;
    }
}

//function makeScroll(a)
//{
//    var a = a || window.event;
//      if (a.pageX || a.pageY) {
//        var cursor = {
//            'y' : a.pageY
//        }
//    }
//    else {
//        var de = document.documentElement;
//        var b = document.body;
//        var cursor = {
//            'y' : ((a.clientY + (de.scrollTop || b.scrollTop) - (de.clientTop || 0)) - (((document.getElementsByTagName('body')[0].offsetHeight - document.getElementsByTagName('table')[0].offsetHeight) / 2) + 29))
//        }
//    }
////    cursor = {'y' : (cursor.y - (383*2)) }
////    console.log(cursor)
//    var scrollpercent = ((cursor.y - 412) / $('ulContainerDiv').offsetHeight)
//    $('ulContainerDiv').scrollTop =(($('ulContainerDiv').getElementsByTagName('div')[0].offsetHeight + 412) * scrollpercent) ;
////    console.log(scrollpercent) ;
////    console.log($('ulContainerDiv').offsetX) ;
//
//}
//
//document.observe("dom:loaded", function() {
//    $('ulContainerDiv').onmousemove = makeScroll ;
//});

function scrollDown(el)
{
    el.scrollTop = el.scrollTop + 10 ;
}

function scrollUp(el)
{
    el.scrollTop = el.scrollTop - 10 ;
}
var MyScrollInterval ;
var scrollSpeed = 25 ;
function setScroll(el , action)
{
    switch(action)
    {
        case 'up' :
        {
            MyScrollInterval = setInterval(function() {
                scrollUp(el)
            } , scrollSpeed)
        }
        break;
        case 'down' :
        {
            MyScrollInterval = setInterval(function() {
                scrollDown(el)
            } , scrollSpeed)
        }
        break;
    }
}

function clearScroll()
{
    clearInterval(MyScrollInterval)
}

function showRefOpm(select)
{
    var showOpm = select.options[select.selectedIndex].getAttribute('opm');
    if(showOpm == 1)
    {
        $('reff_opm_input').style.display = '' ;
        $('ref_stat').value = 1;

    }
    else
    {
        $('reff_opm_input').style.display = 'none' ;
        $('ref_stat').value = 0;
    }
}