var min_width=0;
var max_width=0;
var min_drop=0;
var max_drop=0;
var price=0;
var shape_price=0;

function sortArrayPrice(a,b){
	return a - b;
}
var arr = new Array();
arr = eval('Array('+blackoutmm_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;
	//document.getElementById('minprice').innerHTML = (minP[0].toFixed(2) * (1-discount_)).toFixed(2);
	}

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["shape"] && params["braid"] && params["rods"] && params["recess"])
    {
        document.form_calcblackoutmm.controls.value = params["controls"];
	
	document.form_calcblackoutmm.finish_shape.value = params["shape"];
	selectFinish(document.form_calcblackoutmm.finish_shape.value);
	document.form_calcblackoutmm.braid.value = params["braid"];
	selectBraid(document.form_calcblackoutmm.braid.value);
	document.form_calcblackoutmm.cafe_rods.value = params["rods"];
	document.form_calcblackoutmm.recess.value = params["recess"];
    }
}

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 SetParams()
{
	/*var widthminmax=document.form_calcblackoutmm.widthminmax;
	widthminmax.options[1]=null;
	widthminmax.options[0]=null;
	
	widthminmax.options[0]=new Option(blackoutmm_arr[0], 1);*/
	min_width=parseInt(blackoutmm_arr[0]);
	//widthminmax.options[1]=new Option(blackoutmm_arr[1], 2);
	max_width=parseInt(blackoutmm_arr[1]);
	//widthminmax.value=2;
	
	/*var dropminmax=document.form_calcblackoutmm.dropminmax;
	dropminmax.options[0]=null;
	
	dropminmax.options[0]=new Option(blackoutmm_arr[2], 1);*/
	min_drop=parseInt(blackoutmm_arr[2]);
	//dropminmax.options[1]=new Option(blackoutmm_arr[3], 2);
	max_drop=parseInt(blackoutmm_arr[3]);
	//dropminmax.value=2;
	params = getParams();
	
	width = params["width"];
	drop = params["drop"];
	
	if((width!="")&&(typeof(width) != "undefined"))
		document.form_calcblackoutmm.custom_width.value=unescape(width);
	if((drop!="")&&(typeof(drop) != "undefined"))
		document.form_calcblackoutmm.custom_drop.value=unescape(drop);
	
	if((document.form_calcblackoutmm.custom_width.value!="")&&(document.form_calcblackoutmm.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=true;
	
	//reset shapes value
	/*for(i=0;i<shapes_arr.length;i++)
	{
		form_calc.finish_shape.options[i]=new Option(shapes_arr[i][1], shapes_arr[i][0]);
	}*/
}

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 GetShapePrice(prices_arr, width)
{
	var min_dif_width=-1;
	var sel_width=-1;
	var price=0;
	for(i=0;i<prices_arr[0].length;i++)
	{
		var cur_dif_width=prices_arr[1][i]-width;
		//alert("dif curenta: "+cur_dif_width+"   width: "+prices_arr[1][i]+" custom width:"+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];
			price=prices_arr[3][i];
		}
	}
	//alert(price);
	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 GetPriceForBraid(braid, finish_shape)
{
	var finish_type=0;
	var finish_poz=-1;
	for(i=0; i<finishtypes_arr.length; i++)
		if(finishtypes_arr[i][0]==finish_shape)
		{
			finish_type=finishtypes_arr[i][2];
			finish_poz=i;
		}

	if(finish_type!=0)
	{
		var poz_price=0;
		if(finish_type==1)
		{
			if(braid==1)
				poz_price=4;
			else
				return 0;
		}
		else
		if((finish_type==2)||(finish_type==3))
		{
			if(braid == 0)
				poz_price = 3;
			else
			if((braid==1)||(braid==2))
				poz_price=4;
			else
			if(braid==3)
				poz_price=5;
			else
				return 0;
		}
		else
			return 0;
		//alert("GetShapePrice");
		
		return GetShapePrice(finishtypes_arr[finish_poz][poz_price], parseInt(document.form_calcblackoutmm.custom_width.value));
	}
	return 0;
}
	
function GetCustomPrice()
{
	var custom_width=parseInt(document.form_calcblackoutmm.custom_width.value);
	var custom_drop=parseInt(document.form_calcblackoutmm.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=Math.round(GetPrice(blackoutmm_arr[4], custom_width, custom_drop)*100)/100;
	
	if(price==0)
	{
		var strMaxWidth=GetMaxWidth(blackoutmm_arr[0][4], custom_drop);
		alert("Maximum width for the selected drop is "+strMaxWidth+" mm");
		return;
	}
	
	//set shapes prices
	var finish_shape=document.form_calcblackoutmm.finish_shape.value;
	var braid=document.form_calcblackoutmm.braid.value;
	var cur_braid_price=0;
	if((!braid)&&((finish_shape==2)||(finish_shape==3)))
		braid=1;		
	cur_braid_price=GetPriceForBraid(braid, finish_shape);
//  cur_controls_price = 0;
//  if ( document.form_calcblackoutmm.controls.value == 'Spring' ) cur_controls_price = 2.35;
	var total_price=price+cur_braid_price;
	var save_price=formatDecimal(price*discount_, true, 2);
	document.form_calcblackoutmm.price.value=formatDecimal(total_price-save_price, true, 2);
	document.form_calcblackoutmm.saveprice.value=formatDecimal(save_price, true, 2);
	
	document.form_calcblackoutmm.addtobasket.disabled=false;
        
        if (document.form_calcblackoutmm.custom_width.value >= 875)
        {
            var found = false;
            for(j=0; j<document.form_calcblackoutmm.controls.options.length; j++)
        	if(document.form_calcblackoutmm.controls.options[j].value == "Spring")
                    found = true;
            if (!found)
                document.form_calcblackoutmm.controls.options[document.form_calcblackoutmm.controls.options.length]=new Option("Spring with Centre Pull", "Spring");
        }
        else
        {
            for(j=0; j<document.form_calcblackoutmm.controls.options.length; j++)
                if(document.form_calcblackoutmm.controls.options[j].value == "Spring")
                    document.form_calcblackoutmm.controls.options[j]=null;
        }
    RRP.get();    
	selectFinish(finish_shape);
}

function selectFinish(finish)
{
	var braids=document.form_calcblackoutmm.braid;
	var sel_braid=braids.value;
	var cafe_rods=document.form_calcblackoutmm.cafe_rods;
	var braid_price=0;
	for(; braids.options.length>0;)
		braids.options[0]=null;
		
	for(; cafe_rods.options.length>0;)
		cafe_rods.options[0]=null;

	var poz_finish=-1;		
	for(i=0; i<finishtypes_arr.length; i++)
		if(finishtypes_arr[i][0]==finish)
			poz_finish=i;
			
	if(poz_finish==-1)
		return;

	if(finishtypes_arr[poz_finish][2]==1)
	{
		braids.options[0]=new Option("No Braids", 0);
		braid_price=GetPriceForBraid(1, finish);
		braids.options[1]=new Option("With Braids", 1);
		cafe_rods.options[0]=new Option("no", 0);
                if(sel_braid>1)
		    sel_braid=1;
	}
	else
	{
		if(sel_braid==0)
			sel_braid=1;
		braid_price=GetPriceForBraid(0, finish);
		braids.options[0]=new Option("No Braids", 0);
		braid_price=GetPriceForBraid(1, finish);
		braids.options[1]=new Option("Front", 1);
		braid_price=GetPriceForBraid(2, finish);
		braids.options[2]=new Option("Back", 2);
		braid_price=GetPriceForBraid(3, finish);
		braids.options[3]=new Option("Both Sides", 3);
		if(finishtypes_arr[poz_finish][2]==3)
		{
			cafe_rods.options[0]=new Option("White Sphere", "WhiteWashSphere");
			cafe_rods.options[1]=new Option("Natural Sphere", "NaturalSphere");
			cafe_rods.options[2]=new Option("Teak Sphere", "TeakSphere");
			cafe_rods.options[3]=new Option("Chrome", "Chrome");
			cafe_rods.options[4]=new Option("White Temple", "WhiteWashTemple");
			cafe_rods.options[5]=new Option("Natural Temple", "NaturalTemple");
			cafe_rods.options[6]=new Option("Teak Temple", "TeakTemple");
			cafe_rods.options[7]=new Option("Brass", "Brass");
		}
		else
			cafe_rods.options[0]=new Option("no", 0);
	}
        if (document.form_calcblackoutmm.braid.value == 0)
		sel_braid=0;
	else
	braids.value=sel_braid;
	selectBraid(sel_braid);
}

function selectBraid(braid)
{
	var base_price=price;
	if(base_price<=0)
		return;
	
	var finish_shape=document.form_calcblackoutmm.finish_shape.value;
	var cur_braid_price=0;
	if((!braid)&&((finish_shape==2)||(finish_shape==3)))
		braid=1;
		
	cur_braid_price=GetPriceForBraid(braid, finish_shape);
//  cur_controls_price = 0;
//  if ( document.form_calcblackoutmm.controls.value == 'Spring' ) cur_controls_price = 2.35;
	var total_price=price+cur_braid_price;
	var save_price=price*discount_;
	document.form_calcblackoutmm.price.value=formatDecimal(total_price-save_price, true, 2);
	document.form_calcblackoutmm.saveprice.value=formatDecimal(save_price, true, 2);
	
	RRP.get();
}

function selectScallops(control)
{
	for(; document.form_calcblackoutmm.finish_shape.options.length>0;)
		document.form_calcblackoutmm.finish_shape.options[0]=null;

	document.form_calcblackoutmm.finish_shape.options[0]=new Option("No Scallops", "8");
	document.form_calcblackoutmm.finish_shape.options[1]=new Option("Classic", "11");
	document.form_calcblackoutmm.finish_shape.options[2]=new Option("Wave", "12");
	document.form_calcblackoutmm.finish_shape.options[3]=new Option("Colonial", "13");
	document.form_calcblackoutmm.finish_shape.options[4]=new Option("Pyramid", "14");
	document.form_calcblackoutmm.finish_shape.options[5]=new Option("New Delhi", "15");
	document.form_calcblackoutmm.finish_shape.options[6]=new Option("Tunnel", "16");
	document.form_calcblackoutmm.finish_shape.options[7]=new Option("Quad", "17");
	document.form_calcblackoutmm.finish_shape.options[8]=new Option("Arches", "18");
	document.form_calcblackoutmm.finish_shape.options[9]=new Option("Castille", "19");
	document.form_calcblackoutmm.finish_shape.options[10]=new Option("Turret", "20");

	selectFinish(1);
}

function VerifyBlackoutMMForm(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_calcblackoutmm.custom_inches;
	var custom_mm=document.form_calcblackoutmm.custom_mm;
	
	custom_mm.value=formatDecimal(parseFloat(custom_inches.value.replace(",",".").replace(",","."))*25.4, false, 0);
}

function onChangeCloth(){
	if (document.form_calcblackoutmm.recess.value == "Cloth"){
		alert("Please note that the end brackets will add to the cloth size 35mm - 50mm and may not fit inside recess.");
	}
}
