var min_width=0;
var max_width=0;
var min_drop=0;
var max_drop=0;
var price=0;
var discount=0;

function sortArrayPrice(a,b){
	return a - b;
}
var arr = new Array();
arr = eval('Array('+roman_arr[5][2][3]+')');
var sorter = new Array();
sorter = arr.sort(sortArrayPrice);
	
function getMinimumPrice(){
	minP = sorter;
	document.getElementById('minprice').innerHTML = (minP[0].toFixed(2) * (1-roman_arr[4])).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 GetOtherParams()
{
    params = getParams();
    if (params["rods"] && params["controls"] && params["linen"] && params["recess"] && params["blackout"])
    {
	document.form_calcroman.rods.value = params["rods"];
        selectType(document.form_calcroman.rods.value, document.form_calcroman.linen.value);
	document.form_calcroman.controls.value = params["controls"];
	document.form_calcroman.linen.value = params["linen"];
        selectType(document.form_calcroman.rods.value, document.form_calcroman.linen.value);
	document.form_calcroman.recess.value = params["recess"];
	document.form_calcroman.blackout.value = params["blackout"];
        selectBlackout(document.form_calcroman.blackout.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 selectType(rod, linen)
{
	document.form_calcroman.price.value="";
	document.form_calcroman.saveprice.value="";
	document.form_calcroman.addtobasket.disabled=false;

	price=0;
	
	if(linen==2)
	{
		//show blackout yes/no
		var blackout=document.form_calcroman.blackout;
		blackout.options[0]=new Option("no", 1);
		blackout.options[1]=new Option("yes", 3); // caz pt milltex (in inc se calculeaza cu +20%, la astra e +10%)
	}
	else
	{
		//show blackout not avail
		var blackout=document.form_calcroman.blackout;
		blackout.options[0]=null;
		blackout.options[0]=new Option("no", 1);
		blackout.options[1]=null;
	}
	
	params = getParams();
	
	width = params["width"];
	drop = params["drop"];
	
	if((width!="")&&(typeof(width) != "undefined"))
		document.form_calcroman.custom_width.value=unescape(width);
	if((drop!="")&&(typeof(drop) != "undefined"))
		document.form_calcroman.custom_drop.value=unescape(drop);

	if((document.form_calcroman.custom_drop.value!="")&&(document.form_calcroman.custom_width.value!=""))
		GetCustomPrice();
}

function AddRods()
{
	//add min max
	/*var widthminmax=document.form_calcroman.widthminmax;
	widthminmax.options[1]=null;
	widthminmax.options[0]=null;
	
	widthminmax.options[0]=new Option(roman_arr[0], 1);*/
	min_width=parseInt(roman_arr[0]);
	//widthminmax.options[1]=new Option(roman_arr[1], 2);
	max_width=parseInt(roman_arr[1]);
	//widthminmax.value=2;
	
	/*var dropminmax=document.form_calcroman.dropminmax;
	dropminmax.options[0]=null;
	
	dropminmax.options[0]=new Option(roman_arr[2], 1);*/
	min_drop=parseInt(roman_arr[2]);
	//dropminmax.options[1]=new Option(roman_arr[3], 2);
	max_drop=parseInt(roman_arr[3]);
	//dropminmax.value=2;
	
	discount=parseFloat(roman_arr[4]);
	
	//select type
	selectType(document.form_calcroman.rods.value, document.form_calcroman.linen.value);
}

function selectBlackout(blackout)
{
	var price_blackout=0;
	var base_price=parseFloat(price);
	
	if(base_price==0)
		return;
		
	if(blackout==2)
		price_blackout=price*0.1;
	else
		if(blackout==3) // milltex: 20%
			price_blackout=price*0.2;
			
	var price_prod=formatDecimal(price+price_blackout, true, 2)
	var save_price=formatDecimal(price_prod*discount, true, 2);
	document.form_calcroman.saveprice.value=save_price;
	document.form_calcroman.price.value=formatDecimal(price_prod-save_price, true, 2);
}

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_calcroman.custom_width.value);
	var custom_drop=parseInt(document.form_calcroman.custom_drop.value);
	var rod=document.form_calcroman.rods.value;
	var linen=document.form_calcroman.linen.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 type_poz=-1;
	for(i=5; i<roman_arr.length;i++)
	{
		if((roman_arr[i][0]==rod)&&(roman_arr[i][1]==linen))
			type_poz=i;
	}
	if(type_poz<0)
		return;

	price=GetPrice(roman_arr[type_poz][2], custom_width, custom_drop);
	
	if(price==0)
	{
		var strMaxWidth=GetMaxWidth(roman_arr[type_poz][2], custom_drop);
		alert("Maximum width for the selected drop is "+strMaxWidth+" mm");
		return;
	}
	
	//blackout price
	var price_blackout=0;
	if(document.form_calcroman.blackout.value==2)
		price_blackout=price*0.1;
	else
		if(document.form_calcroman.blackout.value==3) // milltex: 20%
			price_blackout=price*0.2;

	var price_prod=formatDecimal(price+price_blackout, true, 2)
	var save_price=formatDecimal(price_prod*discount, true, 2);
	document.form_calcroman.saveprice.value=save_price;
	document.form_calcroman.price.value=formatDecimal(price_prod-save_price, true, 2);
	
	document.form_calcroman.addtobasket.disabled=false;
}

function VerifyRomanForm(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_calcroman.custom_inches;
		var custom_mm=document.form_calcroman.custom_mm;
		
		custom_mm.value=formatDecimal(parseFloat(custom_inches.value.replace(",",".").replace(",","."))*25.4, false, 0);
	}
