﻿/**********ABA Research Popup*****************/
/* Copyright (c) abaresearch.co.uk           */
/* Please refer to our Terms & Conditions    */
/* for conditions of use.                    */
/* Do NOT modify this code unless directed   */
/* to do so by a member of ABA Research      */
/* staff.                                    */
/*********************************************/



/* Please set the following variables */
var fadeInterval_ABA = 100;  // milliseconds
var cookieExpiryDaysForPositiveFeedback_ABA = "60"; // number of days the cookie should last once visitor agrees to give feedback
var cookieExpiryDaysForNegativeFeedback_ABA = "7"; // number of days the cookie should last once visitor does not agree to give feedback
var timeToLoadPopUpAfter_ABA = "1000"; //milliseconds after page loaded
var timeToClosePopUpAfter_ABA = "20000"; //milliseconds before self-closing
var navigateURL_ABA = "http://www.abaresearch.co.uk/popupsurvey/orangesurvey.htm"; //survey URL
var popUpName_ABA = "ABA_17_12_09633966548573003750"; //unique name
var popUpStartDate_ABA = new Date("07/04/2010"); //start date (mm/dd/yyyy)
var popUpEndDate_ABA = new Date("07/05/2011"); //end date (mm/dd/yyyy)
var popUpIsLive_ABA = "false"; //flag to "turn off" the questionnaire
var popUpIsPopUnder_ABA = "true"; //pop-up vs pop-under
var ratioOfUsersToInvites_ABA = 10; //upper boundary of random number used to sample site visitors


/*  Please do not change these values */
var fadeOpacity_ABA = new Array();
var fadeTimer_ABA = new Array();
var cookieExpiryDateTime_ABA = new Date(); // Please do not set this. As this is set within the code
var cookieCount = 0; // Please do not change this value


// Create PopUpBody
createPopUpBody();

// Add PopUp Load method on Onload based on Browser & keep extrenal onload methods to avoid conflict
addOnloadEvent(loadPopUp_ABA);
function addOnloadEvent(fnc) {
    if (typeof window.addEventListener != "undefined") // W3C standard (Firefox)
        window.addEventListener("load", fnc, false);
    else if (typeof window.attachEvent != "undefined") // Microsoft (Internet Explorer)
    {
        window.attachEvent("onload", fnc);
    }
    else { // If used in external page, keep onload methods if there is already some.
        if (window.onload != null) {
            var oldOnload = window.onload;
            window.onload = function(e) {
                oldOnload(e); window[fnc]();
            };
        }
        else window.onload = fnc; // Finally call our method.
    }
}



function loadPopUp_ABA() {
    var overLayInvite;
    var today = new Date;
    if (popUpIsLive_ABA == "true") {
        // popUpStartDate_ABA & popUpEndDate_ABA is normally a day earlier and later to avoid time comparison
        if (today > popUpStartDate_ABA && today < popUpEndDate_ABA) {
            if (checkIfToDisplayPopUpBasedOnCookie_ABA()) {
                //see if the visitor got lucky
                var randomnumber = Math.floor(Math.random() * (ratioOfUsersToInvites_ABA + 1));
                if (randomnumber == 1 || ratioOfUsersToInvites_ABA == 0) {
                    // Clear all timeouts
                    clearTimeout(overLayInvite);
                    // Fade in after sprecified second
                    overLayInvite = setTimeout('fade_ABA("popupWindow_ABA", true);', timeToLoadPopUpAfter_ABA);
                    if (timeToClosePopUpAfter_ABA != "0") {
                        // fade out after specified second
                        overLayInvite = setTimeout('fade_ABA("popupWindow_ABA", false);', timeToClosePopUpAfter_ABA);
                    }
                }
            }
        }
    }
}

function createPopUpBody() {
    document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"http://www.abaresearch.co.uk/popupsurvey/client_output/" + popUpName_ABA + "_minified.css\" />");
    document.write("<div id=\"popupWindow_ABA\" class=\"popup_ABA\">" +
		"<table width=\"361px\" cellpadding=\"0\" cellspacing=\"0\"> " +
			"<tr>" +
				"<td class=\"popUpTitleTopLeft_ABA\">" +
					"&nbsp;" +
				"</td>" +
				"<td class=\"popUpTitle_ABA\">" +
					"Have your say" +
				"</td>" +
				"<td class=\"popUpTitleTopRight_ABA\" valign=\"middle\" align=\"center\">" +
					"<a href=\"#\" onclick=\"fade_ABA('popupWindow_ABA', false); SetCookie_ABA('" + popUpName_ABA + "','" + cookieCount + "','" + cookieExpiryDaysForNegativeFeedback_ABA + "'); return false;\">" +
						"<img src=\"http://www.abaresearch.co.uk/popupSurvey/images/icons/close.gif\" style=\"oultine: none;\" width=\"21\" height=\"21\" border=\"0\" title=\"close\" />" +
					"</a>" +
				"</td>" +
			"</tr>" +
			"<tr>" +
				"<td colspan=\"3\">" +
					"<div class=\"popupbody_ABA\">" +
						"<p class=\"popUpInvite_ABA\">" +
							"Please help us improve our website by" +
							"<br />" +
							"completing the short survey at the end of" +
							"<br />" +
							"your visit." +
						"</p>" +
						"<input type=\"button\" class=\"popUpButton_ABA\" value=\"No, thanks\" title=\"No, thanks\" onClick=\"fade_ABA('popupWindow_ABA',false);SetCookie_ABA('" + popUpName_ABA + "','" + cookieCount + "','" + cookieExpiryDaysForNegativeFeedback_ABA + "');\" />&nbsp;&nbsp;" +
						"<input type=\"button\" class=\"popUpButton_ABA\" value=\"Yes\" title=\"Yes\" onClick=\"fade_ABA('popupWindow_ABA',false);SetCookie_ABA('" + popUpName_ABA + "','" + cookieCount + "','" + cookieExpiryDaysForPositiveFeedback_ABA + "');javascript:window.open('" + navigateURL_ABA + "','" + popUpName_ABA + "','status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes', true);  if(popUpIsPopUnder_ABA== 'true') window.focus();\"/>" +
					"</div>" +
				"</td>" +
			"</tr>" +
			"<tr>" +
				"<td colspan=\"3\" class=\"popUpFooter_ABA\">" +
					"<a href=\"http://survey.abaresearch.co.uk/privacypolicy.aspx\" target=\"_blank\" class=\"popUpFooter_ABA\">" +
						"Privacy policy</a>&nbsp;© Copyright 2010" +
				"</td>" +
			"</tr>" +
		"</table>" +
	"</div>");
}


function escapeHTML(str) {
    var div = document.createElement("div");
    div.innerHTML = str;
    str = div.innerHTML;
    delete div;
    return str;
};

// GET AND SET THE VISUAL FADE IN AND FADE OUT OF THE INVITATION
// --------------------------------------------------------------------------
function fade_ABA(o, d) {
    // o - Object to fade in or out.
    // d - Display, true =  fade in, false = fade out
    if (o == null) {
        o = 'popupWindow_ABA';
    }
    if (d == null) {
        d = true;
    }
    var obj = document.getElementById(o);
    if ((fadeTimer_ABA[o]) || (d && obj.style.display != 'block') || (!d && obj.style.display == 'block')) {
        if (fadeTimer_ABA[o]) {
            clearInterval(fadeTimer_ABA[o]);
        }
        else {
            if (d) {
                fadeOpacity_ABA[o] = 0;
            }
            else {
                fadeOpacity_ABA[o] = 9;
            }
        }
        obj.style.opacity = "." + fadeOpacity_ABA[o].toString();
        obj.style.filter = "alpha(opacity=" + fadeOpacity_ABA[o].toString() + "0)";
        if (d) {
            obj.style.display = 'block';
            fadeTimer_ABA[o] = setInterval('fadeAnimation_ABA("' + o + '",1);', fadeInterval_ABA);
        }
        else {
            fadeTimer_ABA[o] = setInterval('fadeAnimation_ABA("' + o + '",-1);', fadeInterval_ABA);
        }
    }
}

function fadeAnimation_ABA(o, i) {

    // o - o - Object to fade in or out.
    // i - increment, 1 = Fade In

    var obj = document.getElementById(o);
    fadeOpacity_ABA[o] += i;
    obj.style.opacity = "." + fadeOpacity_ABA[o].toString();
    obj.style.filter = "alpha(opacity=" + fadeOpacity_ABA[o].toString() + "0)";

    if ((fadeOpacity_ABA[o] == '9') | (fadeOpacity_ABA[o] == '0')) {
        if (fadeOpacity_ABA[o] == '0') {
            obj.style.display = 'none';
        }
        else {
            obj.style.opacity = "1";
            obj.style.filter = "alpha(opacity=100)";
        }
        clearInterval(fadeTimer_ABA[o]);
        delete (fadeTimer_ABA[o]);
        delete (fadeTimer_ABA[o]);
        delete (fadeOpacity_ABA[o]);
    }
}

// GET AND SET COOKIE VALUES TO DETERMINE NUMBER OF TIMES TO DISPLAY
// --------------------------------------------------------------------------
function GetCookie_ABA(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return getCookieVal_ABA(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
}

function SetCookie_ABA(name, value) {

    var argv = SetCookie_ABA.arguments;
    var argc = SetCookie_ABA.arguments.length;
    var expires = (argc > 2) ? argv[2] : 0;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;


    // Detrmine cookie expiry date time
    cookieExpiryDateTime_ABA.setTime(cookieExpiryDateTime_ABA.getTime() + (expires * 24 * 60 * 60 * 1000));
    
    
    document.cookie = name + "=" + escape(value) +
    ((expires == null) ? "" : ("; expires=" + cookieExpiryDateTime_ABA.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}

// This is not called as it is expected that cookie would be automatically expired after time specified. 
function DeleteCookie_ABA(name) {
    var cookieExpiryDateTime_ABA = new Date();
    cookieExpiryDateTime_ABA.setTime(cookieExpiryDateTime_ABA.getTime() - 1);
    var cval = GetCookie_ABA(name);
    document.cookie = name + "=" + cval + "; expires=" + cookieExpiryDateTime_ABA.toGMTString();
}



function getCookieVal_ABA(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

function checkIfToDisplayPopUpBasedOnCookie_ABA() {
    var showPopUp;
    cookieCount = GetCookie_ABA(popUpName_ABA);
    if (cookieCount == null) {
        cookieCount = 1;
        showPopUp = true;        
    } else {
        cookieCount++;
        showPopUp = false;        
    }
    return showPopUp;
}
