// define the number of pages that the user should view before the survey is presented to them.
// counter that tracks page views will set itself to zero after the time period between surveys
// is reached.  That is, after the time period is up, the user will then have to view the number
// of pages specified below before the survey shows up
var maxPageViews = 5;

// define the number of days, at a minimum, between survey presentation.
var maxDaysBetweenSurvey = 90;

// the height in pixels of the survey window
var surveyWindowHeight = 400;

// the width in pixels of the survey window
var surveyWindowWidth = 650;

// the pixels from the top of the page where the survey pops open
var surveyWindowLocationTop = 150;

// the pixels from the left edge of the page where the survey pops open
var surveyWindowLocationLeft = 150;

// the location of the page containing the actual survey
var surveyContentLocation = 'http://www.questionpro.com/akira/TakeSurvey?id=691078';

// ***********Note, do not change any of the values below unless you know what you are doing
// The values above are used to configure the appearance of the survey, how often is shows up etc.
// The values below are used to do the calculations to determine if the cookie should show up and
// usually shouldn't need to be changed.
var viewedCookieName = 'viewedSurvey';
var pageViewCountCookieName = 'pageCount';

// hours per day * minutes per hour * seconds per minute * millis per second
var surveyBlackoutDurationMillis = maxDaysBetweenSurvey * 24 * 60 * 60 * 1000;


function customFunctionCreateWindow()
{

    var divId = createNewWindow(surveyWindowWidth,surveyWindowHeight,surveyWindowLocationLeft,surveyWindowLocationTop);
    document.getElementById('windowContent' + divId).innerHTML = '<iframe src=\"' + surveyContentLocation + '\" name=\"adpiframe\" id=\"adpiframe\" width=\"100%\" height=\"100%\" align=\"center\" Frameborder=\"0\" Scrolling=\"yes\" marginheight=\"0\" marginwidth=\"0\"></iframe>';
    //document.getElementById('windowContent' + divId).innerHTML = 'Hey there, this is the stuff';
}

function checkViewedSurvey(){
    //debugger;
    var allCookies = document.cookie;

    // if we find the cookie with the 'viewed cookie name', that means that the user has
    // seen the survey and we don't want to show it again.  This cookie will expire by itself
    // and so all we need to know is if it still exists or not
    var position = allCookies.indexOf(viewedCookieName);

    if (position != -1){
        return true;
    }

    return false;
}

function exceedsPageViewCount(){
    var pageViews = parseInt(getPageViews());
    //debugger;
    if (pageViews > maxPageViews){
        return true;
    } else {
        return false;
    }

}

function getPageViews(){

    var numPageViews = Get_Cookie(pageViewCountCookieName);
    if (isNaN(numPageViews)){
        numPageViews = 0;
    }
    return numPageViews;

}

function shouldDisplaySurvey(){
    if (! checkViewedSurvey()){
        if (exceedsPageViewCount()){
            return true;
        }
    }

    return false;
}

function setViewedSurvey(){
    //debugger;
    var currentDate = new Date();
    var currentMillis = currentDate.getTime();
    var futureMillis = currentMillis + surveyBlackoutDurationMillis;

    var endBlackout = new Date(futureMillis);

    var cookieString = viewedCookieName + '=Y; path=/; expires=' + endBlackout.toGMTString();
    //debugger;

    document.cookie = cookieString;
}

function incrementPageViewCount(){

    var oldValue = parseInt(getPageViews());
    var value = parseInt(oldValue) + 1;

    // now, reset this as a temporary cookie
    var cookieString = pageViewCountCookieName + '=' + value + '; path=/';
    document.cookie = cookieString;

}

function initializeSurveyIfNecessary(){
    if (shouldDisplaySurvey()){
        customFunctionCreateWindow();
        setViewedSurvey();
    }
}

incrementPageViewCount();

initializeSurveyIfNecessary();


