/* Selects */				
var select_option_glass = document.getElementById("pa_glas");				
// var select_option_parts = document.getElementById("pa_delning");				
var select_option_color = document.getElementById("pa_farg");				
var select_option_extras = document.getElementById("pa_tillval");				
var select_option_function = document.getElementById("pa_funktion");				
				
/* Form-groups */				
var all_form_groups = document.getElementsByClassName("pewc-group-wrap");				
				
var one_glass = document.getElementById("pewc-group-40051"); // NY				
var two_glass = document.getElementById("pewc-group-40055"); // NY				
				
// ONE GLASS
var width_one_glass = document.getElementById("pewc_group_40051_40052");
var height_one_glass = document.getElementById("pewc_group_40051_40054");

// TWO GLASS
var width_two_glass = document.getElementById("pewc_group_40055_40056");
var height_two_glass = document.getElementById("pewc_group_40055_40057");				
				
// All widh and height inputs within form-fields				
var all_heights = document.getElementsByClassName("pewc-form-field");				
				
/* Price inputs */				
var new_price = document.getElementById("new-price");				
var total_price = document.getElementById("pewc_total_calc_price");				
				
/* Remove duplicates */				
var pewc_product_width = document.getElementById("pewc_product_width");				
pewc_product_width.parentNode.removeChild(pewc_product_width);				
//var pewc_product_hight = document.getElementById("pewc_product_hight");				
//pewc_product_hight.parentNode.removeChild(pewc_product_hight);				
				
/* Add min and max-text on inputs				
Because class pewc-group contains width, height AND the caluclated price for the selected width and heights				
we need to filter out the last one (the output that displayes the price).				
				
In the else-statment we add a <span> with a class we itterate through later.				
*/				
				
var input_groups = document.querySelectorAll('.pewc-group');				
input_groups.forEach((item, index) => {				
	if ((index % 3 == 2)) {			
		item.innerHTML += "";		
	} else {			
		var parent = item;		
		var child = document.createElement("span");		
		child.className = "min-max-value";		
		parent.appendChild(child);		
	}			
});				
/* We itterate through the newly created span with class "min-max-value"				
Width and height doesn't have the same max and min values-text, in order to DRY as much as possible we detemine if the				
class apperas as even or odd in the DOM. And inserts the correct text for even and odd Divs in the DOM.				
*/				
var min_max = document.querySelectorAll('.min-max-value');				
min_max.forEach((items, indexs) => {				
	if ((indexs % 2 == 0)) {			
		items.innerHTML += '<p class="pewc-description"><small>Min: 3150</small><small>Max: 5600</small></p>';		
	} else {			
		items.innerHTML += '<p class="pewc-description"><small>Min: 1200</small><small>Max: 3000</small></p>';		
	}			
				
});				
				
				
//pewc-calculation-span				
				
				
				
/* Remove all set values */				
for (var i = 0; i < all_heights.length; i++) {				
	all_heights[i].value = "";			
				
	all_heights[i].required = true;			
	all_heights[i].removeAttribute("min");			
	all_heights[i].removeAttribute("max");			
	all_heights[i].onchange = function() {			
		if (pa_farg.value.indexOf("ral") !== -1) {		
			//	alert("RAL")
		} else {		
			//	alert("nop")
		}		
	}			
}				
				
if (select_option_glass.value != "") {				
	showFormgroups();			
}				
				
/* Changing options */				
select_option_glass.onchange = function() {				
	// Reset parts options			
	/* hide all form groups */			
	for (var i = 0; i < all_form_groups.length; i++) {			
		all_form_groups[i].style.display = "none";		
	}			
	if (select_option_glass.value != "") {			
				
		showFormgroups();		
	}			
				
	jQuery(document).ready(function($){			
		var extras_price = document.getElementById("pewc-product-price");		
		var total_price = document.getElementById("pewc_total_calc_price");		
		var unit_price = total_price.value - extras_price.value;		
$(".pewc-calculation-span").html('<span class="woocommerce-Price-amount amount"><bdi>'+unit_price+'&nbsp;<span class="woocommerce-Price-currencySymbol">kr</span>&rlm;</bdi></span>')				
				
				
	});			
};				
				
function showFormgroups() {				
	for (var i = 0; i < all_heights.length; i++) {			
		all_heights[i].value = "";		
		all_heights[i].removeAttribute("min");		
		all_heights[i].removeAttribute("max");		
		all_heights[i].required = false;		
	}			
	/* hide all form groups */			
	for (var i = 0; i < all_form_groups.length; i++) {			
		//		all_form_groups[i].innerHTML = "";
	}			
	// One glass			
	// One glass 7 parts			
	if (select_option_glass.value === "1-glas") {			
				
		/* Show the correct field-group */		
		one_glass.style.display = "block";		
				
		/* Set width and height as required */		
		height_one_glass.required = true;		
		width_one_glass.required = true;		
		width_one_glass.setAttribute("min", "3150")		
		width_one_glass.setAttribute("max", "5600")		
		height_one_glass.setAttribute("min", "1200")		
		height_one_glass.setAttribute("max", "3000")		
				
}				
				
	// Two glass			
	if (select_option_glass.value === "2-glas") {			
				
		// Show the correct field-group		
		two_glass.style.display = "block";		
		// Set width and height as required		
		height_two_glass.required = true;		
		width_two_glass.required = true;		
		width_two_glass.setAttribute("min", "3150")		
		width_two_glass.setAttribute("max", "5600")		
		height_two_glass.setAttribute("min", "1200")		
		height_two_glass.setAttribute("max", "3000")		
				
				
	}			
}				
