
// disable sau enable link pe butonul Add to Basket - radul
function ControlAddBasket(control){
    var button = document.getElementById("add_to_basket");
	if(control == "enable")
		button.onclick = function(){document.forms[0].submit(); return false;};
	else if(control == "disable")
		button.onclick = "";		
	
	return false;
}

// calculates the minimum price for current product
function getMinimumPrice(form_calc)
{
	var min_price=curtains_arr[3][0]*(1-discount);
	for(i=1; i<curtains_arr[3].length; i++)
	{	
		var cur_price=curtains_arr[3][i]*(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 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 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;
}

/*intoarce pretul conform selectiilor curente*/
function GetCustomPrice(form_calc)
{	
	var customwidth=form_calc.customwidth.value;
	var customdrop=form_calc.customdrop.value;
	var myprice;  //pretul afisat
	
	for(i=0; i<curtains_arr[1].length; i++)
		if ((curtains_arr[1][i]==customwidth) && (curtains_arr[2][i]==customdrop))
		{
			myprice=curtains_arr[3][i];
			myprice+= getTiebacks();
			myprice+= getCushions();
			myprice+= getValances();
			form_calc.curtainsrm.value=curtains_arr[0][i];
			form_calc.price.value=formatDecimal(myprice*(1-discount), true, 2);
			form_calc.saveprice.value=formatDecimal(myprice*discount, true, 2);
		}
		
}


function AddDrops(form_calc, customwidth, sel_drop)
{
	if(sel_drop=='')
		sel_drop=form_calc.customdrop.options[form_calc.customdrop.selectedIndex].value;
	for(i=0; i<form_calc.customdrop.options.length; i++)
		form_calc.customdrop.options[0]=null;
		
	var poz=0;
	for(j=0; j<curtains_arr[0].length; j++)
		if(curtains_arr[1][j]==customwidth)
			{	
				var strText=curtains_arr[2][j]+" mm";
				form_calc.customdrop.options[poz]=new Option(strText, curtains_arr[2][j]);
				poz++;
				if((sel_drop==curtains_arr[2][j])||(sel_drop==strText))
					form_calc.customdrop.value=curtains_arr[2][j];
			}
	
	GetCustomPrice(form_calc);
}

function numSort(a,b)
{ 
       return a - b; 
}


function AddWidths(form_calc, slat, sel_width, sel_drop)
{
		
	for(k=form_calc.customwidth.options.length-1; k>=0; k--)
		form_calc.customwidth.options[k]=null;
	
	var widths_array = new Array();
	var l = 0;
	
	for(j=0; j<curtains_arr[0].length; j++)
	{
		var width_found = false;
		for (h=0;h<widths_array.length;h++)
		{
			if (widths_array[h] == curtains_arr[1][j])
				width_found = true;
		}
		if (!width_found)
		{
					widths_array[l] = parseInt(curtains_arr[1][j]);
					l++;
		}
	}

	widths_array.sort(numSort);
	
	for (h=0;h<widths_array.length;h++)
	{
		strText = widths_array[h]+" mm";
		form_calc.customwidth.options[form_calc.customwidth.options.length]=new Option(strText, widths_array[h]);
	}
	
	if (sel_width)
		form_calc.customwidth.value = sel_width;

	AddDrops(document.form_calc_curtains, form_calc.customwidth.value, sel_drop);
}

/*incarca discount si optiuni*/
function Load()
{
	//getDiscount();
	AddWidths(document.form_calc_curtains, document.form_calc_curtains.customwidth.value, document.form_calc_curtains.customdrop.value);	
}


function LoadValues()
{
	params = getParams();
	
	width = params["width"];
	drop = params["drop"];
	
	if((width!="")&&(typeof(width) != "undefined") && (drop!="")&&(typeof(drop) != "undefined"))
	{
		AddWidths(document.form_calc_curtains, '', '');
		document.form_calc_curtains.customwidth.value=unescape(width);
		AddDrops(document.form_calc_curtains, width, '');
		document.form_calc_curtains.customdrop.value= unescape(drop);
		GetCustomPrice(document.form_calc_curtains);
	}
}


function SelectProdType(prodtype)
{
	window.location = prodtype;
}

function getDiscount()
{
	var discountstr=formatDecimal(discount,true,2)*100;
	document.getElementById('save').innerHTML = discountstr;
}

function getTiebacks()
{
	if (tiebacks>0) 
	{	
		if (document.form_calc_curtains.tiebacks.value==1)
		{
			document.form_calc_curtains.tiebacks_val.value=1;
			return tiebacks;
		}
		document.form_calc_curtains.tiebacks_val.value=-1;
	}
	return 0;
}

function getCushions()
{
	if(cushions>0)
	{
		if (document.form_calc_curtains.cushions.value==1)
		{
			document.form_calc_curtains.cushions_val.value=1;
			return cushions;
		}
		document.form_calc_curtains.cushions_val.value=0;
	}
	return 0;
}

function getValances()
{
	if (valances_arr.length>0)
	{
		var vl=document.form_calc_curtains.valances.value;
		document.form_calc_curtains.valances_val.value=vl;
		if (vl>0)
				return  valances_arr[vl-1];
	}
	return 0;
}