var discount=woven_arr[0];
var min_width=0;
var max_width=0;
var min_drop=0;
var max_drop=0;
var price=0;
var recess_price=0;

/*********************************************************************************/	
function getMinimumPrice(form_calc)
{
	var discount_= woven_arr[0];
	var min_price=woven_arr[1][6][3][0];
	for(i=1; i<woven_arr.length; i++)
	{	
			var slat_arr=woven_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;
				}
			}
		
	}
	min_price=formatDecimal(min_price,true,2);
	if(typeof(sale) != 'undefined' && sale.sale > 0)
		min_price = formatDecimal(min_price - (min_price * sale.sale /100), true,2);
	document.getElementById('minprice').innerHTML = min_price;
}

/**************************************************************************************/
	
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["style"] && params["returns"])
    {
        document.form_calcwoven.woven_style.value = params["style"];
	selectStyle(document.form_calcwoven.woven_style.value);
        document.form_calcwoven.controls.value = params["controls"];
        document.form_calcwoven.recess.value = params["recess"];
	selectRecess(document.form_calcwoven.recess.value);
        document.form_calcwoven.returns.value = params["returns"];
	selectReturns(document.form_calcwoven.returns.value);
    }
}

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 selectStyle(woven_style)
{
	document.form_calcwoven.price.value="";
	document.form_calcwoven.saveprice.value="";
	//document.form_calcwoven.addtobasket.disabled=false;
	var style_poz=-1;
	for(i=1; i<woven_arr.length;i++)
	{
		if(woven_arr[i][1]==woven_style)
			style_poz=i;
	}
	if(style_poz<0)
		return;
	
	//add min max
	/*var widthminmax=document.form_calcwoven.widthminmax;
	widthminmax.options[1]=null;
	widthminmax.options[0]=null;
	
	widthminmax.options[0]=new Option(woven_arr[style_poz][2], 1);*/
	min_width=parseInt(woven_arr[style_poz][2]);
	//widthminmax.options[1]=new Option(woven_arr[style_poz][3], 2);
	max_width=parseInt(woven_arr[style_poz][3]);
	//widthminmax.value=2;
	
	/*var dropminmax=document.form_calcwoven.dropminmax;
	dropminmax.options[0]=null;

	
	dropminmax.options[0]=new Option(woven_arr[style_poz][4], 1);*/
	min_drop=parseInt(woven_arr[style_poz][4]);
	//dropminmax.options[1]=new Option(woven_arr[style_poz][5], 2);
	max_drop=parseInt(woven_arr[style_poz][5]);
	//dropminmax.value=2;

	price=0;
	
	params = getParams();
	
	width = params["width"];
	drop = params["drop"];
	style = params["style"];
	
	if((width!="")&&(typeof(width) != "undefined"))
		document.form_calcwoven.custom_width.value=unescape(width);
	if((drop!="")&&(typeof(drop) != "undefined"))
		document.form_calcwoven.custom_drop.value=unescape(drop);
	if((style!="")&&(typeof(style) != "undefined"))
		document.form_calcwoven.woven_style.value=unescape(style);

	if((document.form_calcwoven.custom_drop.value!="")&&(document.form_calcwoven.custom_width.value!=""))
		GetCustomPrice();
}

function AddStyles()
{
	//add styles
	/*var woven_style=document.form_calcwoven.woven_style;
	for(i=0;i<woven_arr.length;i++)
		if(woven_arr[i][1]==1)
			woven_style.options[i]=new Option("Roman Style", woven_arr[i][0]);
		else
			woven_style.options[i]=new Option("Roll-up Style", woven_arr[i][0]);*/
	
	selectStyle(document.form_calcwoven.woven_style.value); //select default style
	selectRecess(document.form_calcwoven.recess.value);
}

function selectRecess(recess)
{
	var returns=document.form_calcwoven.returns;
	if(recess==1)
	{
		returns.options[1]=null;
		returns.options[1]=null;
		returns.options[1]=null;
	}
	else
	{
		returns.options[1]=new Option("Left side £4.40", 2);
		returns.options[2]=new Option("Right side £4.40", 3);
		returns.options[3]=new Option("Both sides £4.40", 4);
	}
	
	//astea 3 linii le-am bagat ! ! !
	price=0;
	if((document.form_calcwoven.custom_drop.value!="")&&(document.form_calcwoven.custom_width.value!=""))
		GetCustomPrice();
	//astea 3 linii le-am bagat ! ! !
}

function selectReturns(returns)
{
	var cur_returns_price=0;
	var base_price=parseFloat(price);
	if(returns!=1)
		cur_returns_price=4.4;

	if(base_price==0)
		return;
		
	document.form_calcwoven.price.value=formatDecimal((base_price+cur_returns_price)*(1-discount), true, 2);
	document.form_calcwoven.saveprice.value=formatDecimal((base_price+cur_returns_price)*discount, true, 2);
	
	RRP.get();
}
	
function ChangePrice(custom_ctrl, form_calc)
{
	custom_ctrl.value="";
	form_calc.price.value="";
	form_calc.saveprice.value="";
	//form_calc.addtobasket.disabled=true; 
	//form_calc.tape.options[1]=null;
}

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_calcwoven.custom_width.value);
	var custom_drop=parseInt(document.form_calcwoven.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 style_poz=-1;
	for(i=1;i<woven_arr.length; i++)
	{
		if(woven_arr[i][1]==document.form_calcwoven.woven_style.value)
			style_poz=i;
	}
	if(style_poz<0)
		return;
	
	price=GetPrice(woven_arr[style_poz][6], custom_width, custom_drop);
	
	if(price==0)
	{
		var strMaxWidth=GetMaxWidth(woven_arr[style_poz][6], custom_drop);
		alert("Maximum width for the selected drop is "+strMaxWidth+" mm");
		return;
	}
	
	var cur_returns_price=0;
	if(document.form_calcwoven.returns.value!=1)
		cur_returns_price=4.4;
		
	document.form_calcwoven.price.value=formatDecimal((price+cur_returns_price)*(1-discount), true, 2);
	document.form_calcwoven.saveprice.value=formatDecimal((price+cur_returns_price)*discount, true, 2);
	
	document.form_calcwoven.addtobasket.disabled=false;
	
	RRP.get();
}

function VerifyWovenForm(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_calcwoven.custom_inches;
		var custom_mm=document.form_calcwoven.custom_mm;
		
		custom_mm.value=formatDecimal(parseFloat(custom_inches.value.replace(",",".").replace(",","."))*25.4, false, 0);
	}
