var BODEN = {
	"Parkett":[
		{"bis":999, "Preis":45.00, "Normal":49.00},
		{"bis":20, "Preis":45.00, "Normal":49.00},
		{"bis":10, "Preis":45.00, "Normal":49.00}
	],
	"Laminat":[
		{"bis":999, "Preis":45.00, "Normal":49.00},
		{"bis":20, "Preis":45.00, "Normal":49.00},
		{"bis":10, "Preis":45.00, "Normal":49.00}
	],
	"Faktor":[
		{"Einsatzgebiet":1, "Wert":1},
		{"Einsatzgebiet":2, "Wert":1},
		{"Einsatzgebiet":3, "Wert":1},
		{"Einsatzgebiet":4, "Wert":1},
		{"Einsatzgebiet":5, "Wert":1},
		{"Einsatzgebiet":6, "Wert":1},
		{"Einsatzgebiet":7, "Wert":1},
		{"Einsatzgebiet":8, "Wert":1},
		{"Einsatzgebiet":9, "Wert":1},
		{"Einsatzgebiet":10, "Wert":1},
		{"Einsatzgebiet":11, "Wert":1},
		{"Einsatzgebiet":12, "Wert":1},
		{"Einsatzgebiet":13, "Wert":1},
		{"Einsatzgebiet":14, "Wert":1}
	],
	"Feuchtesp":0,
	"Trittschall":0,
	"Diagonal":3.00,
	"EntfBoden":14.45,
	"EntfLeim":3.00,
	"Sockel":0,
	"Tueren":0,
	"Kleinmenge":0,
	"Offerte":150
	
};

var text = "";
var text_fr = "";
var text_it = "";
var textSet = "";
var textNormal = "";
var option = false;
var total = 0;
var totalNormal = 0;
function rechneTotal(){
 	text = "";
 	text_fr = "";
 	text_it = "";
 	textSet = "";
 	textNormal = "";
 	option = false;
	total = 0;
	totalNormal = 0;
	
	var subtotal = 0;
	var msg = "";
	var z = parseFloat(document.getElementById("Flaeche").innerHTML,10);
	if((z!=0 || document.Form.Offerte[1].checked) && (document.getElementById("PLZ").value != "" || document.getElementById("ObjPLZ").value != "")){ 
	// nur ausführen, wenn eine Fläche und eine Postleitzahl angegeben ist
		var pr = 0;
		var prNormal = 0;
		// Preis für die Standard-Leistungen ermitteln
		if(document.Form.Boden[0].checked){
			if(z<=BODEN.Parkett[0].bis){pr = BODEN.Parkett[0].Preis}
			if(z<=BODEN.Parkett[1].bis){pr = BODEN.Parkett[1].Preis}
			if(z<=BODEN.Parkett[2].bis){pr = BODEN.Parkett[2].Preis}
			if(z<=BODEN.Parkett[0].bis){prNormal = BODEN.Parkett[0].Normal}
			if(z<=BODEN.Parkett[1].bis){prNormal = BODEN.Parkett[1].Normal}
			if(z<=BODEN.Parkett[2].bis){prNormal = BODEN.Parkett[2].Normal}
		}
		if(document.Form.Boden[1].checked){
			if(z<=BODEN.Laminat[0].bis){pr = BODEN.Laminat[0].Preis}
			if(z<=BODEN.Laminat[1].bis){pr = BODEN.Laminat[1].Preis}
			if(z<=BODEN.Laminat[2].bis){pr = BODEN.Laminat[2].Preis}
			if(z<=BODEN.Laminat[0].bis){prNormal = BODEN.Laminat[0].Normal}
			if(z<=BODEN.Laminat[1].bis){prNormal = BODEN.Laminat[1].Normal}
			if(z<=BODEN.Laminat[2].bis){prNormal = BODEN.Laminat[2].Normal}
		}
		if(pr != 0){ 
		// nur weiterrechnen, wenn oben ein Bodenpreis gefunden wurde.
			// Masse errechnen
			var umfang = 0;
			var regexp = /\*/g;
			var anzTueren = 0;
			var s = document.getElementById("Raum1").value.replace(/[^0-9.*]/g, ""); s = s.replace(regexp, "+");s = eval(s);if (!isNaN(s)){umfang += s*2; anzTueren++};
			s = document.getElementById("Raum2").value.replace(/[^0-9.*]/g, ""); s = s.replace(regexp, "+");s = eval(s);if (!isNaN(s)){umfang += s*2; anzTueren++};
			s = document.getElementById("Raum3").value.replace(/[^0-9.*]/g, ""); s = s.replace(regexp, "+");s = eval(s);if (!isNaN(s)){umfang += s*2; anzTueren++};
			s = document.getElementById("Raum4").value.replace(/[^0-9.*]/g, ""); s = s.replace(regexp, "+");s = eval(s);if (!isNaN(s)){umfang += s*2; anzTueren++};
			s = document.getElementById("Raum5").value.replace(/[^0-9.*]/g, ""); s = s.replace(regexp, "+");s = eval(s);if (!isNaN(s)){umfang += s*2; anzTueren++};
			s = document.getElementById("Raum6").value.replace(/[^0-9.*]/g, ""); s = s.replace(regexp, "+");s = eval(s);if (!isNaN(s)){umfang += s*2; anzTueren++};
			// Standard Leistungen
			text += z+" m2 Boden schwimmend verlegen";
			text_fr += "Pose flottante pour "+z+" m2 de rev&ecirc;tement de sol";
			text_it += "La posa di "+z+" m2 di pavimento galleggiante includano";
	
			// Kleinmengenzuschlag immer
			total += BODEN.Kleinmenge;
			msg += "\nKleinmenge "+BODEN.Kleinmenge;

			// Dampfbremse und Trittschall immer
			total += z * BODEN.Trittschall;
			text += "<br>&bull;&nbsp;&nbsp;inkl. Einbau Dampfbremse und/oder Trittschalld&auml;mmung";
			text_fr += "<br>&bull;&nbsp;&nbsp;inclus montage pare-vapeur et/ou isolation phonique";
			text_it += "<br>&bull;&nbsp;&nbsp;posa della barriera al vapore/anticalpestio";
			msg += "\nTrittschall "+z+" x "+BODEN.Trittschall;

			// Sockel immer
			total += umfang * BODEN.Sockel;
			text += "<br>&bull;&nbsp;&nbsp;inkl. Sockelleistenmontage (geschraubt)";
			text_fr += "<br>&bull;&nbsp;&nbsp;inclus montage des plinthes (viss&eacute;es)";
			text_it += "<br>&bull;&nbsp;&nbsp;montaggio dello zoccolo (avvitato)";
			msg += "\nSockel "+umfang+" x "+BODEN.Sockel;

			// Anpassen der Türen immer
			total += anzTueren * BODEN.Tueren;
			msg += "\nTüren "+anzTueren+" x "+BODEN.Tueren;
			text += "<br>&bull;&nbsp;&nbsp;inkl. 1 T&uuml;re anpassen oder T&uuml;rausschnitt pro Raum";
			text_fr += "<br>&bull;&nbsp;&nbsp;inclus 1 ajustement de porte ou de cadre de porte par local";
			text_it += "<br>&bull;&nbsp;&nbsp;adattamento di 1 porta oppure del telaio della porta a stanza";

			// Schwellenprofil montieren immer
			text += "<br>&bull;&nbsp;&nbsp;inkl. 1 Schwellenprofil montieren pro Raum";
			text_fr += "<br>&bull;&nbsp;&nbsp;inclus 1 montage de profils bombés par local";
			text_it += "<br>&bull;&nbsp;&nbsp;posa della fuga in silicone (1 m a stanza, materiale escluso)";

			
			// Silikonfuge immer
			text += "<br>&bull;&nbsp;&nbsp;inkl. Silikonfuge legen (1 m pro Raum, exkl. Material)";
			text_fr += "<br>&bull;&nbsp;&nbsp;inclus pose des joints en silicone (1 m par local/mat&eacute;riel exclus)";
			text_it += "<br>&bull;&nbsp;&nbsp;montaggio di 1 profilo cavo a stanza";

			// Faktor nach Einsatzgebiet berechnen
			var einsatzgebiet = suchenOrt(document.getElementById("ObjPLZ"));
			if(einsatzgebiet == ""){
				einsatzgebiet = suchenOrt(document.getElementById("PLZ"));
			}
			if(einsatzgebiet != "") { // dazurechnen
				total = total * BODEN.Faktor[einsatzgebiet-1].Wert;
				msg += "\nFaktor Ort "+BODEN.Faktor[einsatzgebiet-1].Wert;
			}
			totalNormal = total * ALLG.Provision;
			// Standardleistungen totalisieren
			subtotal = zweiStellen(total+(z * pr) );
			total = parseFloat(subtotal,10);
			textSet += subtotal+"<br><br><br><br><br>";
			subtotal = zweiStellen(totalNormal+(z * prNormal) );
			totalNormal = parseFloat(subtotal,10);
			textNormal += subtotal+"<br><br><br><br><br>";
			
			// Anreisepauschale
			
			if(einsatzgebiet != "") { // dazurechnen
				if(einsatzgebiet == 2){
					subtotal = zweiStellen(ALLG.Anfahrt[einsatzgebiet-1].Total);
					total += parseFloat(subtotal,10);
					text += "<br><br>Anfahrts-Radius gr&ouml;sser als 20 km um Filiale";
					text_fr += "<br><br>Itin&eacute;raire de plus de 20 km autour de la filiale";
					text_it += "<br><br>Raggio di distanza maggiore a 20 km dalla filiale";
					textSet += "<br><br>"+subtotal;
					subtotal = zweiStellen(ALLG.Anfahrt[einsatzgebiet-1].Normal)
					totalNormal += parseFloat(subtotal,10);
					textNormal += "<br><br>"+subtotal;
					msg += "\Anfahrtsweg: "+ALLG.Anfahrt[einsatzgebiet-1].Total;
				}
				if(einsatzgebiet == 3){
					subtotal = zweiStellen(ALLG.Anfahrt[einsatzgebiet-1].Total);
					total += parseFloat(subtotal,10);
					text += "<br><br>Zuschlagspauschale für autofreie Orte<br> ";
					text_fr += "<br><br>Forfaits de d&eacute;placement pour endroits interdits &agrave; la<br>circulation automobile";
					text_it += "<br><br>Tariffa forfettaria aggiuntiva per luoghi non accessibili<br>in automobile";
					textSet += "<br><br>"+subtotal+"<br>";
					subtotal = zweiStellen(ALLG.Anfahrt[einsatzgebiet-1].Normal)
					totalNormal += parseFloat(subtotal,10);
					textNormal += "<br><br>"+subtotal+"<br>";
					msg += "\Anfahrtsweg: "+ALLG.Anfahrt[einsatzgebiet-1].Total;
				}

			}
	
			// Optionale Leistungen
			
			// Boden entfernen
			if(document.Form.Entsorgen[1].checked){
				subtotal = z * BODEN.EntfBoden;
				msg += "\nBoden entfernen "+z+" x "+BODEN.EntfBoden;
				schreibenOptionen("&bull;&nbsp;&nbsp;Entfernen des bestehenden Bodens","&bull;&nbsp;&nbsp;Enlever les rev&ecirc;tements actuels","&bull;&nbsp;&nbsp;Rimuovere il pavimento vecchio",subtotal);
				
				if(document.Form.Vorhanden[1].checked){// Teppich
					subtotal = z * BODEN.EntfLeim;
					msg += "\nLeim entfernen"+z+" x "+BODEN.EntfLeim;
					schreibenOptionen("&bull;&nbsp;&nbsp;Entfernen von allfälligen Leimresten","&bull;&nbsp;&nbsp;Enlever les restes &eacute;ventuels de colle","&bull;&nbsp;&nbsp;Eliminare eventuali resti di colla",subtotal);
				}
			}
			
			// Diagonal
			if(document.Form.Diagonal[1].checked){
				subtotal = z * BODEN.Diagonal;
				msg += "\nDiagonalzuschlag "+z+" x "+BODEN.Diagonal;
				schreibenOptionen("&bull;&nbsp;&nbsp;Diagonale Montage des Bodens","&bull;&nbsp;&nbsp;Montage diagonal du rev&ecirc;tement","&bull;&nbsp;&nbsp;Montaggio del pavimento diagonale",subtotal);
			}
	
			// Regiestunden
			var s = document.getElementById("Regie").value.replace(/[^0-9.]/g, ""); 
			if (!isNaN(s)&&s>0){
				subtotal = zweiStellen(s);
				schreibenOptionen("&bull;&nbsp;&nbsp;Zusätzliche Pauschale gemäss Ausmess-Service","&bull;&nbsp;&nbsp;Forfaits suppl&eacute;mentaires","&bull;&nbsp;&nbsp;Forfait supplementari",subtotal);
			}
			if(document.Form.Offerte[1].checked){ // nur ausführen, wenn Offerte selektiert wurde
				text = "Vielen Dank, dass Sie unseren Ausmess-Service bestellen. Gerne beraten wir Sie vor Ort. Bei Auftragserteilung wird Ihnen diese Pauschale zurückerstattet.";
				text_fr = "Nous vous remercions de commander notre service de mesurage. Nous vous conseillons volontiers sur place. Lors du passage de la commande, ce forfait vous sera rembours&eacute;";
				text_it = "Grazie per avere scelto il nostro servizio di misurazione. Siamo lieti di fare consulenza sul luogo. Alla conferma dell&rsquo;incarico la tariffa forfettaria sar&agrave; rimborsata.";
				textSet = "&nbsp;";
				textNormal = zweiStellen(BODEN.Offerte);
				total = 0;
				totalNormal = BODEN.Offerte;
				msg += "\nOffertpauschale "+BODEN.Offerte;
			}
		}		
	}
	// Text ausgeben
	if(window.location.pathname.search(/\/fr\//)!= -1){
		document.getElementById("Arbeiten").innerHTML = text_fr;
	}
	else{
		if(window.location.pathname.search(/\/it\//)!= -1){
			document.getElementById("Arbeiten").innerHTML = text_it;
		}
		else{
			document.getElementById("Arbeiten").innerHTML = text;
		}
	}
	document.getElementById("ArbeitenSet").innerHTML = textSet;
	document.getElementById("ArbeitenNormal").innerHTML = textNormal;
	if(document.Form.Offerte[1].checked){
		document.getElementById("Total").innerHTML = "&nbsp;";
	}
	else{
		document.getElementById("Total").innerHTML = zweiStellen(total);
	}
	document.getElementById("TotalNormal").innerHTML = zweiStellen(totalNormal);

	if (window.location.search == "?msg") {alert(msg);}
}

function schreibenOptionen(txt, txt_fr, txt_it, betrag){
	if(!option){
		option = true;
		text += "<br><br>OPTIONALE LEISTUNGEN";
		text_fr += "<br><br>PRESTATIONS OPTIONNELLES";
		text_it += "<br><br>SERVIZI OPZIONALI";
		textSet += "<br><br>";
		textNormal += "<br><br>";
	}
	text += "<br>"+txt;
	text_fr += "<br>"+txt_fr;
	text_it += "<br>"+txt_it;
	betrag = zweiStellen(betrag);
	var subtotal = parseFloat(betrag,10);
	total += subtotal;
	textSet += "<br>"+betrag;
	subtotal = zweiStellen(subtotal*ALLG.Provision);
	totalNormal += parseFloat(subtotal,10);
	textNormal += "<br>"+subtotal;
}
function schreibeRegie(input){
	input.value = input.value.replace(/[^0-9.]/g, "");
	var l = parseFloat(input.value,10);
	if (isNaN(l)) {
		input.value = "0.00 CHF";
	}
	else{
		input.value = zweiStellen(l) + " CHF";
	}
	rechneTotal();
}

function schreibenMasse(input){
	input.value = input.value.replace(/[^0-9. ]/g, " ");
	var l = parseFloat(input.value,10);
	var exp = new RegExp(l);
	var b = input.value.replace(exp,"");
	b = parseFloat(b,10);
	if (isNaN(b)) {b = l};
	if (isNaN(l) || l == 0 || b == 0) {
		input.value = "__ x __";
	}
	else{
		input.value = l + ' * ' + b;
	}
	rechneFlaeche();
}		
function rechneFlaeche(){
	var flaeche = 0;
	var z = document.getElementById("Raum1").value.replace(/[^0-9.*]/g, "");
	z = eval(z);
	if (!isNaN(z)){flaeche += z};
	z = document.getElementById("Raum2").value.replace(/[^0-9.*]/g, "");
	z = eval(z);
	if (!isNaN(z)){flaeche += z};
	z = document.getElementById("Raum3").value.replace(/[^0-9.*]/g, "");
	z = eval(z);
	if (!isNaN(z)){flaeche += z};
	z = document.getElementById("Raum4").value.replace(/[^0-9.*]/g, "");
	z = eval(z);
	if (!isNaN(z)){flaeche += z};
	z = document.getElementById("Raum5").value.replace(/[^0-9.*]/g, "");
	z = eval(z);
	if (!isNaN(z)){flaeche += z};
	z = document.getElementById("Raum6").value.replace(/[^0-9.*]/g, "");
	z = eval(z);
	if (!isNaN(z)){flaeche += z};

	document.getElementById("Flaeche").innerHTML = eineStelle(flaeche) + ' m2';
	rechneTotal();
}
function CheckInput(nr){
	var msg = "";
	var msg_fr = "";
	var msg_it = "";
	frm = document.forms[nr]; 
 	var feld = document.Form.Name.value;
	feld = feld.replace(/ /g, '');
	if(feld == ''){ // kein Name eingetragen
		msg += "\nDas Feld Name ist leer";
		msg_fr += "\nLe champ nom est vide";
		msg_it += "\nLa casella nome è vuota";
	}
	if(document.getElementById("PLZ").value == ""){ // kein Ort eingegen
		msg += "\nOrtsangabe fehlt";
		msg_fr += "\nLa donnée du lieu manque";
		msg_it += "\nManca il luogo";
	}
	// Baumarkt selektiert?
	if (document.Form.Baumarkt.selectedIndex < 1){
		msg += "\nkein Baumarkt ausgewählt";
		msg_fr += "\nLe brico n'a pas été choisi";
		msg_it += "\nNon è stato scelta la filiale";
	}
	if(document.getElementById("Total").innerHTML.length == 1 || document.getElementById("Total").innerHTML == 'CHF 0.00'){ // keine Dienstleistungen wurden selektiert
		msg += "\nkeine Dienstleistungen ausgewählt";
		msg_fr += "\nLa prestation de service n'a pas été choisie";
		msg_it += "\nNon è stato scelto nessun servizio";
	}
	if(msg != ""){
		msg = "Es sind folgende Fehler aufgetreten:" + msg;
		msg_fr = "Les erreurs suivantes sont apparues:" + msg_fr;
		msg_it = "Vi sono i seguenti errori:" + msg_it;
		// Text ausgeben
		if(window.location.pathname.search(/\/fr\//)!= -1){
			alert(msg_fr);
		}
		else{
			if(window.location.pathname.search(/\/it\//)!= -1){
				alert(msg_it);
			}
			else{
				alert(msg);
			}
		}
		return;
	}
	// Werte in die versteckten Felder übertragen

	document.Send.hName.value = document.Form.Name.value;
	document.Send.hObjName.value = document.Form.ObjName.value;
	document.Send.hVorname.value = document.Form.Vorname.value;
	document.Send.hObjVorname.value = document.Form.ObjVorname.value;
	document.Send.hStrasse.value =document.Form.Strasse.value;
	document.Send.hObjStrasse.value = document.Form.ObjStrasse.value;
	document.Send.hPLZ.value = document.Form.PLZ.value;
	document.Send.hObjPLZ.value = document.Form.ObjPLZ.value;
	document.Send.hTel.value = document.Form.Tel.value;
	document.Send.hMail.value = document.Form.Mail.value;
	var i = document.Form.Baumarkt.options.selectedIndex;
	document.Send.hBaumarkt.value = document.Form.Baumarkt.options[i].text;
	for(var i=0;i<document.Form.Boden.length; i++){
		if(document.Form.Boden[i].checked){
			document.Send.hBoden.value = document.Form.Boden[i].value;
			break;}
	}
	document.Send.hRaum1.value = document.Form.Raum1.value;
	document.Send.hRaum2.value = document.Form.Raum2.value;
	document.Send.hRaum3.value = document.Form.Raum3.value;
	document.Send.hRaum4.value = document.Form.Raum4.value;
	document.Send.hRaum5.value = document.Form.Raum5.value;
	document.Send.hRaum6.value = document.Form.Raum6.value;
	document.Send.hFlaeche.value = document.getElementById("Flaeche").innerHTML;

	document.Send.hSockel.value = '';
	for(var i=0;i<document.Form.Entsorgen.length; i++){
		if(document.Form.Entsorgen[i].checked){
			document.Send.hEntsorgen.value = document.Form.Entsorgen[i].value;
			break;}
	}
	for(var i=0;i<document.Form.Vorhanden.length; i++){
		if(document.Form.Vorhanden[i].checked){
			document.Send.hVorhanden.value = document.Form.Vorhanden[i].value;
			break;}
	}

	document.Send.hTrittschall.value = '';
	for(var i=0;i<document.Form.Diagonal.length; i++){
		if(document.Form.Diagonal[i].checked){
			document.Send.hDiagonal.value = document.Form.Diagonal[i].value;
			break;}
	}
	document.Send.hRegie.value = document.Form.Regie.value;
	for(var i=0;i<document.Form.Offerte.length; i++){
		if(document.Form.Offerte[i].checked){
			document.Send.hOfferte.value = document.Form.Offerte[i].value;
			break;}
	}

	// Whitespace in textarea entfernen
	
	var text = document.getElementById("Arbeiten").innerHTML;
	text = text.replace(/&nbsp;/gi, " ");
	document.Send.hArbeiten.value = text.replace(/<br>/gi, "\n");

	text = document.getElementById("ArbeitenSet").innerHTML;
	text = text.replace(/&nbsp;/gi, " ");
	document.Send.hArbeitenSet.value = text.replace(/<br>/gi, "\n");

	text = document.getElementById("ArbeitenNormal").innerHTML;
	text = text.replace(/&nbsp;/gi, " ");
	document.Send.hArbeitenNormal.value = text.replace(/<br>/gi, "\n");


	document.Send.hBemerkungen.value = document.Form.Bemerkungen.value.replace(/\s/g, " ");
	
	document.Send.hTotal.value = document.getElementById("Total").innerHTML;
	document.Send.hTotalNormal.value = document.getElementById("TotalNormal").innerHTML;
	document.Send.hDmp.value = (window.location.search == "?dmp") ? 'dmp' : '';
	
	// FPDF aufrufen
	document.Send.submit();
 }
function limitText(textArea, length) {
  if (textArea.value.length > length) {
    textArea.value = textArea.value.substr(0,length);
		textArea.scrollTop = textArea.scrollHeight;
  }
}

