var xmlhttpmail;

function send_mail() {
		namemail = document.getElementById('cname').value;
		nametitle = document.getElementById('ctitle').value;
		namecompany = document.getElementById('ccompany').value;
		infoemail = document.getElementById('cinfoemail').value;
		infophone = document.getElementById('cinfophone').value;
		mailmessage = document.getElementById('cmessage').value;
		frommail = document.getElementById('from').value;
		//tomail = document.getElementById('to').value;
		referrer = document.referrer;
		var refexp= new RegExp("ainia.fi");
		if(!referrer.match(refexp)) {
			referrer = "http://www.ainia.fi/new";
		}
		topic = window.location.toString();
//		if(namemail.length < 3 || nametitle.length < 3 || namecompany.length < 3 || infoemail.length < 3 || infophone.length < 3 || mailmessage.length < 3) {
//			alert("Täytä kaikki kentät");
//		} else {
		//alert(topic);
		//alert(name + " " + sourcemail + " " + infomail + " " + descriptionmail + " " + frommail + " " + tomail);
		loadXMLDocMail('/custom/contact.php?topic='+topic+'&name='+namemail+'&nametitle='+nametitle+'&company='+namecompany+'&infoemail='+infoemail+'&infophone='+infophone+'&from='+frommail+'&message='+mailmessage+'&referrer='+referrer);
}

function loadXMLDocMail(url) {
        xmlhttpmail=null;
        // code for Mozilla, etc.
        if (window.XMLHttpRequest) {
                xmlhttpmail=new XMLHttpRequest();
        }
        // code for IE
        else if (window.ActiveXObject) {
                xmlhttpmail=new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (xmlhttpmail!=null) {
                xmlhttpmail.onreadystatechange=state_Mail;
                xmlhttpmail.open("GET",url,true);
                xmlhttpmail.send(null);
        }
        else {
                alert("Your browser does not support XMLHTTP.");
        }
}

function state_Mail() {
        // if xmlhttprep shows "loaded"
        if (xmlhttpmail.readyState==4) {
                // if "OK"
                if (xmlhttpmail.status==200) {
					document.getElementById('content').innerHTML=xmlhttpmail.responseText;
					setTimeout('delayer()', 5000);
			   }
                else {
                        alert("Problem retrieving data:" + xmlhttpmail.statusText);
                }
        }
}
function delayer(){
   window.location=referrer;
}
