/**
 * Copyright (C)SERENA Software,Inc. 1998-2005. All Rights Reserved.
 * 
 * This computer software is Licensed Material belonging to Serena Software.
 * It is considered a trade secret and not to be used or divulged by parties
 * who have not received written authorization from Serena Software.
 */

/**
 * JavaScript Window utility functions
 **/

_WindowUtil.prototype.constructor = _WindowUtil;

function _WindowUtil() {
    this.contextUrl = document.location.protocol + "//";
    this.contextUrl += document.location.hostname;
    if ( document.location.port != "") {
        this.contextUrl += ":" + document.location.port;
    }
    this.contextUrl += "/servlet";
    this.isIE = navigator.appName == "Microsoft Internet Explorer";
    
    this.PARAMETERS_MODALDIALOG = {
        height: 400,
        width: 400,
        showHelp: false,
        isDependent: false,
        showDirectories: false,
        showMenubar: false,
        isModal: true,
        showLocation: false,
        isResizable: true,
        showScrollbars: false,
        showStatusbar: false,
        showToolbar: false
        };
    
    this.PARAMETERS_WINDOW = {
        height: 600,
        width: 600,
        isDependent: false,
        showDirectories: false,
        showMenubar: false,
        isModal: false,
        showLocation: false,
        isResizable: true,
        showScrollbars: true,
        showStatusbar: false,
        showToolbar: false
        };
    
    this.PARAMETERS_PREVIEW = {
        height: 600,
        width: 800,
        isDependent: true,
        showDirectories: false,
        showMenubar: false,
        isModal: false,
        showLocation: true,
        isResizable: true,
        showScrollbars: true,
        showStatusbar: false,
        showToolbar: true
        };
    
    this.PARAMETERS_HELP = {
        height: 400,
        width: 650,
        name: "_blank",
        isDependent: false,
        showDirectories: false,
        showMenubar: false,
        isModal: false,
        showLocation: false,
        isResizable: true,
        showScrollbars: true,
        showStatusbar: false,
        showToolbar: true
        };
}

/**
 * Opens a browser windows with a path somewhere within this servlet context
 * and checks for popup blockers
 * @param contextPath A path in the servlet context, absolute to the context root
 * @param width Width of the window in pixels, units should not be specified
 * @param height Height of the window in pixels, units should not be specified
 * @param modal If true the window will be modal on IE, no effect on Firefox
 */
_WindowUtil.prototype.openContextWindow = function( contextPath, inputParameters) {
    // check for invalid arguments
    if ( !contextPath  ||  contextPath == 0) {
        throw new Error( res_invalid_argument + " WindowUtil.openContextWindow.contextPath=" + contextPath);
    }

    var url;
    if ( contextPath.charAt(0) == '/') {
        url = this.contextUrl + contextPath;
    }
    else {
        url = this.contextUrl + '/' + contextPath;
    }
    
    // open window
    return this.openWindow( url, inputParameters);
}


/**
 * Basic window open function, checks for popup blockers.
 */
_WindowUtil.prototype.openWindow = function( url, inputParameters) {
    // check for invalid arguments
    if ( !url  ||  url.length == 0) {
        throw new Error( res_invalid_argument + " WindowUtil.openWindow.url=" + url);
    }
    
    // generate parameters
    var isModal = inputParameters["isModal"];
    var params = new String();

    // sizing and positioning
    if ( inputParameters["width"]) {
        var width = inputParameters["width"];
        var widthInt = null;
        if ( isNaN( width)) {
            params += "width=" + width + ",";
            widthInt = parseInt( width);
        }
        else {
            params += "width=" + width + "px,";
            widthInt = width;
        }
        
        if ( isNaN( widthInt) == false  &&  screen.availWidth) {
            params += "left=" + ((screen.availWidth - widthInt)/2) + ",";
        }
    }
    if ( inputParameters["height"]) {
        var height = inputParameters["height"];
        var heightInt = null;
        if ( isNaN( height)) {
            params += "height=" + height + ",";
            heightInt = parseInt( height);
        }
        else {
            params += "height=" + height + "px,";
            heightInt = height;
        }
        
        if ( isNaN( heightInt) == false  &&  screen.availHeight) {
            params += "top=" + ((screen.availHeight - heightInt)/2) + ",";
        }
    }
    var name = inputParameters["name"] ? inputParameters["name"] : "_blank";

    // boolean value
    params += this.setParameter( "dependent", inputParameters["isDependent"], "yes");
    params += this.setParameter( "directories", inputParameters["showDirectories"], "no");
    params += this.setParameter( "help", inputParameters["showHelp"], "no");
    params += this.setParameter( "location", inputParameters["showLocation"], "no");
    params += this.setParameter( "menubar", inputParameters["showMenubar"], "no");
    params += this.setParameter( "modal", inputParameters["isModal"], "no");
    params += this.setParameter( "resizable", inputParameters["isResizable"], "yes");
    params += this.setParameter( "scrollbars", inputParameters["showScrollbars"], "yes");
    params += this.setParameter( "status", inputParameters["showStatusbar"], "no");
    params += this.setParameter( "toolbar", inputParameters["showToolbar"], "no");

    // other parameters
    if ( inputParameters["miscParameters"]) {
        params += inputParameters["miscParameters"];
    }

    //alert(url + "\n" + params);
    
    // launch the window
    var windowObject = null;
    var wasBlocked = false;
    try {
        if ( this.isIE  &&  isModal) {
            params = params.replace( "width=", "dialogWidth:");
            params = params.replace( "height=", "dialogHeight:");
            params = params.replace( "scrollbars=", "scroll:");
            params = params.replace( new RegExp( "=", "g"), ":");
            params = params.replace( new RegExp( ",", "g"), ";");
            var dialogArguments = inputParameters["dialogArguments"];
            if ( !dialogArguments) { 
                dialogArguments = { openerWindow: window.top};
            }
            else {
                dialogArguments["openerWindow"] = window.top;
            }
            //alert( url + "\n" + params);
            windowObject = window.showModalDialog( url, inputParameters["dialogArguments"], params);
        }
        else {
            windowObject = window.open( url, name, params);
            // test whether it was blocked
            if ( !windowObject  ||  typeof( windowObject) == "undefined") {
                wasBlocked = true;
            }
        }
    }
    catch ( error) {
        wasBlocked = true;
    }

    // tell the user it was blocked
    if ( wasBlocked) {
        alert( res_popup_blocker_on);
        return null;    
    }
    else if ( windowObject != null) {
        windowObject.opener = window.top;
    }
    
    // return the object
    return windowObject;
}



/**
 * Returns the opener of a window
 */
_WindowUtil.prototype.getOpener = function() {
    if ( window.opener) {
        // typical way of getting the opener
        return window.opener;
    }
    else {
        // M$ modal dialog way of getting the opener
        if ( window.dialogArguments  &&  window.dialogArguments["openerWindow"]) {
            return window.dialogArguments["openerWindow"];
        }
    }
    return null;
}

/**
 * Internal Helper method
 */
_WindowUtil.prototype.setParameter = function( parameterKey, inputParameter, defaultParameter) {
    if ( inputParameter != null) {
        return parameterKey + "=" + (inputParameter ? "yes":"no") + ",";
    }

    return parameterKey + "=" + defaultParameter + ",";
}

var WindowUtil = new _WindowUtil();
