/*=======================FACETTE===============================*/

var min_width=0;
var max_width=0;
var min_drop=0;
var max_drop=0;
var price=0;

/**************************************************************/

// calculates the minimum price for current product
function getMinimumPrice()
{
	//var discount=venetianmm_arr[0][6] / 100;
	var min_price=facette_arr[0][6][3][0];
	
	for(i=0; i<facette_arr.length; i++)
	{	
			var slat_arr=facette_arr[i][6];
			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;
				}
			}
		
	}
	mp = formatDecimal(min_price,true,2);
	if(typeof(sale) != 'undefined' && sale.sale > 0)
		mp = formatDecimal(mp - (mp * sale.sale /100), true,2);
	document.getElementById('minprice').innerHTML = mp;
}

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 SetOtherParams()
{
    params = getParams();
    if (params["width"] && params["drop"])
    {
        document.form_calcfacette.custom_width.value = params["width"];
        document.form_calcfacette.custom_drop.value = params["drop"];
    }

	if (params["slat"])
    {
        document.form_calcfacette.slat_value.value = params["slat"];
        selectSlat(document.form_calcfacette.slat_value.value);

		var slatObj = document.form_calcfacette.slat;
		for(k = 0; k < slatObj.length; k++) {
			if(slatObj[k].value == params["slat"])
				slatObj[k].checked = true;
		}
    }

}

function selectSlat(slat)
{
	document.form_calcfacette.slat_value.value=slat; // radul

	document.form_calcfacette.price.value="";
	document.form_calcfacette.saveprice.value="";
	document.form_calcfacette.addtobasket.disabled=false;
	var slat_poz=-1;
	for(i=0; i<facette_arr.length;i++)
	{
		if(facette_arr[i][0]==slat)
			slat_poz=i;
	}
	if(slat_poz<0)
		return;
		
	min_width=parseInt(facette_arr[slat_poz][2]);
	max_width=parseInt(facette_arr[slat_poz][3]);
	min_drop=parseInt(facette_arr[slat_poz][4]);
	max_drop=parseInt(facette_arr[slat_poz][5]);
	
	price=0;
	
	params = getParams();
	
	width = params["width"];
	drop = params["drop"];
	
	if((width!="")&&(typeof(width) != "undefined"))
		document.form_calcfacette.custom_width.value=unescape(width);
	if((drop!="")&&(typeof(drop) != "undefined"))
		document.form_calcfacette.custom_drop.value=unescape(drop);
	
	if((document.form_calcfacette.custom_drop.value!="")&&(document.form_calcfacette.custom_width.value!=""))
		GetCustomPrice();
}

function AddSlats()
{
	//add slats
	/* radul */
	var slat_value = GetSlatValue(document.form_calcfacette);
	//document.form_calcfacette.slat_value.value = slat_value; // put selected slat's value in a hidden field

	selectSlat(slat_value); //select default slat
	
}

function GetSlatValue(form){ // radul
	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 VerifyFacetteForm(form)
{
	if(form.price.value<=0)
	{
		alert('Please insert width and drop first and click the button "Calculate Price"');
		return false;
	}
	return true; 
}

function GetPrice(prices_arr, width, drop)
{
	var min_dif_width=-1, min_dif_drop=-1;
	var sel_width=-1, sel_drop=-1;
	var price=0;
	for(i=0;i<prices_arr[0].length;i++)
	{
		var cur_dif_width=prices_arr[1][i]-width;
		var cur_dif_drop=prices_arr[2][i]-drop;
		
		if(((min_dif_width==-1)||(cur_dif_width<min_dif_width))&&(cur_dif_width>=0))
		{
			min_dif_width=cur_dif_width;
			sel_width=prices_arr[1][i];
			sel_drop=-1;
			min_dif_drop=-1;
		}
		
		if(prices_arr[1][i]==sel_width)
			if(((min_dif_drop==-1)||(cur_dif_drop<min_dif_drop))&&(cur_dif_drop>=0)&&(min_dif_width!=-1))
			{
				min_dif_drop=cur_dif_drop;
				sel_drop=prices_arr[2][i];
				price=prices_arr[3][i];
			}
	}
	return price;
}
	
function GetMaxWidth(prices_arr, drop)
{
	var max_width=0;
	var min_dif_drop=-1;
	var sel_drop=-1;
	for(i=0;i<prices_arr[0].length;i++)
	{
		var cur_dif_drop=prices_arr[2][i]-drop;
		if(((min_dif_drop==-1)||(cur_dif_drop<min_dif_drop))&&(cur_dif_drop>=0))
		{
			min_dif_drop=cur_dif_drop;
			sel_drop=prices_arr[2][i];
			max_width=0;
		}
		
		if((prices_arr[2][i]==sel_drop)&&(max_width<prices_arr[1][i]))
			max_width=prices_arr[1][i];
	}
	return max_width;
}

	
function GetCustomPrice() 
{
	var custom_width=parseInt(document.form_calcfacette.custom_width.value);
	var custom_drop=parseInt(document.form_calcfacette.custom_drop.value);
	if((min_width>custom_width)||(max_width<custom_width)||(isNaN(custom_width)))
	{
		alert("Please enter width between "+min_width+"mm (min) and "+max_width+"mm (max)");
		return false;
	}
	if((min_drop>custom_drop)||(max_drop<custom_drop)||(isNaN(custom_drop)))
	{
		alert("Please enter drop between "+min_drop+"mm (min) and "+max_drop+"mm (max)");
		return false;
	}
	
	//find slat
	var slat=-1;
	for(i=0;i<facette_arr.length; i++)
	{
		if(facette_arr[i][0]==document.form_calcfacette.slat_value.value)
			slat=i;
	}
	if(slat<0)
		return;
	
	price=GetPrice(facette_arr[slat][6], custom_width, custom_drop);
	
	if(price==0)
	{
		var strMaxWidth=GetMaxWidth(facette_arr[slat][6], custom_drop);
		alert("Maximum width for the selected drop is "+strMaxWidth+" mm");
		return;
	}
	
	document.form_calcfacette.price.value = formatDecimal(price*(1-discount), true, 2);
	document.form_calcfacette.saveprice.value = formatDecimal(price*discount, true, 2);
	document.form_calcfacette.addtobasket.disabled=false;
	
	RRP.get();
	
}

function ChangePrice(custom_ctrl, form_calc)
{
	custom_ctrl.value="";
	form_calc.price.value="";
	form_calc.saveprice.value="";
}

/**************************************************************/
