/*
 * MappaTo JavaScript Library v1.0.0
 * http://www.comune.torino.it/
 * Copyright (c) 2009 Comune di Torino
 * Module licensed under GNU Affero GPL licenses.
 * http://www.gnu.org/licenses/agpl.html
 * Date: 2009
 */
function Libs()
{
	this.debugObj = null;
	this.rowDebugCount = 0;
	/* Print in a debug box a input string. */
	this.printMsgBox = function(str)
	{
		if(this.isNull(this.debugObj))
			if(objLibs.isObj(objLabels))
				this.debugObj = document.getElementById(objLabels.debugBoxId);
		if(this.isObj(this.debugObj))
			this.debugObj.innerHTML += this.rowDebugCount++ + " - " + str + "<br />";
	};
	/* Null test */
	this.isNull = function(o)
	{
		if(o == null) return true;
		return false;
	};
	/* Empty test */
	this.isEmpty = function(o)
	{
		if(!o) return true;
		if(o == null) return true;
		if(typeof o == "string") 
			if(objLibs.trim(o) == "") 
				return true;
		if(typeof o == "numeric") 
			if(o == 0) 
				return true;
		return false;
	};
	/* Empty test */
	this.trim = function(str)
	{
		var retVal = str.replace(" ","");
		return retVal;
	};
	/* Numeric test */
	this.isNumeric = function(o)
	{
		if(!o) return false;
		if(o == null) return false;
		if(typeof o == "number") return true;
		if(typeof o == "string")
		{
			var n = o*1;
			if(typeof n == "number") return true;
		}
		return false;
	};
	/* String test */
	this.isString = function(o)
	{
		if(!o) return false;
		if(o == null) return false;
		if(typeof o == "string") return true;
		return false;
	};
	/* Object test */
	this.isObj = function(o)
	{
		if(!o) return false;
		if(o == null) return false;
		if(typeof o == "object") return true;
		return false;
	};
	/* Undefined test */
	this.isUndefined = function(o)
	{
		if(!o) return false;
		if(o == null) return false;
		if(typeof o == "undefined") return true;
		return false;
	};
	/* Function test */
	this.isFunction = function(f)
	{
		if(!f) return false;
		if(f == null) return false;
		if(typeof f == "function") return true;
		return false;
	};
	this.round = function(n,pos)
	{
		if(!n) return n;
		if(n == null) return n;
		if(typeof n != "numeric") return n;
		if(!pos) pos = 2;
		if(pos == null) pos = 2;
		if(typeof pos != "numeric") pos = 2;
		return (Math.round(n*10^pos))/10^pos;
	};
	this.includeDom = function(scriptFileName,funCall,params) 
	{
		var htmlDoc = document.getElementsByTagName('head').item(0);
		var js = document.createElement('script');
		js.setAttribute('language', 'javascript');
		js.setAttribute('type', 'text/javascript');
		js.setAttribute('src', scriptFileName);
		htmlDoc.appendChild(js);
		if(funCall.length > 0)
		{
			//objLibs.printMsgBox("funCall:"+funCall);
			setTimeout('objLibs.checkIncludeDom("'+funCall+'","'+params+'",5)',1000);
		}
	};
	this.checkIncludeDom = function(funCall,params,iter) 
	{
		//alert(eval('typeof '+isLoadVar+''));
		var isLoad = eval('(typeof '+funCall+' == "function")');
		if(isLoad)
			eval(funCall+'('+params+')');
		else
		if(iter>0)
   		setTimeout('objLibs.checkIncludeDom("'+funCall+'","'+params+'",'+(iter-1)+')',1000);
	};
	this.isCookie = function(name) 
	{
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return true;
		}
		return false;
	};
}

/* Initialize Lib sigleton */
function initLib()
{
	if (typeof oldOnLoadLib == 'function') oldOnLoadLib();
	objLibs = new Libs();
}

/* Var sigleton instance*/
var objLibs;

/* Event initialize */
var oldOnLoadLib;
if (typeof window.onload == 'function') 
	oldOnLoadLib = window.onload;
window.onload = initLib;
