function errorText(id, frontside)
{
	var elem;
	var txt;

	if( document.getElementById  && (elem=document.getElementById(id)) ) {
  		if( !elem.firstChild )
   			elem.appendChild( document.createTextNode( txt ) );
   		else if (elem.firstChild.data.indexOf('*') >= 0) {
   		}
   		else if (frontside == true)
   			elem.firstChild.data = "* " + elem.firstChild.data;
   		else
   			elem.firstChild.data += " *";
 	}
 	elem.style.color = "#ff0000";
	return false; 
}


var http_request = false;
function makeRequest(url) {
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			// set type accordingly to anticipated content type
			//http_request.overrideMimeType('text/xml');
			http_request.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = alertContents;
	http_request.open('GET', url, true);
	http_request.send(null);
}

function alertContents() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			//alert(http_request.responseText);
			result = http_request.responseText;
				if (result > 0) {
					document.getElementById('laskuri-tulos').innerHTML = result.replace(/\./, ",") + " &euro;";
				}
				else {
					document.getElementById('laskuri-tulos').innerHTML = "Virhe!";
					
					if (result.indexOf('neliot') >= 0) { alert("Tarkista neliömäärä! Vain numeroita! (0,01-9998,99)"); }
					else if (result.indexOf('vuosi') >= 0) { alert("Tarkista rakennusvuosi! Vain numeroita!"); }
					//else if (result.indexOf('auto_molemmat') >= 0) { alert("Valitse lämmin tai kylmä autotalli- tai autohallipaikka! Älä valitse molempia."); }
					//if (result.indexOf('neliot') >= 0 || result.indexOf('vuosi') >= 0 || result.indexOf('auto_molemmat') >= 0) { ); }
				}
		} else {
			alert('There was a problem with the request.');
		}
	}
}

function get() {
	var getstr = "?";
	
	var sijainti1 = document.getElementById('sijainti1');
	var sijainti2 = document.getElementById('sijainti2');
	var lammitys1 = document.getElementById('lammitys1');
	var lammitys2 = document.getElementById('lammitys2');
	var neliot = document.getElementById('neliot');
	var vuosi = document.getElementById('vuosi');
	var kayttosahko1 = document.getElementById('kayttosahko1');
	var auto_lammin = document.getElementById('auto_lammin');
	var auto_kylma = document.getElementById('auto_kylma');
	var sijainti1 = document.getElementById('sijainti1');
	
	var sijainti_arvo = 0;
	if (sijainti1.checked == true) { sijainti_arvo = 1; }
	if (sijainti2.checked == true) { sijainti_arvo = 2; }
	if (sijainti1.checked == true && sijainti2.checked == true) { sijainti_arvo = 0; }

	var lammitys_arvo = 0;
	if (lammitys1.checked == true) { lammitys_arvo = 1; }
	if (lammitys2.checked == true) { lammitys_arvo = 2; }
	if (lammitys1.checked == true && lammitys2.checked == true) { lammitys_arvo = 0; }
	
	if (neliot.value != "") { getstr += "neliot=" + neliot.value + "&"; }
	if (vuosi.value != "") { getstr += "vuosi=" + vuosi.value + "&"; }
	getstr += "sijainti=" + sijainti_arvo + "&";
	getstr += "lammitys=" + lammitys_arvo + "&";
	if (kayttosahko1.checked) { getstr += "kayttosahko=1&"; }
	if (auto_lammin.checked) { getstr += "auto_lammin=1&"; }
	if (auto_kylma.checked) { getstr += "auto_kylma=1&"; }
	getstr += "send=Lähetä";

	var url = '/wp-content/themes/vuokraturva/asuntoetulaskuri.php' + getstr;
	makeRequest(url);
	//alert(getstr);
}

