var xmlhttp = createXMLHTTP();

/*Create an xmlHTTP object*/
function createXMLHTTP() {
	var XMLHttp = false;
	try {
		XMLHttp = new XMLHttpRequest();
	} catch (e) {
		try {
			XMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Error creating XMLHttpRepuest, your browser is too old.");
				XMLHttp = false;
			}
		}
	}
	return XMLHttp;
}

/*Manages connection status and server response*/
function xmlRequest() {
	if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
			try {
				response = xmlhttp.responseText;
				alert (response);
			} catch(e) {
				alert("Error reading server response: \n" + e.toString());
			}
		} else {
			alert("Problem retrieving the data.");
		}
	}
	else{
		alert("Problem retrieving the data: \n"  + e.toString());
	}
}

function onloadControl(m,c,subContent,memWalk){
	mButtonControl(m,c,subContent);
	if(memWalk=="1") memWalkpopup(m);
}

/*Calls a popup for the memory walk if set to active*/
function memWalkpopup(m,memWalk){
	if (m=="mstart")
	{ 
		MemoryWalk = window.open("http://www.alzheimer.lu/userFiles/image/memWalk.jpg", "MemoryWalk", "location=0,status=0,scrollbars=0,width=800,height=600,top=100,left=50");
 		MemoryWalk.focus();
	}
}

/*Called by the body-tag onload to set the button properties.*/
function mButtonControl(m,c,subContent){
try{
	/*to evade that you mix menus and contents in case they got the same id // ALA color code:98a057 */
	if (m=="mstart") document.getElementById("start").style.backgroundColor = "#d6ffb1";
	if (m=="mbiblio") document.getElementById("biblio").style.backgroundColor = "#d6ffb1";
	document.getElementById(m).style.backgroundColor = "#d6ffb1";
	document.getElementById(m).style.color  = "#FFFFFF";
	document.getElementById(c).style.backgroundColor = "#d6ffb1";
	document.getElementById(c).style.color  = "#FFFFFF";
	/*display is the id of the subcontent of a content which is default set to unvisible. 
	Like the content with id=1 has a subcontent with id=sub1.*/
	document.getElementById(subContent).style.display = "block";
}catch(exception){}
}

/*Called by the contact form, to check if the user has filled out all fields*/
function checkContactMailForm(myform){
	var check = true;
	
	if(myform.name.value =='') {check=false; myform.name.style.background = "#FFFF00";} 
		else {check=true; myform.name.style.background = "#FFFFFF";}
	
	if(myform.email.value =='') {check=false; myform.email.style.background = "#FFFF00";} 
		else {check=true; myform.email.style.background = "#FFFFFF";}
	
	if(myform.message.value =='') {check=false; myform.message.style.background = "#FFFF00";} 
		else {check=true; myform.message.style.background = "#FFFFFF";}
	
	if(myform.email.value.match("@") == null) {check=false; myform.email.style.background = "#FFFF00";} 
		else {check=true; myform.email.style.background = "#FFFFFF";}
		
	return check;
}

/*Check the input from the guestbook*/
function checkGBform(myform){
	var check = false;
	/* dont work ??? but why??? BuG!! checked by PHP
	if(myform.spamProtectRandom.value == myform.spamProtect.value)
		{check=true; myform.spamProtect.style.background = "#FFFFFF";}
		else{check=false; myform.spamProtect.style.background="#FFFF00";}
	*/
		
	if(myform.name.value =='') {check=false; myform.name.style.background = "#FFFF00";} 
		else {check=true; myform.name.style.background = "#FFFFFF";}
	
	if(myform.email.value =='') {check=false; myform.email.style.background = "#FFFF00";} 
		else {check=true; myform.email.style.background = "#FFFFFF";}
	
	if(myform.message.value =='') {check=false; myform.message.style.background = "#FFFF00";} 
		else {check=true; myform.message.style.background = "#FFFFFF";}
	
	if(myform.email.value.match("@") == null) {check=false; myform.email.style.background = "#FFFF00";} 
		else {check=true; myform.email.style.background = "#FFFFFF";}
		
	return check;
}

//enlage pictures from galleria and opens them in a new window
function enlargePic(source){
	var newImg = new Image();
	newImg.src = source;
	var h = newImg.height;
	var w = newImg.width;
	var newWindow = null;
	newWindow = open(source,'Picture Viewer','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no,width='+w+',height='+h+''); 
	newWindow.focus();
}


//Set display:none in CSS to block in order to expand sub menu, used in navigation_sub.php
function showSubMenu(id){
	var temp = "sub"+id;
	try{
		if (document.getElementById(temp).style.display == "block") document.getElementById(temp).style.display = "none";
		else document.getElementById(temp).style.display = "block";
	} catch(e){}
}

/*Add/remove a new email address into the newsletter list with input check*/
function newsletter(id,lang){
	var email = document.getElementById(id).value;
	var checked = false;	
	if((email.match("@") != null)) checked = true;
	if(checked)
	{
		var serverAddress = "php_scripts/newsletter.php?email="+email+"&lang="+lang;
		if(xmlhttp)
		{
			try {			
				xmlhttp.open("GET", serverAddress, true);
				xmlhttp.onreadystatechange = xmlRequest;
				xmlhttp.send(null);
			} catch(e) {
				alert("Cannot connect to server.\n");
			}
		}
		Refresh();
		document.getElementById(id).value = "";
	}
	else 
	{
		if (lang=="fr") alert("Veuillez entrer une adresse email.");	
		else alert("Bitte geben Sie eine Email Adresse an.");	
	}
}