function validate(){
	/*******validate mail********/
	var mail_adress = document.getElementById("mail").value;
	var mail_pattern = /^[\x20-\x2D\x2F-\x7E]+(\.[\x20-\x2D\x2F-\x7E]+)*@(([a-z0-9]([-a-z0-9]*[a-z0-9]+)?){1,63}\.)+[a-z0-9]{2,6}$/;
	if(!mail_adress.match(mail_pattern)) {
		alert("Please enter a valid e-mail adress.");
		document.getElementById("mail_text").style.color = "red";
		document.getElementById("mail").focus();
		return false;
	} else {
		document.getElementById("mail_text").style.color = "black";

		/*******validate captcha********/
		ajax_validate_captcha();
		return false;
	}
}

var http = new getHTTPObject();

function ajax_validate_captcha(){
	var captcha_code = document.getElementById("captcha_code").value;
	var url = "ajax_captcha_backend.php?c="+captcha_code;
	http.open("GET", url, true);
	http.onreadystatechange = handleCaptchaResponse;	http.send(null);}

function handleCaptchaResponse(){
	if(http.readyState == 4){
		if (http.responseText=="invalid") {
			alert("Please fill in the letters exactly as they apear in the image.");
			//ladda ny bild
			document.getElementById('captcha').src = 'securimage/securimage_show.php?' + Math.random();
			//fokus till captch
			document.getElementById("captcha_code").focus();
			document.getElementById("captcha_text").style.color = "red";
		} else {
			//skicka formulär
			document.forms[document.forms.length-1].submit();
		}
	}
}


function getHTTPObject(){	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(E){
				xmlhttp = false;
			}
		}
	@else
		xmlhttp = false;
	@end @*/
	if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
		try {
			xmlhttp = new XMLHttpRequest();
		}catch(e){
			xmlhttp = false;
		}
	}
	return xmlhttp;
}
