// (c) Andrii Vasyliev
// Different functions

if (typeof(sol)=="undefined") var sol = new Object();
sol.dif = {
	badwords : '',

	urlEncode: function (src) {
		var noEncode = /^([a-zA-Z0-9\_\-\.])$/;
		var res = "";
		for (i = 0; i < src.length; i++) {
			c = src.charAt(i);
			res += noEncode.test(c) ? c : "%"+this.char2hex(src.charCodeAt(i));
		};
		return res;
	},

	char2hex: function (c) {
		return String.fromCharCode(this.i2hc(c/16),this.i2hc(c%16));
	},

	i2hc: function (i) {
		return i+(i<10 ? 48 : 55);
	},

	urlDecode: function (str) {
		var res = "";
		for (i = 0; i < str.length; i++) {
			code = str.charAt(i) == '%';
			res += code ? this.hex2char(str.charAt(i+1)+str.charAt(i+2)) : str.charAt(i);
			if (code) i += 2;
		};
		return res;
	},

	hex2char: function (str) {
		return String.fromCharCode(parseInt(str,16));
	},

	trim : function (str) {
		return str.replace(/^\s*|\s*$/g,"");
	},

	nl2br: function (str) {
		return str.replace("\n","<br>","g");
	},

	html_prepare: function (str) {
		return str.replace("&","&amp;","g").replace(">","&gt;","g").replace("<","&lt;","g").replace("\n","<br>","g");
	},

	checkURL: function (str,res) {
		if (typeof(res)=="undefined") res = "";
		return str.match(/^(http:\/\/)?([\da-z][\w\.-]*\.)+[a-z]{2,6}\/?(\?[\da-z\_\-\.=%&]*)?$/i) ? str : res;
	},

	checkEmail: function (str,res) {
		if (typeof(res)=="undefined") res = "";
		return str.match(/^\s*[\w\.-]+@([\da-z][\w\.-]*\.)+[a-z]{2,6}\s*$/i) ? str : res;
	},

	checkText: function (str) {
		var res = "";
		for (i=0; i<str.length; i++) {
			c = str.charCodeAt(i);
			if (c>8 && c<127) res += String.fromCharCode(c);
		};
		return res;
	},

	checkTitle: function (str) {
		return str.replace(/[^a-z0-9 -]/ig,"");
	},
	
	checkBlacklist: function (text) {
		var r = eval('('+sol.ajax.get('/check_blacklist',null,{'text':text})+')');
		this.badwords = r['badwords'];
		return r['text'];
	}
};

