// Copyright (c) 2004 Ilkka Klemetti

pvm = new Date();
pvm.setDate(pvm.getDate() + 365);
var arrHinta = new Array();

function HaeKeksiArvo(offset) {  
	var merkkijononpaa = document.cookie.indexOf (";", offset);  
	if (merkkijononpaa == -1)    
	merkkijononpaa = document.cookie.length;  
	return unescape(document.cookie.substring(offset, merkkijononpaa));
}

function HaeKeksi (name) {  
	var arg = name + "=";  
	var apit = arg.length;  
	var kpit = document.cookie.length;  
	var i = 0;  
	while (i < kpit) {    
		var j = i + apit;    
		if (document.cookie.substring(i, j) == arg)      
		return HaeKeksiArvo (j);
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i == 0) break;   
	}  
	return null;
}

function AsetaKeksi (nimi, arvo, expires) {  
	var argv = AsetaKeksi.arguments;  
	var argc = AsetaKeksi.arguments.length;  
	var expires = (argc > 2) ? argv[2] : null;  
	var path = (argc > 3) ? argv[3] : null;  
	var domain = (argc > 4) ? argv[4] : null;  
	var secure = (argc > 5) ? argv[5] : false;  
	document.cookie = nimi + "=" + escape (arvo) + 
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
	((path == null) ? "" : ("; path=" + path)) +  
	((domain == null) ? "" : ("; domain=" + domain)) +    
	((secure == true) ? "; secure" : "");
}

function PoistaKeksi (nimi) {  
	var exp = new Date();  
	exp.setTime (exp.getTime() - 1);  
	var cval = HaeKeksi (nimi);  
	document.cookie = nimi + "=" + cval + "; expires=" + exp.toGMTString();
}


function Virve2(a) {
	a = Math.round(a * 100) / 100;
	if(a == 0) {
	     a = "";
	     return a;
	}
	if(a - Math.floor(a) == 0) {
	     a = a + ".00";
	     return a;
	}
	if(a * 10 - Math.floor(a * 10) == 0) {
	     a = a + "0";
	     return a;
	}
	return a;
}

function Virve(a) {
	a = Math.round(a * 1000) / 1000;
	if(a == 0) {
	     a = "";
	     return a;
	}
	if(a - Math.floor(a) == 0) {
	     a = a + ".000";
	     return a;
	}
	if(a * 10 - Math.floor(a * 10) == 0) {
	     a = a + "00";
	     return a;
	}
	if(a * 100 - Math.floor(a * 100) == 0) {
	     a = a + "0";
	     return a;
	}
	return a;
}

function fixaus(a) {
	pituus = a.length;
	if(a.indexOf(",") !=-1){
		n = a.indexOf(",");
		alku = a.substring(0, n);
		loppu = a.substring(n + 1, pituus);
		a = alku + "." + loppu;
	}

	return a; 
}

function prosentit() {
	var total = 0;
	total = total + (document.palaskuri.P1.value) * 1;
	total = total + (document.palaskuri.P2.value) * 1;
	total = total + (document.palaskuri.P3.value) * 1;
	total = total + (document.palaskuri.P4.value) * 1;	
	document.palaskuri.P.value = total;
	if(total != 100){
		document.palaskuri.P.style.color="#FF0000";}
	else{   document.palaskuri.P.style.color="#000000";}
	tyhjennys();
}

function laskeseos() {
   with (document.palaskuri) {
	Vertailumaara = fixaus(Lkrit.value) * 1;
	if(Vertailumaara == 0){
		alert("Perusaineen määrä puuttuu.\nGrundmängden fattas.");
		return;
	}
	if(P.value != 100){
		alert("Seossuhteiden summan pitää olla 100%\nTotal blandningen måste vara 100%");
		return;
	}
	if(Akrit.value == 0){LL = Vertailumaara;}
	if(Akrit.value == 1){LL = Vertailumaara / P1.value * 100.0;}
	if(Akrit.value == 2){LL = Vertailumaara / P2.value * 100.0;}
	if(Akrit.value == 3){LL = Vertailumaara / P3.value * 100.0;}
	if(Akrit.value == 4){LL = Vertailumaara / P4.value * 100.0;}
	L1.value = Virve(LL * P1.value / 100.0);
	L2.value = Virve(LL * P2.value / 100.0);
	L3.value = Virve(LL * P3.value / 100.0);
	L4.value = Virve(LL * P4.value / 100.0);
	L.value = Virve(LL);
	AsetaKeksi("P1", P1.value, pvm);
	AsetaKeksi("P2", P2.value, pvm);
	AsetaKeksi("P3", P3.value, pvm);
	AsetaKeksi("P4", P4.value, pvm);
	AsetaKeksi("P", P.value, pvm);
   }	
}
function tyhjennys(){
   with (document.palaskuri){
	L1.value = "";
	L2.value = "";
	L3.value = "";
	L4.value = "";
	L.value = "";
   }
}

function AsetaAlkuarvot(){
	document.palaskuri.P1.value = thj(HaeKeksi("P1"));
	document.palaskuri.P2.value = thj(HaeKeksi("P2"));
	document.palaskuri.P3.value = thj(HaeKeksi("P3"));
	document.palaskuri.P4.value = thj(HaeKeksi("P4"));
	document.palaskuri.P.value = thj(HaeKeksi("P"));
	strHinta = HaeKeksi("Hintatiedot");
	if(strHinta){
		arrHinta = strHinta.split("|");
		for(i = 0; i < document.hintalaskuri.elements.length; i++) {
			document.hintalaskuri.elements[i].value = arrHinta[i];
		}
	}
}

function thj(a){
	if(!a) a = "";
	return a;
}

function hprosentit(n) {
	var total = 0;
	total = total + (document.hintalaskuri.elements[n].value) * 1;
	total = total + (document.hintalaskuri.elements[n + 5].value) * 1;
	total = total + (document.hintalaskuri.elements[n + 10].value) * 1;
	total = total + (document.hintalaskuri.elements[n + 15].value) * 1;	
	if(total == 0) total = "";
	document.hintalaskuri.elements[n + 19].value = total;
	if(total != 100){
		document.hintalaskuri.elements[n + 19].style.color="#FF0000";}
	else{   document.hintalaskuri.elements[n + 19].style.color="#000000";}
	document.hintalaskuri.elements[n + 23].value = "";
}
function LaskeHinta(n) {
   with(document.hintalaskuri){
	if(elements[n + 19].value != 100){
		alert("Seossuhteiden summan pitää olla 100%\nTotal blandningen måste vara 100%");
		return;
	}
	HH1 = fixaus(H1.value);
	HH2 = fixaus(H2.value);
	HH3 = fixaus(H3.value);
	HH4 = fixaus(H4.value);
	hinta = HH1 * elements[n].value / 100.0 + HH2 * elements[n + 5].value / 100.0 +
		HH3 * elements[n + 10].value / 100.0 + HH4 * elements[n + 15].value / 100.0;
	elements[n + 23].value = Virve2(hinta);
	strHinta = "";
	for(i = 0; i < elements.length; i++) {
		strHinta = strHinta + elements[i].value + "|";
	}
	AsetaKeksi("Hintatiedot", strHinta, pvm);
   }
}

function TyhjennaHinta() {
	for (i=1; i < 5; i = i + 1) {
		document.hintalaskuri.elements[i + 23].value = "";
	}
}
