
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 discount=0;

// disable sau enable link pe butonul Add to Basket - radul
function addTo(){
	if(VerifyWoodForm(document.form_calcwood))
		document.forms[0].submit();
	}

/*function ControlAddBasket(){ 
    var button = document.getElementById("add_to_basket");
	if(document.form_calcwood.custom_drop.value != '' && document.form_calcwood.custom_width.value != '')
		button.onclick = function(){
			document.forms[0].submit(); return false;
			};
			
	if( document.form_calcwood.custom_drop.value == '' ||  document.form_calcwood.custom_width.value == '')
		button.onclick = function(){VerifyWoodForm(document.form_calcwood);		}
	
	return false;
}*/

// calculates the minimum price for current product
function getMinimumPrice(form_calc)
{
	var discount=wood_arr[0];
	var min_price=wood_arr[1][6][3][0];
	for(i=1; i<wood_arr.length; i++)
	{	
			var slat_arr=wood_arr[i][6];
			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["tape"] && params["recess"] && params["lift"] && params["tilt"])
        {
            document.form_calcwood.tape.value = params["tape"];
            document.form_calcwood.recess.value = params["recess"];
            document.form_calcwood.lift_control.value = params["lift"];
            document.form_calcwood.tilt_control.value = params["tilt"];
        }

	/* TAKE SLAT FROM QQ AND SELECT IT AS DEFAULT - RaduL*/
	price = 0;
	slat = params["slat"];

	if(slat > 0) {
		var slatObj = document.form_calcwood.slat;
		for(k = 0; k < slatObj.length; k++) {
			if(slatObj[k].value == slat )
				slatObj[k].checked = true;
		}
		document.form_calcwood.slat_value.value = slat;
	}

	if((document.form_calcwood.custom_drop.value!="")&&(document.form_calcwood.custom_width.value!="")) {		
		GetCustomPrice();		
	}
	/* ******************************************* */

	if (document.form_calcwood.custom_width.value > 0 && document.form_calcwood.custom_drop.value)
	{
		selectRecess(document.form_calcwood.recess.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 isSlatCol(slat_text,value)
{
	if(value==0)
		return true;

	if(tapeCol!='' || typeof(tapeCol)!="undefined")
	{
		for(i=0;i<tapeCol.length;i++)
		{
			if(tapeCol[i][0]==slat_text)
			{
				for(j=0;j<tapeCol[i][1].length;j++)
					if(tapeCol[i][1][j]==value)
						return true;
				return false;
			}
		}
	}
	return true;
}

function selectSlat(slat)
{
	document.form_calcwood.slat_value.value=slat; // radul
	document.form_calcwood.price.value="";
	document.form_calcwood.saveprice.value="";
	//document.form_calcwood.addtobasket.disabled=true; DE CORECTAT!
	//ControlAddBasket('enable');
	var slat_poz=-1;
	for(i=0; i<wood_arr.length;i++)
	{
		if(wood_arr[i][0]==slat)
			slat_poz=i;
	}
	if(slat_poz<0)
		return;
	
	/*nu sunt afisate culorile care nu sunt in array pt slatul curent*/
		/*********************************/
		
		var slat_text=wood_arr[slat_poz][1];
		var selectbox = document.getElementById('lista');
		var list = selectbox.childNodes;
    	for(var i = 0; i< list.length; i++)
		{
     		if(list[i].nodeName == 'LI')
		    {
	       		if(isSlatCol(slat_text,list[i].value)==false)
						list[i].style.display='none';
				else
						list[i].style.display='block';
          	}
     	}
		
        var	dropdown=document.getElementById('country');
		if(isSlatCol(slat_text,document.form_calcwood.tape.value)==false)
			setDDValue(dropdown,'','No tape', 0);
		/*******************************/
	
	//add min max
	min_width=parseInt(wood_arr[slat_poz][2]);
	max_width=parseInt(wood_arr[slat_poz][3]);
	min_drop=parseInt(wood_arr[slat_poz][4]);
	max_drop=parseInt(wood_arr[slat_poz][5]);
	
	price=0;
	price_tape=0;
	
			params = getParams();
		
		width = params["width"];
		drop = params["drop"];

		//slat_param = params["slat"]; // radul
		
		if((width!="")&&(typeof(width) != "undefined"))
			document.form_calcwood.custom_width.value=unescape(width);
		if((drop!="")&&(typeof(drop) != "undefined"))
			document.form_calcwood.custom_drop.value=unescape(drop);	 	
		if((slat!="")&&(typeof(slat) != "undefined")) //radul
	   		document.form_calcwood.slat_value.value=unescape(slat);
	
	if((document.form_calcwood.custom_drop.value!="")&&(document.form_calcwood.custom_width.value!=""))
		GetCustomPrice();
}

function AddSlats()
{
	//add slats
	//var slat=document.form_calcwood.slat;
	document.form_calcwood.slat[0].checked = true; // select first slat
	var slat_value = GetSlatValue(document.form_calcwood);
	document.form_calcwood.slat_value.value = slat_value; // put selected slat's value in a hidden field
	document.form_calcwood.tape.value=0;
	discount=wood_arr[0];
	selectSlat(slat_value); //select default slat
}

function selectTape(tape)
{
	var cur_tape_price=0;
	var cur_recess_price=0;
	var base_price=parseFloat(price);

	if(tape==0)
		cur_tape_price=0;
	else
		cur_tape_price=parseFloat(price_tape);
	
	if(document.form_calcwood.recess.value>=2)
		cur_recess_price=4;

	if(base_price==0)
		return;

	document.form_calcwood.price.value=formatDecimal((base_price+cur_tape_price+cur_recess_price)*(1-discount), true, 2);
	document.form_calcwood.saveprice.value=formatDecimal((base_price+cur_tape_price+cur_recess_price)*discount, true, 2);
}

function selectRecess(recess)
{
	var cur_tape_price=0;
	var cur_recess_price=0;
	var base_price=parseFloat(price);
	
	if(document.form_calcwood.tape.value==0)
		cur_tape_price=0;
	else
		cur_tape_price=parseFloat(price_tape);
		
	if(recess>=2)
		cur_recess_price=4;
	
	if(base_price==0)
		return;
		
	document.form_calcwood.price.value=formatDecimal((base_price+cur_tape_price+cur_recess_price)*(1-discount), true, 2);
	document.form_calcwood.saveprice.value=formatDecimal((base_price+cur_tape_price+cur_recess_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; DE CORECTAT!
	//ControlAddBasket('enable');
	//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;
}
	
function GetCustomPrice()
{
	price = 0;
	var custom_width=parseInt(document.form_calcwood.custom_width.value);
	var custom_drop=parseInt(document.form_calcwood.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<wood_arr.length; i++)
	{
		if(wood_arr[i][0]==document.form_calcwood.slat_value.value)
			slat=i;
	}
	if(slat<0)
		return;

	price=GetPrice(wood_arr[slat][6], custom_width, custom_drop);
	
	if(price==0)
	{
		var strMaxWidth=GetMaxWidth(wood_arr[slat][6], custom_drop);
		alert("Maximum width for the selected drop is "+strMaxWidth+" mm");
		return;
	}
	
	var cur_recess_price=0;
	if(document.form_calcwood.recess.value>=2)
		cur_recess_price=4;
	
	//add tapes
	var tape=document.form_calcwood.tape;
	price_tape=0;
	if(wood_arr[slat][9]!=null)
		price_tape=formatDecimal(GetPrice(wood_arr[slat][9], custom_width, custom_drop), true, 2);		
	
	if(price_tape!=0)
	{
		var strTape=wood_arr[slat][8]+" adds "+price_tape+String.fromCharCode(163);
		//tape.options[1]=new Option(strTape, wood_arr[slat][7]);
	}
	
	document.form_calcwood.price.value=formatDecimal((price+cur_recess_price)*(1-discount), true, 2);
	document.form_calcwood.saveprice.value=formatDecimal((price+cur_recess_price)*discount, true, 2);
	
	selectTape(tape.value);
	//document.form_calcwood.addtobasket.disabled=false; DE CORECTAT!
	//ControlAddBasket();
}

function VerifyWoodForm(form)
{
	if(form.price.value<=0)
	{
		alert('Please insert width and drop first and click the button "Calculate Price"');
		return false;
	}
	return true; 
}
