
// disable sau enable link pe butonul Add to Basket - radul
function ControlAddBasket(control){
    var button = document.getElementById("add_to_basket");
	if(control == "enable")
		button.onclick = function(){document.forms[0].submit(); return false;};
	else if(control == "disable")
		button.onclick = "";		
	
	return false;
}

// calculates the minimum price for current product
function getMinimumPrice(form_calc)
{
	var discount=wood_rm_arr[0];
	var min_price=wood_rm_arr[1][2][3][0];
	for(i=1; i<wood_rm_arr.length; i++)
	{	
			var slat_arr=wood_rm_arr[i][2];
			for(j=0; j<slat_arr[0].length; j++)
			{	
				var cur_price=slat_arr[3][j]*(1-discount);
				if (min_price>cur_price)
				{
					min_price=cur_price;
				}
			}
		
	}
	min_price=formatDecimal(min_price,true,2);
	document.getElementById('minprice').innerHTML = min_price;
}


function GetSlatValue(form){
	var slat_value;
	for (i=0;i<form.slat.length;i++) {
		if (form.slat[i].checked)
			slat_value = form.slat[i].value;
	}
	return slat_value;
}

function getParams()
{
	var idx = document.URL.indexOf('?');
	var params = new Array();
	if (idx != -1)
	{
		var pairs = document.URL.substring(idx+1, document.URL.length).split('&');
		for (var i=0; i<pairs.length; i++)
		{
			nameVal = pairs[i].split('=');
			params[nameVal[0]] = nameVal[1];
		}
	}
	return params;
}

function formatDecimal(argvalue, addzero, decimaln) {
  var numOfDecimal = (decimaln == null) ? 2 : decimaln;
  var number = 1;

  number = Math.pow(10, numOfDecimal);

  argvalue = Math.round(parseFloat(argvalue) * number) / number;
  argvalue = "" + argvalue;

  if (argvalue.indexOf(".") == 0)
    argvalue = "0" + argvalue;

  if (addzero == true) {
    if (argvalue.indexOf(".") == -1)
      argvalue = argvalue + ".";

    while ((argvalue.indexOf(".") + 1) > (argvalue.length - numOfDecimal))
      argvalue = argvalue + "0";
  }

  return argvalue;
}

function GetCustomPrice(form_calc, customdrop, slat)
{
	var discount=wood_rm_arr[0];
	for(i=1; i<wood_rm_arr.length; i++)
		if(wood_rm_arr[i][0]==slat)
		{
			var slat_arr=wood_rm_arr[i][2];
			for(j=0; j<slat_arr[0].length; j++)
				if(slat_arr[0][j]==customdrop)
				{
					form_calc.price.value=formatDecimal(slat_arr[3][j]*(1-discount), true, 2);
					form_calc.saveprice.value=formatDecimal(slat_arr[3][j]*discount, true, 2);
				}
		}
		
	RRP.get();
}

function AddDrops(form_calc, slat, customwidth, sel_drop)
{
	if(sel_drop=='')
		sel_drop=form_calc.customdrop.options[form_calc.customdrop.selectedIndex].value;
	for(i=0; i<form_calc.customdrop.options.length; i++)
		form_calc.customdrop.options[0]=null;
		
	for(i=1; i<wood_rm_arr.length; i++)
		if(wood_rm_arr[i][0]==slat)
		{
			var slat_arr=wood_rm_arr[i][2];
			var poz=0;
			for(j=0; j<slat_arr[0].length; j++)
				if(slat_arr[1][j]==customwidth)
				{
					var strText=slat_arr[2][j]+" mm";
					form_calc.customdrop.options[poz]=new Option(strText, slat_arr[0][j]);
					poz++;
					if((sel_drop==slat_arr[2][j])||(sel_drop==strText))
						form_calc.customdrop.value=slat_arr[0][j];
				}
		}
	GetCustomPrice(form_calc, form_calc.customdrop.value, slat);
}

function numSort(a,b)
{ 
       return a - b; 
}

function AddWidths(form_calc, slat, sel_width, sel_drop)
{
	form_calc.slat_value.value = slat; // put selected slat's value in a hidden field
	
	for(k=form_calc.customwidth.options.length-1; k>=0; k--)
		form_calc.customwidth.options[k]=null;
	
	var widths_array = new Array();
	l = 0;
	for(i=1; i<wood_rm_arr.length; i++)
		if(wood_rm_arr[i][0]==slat)
		{
			var slat_arr=wood_rm_arr[i][2];
			for(j=0; j<slat_arr[0].length; j++)
			{
				var width_found = false;
				for (h=0;h<widths_array.length;h++)
				{
					if (widths_array[h] == slat_arr[1][j])
						width_found = true;
				}
				if (!width_found)
				{
					widths_array[l] = parseInt(slat_arr[1][j]);
					l++;
				}
			}
		}

	widths_array.sort(numSort);
	
	for (h=0;h<widths_array.length;h++)
	{
		strText = widths_array[h]+" mm";
		form_calc.customwidth.options[form_calc.customwidth.options.length]=new Option(strText, widths_array[h]);
	}

	if (sel_width)
		form_calc.customwidth.value = sel_width;

	AddDrops(document.form_calcwoodrm, slat, form_calc.customwidth.value, sel_drop);
}

function LoadSlat()
{
	//document.form_calcwoodrm.slat[0].checked = true; // select first slat
	var slat_value = GetSlatValue(document.form_calcwoodrm);

	document.form_calcwoodrm.slat_value.value = slat_value; // put selected slat's value in a hidden field
	//AddWidths(document.form_calcwoodrm, document.form_calcwoodrm.slat.value, document.form_calcwoodrm.customwidth.value, document.form_calcwoodrm.customdrop.value);
	AddWidths(document.form_calcwoodrm, slat_value, document.form_calcwoodrm.customwidth.value, document.form_calcwoodrm.customdrop.value);	
}

function getDropID(slat, width, drop)
{
	for(i=1;i<(wood_rm_arr.length); i++)
	{
		if (slat == wood_rm_arr[i][0])
		{
			arr_no = i;
		}
	}
	
	for(i=0;i<(wood_rm_arr[arr_no][2][1].length); i++)
	{
		if ((wood_rm_arr[arr_no][2][1][i] == width) && (wood_rm_arr[arr_no][2][2][i] == drop))
			return wood_rm_arr[arr_no][2][0][i];
	}
	
}

function LoadValues()
{
	params = getParams();
	
	var slat_param = params["slat"];
	width = params["width"];
	drop = params["drop"];

	if(slat_param > 0) {
		var slatObj = document.form_calcwoodrm.slat;
		for(k = 0; k < slatObj.length; k++) {
			if(slatObj[k].value == slat_param)
				slatObj[k].checked = true;
		}
	}

	
	if((slat_param!="")&&(typeof(slat_param) != "undefined") && (width!="")&&(typeof(width) != "undefined") && (drop!="")&&(typeof(drop) != "undefined"))
	{
		document.form_calcwoodrm.slat_value.value=unescape(slat_param);
		AddWidths(document.form_calcwoodrm, slat_param, '', '');
		document.form_calcwoodrm.customwidth.value=unescape(width);
		AddDrops(document.form_calcwoodrm, slat_param, width, '');
		document.form_calcwoodrm.customdrop.value=getDropID(slat_param, width, drop);
		
		//GetCustomPrice(document.form_calcwoodrm, drop, document.form_calcwoodrm.slat_value.value); -> GRESIT!
		GetCustomPrice(document.form_calcwoodrm, document.form_calcwoodrm.customdrop.value, document.form_calcwoodrm.slat_value.value);
	}
}

function SelectProdType(prodtype)
{
		window.location = prodtype;
}
