/*
* GfK NOP DHTML  (c) 2010 GfK NOP Ltd
*/

var gfk_debug = 0;

// this is the name of the cookie that will drop to stop people getting multiple invites

var gfk_survey_cookie = "gfk_j437834_cookie";

if (gfk_debug == 1) { gfk_erase_cookie(gfk_survey_cookie) }

// The incidence rate... 100 = everyone, -1 = no one

var gfk_INCIDENCE = 10;

// survey is embedded in page so we need to check to make sure we can serve it
var gfk_e = document.createElement("iframe");


try { gfk_x = gfk_e.nodeName }
catch (err) { gfk_INCIDENCE = -1 }

if (!gfk_x.match(/^IFRAME$/i)) { gfk_INCIDENCE = -1 }


// --

var gfk_theDate = new Date();
gfk_month = gfk_theDate.getMonth();
gfk_month = gfk_month + 12;
gfk_theDate.setMonth(gfk_month);   // this is all for the cookie expiry

var gfk_colorscheme = "#ACACAC"; // for border and link colour

var gfk_d = document.createElement("div"); // create the invitation layer


// now style it

gfk_d.style.borderStyle = "solid";
gfk_d.style.borderColor = gfk_colorscheme;
gfk_d.style.borderWidth = "2px";
gfk_d.style.width = "525";
gfk_height = 375;
if (navigator.userAgent.match(/chrome|safari/i)) { // chrome and safari pad things a bit more
    gfk_height = 400;
}
gfk_d.style.fontFamily = "arial";
gfk_d.style.fontSize = "12";
gfk_d.style.textAlign = "center";
gfk_d.style.zIndex = "100";
gfk_d.style.padding = "0";

gfk_d.style.position = "absolute";
gfk_d.style.left = (document.body.clientWidth / 2) -(525 / 2) + "px";
gfk_d.style.top = "150px";
gfk_d.style.backgroundColor = "#ffffff";

// and fill it in


gfk_d.innerHTML = "<div style=\"overflow: none; height: 15px; cursor: pointer; background: " + gfk_colorscheme+ ";text-align: right; color: #fff;font-family:arial,sans-serif;font-size: smaller; font-weight:bold; padding-right: 3px;\" onclick=\"gfk_close_popup();\">X</div><iframe src=\"http://fast.surveys.com/projects/j437834/websurvey.asp?id=x\" height=\"" +  gfk_height + "\" width=\"525\" name=\"surveyframe\" frameborder=\"0\" scrolling=\"no\"></iframe>";



// check against incidence rate (100=everyone, -1 = no one)

if (gfk_incidence_rate(gfk_INCIDENCE)) {

    // only serve if they haven't got a cookie

    if (!gfk_get_cookie(gfk_survey_cookie)) {

        gfk_set_cookie(gfk_survey_cookie, "1");
        window.setTimeout("document.body.appendChild(gfk_d)", 1500); // pop up after 1.5 secs

    }

}


function gfk_close_popup() {
       gfk_d.style.display = "none";
       gfk_d.innerHTML = "";

}




// Functions to set, get and delete cookies
// First, to set a cookie - set a 3 month expiry date. Year is handled automatically if the month spills over



function gfk_set_cookie(gfk_cookiename, value) {


    document.cookie = gfk_cookiename + "=" + value + ";path=/" + ";expires=" + gfk_theDate.toGMTString();
}

function gfk_get_cookie(gfk_cookiename) {

    // document.cookie gives us a list of all the cookies from this domain
    // we need to find just the cookie we are interested in
    // take a substring between "cookiename=" and ";" - this is the value of the cookie
    // this function will return either the value of the cookie, or a null value if it's not there

    var gfk_nameStr = gfk_cookiename + "=";
    var gfk_maxLen = document.cookie.length;
    var gfk_i = 0;
    while (gfk_i < gfk_maxLen) {
        var gfk_j = gfk_i + gfk_nameStr.length;
        if (document.cookie.substring(gfk_i, gfk_j) == gfk_nameStr) {
            var gfk_cookieEnd = document.cookie.indexOf(";", gfk_j);

            if (gfk_cookieEnd == -1) {
                gfk_cookieEnd = document.cookie.length;
            }

            return unescape(document.cookie.substring(gfk_j, gfk_cookieEnd));
        }
        gfk_i++;
    }
    return "";
}

// simply erase the cookie by making it expire before now

function gfk_erase_cookie(gfk_cookiename) {
    document.cookie = gfk_cookiename + "=0;expires=Sun, 26 Mar-2000 12:00:00 GMT;path=/";
    //alert("Cookie erased");
}

// do they accept cookies?
// simply drop a test cookie and try to retrieve it. if it's not there, they don't take cookies

function check_cookie_accept() {
    gfk_set_cookie('testaccept', '1');
    var gfk_ck = gfk_get_cookie('testaccept');

    gfk_erase_cookie('testaccept');
    if (gfk_ck == 1) {
        //alert('This browser accepts cookies');
        return 1;
    }
    else {
        //alert('This browser is not cookie capable');
        return 0;
    }
    // erase it for good measure


}




function gfk_incidence_rate(gfk_incidence) {

    var gfk_percentage = gfk_incidence;
    var gfk_randNum = Math.round(Math.random() * 100);


    if (gfk_randNum <= gfk_percentage) {
        return 1; // success
    }
    else {
        return 0; // failed

    }
}