/* $Id: Utils.js 84386 2009-10-01 11:47:48Z k.reimer $
 * Copyright (C) 2006 IP Labs GmbH <http://www.iplabs.de/>
 * All rights reserved.
 */
 
 /**
 * @fileoverview
 *
 * Contains the Utils class. Depends on the HTMLUtils class.
 *
 * @author Andreas Kornetka  <a.kornetka@iplabs.de>
 * @author Thorsten Schüller <t.schueller@iplabs.de>
 * @version $Revision: 84386 $
 */

/**
 * Static Utils class object.
 *
 * @class <p>This class provides some usefull methods for general purpose.</p>
 *
 * @constructor
 * @jscram.public class="Utils"
 */
var Utils = new Object();


/**
 * Hides an element (a box) per id.
 *
 * @param {String} el
 *            The id of the closing element. Inevitable parameter!
 */
Utils.closeById = function(el) {
	el = document.getElementById(el);
	if(el) {el.style.display = 'none';}
};

/**
 * Submit a form by its id.
 *
 * @param {String} form
 *            The id of the to be submitted form. Inevitable parameter! 
 * @param {String} btn
 *            The optional id of a button that has to be active in order to allow the form submit.
 */
Utils.submitById = function(form, btn) {
	var bT = btn ? false : true;
	form = document.getElementById(form);
	btn  = document.getElementById(btn);
	if(btn) {bT = HTMLUtils.hasClassName(btn, 'off') ? false : true;}
	if(form && bT) {form.submit();}
};

/**
 * Call a function (if a specified button is active).
 *
 * @param {String} func 
 *            The name of the function that should be called.
 * @param {String} btn
 *            The optional id of a button that has to be active in order to allow the function call.
 */
Utils.callById = function(func, btn) {
	var bT = btn ? false : true;
	btn  = document.getElementById(btn);
	if(btn) {bT = HTMLUtils.hasClassName(btn, 'off') ? false : true;}
	if(func && bT) {eval(func + '()');}
};

/**
 * Opens a javascript popup. Usage example:
 *
 * <a href="link.html" 
 *    onclick="return Utils.popup(this, -1, -1, 640, 480, 'scrollbars=no')">
 *   Click me
 * </a>
 * 
 * or:
 *
 * <li onclick="Utils.popup('link.html', -1, -1, 640, 480, 'scrollbars=no')">
 *   Click me
 * </a>
 *
 * @param {Object || String} obj
 *            Two posibilites: The A element the popup is connected to or
 *            the reference of the link itself.
 * @param {Integer} x
 *            The x position of the popup or -1 if it should be centered.
 * @param {Integer} y
 *            The y position of the popup or -1 if it should be centered.
 * @param {Integer} w
 *            The width of the popup.
 * @param {Integer} h
 *            The height of the popup.
 * @param {String} opts
 *            Additional window options (See window.open JavaScript command).
 * @return
 *            False for ready event propagation.
 */
Utils.popup = function(obj, x, y, w, h, opts) {
    var args;
    if(x < 0) {x = screen ? (screen.width - w) >> 1 : 0;}
    if(y < 0) {y = screen ? (screen.height - h) >> 1 : 0;}
    args = 'width=' + w + ',height=' + h + ',left=' + x + ',top=' + y;
    if(opts != '') {args = args + ',' + opts;}
	if(typeof obj == 'string') {open(obj, '_blank', args).focus();}
	else {
		open(obj.href, obj.target ? obj.target : '_blank', args).focus();	
    	return false;	
	}
};

/**
 * Check if current borswer is Safari.
 *
 * @return 
 *		      False if not Safari, otherwise true.
 */
Utils.isSafari = function() {
	var safari = typeof(navigator.vendor) != 'undefined' ? true : false;
	if(safari) {safari = navigator.vendor.indexOf('Apple') >= 0 ? true : false;}
	return safari;
};

/**
 * Check if current borswer is ie (with the useage of conditional compliation).
 *
 * @param {Integer} jscript
 *            Optional comparing jscript parameter
 * @return 
 *		      False if not IE, otherwise true or (when parameter not set) the jscript version number.
 */
Utils.isIE = function(jscript) {
 	var iE = false;
	/*@cc_on
    	@if(@_jscript) {
    		iE = @_jscript_version;
			return (!jscript ? iE : jscript <= iE);
    	}
	@end @*/
 	return false;
};


/**
 * Returns true if the browser is an Internet Explorer which needs an alpha
 * hack to display alpha transparent images. Returns false for IE 7 and all
 * other browsers.
 * 
 * @return {Boolean} If the browser needs an alpha hack
 */
Utils.needAlphaHack = function()
{
    /*@cc_on
          @if(@_jscript)
              return @_jscript_version < 5.7; 
          @end 
      @*/
    return false;
};

/**
 * Check if an specified ActiveX object is already installed
 *
 * @param {String} objName
 *            The ActiveX object. Inevitable parameter!
 * @return 
 *		      False if the desired ActiveX is not installed, otherwise true.
 */
Utils.checkActiveXObject = function(objName) {
	var obj;
	try {
        obj = new ActiveXObject(objName);
        obj = null;
		return true;
    }  catch(e) {return false;} 
    return false;
};

Utils.checkLocalAdminRights = function() {
    var localAdmin = IsLocalAdmin();
    return localAdmin;
};

/**
 * Align the popup box to the popup size if the height is different. 
 * This funtion need a content container div beetween the 
 * subbars for resizing.  
 *
 * @param {String} boxId
 *            The popup box id
 * @param {String} contentId
 *            The content id
 * @return 
 *		      False if the box can't resize, otherwise true
 */
Utils.alignPopupBoxSize = function(boxId, contentId)
{
    var innerHeight;
    var popupBox;
    var popupContent;
    var newContentDiv;
    var tmpContent;
    var diff;

    popupBox     = document.getElementById(boxId);
    popupContent = document.getElementById(contentId);

    // get the innerHeight  
    if (self.innerHeight) { 
        innerHeight = self.innerHeight; // all except Explorer
    } else if (document.documentElement && document.documentElement.clientHeight) { 
        innerHeight = document.documentElement.clientHeight; // Explorer 6 Strict Mode
    } else if (document.body) { 
        innerHeight = document.body.clientHeight; // other Explorers
    } else {
        return false;
    }
    
    diff  = popupBox.offsetHeight - popupContent.offsetHeight;
    
    if (popupContent.offsetHeight-1 < (innerHeight-diff)) // expand content
    {
        popupContent.style.height = (innerHeight-diff)+'px';
    }            
    else // reduce content and add a container div for scrolling
    {
        popupContent.style.position = "static";
        popupContent.style.overflow = "visible";

        newContentDiv = document.createElement("div");
        newContentDiv.style.width = "100%";
        popupContent.parentNode.insertBefore(newContentDiv, popupContent);
        
        tmpContent = popupContent.parentNode.removeChild(popupContent);
        newContentDiv.appendChild(tmpContent);
        newContentDiv.style.overflow = 'auto';
        diff    = popupBox.offsetHeight - newContentDiv.offsetHeight;
        newContentDiv.style.height = (innerHeight-diff)+'px';
    }        
    
    return true;
};


/**
 * A static method for workaround the EOLAS patch. Just pass the object
 * source code to this function and it writes it to the document. Only works
 * in IE and is only needed in IE.
 *
 * @param {String} output
 *            The object source to write to the document
 */
Utils.eolasWorkaround = function(output)
{
    document.write(output);
};