
/*
* textChanged(pris As String)
* Metoden beregner summen av pris og antall, dvs (pris1*antall1) +...+ (pris10*antall10) 
*/
function textChanged(pris){
	
		
		//var prisen = document.getElementById(pris).value
		//var itext1 = document.getElementById(pris).value
		
		/* Henter pris fra pris textboxene og konverterer pris til float (finnes ikke parseDouble) */
		pris1 = bestillingForm.Bestilling1_TextBoxPris1.value
		prisTall1 = konverterPris(pris1)
		pris2 = bestillingForm.Bestilling1_TextBoxPris2.value
		prisTall2 = konverterPris(pris2)
		pris3 = bestillingForm.Bestilling1_TextBoxPris3.value
		prisTall3 = konverterPris(pris3)
		pris4 = bestillingForm.Bestilling1_TextBoxPris4.value
		prisTall4 = konverterPris(pris4)
		pris5 = bestillingForm.Bestilling1_TextBoxPris5.value
		prisTall5 = konverterPris(pris5)
		pris6 = bestillingForm.Bestilling1_TextBoxPris6.value
		prisTall6 = konverterPris(pris6)
		pris7 = bestillingForm.Bestilling1_TextBoxPris7.value
		prisTall7 = konverterPris(pris7)
		pris8 = bestillingForm.Bestilling1_TextBoxPris8.value
		prisTall8 = konverterPris(pris8)
		pris9 = bestillingForm.Bestilling1_TextBoxPris9.value
		prisTall9 = konverterPris(pris9)
		pris10 = bestillingForm.Bestilling1_TextBoxPris10.value
		prisTall10 = konverterPris(pris10)
		
		/* Henter antall fra antall textboxene og konverterer antall til Integer */
		antall1 = bestillingForm.Bestilling1_TextBoxAntall1.value
		antallTall1 = konverterAntall(antall1)
		antall2 = bestillingForm.Bestilling1_TextBoxAntall2.value
		antallTall2 = konverterAntall(antall2)
		antall3 = bestillingForm.Bestilling1_TextBoxAntall3.value
		antallTall3 = konverterAntall(antall3)
		antall4 = bestillingForm.Bestilling1_TextBoxAntall4.value
		antallTall4 = konverterAntall(antall4)
		antall5 = bestillingForm.Bestilling1_TextBoxAntall5.value
		antallTall5 = konverterAntall(antall5)
		antall6 = bestillingForm.Bestilling1_TextBoxAntall6.value
		antallTall6 = konverterAntall(antall6)
		antall7 = bestillingForm.Bestilling1_TextBoxAntall7.value
		antallTall7 = konverterAntall(antall7)
		antall8 = bestillingForm.Bestilling1_TextBoxAntall8.value
		antallTall8 = konverterAntall(antall8)
		antall9 = bestillingForm.Bestilling1_TextBoxAntall9.value
		antallTall9 = konverterAntall(antall9)
		antall10 = bestillingForm.Bestilling1_TextBoxAntall10.value
		antallTall10 = konverterAntall(antall10)

		/*
		prisTall1 = konverterPris(pris1)
		prisTall2 = konverterPris(pris2)
		prisTall3 = konverterPris(pris3)
		prisTall4 = konverterPris(pris4)
		prisTall5 = konverterPris(pris5)
		prisTall6 = konverterPris(pris6)
		prisTall7 = konverterPris(pris7)
		prisTall8 = konverterPris(pris8)
		prisTall9 = konverterPris(pris9)
		prisTall10 = konverterPris(pris10)
		*/
		
		//sum = pris1 + pris2 + pris3 + pris4 + + pris5 + pris6 + pris7 + pris8 + pris9 + pris10
		//sum = prisTall1 + prisTall2
		//sum = prisTall1 + prisTall2 + prisTall3 + prisTall4 + prisTall5 + prisTall6 + prisTall7 + prisTall8 + prisTall9 + prisTall10
		sum = (prisTall1 * antallTall1) + (prisTall2 * antallTall2) + (prisTall3 * antallTall3) + (prisTall4 * antallTall4) + (prisTall5 * antallTall5) + (prisTall6* antallTall6) + (prisTall7* antallTall7) + (prisTall8* antallTall8) + (prisTall9 * antallTall9) + (prisTall10 * antallTall10)
		if(sum.toString() =="NaN"){
			alert("Du må skrive inn pris i riktig format!")
		}
		bestillingForm.Bestilling1_TextBoxSum.value = sum
		//alert(""+sum)
		
		//sum = prisen
		prisTall1 = 0
		prisTall2 = 0
		prisTall3 = 0
		prisTall4 = 0
		prisTall5 = 0
		prisTall6 = 0
		prisTall7 = 0
		prisTall8 = 0
		prisTall9 = 0
		prisTall10 = 0
		//sum = 0
		antallTall1 = 0
		antallTall2 = 0
		antallTall3 = 0
		antallTall4 = 0
		antallTall5 = 0
		antallTall6 = 0
		antallTall7 = 0
		antallTall8 = 0
		antallTall9 = 0
		antallTall10 = 0
		
	}
	
	/*
	* konverterPris(prisString As String)
	* Metoden konverterer pris til float
	*/
	function konverterPris(prisString){
		
		//konverterer først komma til punktum
		prisString2 = prisString.replace(/,/g, '.')
		
		if(prisString2 != ""){
			//Parser string til float:
			prisFloat = parseFloat(prisString2)
			return prisFloat
		}
		prisString2 = 0
		return prisString2	
		
	}
	
	function sjekkAntallTextFelt(antallet){
		//var antallet = document.getElementById(antall).value
		var validChars = "0123456789"
		var isNumber = true
		var Char 
		var feilChar = ""
		
		for(i=0; i<antallet.length && isNumber == true; i++){
			Char = antallet.charAt(i)
			if(validChars.indexOf(Char) == -1){
				isNumber = false
				feilChar = antallet.charAt(i)
			}
		}
		if(isNumber == false){
			alert("Du må skrive inn antall i riktig format! "+ feilChar +" er ikke et hel tall!")
		}
	}
	
	function konverterAntall(antallString){
		if(antallString != ""){
			sjekkAntallTextFelt(antallString)
			//Parser string til float:
			antallString = parseInt(antallString)
			return antallString
		}
		return antallString
	}
	
	function textChangedToSession(navn, nyVerdi, dager){
		var cookienavn = navn
		var verdi = document.getElementById(nyVerdi).value
		document.cookie = navn+"="+verdi+"; "+expires+"="+dager +"; path=/"
		
	}