function addLoadListener(fn){
    if(typeof window.addEventListener !='undefined')    window.addEventListener('load',fn,false);
    else if(typeof document.addEventListener !='undefined')    document.addEventListener('load',fn,false);
    else if(typeof window.attachEvent !='undefined')    window.attachEvent('onload',fn);
    else{
        var oldfn=window.onload
        if(typeof window.onload !='function')    window.onload=fn;
        else    window.onload=function(){oldfn();fn();}
    }
}

// content ophalen
//addLoadListener(function() {getContent(0)});

//alle checkboxen verzamelen!
var allCbs = [];
var allRadios = [];
var allInputs = document.getElementsByTagName('input');
addLoadListener(getAllCheckboxes);
addLoadListener(getAllRadios);

//externe links voorzien van target attribute.
addLoadListener(externalLinks);

// checken of kleur trefwoordmatch aangepast moet worden
addLoadListener(trefwoodColorChecker);

//actie van forms aanpassen indien op test of develop-server
addLoadListener(formActionToggler);

var allTextInputs = ['qstTrefwoord', 'qmaTrefwoord', 'qcsTrefwoord'];

//actie van forms aanpassen indien op test of develop-server voor test/develop-doeleinden
function formActionToggler() {
	theForms = $$('form');
	
	if((location.href.indexOf('test.qompas')>=0) || (location.href.indexOf('develop.qompas')>=0)) {
		for(f=0; f < theForms.length; f++) {
			var oldAction = theForms[f].action.substring(7);
			var newAction = '';

			if(location.href.indexOf('test.qompas')>=0) {
				newAction = 'http://test.' + oldAction;
			}else {
				newAction = 'http://develop.' + oldAction;
			}
			theForms[f].action = newAction;		
		}
	}
}

/*
check of er in de trefwoordvelden iets anders dan de defaultwaarde 'trefwoord' staat ingevuld.
een aantal browsers vult een eerdere waarde in op basis van sessie-info.
als dat het geval is, gaan we niet uit van de defaultkleur grijs, maar veranderen we de textkleur in zwart.
*/
function trefwoodColorChecker() {
	for(t=0; t < allTextInputs.length; t++) {
		if($(allTextInputs[t]).value!='trefwoord') {
			$(allTextInputs[t]).className = 'trefwoordAfter';
		}
	}
}



function externalLinks() {
	if (!document.getElementsByTagName) return;
 var theAnchors = document.getElementsByTagName("a");
 for (var i=0; i<theAnchors.length; i++) {
   var theAnchor = theAnchors[i];
	 if(theAnchor.getAttribute("rel")) {
		if ((theAnchor.getAttribute("href")) && (theAnchor.getAttribute("rel").indexOf("external")>=0))
			theAnchor.target = "_blank";
		 }	 
 }
}


function submitForm(tool, type) {
	// trefwoord clearen
	clearTrefwoord($(tool+'Trefwoord'));
	
	// bij qcs resulttype bepalen
	if(tool=='qcs' && type!='') {
		var	resulttypeArr = ['bedrijf', 'startfunctie'];	
		$('qcsResulttype').value=resulttypeArr[type];
	}
	
	// form submitten
	$(tool+'Form').submit();
}


function resetForm(tool) {
	if(tool=='qma') {
		$('qmaOplSelector').innerHTML = 'Wat is je huidige opleiding?';
	}
	if(tool=='qcs') {
		$('qcsOplSelector').innerHTML = 'Wat is je vooropleiding?';
	}
		
	$(tool+'Form').reset();

	// trefwoord leegmaken
	$(tool + 'Trefwoord').value='';

}


function clearTrefwoord(obj) {
	if(obj.value=='trefwoord') {
		obj.value = '';
	}
	toggleClassName(obj, 'trefwoordAfter');
}


function toggleChild(obj) {
	var elId = obj.id.split('-');
	var childObj  = $(elId[0] + '-child-' + elId[2]);

	toggleVisibility(childObj);
}

function toggleVisibility(obj, withId) {
	
	if(withId){ // we hebben een obj-id ipv een obj gekregen van de aanroep.
		obj=$(obj);
	}

	if(obj.className=='visibleContent') {
		toggleClassName(obj, 'hiddenContent');
	}
	else {
			toggleClassName(obj, 'visibleContent');
	}
}



function navigateTab(cur) {
	//cur = de tab die zichtbaar wordt

	var numTabs = 3;
	var theTab;

	// alle andere tabs uitzetten
	for(i=1; i <= numTabs; i++) {
		if(i!=(cur)) {
				// tab content hiden
				var theContent = $('content'+i);
					toggleClassName(theContent, 'hiddenContent');
					// niet doen ivm IE:
					//new Effect.Fade(theContent, {duration: 0.5});	
				
				
				
				//tab unhighlight
				theTab = $('tab'+i);
				toggleClassName(theTab, '');
			}
	}

	//cur tab content aanzetten
	var theNewContent = $('content'+cur);
		toggleClassName(theNewContent, 'visibleContent');
		// niet doen ivm IE:
		//new Effect.Appear(theNewContent, {duration: 0.5});	
	

	//cur tab highlight
	theTab = $('tab'+cur);
	toggleClassName(theTab, 'currentTab');
	
	//headerkleur aanpassen
	theHeader = $('header');
	toggleClassName(theHeader, 'header' + cur);
}



function toggleClassName(obj, newClassName){
	obj.className = newClassName;
	}


function getAllCheckboxes() {
	for(i=0; i<allInputs.length; i++) {
		if(allInputs[i].type=='checkbox') {
			allCbs.length++;			
			allCbs[allCbs.length-1] = allInputs[i];
		}
	}
}

function getAllRadios() {
	for(i=0; i<allInputs.length; i++) {
		if(allInputs[i].type=='radio') {
			allRadios.length++;			
			allRadios[allRadios.length-1] = allInputs[i];
		}
	}
}

function listenParentChilds(obj) {
	var type;

	if(obj.id.indexOf('-parent-')>=0) {
		type='parent';
	}else {
		type='child';
	}
	
	var parentId = obj.id.split('-')[2];
	allChilds = getAllchilds(parentId);
	
	// wordt de parent aan/uit gezet, dan alle childs ook aan/uit.
	if(type=='parent'){
		if(obj.checked==true) {
			toggleAllChilds(allChilds, 'on');
		}
		else{
			toggleAllChilds(allChilds, 'off');
		}
	}
	if(type=='child'){
		// wat is het bijbehorende parent?
		parentId = obj.id.split('-')[0] + '-parent-' + obj.id.split('-')[2];

		// als 1 child wordt uitgezet, dan de parent ook uit.
		if(!obj.checked) {
			$(parentId).checked=false;
		}

		// als alle child selected worden, dan de parent ook selected.
		var allChildsSelected = checkAllChildsSelected(allChilds);
		if(allChildsSelected) {
			$(parentId).checked=true;
		}
		
	}

		


}

function checkAllChildsSelected(arr) {
	var result = false;
	var itemsChecked = [];
	for(a=0; a < arr.length; a++) {
		if(arr[a].checked) {
			itemsChecked.length++;
			itemsChecked[itemsChecked.length-1] = arr[a]
		}
	}
	// zijn alle items unchecked?
	if(arr.length==itemsChecked.length) {
		result = true;
	}
	return result;
}


function toggleAllChilds(allChilds, state) {
	for(i=0; i < allChilds.length; i++) {
		if(state=='on') {
			allChilds[i].checked=true;
		}
		else{
			allChilds[i].checked=false;
		}
	}
}

function getAllchilds(parentId){
	returnArr = [];
	for(i=0; i<allCbs.length; i++) {
		if((allCbs[i].id.indexOf(parentId)>=0) && (allCbs[i].id.indexOf('-child-')>=0))		{
			returnArr.length++;
			returnArr[returnArr.length-1] = allCbs[i];
		}	
	}
	return returnArr;
}


function updateSelector(obj, target, value, overruleObj)  {
	var maxLength = 29;
	if(obj.checked || overruleObj)  {
		if(value.length > maxLength) {
			$(target).innerHTML = value.substring(0,maxLength) + "...";
		}
		else {
			$(target).innerHTML = value;
		}
	}
}

function deselectRadios(askmatchId) {
	for(r=0; r < allRadios.length; r++)	 {
		if(allRadios[r].id.indexOf(askmatchId)>=0) {
			allRadios[r].checked=false;
		}
	}
	if(askmatchId=='206') {
		updateSelector('', 'qmaOplSelector', 'Wat is je huidige opleiding?', true);
	}
	else if (askmatchId=='600') {
		updateSelector('', 'qcsOplSelector', 'Wat is je vooropleiding?', true);
	}
	
}
