var curfocus = '';
var lastDownEffectBrengen = '';
var lastDownEffectGrofvuil = '';
var lastDownEffectKalender = '';

function setFocus(subnaam) {
    curfocus = subnaam;
}

function showSub(subnaam) {
    	if ($(subnaam).visible()) {
            clearQueue(subnaam);
        }   
        if (!$(subnaam).visible() ||  $(subnaam).style.height != '28px') {
            $(subnaam).style.height = '28px';
            Effect.SlideDown($(subnaam), {duration: 0.5, queue: {position: 'end', scope: subnaam}});
        }
}

function hideSub(subnaam) {
    if (subnaam != curfocus) {
        $(subnaam).style.height = '28px';
        switch (subnaam) {
            case 'subbrengen':      lastDownEffectBrengen = Effect.SlideUp($(subnaam), {delay:0.5, duration: 0.5, queue: {position: 'end', scope: subnaam}});
                                    break;
            case 'subgrofvuil':     lastDownEffectGrofvuil = Effect.SlideUp($(subnaam), {delay:0.5, duration: 0.5, queue: {position: 'end', scope: subnaam}});
                                    break;
            case 'subkalender':     lastDownEffectKalender = Effect.SlideUp($(subnaam), {delay:0.5, duration: 0.5, queue: {position: 'end', scope: subnaam}});
                                    break;
            case 'subbrengensmall': lastDownEffectBrengen = Effect.SlideUp($(subnaam), {delay:0.5, duration: 0.5, queue: {position: 'end', scope: subnaam}});
                                    break;
            case 'subgrofvuilsmall':lastDownEffectGrofvuil = Effect.SlideUp($(subnaam), {delay:0.5, duration: 0.5, queue: {position: 'end', scope: subnaam}});
                                    break;
            case 'subkalendersmall':lastDownEffectKalender = Effect.SlideUp($(subnaam), {delay:0.5, duration: 0.5, queue: {position: 'end', scope: subnaam}});
                                    break;
        }
    }
}

function clearQueue(subnaam) {
	var queue = Effect.Queues.get(subnaam);
	queue.each(function(e) { e.cancel() });
}

function clearHide(subnaam) {
        switch (subnaam) {
            case 'subbrengen':      Effect.Queues.get(subnaam).remove(lastDownEffectBrengen);
                                    break;
            case 'subgrofvuil':     Effect.Queues.get(subnaam).remove(lastDownEffectGrofvuil);
                                    break;
            case 'subkalender':     Effect.Queues.get(subnaam).remove(lastDownEffectKalender);
                                    break;
            case 'subbrengensmall': Effect.Queues.get(subnaam).remove(lastDownEffectBrengen);
                                    break;
            case 'subgrofvuilsmall':Effect.Queues.get(subnaam).remove(lastDownEffectGrofvuil);
                                    break;
            case 'subkalendersmall':Effect.Queues.get(subnaam).remove(lastDownEffectKalender);
                                    break;        
        }        
}

function showSubs(id) {
	var elem = document.getElementsByClassName('sub'+id);
	for (var i = 0; i < elem.length; i++) {
		Element.toggle(elem[i]);
	}
    /*
	if (elem.length > 0) {
		if ($('img'+id).src.match('../../img/icons/arrow-right.gif')) {
			$('img'+id).src = '../../img/icons/arrow-down.gif';
		}
		else {
			$('img'+id).src = '../../img/icons/arrow-right.gif';
		}
	}
    */
}
function createXMLHttpRequest() {
	//document.getElementById("spinner").style.display = "block";
	if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
	}
}

function getCalendar(time) {
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = getCalendarHTML;
	xmlHttp.open("GET", "/extra/agenda.php?time="+time, true);
	xmlHttp.send(null);
}

function getCalendarHTML() {
	if (xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			document.getElementById("caldiv").innerHTML = xmlHttp.responseText;
		}
		//document.getElementById("spinner").style.display = "none";
	}
}

function zoekIntern() {
	var intern = document.getElementById("internfield").value;
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = zoekInternChange;
	xmlHttp.open("GET", "extra/agenda_nieuw.php?type=intern&search="+intern, true);
	xmlHttp.send(null);
}

function zoekInternChange() {
	if (xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			document.getElementById("internselect").innerHTML = "<select name='intern[]' size='6' multiple='multiple'>"+xmlHttp.responseText+"</select>";
			document.getElementById("spinner").style.display = "none";
		}
	}
}

function zoekExtern() {
	var intern = document.getElementById("externfield").value;
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = zoekExternChange;
	xmlHttp.open("GET", "extra/agenda_nieuw.php?type=extern&search="+intern, true);
	xmlHttp.send(null);
}

function zoekExternChange() {
	if (xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			document.getElementById("externselect").innerHTML = "<select name='extern[]' size='6' multiple='multiple'>"+xmlHttp.responseText+"</select>";
			document.getElementById("spinner").style.display = "none";
		}
	}
}

function showOptions(object, id) {
	if (navigator.appName == "Microsoft Internet Explorer") {
		document.getElementById("showoptions").style.left = document.body.scrollLeft + event.clientX;
		document.getElementById("showoptions").style.top = document.body.scrollTop + event.clientY;
	} 
	else {
    	document.getElementById("showoptions").style.left = object.pageX;
	    document.getElementById("showoptions").style.top = object.pageY;
	} 
}

function lameFeature() {
    var d = new Date()
    var time = d.getHours();
    if (time < 12) 
    {
        document.write("Goedemorgen");
    }
    else if (time >= 12 &&time < 18)
    {
        document.write("Goedemiddag");
    }
    else if (time >= 18)
    {
        document.write("Goedenavond");
    }
}

function changeCity() {
    box = document.getElementById("cityselector");
	destination = box.options[box.selectedIndex].value;
	if (destination) location.href = destination;
}

function openWindow(documentName,documentTitle) {
  window.open(documentName, documentTitle, "width=800,height=600,center,toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=0,copyhistory=0");
}

function zoekStraat() {
	document.getElementById("ophaaldagresultaat").innerHTML = '';
	var straat = document.getElementById("zoekstraat").value;
	document.getElementById("zoekresultaten").innerHTML = '<select name="project" size="10" style="width: 240px;"><option>Een moment geduld a.u.b.</option><option>De resultaten worden opgehaald.</option></select>';
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = zoekStraatChange;
	xmlHttp.open("GET", "/extra/straten.php?action=straat&straatnaam="+straat, true);
	xmlHttp.send(null);
}

function zoekStraatChange() {
	if (xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			document.getElementById("zoekresultaten").innerHTML = xmlHttp.responseText;
			//document.getElementById("spinner").style.display = "none";
		}
	}
}
function getOphaalDag() {
	//var straat_id = document.getElementById("checkstraat").options[document.getElementById("checkstraat").selectedIndex].value;
	var straat_id = document.getElementById("checkstraat").value;
	document.getElementById("ophaaldagresultaat").innerHTML = 'Een moment geduld a.u.b.<br />De resultaten worden opgehaald.';
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = getOphaalDagChange;
	xmlHttp.open("GET", "/extra/straten.php?action=dag&id="+straat_id, true);
	xmlHttp.send(null);
}

function getOphaalDagChange() {
	var straat = document.getElementById("checkstraat").options[document.getElementById("checkstraat").selectedIndex].text;
	document.getElementById("locatie").innerHTML = 'Uw locatie: '+ straat + ', Wassenaar';
	if (xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			document.getElementById("ophaaldagresultaat").innerHTML = 'Straat: '+ straat + '<br />' + 'Ophaaldag: ' + xmlHttp.responseText;
			//document.getElementById("spinner").style.display = "none";
		}
	}
}

function checkRedenValue() {
    var optionValue = document.formpas.reden.value;
	if (optionValue == "pas kapot") {
        showPaskapot();
    }
    else if (optionValue == "anders") {
        showFieldAnders();
    }
    else {
        hideFields();
    }

	if (optionValue != "nieuwbouwwoning" && optionValue != 'pas kapot') {
		showRekeningField();
	}
    else {
        hideFields();
    }
}

function showFieldPasnummer() {
    new Effect.BlindDown($('divPasnummer'), { duration: 0.3 });
    document.getElementById('divPaskapot').style.display = 'none';
	document.getElementById('divPasnummer').style.display = 'none';
	document.getElementById('divRekeningnummer').style.display = 'none';
	document.getElementById('divRekeninghouder').style.display = 'none';
}

function showPaskapot() {
    new Effect.BlindDown($('divPaskapot'), { duration: 0.3 });
    document.getElementById('divAnders').style.display = 'none';
    document.getElementById('divPasnummer').style.display = 'none';
	document.getElementById('divRekeningnummer').style.display = 'none';
	document.getElementById('divRekeninghouder').style.display = 'none';
}

function showFieldAnders() {
    new Effect.BlindDown($('divAnders'), { duration: 0.3 });
    document.getElementById('divPasnummer').style.display = 'none';
    document.getElementById('divPaskapot').style.display = 'none';
	document.getElementById('divRekeningnummer').style.display = 'none';
	document.getElementById('divRekeninghouder').style.display = 'none';
}

function showRekeningField() {
	new Effect.BlindDown($('divRekeningnummer'), { duration: 0.3 });
	new Effect.BlindDown($('divRekeninghouder'), { duration: 0.3 });
    document.getElementById('divPaskapot').style.display = 'none';
	document.getElementById('divPasnummer').style.display = 'none';
	document.getElementById('divAnders').style.display = 'none';
}

function hideFields() {
    document.getElementById('divPasnummer').style.display = 'none';
    document.getElementById('divAnders').style.display = 'none';
	document.getElementById('divRekeningnummer').style.display = 'none';
	document.getElementById('divRekeninghouder').style.display = 'none';
	document.getElementById('divPaskapot').style.display = 'none';
}

function deleteGrofvuilItem(element) {
	$('input'+element.id).remove();
	$(element.id).remove();
}

function addGrofvuilItem(element) {
	var text = element.options[element.options.selectedIndex].text;
	var val = element.options[element.options.selectedIndex].value;
	$('planneritemslist').insert('<li id="gvitem'+val+'" onclick="deleteGrofvuilItem(this)">'+text+'</li>');
	$('planneritemslist').insert('<input type="hidden" name="gvitem[]" id="inputgvitem'+val+'" value="'+val+'---'+text+'"/>');
	element.options.selectedIndex = 0;
}

intg = 2;
jQuery(document).ready(function(){
	jQuery('.morefiles').click(function(){
		jQuery('.morefiles').before('<input type="file" name="attachement'+intg+'" />');
		intg+=1;
		return false;
	});
	jQuery('#middle_content table.lightbox a').lightBox();
});