
// calculates the minimum price for current product
function getMinimumPrice(form_calc)
{
	var discount=wood_stl_arr[0];
	var min_price=wood_stl_arr[1][6][3][0];
	for(i=1; i<wood_stl_arr.length; i++)
	{	
			var slat_arr=wood_stl_arr[i][6];
			for(j=0; j<slat_arr[0].length; j++)
			{	
				var cur_price=(slat_arr[3][j]+15.72)*(1-discount); // la pretul de RM se adauga 6.29 lire
				if (min_price>cur_price)
				{
					min_price=cur_price;
				}
			}
		
	}
	min_price=formatDecimal(min_price,true,2);
	document.getElementById('minprice').innerHTML = min_price;
}

var min_width=0;
var max_width=0;
var min_drop=0;
var max_drop=0;
var price=0;
var recess_price=0;
var discount=0;

function selectRecess(recess)
{
	if(recess==0) // INSIDE
	{
		document.form_calcwoodstl.recess.value=0;
		if((document.form_calcwoodstl.custom_width.value!="")&&(document.form_calcwoodstl.custom_drop.value!=""))
			GetCustomPrice();

	}
	else // OUTSIDE
	{
		GetCustomPrice();
		// RM price SCOS
		/*
		if(price_rm >0)
		{
			document.form_calcwoodstl.price.value = formatDecimal(price_rm, true, 2);
			alert("RM: " + price_rm);
		}
		else // STL price
		*/
			document.form_calcwoodstl.price.value = formatDecimal(price_stl, true, 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 SetOtherParams()
{
	params = getParams();
	if (params["recess"])
		document.form_calcwoodstl.recess.value = params["recess"];

	/* TAKE SLAT FROM QQ AND SELECT IT AS DEFAULT - RaduL*/
	price = 0;
	slat = params["slat"];
	outside = params["outside"];

	if(slat > 0) {
		var slatObj = document.form_calcwoodstl.slat;
		for(k = 0; k < slatObj.length; k++) {
			if(slatObj[k].value == slat )
				slatObj[k].checked = true;
		}
		document.form_calcwoodstl.slat_value.value = slat;
	}

	// default recess OUTSIDE when coming from QQ
	if((outside!="")&&(typeof(outside) != "undefined")) {
		document.form_calcwoodstl.recess.value = outside;
		var recess=document.form_calcwoodstl.recess.value;
	}
	
	if((document.form_calcwoodstl.custom_drop.value!="")&&(document.form_calcwoodstl.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 GetRMPrice(custom_width, custom_drop)
{
	document.form_calcwoodstl.woodrm.value=0;
	var slat=-1;
	var slattext="";
	
	var slatctrl=document.form_calcwoodstl.slat;
	var slat_value;
	
	for (i=0;i<slatctrl.length;i++) {
		if (slatctrl[i].checked)
			slat_value = slatctrl[i].value;
	}
	
	for (i=0;i<slatctrl.length;i++) {   //preluam id-ul slatului selectat
		if (slatctrl[i].checked)
			slat_value = slatctrl[i].value;
	}
	
	var stl_slat=-1;
	for(i=1;i<wood_stl_arr.length; i++)
	{
		if(wood_stl_arr[i][0]==slat_value) //cautam array-ul cu slatul selectat 
			{
				stl_slat=i;
				break;
			}
	}
	
	if (stl_slat<0) return 0;
	slattext=wood_stl_arr[stl_slat][1];
	
	for(i=0; i<wood_rm_arr.length; i++)
	{
		if(wood_rm_arr[i][1]==slattext)
			slat=i;
	}	
	
	if(slat!=-1)
	{
		for(i=0; i<wood_rm_arr[slat][2][0].length; i++)
		{
			if((wood_rm_arr[slat][2][1][i]==custom_width)&&(wood_rm_arr[slat][2][2][i]==custom_drop))
			{				
				document.form_calcwoodstl.woodrm.value=wood_rm_arr[slat][2][0][i];
				document.form_calcwoodstl.woodrmslat.value=wood_rm_arr[slat][0];
				return wood_rm_arr[slat][2][3][i];
			}
		}
	}
	return 0;
}


function selectSlat(slat)
{
	document.form_calcwoodstl.price.value="";
	document.form_calcwoodstl.saveprice.value="";
	document.form_calcwoodstl.addtobasket.disabled=false;

	var custom_drop=document.form_calcwoodstl.custom_drop;
	var curr_drop=document.form_calcwoodstl.custom_drop.value;
	var sel_drop=0;
	var slat_poz=-1;
	for(i=1; i<wood_stl_arr.length;i++)
	{
		if(wood_stl_arr[i][0]==slat)
			slat_poz=i;
	}
	if(slat_poz<0)
		return;
		
	for(k=document.form_calcwoodstl.custom_drop.options.length-1; k>=0; k--)
		document.form_calcwoodstl.custom_drop.options[k]=null;
	
	min_width=parseInt(wood_stl_arr[slat_poz][2]);
	max_width=parseInt(wood_stl_arr[slat_poz][3]);

	min_drop=parseInt(wood_stl_arr[slat_poz][4]);
	max_drop=parseInt(wood_stl_arr[slat_poz][5]);
	
	for(i=0; i<wood_stl_arr[slat_poz][6][0].length; i++)
	{
		var found=false;
		for(j=0; j<custom_drop.options.length; j++)
			if(custom_drop.options[j].value==wood_stl_arr[slat_poz][6][2][i])
				found=true;
		if(!found)
		{
			custom_drop.options[custom_drop.options.length]=new Option(wood_stl_arr[slat_poz][6][2][i]+"mm", wood_stl_arr[slat_poz][6][2][i]);
			if(curr_drop==wood_stl_arr[slat_poz][6][2][i])
				sel_drop=1;
		}
	}
	
	if(sel_drop)
		custom_drop.value=curr_drop;
	else
		custom_drop.value=custom_drop.options[0].value;
	
	price=0;
	
	params = getParams();
	
	width = params["width"];
	drop = params["drop"];
	//slat = params["slat"];
	
	if((width!="")&&(typeof(width) != "undefined"))
		document.form_calcwoodstl.custom_width.value=unescape(width);
	if((drop!="")&&(typeof(drop) != "undefined"))
		document.form_calcwoodstl.custom_drop.value=unescape(drop);
	if((slat!="")&&(typeof(slat) != "undefined"))		
		document.form_calcwoodstl.slat_value.value = slat; // put selected slat's value in a hidden field
	
	if((document.form_calcwoodstl.custom_drop.value!="0")&&(document.form_calcwoodstl.custom_width.value!=""))
		GetCustomPrice();
}

function AddSlats()
{
	var slat=GetSlatValue(document.form_calcwoodstl);
	//form_calcwoodstl.slat_value.value = slat; // hidden field
	discount=wood_stl_arr[0];
	
	var recess=document.form_calcwoodstl.recess.value;
	/* SCOS RM
	if(typeof(wood_rm_arr) != "undefined" && wood_rm_arr.length>0) { // RM ARRAY 
		discountrm=wood_rm_arr[0];
	}
	*/

	selectSlat(slat);
}


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 LoadSlat()
{
	document.form_calcwoodstl.slat[0].checked = true; // select first slat
	
	var slat_value = GetSlatValue(document.form_calcwoodstl);
	document.form_calcwoodstl.slat_value.value = slat_value; // put selected slat's value in a hidden field
	//AddWidths(document.form_calcwoodrm, document.form_calcwoodrm.slat.value, document.form_calcwoodrm.customwidth.value, document.form_calcwoodrm.customdrop.value);
}


function ChangePrice(custom_ctrl, form_calc)
{
	custom_ctrl.value="";
	form_calc.price.value="";
	form_calc.saveprice.value="";
	//form_calc.addtobasket.disabled=true; 
}

function GetPrice(prices_arr, width, drop)
{
	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))
		{
			/* SCOS RM
			if(cur_dif_width==0)
				price=prices_arr[3][i];
			else
			*/
				//price=prices_arr[3][i]+16.60; // -> OLD
				//price=prices_arr[3][i]+12.24; // -> OLD, adauga 7.34 lire la pretul final de rm (cu discount de 0.4 -> 12.24 * (1-0.4))
				//price=prices_arr[3][i]+17.95; // -> OLD, adauga 7.18 lire la pretul final de rm (cu discount de 0.6 -> 17.95 * (1-0.6))
				price=prices_arr[3][i]+15.72; // -> se adauga 6.29 lire la pretul final de rm (pt formula, a se vedea inc-ul)				
		}
	}
	/* old
        }
	*** */
	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;
	price_rm = 0;
	
	var custom_width=parseInt(document.form_calcwoodstl.custom_width.value);
	var custom_drop=parseInt(document.form_calcwoodstl.custom_drop.value);
	
	/* new */
	document.form_calcwoodstl.price.value='';
	document.form_calcwoodstl.saveprice.value='';
	//document.form_calcwoodstl.addtobasket.disabled=true;
	
	var recess=document.form_calcwoodstl.recess.value;
	document.form_calcwoodstl.woodrm.value=0;	

	if (custom_drop==1500 && (isNaN(custom_width) || custom_width<2000 || custom_width>2400))
    {
		alert("Please enter width between 2000mm (min) and "+max_width+"mm (max)");
		return false;
	}
	/* *** */
	
	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 slat_value=GetSlatValue(document.form_calcwoodstl);
	var slat=-1;
	for(i=1;i<wood_stl_arr.length; i++)
	{
		if(wood_stl_arr[i][0]==slat_value)
			slat=i;
	}

	if(slat<0)
		return;
	
	if(recess==0)
		custom_width-=10;
	/* SCOS RM
	if(typeof(wood_rm_arr) != "undefined"  && wood_rm_arr.length>0) { // RM ARRAY
		discountrm=wood_rm_arr[0];
		discount = discountrm;
		price=GetRMPrice(custom_width, custom_drop); // take RM price (if any)
		price_rm=price;
	}
	*/
	
	if(!price) // no price yet
	{		
		discount=wood_stl_arr[0]; // STL DISCOUNT
		
		price_stl=GetPrice(wood_stl_arr[slat][6], custom_width, custom_drop); // STL PRICE
		price = price_stl;
	}	

	if(recess==0)
	custom_width+=10;	

	if(price==0)
	{
		var strMaxWidth=GetMaxWidth(wood_stl_arr[slat][6], custom_drop);
		alert("Maximum width for the selected drop is "+strMaxWidth+" mm");
		return;
	}
	
	document.form_calcwoodstl.price.value=formatDecimal(price*(1-discount), true, 2);
	document.form_calcwoodstl.saveprice.value=formatDecimal(price*discount, true, 2);
	document.form_calcwoodstl.addtobasket.disabled=false;
}

function VerifyWoodStlForm(form)
{
	if(form.price.value<=0)
	{
		alert('Please insert width and drop first and click the button "Calculate Price"');
		return false;
	}
	return true; 
}

function getDiscount(form_calc)
{
		var discount=wood_stl_arr[0];
		discount=formatDecimal(discount,true,2)*100;
		document.getElementById('save').innerHTML = discount;
}

function SelectProdType(prodtype)
{
		window.location = prodtype;
}
