//*****************************************************************************************************************

// Sluji za iz4islqvane na razlikata v dni mejdu start date i end date
// na stranicata ot koqto se vika, zadyljitelno trqbva da ima poletata:
//Starting_date i End_date. Sy6to taka i hidden formata na googlecheckout
function DateDiffInDays()
{         
      var strStartDate = document.getElementById("Starting_date").value;
      var strEndDate = document.getElementById("End_date").value;
      var product = document.getElementById("product_name").value;
      var dateStartDate;
      var dateEndDate;
            
      if (strStartDate != '')
      {
        dateStartDate = cal_prs_date1(document.getElementById("Starting_date").value);
      }
      
      if (strEndDate != '')
      {
        dateEndDate = cal_prs_date1(document.getElementById("End_date").value);
      }
     
      var numStartDate = Date.parse(dateStartDate);
      var numEndDate = Date.parse(dateEndDate);
      
      var item_price = document.getElementById("item_price_1");
      
      //ako ednata ot dvete dati ne e popylnena, vry6tame ni6to
      if (isNaN(numStartDate) || isNaN(numEndDate))
      {
        item_price.value = "";
        return "";
      }
      
      var diff = (numEndDate - numStartDate)/1000/3600/24;
     
      return Math.round(diff);
}   

//*****************************************************************************************************************

function CalculatePrice()
{
    //ednata promenliva e za cenata koqto se pokazva na clienta,
    //a drugata za hidden formata na GoogleCheckout
    var finalPrice = document.getElementById("price");
    var item_price = document.getElementById("item_price_1");
var prediod_dni = document.getElementById("prediod_dni");
    var product = document.getElementById("product_name").value;
    
    item_price.value = "";
    finalPrice.value = "";  

    
//alert(product);
    var pricePeriods = GetPricePerdiods(product);
    

//alert (product);
//alert (pricePeriods);




    if (pricePeriods == -1)
    {
        //Marti, do tuk po princip ne trqbva da se stiga :)
        alert("There is no price list defined for this product");
        return;
    }
    
    var i;
    var numOfDays = DateDiffInDays();

//alert (numOfDays);

//if(product=="car_02t_02")
//{
//if(numOfDays>=15)
//{
//item_price.value = numOfDays *37
//finalPrice.value = numOfDays *37;
//ValidatePirce(item_price);
//}
//}

switch(product)
{
case "car_01t_02":
{
if(numOfDays>=15)
{
//alert(product);
item_price.value = numOfDays *31;
finalPrice.value = numOfDays *31;
ValidatePirce(item_price);
return;
}
}

case "car_02t_02":
{
if(numOfDays>=15)
{
//alert(product);
item_price.value = numOfDays *37;
finalPrice.value = numOfDays *37;
ValidatePirce(item_price);
return;
}
}

case "car_03t_02":
{
if(numOfDays>=15)
{
//alert(product);
item_price.value = numOfDays *43;
finalPrice.value = numOfDays *43;
ValidatePirce(item_price);
return;
}
}

case "car_04t_02":
{
if(numOfDays>=15)
{
//alert(product);
item_price.value = numOfDays *43;
finalPrice.value = numOfDays *43;
ValidatePirce(item_price);
return;
}
}

case "car_05t_02":
{
if(numOfDays>=15)
{
//alert(product);
item_price.value = numOfDays *53;
finalPrice.value = numOfDays *53;
ValidatePirce(item_price);
return;
}
}

case "car_06t_02":
{
if(numOfDays>=15)
{
//alert(product);
item_price.value = numOfDays *53;
finalPrice.value = numOfDays *53;
ValidatePirce(item_price);
return;
}
}

case "car_07t_02":
{
if(numOfDays>=15)
{
//alert(product);
item_price.value = numOfDays *73;
finalPrice.value = numOfDays *73;
ValidatePirce(item_price);
return;
}
}

case "car_08t_02":
{
if(numOfDays>=15)
{
//alert(product);
item_price.value = numOfDays *109;
finalPrice.value = numOfDays *109;
ValidatePirce(item_price);
return;
}
}

case "car_09t_02":
{
if(numOfDays>=15)
{
//alert(product);
item_price.value = numOfDays *63;
finalPrice.value = numOfDays *63;
ValidatePirce(item_price);
return;
}
}

}


    
//alert("Broi dni: "+numOfDays);

    //ako ednata ot dvete dati ne e popylneni
    if (numOfDays == "" || numOfDays < 0)
    {
        alert("Please fill in Start and End date. (Start date should be before end date)");
        return;
    }
    
    for (i=0; i<=pricePeriods.length; i=i+3)
    {
        if (numOfDays >= pricePeriods[i] && numOfDays <= pricePeriods[i+1])
        {
            item_price.value = numOfDays * pricePeriods[i+2];
            finalPrice.value = numOfDays * pricePeriods[i+2];
            break;
        }      
    }
    
    ValidatePirce(item_price);    
}

//*****************************************************************************************************************

//vzimane na pravilniqt masiv s ceni ot Prices.js za syotvetniqt product
function GetPricePerdiods(model)
{
    switch(model)
    {
//Tuk zapochvat Table 1
        case "car_00t_01":
            return car_00t_01;

        
        case "car_01t_01":
            return car_01t_01;

        
        case "car_02t_01":
            return car_02t_01;

        
        case "car_03t_01":
            return car_03t_01;

        
        case "car_04t_01":
            return car_04t_01;

        
        case "car_05t_01":
            return car_05t_01;

        
        case "car_06t_01":
            return car_06t_01;

        
        case "car_07t_01":
            return car_07t_01;

        
        case "car_08t_01":
            return car_08t_01;

        
        case "car_09t_01":
            return car_09t_01;

        
        case "car_10t_01":
            return car_10t_01;

        
//Tuk zapochvat Table 3 (respektivno sega e 2)
        case "car_01t_02":
            return car_01t_02;

        
        case "car_02t_02":
            return car_02t_02;

        
        case "car_03t_02":
            return car_03t_02;

        
        case "car_04t_02":
            return car_04t_02;

        
        case "car_05t_02":
            return car_05t_02;

        
        case "car_06t_02":
            return car_06t_02;

        
        case "car_07t_02":
            return car_07t_02;

        
        case "car_08t_02":
            return car_08t_02;

        
        case "car_09t_02":
            return car_09t_02;

        
        default:
            return -1;
    }
}

