Mon Mar 27 2017
Copied to clipboard! Copy reply
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
;(function() {

    if (!window.location.href.match(/test/))
    {
      // Leave this uncommented until ready for prod
      return
    }
    else
    {
      // For debugging/testing - uncomment when testing if needed
      //window.sessionStorage.clear()

      // Mimic a product in cart
      // var rs = {
      //   pixp_cartCount: "1",
      //   pixp_price: "229.00",
      //   pixp_name: "TOTALe Online - Basic - 24 months"
      // }
    }


    var conf = {
      // Countdown time in minutes
      timer: 10,

      // Prices to show RTO for
      prices: {
        "229.00": {
          dollarOff: '$20',
          promoToApply: {
            'S5': "tajmahal_S5", // Will bring it to 209
            '24': "tajmahal_24m" // Will bring it to 209
          }
        },
        "179.00": {
          dollarOff: '$10',
          promoToApply: {
            'S5': "colosseum_S5",  // Will bring it to 169
            '24': "colosseum_24m"  // Will briung it to 169
          }
        }
      }
    }




    /*
     * We only want to show if there's 1 product in cart,
     * the product is either S5 or 24M, and the product
     * price is one of the keys in `map` {} above
     */

     // Leave if more than one product in cart
    if (rs.pixp_cartCount != "1")
    {
      return
    }

    // Product's price is not found in conf's "prices"
    if (Object.keys(conf.prices).indexOf(rs.pixp_price) == -1)
    {
      return
    }

    // regex should be promoToApply's keys. ie, S5/24
    // Since those are the products we want to apply promos for, this makes
    // sure rto only shows for products are keys of promosToApply
    var regex = new RegExp(Object.keys(conf.prices[rs.pixp_price].promoToApply).join('|'), 'i')
    if (!rs.pixp_name.match(regex))
    {
      return
    }

    // Which level do we have in cart?
    var level = null

    if (rs.pixp_name.match(/24 months/i))
    {
      level = '24'
    }

    if (rs.pixp_name.match(/S5/i))
    {
      level = 'S5'
    }

    if (!level)
    {
      return
    }


    var endtime;

    if (window.sessionStorage.getItem('rto_endtime'))
    {
      endtime = new Date(window.sessionStorage.getItem('rto_endtime'))
      if (new Date > endtime)
      {
        stopClock()
        return
      }
    }
    else
    {
      endtime = new Date(new Date().getTime() + conf.timer * 60 * 1000)
    }


    // Cache dom elements
    var $hours = jQuery('span.hours > span:first-of-type')
    var $minutes = jQuery('span.minutes > span:first-of-type')
    var $seconds = jQuery('span.seconds  > span:first-of-type')


    // Starts and update the clock
    function updateClock()
    {
      var timeleft = new Date( endtime - (new Date()) );
      $hours.html( (timeleft.getHours() < 10 ? '0' : '') + timeleft.getHours())
      $minutes.html( (timeleft.getMinutes() < 10 ? '0' : '') + timeleft.getMinutes())
      $seconds.html( (timeleft.getSeconds() < 10 ? '0' : '') + timeleft.getSeconds())
      window.sessionStorage.setItem('rto_endtime', endtime.toString())

      if (Math.floor(timeleft/1000) <= 0)
      {
        stopClock()
      }
    }


    // Stops the clock
    function stopClock()
    {
      jQuery('#rto-wrap').remove()
      clearInterval(clockinterval)
    }


    // Fire up the clock. Update it every 1s
    //var $rto = $(rtodom)
    var $rto = jQuery('#rto-wrap').detach()
    jQuery(document.body).prepend($rto)
    $rto.css({display:'block'})

    jQuery('.dollaroff').html(conf.prices[rs.pixp_price].dollarOff)
    updateClock()

    var clockinterval = setInterval(updateClock,1000);



    jQuery('.rto-callout-button > button').click(function(e) {
      window.sessionStorage.setItem('rto_endtime', new Date(-1).toString())
      endtime = new Date(-1)
      var promo_to_apply = conf.prices[rs.pixp_price].promoToApply[level]
      jQuery('#coupon_code').val(promo_to_apply)
      jQuery('#discountsubmit').click()
    })

  })()