// functions 1.6
// requires dom 3.3
//
//  1.6 some legacy functions added
//  1.5 object log separated
//  1.4 added functions isSet and isDefined
// 	1.3	exception handling wrapped to object xcp
// 	1.2	object browser added - it detects the browser version
// 	1.1	method String.prototype.removeHtmlEntities enhanced
// 		method String.prototype.removeInlineHtml added
// 		functions winH and winW removed (moved to dhtml)
// 	1.0	nbsp character definition added
// 		function isDefined added
// 		function isEmpty(variable) added
// 		function isSet(variable) added

var nbsp = String.fromCharCode(160); // non-breaking space

// skip leading whitespace
String.prototype.ltrim = function() {return this.replace(/^\s*(.*)/, "$1");}

// skip trailing whitespace
// String.prototype.rtrim = function() {return(this.replace(/(.*?)\s*$/, "$1"));}
String.prototype.rtrim = function() {return this.replace(/\s*$/, "");}

// skip leading and trailing whitespace
// String.prototype.trim = function() {return(this.replace(/\s*(.*?)\s*$/, "$1"));}
String.prototype.trim = function() {return this.ltrim().rtrim();}

// test if the string is empty
// String.prototype.isEmpty = function() {return (this.search(/\S/) < 0) ? true : false;}
String.prototype.isEmpty = function() {return (this.search(/\S/) < 0) ? true : false;}

// test if string is empty HTML
String.prototype.isBlank = function() {
	// by fczbkk
	var str = this;
	var blankCharacters = [" ", "&nbsp;", "&#032;"];
	for (var i = 0; i < blankCharacters.length; i++)
		str = str.replace(eval("/" + blankCharacters[i] + "/gi"), "");
	return (str.isEmpty());
}

// replaces HTML entities with normal characters
String.prototype.removeHtmlEntities = function() {
	var str = this;
	str = str.replace(/&nbsp;/g," ");
	str = str.replace(/&amp;/g,"&");
	str = str.replace(/&hellip;/g,"...");
	str = str.replace(/&quot;/g,'"');
	str = str.replace(/&lt;/g,"<");
	str = str.replace(/&gt;/g,">");
	return str;
}

String.prototype.removeInlineHtml = function() {
	var str = this;
	str = str.replace(/[<]{1}[^<>]*[>]{1}/g, ""); // remove all tags
	str = str.replace(/[ ]{2}/g, " "); // remove multiple spaces
	str = str.removeHtmlEntities(); // translate entities
	return str;
}

//	Split string into 2 substrings by delimiter
String.prototype.split2 = function(delimiter) {
	var text = this;
	var result = new Array();
	var index = text.indexOf(delimiter);
	if (index < 0) {
		result[0] = text;
	} else {
		result[0] = text.substring(0, index);
		result[1] = text.substring(index + 1);
	}
	return result;
}




// logging
var log = {
	enabled:      true,  // if true, logging is on
	initialized:  false, // if true, log is prepared to work
	messagesId:   "messages",
	messages:     null,
	messagesList: null,
	
	init: function(messagesId) {
		if (typeof messagesId != "undefined")
			log.messagesId = messagesId;
		log.messages = elm.get(log.messagesId);
		if (log.messages == null)
			return false; // init failed
		log.messagesList = document.createElement("ul");
		log.messages.appendChild(log.messagesList);
		log.initialized = true; // init succeded
		return true;
	},
	
	write: function(msgText, msgType) {
		var i;
		if (!log.enabled) return true;
		if (log.messagesList == null) return false; // error - list not initialized properly
		var li = document.createElement("li");
		var msgLines = msgText.split("\n");
		var br = null;
		var msg = null;
		for (i = 0; i < msgLines.length; i++) {
			msg = document.createTextNode(msgLines[i]);
			br = document.createElement("br")
			li.appendChild(msg);
			li.appendChild(br);
		}
		if (log.messagesList.firstChild)
			log.messagesList.insertBefore(li, log.messagesList.firstChild);
		else
			log.messagesList.appendChild(li);
		return true;
	}
}




// exception handling
var xcp = {
	warnings: false, // if true, warning messages are on
	errors:   true, // if true, error messages are on
	_log:     false, // true if log is on - private, do not change
	init: function() {
		if (log.initialized)
			xcp._log = true;
		return true;
	},
	
	warn: function(ex, location) { // non-critical exception handler
		if (xcp.warnings) {
			var result = "Warning"+"\n";
			result = "JavaScript exception: " + ex.name+"\n";
			result += "Warning message: " + ex.message + "\n";
			result += "Warning location: " + location + "\n";
			if (xcp._log)
				log.write(result, "warning");
		}
		return true;
	},
	
	err: function(ex, location) { // critical exception handler
		if(xcp.errors) {
			var result = "Error"+"\n";
			result += "JavaScript exception: " + ex.name+"\n";
			result += "Error message: " + ex.message + "\n";
			result += "Error location: " + location + "\n";
			if (xcp._log)
				log.write(result, "error");
		}
		return true;
	}
}




var browser = {
	// this object serves for browser detection purposes
	// may NOT use any JS library, because it is initialized before page load
	gecko: false,
	opera: false,
	ie:    false,
	ie5:   false,
	ie55:  false,
	ie6:   false,
	
	init: function() {
		var appName = navigator.appName.toLowerCase();
		
		if(appName.indexOf('netscape') > -1 && document.getElementById && document.childNodes && !document.all)
			browser.gecko = true;
		
		if(appName.indexOf('opera') > -1 && document.getElementById && document.childNodes)
			browser.opera = true;
		
 		if (appName.indexOf('explorer') > -1 && document.getElementById && document.childNodes && !document.addEventListener)
			browser.ie = true;
		
		if(!browser.ie && !browser.ns && document.addEventListener)
			browser.opera = true;
		
		var userAgent = navigator.userAgent.toLowerCase();
		
		if (browser.ie) {
			if (userAgent.indexOf('5.0') > -1) browser.ie50 = true;
			else if (userAgent.indexOf('5.5') > -1) browser.ie55 = true;
			else browser.ie6 = true;
		}
		return true;
	}
}
browser.init(); // init immediately




var isDefined = function(variable) { // checks if variable is defined
	if (typeof variable == "undefined") return false;
	else return true;
}

var isSet = function(variable) { // checks if variable is defined
	if (!isDefined(variable)) return false;
	if (variable === null) return false;
	return true;
}

var replaceByImage = function(obj, src, alt) {
	// replaces object content by image
	obj.innerHTML = "<img src=\""+src+"\" alt=\""+alt+"\" />";
	return true;
}

// legacy functions
function setTarget(link) {
	if (link.href) {
		var domain = document.location.host;
		// Obsahuje-li domain port, odstranime ho
		if (domain) domain = (domain.indexOf(":")>-1)? domain.substr(0,domain.indexOf(":")): domain;
		var host = link.host;
		// Obsahuje-li host port, odstranime ho
		if(host) host = (host.indexOf(":")>-1)? host.substr(0,host.indexOf(":")): host;
		var protocol = link.protocol;
		if ((host != domain) && (host != ("www."+domain) && ("www."+host) != domain)&&((protocol == "http:") || (protocol == "https:") || (protocol == "ftp:"))) {
			// jestlize se domena dokumentu a odkazu neshoduji, otevirani nastavime do noveho okna
			link.target="_blank";
//			if (!cls.has(link,"noIcon")) addIcon(link,newWindowIcon,"after");
		}
		return true;
	}
	else return false;
}

function addContent(item, contentString, position) {
	try {
		var cnt = document.createTextNode(contentString);
		if (position == "before") item.insertBefore(cnt.cloneNode(true), item.firstChild);
		else item.appendChild(cnt.cloneNode(true));
		return true;
	} catch (ex) {
		alert("addContent error "+ex);
		return false;
	}
}

function addIcon(item, iconImage, position, iconTitle) {
	try {
		space = document.createTextNode(nbsp)
		icon = document.createElement("img");
		icon.src = iconImage;
		icon.alt = "*";
		if (typeof iconTitle != "undefined") {
			icon.title = iconTitle;
			icon.alt = iconTitle;
		};
		icon.className = "icon";
		if (position == "before") {
			item.insertBefore(space, item.firstChild);
			item.insertBefore(icon, item.firstChild);
		} else {
			item.appendChild(space);item.appendChild(icon);
		};
	} catch (ex) {}
	return true;
}

function openImageWindow(e){
	link = evt.getTarget(e);
	while (link.tagName.toLowerCase()!="a")
		link = link.parentNode;
	link.target = "_blank";
}

function getChildrenByTag(tagName, srcElm) {
	try{
		if(document.getElementsByTagName){
			tagName = tagName.toLowerCase();
			srcElm = (srcElm) ? srcElm : document;
			foundElements = [];
			if(srcElm.all) allElements = srcElm.all;	// IE hack
			else allElements = srcElm.getElementsByTagName(tagName);
			for(var i = 0; i < allElements.length; i++)
				if((tagName == "*") || (allElements[i].tagName.toLowerCase() == tagName.toLowerCase()))
						if(allElements[i].parentNode == srcElm)
							foundElements[foundElements.length] = allElements[i];
			return(foundElements);
		}
		else return([]);
	}
	catch(ex) {alert("getChildrenByTag error ("+ex.name+"): "+ex.message);return(false);}
}

