cityZip = 0;
curCityName = "";

function calc(ele){
	if(ele){
		if(parseInt(ele.value) == ele.value || ele.value == ""){
			calculateForm();
		}else{
			alert('Indtast venligst et tal!');
			ele.focus();
		}
	}
}

function calculateForm(){
	price = 0;
	
	price = document.getElementById("form_field_51").value*priceOpb;

	mobgrad_lille = document.getElementById("form_field_45_4");
	mobgrad_mellem = document.getElementById("form_field_45_5");
	mobgrad_stor = document.getElementById("form_field_45_6");
	if(mobgrad_lille.checked){
		price = price * prices["mobgrad_lille"] / 100;
	}else if(mobgrad_mellem.checked){
		price = price * prices["mobgrad_mellem"] / 100;
	}else if(mobgrad_stor.checked){
		price = price * prices["mobgrad_stor"] / 100;
	}

	subtotal = price;
	subtotal = Math.round(subtotal);
	moms = subtotal * 0.25;
	total = subtotal + moms;
	document.getElementById("form_field_46").value = total;
	document.getElementById("form_field_47").value = moms;
	document.getElementById("form_field_48").value = subtotal;
}

function validateZip(ele){
	if(parseInt(ele.value*1) && ele.value.length == 4){
		getPrices(ele.value);
	}
}

function getPrices(zip){
	$("#form_field_50").val("");
	$("#loadingGif").show();
	$.get("/files/getPrices.php", {zip: zip},
		function(data){
			var xml = data;
			$('city',xml).each(function(c) {
				cityZip = $(this).find("name").attr("zip");
				curCityName = $(this).find("name").text();
			})
			$('prices',xml).each(function(i) {
				name = $(this).find("price").attr("name");
				price = $(this).find("price").text();
				if(name == "opbevaring"){
					priceOpb = price;
				}
			})
			if(cityZip > 0){
				$(".calcBoxMain input").removeAttr("disabled");
				loadPrices = false;

				$("#form_field_49").attr("readonly","readonly");

				$("#form_field_50").val(curCityName);
			}else{
				alert("Indtast et gyldigt postnummer!");
				$("#form_field_50").val("Ukendt");
			}
			$("#form_field_51").focus();
			$("#loadingGif").hide();
	});
}

function printForm(){
	str = "";
	$(".calcBoxMain :text").each(function(c) {
		if(str != ""){
			str += "&"
		}
		str += $(this).attr("name") + "=" + $(this).val();
	})
	$(".calcBoxMain input[@type=radio][@checked]").each(function(c) {
		if(str != ""){
			str += "&"
		}
			radioName = $(this).attr("name").split("[");
			radioName = radioName[0];
			str += radioName + "=" + $(this).attr("value");
	})
	printWindow = window.open("/index/opbevaring/prisberegner/print?" + str, "printWindow", "");
}
