                         function setCookie(cookieName,cookieValue,seconds) {
    var today = new Date();
    var expire = new Date();
    if (seconds==null || seconds==0) seconds=60*60*24;
    expire.setTime(today.getTime() + (seconds * 1000));
    document.cookie = cookieName+"="+escape(cookieValue)
                + ";expires="+expire.toGMTString();
}

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    var value = unescape(dc.substring(begin + prefix.length, end));
    return unescape(value);
}

function popup(href, target, width, height) {
    /*  close any existing window
    if (typeof openWin != 'undefined') {
        if (openWin && !openWin.closed) {
            openWin.close();
        }
    }
    */
    openWin = window.open(href, target,
        'width=' + width + ',height=' + height);
    // this is for netscape 2 - may not need this :)
    if (openWin.opener == null) openWin.opener = self;
    if (window.focus) { openWin.focus(); }
    return false;
}

function popupplus(href, target, width, height, features) {
    openWin = window.open(href, target,
        'width=' + width + ',height=' + height + "," + features);
    // this is for netscape 2 - may not need this :)
    if (openWin.opener == null) openWin.opener = self;
    if (window.focus) { openWin.focus(); }
    return false;
}

function doOnLoadAjax() {  ajaxEngine.registerAjaxObject("ajaxErrorHandler", new AjaxErrorHandler());
    ajaxEngine.registerAjaxObject("ajaxErrorIgnore", new AjaxErrorIgnore());  ajaxEngine.registerAjaxObject("ajaxDivUpdater", new AjaxDivUpdater());

    ajaxEngine.registerAjaxObject("openEditSubscriptionNotificationLightbox", new openEditSubscriptionNotificationLightbox());
    ajaxEngine.registerAjaxObject("closeEditSubscriptionNotificationLightbox", new closeEditSubscriptionNotificationLightbox());  ajaxEngine.registerRequest("addSubscription", "/subscription/add.dos");  ajaxEngine.registerRequest("deleteSubscription", "/subscription/delete.dos");  ajaxEngine.registerRequest("editSubscriptionNotification", "/user/editSubscriptionNotification.dos");
    
    ajaxEngine.registerAjaxElement('subscribeButton');
    
    ajaxEngine.registerRequest("blockUser", "/user/block.dos");
    ajaxEngine.registerRequest("removeFriend", "/friend/remove.dos");
    ajaxEngine.registerRequest("addFriend", "/friend/request.dos");
    ajaxEngine.registerAjaxObject("blockButton", new AjaxDivUpdater());
    ajaxEngine.registerAjaxObject("friendRequestSent", new AjaxFriendRequestSent());  ajaxEngine.registerRequest("checkUsernameAjax", "/user/checkUsername.dos");
    ajaxEngine.registerAjaxObject("usernameAvailable", new usernameAvailable());

}
Event.observe(window, 'load', doOnLoadAjax);  function AjaxErrorHandler() {
    this.ajaxUpdate = function(ajaxResponse) {
        var pn = ajaxResponse.childNodes[0].nodeValue;
        alert(pn);
    }
}  function AjaxErrorIgnore() {
    this.ajaxUpdate = function(ajaxResponse) {}
} 
function AjaxDivUpdater() {
    this.ajaxUpdate = function(ajaxResponse) {
        nodes = ajaxResponse.childNodes;
        var id = ajaxResponse.getElementsByTagName('id');
        var html = ajaxResponse.getElementsByTagName('html');
        if (id != undefined) {
            $(id[0].firstChild.nodeValue).innerHTML =
                                    html[0].firstChild.nodeValue;
        }
    }
}

function openEditSubscriptionNotificationLightbox() {
    this.ajaxUpdate = function(ajaxResponse) {
        openLightBox('subscribeConfirmation');
        
        var fbC = $("fbChecking");
        if (fbC) {
            if (!FB) {
                fbC.hide();
                return;
            }
            checkFbEmailPermission();
        }
    }
}

function checkFbEmailPermission() {
    FB.Bootstrap.requireFeatures(["Connect"], function() {
        FB.Facebook.init("8f308e5e293a730f05c7b520c5f23eea", "/connect/xd_receiver.htm");
        FB.Connect.requireSession(function() {
            FB.Facebook.apiClient.users_hasAppPermission("email", 
                function(hasPerm)
            {
                if (hasPerm) {
                    $("fbChecking").hide();
                    $("subscriptionSettings").show();
                }
                else {
                    $("fbChecking").hide();
                    $("fbNoPermission").show();
                }
            });
        });
    });
}

function closeEditSubscriptionNotificationLightbox() {
    this.ajaxUpdate = function(ajaxResponse) {
        closeLightBox('subscribeConfirmation');
    }
}

function editSubscriptionNotification(formEl) {
    ajaxEngine.sendRequest('editSubscriptionNotification', {
        method : 'post', 
        parameters: $(formEl).serialize(true)
    });
}

function subscribe(otype, oid) {
    ajaxEngine.sendRequest('addSubscription', {
        method : 'post', 
        parameters: { 'type': otype, 'id': oid}
    });
}

function unsubscribe(otype, oid) {
    ajaxEngine.sendRequest('deleteSubscription', {
        method : 'post',
        parameters: { 'type': otype, 'id': oid}
    });
}  function blockUser(userId, elId, block) {
    ajaxEngine.sendRequest("blockUser", {
        method : "post",
        parameters : {
            "userId" : userId,
            "id" : elId,
            "block" : (block ? 1 : 0)
        }
    });
}

function makeFriend(userId, elId, remove) {
    if (remove == true) {  ajaxEngine.sendRequest("removeFriend", {
            method : "post",
            parameters : {
                "userId" : userId,
                "id" : elId
          }
        });
    } else {  ajaxEngine.sendRequest("addFriend", {
            method : "post",
            parameters : {
                "userId" : userId,
                "id" : elId
          }
        });
    }
}  function AjaxFriendRequestSent() {
    this.ajaxUpdate = function(ajaxResponse) {  alert("Friend request sent. When the request is accepted, you'll become friends.");
    }
}  if (!window.XMLHttpRequest) {
    Event.observe(window, 'load', function() {
        $$('input.button').each(
            function(e) {
                Event.observe(e, 'mouseover', function() {
                        Element.addClassName(e, 'buttonHover'); });
                Event.observe(e, 'mouseout', function() {
                        Element.removeClassName(e, 'buttonHover'); });
            });
        });
}  var isSafari3 = false; 
if (window.devicePixelRatio && window.getMatchedCSSRules && !window.Opera) {
    isSafari3 = !!window.getMatchedCSSRules(document.documentElement,'');
}
if (isSafari3) {
    Event.observe(window, 'load', function() {
        $$('input.button').each(
            function(e) {
                e.value=e.value.toUpperCase();
            });
        });
} 
function changeLocation(href) {
    // todo: would like to detect if we are in an iframe or not
    parent.window.location=href;
    return false;
}  function toggleInlineHelp(elem) {
    $(elem).toggle();
    return false;
}  function usernameAvailable() {
    this.ajaxUpdate = function(ajaxResponse) {
        var availableTags = ajaxResponse.getElementsByTagName('available');
        var isAvailable = (availableTags[0].firstChild.nodeValue == "0" ? false : true);  if (usernameAvailableCallBack) {
            usernameAvailableCallBack(isAvailable);
        }
    }
}

function checkUsernameAjax(name) {
    ajaxEngine.sendRequest('checkUsernameAjax', {
        method : 'post', 
        parameters: { 'username': name }
    });
}