
var min_width=0;
var max_width=0;
var min_drop=0;
var max_drop=0;
var price=0;
var discount=0;

var discountrm=0;
var price_rm=0;
var price_mm=0;

// disable sau enable link pe butonul Add to Basket - radul

function addTo(){
	if(VerifyVenetianMMForm(document.form_calcvenetianmm))
		document.form_calcvenetianmm.submit();
	}


/*function ControlAddBasket(){ 
    var button = document.getElementById("add_to_basket");
	if(document.form_calcvenetianmm.custom_drop.value != '' && document.form_calcvenetianmm.custom_width.value != '' document.form_calcvenetianmm.price.value != ''){
		button.onclick = function(){
			document.forms[0].submit(); return false;
		};
	}
	else if( document.form_calcvenetianmm.custom_drop.value == '' ||  document.form_calcvenetianmm.custom_width.value == '')
		button.onclick = function(){
			VerifyVenetianMMForm(document.form_calcvenetianmm);		
		}
	}
	
	return false;
}*/

// calculates the minimum price for current product
function getMinimumPrice(form_calc)
{
	/* MM */
	var discount=venetianmm_arr[0][6] / 100;
	var min_price=venetianmm_arr[0][7][3][0];
	
	for(i=0; i<venetianmm_arr.length; i++)
	{	
			var slat_arr=venetianmm_arr[i][7];
			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;
				}
			}
		
	}

	/* RM */
	/* REMOVED
	if(typeof(venetian_rm_arr) != "undefined") {
		var discount_rm=venetian_rm_arr[0];
		var min_price_rm=venetian_rm_arr[1][2][3][0];
		
		for(i=1; i<venetian_rm_arr.length; i++)
		{	
				var slat_arr_rm=venetian_rm_arr[i][2];
				for(j=0; j<slat_arr_rm[0].length; j++)
				{	
					var cur_price_rm=slat_arr_rm[3][j]*(1-discount_rm);
					if (min_price_rm>cur_price_rm)
					{
						min_price_rm=cur_price_rm;
					}
				}
			
		}
		if(min_price > min_price_rm)
			min_price = min_price_rm;
	} // end if RM
	*/
	
	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 GetSlatValue(form){
	var slat_value;
	for (i=0;i<form.slat.length;i++) {
		if (form.slat[i].checked)
			slat_value = form.slat[i].value;
	}
	return slat_value;
}

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["lift"] && params["tilt"] && params["brakets"] && params["recess"])
    {
        document.form_calcvenetianmm.lift_control.value = params["lift"];
        document.form_calcvenetianmm.tilt_control.value = params["tilt"];
        document.form_calcvenetianmm.brackets.value = params["brakets"];
        document.form_calcvenetianmm.recess.value = params["recess"];
        selectBrackets(document.form_calcvenetianmm.brackets.value);
    }

	/* TAKE SLAT FROM QQ AND SELECT IT AS DEFAULT - RaduL*/
	price = 0;
	slat = params["slat"];

	if(slat > 0) {
		var slatObj = document.form_calcvenetianmm.slat;
		for(k = 0; k < slatObj.length; k++) {
			if(slatObj[k].value == slat )
				slatObj[k].checked = true;
			//else
				//slatObj[k].checked = false;
		}
		document.form_calcvenetianmm.slat_value.value = slat;
	}
	
	if((document.form_calcvenetianmm.custom_drop.value!="")&&(document.form_calcvenetianmm.custom_width.value!="")) {		
		GetCustomPrice();		
	}
	/* ******************************************* */
	CheckControls();
}


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 selectSlat(slat)
{
	document.form_calcvenetianmm.slat_value.value=slat; // radul
	document.form_calcvenetianmm.price.value="";
	document.form_calcvenetianmm.saveprice.value="";
	//document.form_calcvenetianmm.addtobasket.disabled=true;
	//ControlAddBasket('disable');
	var slat_poz=-1;
	for(i=0; i<venetianmm_arr.length;i++)
	{
		if(venetianmm_arr[i][0]==slat)
			slat_poz=i;
	}
	if(slat_poz<0)
		return;
	
	min_width=parseInt(venetianmm_arr[slat_poz][2]);
	max_width=parseInt(venetianmm_arr[slat_poz][3]);
	min_drop=parseInt(venetianmm_arr[slat_poz][4]);
	max_drop=parseInt(venetianmm_arr[slat_poz][5]);
	
	price=0;
	
	discount=venetianmm_arr[slat_poz][6] / 100;

	params = getParams();
	
	width = params["width"];
	drop = params["drop"];
	//slat = params["slat"];
	outside = params["outside"];
		
	if((width!="")&&(typeof(width) != "undefined"))
		document.form_calcvenetianmm.custom_width.value=unescape(width);
	if((drop!="")&&(typeof(drop) != "undefined"))
		document.form_calcvenetianmm.custom_drop.value=unescape(drop);
	if((slat!="")&&(typeof(slat) != "undefined"))
		document.form_calcvenetianmm.slat.value=unescape(slat);

	if((outside!="")&&(typeof(outside) != "undefined"))
		document.form_calcvenetianmm.recess.value = outside;
	var recess=document.form_calcvenetianmm.recess.value;
	
	if((document.form_calcvenetianmm.custom_drop.value!="")&&(document.form_calcvenetianmm.custom_width.value!=""))
		GetCustomPrice();
}

function AddSlats()
{
	var slat_value = GetSlatValue(document.form_calcvenetianmm);
	document.form_calcvenetianmm.slat_value.value = slat_value; // put selected slat's value in a hidden field
	
	var recess=document.form_calcvenetianmm.recess.value;

	/* REMOVED
	if(typeof(venetian_rm_arr) != "undefined" && venetian_rm_arr.length>0) // RM
		discountrm=venetian_rm_arr[0];
	*/
	
	selectSlat(slat_value); //select default slat
	var recess=document.form_calcvenetianmm.recess.value;
	selectRecess(recess);
}

function selectRecess(recess)
{
	if(recess==0) // INSIDE
	{
		document.form_calcvenetianmm.recess.value=0;
		if((document.form_calcvenetianmm.custom_width.value!="")&&(document.form_calcvenetianmm.custom_drop.value!=""))
			GetCustomPrice();
	}
	else if(recess==5)
	{
		document.form_calcvenetianmm.recess.value=0;
	}
	else // OUTSIDE
	{
		GetCustomPrice();
		/* RM or MM price */
		if(price_rm >0)
		{
			document.form_calcvenetianmm.price.value = formatDecimal(price_rm, true, 2);
		}
		else // MM price
			document.form_calcvenetianmm.price.value = formatDecimal(price_mm, true, 2);
	}
}

function selectBrackets(brackets)
{
	GetCustomPrice();
	/*	
	var cur_brackets_price=0;
	var base_price=parseFloat(price);
	if(brackets!=0)
		cur_brackets_price=1.95;
		
	if(base_price==0)
		return;

	//find slat
	var slat=-1;
	for(i=0;i<venetianmm_arr.length; i++)
	{
		if(venetianmm_arr[i][0]==document.form_calcvenetianmm.slat.value)
			slat=i;
	}
	if(slat<0)
		return;

	var discount=venetianmm_arr[slat][6] / 100; // MM discount
	*/
/*
	document.form_calcvenetianmm.price.value=formatDecimal((price+cur_brackets_price)*(1-discount), true, 2);
	document.form_calcvenetianmm.saveprice.value=formatDecimal((price+cur_brackets_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; 
	//ControlAddBasket('disable');
}

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 GetRMPrice(custom_width, custom_drop)
{
	document.form_calcvenetianmm.venetianrm.value=0;
	var slat=-1;
	
	/* radul */
	var slattext="";

	for(i=0; i<venetianmm_arr.length; i++) {
		if(venetianmm_arr[i][0]==document.form_calcvenetianmm.slat_value.value)
			slattext = venetianmm_arr[i][1];
	}

	for(i=0; i<venetian_rm_arr.length; i++)
	{
		if(venetian_rm_arr[i][1]==slattext)
			slat=i;
	}
	/* end radul */
		
	if(slat!=-1)
	{
		for(i=0; i<venetian_rm_arr[slat][2][0].length; i++)
		{
			if((venetian_rm_arr[slat][2][1][i]==custom_width)&&(venetian_rm_arr[slat][2][2][i]==custom_drop))
			{
				document.form_calcvenetianmm.venetianrm.value=venetian_rm_arr[slat][2][0][i];
				document.form_calcvenetianmm.venetianrm_slat.value=venetian_rm_arr[slat][0];
				return venetian_rm_arr[slat][2][3][i];
			}
		}
	}
	return 0;	
}

function GetCustomPrice()
{
	price = 0;
	var custom_width=parseInt(document.form_calcvenetianmm.custom_width.value);
	var custom_drop=parseInt(document.form_calcvenetianmm.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 slat=-1;
	for(i=0;i<venetianmm_arr.length; i++)
	{
		if(venetianmm_arr[i][0]==document.form_calcvenetianmm.slat_value.value)
			slat=i;
	}
	if(slat<0)
		return;


	var recess=document.form_calcvenetianmm.recess.value;
	document.form_calcvenetianmm.venetianrm.value=0;
	document.form_calcvenetianmm.brackets.disabled = false;

	if(recess==0)
		custom_width-=12;
	
	/* REMOVED
	if(typeof(venetian_rm_arr) != "undefined") {
		discount=discountrm;
		price=GetRMPrice(custom_width, custom_drop);
		price_rm=price;
	}
	*/
	
	if(recess==0)
		custom_width+=12;

	price_mm=GetPrice(venetianmm_arr[slat][7], custom_width, custom_drop);	
	
	//alert("Slat: " + slat);

	if(!price || price == 0)
	{
		discount=venetianmm_arr[slat][6] / 100; // MM discount		
		price = price_mm;
		if(price==0)
		{
			var strMaxWidth=GetMaxWidth(venetianmm_arr[slat][7], custom_drop);
			alert("Maximum width for the selected drop is "+strMaxWidth+" mm");
			return;
		}
	}	

	//brackets price
	brackets_price = bracketPrice();
	
	price += brackets_price;
	
	/* REMOVED
	if(price_rm > 0) { // RM price
		brackets_price = 0;		
		document.form_calcvenetianmm.brackets[0].selected = true;
		document.form_calcvenetianmm.brackets.disabled = true;
	}
	*/
	
	document.form_calcvenetianmm.price.value=formatDecimal((price)*(1-discount), true, 2);
	document.form_calcvenetianmm.saveprice.value=formatDecimal((price)*discount, true, 2);

	
	
	if(price==price_rm)
		price_rm=document.form_calcvenetianmm.price.value;
	else
		price_mm=document.form_calcvenetianmm.price.value;
	
	RRP.get();
}

/* ---------------------------------------------------------- */
function getDiscount()
{
	return venetianmm_arr[0][6] / 100;	
}

function bracketPrice()
{
	var brackets=document.form_calcvenetianmm.brackets;
	var brackets_price=0;
	if(brackets.value!=0)
		brackets_price = 0.78 / (1 - getDiscount()); // + 0.78 lire
	return brackets_price;
}

function nextdayPrice()
{
	var delivery = document.form_calcvenetianmm.nextday;
	var delivery_price = 0;
	if(delivery.value != 0)
		delivery_price = 5 / (1 - getDiscount()); // + 5 lire
	return delivery_price;
}

function findSlat()
{
//find slat
	var slat=-1;
	for(i=0;i<venetianmm_arr.length; i++)
	{
		if(venetianmm_arr[i][0]==document.form_calcvenetianmm.slat_value.value)
			slat=i;
	}
	if(slat<0)
		return;
	return parseInt(slat);
}
/* ---------------------------------------------------------- */


function VerifyVenetianMMForm(form)
{
	if(form.price.value<=0)
	{
		alert('Please insert width and drop first and click the button "Calculate Price"');
		return false;
	}
	return true; 
}

function OnChangeLiftControl(lift)
{
	tilt=document.form_calcvenetianmm.tilt_control;
	if(lift==1)
		tilt.options[0]=new Option("Left");
	else
		tilt.options[0]=new Option("Right");
}

function remControls(tilt, lift){
	lift.options.length = 1;
	lift.options[0] = new Option("No", "-1");
	tilt.options.length = 1;
	tilt.options[0] = new Option("No", "-1");
}

function setControls(tilt, lift){
	lift.options.length = 2;
	lift.options[0] = new Option("Right", "1");
	lift.options[1] = new Option("Left", "2");
	lift.selectedIndex = 1;
	tilt.options.length = 2;
	tilt.options[0] = new Option("Right", "1");
	tilt.options[1] = new Option("Left", "2");
	tilt.selectedIndex = 0;
}

function CheckControls(){
	var custom_width = parseInt(document.form_calcvenetianmm.custom_width.value);
	var custom_drop = parseInt(document.form_calcvenetianmm.custom_drop.value);
	var lift = document.form_calcvenetianmm.lift_control;
	var tilt = document.form_calcvenetianmm.tilt_control;
	
	// width between 251 si 455 => alert box
	if (custom_width >= 251 && custom_width <= 455){
		alert("Blinds between 251-455mm can only have standard controls!");
		lift.value = 1;
		tilt.value = 2;
	}
	
	// width between 210 si 250 => controls not available
	if(custom_width >= 210 && custom_width <= 250){
		alert("Blinds between 210-250mm wide do not have controls!");
		remControls(tilt, lift);
	}
}

function SetControlsOptions(){
	var custom_width = parseInt(document.form_calcvenetianmm.custom_width.value);
	var lift = document.form_calcvenetianmm.lift_control;
	var tilt = document.form_calcvenetianmm.tilt_control;
	if(custom_width > 250 && tilt.options.length == 1){
		setControls(tilt, lift);
	}
}
