/*
	explanation: Excecute a function after page is fully loaded
*/
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();}
    }
}

function parseXML(url){
	var xmlDoc = null;

	try { //Internet Explorer	
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	}
	catch(e){
		try { //Firefox, Mozilla, Opera, etc.
			xmlDoc=document.implementation.createDocument("","",null);
		}
		catch(e){
			alert(e.message);
			return;
		}
	}
	xmlDoc.async=false;
	xmlDoc.load(url);
	return xmlDoc;
}

function initStart(){
	writeUserMail(document.form_login.userMail);
}

var randomN = null;
function showQuotes(){
	var xmlDoc = parseXML('xml/quotes.xml');
	x=xmlDoc.documentElement.childNodes;
	var numQuotes = 0;
	for (i=0;i<x.length;i++){ 
		if (x[i].nodeType==1){
			numQuotes++;
		} 
	}

	newRandomN = Math.floor(Math.random() * (numQuotes))
	while(newRandomN == randomN){
		newRandomN = Math.floor(Math.random() * (numQuotes))
	}
	randomN = newRandomN;
	qTitle = xmlDoc.getElementsByTagName("quoteTitle")[randomN].childNodes[0].nodeValue;
	qMessage = xmlDoc.getElementsByTagName("quoteMessage")[randomN].childNodes[0].nodeValue;
	$("quotes").innerHTML = 
		"<a href=\"/deltapi/quotes.php\" title=\"Lees onze klantverhalen\"><h1>"+ qTitle +"</h1><p>\""+ qMessage +"\"</p></a>";
	new Effect.Opacity('quotes', {duration:2.0, from:0.1, to:1.0});
	setTimeout("fadeQuote()",5000)
}

function fadeQuote(){
	new Effect.Opacity('quotes', {duration:2.0, from:1.0, to:0.0});	
	setTimeout("showQuotes()",2000)
}

/*
	Opens and closes elements in TOC
*/
function toggleToc(objId, targetId){
	targetObj = $(targetId);
	if(targetObj.style.display == "none")	{
//		objId.className = "open";
		objId.parentNode.setAttribute("class","open");
        new Effect.BlindDown(targetObj, {duration: 0.5})
		//$(targetObj).style.display = "block";
	}
	else{
//		objId.className = "closed";	
		objId.parentNode.setAttribute("class","closed");
		new Effect.BlindUp(targetObj, {duration: 0.5})
		//$(targetObj).style.display = "none";
	}
}

/*
	Sets the currentPage to active + Writes the pageTitle
*/
function checkActivePage(){
	
	displayRandomImage();
	
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	
	var pageTitle = sPage.substring(0, sPage.lastIndexOf("."));
	var pageTitle = pageTitle.replace(/\_/g," ");
	var pageTitle = UCFirst(pageTitle);
	
	//var pageTitleObj = $("pageTitle");			pageTitleObj.innerHTML = pageTitle;
	var pageBCTitleObj = $("pageBCTitle");		pageBCTitleObj.innerHTML = pageTitle;
	
	var allOl = document.getElementsByTagName("a");

	for (var i=0;i<allOl.length;i++){
		//if(allOl[i].getAttribute("href") == sPage){		// Doesn't work in IE7
		if(allOl[i].href == window.location && allOl[i].parentNode.parentNode.parentNode.parentNode.className == 'tocMenu'){
			//allOl[i].setAttribute("class","active");		// FF style (doens't work in IE7)
			allOl[i].className = 'active';					// IE sollution to set className
			parentObj = allOl[i].parentNode.parentNode;		// get Object of OL
			parentObj.style.display = "block";				// set OL to visible
			parentObj = allOl[i].parentNode.parentNode.parentNode;	// get Object of Li
			parentObj.className = 'open';
		}
	}
}

function displayRandomImage(){
	var allImg = new Array();
	allImg[0] = "foto-fu-dp6-1.jpg";
	allImg[1] = "foto-fu-dp6-2.jpg";
	allImg[2] = "foto-fu-dp6-3.jpg";
	allImg[3] = "foto-fu-dvdt-1.jpg";
	allImg[4] = "foto-fu-dvdt-2.jpg";
	allImg[5] = "foto-fu-dvdt-3.jpg";
	allImg[6] = "foto-fu-eh-1.jpg";
	allImg[7] = "foto-fu-eh-2.jpg";
	allImg[8] = "foto-fu-eh-3.jpg";
	allImg[9] = "foto-fu-inbo-1.jpg";
	allImg[10] = "foto-fu-inbo-2.jpg";
	allImg[11] = "foto-fu-inbo-3.jpg";
	allImg[12] = "foto-fu-inbo-4.jpg";
	allImg[13] = "foto-fu-ks-1.jpg";
	allImg[14] = "foto-fu-ks-2.jpg";
	allImg[15] = "foto-fu-ks-3.jpg";
	allImg[16] = "foto-fu-mcn-1.jpg";
	allImg[17] = "foto-fu-mcn-2.jpg";
	allImg[18] = "foto-fu-va-1.jpg";
	allImg[19] = "foto-fu-va-2.jpg";
	allImg[20] = "foto-fu-va-3.jpg";
	allImg[21] = "foto-fu-vdl-2.jpg";
	allImg[22] = "foto-fu-vdl-3.jpg";

	
	var i = Math.floor(Math.random() * allImg.length);
	if($('pagePhoto').src.indexOf(".jpg") == -1) $('pagePhoto').src = "../media/"+allImg[i];
}

/*
	Sets the first character of a String to uppercase
*/
function UCFirst(str){
   // split string
   firstChar = str.substring(0,1);
   remainChar = str.substring(1);

   // convert case
   firstChar = firstChar.toUpperCase(); 

   return firstChar + remainChar
}

/*
	Show newsitems
*/

function showNews(page){
	var xmlDoc = document.load('http://www.engineeringnow.nl/rss/rss.asp?iNTypeID=139');
	alert(xmlDoc);
}

function confirmMe(which){
	if(which == "mail")	var txt = "Aanvraag opnieuw ontvangen\n----------------------------\nWeet u zeker dat u de aanvraag opnieuw, per e-mail, wilt ontvangen? Klik op 'OK' om te bevestigen";
	if(which == "user")	var txt = "Gebruiker verwijderen\n----------------------------\nWeet u zeker dat u deze gebruiker wilt verwijderen? Klik op 'OK' om te bevestigen of 'Annuleren' om terug te keren.";

	if(confirm(txt)) 	return true
	else			return false		
}

function saveUserMail(userMail){
	//createCookie('userMail',userMail,365);
}

function writeUserMail(obj){
	//if(readCookie('userMail')) obj.value = readCookie('userMail');
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function showQuote(nr){
	var objId = "id_"+nr
	var obj = $(objId)
	if(obj.style.display == "block"){
		new Effect.Opacity(objId, {duration:1.0, from:1.0, to:0.0});	
		obj.style.display = 'none';
	}
	else {
		new Effect.Opacity(objId, {duration:0.5, from:0, to:1.0});	
		obj.style.display = 'block';
	}
}
