﻿//1.SurveyId must be greater than 0 to show survey.
//2.Survey will only be shown to every third visitors.
//3.if survey has been shown, it will not be shown and counted again in current session.
//4.if the visit choose NoThanks, the survey will not be shown in 10days, but the visit will still be counted
function showSurvey(surveyID) {
    if (surveyID > 0) {
        var surveycount;
        
        if (get_cookie("surveyshowed") != "true") {
            jQuery.ajax({
                type: 'POST',
                url: "/Survey/UpdateSurveyCount.aspx",
                data: { SurveyID: surveyID },
                success: function(data) {
                    surveycount = data;
                    // x%3 if return 0 then x is multiple of 3
                    // then show survey after 5sec or 5000mil sec
                    if ((surveycount % 1) == 0) {
                        if (readCookie("showSurvey") != 0) {
                            window.setTimeout(function() {
                                jQuery.facebox({ ajax: '/survey/survey1.htm' });
                            }, 5000);
                            document.cookie = "surveyshowed=true";
                        }
                    }
                },
                error: function(error) { alert("error"); alert(error); }
            });
        }
    }
}

function noThanksClicked() {
    jQuery.facebox.close();
    createCookie("showSurvey", 0, 10);
}

function goClicked() {
    jQuery.facebox.close();
}


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 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;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

//Get cookie routine by Shelley Powers
//Get session cookie stored in document.cookie
function get_cookie(Name) {
    var search = Name + "="
    var returnvalue = "";
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search)
        // if cookie exists
        if (offset != -1) {
            offset += search.length
            // set index of beginning of value
            end = document.cookie.indexOf(";", offset);
            // set index of end of cookie value
            if (end == -1) end = document.cookie.length;
            returnvalue = unescape(document.cookie.substring(offset, end))
        }
    }
    return returnvalue;
}
