﻿var AppAlert=function(pAppURL) {
  this.userAgent=navigator.userAgent||navigator.vendor||window.opera;
  this.appURL=pAppURL;
  this.androidBrowserRegex=/ip(hone|od)/i;
  this.myDealerAppRegex=/MyDealerApp/i;
  var that=this;
  
  this.detect=function() {
    if(!that.getCookie('hideappalert')) {
      if(that.androidBrowserRegex.test(that.userAgent) && !that.myDealerAppRegex.test(that.userAgent)) {
        var oConfirm=confirm('\
             Want to take us on the go? We support the iPhone with a free App!\n\n\
             Tap OK to see more details, or Cancel to dismiss this prompt indefinitely.\
            ');
        that.setCookie('hideappalert','true',365);
        if(oConfirm) {
          window.location.href=that.appURL;
        } 
      }
    }
  };

  this.getCookie=function(pName) {
    var nameEQ=pName+'=';
    var ca=document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
      var c=ca[i];
      while(c.charAt(0)==' ')c=c.substring(1,c.length);
      if(c.indexOf(nameEQ)==0) return c.substring(nameEQ.length,c.length);
    }
    return null;
  };

  this.setCookie=function(pName,pValue,pDays) {
    if(pDays) {
      var date=new Date();
      date.setTime(date.getTime()+(pDays*24*60*60*1000));
      var expires='; expires='+date.toGMTString();
    } else {
      var expires='';
    }
    document.cookie=pName+'='+pValue+expires+'; path=/';
  };

  this.detect();
};


