﻿// JScript File

var pageDuration= 3;

var surveyCookie="SurveyPopUp";
var startDate="startDate";
var pgsVisited="pageCount";

var showPopupCookie="showPopUp";
var mainstartDate="mainStartDate";

function ShowPopUpCheck(surveytext,popupstartTime, reappearTime, pageVisitCount)
{
    var cookiePresent=getCookie(surveyCookie);
    var showpopup=getCookie(showPopupCookie);

    if(showpopup=='')
    {
        var today = new Date(); 
        setCookie("showPopUp",mainstartDate+"="+today.getTime()+"&showPopUp=",today.getTime()+ 30*24*60*60*1000,'/','','');
        showpopup=getCookie(showPopupCookie);
    }

    var mainCookie=showpopup.split("&");
    var surveyCookieArray=cookiePresent.split("&");  
          
    if(mainCookie[1].split("=")[1].toUpperCase()=="YES")
    {
        //never show popup
        return;
    }
    else if(mainCookie[1].split("=")[1].toUpperCase()=="NO")
    {            
        var x= mainCookie[0].split("=")[1];
        var today=new Date();
        
        if(today.getTime() - x > parseInt(reappearTime)*24*60*60*1000)
        {
            //showpopup
            showConfirmBox(surveytext);
            setCookie("SurveyPopUp",cookieValue,-100,'/','','');
        }
    }
    else
    {
        if(cookiePresent!='')
        {
            if((surveyCookieArray[0].split("=")[1]!="") && (surveyCookieArray[1].split("=")[1]!=""))
            {
                var x=surveyCookieArray[0].split("=")[1];
                var today=new Date();
                var pgCnt=parseInt(surveyCookieArray[1].split("=")[1]);
                
                if(pgCnt>=(parseInt(pageVisitCount)-1))
                {
                    //showpopup
                    showConfirmBox(surveytext);
                    setCookie("SurveyPopUp",cookieValue,-100,'/','','');
                }
                else
                {
                    var today = new Date();
                    var pageCount=pgCnt+1;
                    var cookieValue= startDate+"="+today.getTime()+"&"+pgsVisited+"="+pageCount;
                    setCookie("SurveyPopUp",cookieValue,parseInt(surveyCookieArray[0].split("=")[1])+ 30*24*60*60*1000,'/','','');
                    setTimeout("timeoutfunction('"+cookieValue+"','"+surveytext+"')",((parseInt(surveyCookieArray[0].split("=")[1])+parseInt(popupstartTime)*60*1000) - today.getTime()));
                }
            }
            else
            {
                 var today = new Date();
                 var cookieValue= startDate+"="+today.getTime()+"&"+pgsVisited+"="+1;
                 setCookie("SurveyPopUp",cookieValue,parseInt(surveyCookieArray[0].split("=")[1])+ 30*24*60*60*1000,'/','','');
                 setTimeout("timeoutfunction('"+cookieValue+"','"+surveytext+"')",((parseInt(surveyCookieArray[0].split("=")[1])+parseInt(popupstartTime)*60*1000) - today.getTime()));
            }
        }
        else
        {
            var today = new Date();    
            var cookieValue= startDate+"="+today.getTime()+"&"+pgsVisited+"="+1;
            setCookie("SurveyPopUp",cookieValue,today.getTime()+ 30*24*60*60*1000,'/','','');
            setTimeout("timeoutfunction('"+cookieValue+"','"+surveytext+"')",(today.getTime()+parseInt(popupstartTime)*60*1000 - today.getTime()));
        }        
    }
}

function timeoutfunction(cookieValue,surveytext)
{    
    showConfirmBox(surveytext);
}


function getCookie(cookieName)
{
    var search=cookieName + "=";
    if(document.cookie.length>0)
    {
        var cookieVar= document.cookie.indexOf(cookieName);
        if(cookieVar!=-1)
        {
            cookieVar+=search.length;
            end=document.cookie.indexOf(";",cookieVar);
            if(end==-1)
            {
                end=document.cookie.length;
            }
            return document.cookie.substring(cookieVar,end);
        }
        else
        {
            return '';
        }
    }
    else
    {
        return '';
    }
}

function setCookie(name, value, expire,path,domain,secure) 
{
   var expdate=new Date(expire);
   document.cookie = name + "=" + value
   + ((expire == null) ? "" : "; expires=" + expdate.toGMTString()+( ( path ) ? ";path=" + path : "" ) +
 ((domain) ? ";domain=" + domain : "" ) +
 ((secure) ? ";secure" : "" ));
}

function showConfirmBox(surveytext)
{
    showDialog('Kodak Graphic Communications Group',surveytext.split(";")[0]+'<br /><br/><br /><div style="position:relative; vertical-align:middle; padding-left:160px; padding-right:160px;"><div style="float:left;color:#0066cc; cursor:pointer; vertical-align:middle;" onclick="responseFunction(\'YES\')"><span>YES</span></div><div style="cursor:pointer;color:#0066cc; float:right;" onclick="responseFunction(\'NO\')"><span>NO</span></div></div>','prompt','',surveytext.split(";")[1]);
}

function responseFunction(answer)
{
    var today = new Date();
    if(answer=='YES')
    {
        setCookie("showPopUp",mainstartDate+"="+today.getTime()+"&showPopUp=YES",today.getTime()+ 30*24*60*60*1000,'/','','');
        window.open(surveytext.split(";")[1]);
        hideDialog();
    }
    else
    {
        var cookieValue= startDate+"="+today.getTime()+"&"+pgsVisited+"=";
        setCookie("showPopUp",mainstartDate+"="+today.getTime()+"&showPopUp=NO",today.getTime()+ 30*24*60*60*1000,'/','','');
        setCookie("SurveyPopUp",cookieValue,today.getTime()+ 30*24*60*60*1000,'/','','');
        hideDialog();
    }    
}