/*=======================DUETTE===============================*/

var min_width=0;
var max_width=0;
var min_drop=0;
var max_drop=0;
var price=0;

/**************************************************************/


function sortArrayPrice(a,b){
	return(a-b);
}

var arr = new Array();
arr = eval('Array('+duette_arr[4][3]+')');
var sorter = new Array();
sorter = arr.sort(sortArrayPrice);
	
function getMinimumPrice(){
	minP = sorter;
	mp = (minP[0].toFixed(2) * (1-discount)).toFixed(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"] /*&& params["controls"] && params["recess"] && params["bracket"]*/)
    {
        document.form_calcduette.custom_width.value = params["width"];
        document.form_calcduette.custom_drop.value = params["drop"];
        /*document.form_calcduette.recess.value = params["recess"];
        document.form_calcduette.controls.value = params["controls"];
        document.form_calcduette.bracket.value = params["bracket"];*/
        GetCustomPrice();
    }
}

function SetParams()
{
	min_width=parseInt(duette_arr[0]);
	max_width=parseInt(duette_arr[1]);
	min_drop=parseInt(duette_arr[2]);
	max_drop=parseInt(duette_arr[3]);
}

/**************************************************************/

function VerifyDuetteForm(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_calcduette.custom_width.value);
	var custom_drop=parseInt(document.form_calcduette.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;
	}
	
	price=GetPrice(duette_arr[4], custom_width, custom_drop);

	if(price==0)
	{
		var strMaxWidth=GetMaxWidth(duette_arr[4], custom_drop);
		alert("Maximum width for the selected drop is "+strMaxWidth+" mm");
		return;
	}

    price+=bracketPrice();
		
	document.form_calcduette.price.value=formatDecimal(price*(1-discount), true, 2);
	document.form_calcduette.saveprice.value=formatDecimal(price*discount, true, 2);
	
	RRP.get();	
}

function bracketPrice()
{
	var bracket_price=0;
	if(document.form_calcduette.bracket.value==1)
		bracket_price=0.53;
	
  return bracket_price;
}

function ChangePrice(custom_ctrl, form_calc)
{
	custom_ctrl.value="";
	form_calc.price.value="";
	form_calc.saveprice.value="";
}

/**************************************************************/

