function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function getCookie(c_name) {
    if (document.cookie.length>0) {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1) {
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}

function checkCookie() {
    var cookie_vaue;
    var cookie_exists;
    
    cookie_value = getCookie("FW_newsletter");
        
    if (cookie_value != null && cookie_value != "") {
        cookie_exists = true;
        //alert("Cookie exists");
    }
    else {
        cookie_exists = false;
        //alert("Cookie doesn't exist");
    }
    return cookie_exists;
}

function getFileName() {
    //this gets the full url
    var url = document.location.href;
    //this removes the anchor at the end, if there is one
    url = url.substring(0, (url.indexOf("#") == -1) ? url.length : url.indexOf("#"));
    //this removes the query after the file name, if there is one
    url = url.substring(0, (url.indexOf("?") == -1) ? url.length : url.indexOf("?"));
    //this removes everything before the last slash in the path
    url = url.substring(url.lastIndexOf("/") + 1, url.length);
    //return
    return url;
}

function PopupNewsletterDiv() {
    if (checkCookie() == false && getFileName().toLowerCase() != "clear_newsletter_popup_cookie.aspx" && getFileName().toLowerCase() != "popup_newsletter_download.aspx" && getFileName().toLowerCase() != "fwks.aspx" && getFileName().toLowerCase().substr(0, 4) != "ctl_") {
        var how_many_seconds_before_we_launch_popup;
        
        how_many_seconds_before_we_launch_popup = 10;
        
        setTimeout("setCookie('FW_newsletter', 'true', 1)", (how_many_seconds_before_we_launch_popup * 1000));
        setTimeout("document.getElementById('FloatDivBackground').style.display = 'block'", (how_many_seconds_before_we_launch_popup * 1000));
        setTimeout("fade('FloatDivBackground', 0, 70, 3000)", (how_many_seconds_before_we_launch_popup * 1000));
        
        
        //Throw user back up to the top of the page where gray area is waiting for them
        setTimeout("window.location.href='#'", (how_many_seconds_before_we_launch_popup * 1000));
        
        //Hide scrollbars
        //setTimeout("document.documentElement.style.overflow = 'hidden'", (how_many_seconds_before_we_launch_popup * 1000));	 // firefox, chrome
        //setTimeout("document.body.scroll = 'no'", (how_many_seconds_before_we_launch_popup * 1000));	// ie only
    }
}

function setOpacity(eID, opacityLevel) {
    var eStyle = document.getElementById(eID).style;
    eStyle.opacity = opacityLevel / 100;
    eStyle.filter = 'alpha(opacity='+opacityLevel+')';
}

function fade(eID, startOpacity, stopOpacity, duration) {
    var speed = Math.round(duration / 1000);
    var timer = 0;
    
    if (startOpacity < stopOpacity){ // fade in
        for (var i=startOpacity; i<=stopOpacity; i++) {
            setTimeout("setOpacity('"+eID+"',"+i+")", timer * speed);
            timer++;
        }
        setTimeout("document.getElementById('FloatDivForeground').style.display='block'", 1000);
        return;
    }
    for (var i=startOpacity; i>=stopOpacity; i--) { // fade out
        setTimeout("setOpacity('"+eID+"',"+i+")", timer * speed);
        timer++;
    }
    setTimeout("document.getElementById('FloatDivForeground').style.display='block'", 1000);
}

function NoThanksOrClose() {
    document.getElementById('FloatDivBackground').style.display='none';
    document.getElementById('FloatDivForeground').style.display='none';
    
    //Show scrollbars
    //document.documentElement.style.overflow = "auto";	 // firefox, chrome
    //document.body.scroll = "auto";	// ie only
}

function DeletePopupNewsletterMessage(message_id) {
    NewsletterPopup.DeleteNewsletterPopupItem(message_id);
    $get("ctl00_ContentPlaceHolder1_AlertDiv").innerHTML = "The popup newsletter message has been deleted";
    $get("ctl00_ContentPlaceHolder1_AlertDiv").style.display = "block";
    NewsletterPopup.PopulateMessages(PopulateMessages_Results);
}

function PopulateMessages_Results(result) {
    $get("ctl00_ContentPlaceHolder1_MessagesDiv").innerHTML = result;
}

function DeletePopupNewsletterDocument(document_id) {
    NewsletterPopup.DeleteNewsletterPopupDocument(document_id);
    $get("ctl00_ContentPlaceHolder1_AlertDiv").innerHTML = "The popup newsletter document has been deleted";
    $get("ctl00_ContentPlaceHolder1_AlertDiv").style.display = "block";
    NewsletterPopup.PopulateDocuments(PopulateDocuments_Results);
}

function PopulateDocuments_Results(result) {
    $get("ctl00_ContentPlaceHolder1_DocumentsDiv").innerHTML = result;
}