﻿/*$(document).ready(function() {
    alert("DOCUMENT READY FUNC");
    tb_show("Vinnova", "/vinnova_templates/Survey-test.aspx?height=300&width=350", "");
});*/

function scriptHandler(runParam, surveyChoiceParam, pageClicksParam, timeOnSiteParam, visitedURLParam, printParam) {
    //alert("runParam: " + runParam + " surveyC: " + surveyChoiceParam + " pageClicks: " + pageClicksParam + " time: " + timeOnSiteParam + " visUrl: " + visitedURLParam);
    $(document).ready(function() {
        if (runParam == "true") {
            /* print of true */
            /*if (printParam == "true") {
                window.print();
            }*/
            /* Stores nr of visited pages in cookie*/
            if (readCookie("pageVisits") == null) {
                createCookie("pageVisits", "1", 0);

            }
            /* Increments nr of visited pages in cookie */
            if (readCookie("pageVisits") != null) {

                if (readCookie("lastVisitedURL") != null) {
                    var currURL = readCookie("lastVisitedURL");
                    var urlStr = document.URL;
                    if (currURL.indexOf(urlStr) == -1) {
                        /* Increment if current page differs from previous */
                        var c = readCookie("pageVisits");
                        var inc = (parseInt(c) + 1);
                        createCookie("pageVisits", inc, 0);
                    }
                    createCookie("lastVisitedURL", urlStr, 0);
                }

            }
            /* Creates session timeStampSessionStart */
            if (readCookie("timeStampSessionStart") == null) {
                //alert("timeStampSessionStart null!");
                var t = Date.parse(new Date()); //milliseconds since 1970/01/01
                createCookie("timeStampSessionStart", t, 0);
            }
            /* Creates new VisitedURLs cookie -> saves URLs that user visited on site */
            if (readCookie("visitedURLs") == null) {
                var currentURL = document.URL;
                var days = 7;
                createCookie("visitedURLs", currentURL, days);

                /* Create current URL-cookie (not array) in order to save new page visits instead of pageclicks */
                createCookie("lastVisitedURL", currentURL, 0);
            }
            /* Checks current visitor-URL, appends to old URLs, create "URL-array"-cookie */
            if (readCookie("visitedURLs") != null) {
                var currentURL = document.URL;
                var URLCookie = readCookie("visitedURLs");
                var URLAppend = URLCookie + currentURL;
                //alert("expiryDateCookie: " + (readCookie("expiryDateCookie") / (24 * 60 * 60 * 1000)));
                createCookie("visitedURLs", URLAppend, 0);
            }

            var timeNow = Date.parse(new Date()); //milliseconds since 1970/01/01
            var timeDiff = (timeNow - readCookie("timeStampSessionStart")); // Visitor session time
            var numberOfPageClicks = parseInt(readCookie("pageVisits")); // Number of clicks by visitor on site
            var str = visitedURLParam; // Specified URL,
            var URLArray = readCookie("visitedURLs");

            /* parse suitable parameters to integers */
            if (pageClicksParam != "null") {
                pageClicksParam = parseInt(pageClicksParam);
            }
            if (timeOnSiteParam != "null") {
                timeOnSiteParam = parseInt(timeOnSiteParam);
            }


            /* Show survey if survey not shown before or closed with x in popup-window or if
            * survey was declined by visitor and at least one of three different conditions are met
            */
            /*if ((readCookie("survey") == null) && (readCookie("StngCookie") == null)) { // || ((readCookie("survey") == surveyChoiceParam) && ((numberOfPageClicks > pageClicksParam) || (timeDiff > (timeOnSiteParam * 1000)) || (URLArray.indexOf(str) > -1)))) {
            alert("1");
            tb_show("Vinnova", "/vinnova_templates/Survey-test.aspx?height=300&width=350", "");*/
            /* If the user presses 'Stng' on popup the survey-cookie is still null.
            * To prevent the popup to show before any of the parameter conditions are met, 
            * another (session )cookie is created.
            */
            //createCookie("StngCookie", "true", 0);

            //}



            /* If the visitor has not been exposed to the survey, and if at least one of three conditions are met
            * -> show survey-popup. If the visitor presses the "Stäng"-button on the popup, a session cookie is created
            * that prevents the popup from showing during the current session.
            */
            if ((readCookie("survey") == null) && (readCookie("StngCookie") == null) && ((numberOfPageClicks > pageClicksParam) || (timeDiff > (timeOnSiteParam * 1000)) || (URLArray.indexOf(str) > -1))) {
                createCookie("StngCookie", "true", 0);
                //(readCookie("survey") == surveyChoiceParam)
                //(readCookie("parameterBasedPopup") == null)
                //createCookie("parameterBasedPopup", "true", 21);
                //alert("2");
                tb_show("Webbenkät", "/vinnova_templates/Survey-test.aspx?height=270&width=450", "");
            }

        }
        /* print of true */
        if (printParam == "true") {
           window.print();
        }

    });
}


/* Gets called from Survey-test.aspx when user presses button in survey-popup.
 * The cookie is saved a specified nr of days.
 */
function surveyAnswer(participated) {
    //alert("participated: " + participated);
    createCookie("survey", participated, 180);

}

/* Creates a Cookie, if days=0 the cookie terminates when browser closes. 
 * If days = -1, cookie terminates immediately
 */
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function createExpiryDateCookie(days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        //var expires = "; expires=" + date.toGMTString();
        var expires = (days * 24 * 60 * 60 * 1000);
        createCookie("expiryDateCookie",expires, days);
}

/* Reads cookie, returns null if not existing*/
function readCookie(name) {
    var nameEQ = name + "=";
    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;
}

/* Deletes cookie by setting expierydate to old date */
function eraseCookie(name) {
    createCookie(name, "", -1);
}

