
var min_width=0;
var max_width=0;
var min_drop=0;
var max_drop=0;
var price=0;
var discount=0;
var delivery = 0;

// disable sau enable link pe butonul Add to Basket - radul

function addTo(){
	if(VerifyVenetianMMForm(document.form_calcvenetianmm))
		document.forms[0].submit();
	}
	
	
/*function ControlAddBasket(){ 
    var button = document.getElementById("add_to_basket");
	if(document.form_calcvenetianmm.custom_drop.value != '' && document.form_calcvenetianmm.custom_width.value != '')
		button.onclick = function(){
				button.onclick = function(){};
			};
			
	if( document.form_calcvenetianmm.custom_drop.value == '' ||  document.form_calcvenetianmm.custom_width.value == '')
		button.onclick = function(){
			VerifyVenetianMMForm(document.form_calcvenetianmm);		
			}
	
	return false;
}*/

function CheckReferrer() {	
	var ref = document.referrer;

	params = getParams();
	var nd = 0; // nextday parameter
	if(params["nd"] == 1)
		nd = 1;

	/* DEZACTIVARE NEXTDAY */
	if(ref != "http://www.blindsuk.net/venetian-next-day.html" && ref != "http://www.blindsuk.net/venetian-next-day-page1.html" && nd == 0) {
		document.form_calcvenetianmm.nextday.selectedIndex = 1; // select standard when not coming from above links
		//document.form_calcvenetianmm.nextday2.selectedIndex = 1; // the 2nd select element // REMOVED
	}
	else { // select nextday
		document.form_calcvenetianmm.nextday.selectedIndex = 0;
		//document.form_calcvenetianmm.nextday2.selectedIndex = 0; // REMOVED
	}

}


// calculates the minimum price for current product
function getMinimumPrice(form_calc)
{
	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;
				}
			}
		
	}
	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();		
	}
	/* ******************************************* */

}

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];

	var params = getParams();
	
	var width = params["width"];
	var drop = params["drop"];
	//var slat_param = params["slat"];
	
	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((document.form_calcvenetianmm.custom_drop.value!="")&&(document.form_calcvenetianmm.custom_width.value!=""))
		GetCustomPrice();
}

function AddSlats()
{
	//add slats
	/*
	var params = getParams();
	
	var width = params["width"];
	var drop = params["drop"];
	var slat = params["slat"];
	
	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);
	*/
	/*
	document.form_calcvenetianmm.slat[0].checked = true; // select first slat
	var slat1 = document.getElementById("slat1");
	slat1.checked="checked";
	*/
	var slat_value = GetSlatValue(document.form_calcvenetianmm);
	document.form_calcvenetianmm.slat_value.value = slat_value; // put selected slat's value in a hidden field

	selectSlat(slat_value); //select default slat
}

function selectBrackets(brackets)
{
	var cur_brackets_price= bracketPrice();
	var base_price=parseFloat(price);

	if(base_price==0)
		return;
	price += cur_brackets_price;
	
	var save_price=formatDecimal((price)*discount/100, true, 2);
	document.form_calcvenetianmm.saveprice.value=save_price;
	document.form_calcvenetianmm.price.value=formatDecimal(price-save_price, true, 2);
	
	RRP.get();
}

function ChangePrice(custom_ctrl, form_calc)
{
	custom_ctrl.value="";
	form_calc.price.value="";
	form_calc.saveprice.value="";
	price = 0;
	saveprice = 0;
	//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;

		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()
{
	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;
	
	price=GetPrice(venetianmm_arr[slat][7], custom_width, custom_drop);
	
	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;
	
	// nextday price
	var delivery = document.form_calcvenetianmm.nextday;
	var delivery_price = 0;

	/* *** INTRERUPERE TEMPORARA *** */
//	if(delivery.value != 0) {
//		alert("Sorry, NEXT DAY service will return on 06/04/2010.");
//		document.form_calcvenetianmm.nextday.selectedIndex = 1;
//	}
	/* ***************************** */
	/* DE REACTIVAT! */
	if(delivery.value != 0)
		delivery_price = 1 / (1 - discount/100); // + 1 lire
	
	price += delivery_price;
	
	var save_price=formatDecimal((price)*discount/100 , true, 2);
	document.form_calcvenetianmm.saveprice.value = save_price;
	document.form_calcvenetianmm.price.value = formatDecimal(price-save_price, true, 2);
	//document.form_calcvenetianmm.addtobasket.disabled=false;
	//ControlAddBasket();
	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=1.95;
		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 = 1 / (1 - getDiscount()); // + 1 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 checkDelivery(formname) {
	var delivery = formname.nextday.value;
	return delivery;
}
*/

function checkDelivery(nextday)
{
	var cur_delivery_price=0;
	var base_price = parseFloat(price);
	if(nextday != 0)
		cur_delivery_price = 5;
		
	if(base_price == 0)
		return;

	var save_price=formatDecimal((price+cur_delivery_price)*discount/100, true, 2);
	document.form_calcvenetianmm.saveprice.value=save_price;
	document.form_calcvenetianmm.price.value = formatDecimal(base_price+cur_delivery_price-save_price, true, 2);
	
	RRP.get();
}

	
