
/**
* PTools System Klasse
* 
* @author PCSG - Henning
* @version 1.1
*/
// IE Flickering Bug
try {
	document.execCommand("BackgroundImageCache", false, true);
} catch (err)
{
	// Nothing to do
};

if (typeof _ptools == 'undefined') {
	var _ptools = {};
};


_ptools.System = function(settings)
{
	var t = this;
	t.settings = settings ? settings : {};

	var browser = navigator.userAgent.toLowerCase();
	t.isMSIE 	  = (navigator.appName == "Microsoft Internet Explorer");
	t.isMSIE5 	  = t.isMSIE && (navigator.userAgent.indexOf('MSIE 5') != -1);
	t.isMSIE5_0   = t.isMSIE && (navigator.userAgent.indexOf('MSIE 5.0') != -1);
	t.isMSIE6 	  = t.isMSIE && (navigator.userAgent.indexOf('MSIE 6') != -1);
	t.isMSIE7 	  = navigator.userAgent.indexOf('MSIE 7') != -1;
	t.isFirefox	  = browser.indexOf('firefox') != -1;
	t.isGecko 	  = browser.indexOf('gecko') != -1;
	t.isGecko18   = browser.indexOf('gecko') != -1 && browser.indexOf('rv:1.8') != -1;
	t.isSafari 	  = browser.indexOf("safari") != -1;
	t.isKonqueror = browser.indexOf("konqueror") != -1;
	t.isOpera 	  = browser.indexOf('opera') != -1;
	t.isMac 	  = browser.indexOf('mac') != -1;
	t.isNS7 	  = browser.indexOf('netscape/7') != -1;
	t.isNS71 	  = browser.indexOf('netscape/7.1') != -1;

	t.host = document.location.protocol+"//"+document.location.host;
	t.settings.host = t.host;

	t.setAttribute = function(name, value)
	{
		if (name == 'host') {
			return;
		};

		t.settings[name] = value;
	};

	t.getAttribute = function( name )
	{
		if (t.settings[name]) {
			return t.settings[name];
		};

		return false;
	};
};

// px löschen
_ptools.System.prototype.replaceEntity = function(ent)
{
	return parseInt(ent);
};
// Max Höhe vom Windows
_ptools.System.prototype.getMaxHeight = function()
{
	if (window.innerHeight) {
		return window.innerHeight;
	};
		
	if (document.body.clientHeight) {
		return document.body.clientHeight;
	};

	if (screen.height) {
		return screen.height;
	};
	
	return 768;
};
// Max Höhe vom Windows
_ptools.System.prototype.getMaxWidth = function()
{
	if (window.innerHeight) {
		return window.innerWidth;
	};
	
	if (document.body.clientWidth) {
		return document.body.clientWidth;
	};
	
	if (screen.width) {
		return screen.width;
	};
	
	return document.body.clientWidth;
};

_ptools.System.prototype.isInteger = function(s)
{
	return (s.toString().search(/^-?[0-9]+$/) == 0);
};

_ptools.System.prototype.hasScrollbar = function(oElm)
{
	if (oElm.clientHeight < oElm.scrollHeight) {
   		return true;
	};

	return false;
};


// Scroll Position herausfinden
_ptools.System.prototype.getScrollXY = function()
{
	var scrOfX = 0, scrOfY = 0;
	
	if (typeof(window.pageYOffset) == 'number')
	{
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if (document.body && (document.body.scrollLeft || document.body.scrollTop))
	{
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
	{
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	};
	
	return [ scrOfX, scrOfY ];
};

// UTF 8 Encode
_ptools.System.prototype.encode_utf8 = function(rohtext)
{
	// dient der Normalisierung des Zeilenumbruchs
	rohtext     = rohtext.replace(/\r\n/g,"\n");
	var utftext = "";
	
	for (var n=0; n<rohtext.length; n++)
	{
		// ermitteln des Unicodes des  aktuellen Zeichens
		var c=rohtext.charCodeAt(n);
		// alle Zeichen von 0-127 => 1byte
		if (c<128)
		{
			utftext += String.fromCharCode(c);
		} else if ((c>127) && (c<2048))
		{
			// alle Zeichen von 127 bis 2047 => 2byte
			utftext += String.fromCharCode((c>>6)|192);
			utftext += String.fromCharCode((c&63)|128);
		} else
		{
			// alle Zeichen von 2048 bis 66536 => 3byte
			utftext += String.fromCharCode((c>>12)|224);
			utftext += String.fromCharCode(((c>>6)&63)|128);
			utftext += String.fromCharCode((c&63)|128);
		};
	};
	
	return utftext;
};

// UTF 8 Decode
_ptools.System.prototype.decode_utf8 = function(utftext)
{
	var plaintext = ""; var i=0; var c=c1=c2=0;
	// while-Schleife, weil einige Zeichen uebersprungen werden
	while (i < utftext.length)
	{
		c = utftext.charCodeAt(i);
		if (c < 128)
		{
			plaintext += String.fromCharCode(c);
			i++;
		} else if ((c > 191) && (c < 224))
		{
			c2 = utftext.charCodeAt(i+1);
			plaintext += String.fromCharCode(((c&31)<<6) | (c2&63));
			i+=2;
		} else
		{
			c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2);
			plaintext += String.fromCharCode(((c&15)<<12) | ((c2&63)<<6) | (c3&63));
			i+=3;
		};
	};
	
	return plaintext;
};

// Value prüfen ob diese leer ist, wenn ja wird ein leerer String zurück gegeben
_ptools.System.prototype.checkValue = function( val )
{
	if (val && val != "") {
		return val;
	};

	return "";
};

// JavaScript Datei nachladen
_ptools.System.prototype.loadScript = function(__file__)
{
	var aScript = document.getElementsByTagName('script');
	var check = false;

	for(var i = 0, len = aScript.length; i < len; i++)
	{
		if (aScript[i].src == __file__ || aScript[i].src == this.host+__file__)
		{
			check = true;
			break;
		};
	};

	if (!check)
	{
		a = document.createElement('script');
		a.setAttribute('type','text/javascript');
		a.setAttribute('src',__file__);
		document.getElementsByTagName('head')[0].appendChild(a);
	};
};

// eine art var_dump - JavaScript Attribute eines Objektes
_ptools.System.prototype.getAttributesOfObj = function( obj )
{
	var str = '';
	
	for(i in obj) {
		str += i+'=>'+obj[i]+"\n";
	};

	return str;
};

_ptools.System.prototype.var_dump = function( obj )
{
	return this.getAttributesOfObj( obj );
};

// PHP URL Params bekommen
_ptools.System.prototype.getUrlParams = function( str )
{
	var str_ = str.split('?');
	
	if (!str_[1]){
		return false;
	};
	
	str_ = str_[1].split('&');
	var r = new Array();

	for(var i = 0, len = str_.length; i < len; i++)
	{
		sp = str_[i].split('=');
		r[sp[0]] = sp[1];
	};

	return r;
};

// Input Value bei Focus löschen und wieder herstellen
_ptools.System.prototype.FieldFocus = function( InputField, FValue, SValue )
{
	var InputField = document.getElementById ( InputField );
	var _value     = InputField.value;

	if (_value != '' && FValue != '  ') {
		SValue =_value;
	};

	if (InputField.value =='') {
		InputField.value = SValue;
	};

	if (InputField)
	{
		InputField.onfocus = function ()
		{
			if (InputField.value == SValue)
			{
				InputField.value = FValue;
			} else
			{
				InputField.select();
			};

		};

		InputField.onblur = function ()
		{
			if (InputField.value == FValue) {
				InputField.value = SValue;
			};
		};
	};
};

_ptools.System.prototype.in_array = function( item, arr )
{
	for (p = 0; p < arr.length; p++) 
	{
		if (item == arr[p]) {
			return true;
		};
	};
	
	return false;
};

_ptools.System.prototype.empty = function( v )
{
	if (v == '' ||
		v == 0 ||
		v == '0' ||
		v == null ||
		v == false)
	{
		return true;
	};

	return false;
};

_ptools.System.prototype.getElmPos = function(element)
{
	var elem = element, tagname="", x=0, y=0;

	if (typeof elem == 'undefined' || !elem || elem == null) {
		return {x:0,y:0};
	};

	while ((typeof elem == "object") && elem != null && (typeof elem.tagName != "undefined"))
	{
		y = y+elem.offsetTop;
		x = x+elem.offsetLeft;
		
		tagname = elem.tagName.toUpperCase();

		if (tagname=="BODY") {
			elem = 0;
		};
		
		if (typeof(elem)=="object")
		{
      		if (typeof(elem.offsetParent) == "object") {
      			elem = elem.offsetParent;
      		};
		};
	};

	return {x:x,y:y};
};

_ptools.System.prototype.resizeVar = function(var1, var2, max)
{
	if (var1 > max) 
	{
		resize_by_percent = (max * 100 )/ var1;
		var2 = Math.round((var2 * resize_by_percent)/100);
		var1 = max;
	};
	
	if (var2 > max) 
	{
		resize_by_percent = (max * 100 )/ var2;
		var1 = Math.round((var1 * resize_by_percent)/100);
		var2 = max;
	};

	return {
		var1 : var1,
		var2 : var2
	};
};

_ptools._System = new _ptools.System();

/**
 * Browser
 */

_ptools._Browser = 
{
	__constructor : function() 
	{
		var t = this;

		var browser = navigator.userAgent.toLowerCase();
		t.isMSIE 		= (navigator.appName == 'Microsoft Internet Explorer');
		t.isMSIE5 		= t.isMSIE && (navigator.userAgent.indexOf('MSIE 5') != -1);
		t.isMSIE5_0 	= t.isMSIE && (navigator.userAgent.indexOf('MSIE 5.0') != -1);
		t.isMSIE6 		= t.isMSIE && (navigator.userAgent.indexOf('MSIE 6') != -1);
		t.isMSIE7 		= navigator.userAgent.indexOf('MSIE 7') != -1;

		t.isFirefox		= browser.indexOf('firefox') != -1;
		t.isGecko 		= browser.indexOf('gecko') != -1;
		t.isGecko18 	= browser.indexOf('gecko') != -1 && browser.indexOf('rv:1.8') != -1;
		t.isSeamonkey	= browser.indexOf('seamonkey') != -1;

		t.isSafari 		= browser.indexOf('safari') != -1;
		t.isKonqueror 	= browser.indexOf('konqueror') != -1;
		t.isOpera 		= browser.indexOf('opera') != -1;
		t.isMac 		= browser.indexOf('mac') != -1;

		t.isNS7 		= browser.indexOf('netscape/7') != -1;
		t.isNS71 		= browser.indexOf('netscape/7.1') != -1;
		
		t.isIPod		= browser.indexOf('iPod') != -1;
		t.isIPhone		= browser.indexOf('iPhone') != -1;
	},
	
	get : function()
	{
		return navigator.userAgent.toLowerCase();
	}
};
_ptools._Browser.__constructor();

/**
 * Types
 */

_ptools.String = function(string)
{
	var t = this;
	var _string = null;
	var _isMSIE = false;

	t.__constructor = function( string )
	{
		if (_ptools._Browser.isMSIE || _ptools._Browser.isKonqueror) {
			_isMSIE = true;
		};

		if (_isMSIE)
		{
			_string = new Array();
			_string[_string.length] = string;
		} else
		{
			_string = string;
		};
	};

	t.push = function( string )
	{
		if (_isMSIE)
		{
			 _string[_string.length] = string;
		} else
		{
			_string += string;
		};
	};

	t.get = function()
	{
		if (_isMSIE) {
			return _string.join("");
		};
		
		return _string;
	};
	
	t.htmlDecode = function(entity)
	{
		var chars = {
			'&amp;' : '&',
			'&quot;' : '"',
			'&amp;'  : '&',
			'&lt;'   : '<',
			'&gt;'   : '>',
			'&nbsp;' : ' ',
			
			// ISO 8859-1 Symbols
			'&yen;'  : '¥',
			'&sect;' : '§',
			'&uml;'  : '¨',
			'&copy;' : '©',
			'&ordf;' : 'ª',
			'&not;'  : '¬',
			//'&shy;'  : '',­
			'&reg;'  : '®',
			'&macr;' : '¯',
			'&deg;'  : '°',
			'&sup2;' : '²',
			'&sup3;' : '³',
			'&para;' : '¶',
			'&sup1;' : '¹',
			'&ordm;' : 'º',
			'&cent;' : '¢',
			'&iexcl;'   : '¡',
			'&pound;'   : '£',
			'&curren;'  : '¤',
			'&brvbar;'  : '¦',
			'&acute;'   : '´',
			'&micro;'   : 'µ',
			'&laquo;'   : '«',
			'&plusmn;'  : '±',
			'&middot;'  : '·',
			'&cedil;'   : '¸',
			'&raquo;'   : '»',
			'&frac14;'  : '¼',
			'&frac12;'  : '½',
			'&frac34;'  : '¾',
			'&iquest;'  : '¿',
			'&times;'   : '×',
			'&divide;'  : '÷'
		};
		
		for (i in chars) {
			entity = eval('entity.replace(/'+ i +'/gi, chars[i])');
		};

		return entity;
	};

	t.__constructor( string );
};
