
/*********************************************************************************/	

function CheckReferrer() {
	var ref = document.referrer;
	if(ref != "http://www.webwoodenblinds.co.uk/woven-blinds-next-day.html")
		document.form_calcwoven.nextday.selectedIndex = 1; // select default standard when not coming from above links
	else
		document.form_calcwoven.nextday.selectedIndex = 0; // select nextday
}

function sortArrayPrice(a,b){
	return a - b;
}

function getMinimumPrice(form_calc)
{
	var min_price_rm = min_price_stl = 9999;
	
	// MM
	var discount_mm= woven_arr[0];
	var min_price_mm=woven_arr[1][6][3][0];
	for(i=1; i<woven_arr.length; i++)
	{	
			var slat_arr=woven_arr[i][6];
			for(j=0; j<slat_arr[0].length; j++)
			{	
				var cur_price_mm=slat_arr[3][j]*(1-discount_mm);
				if (min_price_mm>cur_price_mm)
				{
					min_price_mm=cur_price_mm;
				}
			}
		
	}

	// RM
	if(typeof(woven_rm_arr) != "undefined" && woven_rm_arr.length>0) {
		var discount_rm=woven_rm_arr[0];
		var min_price_rm=woven_rm_arr[1][2][3][0];
		for(i=1; i<woven_rm_arr.length; i++)
		{	
				var slat_arr=woven_rm_arr[i][2];
				for(j=0; j<slat_arr[0].length; j++)
				{	
					var cur_price_rm=slat_arr[3][j]*(1-discount_rm);
					if (min_price_rm>cur_price_rm)
					{
						min_price_rm=cur_price_rm;
					}
				}
			
		}
	}

	// STL
	if(typeof(woven_stl_arr) != "undefined" && woven_stl_arr.length>0) {
		var discount_stl=woven_stl_arr[0];
		var min_price_stl=woven_stl_arr[1][2][3][0];
		for(i=1; i<woven_stl_arr.length; i++)
		{	
				var slat_arr=woven_stl_arr[i][2];
				for(j=0; j<slat_arr[0].length; j++)
				{	
					var cur_price_stl=slat_arr[3][j]*(1-discount_stl);
					if (min_price_stl>cur_price_stl)
					{
						min_price_stl=cur_price_stl;
					}
				}
			
		}
	}

	var arr = new Array(min_price_mm, min_price_rm, min_price_stl);
	var sorter = new Array();
	sorter = arr.sort(sortArrayPrice);
	
	min_price=formatDecimal(sorter[0],true,2);

	document.getElementById('minprice').innerHTML = min_price;
}

/**************************************************************************************/
	
var discount=0;
var min_width=0;
var max_width=0;
var min_drop=0;
var max_drop=0;
var price=0;
//var tape_price=0;
var recess_price=0;

var price_stl=0;
var discountstl=0;

var discountrm=0;
var price_rm=0;
var price_mm=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["style"] && params["returns"])
    {
        document.form_calcwoven.woven_style.value = params["style"];
	selectStyle(document.form_calcwoven.woven_style.value);
        document.form_calcwoven.controls.value = params["controls"];
        document.form_calcwoven.recess.value = params["recess"];
	/*selectRecess(document.form_calcwoven.recess.value);*/
        document.form_calcwoven.returns.value = params["returns"];
	selectReturns(document.form_calcwoven.returns.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 selectStyle(woven_style)
{
	document.form_calcwoven.price.value="";
	document.form_calcwoven.saveprice.value="";
	//document.form_calcwoven.addtobasket.disabled=true;
	var style_poz=-1;
	for(i=1; i<woven_arr.length;i++)
	{
		if(woven_arr[i][1]==woven_style)
			style_poz=i;
	}
	if(style_poz<0)
		return;
	
	//add min max
	/*var widthminmax=document.form_calcwoven.widthminmax;
	widthminmax.options[1]=null;
	widthminmax.options[0]=null;
	
	widthminmax.options[0]=new Option(woven_arr[style_poz][2], 1);*/
	min_width=parseInt(woven_arr[style_poz][2]);
	//widthminmax.options[1]=new Option(woven_arr[style_poz][3], 2);
	max_width=parseInt(woven_arr[style_poz][3]);
	//widthminmax.value=2;
	
	/*var dropminmax=document.form_calcwoven.dropminmax;
	dropminmax.options[0]=null;
	
	dropminmax.options[0]=new Option(woven_arr[style_poz][4], 1);*/
	min_drop=parseInt(woven_arr[style_poz][4]);
	//dropminmax.options[1]=new Option(woven_arr[style_poz][5], 2);
	max_drop=parseInt(woven_arr[style_poz][5]);
	//dropminmax.value=2;

	price=0;
	
	params = getParams();
	
	width = params["width"];
	drop = params["drop"];
	style = params["style"];
	
	if((width!="")&&(typeof(width) != "undefined"))
		document.form_calcwoven.custom_width.value=unescape(width);
	if((drop!="")&&(typeof(drop) != "undefined"))
		document.form_calcwoven.custom_drop.value=unescape(drop);
	if((style!="")&&(typeof(style) != "undefined"))
		document.form_calcwoven.woven_style.value=unescape(style);

	if((document.form_calcwoven.custom_drop.value!="")&&(document.form_calcwoven.custom_width.value!=""))
		GetCustomPrice();
}

function AddStyles()
{
	/* new */
	var discount=woven_arr[0]; // new

	if(typeof(woven_rm_arr) != "undefined" && woven_rm_arr.length > 0) // RM
		discountrm=woven_rm_arr[0];
	if(typeof(woven_stl_arr) != "undefined" && woven_stl_arr.length > 0) // STL
		discountstl=woven_stl_arr[0];

	/* *** */
	
	selectStyle(document.form_calcwoven.woven_style.value); //select default style
	/*selectRecess(document.form_calcwoven.recess.value);*/
}

/*function selectRecess(recess)
{
	var returns=document.form_calcwoven.returns;
	if(recess==1)
	{
		returns.options[1]=null;
		returns.options[1]=null;
		returns.options[1]=null;
	}
	else
	{
		// new 
		if(price_rm || price_stl) {			
			noReturns();
		}
		else {
			returns.options[1]=new Option("Left side £4.40", 2);
			returns.options[2]=new Option("Right side £4.40", 3);
			returns.options[3]=new Option("Both sides £4.40", 4);
		}
		
		// OLD
		//returns.options[1]=new Option("Left side £4.40", 2);
		//returns.options[2]=new Option("Right side £4.40", 3);
		//returns.options[3]=new Option("Both sides £4.40", 4);
		// 
	}
	
	//astea 3 linii le-am bagat ! ! !
	price=0;
	if((document.form_calcwoven.custom_drop.value!="")&&(document.form_calcwoven.custom_width.value!=""))
		GetCustomPrice();
	//astea 3 linii le-am bagat ! ! !	
}*/

function selectReturns(returns)
{
	var cur_returns_price=0;
	//var base_price=parseFloat(price); // old
	var base_price = 0; // new
	if(returns!=1)
		cur_returns_price=4.4;
	/* new */
	if (price_rm >0) // we have rm price
	{
		base_price=parseFloat(price_rm);
	}
	else if (price_stl >0) // we have stl price
	{
		base_price=parseFloat(price_stl);
	}
	else if (price_mm >0) // mm price
	{
		base_price=parseFloat(price_mm);
	}
	/* *** */

	if(base_price==0)
		return;
		
	document.form_calcwoven.price.value=formatDecimal((base_price+cur_returns_price)*(1-discount), true, 2);
	document.form_calcwoven.saveprice.value=formatDecimal((base_price+cur_returns_price)*discount, 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;
}

/* new */
function GetRMPrice(custom_width, custom_drop)
{
	document.form_calcwoven.romanrm.value=0;

	//find style
	var style_poz=-1;
	for(i=1;i<woven_rm_arr.length; i++)
	{
		if(woven_rm_arr[i][1]==document.form_calcwoven.woven_style.value)
			style_poz=i;
	}

	if(style_poz!=-1)
	{
		for(i=0; i<woven_rm_arr[style_poz][2][0].length; i++)
		{
			if((woven_rm_arr[style_poz][2][1][i]==custom_width)&&(woven_rm_arr[style_poz][2][2][i]==custom_drop))
			{
				//alert("am gasit pret "+woven_rm_arr[slat][2][3][i]+", id "+woven_rm_arr[slat][2][0][i]);
				document.form_calcwoven.romanrm.value=woven_rm_arr[style_poz][2][0][i];
				document.form_calcwoven.romanrmstyle.value=woven_rm_arr[style_poz][0]; // ID STYLE
				return woven_rm_arr[style_poz][2][3][i];
			}
		}
	}
	return 0;
}
/*
function GetSTLPrice(prices_arr, width, drop) // OLD FORMAT
{
	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;
		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];
		}
		if((prices_arr[1][i]==sel_width)&&(cur_dif_drop==0))
		{			
			//if(cur_dif_width==0)
				//price=prices_arr[3][i];
			//else
				//price=prices_arr[3][i]+16.60;

			price=prices_arr[3][i] + 16.60;
			document.form_calcwoven.romanstl.value=prices_arr[0][i];			
		}
	}
	return price;
}
*/
function GetSTLPriceNew(width, drop) // NEW FORMAT
{
	document.form_calcwoven.romanstl.value=0;
	
	var min_dif_width=-1;
	var sel_width=-1;
	var price=0;

	//find style
	var style_poz=-1;
	for(i=1;i<woven_stl_arr.length; i++)
	{
		if(woven_stl_arr[i][1]==document.form_calcwoven.woven_style.value)
			style_poz=i;
	}

	if(style_poz!=-1)
	{
		var prices_arr = woven_stl_arr[style_poz][2];

		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];
			}
			if((prices_arr[1][i]==sel_width)&&(cur_dif_drop==0))
			{
				/*
				if(cur_dif_width==0)
					price=prices_arr[3][i];
				else
					price=prices_arr[3][i]+16.60;
				*/
				//
				//price=prices_arr[3][i] + 16.60; // adds ~10 to the rm price
				
				//price=prices_arr[3][i] + 10.84; // adds 6.5 to the rm price
				price=prices_arr[3][i]; // adds 6.5 to the rm price
				
				document.form_calcwoven.romanstl.value=prices_arr[0][i];
				document.form_calcwoven.romanstlstyle.value=woven_stl_arr[style_poz][0]; // ID STYLE
			}
		} // end for 
	} // end if style found
	return price;
}


function noReturns() // prevents using returns for rm and stl
{
	for(i=0; i<document.form_calcwoven.returns.options.length;)
	{
		document.form_calcwoven.returns.options[i]=null;
	}	
	document.form_calcwoven.returns.options[0]=new Option("no", 1);
}
/* *** */
	
function GetCustomPrice()
{
	var custom_width=parseInt(document.form_calcwoven.custom_width.value);
	var custom_drop=parseInt(document.form_calcwoven.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 style_poz=-1;
	for(i=1;i<woven_arr.length; i++)
	{
		if(woven_arr[i][1]==document.form_calcwoven.woven_style.value)
			style_poz=i;
	}
	if(style_poz<0)
		return;
	
	//price=GetPrice(woven_arr[style_poz][6], custom_width, custom_drop); // old
	
	/* new */
	price = 0;	
	
	if(typeof(woven_rm_arr) != "undefined")
		price_rm=GetRMPrice(custom_width, custom_drop);	

	if(typeof(woven_stl_arr) != "undefined")
		//price_stl=GetSTLPrice(woven_stl_arr[style_poz][2], custom_width, custom_drop);	
		price_stl=GetSTLPriceNew(custom_width, custom_drop);
	
	price_mm=GetPrice(woven_arr[style_poz][6], custom_width, custom_drop);
	
	if(price_rm) {
		price = price_rm;
		discount = discountrm; 
		//noReturns();
		//alert('RM!');		
		
			/* tests */
			price_rm_test = formatDecimal((price)*(1-discount), true, 2)
			price_rm_save_test = formatDecimal((price)*discount, true, 2);
			//alert("PRICE RM: " + price_rm_test + "\nSAVE RM: " + price_rm_save_test + "\nTOTAL: " + price);
		
	}
	else if(price_stl) {
		price = price_stl;
		discount = discountstl;
		//noReturns();

		if (price)
		{
			document.form_calcwoven.roman_stl_sample.value=roman_stl_sample;
			//document.form_calcwoodmm.woodstlslat.value=woven_stl_arr[slat][0];
		}		
		
			/* tests */
			price_stl_test = formatDecimal((price)*(1-discount), true, 2)
			price_stl_save_test = formatDecimal((price)*discount, true, 2);
			//alert("PRICE STL: " + price_stl_test + "\nSAVE STL: " + price_stl_save_test + "\nTOTAL: " + price);
		
	}
	else if(price_mm) {
		price = price_mm;
		discount=woven_arr[0];

			/* tests */
			price_mm_test = formatDecimal((price)*(1-discount), true, 2)
			price_mm_save_test = formatDecimal((price)*discount, true, 2);
			//alert("PRICE MM: " + price_mm_test + "\nSAVE MM: " + price_mm_save_test + "\nTOTAL: " + price);
		
	}
			
	/* *** */
	
	if(price==0)
	{
		var strMaxWidth=GetMaxWidth(woven_arr[style_poz][6], custom_drop);
		alert("Maximum width for the selected drop is "+strMaxWidth+" mm");
		return;
	}
	
	var cur_returns_price=0;
	if(document.form_calcwoven.returns.value!=1)
		cur_returns_price=4.4;

	// nextday price
	var delivery = document.form_calcwoven.nextday;
	var delivery_price = 0;
	
	if (delivery.value != 0)
		delivery_price = 5 / (1 - discount);
		
	price += delivery_price;
	var total_price = price + cur_returns_price;
	var save_price = formatDecimal(total_price * discount, true, 2);
	
	document.form_calcwoven.price.value = formatDecimal(total_price - save_price, true, 2);
	document.form_calcwoven.saveprice.value = formatDecimal(save_price, true, 2);

	//document.form_calcwoven.price.value=formatDecimal((price+cur_returns_price)*(1-discount), true, 2);
	//document.form_calcwoven.saveprice.value=formatDecimal((price+cur_returns_price)*discount, true, 2);
	
	document.form_calcwoven.addtobasket.disabled=false;

}

function VerifyWovenForm(form)
{
	if(form.price.value<=0)
	{
		alert('Please insert width and drop first and click the button "Price Calculator"');
		return false;
	}
	return true; 
}

function ConvertInchToMm()
	{
		var custom_inches=document.form_calcwoven.custom_inches;
		var custom_mm=document.form_calcwoven.custom_mm;
		
		custom_mm.value=formatDecimal(parseFloat(custom_inches.value.replace(",",".").replace(",","."))*25.4, false, 0);
	}

function SelectProdType(prodtype)
{
  window.location = prodtype;
}
