var min_width=0;
var max_width=0;
var min_drop=0;
var max_drop=0;
var price=0;
var shape_price=0;
var discount=0;

function sortArrayPrice(a,b){
	return a - b;
}
var arr = new Array();
arr = eval('Array('+pleatedmm_arr[5][3]+')');
var sorter = new Array();
sorter = arr.sort(sortArrayPrice);
	
function getMinimumPrice(){
	minP = sorter;
	var discount_ = pleatedmm_arr[4];
	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 SetOtherParams()
{
    params = getParams();
    if (params["controls"] && params["recess"])
    {
        document.form_calcpleatedmm.controls.value = params["controls"];
        document.form_calcpleatedmm.recess.value = params["recess"];
    }
}

function SetParams()
{
	/*var widthminmax=document.form_calcpleatedmm.widthminmax;
	widthminmax.options[1]=null;
	widthminmax.options[0]=null;
	
	widthminmax.options[0]=new Option(pleatedmm_arr[0], 1);*/
	min_width=parseInt(pleatedmm_arr[0]);
	//widthminmax.options[1]=new Option(pleatedmm_arr[1], 2);
	max_width=parseInt(pleatedmm_arr[1]);
	//widthminmax.value=2;
	
	/*var dropminmax=document.form_calcpleatedmm.dropminmax;
	dropminmax.options[0]=null;
	
	dropminmax.options[0]=new Option(pleatedmm_arr[2], 1);*/
	min_drop=parseInt(pleatedmm_arr[2]);
	//dropminmax.options[1]=new Option(pleatedmm_arr[3], 2);
	max_drop=parseInt(pleatedmm_arr[3]);
	//dropminmax.value=2;
	
	discount=pleatedmm_arr[4];
	
	params = getParams();
	
	width = params["width"];
	drop = params["drop"];
	
	if((width!="")&&(typeof(width) != "undefined"))
		document.form_calcpleatedmm.custom_width.value=unescape(width);
	if((drop!="")&&(typeof(drop) != "undefined"))
		document.form_calcpleatedmm.custom_drop.value=unescape(drop);
	if((document.form_calcpleatedmm.custom_width.value!="")&&(document.form_calcpleatedmm.custom_drop.value!=""))
		GetCustomPrice();
}

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 ChangePrice(custom_ctrl, form_calc)
{
	custom_ctrl.value="";
	form_calc.price.value="";
	form_calc.saveprice.value="";
	form_calc.addtobasket.disabled=false;
}

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;
		//alert("dif curenta: "+cur_dif_width+"   width: "+sizes_arr[1][i]+" custom width:"+custom_width);
		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_calcpleatedmm.custom_width.value);
	var custom_drop=parseInt(document.form_calcpleatedmm.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(pleatedmm_arr[5], custom_width, custom_drop);
	
	if(price==0)
	{
		var strMaxWidth=GetMaxWidth(pleatedmm_arr[5], custom_drop);
		alert("Maximum width for the selected drop is "+strMaxWidth+" mm");
		return;
	}
	
	document.form_calcpleatedmm.price.value=formatDecimal(price*(1-discount), true, 2);
	document.form_calcpleatedmm.saveprice.value=formatDecimal(price*discount, true, 2);
	
	document.form_calcpleatedmm.addtobasket.disabled=false;
	
	RRP.get();
}

function VerifyPleatedMMForm(form)
{
	if(form.price.value<=0)
	{
		alert('Please insert width and drop first and click the button "Calculate Price"');
		return false;
	}
	return true; 
}
function ConvertInchToMm()
	{
		var custom_inches=document.form_calcpleatedmm.custom_inches;
		var custom_mm=document.form_calcpleatedmm.custom_mm;
		
		custom_mm.value=formatDecimal(parseFloat(custom_inches.value.replace(",",".").replace(",","."))*25.4, false, 0);
	}