//Change/Toggle the button image, menuId: Id of the menu; type: 0(Normal button) or 1(Toggled button)
function menuBtnSwap(menuId, type){
	if(type) {
		$id(menuId).className = "menuBtnPressed";
	}
}

function $id(id){
	return document.getElementById(id);	
}

function contactSend(){
	var name = $id("name").value;
	var company = $id("company").value;
	var adres = $id("adres").value;
	var postcode = $id("postcode").value;
	var city = $id("city").value;
	var email = $id("email").value;
	var subject = $id("subject").value;
	var message = $id("message").value;
	var botCheck = $id("botCheck").value;
	
	// BotCheck
	if(botCheck != "") {
		return;
	}

	//Ajax request to process.php
	var xmlHttp = (window.XMLHttpRequest)? new XMLHttpRequest(): new ActiveXObject("MSXML2.XMLHTTP");
	if(xmlHttp == null){
		//normal post submit
		$id("contactForm").submit();
	}
	else{ 
		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState == 4){ 
				if(xmlHttp.status == 200){
					//Reset fields
					$id("nameError").innerHTML =  "";
					$id("messageError").innerHTML = "";
					$id("subjectError").innerHTML = "";
					$id("emailError").innerHTML = "";
					$id("companyError").innerHTML = "";
					$id("adresError").innerHTML = "";
					$id("postcodeError").innerHTML = "";
					$id("cityError").innerHTML = "";					
				   	if(xmlHttp.responseText){
						//Errors
						var data = eval('(' + xmlHttp.responseText + ')');
					  	if(data[0]){
							$id("nameError").innerHTML = "U moet een naam invullen"; 
					   	}
					   	if(data[1]){
					   		$id("messageError").innerHTML = "U moet een bericht schrijven";					
						}						
					   	if(data[2]){
					   		$id("subjectError").innerHTML = "U moet een onderwerp kiezen";
					   	}
					   	if(data[3]){
					   		$id("emailError").innerHTML = "U moet een e-mail adres invullen";
					   	}						
					   	if(data[4]){
					   		$id("emailError").innerHTML = "U moet een geldig e-mail adres invullen";
					   	}
						if(data[5]){
							$id("companyError").innerHTML = "U moet uw bedrijfs naam invullen";
						}
						if(data[6]){
							$id("adresError").innerHTML = "U moet een adres invullen";
						}
						if(data[7]){
							$id("postcodeError").innerHTML = "U moet een postcode invullen";
						}
						if(data[8]){
							$id("cityError").innerHTML = "U moet een plaats invullen";
						}
						if(data[9]){
							$id("postcodeError").innerHTML = "U moet een geldige postcode invullen";
						}
				   	}
				   	else{
					   	//Message send
						$id("nameError").innerHTML = "<font color='00FF00'><b>Uw bericht is verstuurd</b></font>"; 
						$id("contactForm").reset();
				   	}
			   	}
			   	else{
				   	//Try normal post submit
				   	$id("contactForm").submit();
			   	} 
		   	} 
		}
		xmlHttp.open("POST","scripts/process.php",true);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
		xmlHttp.send("&name=" + name + "&company=" + company + "&adres=" + adres + "&postcode=" + postcode + "&city=" + city + "&email=" + email + "&subject=" + subject + "&message=" + message);
	}
}

function openContactForm(){
	$id("companyRow").style.display = "block";
	$id("openCloseImgContainer").innerHTML = '<img src="images/min.jpg" />';
}

function closeContactForm(){
	$id("companyRow").style.display = "none";
	$id("openCloseImgContainer").innerHTML = '<img src="images/plus.jpg" />';
}

function show(id){
	if(id == "nieuwsDiv"){
		$id(id).style.display = "block";
		$id("eventsDiv").style.display = "none";
	}
	else if(id == "eventsDiv"){
		$id(id).style.display = "block";
		$id("nieuwsDiv").style.display = "none";	
	}
}

function CMLogin(){
	var username = $("#username").val();
	var password = $("#password").val();
	$.get("scripts/login.php", {"username": username, "password": password}, function(data){
		if(data == 1){
			window.location="cm.php";	
		}
		else{
			$("#loginError").html("Uw gebruikersnaam en/of wachtwoord is niet correct");
		}
	});
}

function CMLogout(){
	$.post("scripts/logout.php");
}
