﻿// *******************************************************************************
// **  ENTER THE Setup Variables for the Exit Survey here ************************
var sourceDir = "http://www.scarborough.gov.uk/jsexit/";
// incidence is measured as number of times per 100. 100 = always, -1 = never
var incidence = 15;
var active = true;
// *******************************************************************************

// Check for active flag...
if (active) {

// check if we have already instigated the window and must always load the js files. 
var CookieVal = readCookie("XitSurvey");
    if (CookieVal=='true') {incidence=100;}
//alert(CookieVal + ":" + (CookieVal!='false') + "   inc:" + incidence);
    // Check if we should run the Invitation
    if (incidence_rate(incidence) && (CookieVal!='false')) {
        // great - lets load the files
        document.write('<script type="text/javascript" src="' + sourceDir + 'XitVars.js"><\/script>');
        document.write('<link rel="stylesheet" type="text/css" href="' + sourceDir + 'Xitsurvey.css" />');
        document.write('<script type="text/javascript" src="' + sourceDir + 'jquery.js"><\/script>');
        document.write('<script type="text/javascript" src="' + sourceDir + 'goInvite.js"><\/script>');
    }
}

function incidence_rate(incidence) {
    var perc = incidence;
    var rndNum = Math.round(Math.random() * 100);

    if (rndNum <= perc) {
        return 1; // GO for it
    }
    else {
        return 0; // Not this time
    }
}

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 "empty";
}
