
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(VerifyWoodmmForm(document.form_calcwoodmm))
		document.forms[0].submit();
	}

/*function ControlAddBasket(){ 
    var button = document.getElementById("add_to_basket");
	if(document.form_calcwoodmm.custom_drop.value != '' && document.form_calcwoodmm.custom_width.value != '')
		button.onclick = function(){
			document.forms[0].submit(); return false;
			};
			
	if( document.form_calcwoodmm.custom_drop.value == '' ||  document.form_calcwoodmm.custom_width.value == '')
		button.onclick = function(){VerifyWoodmmForm(document.form_calcwoodmm);		}
	
	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);
	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_calcwoodmm.tape.value = params["tape"];
            document.form_calcwoodmm.recess.value = params["recess"];
            document.form_calcwoodmm.lift_control.value = params["lift"];
            document.form_calcwoodmm.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_calcwoodmm.slat;
		for(k = 0; k < slatObj.length; k++) {
			if(slatObj[k].value == slat )
				slatObj[k].checked = true;
		}
		document.form_calcwoodmm.slat_value.value = slat;
	}

	if((document.form_calcwoodmm.custom_drop.value!="")&&(document.form_calcwoodmm.custom_width.value!="")) {		
		GetCustomPrice();		
	}
	/* ******************************************* */

	if (document.form_calcwoodmm.custom_width.value > 0 && document.form_calcwoodmm.custom_drop.value)
	{
		selectRecess(document.form_calcwoodmm.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_calcwoodmm.slat_value.value = slat; // radul
	document.form_calcwoodmm.price.value = "";
	document.form_calcwoodmm.saveprice.value = "";
	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;
	

		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_calcwoodmm.tape.value)==false)
			setDDValue(dropdown,'','Standard Cords', 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_calcwoodmm.custom_width.value=unescape(width);
		if((drop!="")&&(typeof(drop) != "undefined"))
			document.form_calcwoodmm.custom_drop.value=unescape(drop);	 	
		if((slat!="")&&(typeof(slat) != "undefined")) //radul
	   		document.form_calcwoodmm.slat_value.value=unescape(slat);
	
	if((document.form_calcwoodmm.custom_drop.value!="")&&(document.form_calcwoodmm.custom_width.value!=""))
		GetCustomPrice();
}

function AddSlats()
{
	//add slats
	//var slat=document.form_calcwoodmm.slat;
	//document.form_calcwoodmm.slat[0].checked = true; // select first slat
	var slat_value = GetSlatValue(document.form_calcwoodmm);
	//document.form_calcwoodmm.slat_value.value = slat_value; // put selected slat's value in a hidden field
	document.form_calcwoodmm.tape.value=0;
	discount=wood_arr[0];
	selectSlat(slat_value); //select default slat
	
	
	
		//add slats
	/* document.form_calcwoodmmmm.slat.value 
		slat.value
	*/
	//document.form_calcwoodmmmm.prodtype.value=1;
	
}

function selectTape(tape){
	var cur_tape_price = 0;
	var cur_recess_price = 0;
	var base_price = parseFloat(price);
	var total_price = 0;

	if(tape == 0)
		cur_tape_price = 0;
	else{
			cur_tape_price = parseFloat(price_tape);
			base_price = 0; // we use only the price for tapes 
		}	
	if(document.form_calcwoodmm.recess.value >= 2)
		cur_recess_price = 4;
	total_price = base_price + cur_tape_price + cur_recess_price;
	if(total_price == 0)
		return;
	document.form_calcwoodmm.price.value = formatDecimal(total_price * (1-discount), true, 2);
	document.form_calcwoodmm.saveprice.value = formatDecimal(total_price * discount, true, 2);
}

function selectRecess(recess)
{
	if(recess == 0) // INSIDE
		document.form_calcwoodmm.returns.value=0; // RETURNS NO
	else // OUTSIDE
		document.form_calcwoodmm.returns.value=1; // RETURNS YES
}
	
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_calcwoodmm.custom_width.value);
	var custom_drop=parseInt(document.form_calcwoodmm.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_calcwoodmm.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_calcwoodmm.recess.value >= 2)
		cur_recess_price=4;
	
	//add tapes
	var tape=document.form_calcwoodmm.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_calcwoodmm.price.value=formatDecimal((price+cur_recess_price)*(1-discount), true, 2);
	document.form_calcwoodmm.saveprice.value=formatDecimal((price+cur_recess_price)*discount, true, 2);
	
	selectTape(tape.value);
	//document.form_calcwoodmm.addtobasket.disabled=false; DE CORECTAT!
	//ControlAddBasket();
}

function VerifyWoodmmForm(form)
{
	if(form.price.value<=0)
	{
		alert('Please insert width and drop first and click the button "Price Calculator"');
		return false;
	}
	return true; 
}

function selectReturns(returns)
{
	if(returns == 1 && document.form_calcwoodmm.recess.value == 0){
		var answer = confirm('Pelmet returns are required for installing the blind outside recess only !');
		if (answer == false){
			document.form_calcwoodmm.returns.value = 0;
			returns = 0;
	    }
	}
	//addReturnsToPrice(returns);
}

