var min_width=0;
var max_width=0;
var min_drop=0;
var max_drop=0;
var price=0;
var shape_price=0;

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["conservtype"] == "roof" && params["type"] && params["area"] && params["no"] && params["survey"] && params["wand"])
    {
        document.form_calccnsrvroof.roof_type.value = params["type"];
        selectRoofType(document.form_calccnsrvroof.roof_type.value);
        document.form_calccnsrvroof.surface.value = params["area"];
        selectSurface(document.form_calccnsrvroof.surface.value);
        document.form_calccnsrvroof.noblinds.value = params["no"];
        selectNoBlinds(document.form_calccnsrvroof.noblinds.value);
        document.form_calccnsrvroof.survey.value = params["survey"];
        selectNoBlinds(document.form_calccnsrvroof.noblinds.value);
        document.form_calccnsrvroof.telescopic.value = params["wand"];
        selectNoBlinds(document.form_calccnsrvroof.noblinds.value);
    }
    if(params["conservtype"] == "side" && params["width"] && params["drop"] && params["controls"] && params["recess"] && params["bracket"])
    {
        document.form_calccnsrvside.custom_width.value = params["width"];
        document.form_calccnsrvside.custom_drop.value = params["drop"];
	GetCustomPrice();
        document.form_calccnsrvside.recess.value = params["recess"];
        document.form_calccnsrvside.controls.value = params["controls"];
        document.form_calccnsrvside.bracket.value = params["bracket"];
        selectBracket(document.form_calccnsrvside.bracket.value);
    }
}

function SetParams()
{
	min_width=parseInt(cnsrv_side_arr[0]);
	max_width=parseInt(cnsrv_side_arr[1]);
	min_drop=parseInt(cnsrv_side_arr[2]);
	max_drop=parseInt(cnsrv_side_arr[3]);
	if (document.form_calccnsrvside.price.value > 0)
		document.form_calccnsrvside.addtobasket.disabled=false;
	selectRoofType(document.form_calccnsrvroof.roof_type.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 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, 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()
{
	var custom_width=parseInt(document.form_calccnsrvside.custom_width.value);
	var custom_drop=parseInt(document.form_calccnsrvside.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;
	}
	
	//price=GetPrice(cnsrv_side_arr[4], custom_width, custom_drop)*1.05;
	price=GetPrice(cnsrv_side_arr[4], custom_width, custom_drop);
	
	if(price==0)
	{
		var strMaxWidth=GetMaxWidth(cnsrv_side_arr[4], custom_drop);
		alert("Maximum width for the selected drop is "+strMaxWidth+" mm");
		return;
	}
	
	var bracket_price=0;
	if(document.form_calccnsrvside.bracket.value==1)
		bracket_price=0.53;
		
	document.form_calccnsrvside.price.value=formatDecimal((price+bracket_price)*0.8, true, 2);
	document.form_calccnsrvside.saveprice.value=formatDecimal((price+bracket_price)*0.2, true, 2);
	
	document.form_calccnsrvside.addtobasket.disabled=false;
}

function selectBracket(bracket)
{
	var base_price=price;
	if(price==0)
		return;
	var bracket_price=0;
	if(bracket==1)
		bracket_price=0.53;
		
	document.form_calccnsrvside.price.value=formatDecimal((price+bracket_price)*0.8, true, 2);
	document.form_calccnsrvside.saveprice.value=formatDecimal((price+bracket_price)*0.2, true, 2);
	
	document.form_calccnsrvside.addtobasket.disabled=false;
}

function VerifyConservatorySideForm(form)
{
	if(form.price.value<=0)
	{
		alert('Please insert width and drop first and click the button "Calculate Price"');
		return false;
	}
	return true; 
}

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 selectRoofType(roof_type)
{
	document.form_calccnsrvroof.price.value="";
	document.form_calccnsrvroof.saveprice.value="";
	
	//document.form_calccnsrvroof.addtobasket.disabled=true;
	
	var poz_roof_type=-1;
	for(i=0;i<cnsrv_roof_arr.length; i++)
		if(cnsrv_roof_arr[i][0]==roof_type)
			poz_roof_type=i;
	if(poz_roof_type<0)
	{
		//document.form_calccnsrvroof.addtobasket.disabled=true;
		return;
	}
	//delete all surfaces
	var surface=document.form_calccnsrvroof.surface;
	for(;surface.options.length;)
		surface.options[0]=null;
	//add new surfaces
	var last_surface=-1;
	for(i=0;i<cnsrv_roof_arr[poz_roof_type][1][0].length;i++)
		if(cnsrv_roof_arr[poz_roof_type][1][1][i]!=last_surface)
		{
			last_surface=cnsrv_roof_arr[poz_roof_type][1][1][i];
			surface.options[surface.options.length]=new Option(cnsrv_roof_arr[poz_roof_type][1][1][i],cnsrv_roof_arr[poz_roof_type][1][0][i]);
		}
	selectSurface(surface.value);
}

function selectSurface(surface_val)
{
	document.form_calccnsrvroof.price.value="";
	document.form_calccnsrvroof.saveprice.value="";
	
	//document.form_calccnsrvroof.addtobasket.disabled=true;
	
	var poz_roof_type=-1;
	var roof_type=document.form_calccnsrvroof.roof_type.value;
	for(i=0;i<cnsrv_roof_arr.length; i++)
		if(cnsrv_roof_arr[i][0]==roof_type)
			poz_roof_type=i;
	if(poz_roof_type<0)
	{
		//document.form_calccnsrvroof.addtobasket.disabled=true;
		return;
	}
	//finding surface
	var surface=-1;
	for(i=0;i<cnsrv_roof_arr[poz_roof_type][1][0].length;i++)
		if(cnsrv_roof_arr[poz_roof_type][1][0][i]==surface_val)
			surface=cnsrv_roof_arr[poz_roof_type][1][1][i];
	//delete all no blinds
	var noblinds=document.form_calccnsrvroof.noblinds;
	for(;noblinds.options.length-1;)
		noblinds.options[1]=null;
	//finding number of blinds
	for(i=0;i<cnsrv_roof_arr[poz_roof_type][1][0].length;i++)
		if(cnsrv_roof_arr[poz_roof_type][1][1][i]==surface)
		{
			noblinds.options[noblinds.options.length]=new Option(cnsrv_roof_arr[poz_roof_type][1][2][i],cnsrv_roof_arr[poz_roof_type][1][0][i]);
		}

	selectNoBlinds(noblinds.value);
}

function selectNoBlinds(noblinds_value)
{
	var poz_roof_type=-1;
	var roof_type=document.form_calccnsrvroof.roof_type.value;
	for(i=0;i<cnsrv_roof_arr.length; i++)
		if(cnsrv_roof_arr[i][0]==roof_type)
			poz_roof_type=i;
	if(poz_roof_type<0)
	{
		//document.form_calccnsrvroof.addtobasket.disabled=true;
		return;
	}
	//finding noblinds
	var noblinds=-1;
	for(i=0;i<cnsrv_roof_arr[poz_roof_type][1][0].length;i++)
		if(cnsrv_roof_arr[poz_roof_type][1][0][i]==noblinds_value)
			noblinds=i;
	if(noblinds<0)
	{
		//document.form_calccnsrvroof.addtobasket.disabled=true;
		return;
	}
		
	price=cnsrv_roof_arr[poz_roof_type][1][3][noblinds]*1.05;
	
	var price_survey=0;
	if(document.form_calccnsrvroof.survey.value==1)
		price_survey=89;
		
	var price_telescopic=0;
	if(document.form_calccnsrvroof.telescopic.value==1)
		price_telescopic=19;
	else
		if(document.form_calccnsrvroof.telescopic.value==2)
			price_telescopic=26;
	
	document.form_calccnsrvroof.price.value=formatDecimal((price+price_survey+price_telescopic)*0.8, true, 2);
	document.form_calccnsrvroof.saveprice.value=formatDecimal((price+price_survey+price_telescopic)*0.2, true, 2);
	
	document.form_calccnsrvroof.addtobasket.disabled=false;
}

function VerifyConservatoryRoofForm(form)
{
	if(form.price.value<=0)
	{
		alert('Please insert width and drop first and click the button "Calculate Price"');
		return false;
	}
	return true; 
}

