
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();
    //slat = params["slat"];
    width = params["width"];
    drop = params["drop"];
    //cutter = params["cutter"];
    var slat_param = params["slat"];
	
    if((slat_param!="")&&(typeof(slat_param) != "undefined") && (width!="")&&(typeof(width) != "undefined") && (drop!="")&&(typeof(drop) != "undefined"))
    {
	//document.form_calcvenetianrm.slat.value = slat;
	document.form_calcvenetianrm.slat_value.value=unescape(slat_param); // radul
	AddWidths(document.form_calcvenetianrm, slat_param, width, drop)
	document.form_calcvenetianrm.customwidth.value = width;
	AddDrops(document.form_calcvenetianrm, slat_param, width, drop)
	drop_id = getDropID(slat_param, width, drop);
	document.form_calcvenetianrm.customdrop.value = drop_id;
	GetCustomPrice(document.form_calcvenetianrm, document.form_calcvenetianrm.customdrop.value, document.form_calcvenetianrm.slat_value.value)
    }
    
	/*
    if (cutter == "Yes")
	document.form_calcvenetianrm.slatcutter.value = 1;
    else if (cutter == "No")
        document.form_calcvenetianrm.slatcutter.value = 0;
	*/

}

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 GetCustomPrice(form_calc, customdrop, slat)
{
	var discount=venetian_rm_arr[0];
	for(i=1; i<venetian_rm_arr.length; i++)
		if(venetian_rm_arr[i][0]==slat)
		{
			var slat_arr=venetian_rm_arr[i][2];
			for(j=0; j<slat_arr[0].length; j++)
				if(slat_arr[0][j] == customdrop)
				{
					form_calc.price.value=formatDecimal(slat_arr[3][j]*(1-discount), true, 2);
					form_calc.saveprice.value=formatDecimal(slat_arr[3][j]*discount, true, 2);
                                        form_calc.venetianrm.value=j+1;
										/*
                                        if (slat_arr[4][j] == 1)
                                        {
                                            if (form_calc.slatcutter.options.length == 2)
                                                form_calc.slatcutter.options[1]=null;
                                        }
										*/
										/* SCOS SLAT CUTTER
                                        else
                                        {
                                            if (form_calc.slatcutter.options.length == 1)
                                                form_calc.slatcutter.options[1]=new Option('Yes - £ 4.21', 1);
                                        }
										*/
				}
		}
}

function AddDrops(form_calc, slat, customwidth, sel_drop)
{
	for(i=0; i<form_calc.customdrop.options.length; i++)
		form_calc.customdrop.options[0]=null;
	
	var is_drop = false;
	for(i=1; i<venetian_rm_arr.length; i++)
		if(venetian_rm_arr[i][0]==slat)
		{
			var slat_arr=venetian_rm_arr[i][2];
			var poz=0;
			for(j=0; j<slat_arr[0].length; j++)
				if(slat_arr[1][j]==customwidth)
				{
					var strText=slat_arr[2][j]+" mm";
					form_calc.customdrop.options[poz]=new Option(strText, slat_arr[0][j]);
					poz++;
					if (slat_arr[2][j] == sel_drop)
					{
						is_drop = true;
						sel_drop_val = slat_arr[0][j];
					}
				}
		}
	
	if (is_drop)
		form_calc.customdrop.value = sel_drop_val;
		
	GetCustomPrice(form_calc, form_calc.customdrop.value, slat);
}

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;
	
	l = 0;
	for(i=1; i<venetian_rm_arr.length; i++)
	if(venetian_rm_arr[i][0]==slat)
	{
		var widths_array = new Array();
		var slat_arr = venetian_rm_arr[i][2];
		for(j=0; j<slat_arr[0].length; j++)
		{
			var width_found = false;
			
			for(k=0; k < widths_array.length; k++)
				if(widths_array[k] == slat_arr[1][j])
					width_found = true;
		
			if (!width_found)
			{
				widths_array[l] = parseInt(slat_arr[1][j]);
				l++;
			}
		}
	}

	widths_array.sort(numSort);
	//form_calc.customwidth.options[0] = null;
	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(form_calc, slat, form_calc.customwidth.value, sel_drop);
}

function getDropID(slat, width, drop)
{
	for(i=1;i<(venetian_rm_arr.length); i++)
	{
		if (slat == venetian_rm_arr[i][0])
		{
			arr_no = i;
		}
	}
	
	for(i=0;i<(venetian_rm_arr[arr_no][2][1].length); i++)
	{
		if ((venetian_rm_arr[arr_no][2][1][i] == width) && (venetian_rm_arr[arr_no][2][2][i] == drop))
			return venetian_rm_arr[arr_no][2][0][i];
	}
}

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()
{
	var slat_value = GetSlatValue(document.form_calcvenetianrm);
	document.form_calcvenetianrm.slat_value.value = slat_value; // put selected slat's value in a hidden field

	AddWidths(document.form_calcvenetianrm, slat_value, document.form_calcvenetianrm.customwidth.value, document.form_calcvenetianrm.customdrop.value);
}
