/**
* ################
* TESTET, INGEN KENDTE BUGS
* ################
**/

function loadForm(){
	var length = document.getElementsByTagName("hr").length; 									// HENTER ANTAL HR(DER HVOR PRODUKT IDERNE STÅR I)

	if(length >= 1) {																			// KONTROL AF OM length ER STØRRE END 0 - ALH
		for(xy = 0; xy < length; xy++){															// LØB PRODUKTERNE IGENNEM
			var product_id = document.getElementsByTagName("hr")[xy].title;						// HENT DET ENKELTE PRODUKT ID			
			changePrice(product_id);															// KØR CHANGE PRICE PÅ DEN PRODUKT ID
		}
	}
}

function changePrice(product_id){																// OPDATERER VISTE PRISER
	var amount = document.getElementById("quantity"+product_id).value;							// HENT ANTAL VALUE
	var parentPriceMonth = document.getElementById("parentPriceMonth"+product_id).title;		// HENT PRIS OM MÅNEDEN PÅ HOVED PRUDUKT
	var newParentPriceMonth = parentPriceMonth * amount;										// NY PRIS OM MÅNEDEN ER AMOUNT x PRIS OM MÅNEDEN
	document.getElementById("parentPriceMonth"+product_id).innerHTML = newParentPriceMonth;		// VIS NY PRIS PÅ SIDEN
	var totalMonth = newParentPriceMonth;														// DET TOTALE MÅNEDLIGE BELØB BLIVER STARTET

	var parentPriceStart = document.getElementById("parentPriceStart"+product_id).title;		// BETAL NU PRISEN, PÅ HOVED PRUDUKT BLIVER HENTET
	var totalStart = parentPriceStart * 1;														// DET TOTALE BETAL NU BELØB BLIVER STARTET
	
	document.getElementById("totalStart"+product_id).innerHTML = totalStart;					// OPDATER TOTALE BETAL NU PRIS PÅ SIDEN
	document.getElementById("totalMonth"+product_id).innerHTML = totalMonth;					// OPDATER TOTALE MÅNEDLIGE BELØB PÅ SIDEN
		
	var numChilds = document.getElementById(product_id+"numChilds").value;						// HENTER ANTAL CHILDS
		for(i=1;i<=numChilds;i++){																// LØBER IGENNEM CHILD PRODUCTS
			
			var childPriceMonth = document.getElementById("mdr"+i+product_id).title;			// CHILD PRIS OM MÅNEDEN BLIVER HENTET FRA TITLE
			var newChildPriceMonth = childPriceMonth * amount;									// DEN NYE CHILD PRIS OM MÅNEDEN ER PRIS x ANTAL
			totalMonth = totalMonth + newChildPriceMonth;										// LIGGES TIL TOTALE MÅNEDLIGE BELØB
			document.getElementById("mdr"+i+product_id).innerHTML = newChildPriceMonth;			// HER VISER VI DET NYE CHILD MÅNEDLIGE BELØB
			
			var childPriceStart = document.getElementById("start"+i+product_id).title * 1;		// CHILD BETAL NU BLIVER HENTET FRA TITLE
			totalStart = totalStart + childPriceStart;											// LIGGES TIL DET TOTALE BETAL NU BELØB
			document.getElementById("start"+i+product_id).innerHTML = childPriceStart;			// HER VISER VI DET NYE CHILD BETAL NU BELØB
			
			
			document.getElementById("totalStart"+product_id).innerHTML = totalStart;			// OPDATER TOTALE BETAL NU PRIS PÅ SIDEN
			document.getElementById("totalMonth"+product_id).innerHTML = totalMonth;			// OPDATER TOTALE MÅNEDLIGE BELØB PÅ SIDEN

		}
}

function changeGroundPrice(mdrI,childI,startI,product_id){			// SKIFTER DE PRISER DER REGNES UD FRA
	
	var groundPriceOld = mdrI+document.getElementById(childI+product_id).value;					// HER HENTES DEN GAMLE MÅNEDLIGE GRUND PRIS
	var newGroundPrice = document.getElementById(groundPriceOld+product_id).value;				// FINDER NY MÅNEDLIGE GRUND PRIS
	document.getElementById(mdrI+product_id).title = newGroundPrice;							// GEMMER DEN NYE MÅNEDLIGE GRUND PRIS
	
	var startGroundPriceOld = startI+document.getElementById(childI+product_id).value;			// HER HENTES DEN GAMLE BETAL NU GRUND PRIS
	var startNewGroundPrice = document.getElementById(startGroundPriceOld+product_id).value;	// FINDER NY BETAL NU GRUND PRIS
	document.getElementById(startI+product_id).title = startNewGroundPrice;						// GEMMER DEN NYE BETAL NU GRUND PRIS
	
	changePrice(product_id);
//	setTimeout(changePrice(product_id),0);														// KØRER CHANGEPRICE, FOR AT OPDATERER VISTE PRISER
	
}

function recalculatePrice(boxChecked,price) {
	//alert('Child: '+child+'\nPrice: '+price);
	//alert(document.getElementsByName('child')[0].checked);
	
	var totalPrice = document.getElementById('parentPriceStart7').innerHTML*1; 					// GANG MED ÉN FOR AT SIKRE TOLKNING SOM TAL
	var itemPrice = price*1;																	// GANG MED ÉN FOR AT SIKRE TOLKNING SOM TAL
	
	if(boxChecked == true) {
		document.getElementById('parentPriceStart7').innerHTML = itemPrice+totalPrice;
	} else {
		document.getElementById('parentPriceStart7').innerHTML = totalPrice-itemPrice;
	}
	/*
	var length = document.getElementsByTagName("hr").length;									// HENTER ANTAL HR(DER HVOR PRODUKT IDERNE STÅR I)
	
	if(length > 0) {																			// KONTROL AF OM length ER STØRRE END 0 - ALH
		for(xy = 0; xy <= length; xy++){														// LØB PRODUKTERNE IGENNEM
			var product_id = document.getElementsByTagName("hr")[xy].title;						// HENT DET ENKELTE PRODUKT ID
			changePrice(product_id);															// KØR CHANGE PRICE PÅ DEN PRODUKT ID
		}
	}
	*/
}

function checkPrice() {
	var totalPrice = document.getElementById('parentPriceStart7').innerHTML;
	
	//alert(totalPrice);
	
	if(totalPrice > 0) {
		return true;
	} else {
		alert('Du har ikke valgt et ledigt domæne. Prøv igen.');
		return false;
	}
}
