var min_width=0;
var max_width=0;
var min_drop=0;
var max_drop=0;
var price=0;
var shape_price=0;
var discount=0;

function sortArrayPrice(a,b){
  return(a-b);
}

var arr = new Array();
arr = eval('Array('+rollermm_arr[5][3]+')');
var sorter = new Array();
sorter = arr.sort(sortArrayPrice);
  
function getMinimumPrice(){
  minP = sorter;
  var discount_ = rollermm_arr[4];
  document.getElementById('minprice').innerHTML = (minP[0].toFixed(2) * (1-discount_)).toFixed(2);
  }

function CheckReferrer() {
  
  var ref = document.referrer;
  if(ref != "http://www.webwoodenblinds.co.uk/roller-blinds-nextday.html")
    document.form_calcrollermm.nextday.selectedIndex = 1; // select default "Standard, 5 days" when not coming from above links
  else
    document.form_calcrollermm.nextday.selectedIndex = 0; // select default "Nextday"
//    document.form_calcrollermm.nextday.selectedIndex = 1; // for bank holiday
}

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["controls"] && params["controls"] && params["scallop"] && params["braid"] && params["rods"] && params["recess"])
  if (params["controls"] && params["recess"])
    {
        document.form_calcrollermm.controls.value = params["controls"];
        //document.form_calcrollermm.finish_shape.value = params["scallop"];
        //selectFinish(document.form_calcrollermm.finish_shape.value);
        //document.form_calcrollermm.braid.value = params["braid"];
        //selectBraid(document.form_calcrollermm.braid.value);
        //document.form_calcrollermm.cafe_rods.value = params["rods"];
        document.form_calcrollermm.recess.value = params["recess"];
        //selectRecess(document.form_calcrollermm.recess.value);
    }
}

function SetOtherParams()
{
    params = getParams();
    if (params["controls"] && params["recess"])
    {
        document.form_calcrollermm.controls.value = params["controls"];
        document.form_calcrollermm.recess.value = params["recess"];
    }
}

function SetParams()
{
  /*var widthminmax=document.form_calcrollermm.widthminmax;
  widthminmax.options[1]=null;
  widthminmax.options[0]=null;
  
  widthminmax.options[0]=new Option(rollermm_arr[0], 1);*/
  min_width=parseInt(rollermm_arr[0]);
  //widthminmax.options[1]=new Option(rollermm_arr[1], 2);
  max_width=parseInt(rollermm_arr[1]);
  //widthminmax.value=2;
  
  /*var dropminmax=document.form_calcrollermm.dropminmax;
  dropminmax.options[0]=null;
  
  dropminmax.options[0]=new Option(rollermm_arr[2], 1);*/
  min_drop=parseInt(rollermm_arr[2]);
  //dropminmax.options[1]=new Option(rollermm_arr[3], 2);
  max_drop=parseInt(rollermm_arr[3]);
  //dropminmax.value=2;
  
  discount=parseFloat(rollermm_arr[4]);
  
  params = getParams();
  
  width = params["width"];
  drop = params["drop"];
  
  if((width!="")&&(typeof(width) != "undefined"))
    document.form_calcrollermm.custom_width.value=unescape(width);
  if((drop!="")&&(typeof(drop) != "undefined"))
    document.form_calcrollermm.custom_drop.value=unescape(drop);
  if((document.form_calcrollermm.custom_width.value!="")&&(document.form_calcrollermm.custom_drop.value!=""))
    GetCustomPrice();
}

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;
  
  //reset shapes value
  /*for(i=0;i<shapes_arr.length;i++)
  {
    form_calc.finish_shape.options[i]=new Option(shapes_arr[i][1], shapes_arr[i][0]);
  }*/
}

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 GetShapePrice(prices_arr, width)
{
  var min_dif_width=-1;
  var sel_width=-1;
  var price=0;
  for(i=0;i<prices_arr[0].length;i++)
  {
    var cur_dif_width=prices_arr[1][i]-width;
    //alert("dif curenta: "+cur_dif_width+"   width: "+prices_arr[1][i]+" custom width:"+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];
      price=prices_arr[3][i];
    }
  }
  //alert(price);
  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 GetPriceForBraid(braid, finish_shape)
{
  var finish_type=0;
  var finish_poz=-1;
  for(i=0; i<finishtypes_arr.length; i++)
    if(finishtypes_arr[i][0]==finish_shape)
    {
      finish_type=finishtypes_arr[i][2];
      finish_poz=i;
    }

  if(finish_type!=0)
  {
    var poz_price=0;
    if(finish_type==1)
    {
      if(braid==1)
        poz_price=3;
      else
        return 0;
    }
    else
    if((finish_type==2)||(finish_type==3))
    {
      if((braid==1)||(braid==2))
        poz_price=3;
      else
      if(braid==3)
        poz_price=4;
      else
        return 0;
    }
    else
      return 0;
    //alert("GetShapePrice");
    return GetShapePrice(finishtypes_arr[finish_poz][poz_price], parseInt(document.form_calcrollermm.custom_width.value));
  }
  return 0;
}
  
function GetCustomPrice()
{
  var custom_width=parseInt(document.form_calcrollermm.custom_width.value);
  var custom_drop=parseInt(document.form_calcrollermm.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(rollermm_arr[5], custom_width, custom_drop);
  
  if(price==0)
  {
    var strMaxWidth=GetMaxWidth(rollermm_arr[0][4], custom_drop);
    alert("Maximum width for the selected drop is "+strMaxWidth+" mm");
    return;
  }
  
  //set shapes prices
  /*
  var finish_shape=document.form_calcrollermm.finish_shape.value;
  var braid=document.form_calcrollermm.braid.value;
  var cur_braid_price=0;
  if((!braid)&&((finish_shape==2)||(finish_shape==3)))
    braid=1;
  cur_braid_price=GetPriceForBraid(braid, finish_shape);
  */
  
  //var total_price=price+cur_braid_price;

  // NEXTDAY price
  /* *********************************************** */
  var delivery = document.form_calcrollermm.nextday;
  //if ((custom_width > 1840 && custom_drop > 1700) || (custom_drop > 2750)){ // no nextday dimensions -- OLD! --
  if ((custom_width > 1900 && custom_drop >= 1700) || (custom_drop > 2700)){ // no nextday dimensions (JOINT!)
    if(delivery.selectedIndex == 0){
      alert("Next day service is not available for these sizes!");
    }
    delivery.selectedIndex = 1;
  }
  var delivery_price = 0;
  
  /* *** for bank holiday *** */
  if(delivery.value != 0 && false) {
    alert("Sorry, nextday service is no longer available on this product.");
    document.form_calcrollermm.nextday.selectedIndex = 1;
    delivery.value = 0;
  }
  if (delivery.value != 0 && discount != 1)
    delivery_price = 5 / (1 - discount);

  // controls price (for metal controls only)
  /* ******************************************************* */
  var controls_price = 0;
  var controls = document.form_calcrollermm.controls.value;
  if(controls == "Right Metal" || controls == "Left Metal") {
    if(custom_drop <= 1000)
      controls_price = 0.62;
    else if(custom_drop > 1000 && custom_drop <= 2000)
      controls_price = 1.26;
    else if(custom_drop > 2000)
      controls_price = 2.32;
    
    controls_price = controls_price / (1 - discount);
  } //end if metal controls
  /* ******************************************************* */

  var total_price = price + delivery_price + controls_price;
  var save_price = total_price * discount;
  /* *********************************************** */
  
  /*
  var total_price=price;
  //var save_price=formatDecimal((price+cur_braid_price)*discount, true, 2);
  var save_price=formatDecimal((price)*discount, true, 2);
  */
  
  document.form_calcrollermm.price.value=formatDecimal(total_price-save_price, true, 2);
  document.form_calcrollermm.saveprice.value=formatDecimal(save_price, true, 2);
  
  document.form_calcrollermm.addtobasket.disabled=false;
}

function selectBraid(braid)
{
  var base_price=price;
  if(base_price<=0)
    return;
  
  var finish_shape=document.form_calcrollermm.finish_shape.value;
  var cur_braid_price=0;
  if((!braid)&&((finish_shape==2)||(finish_shape==3)))
    braid=1;
    
  cur_braid_price=GetPriceForBraid(braid, finish_shape);
  var total_price=price+cur_braid_price;
  var save_price=(price+cur_braid_price)*discount;
  document.form_calcrollermm.price.value=formatDecimal(total_price-save_price, true, 2);
  document.form_calcrollermm.saveprice.value=formatDecimal(save_price, true, 2);
}

function VerifyrollerMMForm(form)
{
  if(form.price.value<=0)
  {
    alert('Please insert width and drop first and click the button "Price Calculator"');
    return false;
  }
  return true; 
}

function onChangeCloth(){
  if (document.form_calcrollermm.recess.value == "Cloth"){
    alert("Please note that the end brackets will add to the cloth size 35mm - 50mm and may not fit inside recess.");
  }
}
