/** $Id: scripts_app.js,v 1.1.2.3 2008/05/30 12:56:09 andree.wormuth Exp $ */

function openWin( url, width, height, windowName ) {
    openWin( url, width, height, "yes", windowName );
}

/**
	Opens a windows with specified parameters.
	
	@param url
	@param width
	@param height
	@param scrollbars yes, no. Default is yes.
	@param windowName
	*/
function openWin( url, width, height, scrollbars, windowName ) {
    if ( !windowName ) {
        windowName="newWindow";
    }
    var newWindow = open( url, windowName, "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars="+scrollbars+",resizable=yes,copyhistory=no,width=" + width + ",height=" + height );
    newWindow.focus();
}

function openHelpWin( urlAnker ) {
    helpWin = open( urlAnker, "helpWindow", "toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=600,height=500" );
    helpWin.focus();
}

function isNumber( text ) {
    if ( text.length < 1 ) {
    	return false;
   	}
    for ( var i=0; i < text.length; i++ ) {
    	var character = text.charAt( i );
        if ( ( character < '0' || character > '9' ) && character != '.' && character != ',' ) {
            return false;
        }
    }
    return true;
}

function isInteger( text ) {
    if ( text.length < 1 ) {
    	return false;
   	}
    for ( var i=0; i < text.length; i++ ) {
    	var character = text.charAt( i );
        if ( character < '0' || character > '9'  ) {
            return false;
        }
    }
    return true;
}

function setDocumentElementVisibility( thisDocument, name, visibility ) {
	if( thisDocument != null && thisDocument.getElementById( name ) != null ) {
		thisDocument.getElementById( name ).style.display = visibility;
	}
}

/**
	Opens a progress bar message in specified document's body.
	
	@param document The message will be centered in this document's body and its location will be reset, if url is specified.
	@param message Message to display above image
	@param image Image to display beneath message
	@param url new location of document. Leave empty if the location shall not be reset.
	*/
function showProgress( document, message, image, url ) {
    var progressDiv = document.createElement( "div" );
    var dropShadow = document.createElement( "div" );
    progressDiv.id = "ProgressWindow";
    progressDiv.className = "Progress";
    dropShadow.className = "DropShadow";
    dropShadow.id = "ProgressWindowShadow";
    
    var progressText = document.createElement( "p" );
    progressText.appendChild( document.createTextNode( message ) );
    progressDiv.appendChild( progressText );

    var progressImage = document.createElement( "img" );
    progressImage.className = "Progress";
    progressImage.src = image;

    progressDiv.appendChild( progressImage );

	// creates a temporary body element to write div to... gulp
	if( !document.body ) {
		document.write( '<body/>') ;
	}
    // IE only finds them this way:
    var styleWidth = getStyle( "DIV.Progress", "width" );	
    var	styleHeight = getStyle( "DIV.Progress", "height" );	
	
    if( typeof styleWidth == 'undefined' ) {
    	// Firefox only like this:
		styleWidth = getStyle( "div.Progress", "width" );
    }
    if( typeof styleHeight == 'undefined' ) {
    	// Firefox only like this:
		styleHeight = getStyle( "div.Progress", "height" );
    }
    
    var centeredLeft = Math.round( ( document.body.clientWidth - parseInt( styleWidth ) ) / 2 );
    var centeredTop = Math.round( ( document.body.clientHeight - parseInt( styleHeight ) ) / 2 );
    
    progressDiv.style.top = centeredTop + 'px';
    progressDiv.style.left = centeredLeft + 'px';
    dropShadow.style.top = centeredTop + 10 + 'px';
    dropShadow.style.left= centeredLeft + 10 + 'px';
    
    
    document.body.appendChild( dropShadow );
    document.body.appendChild( progressDiv );
    
	if( url ) {
	    document.location.href = url;
	}
}

function hideProgress( document ) {
	var progressWindow = document.getElementById( 'ProgressWindow' );
	if( progressWindow ) {
		progressWindow.style.display = 'none';
	}
	var shadow = document.getElementById( 'ProgressWindowShadow' );
	if( shadow ) {
		shadow.style.display = 'none';
    }
}

/**
	Gets the specified property of the specified style class, e.g. the "border" value of the "DIV.Message" class.
	
	@param styleClass style class to find
	@param property style property to get 
	*/
function getStyle( styleClass, property ) {
	var cssRules;
	if (document.all) {
		cssRules = 'rules';
	}
	else if (document.getElementById) {
		cssRules = 'cssRules';
	}
	for (var S = 0; S < document.styleSheets.length; S++) {
		for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
			var currentRule = document.styleSheets[S][cssRules][R];
			if (currentRule.selectorText == styleClass) {
				return document.styleSheets[S][cssRules][R].style[property];
			}
		}
	}
}

// layer breite
layerWidth='250';

// layer text farben
textColorTitle = '#ffffff';
textColorMain = '#000000';

var ns4 = (document.layers)? true:false;
var ns6 = (document.getElementById)? true:false;
var ie  = (document.all)? true:false;

/* ########################################################################## */

function showLayer(name){//layername
    if (ie) {document.all[''+name].style.visibility = "visible"}
    else if(ns4) {document.layers[''+name].visibility = "show";}
    else if(ns6) {document.getElementById(''+name).style.visibility = "visible";}
}

function hideLayer(name){
    if (ie) {document.all[''+name].style.visibility = "hidden"}
    else if(ns4) {document.layers[''+name].visibility = "hide"}
    else if(ns6) {document.getElementById(''+name).style.visibility = "hidden";}
}

function layerPos(name, l, t){//layername, leftpos, toppos
    if (ie){
        document.all[''+name].style.left = l;
        document.all[''+name].style.top = t;
    }else if(ns4){
        document.layers[''+name].left = l;
        document.layers[''+name].top = t;
    }else if(ns6){
        document.getElementById(''+name).style.left = l;
        document.getElementById(''+name).style.top = t;
    }
}

function setLayerPos(name, l, t){//layername, leftpos, toppos
    pageWidth=(!ns4)?document.body.offsetWidth-4:innerWidth;
    pageHeight=(!ns4)?document.body.offsetHeight-2:innerHeight;
    l = l - layerWidth / 2;
    if(l <= layerWidth / 2){
        l = 10;
    }else if(l + layerWidth / 2 + 150 >= pageWidth){
        l = pageWidth - layerWidth / 2 - 150;
    }
    if(ns4){
        lHeight = document.layers[''+name].clip.height;
    }else if(ie){
        lHeight = document.all[''+name].offsetHeight;
    }else if(ns6){
        lHeight = document.getElementById(''+name).offsetHeight;
    }
    if(t + lHeight + 10 >= pageHeight){
        t = t - lHeight - 10;
    }
    layerPos(name, l, t);
}

function layerSize(name, w, h){//layername, width, height
    if (ie){
        document.all[''+name].style.width = w;
        document.all[''+name].style.height = h;
    }else if(ns4){
        document.layers[''+name].clip.width = w;
        document.layers[''+name].clip.height = h;
    }else if(ns6){
        document.getElementById(''+name).style.width = w;
        document.getElementById(''+name).style.height = h;
    }
}

function reWriteLayer(name, content){//layername, layercontent
    if(ie){
        document.all[''+name].innerHTML = content;
    }else if(ns4){
        document.layers[''+name].document.write(content);
        document.layers[''+name].document.close();
    }else if(ns6){
        over = document.getElementById(''+name);
        over.innerHTML = content;
    }
}

function reWriteAndPositionLayer(id , content) {
    reWriteLayer(id , content);    
    setLayerPos(id, myX, myY);
}

/**
 * Determines if a reference is defined
 */
function referenceDefined(o) {return (typeof o!="undefined");};

/**
 * Checks whether an object has the specified style class.
 */
function hasStyleClass(o,name) {
	return new RegExp("(^|\\s)"+name+"(\\s|$)").test(o.className);
};

/**
 * Add a class to an object.
 */
function addStyleClass(o,name) {
	var c = o.className || "";
	if (referenceDefined(c) && !hasStyleClass(o,name)) {
		o.className += (c?" ":"") + name;
		//o.className = name + (c?" ":"") + o.className;
	}
};

/**
 * Remove a class from an object
 */
function removeStyleClass(o,name) {
	var c = o.className || "";
	o.className = c.replace(new RegExp("(^|\\s)"+name+"(\\s|$)"),"$1");
}

