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;
var delivery = 0;

var discountrm=0;
var price_rm=0;
var price_mm=0;

function sortArrayPrice(a,b){
	return(a-b);
}

// MM
var arr = new Array();
arr = eval('Array('+rollermm_arr[5][3]+')');
var sorter = new Array();
sorter = arr.sort(sortArrayPrice);

// RM
var arrRM = new Array();
if(typeof(rollerrm_arr) != "undefined" && rollerrm_arr.length > 0) {
	arrRM = eval('Array('+rollerrm_arr[3]+')');
}
var sorterRM = new Array();
sorterRM = arrRM.sort(sortArrayPrice);

function getMinimumPrice(){
	var minpriceMM = minpriceRM = 9999;
	
	// MM
	minP = sorter;
	var discount_ = rollermm_arr[4];
	minpriceMM = (minP[0].toFixed(2) * (1-discount_)).toFixed(2);
	//alert("MM: " + minpriceMM);

	// RM
	if(typeof(rollerrm_arr) != "undefined" && rollerrm_arr.length > 0) {
		minPRM = sorterRM;
		var discountRM = rollerrm_arr[0];
		minpriceRM = (minPRM[0].toFixed(2) * (1-discountRM)).toFixed(2);
		//alert("RM: " + minpriceRM);
	}
	
	var minArray = new Array();
	minArray[0] = minpriceMM;
	minArray[1] = minpriceRM;
	var minPrice = minArray.sort(sortArrayPrice);
	
	//alert("MinPrice: " + minPrice[0]);
	//document.getElementById('minprice').innerHTML = minPrice[0]; // REMOVED
	mp = minpriceMM;
	if(typeof(sale) != 'undefined' && sale.sale > 0)
		mp = formatDecimal(mp - (mp * sale.sale /100), true,2);
	document.getElementById('minprice').innerHTML = mp;
	
}

function CheckReferrer() {
	var ref = document.referrer;

	params = getParams();
	var nd = 0; // nextday parameter
	if(params["nd"] == 1)
		nd = 1;

	if(ref != "http://www.blindsuk.net/roller-next-day.html" && ref != "http://www.blindsuk.net/roller-next-day-page2.html" && nd == 0) {
		document.form_calcrollermm.nextday.selectedIndex = 1; // select default "Standard, 7 days" when not coming from above links
		document.form_calcrollermm.nextday2.selectedIndex = 1; // the 2nd select element
	}
	else {
		document.form_calcrollermm.nextday.selectedIndex = 0; // select default NEXTDAY
		document.form_calcrollermm.nextday2.selectedIndex = 0;
	}
}

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_calcrollermm.controls.value = params["controls"];
        //document.form_calcrollermm.finish_shape.value = params["scallop"];
        //selectFinish(document.form_calcrollermm.finish_shape.value);
        //document.form_calcrollermm.braid.value = params["braid"];
        //selectBraid(document.form_calcrollermm.braid.value);
        //document.form_calcrollermm.cafe_rods.value = params["rods"];
        document.form_calcrollermm.recess.value = params["recess"];
        //selectRecess(document.form_calcrollermm.recess.value);
    }
}

function SetOtherParams()
{
    params = getParams();
    if (params["controls"] && params["recess"])
    {
        document.form_calcrollermm.controls.value = params["controls"];
        document.form_calcrollermm.recess.value = params["recess"];
    }
}

function SetParams()
{
	min_width=parseInt(rollermm_arr[0]);
	max_width=parseInt(rollermm_arr[1]);
	min_drop=parseInt(rollermm_arr[2]);
	max_drop=parseInt(rollermm_arr[3]);
	
	discount=parseFloat(rollermm_arr[4]);
	
	params = getParams();
	
	width = params["width"];
	drop = params["drop"];

	outside = params["outside"];

	// default recess OUTSIDE when coming from QQ
	if((outside!="")&&(typeof(outside) != "undefined")) {
		document.form_calcrollermm.recess.value = "Outside";
		var recess=document.form_calcrollermm.recess.value;
	}

	if((width!="")&&(typeof(width) != "undefined"))
		document.form_calcrollermm.custom_width.value=unescape(width);
	if((drop!="")&&(typeof(drop) != "undefined"))
		document.form_calcrollermm.custom_drop.value=unescape(drop);
	if((document.form_calcrollermm.custom_width.value!="")&&(document.form_calcrollermm.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="";
}

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 GetRMPrice(custom_width, custom_drop)
{
	document.form_calcrollermm.rollerrm.value=0;
	var priceRM = 0;

	//document.form_calcrollerrm.price.value = '';
	//document.form_calcrollerrm.saveprice.value = '';
	for(i=0; i<rollerrm_arr[1].length; i++)
	{
		if(custom_width == rollerrm_arr[1][i] && custom_drop == rollerrm_arr[2][i])
		{
			/*
			var salesave = rollerrm_arr[3][i]*rollerrm_arr[0];
			var saleprice = rollerrm_arr[3][i]-salesave;
			document.form_calcrollermm.price.value=formatDecimal(saleprice, true, 2);
			document.form_calcrollermm.saveprice.value=formatDecimal(salesave, true, 2);
			document.form_calcrollermm.rollerrm.value=i+1;
			*/
			document.form_calcrollermm.rollerrm.value=i+1;
			priceRM = rollerrm_arr[3][i];
			
		}
	}
	
	return priceRM;
}

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 price = 0;
	var custom_width=parseInt(document.form_calcrollermm.custom_width.value);
	var custom_drop=parseInt(document.form_calcrollermm.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;
	}


	var recess=document.form_calcrollermm.recess.value;
	document.form_calcrollermm.rollerrm.value=0;
	
	if(recess=="Inside")
		custom_width -= 3;

	/* REMOVED
	if(typeof(rollerrm_arr) != "undefined" && document.form_calcrollermm.nextday.value == 0) { // maybe RM price, but not on nextday!
		discountrm = rollerrm_arr[0];
		discount=discountrm;
		//alert("Width : " + custom_width + "\nDrop: " + custom_drop);
		price=GetRMPrice(custom_width, custom_drop);
		price_rm=price;
		//alert("Price RM: " + price);
	}
	*/
	price_rm = 0;
	
	if(recess=="Inside")
		custom_width += 3;
		
	price_mm = GetPrice(rollermm_arr[5], custom_width, custom_drop);
	
	if(!price || price == 0)
	{
		//alert('NU AM PRET DE RM!');
		discount=parseFloat(rollermm_arr[4]); // MM discount
		price = price_mm;
		if(price == 0) {
			var strMaxWidth=GetMaxWidth(rollermm_arr[5], custom_drop);
			alert("Maximum width for the selected drop is "+strMaxWidth+" mm");
			return;
		}
	}

	// nextday price
	var delivery = document.form_calcrollermm.nextday;
	var delivery_price = 0;

	if (delivery.value != 0 && discount != 1)
		delivery_price = 5 / (1 - discount);

	var total_price = price + delivery_price;
	var save_price = total_price * discount;

	document.form_calcrollermm.price.value=formatDecimal(total_price-save_price, true, 2);
	document.form_calcrollermm.saveprice.value=formatDecimal(save_price, true, 2);
	
	document.form_calcrollermm.addtobasket.disabled=false;
	
	RRP.get();
}

function VerifyrollerMMForm(form)
{
	if(form.price.value<=0)
	{
		alert('Please insert width and drop first and click the button "Calculate Price"');
		return false;
	}
	/*
	// Christmas delivery notification
	var delivery = form.nextday.value;
	if (delivery == 0) // no alert for nextday delivery!
		alert('This product is not guaranteed for Christmas delivery.');
	*/
	return true; 
}

function SelectProdType(prodtype)
{
		window.location = prodtype;
}

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

function SelNextday(source_value, destination) {
	if(source_value == 0) // no nextday
		destination.selectedIndex = 1; // select standard
	else
		destination.selectedIndex = 0; // select nextday
}

