
var theCookie = {
    getValue:function(name){
        var aList = document.cookie.split(';');
        if(!aList.length)return "";
        for(var each = 0; each < aList.length; each++){ 
            var aPair = aList[each].split('=');
            if(aPair[0].replace(/ /g,"") == name)
                return typeof(unescape(aPair[1])) == "undefined" ? "" : unescape(aPair[1]);
        }
        return "";
    },
    setValue:function(name, value){
        var aDate = new Date();
        aDate.setMonth(aDate.getMonth() + 6);
        document.cookie = name + "=" + escape(value) + ";expires=" + aDate.toGMTString() + ";path=/;";
    }
}
function when(anObject, anEvent, takeAction){
    if(anObject.attachEvent)
        anObject.attachEvent("on" + anEvent, takeAction);
    else if(anObject.addEventListener)
        anObject.addEventListener(anEvent, takeAction, false);
    else anObject["on" + anEvent] = takeAction;
}
function doRandomSurvey(onEvent, withUrl, howRandom){
    //random guard clause, fails one out of howRandom times
    if(Math.floor(Math.random() * howRandom) != 0)return;
    when(window, onEvent, function(){
        var cookieName = "insario-popup";
        if(theCookie.getValue(cookieName) == "supress") return;
        window.open("/gcs/travel/us/resources/selectSurvey.aspx?surveyURL=" + encodeURI(withUrl), "survey", "height=450, width=640, scrollbars=yes, resizable=yes");
        window.focus();
        //supress future popups for this url
        theCookie.setValue(cookieName,"supress");
    });
}
