﻿//##### slider carat #####
var caratSliderHandle; //so we have a handle to the slider
var priceSliderHandle;
var ratioSliderHandle;
var c_m=1;

function OnCaratSliderLoad(sender, eventArgs)
{    
    caratSliderHandle = sender; //sort out a handle to the slider to be used wherever
    if (caratSliderHandle!=null )
    {
        var minCarat = parseFloat(diamondPreferences.MinCarat).toFixed(2);
        var maxCarat = parseFloat(diamondPreferences.MaxCarat).toFixed(2);
        
        var minPos = CalculatePositionFromCaratValue(minCarat);
        var maxPos = CalculatePositionFromCaratValue(maxCarat);
        
        caratSliderHandle.set_selectionStart(minPos);
        caratSliderHandle.set_selectionEnd(maxPos);
                
        document.getElementById('caratMin').innerHTML =  parseFloat(minCarat).toFixed(2);
        document.getElementById('caratMax').innerHTML = parseFloat(maxCarat).toFixed(2);
    }
}

function SliderCaratSliding(sender, eventArgs)
{
    SliderCaratPosition(sender);
}

function SliderCaratSlidingComplete(sender, eventArgs)
{
    SliderCaratPosition(sender);
}

function SliderCaratFinishedSliding()
{
    //Refresh results
    diamondPreferences.MinCarat = document.getElementById('caratMin').innerHTML;
    diamondPreferences.MaxCarat = document.getElementById('caratMax').innerHTML;
    diamondPreferences.CurrentPageNumber = 0; //reset to the first page
    UpdateResults(true);
}

function SliderCaratPosition(sender)
{
    var isSliderTheLeftOne =  GetActiveDragHandle(sender);
    var mypos = isSliderTheLeftOne == true? sender.get_selectionStart() : sender.get_selectionEnd();
    
    if(isSliderTheLeftOne)
        document.getElementById('caratMin').innerHTML =  CalculateCaratValue(mypos);
    else
        document.getElementById('caratMax').innerHTML = CalculateCaratValue(mypos);
}

function CalculatePositionFromCaratValue(caratWeight)
{
    var position=0;
    if(caratWeight>=0.18 && caratWeight<=1.00)
    {
        caratWeight=caratWeight - 0.18;
        position = caratWeight/0.02;        
    }
    else if(caratWeight>1.00 && caratWeight<=2.00)
    {
        caratWeight=caratWeight - 1.0;
        position = caratWeight/0.05 + (41); //previous positions
    }
    else if(caratWeight>2.00 && caratWeight<=2.50)
    {
        caratWeight=caratWeight - 2.0;
        position = caratWeight/0.10 + (61); //previous positions
    }
    else if(caratWeight>2.50 && caratWeight<=4.00)
    {
        caratWeight=caratWeight - 2.5;
        position = caratWeight/0.25 + (66); //previous positions
    }
    else if(caratWeight>4.00 && caratWeight<=10.00)
    {
        caratWeight=caratWeight - 4.0;
        position = caratWeight/0.50 + (72); //previous positions
    }
    else if(caratWeight>10.00 && caratWeight<=20.00)
    {
        caratWeight=caratWeight - 10;
        position = caratWeight/5 + (84); //previous positions
    }
    else if(caratWeight>20.00 && caratWeight<=30.00)
    {
        caratWeight=caratWeight - 20.0;
        position = caratWeight/10 + (86); //previous positions
    }
    
    return position;
}

function CalculateCaratValue(mypos)
{
    var caratWeight=0;
    var multiplier=0;
    
    if(mypos<=41) //.18 to 1.00 incs of .02
    {
        caratWeight=.18;
        multiplier = .02;
    }
    else if(mypos>41 && mypos<=61) //1.00 to 2.00 incs of .05
    {
        caratWeight=1;
        mypos-=41;
        multiplier =  .05;
    }
    else if(mypos>61 && mypos<=66) //2.00 to 2.50 incs of .10
    {
        caratWeight=2;
        mypos-=61;
        multiplier = .10;
    }
    else if(mypos>66 && mypos<=72) //2.50 to 4.00 incs of .25
    {
        caratWeight=2.5;
        mypos-=66;
        multiplier = .25;
    }
    else if(mypos>72 && mypos<=84) //4.00 to 10.00 incs of .50
    {
        caratWeight=4;
        mypos-=72;
        multiplier = .50;
    }
    else if(mypos>84 && mypos<=86) //10 to 20 incs of 5.0
    {
        caratWeight=10;
        mypos-=84;
        multiplier = 5;
    }
    else if(mypos>86) //20 to 30, inc of 10.00
    {
        caratWeight=20;
        mypos-=86;
        multiplier = 10;
    }
    
    caratWeight+= (mypos * multiplier);
    
    return caratWeight.toFixed(2);
}


//##### slider price #####
function OnPriceSliderLoad(sender, eventArgs)
{
    priceSliderHandle = sender; //sort out a handle to the slider to be used wherever
    if (priceSliderHandle!=null )
    {
        Website.WebServices.WebService.GetCM(GetCookie(), 
        function(success)
        {
            c_m = success; //we use this for the price slider
            var minPrice = diamondPreferences.MinPrice;
            var maxPrice = diamondPreferences.MaxPrice;
            
            var minPos = CalculatePositionFromPriceValue(minPrice);
            var maxPos = CalculatePositionFromPriceValue(maxPrice);
            
            priceSliderHandle.set_selectionStart(minPos);
            priceSliderHandle.set_selectionEnd(maxPos);
            
            document.getElementById('priceMin').innerHTML =  FormatAmount(parseFloat(minPrice).toFixed(0));
            document.getElementById('priceMax').innerHTML = FormatAmount(parseFloat(maxPrice).toFixed(0));
        },
        function(error)
        {
        });
    }
    
}
function SliderPriceSliding(sender, eventArgs)
{
    SliderPricePosition(sender);
}

function SliderPriceSlidingComplete(sender, eventArgs)
{
    SliderPricePosition(sender);
}

function SliderPriceFinishedSliding()
{
     //Refresh results
    diamondPreferences.MinPrice = document.getElementById('priceMin').innerHTML.replace(/\,/g,'');
    diamondPreferences.MaxPrice = document.getElementById('priceMax').innerHTML.replace(/\,/g,'');
    diamondPreferences.CurrentPageNumber = 0; //reset to the first page
    UpdateResults(false);
}

function SliderPricePosition(sender)
{
    var isSliderTheLeftOne =  GetActiveDragHandle(sender);
    var mypos = isSliderTheLeftOne == true? sender.get_selectionStart() : sender.get_selectionEnd();
    
    if(isSliderTheLeftOne)
        document.getElementById('priceMin').innerHTML =  FormatAmount(CalculatePriceValueFromPosition(mypos));
    else
        document.getElementById('priceMax').innerHTML = FormatAmount(CalculatePriceValueFromPosition(mypos));
}

//NO MATTER WHAT THE PRICE, IT CONVERTS IT TO GBP FIRST TO STICK TO OUR SLIDER PROGRESSION
function CalculatePositionFromPriceValue(price)
{
    if(c_m!=1)
    {
        price = price/c_m;
    }
    var position=0;
    
    if(price>=100 && price<=2500)
    {
        price=price - 100;
        position = price/100;
    }
    else if(price>2500 && price<=10000)
    {
        price=price - 2500;
        position = price/500 + (24); //previous positions
    }
    else if(price>10000 && price<=20000)
    {
        price=price - 10000;
        position = price/1000 + (40); //previous positions
    }
    else if(price>20000 && price<=40000)
    {
        price=price - 20000;
        position = price/2000 + (50); //previous positions
    }
    else if(price>40000 && price<=100000)
    {
        price=price - 40000;
        position = price/5000 + (60); //previous positions
    }
    else if(price>100000 && price<=200000)
    {
        price=price - 100000;
        position = price/10000 + (72); //previous positions
    }
    else if(price>200000 && price<=400000)
    {
        price=price - 200000;
        position = price/50000 + (82); //previous positions
    }
    else if(price>400000)
    {
        position = 87; //previous positions
    }
    return position;
}

function CalculatePriceValueFromPosition(mypos)
{
    var price=0;
    var multiplier=0;
    
    if(mypos<=24) //100 to 2500 incs of 100
    {
        price=100;
        multiplier = 100;
    }
    else if(mypos>24 && mypos<=40) //2,500 to 10,000 incs of 500
    {
        price=2500;
        mypos-=25;
        multiplier =  500;
    }
    else if(mypos>40 && mypos<=50) //10,000 to 20,000 incs of 1,000
    {
        price=10000;
        mypos-=41;
        multiplier = 1000;
    }
    else if(mypos>50 && mypos<=60) //20,000 to 40,000 incs of 2,000
    {
        price=20000;
        mypos-=50;
        multiplier = 2000;
    }
    else if(mypos>60 && mypos<=72) //40,000 to 100,000 incs of 5,000
    {
        price=40000;
        mypos-=60;
        multiplier = 5000;
    }
    else if(mypos>72 && mypos<=82) //100,000 to 200,000 incs of 10,000
    {
        price = 100000;
        mypos-=72;
        multiplier = 10000;
    }
     else if(mypos>82 && mypos<=86) //200,000 to 400,000 incs of 50,000
    {
        price = 200000;
        mypos-=82;
        multiplier = 50000;
    }
    else if(mypos>86) //400,000 + inc infinite
    {
        price = 0;
        mypos-=86;
        multiplier = 10000000;
    }
    
    price = ((mypos * multiplier) + price)*c_m;
    
    return price.toFixed(0);
}


//##### slider ratio #####

function OnRatioSliderLoad(sender, eventArgs)
{    
    if (sender != null )
    {
        ratioSliderHandle = sender; //sort out a handle to the slider to be used wherever
        var minratio = parseFloat(diamondPreferences.MinRatio).toFixed(2);
        var maxratio = parseFloat(diamondPreferences.MaxRatio).toFixed(2);
        
        var minPos = CalculatePositionFromratioValue(minratio);
        var maxPos = CalculatePositionFromratioValue(maxratio);
        
        ratioSliderHandle.set_selectionStart(minPos);
        ratioSliderHandle.set_selectionEnd(maxPos);
        
        document.getElementById('ratioMin').innerHTML =  parseFloat(minratio).toFixed(2);
        document.getElementById('ratioMax').innerHTML = parseFloat(maxratio).toFixed(2);
    }
}

function SliderRatioSliding(sender, eventArgs)
{
    SliderRatioPosition(sender);
}

function SliderRatioSlidingComplete(sender, eventArgs)
{
    SliderRatioPosition(sender);
}

function SliderRatioFinishedSliding()
{
    //Refresh results
    diamondPreferences.MinRatio = document.getElementById('ratioMin').innerHTML;
    diamondPreferences.MaxRatio = document.getElementById('ratioMax').innerHTML;
    
    UpdateResults(false);
}

function SliderRatioPosition(sender)
{
    var isSliderTheLeftOne =  GetActiveDragHandle(sender);
    var mypos = isSliderTheLeftOne == true? sender.get_selectionStart() : sender.get_selectionEnd();
    
    if(isSliderTheLeftOne)
        document.getElementById('ratioMin').innerHTML =  CalculateRatioValue(mypos);
    else
        document.getElementById('ratioMax').innerHTML = CalculateRatioValue(mypos);
}

function CalculatePositionFromratioValue(ratioValue)
{
    var position=0;
    
    if(ratioValue>=1 && ratioValue<=1.2)
    {
        ratioValue=ratioValue - 1;
        position = ratioValue/0.01;
    }
    else if(ratioValue>1.2 && ratioValue<=2.64)
    {
        ratioValue=ratioValue - 1.2;
        position = ratioValue/0.05 + (21); //previous positions
    }
    else if(ratioValue>2.64 && ratioValue<=3.73)
    {
        ratioValue=ratioValue - 2.64;
        position = ratioValue/0.05 + (46); //previous positions
    }
    else if(ratioValue>3.73 && ratioValue<=4.13)
    {
        ratioValue=ratioValue - 3.73;
        position = ratioValue/0.05 + (65); //previous positions
    }
    else if(ratioValue>4.13 && ratioValue<=4.82)
    {
        ratioValue=ratioValue - 4.13;
        position = ratioValue/0.05 + (72); //previous positions
    }
    else if(ratioValue>4.82 && ratioValue<=5.01)
    {
        ratioValue=ratioValue - 4.82;
        position = ratioValue/0.05 + (84); //previous positions
    }
    
    return position;
}

function CalculateRatioValue(mypos)
{
    var ratioValue=0;
    var multiplier=0;
    
    if(mypos<=21) 
    {
        ratioValue=1.0;
        multiplier = .01;
    }
    else if(mypos>21 && mypos<=46) 
    {
        ratioValue=1.20;
        mypos-=21;
        multiplier =.05;
    }
    else if(mypos>46 && mypos<=65) 
    {
        ratioValue=2.64;
        mypos-=46;
        multiplier = .05;
    }
    else if(mypos>65 && mypos<=72) 
    {
        ratioValue=3.73;
        mypos-=65;
        multiplier = .01;
    }
    else if(mypos>72 && mypos<=84) 
    {
        ratioValue=4.13;
        mypos-=72;
        multiplier = .05;
    }
    else if(mypos>84 && mypos<=86) 
    {
        ratioValue=4.82;
        mypos-=84;
        multiplier = .05;
    }
    else if(mypos>86)
    {
        ratioValue=5;
        mypos-=86;
        multiplier = .01;
    }
    
    ratioValue+= (mypos * multiplier);
    
    return ratioValue.toFixed(2);
}


function GetActiveDragHandle(slider)
{
    var dragHandles = slider.get_dragHandles();
    var activeDragHandle = slider.get_activeHandle();

    // The first drag handle is active.
    if(activeDragHandle === dragHandles[0]) return true;
    // The second drag handle is active.
    return false;
}
