<!--
/**
 * @name - LightwareCommonFunctions.js
 * @author - Rene Stephan Dettelbacher
 * @create - 29.03.2005
 * @modify - 26.11.2006
 * @state - RELEASED
 *
 * Copyright (c) by Rene Stephan Dettelbacher, 2004 - 2007 all rights reserved
 */

var DEBUG=1;
/**
 * if DEBUG is set and true this function traces the given line
 * this function gets called from MYSCREEN-OBJECTS when there are errors
 *
 * @param string methodname - the method the error occured in
 * @param string errormessage - a message describing the error
 * @param mixed parameter - a parameter that failed the test, this parameter is optional
 */
function debug(methodname,errormessage,parameter){
	if (isset(DEBUG)&&DEBUG){
		trace('METHODNAME: '+methodname);
		trace('MESSAGE: '+errormessage);
		if (isset(parameter)) print_r(parameter);
		trace('');
	}
}
/**
 * prints all values of an object or an array
 *
 * @param mixed obj - the object or array that should be printed
 */
function print_r(obj){
	if(is_object(obj)||is_array(obj)){
		var output='';
		if (is_array(obj)) output='Array('+obj+'):\n';
		else output='Object('+obj+'):\n';
		for(var i in obj){
			//if (isset(i) && isset(obj[i])){
				output+='['+i+']='+obj[i]+'\n';
			//}
		}
		trace(output);
	}else trace(obj);
}
/**
 * traces a given value as one line of text
 *
 * @param mixed line - the line that should be traced
 */
function trace(line){
	//if (isset(MYSCREEN) && MYSCREEN!=top.MYSCREEN) return top.trace(line);
	var tracefield=document.getElementById('TRACEFIELD');
	if (!tracefield){
		var width=400;
		var height=300;
		var x=10;
		var y=10;
		var div=document.createElement('div');
		div.style.position='absolute';
		div.style.right=x+'px';
		div.style.bottom=y+'px';
		div.style.zIndex=50000;
		div.style.width=width+'px';
		div.style.height=height+'px';
		tracefield=document.createElement('textarea');
		tracefield.style.fontFamily='Courier';
		tracefield.id='TRACEFIELD';
		tracefield.style.width=width+'px';
		tracefield.style.height=height+'px';
		div.appendChild(tracefield);
		document.getElementsByTagName('body')[0].appendChild(div);
	}
	tracefield.value+=line+"\n";
	return true;
}
/**
 * searches testvar in arr and returns the index if it is found
 * otherwise null is returned
 *
 * @param mixed testvar - the var to be searched in the array
 * @param Array arr - the array to be searched
 * @return int or null, the index in the array where the given input occures
 *    null if the index could not be found
 */
function array_search(testvar,arr){
	for (var i in arr){
		if (testvar==arr[i]) return i;
	}
	return null;
}
// Array.copy() - Copy an array
if( typeof Array.prototype.copy==='undefined' ) {
 Array.prototype.copy = function() {
  var a = [], i = this.length;
  while( i-- ) {
   a[i] = typeof this[i].copy!=='undefined' ? this[i].copy() : this[i];
  }
  return a;
 };
}
/**
 * searches testvar in arr and returns true if found, otherwise false
 *
 * @param mixed testvar - the var to be searched in the array
 * @param Array arr - the array to be searched
 * @return bool
 */
function in_array(testvar,arr){
	for (var i in arr){
		if (testvar==arr[i]) return true;
	}
	return false;
}
/**
 * trims whitespace on both sides of a string
 *
 * @see ltrim(), rtrim()
 * @param string text - the text where whitespace on both sides should be cut
 * @param string - the trimmed text
 */
function trim(text){
	text=ltrim(text);
	text=rtrim(text);
	return text;
}
//trims whitespace on the left side of a string
/**
 * trims whitespace on the left side of a string
 *
 * @param string text - the text where whitespace on the left side should be cut
 * @param string - the trimmed text
 */
function ltrim(text){
	text=text.toString();
	return text.replace(/^\s+(.*)/,'$1');
}
/**
 * trims whitespace on the right side of a string
 *
 * @param string text - the text where whitespace on the right side should be cut
 * @param string - the trimmed text
 */
function rtrim(text){
	text=text.toString();
	return text.replace(/(.*?)\s+$/,'$1');
}
/**
 * tests if the given string is empty
 *
 * @see trim()
 * @param string text - the text to be tested
 * @return bool - true of the trimmed string is empty, otherwise false
 */
function empty(text){
	text=trim(text.toString());
	if (text=='') return true;
	return false;
}
/**
 * tests if a variable is set, note that if testvar is an int and NaN also false is returned
 *
 * @param mixed testvar - the variable that is tested
 * @return bool
 */
function isset(testvar){
	if (typeof testvar=='number' && testvar.toString()=='0') return true;
	if (typeof testvar=='undefined' || (typeof testvar=='number' && (testvar.toString()=='NaN')) || testvar==null) return false;
	else return true;
}
/**
 * tests if the variable is an array
 *
 * @param mixed testvar the parameter to be tested if it is an array
 * @return bool
 */
function is_array(testvar){
	if (!isset(testvar)) return false;
	if (typeof testvar=='object'&&method_exists(testvar,'sort') && (testvar.sort).toString().indexOf('[native code]')!=-1) return true;
	return false;
}
/**
 * tests if the variable is an object
 *
 * @param mixed testvar - the parameter to be tested if it is an object
 * @return bool
 */
function is_object(testvar){
	if (!isset(testvar)) return false;
	if (typeof testvar=='object'&&!is_array(testvar)) return true;
	return false;
}
/**
 * tests if the variable is a valid number
 *
 * @param mixed testvar - the parameter to be tested if it is a number
 * @return bool
 */
function is_int(testvar){
	if (typeof testvar=='number'&&testvar.toString()!='NaN') return true;
	return false;
}
/**
 * tests if the variable is a valid number and >= 0
 *
 * @param mixed testvar - the parameter to be tested if it is a number
 * @return bool
 */
function is_unsigned_int(testvar){
	if (is_int(testvar)&&testvar>=0) return true;
	return false;
}
/**
 * checks if a function exists
 *
 * @param string functionName - the name of the function to be checked
 * @return bool - true if the given function name is a callable function
 */
function function_exists(functionName){
	if (isset(functionName) && typeof functionName=='function') return true;
	return false;
}
/**
 * checks if a method exists
 *
 * @param Object obj - the object to be checked
 * @param string methofName - the name of the method
 * @return bool - true if the methodName is a callable method on that object
 */
function method_exists(obj,methodName){
	if (isset(obj) && isset(obj[methodName]) && function_exists(obj[methodName])) return true;
	return false;
}
function is_function(FunctionName){
	if (isset(FunctionName) && typeof FunctionName=='function') return true;
	return false;
}
/**
 * checks a object for a specific type
 *
 * @param object obj - the object to check
 * @param string type - the type as a string
 */
function is_subclass_of(obj,type){
	if (is_object(obj) && method_exists(obj,'GetTypes')){
		var types=obj.GetTypes();
		if (is_array(types) && in_array(type,types)){
			return true;
		}
	}
	return false;
}
/**
 * gets the current timestamp
 * => microseconds elapsed from 1970-01-01
 */
function time(){
	var d=new Date();
	return Date.parse(d)+d.getMilliseconds();
}
function dec2hex(decvalue){
	var hex=trim(''+decvalue);
	if (!hex.match(/^\d+$/)) hex=0;
	var hexvalues=new Array(0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f');
	var str1=hexvalues[(parseInt(hex/16))];
	var str2=hexvalues[(parseInt(hex%16))];
	hex=str1+''+str2;
	return hex;
}
//liefert den dezimalwert einer headezimalzahl - schafft bis zu 6 Stellen.
function hex2dec(hexvalue){
	hexvalue=hexvalue.toLowerCase();
	var decexp=Array(1,16,256,4096,65536,1048576);
	var decvalues=new Array(0,1,2,3,4,5,6,7,8,9);
	decvalues['a']=10;
	decvalues['b']=11;
	decvalues['c']=12;
	decvalues['d']=13;
	decvalues['e']=14;
	decvalues['f']=15;
	var zahl=0;
	if (hexvalue.length>6) hexvalue=hexvalue.substr(0,6);
	for (var i=0;i<hexvalue.length;i++){
		var index=hexvalue.length-i-1;
		zahl+=decexp[index]*decvalues[(hexvalue.substr(i,1))];
	}
	return zahl;
}
//-->