function openDialog(inName, inURL, inWidth, inHeight, inSizable)
{
	var left = Math.round((window.screen.availWidth  - inWidth ) / 2);
	var top  = Math.round((window.screen.availHeight - inHeight) / 2);
	var windowString = "left="+left+",top="+top+",width="+inWidth+",height="+inHeight+",scrollbars=yes,toolbar=no,location=no,resizable=yes";
	window.open(inURL,'iets',windowString);
}

String.prototype.left = function left(n)
{
	if (n <= 0) return "";
	else if (n > this.length) return this;
	else return this.substring(0,n);
}

String.prototype.right = function right(n)
{
	if (n <= 0) return "";
	else if (n > this.length) return this;
	else return this.substring(this.length, this.length - n);
}

function loadXML(xmlFile)
{
	
	if(document.implementation && document.implementation.createDocument)
	{
		var myXMLHTTPRequest = new XMLHttpRequest();
		myXMLHTTPRequest.open("GET", xmlFile, false);
		myXMLHTTPRequest.send(null);
		
		return myXMLHTTPRequest.responseXML;
	}
	else if(window.ActiveXObject)
	{
		xml = new ActiveXObject("MSXML2.DOMDocument");
		xml.async = false;
		xml.load(xmlFile);
		
		return xml;
	}
}


function runTransform(xmlSource,xslSource,elementID)
{
	if(document.implementation && document.implementation.createDocument)
	{
		//Mozilla
		var xsltProcessor = new XSLTProcessor();
		xsltProcessor.importStylesheet(xslSource);
		
		var resultDocument = xsltProcessor.transformToFragment(xmlSource, document);
		
		if (elementID) document.getElementById(elementID).appendChild(resultDocument);
		else	return resultDocument;
		
	}
	else if(window.ActiveXObject)
	{
		//Internet Explorer
		
		if (elementID) document.getElementById(elementID).innerHTML = xmlSource.transformNode(xslSource);
		else return xmlSource.transformNode(xslSource);
		
		//return xmlSource.transformNode(xslSource);
	}
	else
	{
		alert("AJAx functionality not supported...");
		return "error";
	}
}


function loadAphorisms()
{
	aphDoc = loadXML(xmlaphorismFile);
	aphXSLDoc = loadXML(xslStylesheet);
	
	aphArray = aphDoc.getElementsByTagName('aphorism');
	document.getElementById("aphorismHolder").style.display = "block";
	
	dummy = aphDoc.getElementsByTagName('aphorisms');
	if (dummy[0].getAttribute("refreshrate") != "") refreshRate = (dummy[0].getAttribute("refreshrate") * 1000);

	
	fadeIn(true);
}


function fadeIn(firstLoad)
{ 
	if (firstLoad)
	{
		do random_num = (Math.floor(Math.random()*aphArray.length)); while (random_num == oldnum);
		oldnum = random_num;		
		
		aphNode = aphArray[random_num];
		
		document.getElementById("aphorism").innerHTML = "";
		runTransform(aphNode,aphXSLDoc,"aphorism");					
	}
	
	if (fadeValue < 10)
	{
		fadeValue+=1;
		document.getElementById("aphorism").style.opacity = fadeValue/10;
		document.getElementById("aphorism").style.filter = 'alpha(opacity=' + fadeValue*10 + ')';
		setTimeout("fadeIn(false)",50); 
	}
	else setTimeout("fadeOut()",refreshRate);
}

function fadeOut()
{ 				
	if (fadeValue > 0)
	{
		fadeValue-=1;
		document.getElementById("aphorism").style.opacity = fadeValue/10;
		document.getElementById("aphorism").style.filter = 'alpha(opacity=' + fadeValue*10 + ')';
		setTimeout("fadeOut()",50); 
	}
	else setTimeout("fadeIn(true)",5);
}

function fadeInMessage()
{
	if (fadeMessageValue < 10)
	{
		fadeMessageValue+=1;
		document.getElementById("message").style.opacity = fadeMessageValue/10;
		document.getElementById("message").style.filter = 'alpha(opacity=' + fadeMessageValue*10 + ')';
		setTimeout("fadeInMessage()",90); 
	}				
}

function loadContent(linkID,isClick)
{
	
	search_string = document.location.search;
	fadeMessageValue = 0;
	
	if ((search_string == "?status=sent") && (!isClick)) linkID = "-2";
	if ((search_string == "?status=sent") && (isClick) && (linkID != "")) document.location.search = "?id=" + linkID;
	if (search_string == "?status=error")
	{
		linkID = -1;
		document.getElementById("errorMessage").innerHTML = "U heeft het formulier niet correct ingevoerd.";
		document.getElementById("errorMessage").style.color = "#FF0000";
	}
	
	
	if (!linkID) 
	{
		if (document.location.search != "")
		{
			varString = document.location.search;
	
			
			linkID = varString.right((varString.length - varString.indexOf("=")) - 1);
		}
		else linkID = "1";
	}
	
	
	if (linkID == "-1") loadEmailform();
	else if (linkID == "-2") loadEmailform(true);
	else if (linkID == "-3") loadReferentList();
	else
	{
		mainCon = document.getElementById("mailContainer");
		mainCon.style.display = "none";
		mailCon = document.getElementById("mainContainer");
		mailCon.style.display = "block";
		document.getElementById("referentsContainer").style.display = "none";
		
		document.getElementById("mainContainer").innerHTML = "";
		cntDoc = loadXML(xmlContentFile);
		
		if (cntDoc.childNodes[1]) xNum=1;
		else xNum=0;
		
		cntDoc.childNodes[xNum].setAttribute("showid",linkID);
	
		runTransform(cntDoc,aphXSLDoc,"mainContainer");	
	}
	
	checkWindowSize();
}

function loadReferentList()
{
	//mainCon = $("mainContainer");
	//mainCon.innerHTML = "Testing";
	document.getElementById("referentsContainer").style.display = "block";
	document.getElementById("mainContainer").style.display = "none";
	document.getElementById("mailContainer").style.display = "none";
}

function loadEmailform(doFade)
{
	mainCon = document.getElementById("mainContainer");
	mainCon.style.display = "none";
	mailCon = document.getElementById("mailContainer");
	mailCon.style.display = "block";
	document.getElementById("referentsContainer").style.display = "none";
	
	if (doFade) 
	{
		//document.getElementById("message").style.display = "block";
		document.getElementById("errorMessage").innerHTML = "Gegevens zijn succesvol verzonden";
		document.getElementById("errorMessage").style.color = "#297f2d";
		fadeInMessage();
	}
}

function sendForm()
{
	status = true;
	if (document.contactForm.inputName.value == "")
	{
		document.contactForm.inputName.style.border = "1px solid #d00202";
		document.getElementById("nameLabel").style.color = "#d00202";
		status = false;
	}
	
	if (document.contactForm.inputCompany.value == "")
	{
		document.contactForm.inputCompany.style.border = "1px solid #d00202";
		document.getElementById("companyLabel").style.color = "#d00202";
		
		status = false;
	}
	
	var emailStr = document.contactForm.inputEmail.value;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	
	if (!(filter.test(emailStr))) 
	{
		status = false;
		document.contactForm.inputEmail.style.border = "1px solid #d00202";
		document.getElementById("emailLabel").style.color = "#d00202";
		document.getElementById("errorMessage").innerHTML = "Sommige velden zijn niet of verkeerd ingevuld."
		document.getElementById("errorMessage").style.display = "block";
	}
  
//	alert(status);
	if (status) document.contactForm.submit();
}

function checkWindowSize()
{
	var dpHeight = document.documentElement.clientHeight;
	var mainTextCon = $("mainContainer");
	var dummyThing = (dpHeight - parseInt($("topPart").getHeight())) - 30;
	mainTextCon.style.height = (dummyThing - 5) + "px";
	
	/*
	text = "height displayport: " + dpHeight;
	text += "\nheight text container: " + mainTextCon.getHeight();
	text += "\ndpHeight - topPart: " + dummyThing;
	alert(text);
	*/
}
