// Initialize FixedPoint Framework and create build
window.onload = function()
	{
	var loadFpfw = document.createElement("script");
	loadFpfw.src = '/lib/fpfw/fp_fw.js'+'?disablecache='+Math.round(Math.random()*9999); 
	loadFpfw.language = "javascript";
	document.getElementsByTagName("head")[0].appendChild(loadFpfw);


	loadFpfw.onreadystatechange= function()
		{
		(this.readyState == 'complete') ? fpInit() : false;
		}
	loadFpfw.onload = function(){fpInit();};
	}

function fpInit()
	{
	fp.createBuild(
			{path: '/lib/fpfw/', file: 'fp_essentials.js', init: false, disableCache:true, confirmLoad : 'typeof(fpCommon)'},	// Fixed Point User Interface control
			{path: '/lib/fpfw/', file: 'fp_uicontrol.js', init: function(){fpUIC.init(); initSliders();}, disableCache:true, confirmLoad : 'typeof(fpUIC)'}	// Fixed Point User Interface control
		);
	}

function toggleDistro(elem)
	{
	var elems = elem.parentNode.getElementsByTagName("DIV");

	for(var i=0, j=elems.length; i<j;i++)
		{
		elems[i].className = (elems[i]== elem) ? "distro-container distro-selected" : "distro-container";
		}
	};

var tentacle_core = {
	updateTotal : function()
		{
		if(!document.forms[0] || !document.forms[0].billingdiscount) return false;

		var discount = 0;
		if(document.forms[0].billingdiscount[0].checked) discount = document.forms[0].billingdiscount[0].value; 
		if(document.forms[0].billingdiscount[1].checked) discount = document.forms[0].billingdiscount[1].value; 
		if(document.forms[0].billingdiscount[2].checked) discount = document.forms[0].billingdiscount[2].value;

		var total = 0;
		var totalElems = document.getElementsByTagName("DIV");
		for(var i=0, j=totalElems.length; i<j;i++)
			{
			if(totalElems[i].getAttribute("id"))
				{
				var elemID = totalElems[i].getAttribute("id");
				if(elemID.indexOf('price') != -1)
					{
					total+=parseInt(totalElems[i].innerHTML);
					}
				}
			}
		total = total-((total/100)*discount);
		document.getElementById("totalpricewithoutvat").innerHTML = total;
		document.getElementById("vat").innerHTML = (total*0.23).toFixed(2);
		document.getElementById("totalpricewithvat").innerHTML = (parseFloat(total)+parseFloat((total*0.23).toFixed(2))).toFixed(2);
		document.getElementById("order-total").value = total;
		},

	countBackup: function(elem)
		{
		if(document.forms[0]['backup'][1].checked)
			{
			document.getElementById("backup-price").innerHTML= 0.5*parseInt(document.getElementById("hdd").innerHTML);
			}
		else	{
			document.getElementById("backup-price").innerHTML= 0;
			}
		tentacle_core.updateTotal();
		},

	validate: function()
		{
		var elems= document.forms[0].getElementsByTagName("*");
		var mustfillCount=0;
		var filledCount = 0;
		for(var i=0, j=elems.length; i<j; i++)
			{
			if(elems[i].getAttribute('data-mustfill'))
				{
				mustfillCount++;
				if(elems[i].value=="")
					{
					elems[i].className="mustfill";
					}
				else	{
					elems[i].className="";
					filledCount++;
					}
				}
			}

		if(mustfillCount == filledCount) document.forms[0].submit();
else return false;
		},

	contact: function(toggle)
		{
		if(toggle)
			{
			document.getElementById('companyContact').style.display='block';
			document.getElementById('companymustfill').setAttribute("data-mustfill", "true");
			document.getElementById('companyidmustfill').setAttribute("data-mustfill", "true");
			}
		else	{
			document.getElementById('companyContact').style.display='none';
			document.getElementById('companymustfill').removeAttribute("data-mustfill");
			document.getElementById('companyidmustfill').removeAttribute("data-mustfill");
			}

		},

	domain: function(elem)
		{
		if(!elem)
			{
			document.getElementById('olddomain').removeAttribute("data-mustfill");
			document.getElementById('newdomain').removeAttribute("data-mustfill");
			}
		else
	{
		if(elem.indexOf('newdomain') != -1)
			{
			document.getElementById('olddomain').removeAttribute("data-mustfill");
			document.getElementById('newdomain').setAttribute("data-mustfill", "true");
			}

		if(elem.indexOf('olddomain') != -1)
			{
			document.getElementById('olddomain').setAttribute("data-mustfill", 'true');
			document.getElementById('newdomain').removeAttribute("data-mustfill");
			}
		}
}
	};

function initSliders()
	{
	var sliders = [];
	var sliderElems = document.getElementsByTagName("*");
	for(var i=0, j=sliderElems.length; i<j; i++)
		{
		if(sliderElems[i].className=='slider-container')
			{
			sliders.push(sliderElems[i]);
			}
		}

	for(var i=0, j=sliders.length; i<j; i++)
		{
		var slider = sliders[i];
		var sliderHandle = sliders[i].getElementsByTagName("DIV")[1];
		var sliderBar = sliders[i].getElementsByTagName("DIV")[0];

		sliderHandle.parent = slider;
		sliderHandle.slave = sliderBar;
		
		slider.handle = sliderHandle;
		slider.bar = sliderBar;
		
		slider.minValue = parseInt(slider.getAttribute("data-valuesMin"));
		slider.maxValue = parseInt(slider.getAttribute("data-valuesMax"));
		slider.moveBy = parseInt(slider.getAttribute("data-moveBy"));
		slider.units = parseInt(slider.getAttribute("data-units"));
		slider.price = parseInt(slider.getAttribute("data-price"));
		slider.unitContainer = document.getElementById(slider.getAttribute("data-unitContainer"));
		slider.priceContainer = document.getElementById(slider.getAttribute("data-priceContainer"));
		slider.inputContainer= ''+slider.getAttribute("data-unitContainer")+'';


		if(!isNaN(document.forms[0][slider.inputContainer].value))
			{

			var test = document.forms[0][slider.inputContainer].value*(slider.scrollWidth / ((slider.maxValue - slider.minValue)/slider.moveBy));
			sliderHandle.style.left = test+"px";
			sliderBar.style.width = test+"px";

			slider.unitContainer.innerHTML = slider.minValue + (document.forms[0][slider.inputContainer].value * slider.moveBy);
			slider.priceContainer.innerHTML = (parseInt(document.forms[0][slider.inputContainer].value)*slider.price) + " &euro;/kk";
			tentacle_core.updateTotal();
			tentacle_core.countBackup()

			}
		fpUIC.bindEvent(sliderHandle, 'mouseover', function()
			{
			if(!fpUIC.mouseDown)
				{
				if(!this.style.left) this.style.left = 0+"px";
				this.startPos = parseInt(this.style.left) - fpUIC._x;
				}
			});

		fpUIC.bindEvent(sliderHandle, 'mousedown', function()
			{
			tentacle_core.slide = true;
			if(this.startPos+fpUIC._x > 0 && this.startPos+fpUIC._x < sliders[0].scrollWidth-11)
				{
				this.style.left = this.startPos+fpUIC._x+"px";
				this.slave.style.width = this.slave.startPos+fpUIC._x+"px";
				}
			});

		fpUIC.bindEvent(sliderHandle, 'mouseup', function(){tentacle_core.slide = false;});
		fpUIC.bindEvent(slider, 'mousemove', function()
			{
			if(fpUIC.mouseDown)
				{
				if(this.handle.startPos+fpUIC._x > 0 && this.handle.startPos+fpUIC._x < this.scrollWidth-11)
					{
					this.handle.style.left = this.handle.startPos+fpUIC._x+"px";
					this.bar.style.width = this.handle.startPos+fpUIC._x+"px";

					var valueSpace = this.maxValue - this.minValue;
					var elemWidth = this.scrollWidth;

					document.forms[0][this.inputContainer].value = Math.round(Math.round((parseInt(this.handle.style.left) * valueSpace) / elemWidth) / this.moveBy); 
					this.unitContainer.innerHTML = this.minValue + (document.forms[0][this.inputContainer].value * this.moveBy);
					this.priceContainer.innerHTML = (parseInt(document.forms[0][this.inputContainer].value)*this.price) + " &euro;/kk";
					tentacle_core.updateTotal();
					tentacle_core.countBackup()
					}
				}
			});
		}
	tentacle_core.updateTotal();
	}
