function CUtils()
{
}



CUtils.addEventHandler = function(obj, etype, fp, cap) 
{
	cap = cap || false;
	if (obj.addEventListener) obj.addEventListener(etype, fp, cap);
	else if (obj.attachEvent) obj.attachEvent("on" + etype, fp);
};


CUtils.removeEventHandler = function(obj, etype, fp, cap) 
{
	cap = cap || false;
	if (obj.removeEventListener) obj.removeEventListener(etype, fp, cap);
	else if (obj.detachEvent) obj.detachEvent("on" + etype, fp);
};


CUtils.loadScript = function(url)
{
	var head = document.getElementsByTagName("head")[0];
	var s = document.createElement("script");
	
	s.type = "text/javascript";
	s.src = url;

	head.appendChild(s);
};


CUtils.jsonpList = {};

CUtils.getJSONP = function(url, params, fncb)
{
	var querydata = "";
	var sep = "";
	var jsonptag = "t" + new Date().getTime();
	
	params = params || {};	
	params.jsonptag = jsonptag;
	
	for (var key in params)
	{
		var val = params[key];
		
		if (val != null && val != "")
		{
			querydata += sep;
			querydata += encodeURIComponent(key);
			querydata += "=";
			querydata += encodeURIComponent(val);
			sep = "&";
		}
	}

	if (url[url.length - 1] != '&')
	{
		url += url.indexOf("?") < 0 ? "?" : "&";
	}
	url += querydata;
	
	var head = document.getElementsByTagName("head")[0];
	var s = document.createElement("script");
	
	s.type = "text/javascript";
	s.src = url;

	var jsonp = {
		script : s,
		fncb : fncb
	};
	
	CUtils.jsonpList[jsonptag] = jsonp;


	head.appendChild(s);
};


CUtils.jsonpCallback = function(jsonptag, data)
{
	var jsonp = CUtils.jsonpList[jsonptag];
	
	jsonp.fncb(data);
	
	jsonp.script.parentNode.removeChild(jsonp.script);;
	
	jsonp.script = null;
	jsonp.fncb = null;

	CUtils.jsonpList[jsonptag] = null;
};





CUtils.fillElement = function(e, obj)
{
	if (typeof(e) == "string")  e = document.getElementById(e);
	
	if (!e.fill_template)
	{
		e.fill_template = e.innerHTML;
	}
	
	e.innerHTML = e.fill_template.replace(/{(.*?)}/g, 
		function(text)
		{
			var p = text.substring(1, text.length - 1);
			return obj[p];
		}
	);
};


CUtils.getMousePos = function(ev)
{
	var p = {};

	if (ev.pageX || ev.pageY) {
		p.x = ev.pageX;
		p.y = ev.pageY;
	}
	else if (ev.clientX || ev.clientY) {
		p.x = ev.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		p.y = ev.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	
	return p;
};


CUtils.getURLParam = function(name)
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	
	if( results == null )
		return "";

	return results[1];
};


CUtils.nl2br = function(s)
{
	return s ? s.replace(/\n/g, "<br />") : "";
};


String.prototype.trim = function(s)
{
	return this.replace(/^\s*/, "").replace(/\s*$/, "")
};


String.prototype.append = function(str, sep)
{
	var ret = this;

	if (str && str != "")
	{
		if (ret != "") ret += sep + " ";
		
		ret += str;
		
		return ret;
	}
	
	return ret;
};


String.prototype.phone = function()
{
	if (this == "") return "";

	var ret = "";
	
	var cpos = 0;
	
	if (this.substr(0, 3) == "021")
	{
		ret += "021.";
		cpos = 3;
	}
	else if (this.substr(0, 2) == "00")
	{
		cpos = this.indexOf("0", 2) + 1;
		ret += this.substr(0, cpos) + ".";
	}
	else
	{
		ret += this.substr(0, 4) + ".";
		cpos = 4;
	}
	
	ret += this.substr(cpos, 3) + ".";
	ret += this.substr(cpos + 3, 3);
	
	if (this.length > cpos + 6)
	{
		ret += ".";
		ret += this.substr(cpos + 6);
	}
	
	return ret;
};


Date.prototype.toSqlString = function()
{
	return this.getFullYear() + "-" + (this.getMonth() + 1) + "-" + this.getDate();
};


CUtils.dateAddDays = function(d, days)
{
	d = d || new Date();
	
	d.setDate(d.getDate() + days);
	
	return d;
};


CUtils.dateAddMonths = function(d, mon)
{
	d = d || new Date();
	
	d.setMonth(d.getMonth() + mon);
	
	return d;
};


CUtils.date2str = function(s)
{
	var a = s.split("-");
	var d = new Date();
	
	if (a.length != 3) return s;
	
	return a[2] + "/" + a[1] + "/" + a[0];
};


CUtils.removeDiacritics = function(s)
{
	var r = "";
	var c2;

	for (var i = 0, len = s.length; i < len; i++)
	{
		c2 = s.charCodeAt(i);

		if (c2 == 354 || c2 == 355) c2 = 116; // T, t,
		if (c2 == 350 || c2 == 351) c2 = 115; // S, s,
		if (c2 == 206 || c2 == 238) c2 = 105; // I^ i^

		if (c2 == 226 || c2 == 194 || c2 == 258 || c2 == 259) c2 = 97; // a~ a^
		
		r += String.fromCharCode(c2);
	}
	
	return r;
};



CUtils.showSystemError = function(msg)
{
	var ctlfg = document.getElementById("ctl_system_error_fg");
	var ctlbg = document.getElementById("ctl_system_error_bg");

	if (ctlfg == null)
	{
		ctlbg = document.createElement("div");
		ctlbg.id = "ctl_system_error_bg";
		document.body.appendChild(ctlbg);

		ctlfg = document.createElement("div");
		ctlfg.id = "ctl_system_error_fg";
		document.body.appendChild(ctlfg);
	}

	var mmm = msg.replace(/\r\n/g, "<br />").replace(/\n/g, "<br />").replace(/\t/g, "&nbsp;&nbsp;&nbsp;&nbsp;");

	ctlfg.innerHTML = 
"<table style='width:100%; height:100%'><tr><td></td>" +
"<td style='width:400px'>" +
"<div class='moduletable moduletable_add'>" + 
	"<div class='bt_error_msg bt_bold' style='text-align:center'>EROARE DE SISTEM</div>  <br /><br />" +
	"<div style='text-align:center'>Daca eroarea persista va rugam sa contactati administratorul.</div>  <br /><br /><br />" + 
	"<b>Detalii tehnice</b>  <br />" +
	"<div>" + mmm + "</div>  <br /><br />" +
	"<div style='text-align:center'>" + 
		"<input type='button' class='button' value='OK' style='width:80px' onclick='CUtils.hideSystemError();' />" +
	"</div>" +
"</div>" +
"</td>" +
"<td></td></tr></table>";
	
	ctlfg.style.display = "";
	ctlbg.style.display = "";

	return [];
};


CUtils.hideSystemError = function()
{
	var ctlfg = document.getElementById("ctl_system_error_fg");
	var ctlbg = document.getElementById("ctl_system_error_bg");

	ctlfg.style.display = "none";
	ctlbg.style.display = "none";
};


CUtils.getIncidentInfoHTML = function(icount, long_text)
{
	var s1 = long_text == true ? " incident" : "";
	var sn = long_text == true ? " incidente" : "";

	if (icount == 0)
	{
		return "<span style='color:green'>0" + sn + "</span>";
	}
	
	if (icount == 1)
	{
		return "<span style='color:orange'>" + icount + s1 + "</span>";
	}
	
	if (icount < 5)
	{
		return "<span style='color:orange'>" + icount + sn + "</span>";
	}

	return "<span style='color:red'>" + icount + sn + "</span>";
};


CUtils.log = function(msg, obj)
{
	var elog = document.getElementById("__ajspxlog");
	
	if (elog == null)
	{
		elog = document.createElement("textarea");
		elog.id = "__ajspxlog";
		elog.rows = "20";
		elog.style.width = "100%";
		
		document.body.appendChild(elog);
	}
	
	elog.style.display = "";
	
	elog.value += msg + "\n";
	
	if (obj)
	{
		if (typeof(obj) == "object")
		{
			for (var p in obj)
			{
				elog.value += "   " + p + " = " + obj[p] + "\n";
			}
		}
		else
		{
			elog.value += obj.toString() + "\n";
		}
	}
};
