/* start */
//var woodstlsample=0; // se ia din js-ul fiecarui produs
var price_stl=0;
var discountstl=0;

var min_width=0;
var max_width=0;
var min_drop=0;
var max_drop=0;
var price=0;

var recess_price=0;
var discount=0;
var discountrm=0;
var price_rm=0;
var price_mm=0;
var delivery = 0;

function CheckReferrer() {
  var ref = document.referrer;

  params = getParams();
  var nd = 0; // nextday parameter
  if(params["nd"] == 1)
    nd = 1;

  if(ref != "http://www.blindsuk.net/wooden-next-day.html" && nd == 0) {
    document.form_calcwoodmm.nextday.selectedIndex = 1; // select default "Standard, 7-10 days" when not coming from above links
    //document.form_calcwoodmm.nextday2.selectedIndex = 1; // the 2nd select element (REMOVED)
  }
  else {
    document.form_calcwoodmm.nextday.selectedIndex = 0;
//    document.form_calcwoodmm.nextday.selectedIndex = 1; // for bank holiday
    //document.form_calcwoodmm.nextday2.selectedIndex = 0; // (REMOVED)
  }   
}

function getDiscount()
{
    var discountstr=wood_arr[0];
    discountstr=formatDecimal(discountstr,true,2)*100;
    document.getElementById('save').innerHTML = discountstr;
}

function sortArrayPrice(a,b){
  return a - b;
}

// calculates the minimum price for current product
function getMinimumPrice()
{
  var min_price_rm = min_price_stl = 9999;

  // MM 
  var discount_mm=wood_arr[0];
  var min_price_mm=wood_arr[1][7][3][0];
  for(i=1; i<wood_arr.length; i++)
  { 
      var slat_arr=wood_arr[i][7];
      for(j=0; j<slat_arr[0].length; j++)
      { 
        var cur_price_mm=slat_arr[3][j]*(1-discount_mm);
        if (min_price_mm>cur_price_mm)
        {
          min_price_mm=cur_price_mm;
        }
      }
    
  }
  /* REMOVED
  // RM
  if(typeof(wood_rm_arr) != "undefined" && wood_rm_arr.length>0) {
    var discount_rm=wood_rm_arr[0];
    var min_price_rm=wood_rm_arr[1][2][3][0];
    for(i=1; i<wood_rm_arr.length; i++)
    { 
        var slat_arr=wood_rm_arr[i][2];
        for(j=0; j<slat_arr[0].length; j++)
        { 
          var cur_price_rm=slat_arr[3][j]*(1-discount_rm);
          if (min_price_rm>cur_price_rm)
          {
            min_price_rm=cur_price_rm;
          }
        }
      
    }
  }
  */

  /* REMOVED
  // STL
  if(typeof(wood_stl_arr) != "undefined" && wood_stl_arr.length>0) {
    var discount_stl=wood_stl_arr[0];
    var min_price_stl=wood_stl_arr[1][6][3][0];
    for(i=1; i<wood_stl_arr.length; i++)
    { 
        var slat_arr=wood_stl_arr[i][6];
        for(j=0; j<slat_arr[0].length; j++)
        { 
          var cur_price_stl=slat_arr[3][j]*(1-discount_stl);
          if (min_price_stl>cur_price_stl)
          {
            min_price_stl=cur_price_stl;
          }
        }
      
    }
  }
  */
  var arr = new Array(min_price_mm, min_price_rm, min_price_stl);
  var sorter = new Array();
  sorter = arr.sort(sortArrayPrice);
  
  min_price=formatDecimal(sorter[0],true,2);
  if(typeof(sale) != 'undefined' && sale.sale > 0)
    min_price = formatDecimal(min_price - (min_price * sale.sale /100), true,2);
  document.getElementById('minprice').innerHTML = min_price;
}


function selectRecess(recess){  
  // INSIDE
  if(recess == 0) {
    document.form_calcwoodmm.recess.value=0;
    document.form_calcwoodmm.returns.value=0;
    if((document.form_calcwoodmm.custom_width.value!="")&&(document.form_calcwoodmm.custom_drop.value!=""))
      GetCustomPrice();

  }
  else if(recess == 5){
    document.form_calcwoodmm.recess.value=0;
  }
  // OUTSIDE
  else {
    GetCustomPrice();
  }
}

function addReturnsToPrice(returns){
  if (returns == 1) // RETURNS
  {
    //curent_price = parseFloat(document.form_calcwoodmm.price.value);
    if (price_rm >0)
    {
      //document.form_calcwoodmm.price.value = formatDecimal(parseFloat(price_rm) + 10, true, 2);
      document.form_calcwoodmm.price.value = formatDecimal(parseFloat(price_rm), true, 2); // ATENTIE!! PT. GLOBAL SI DECORA DOAR!
    }

    else if (price_stl >0)
    {
      //document.form_calcwoodmm.price.value = formatDecimal(parseFloat(price_stl) + 10, true, 2);
      document.form_calcwoodmm.price.value = formatDecimal(parseFloat(price_stl), true, 2); // ATENTIE!! PT. GLOBAL SI DECORA DOAR!
    }

    else if (price_mm >0)
    {
      //document.form_calcwoodmm.price.value = formatDecimal(parseFloat(price_mm) + 10, true, 2);
      document.form_calcwoodmm.price.value = formatDecimal(parseFloat(price_mm), true, 2); // ATENTIE!! PT. GLOBAL SI DECORA DOAR!
    }
  }
  if (returns == 0) // NO RETURNS
  {
    if (price_rm) // RM price
      document.form_calcwoodmm.price.value = formatDecimal(price_rm, true, 2);
    else if(price_stl) // STL price
      document.form_calcwoodmm.price.value = formatDecimal(price_stl, true, 2);
    else if (price_mm) // MM price
      document.form_calcwoodmm.price.value = formatDecimal(price_mm, true, 2);
  }
}

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);
}

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["lift"] && params["tilt"] && params["tape"] && params["recess"] && params["returns"])
    {
  document.form_calcwoodmm.lift_control.value = params["lift"];
  document.form_calcwoodmm.tilt_control.value = params["tilt"];
  document.form_calcwoodmm.tape.value = params["tape"];
        selectTape(document.form_calcwoodmm.tape.value);
  document.form_calcwoodmm.recess.value = params["recess"];
        selectRecess(document.form_calcwoodmm.recess.value);
        if (params["returns"] == 0 && params["recess"] == 1)
        {
            document.form_calcwoodmm.returns.value = params["returns"];
            addReturnsToPrice(document.form_calcwoodmm.returns.value);
        }
    }

  /* TAKE SLAT FROM QQ AND SELECT IT AS DEFAULT - RaduL*/
  price = 0;
  slat = params["slat"];
  outside = params["outside"];
  
  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;
  }
  
  // default recess OUTSIDE when coming from QQ
  if((outside!="")&&(typeof(outside) != "undefined")) {
    document.form_calcwoodmm.recess.value = outside;
    var recess=document.form_calcwoodmm.recess.value;
  }

  if((document.form_calcwoodmm.custom_drop.value!="")&&(document.form_calcwoodmm.custom_width.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 selectSlat(slat)
{
  document.form_calcwoodmm.price.value="";
  document.form_calcwoodmm.saveprice.value="";
  //document.form_calcwoodmm.addtobasket.disabled=true;
  
  document.form_calcwoodmm.slat_value.value = slat;
  
  var slat_poz=-1;
  for(i=1; i<wood_arr.length;i++)
  {
    if(wood_arr[i][0]==slat)
      slat_poz=i;
  }
  
  if(slat_poz<0)
    return;
  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;

  if(wood_arr[slat_poz][6] != '') // tapestr exists for current slat, show it as option
    document.form_calcwoodmm.tape.options[1]=new Option(wood_arr[slat_poz][6], 1);
  else
    document.form_calcwoodmm.tape.options[1]=null;
  
  params = getParams();
  
  width = params["width"];
  drop = params["drop"];
  //slat = params["slat"]; // radul
  //outside = params["outside"];
  
  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); // put selected slat's value in a hidden field
  /*
  if((outside!="")&&(typeof(outside) != "undefined"))
    document.form_calcwoodmm.recess.value = outside;
  var recess=document.form_calcwoodmm.recess.value;
  */
  //alert(recess);

  if((document.form_calcwoodmm.custom_drop.value!="")&&(document.form_calcwoodmm.custom_width.value!="")) {
    GetCustomPrice();
  }

}

function AddSlats()
{
  //add slats
  /* document.form_calcwoodmm.slat.value 
    slat.value
  */
  //document.form_calcwoodmm.prodtype.value=1;
  var slat=GetSlatValue(document.form_calcwoodmm);
  var recess=document.form_calcwoodmm.recess.value;
  var returns=document.form_calcwoodmm.returns.value;
    
  discount=wood_arr[0];
  /* REMOVED
  if(typeof(wood_rm_arr) != "undefined" && wood_rm_arr.length>0) // RM
    discountrm=wood_rm_arr[0];
  if(typeof(wood_stl_arr) != "undefined" && wood_stl_arr.length>0) // STL
    discountstl=wood_stl_arr[0];
  */
  selectSlat(slat); //select default slat
  selectRecess(recess);
}

function selectTape(tape)
{ 
  GetCustomPrice();
}

function ChangePrice(custom_ctrl, form_calc)
{
  custom_ctrl.value="";
  form_calc.price.value="";
  form_calc.saveprice.value="";
  //form_calc.addtobasket.disabled=true; 
  document.form_calcwoodmm.woodrm.value=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;
    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 GetRMPrice(custom_width, custom_drop)
{
  document.form_calcwoodmm.woodrm.value=0;

  var slat=-1;
  var slattext="";
  var slatctrl=document.form_calcwoodmm.slat;
  var slat_value = 0;
  
  for (i=0;i<slatctrl.length;i++) {
    if (slatctrl[i].checked)
      slat_value = slatctrl[i].value;
  }
  
  var mm_slat=-1;
  for(i=1;i<wood_arr.length; i++)
  {
    if(wood_arr[i][0]==slat_value)
      {
        mm_slat=i;
        break;
      }
  }
  
  if (mm_slat<0) return 0;
  slattext=wood_arr[mm_slat][1];
  
  for(i=0; i<wood_rm_arr.length; i++)
  {
    if(wood_rm_arr[i][1]==slattext)
      slat=i;
  }
  
  if(slat!=-1)
  {
    for(i=0; i<wood_rm_arr[slat][2][0].length; i++)
    {
      if((wood_rm_arr[slat][2][1][i]==custom_width)&&(wood_rm_arr[slat][2][2][i]==custom_drop))
      {
        document.form_calcwoodmm.woodrm.value=wood_rm_arr[slat][2][0][i];
        document.form_calcwoodmm.woodrmslat.value=wood_rm_arr[slat][0];
        return wood_rm_arr[slat][2][3][i];
      }
    }
  }
  return 0;
}

function GetSTLPrice(prices_arr, width, drop)
{
  var min_dif_width=-1;
  var sel_width=-1;
  var price=0;
        if ((drop==1500 && document.form_calcwoodmm.slat_value.value==2 && (width<1950 || width>2400)) || (drop==1500 && document.form_calcwoodmm.slat_value.value!=2 && (width<2000 || width>2400)))
        {
            price=0;
        }
        else
        {
  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;
    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];
    }
    if((prices_arr[1][i]==sel_width)&&(cur_dif_drop==0))
    {
      /*
      if(cur_dif_width==0)
        price=prices_arr[3][i];
      else
      */
        price=prices_arr[3][i]+16.60;
    }
  }
        }
    
  return price;
}
  
function GetCustomPrice()
{
  price = 0;
  price_stl = 0;
  price_rm = 0;
  
  var custom_width=parseInt(document.form_calcwoodmm.custom_width.value);
  var custom_drop=parseInt(document.form_calcwoodmm.custom_drop.value);
  var recess=document.form_calcwoodmm.recess.value;

  document.form_calcwoodmm.woodrm.value=0;
    
  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_size=GetSlatValue(document.form_calcwoodmm);
  var slat=-1;
  for(i=1;i<wood_arr.length; i++)
  {
    if(wood_arr[i][0]==slat_size)
      slat=i;     
  }
  
  if(slat<0)
    return;
  if(recess==0)
    custom_width-=10;
  /* REMOVED
  if(typeof(wood_rm_arr) != "undefined" && wood_rm_arr[slat] != null) {   
    price=GetRMPrice(custom_width, custom_drop);
    price_rm=price;

    if(price_rm) { // no returns (radul)
      for(i=document.form_calcwoodmm.returns.options.length; i>=0; i--)
      {
        document.form_calcwoodmm.returns.options[i]=null;
      }
      document.form_calcwoodmm.returns.options[0]=new Option("no", 0);
      document.form_calcwoodmm.returns.value = 0;
    }           
  }
  */
  if(recess==0)
    custom_width+=10;

  price_mm=GetPrice(wood_arr[slat][7], custom_width, custom_drop);

  /* REMOVED
  if(typeof(wood_stl_arr) != "undefined" && wood_stl_arr[slat] != null) {
    price_stl=GetSTLPrice(wood_stl_arr[slat][6], custom_width, custom_drop);
    if(price_stl) { // no returns (radul)
      for(i=document.form_calcwoodmm.returns.options.length; i>=0; i--)
      {
        document.form_calcwoodmm.returns.options[i]=null;
      }
      document.form_calcwoodmm.returns.options[0]=new Option("no", 0);
      document.form_calcwoodmm.returns.value = 0;
    }   
  }

  if (!price || price == 0)
  {
    price = price_stl;
    //discount=discountstl;
    if (price > 0)
    {
      document.form_calcwoodmm.wood_stl_sample.value=woodstlsample;
      //document.form_calcwoodmm.woodstlslat.value=wood_stl_arr[slat][0];
    }
  }
  */
  
  if(!price || price == 0)
  {
    document.form_calcwoodmm.wood_stl_sample.value = 0;
    discount=wood_arr[0];
    //price=GetPrice(wood_arr[slat][7], custom_width, custom_drop);
    price = price_mm;
    if(price==0)

    {
      var strMaxWidth=GetMaxWidth(wood_arr[slat][7], custom_drop);
      alert("Maximum width for the selected drop is "+strMaxWidth+" mm");
      return;
    }
  }

  var cur_recess_price=0;
  
  //tapes price
  var tape=document.form_calcwoodmm.tape;
  var tape_price=0;
  if(tape.value!=0)
    tape_price=price*0.1;

  // nextday price
  var delivery = document.form_calcwoodmm.nextday;
  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_calcwoodmm.nextday.selectedIndex = 1;
    delivery.value = 0;
  }

  if(delivery.value == 1)
    delivery_price = 12.5; // in total 5 lire adaugate la our price     
  else if(delivery.value == 2)
    delivery_price = 25; // in total 10 lire adaugate la our price

  /* REMOVED
  if(price_rm)
    discount=discountrm;
  else if(price_stl)
    discount=discountstl;
  */
  document.form_calcwoodmm.price.value=formatDecimal((price+tape_price+cur_recess_price + delivery_price)*(1-discount), true, 2);
  document.form_calcwoodmm.saveprice.value=formatDecimal((price+tape_price+cur_recess_price + delivery_price)*discount, true, 2);

  document.form_calcwoodmm.addtobasket.disabled=false;
  
  if(price==price_rm)
    price_rm=document.form_calcwoodmm.price.value;
  else
    price_mm=document.form_calcwoodmm.price.value;

  outside = params["outside"];

  if(!price_rm && !price_stl) { //OUTSIDE, no RM, no STL -> returns YES (radul)
    for(i=document.form_calcwoodmm.returns.options.length; i>=0; i--){
      document.form_calcwoodmm.returns.options[i] = null;
    }
    document.form_calcwoodmm.returns.options[1] = new Option("yes", 1);
    document.form_calcwoodmm.returns.options[0] = new Option("no", 0);
    document.form_calcwoodmm.returns.value = 0; 
    // default returns NO when coming from QQ
    if(outside) {
      document.form_calcwoodmm.returns.options[0].selected = true;
      document.form_calcwoodmm.returns.value=0;
    }
  }
  if(document.form_calcwoodmm.recess.value == 1 && !price_rm && !price_stl){
    document.form_calcwoodmm.returns.value = 1;
  }
  if(document.form_calcwoodmm.returns.value == 1) {
    addReturnsToPrice(1);
  }

  RRP.get();
}

function VerifyWoodmmForm(form)
{
  if(form.price.value<=0)
  {
    alert('Please insert width and drop first and click the button "Calculate Price"');
    return false;
  }
  return true; 
}

function ConvertInchToMm()
{
  var custom_inches=document.form_calcwoodmm.custom_inches;
  var custom_mm=document.form_calcwoodmm.custom_mm;
  
  custom_mm.value=formatDecimal(parseFloat(custom_inches.value.replace(",",".").replace(",","."))*25.4, false, 0);
}

function SelectProdType(prodtype)
{
    window.location = prodtype;
}

function GetSlatValue(form){
  var slat_value = 0;
  
  for (i=0;i<form.slat.length;i++) {
    if (form.slat[i].checked)
      slat_value = form.slat[i].value;
  }
  
  return slat_value;
}

