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;
var delivery = 0;

var discountrm=0;
var price_rm=0;
var price_mm=0;

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

// MM
var arr = new Array();
arr = eval('Array('+blackoutmm_arr[4][3]+')');
var sorter = new Array();
sorter = arr.sort(sortArrayPrice);

// RM
var arrRM = new Array();
if(typeof(blackout_arr) != "undefined" && blackout_arr.length > 0) {
  arrRM = eval('Array('+blackout_arr[3]+')');
}
var sorterRM = new Array();
sorterRM = arrRM.sort(sortArrayPrice);

function getMinimumPrice(){
  var minpriceMM = minpriceRM = 9999;
  
  // MM
  minP = sorter;
  //var discount_ = blackoutmm_arr[4]; (se ia direct din js)
  minpriceMM = (minP[0].toFixed(2) * (1-discount_)).toFixed(2);
  //alert("MM: " + minpriceMM);

  // RM
  if(typeof(blackout_arr) != "undefined" && blackout_arr.length > 0) {
    minPRM = sorterRM;
    var discountRM = blackout_arr[0];
    minpriceRM = (minPRM[0].toFixed(2) * (1-discountRM)).toFixed(2);
    //alert("RM: " + minpriceRM);
  }
  
  var minArray = new Array();
  minArray[0] = minpriceMM;
  minArray[1] = minpriceRM;
  var minPrice = minArray.sort(sortArrayPrice);
  
  //alert("MinPrice: " + minPrice[0]);
  document.getElementById('minprice').innerHTML = minPrice[0];
  
}

function CheckReferrer() {
  
  var ref = document.referrer;
  if(ref != "http://www.webwoodenblinds.co.uk/blackout-blinds-nextday.html")
    document.form_calcblackoutmm.nextday.selectedIndex = 1; // select default "Standard, 5 days" when not coming from above links
  else
    document.form_calcblackoutmm.nextday.selectedIndex = 0; // select default "Nextday"
//    document.form_calcblackoutmm.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["recess"])
    {
        document.form_calcblackoutmm.controls.value = params["controls"];
        //document.form_calcblackoutmm.finish_shape.value = params["scallop"];
        //selectFinish(document.form_calcblackoutmm.finish_shape.value);
        //document.form_calcblackoutmm.braid.value = params["braid"];
        //selectBraid(document.form_calcblackoutmm.braid.value);
        //document.form_calcblackoutmm.cafe_rods.value = params["rods"];
        document.form_calcblackoutmm.recess.value = params["recess"];
        //selectRecess(document.form_calcblackoutmm.recess.value);
    } 
}

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

function SetParams()
{
  min_width=parseInt(blackoutmm_arr[0]);
  max_width=parseInt(blackoutmm_arr[1]);
  min_drop=parseInt(blackoutmm_arr[2]);
  max_drop=parseInt(blackoutmm_arr[3]);
  
  //discount=parseFloat(blackoutmm_arr[4]);
  discount=discount_;
  
  params = getParams();
  
  width = params["width"];
  drop = params["drop"];

  outside = params["outside"];

  // default recess OUTSIDE when coming from QQ
  if((outside!="")&&(typeof(outside) != "undefined")) {
    document.form_calcblackoutmm.recess.value = "Outside";
    var recess=document.form_calcblackoutmm.recess.value;
  }

  if((width!="")&&(typeof(width) != "undefined"))
    document.form_calcblackoutmm.custom_width.value=unescape(width);
  if((drop!="")&&(typeof(drop) != "undefined"))
    document.form_calcblackoutmm.custom_drop.value=unescape(drop);
  if((document.form_calcblackoutmm.custom_width.value!="")&&(document.form_calcblackoutmm.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="";
}

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 GetRMPrice(custom_width, custom_drop)
{
  document.form_calcblackoutmm.blackout.value=0;
  var priceRM = 0;

  //document.form_calcblackout.price.value = '';
  //document.form_calcblackout.saveprice.value = '';
  for(i=0; i<blackout_arr[1].length; i++)
  {
    if(custom_width == blackout_arr[1][i] && custom_drop == blackout_arr[2][i])
    {
      /*
      var salesave = blackout_arr[3][i]*blackout_arr[0];
      var saleprice = blackout_arr[3][i]-salesave;
      document.form_calcblackoutmm.price.value=formatDecimal(saleprice, true, 2);
      document.form_calcblackoutmm.saveprice.value=formatDecimal(salesave, true, 2);
      document.form_calcblackoutmm.blackout.value=i+1;
      */
      document.form_calcblackoutmm.blackout.value=i+1;
      priceRM = blackout_arr[3][i];
      
    }
  }
  
  return priceRM;
}

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 price = 0;
  var custom_width=parseInt(document.form_calcblackoutmm.custom_width.value);
  var custom_drop=parseInt(document.form_calcblackoutmm.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;
  }


  var recess=document.form_calcblackoutmm.recess.value;
  document.form_calcblackoutmm.blackout.value=0;
  
  if(recess=="Inside")
    custom_width -= 3;

  if(typeof(blackout_arr) != "undefined") {
    discountrm = blackout_arr[0];
    discount=discountrm;
    //alert("Width : " + custom_width + "\nDrop: " + custom_drop);
    price=GetRMPrice(custom_width, custom_drop);
    price_rm=price;
    //alert("Price RM: " + price);
  }

  
  if(recess=="Inside")
    custom_width += 3;
    
  price_mm = GetPrice(blackoutmm_arr[4], custom_width, custom_drop);
  
  if(!price || price == 0)
  {
    //alert('NU AM PRET DE RM!');
    //discount=parseFloat(blackoutmm_arr[4]); // MM discount
    discount = discount_;
    price = price_mm;
    if(price == 0) {


      var strMaxWidth=GetMaxWidth(blackoutmm_arr[4], custom_drop);
      alert("Maximum width for the selected drop is "+strMaxWidth+" mm");
      return;
    }
  }
  
  // nextday price
  var delivery = document.form_calcblackoutmm.nextday;
  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; // select standard
  } 
  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_calcblackoutmm.nextday.selectedIndex = 1;
    delivery.value = 0;
  
  if (delivery.value != 0)
    delivery_price = 5 / (1 - discount);

  /* *** NO NEXTDAY FOR SOME PRODS  *** */
  /* REMOVED
  var form_action  = document.form_calcblackoutmm.action;
  var split_action = form_action.split('http://www.webwoodenblinds.co.uk/cart/submitblackoutmm.php?blackoutmm_sample=');
  var pid = split_action[1];

  if(delivery.value != 0 && pid == 8) {
    alert("Sorry, the NEXT DAY service is not available for this product.");
    document.form_calcblackoutmm.nextday.selectedIndex = 1;
    delivery_price = 0;
    delivery.value = 0;
  }
  */
  /* *************************** */

  // controls price (for metal controls only)
  /* ******************************************************* */
  var controls_price = 0;
  var controls = document.form_calcblackoutmm.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
  /* ******************************************************* */
  
  price += delivery_price  + controls_price;
  var total_price = price;
  var save_price = formatDecimal(price * discount, true, 2);
  
  document.form_calcblackoutmm.price.value = formatDecimal(total_price - save_price, true, 2);
  document.form_calcblackoutmm.saveprice.value = formatDecimal(save_price, true, 2);
  
  document.form_calcblackoutmm.addtobasket.disabled=false;
}

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

function SelectProdType(prodtype)
{
    window.location = prodtype;
}

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

