/**
 * @author pversai
 */

if(typeof RDCOM == "undefined"){
    /**    
     * The RDCOM global namespace object
     * @class RDCOM
     * @static
     */
    var RDCOM = {};
};

if(typeof RDCOM.utils == "undefined"){
    RDCOM.utils = {};
};

RDCOM.utils.ExitPopup = function(){
    this.exitStatus = true;
    var libs = RDCOM.utils;
    window.exitpopup = this;
    this.fixExternalLinks();
    libs.addEvent(window, "submit", this.setExitStatus, false);
    window.onbeforeunload = this.exit;
}
RDCOM.utils.ExitPopup.prototype = {
    popupURL:String(),
    exit:function(){
        if (window.exitpopup.getExitStatus()) {
                //Displays the Offer
                var win = window.open(window.exitpopup.popupURL, "ExitPop", "menubar=no, status=no, scrollbars=auto, menubar=no, width="+window.exitpopup.width+", height="+window.exitpopup.height);
        }
    },
    fixExternalLinks: function(e){
        var libs = RDCOM.utils;
        var links = document.getElementsByTagName("a");
        for (var i = 0; i < links.length; i++) {
            if (links.item(i).href.substr(1) != "#") {
                var url = new libs.URL(links.item(i).href);
                if (url.getHost() == window.location.hostname) {
                    libs.addEvent(links.item(i), "click", window.exitpopup.setExitStatus, false);
                }
            }
        }
    },
    getExitStatus:function(){
        return window.exitpopup.exitStatus;
    },
    setExitStatus:function(e){
        window.exitpopup.exitStatus = false;
    },
    setPopupURL:function(url){
        if (typeof window.exitpopup != "undefined") {
            window.exitpopup.popupURL = url;
        }
    },
    setWidth:function(width){
        window.exitpopup.width = width;
    },
    setHeight:function(height){
        window.exitpopup.height = height;
    }
}
// getElementById shortcut
function $f(idName)  {return document.getElementById(idName); }
// getElementsByTagName shortcut
function $t(tagName) {return document.getElementsByTagName(tagName); }
// createElement shortcut
function $c(elName)  {return document.createElement(elName); }