var Loader = {
	loader: null,
	create: function() {
		Loader.loader = new Element('div').setStyles({opacity: 0.2, left: '0px', top: '0px', width: '100%', height: '100%', position: 'absolute', display:'none', backgroundColor: '#cccccc', zIndex: 99998});
		Loader.loaderImg = new Element('div', {'class':'loader'}).setStyles({display:'none', zIndex: 99999});
		document.body.appendChild(Loader.loader);
		document.body.appendChild(Loader.loaderImg);
	},
	show: function() {
		if (Loader.visible()) return;
		Loader.loader.style.display = '';
		Loader.loaderImg.style.display = '';
	},
	hide: function() {
		if (Loader.loader) {
			Loader.loader.style.display = 'none';
			Loader.loaderImg.style.display = 'none';
		}
	},
	visible: function() {
		return (Loader.loader && Loader.loader.style.display != 'none');
	}
};


var ErrorMsg = {
	create: function() {
		ErrorMsg.frame = new Element('div', {'class': 'error_msg'}).setStyles({display: 'none', opacity: 0, zIndex: 99993});
		var close = new Element('div', {'class': 'close'});
		close.addEvent('click', ErrorMsg.hide);
		ErrorMsg.frame.appendChild(close);
		var erImg = new Element('div', {'class': 'image'});
		ErrorMsg.frame.adopt(erImg);
		ErrorMsg.text = new Element('div', {'class': 'text'})
		ErrorMsg.frame.appendChild(ErrorMsg.text);
		document.body.appendChild(ErrorMsg.frame);
		ErrorMsg.fx = new Fx.Tween(ErrorMsg.frame, {
			duration: 250,
			link: 'cancel',
			property: 'opacity'
		});
 	},
	show: function(str) {
		if (!ErrorMsg.frame) {
			ErrorMsg.create();
		}
		ErrorMsg.text.innerHTML = ErrorMsg.getStr(str);
		ErrorMsg.frame.style.display = '';
		ErrorMsg.fx.start(1);
	},
	hide: function() {
		if (!ErrorMsg.frame) {
			return;
		}
		ErrorMsg.fx.start(0).chain(function() {
			ErrorMsg.frame.style.display = 'none';
		});
	},
	getStr: function(v) {
		if (typeOf(v) != 'array') v = [v];
		var er = '<ul>';
		for (var i = 0, n = v.length; i < n; i++) {
			var val;
			if (v[i].indexOf(':::') > 0) {
				var tmp = v[i].split(':::');
				val = tmp[0].replace('__REPLACE__', tmp[1]);
			} else {
				val = v[i];
			}
			er += '<li>' + val + '</li>';
		}
		er += '</ul>';
		return er;
	},
	visible: function() {
		return ErrorMsg.frame.style.display != 'none';
	}
}

var Tooltip = {
	frame: null,

	create: function(dir) {
		Tooltip.dir = (dir == 'R') ? 'R' : 'L';
		this.frame = new Element('div', {'class': 'tooltip'}).setStyles({position: 'absolute', zIndex: 9999, opacity: 0});
		var tbl, tbody, tr, td;
		tbl = new Element('table');
		tbody = new Element('tbody');
		tbl.appendChild(tbody);
		tr = new Element('tr');
		tbody.appendChild(tr);
		td = new Element('td', {'class': 'top-left'});
		tr.appendChild(td);
		td = new Element('td', {'class': 'top'});
		tr.appendChild(td);
		td = new Element('td', {'class': 'top-right'});
		tr.appendChild(td);
		tr = new Element('tr');
		tbody.appendChild(tr);
		td = new Element('td', {'class': 'center-left'});
		tr.appendChild(td);
		td = new Element('td', {'class': 'center'});
		tr.appendChild(td);
		this.container = new Element('div');
		td.appendChild(this.container);
		td = new Element('td', {'class': 'center-right'});
		tr.appendChild(td);
		tr = new Element('tr');
		tbody.appendChild(tr);
		td = new Element('td', {'class': 'bottom-left'});
		tr.appendChild(td);
		td = new Element('td', {'class': ((Tooltip.dir == 'L') ? 'bottom-ar' : 'bottom-al')});
		tr.appendChild(td);
		td = new Element('td', {'class': 'bottom-right'});
		tr.appendChild(td);

		Tooltip.frame.appendChild(tbl);
		document.body.appendChild(this.frame);
	},

	hide: function(fade) {
		Tooltip.frame.fade('out')
	},
	show: function(str, obj) {
		if (str == '') return;
		Tooltip.container.innerHTML = str;
		Tooltip.frame.setStyles({top: '0px', left: '0px'});
		var p = obj.getPosition();

		var tLeft;
		if (Tooltip.dir == 'R') {
			tLeft = p.x - 34;
		} else {
			tLeft = p.x - Tooltip.frame.offsetWidth + 50;
		}

		Tooltip.frame.setStyles({top: (p.y - Tooltip.frame.offsetHeight) + 'px', left: tLeft + 'px'});
		Tooltip.frame.fade('in')
	},
	visible: function() {
		return (Tooltip.frame.style.visibility == 'visible');
	}
}



function checkDomain(val) {
	if (val == "") {
		return false;
	}
	if (val.substr(0, 4) == "www.") {
		val = val.substr(4);
	}
	var v = val.split(".");

	var wIT, wNOIT;
	for (var i = 0, n = document.forms["check_domain"].elements.length; i < n; i++) {
		if (document.forms["check_domain"].elements[i].name.indexOf("tld_") === 0) {
			if (document.forms["check_domain"].elements[i].name == "tld_it" && document.forms["check_domain"].elements[i].checked) {
				wIT = true
			}
			if (document.forms["check_domain"].elements[i].name != "tld_it" && document.forms["check_domain"].elements[i].checked) {
				wNOIT = true
			}
		}
	}
	var tMax;
	if (wNOIT) {
		tMax = 1;
		for (var i = 0, n = aTld.length; i < n; i++) {
			if (v[v.length - 1] == aTld[i]) {
				++tMax;
				break;
			}
		}
	} else if (wIT) {
		tMax = 3;
		if (v[v.length - 1] == "it") {
			++tMax;
		}
	} else {
		tMax = 0;
	}
	if (v.length > tMax) {
		return false;
	}
	var re;
	if (v.length == 1) {
		re = /^([a-zA-Z0-9\-]){3,}$/;
	} else 	if (v.length == 2) {
		re = /^([a-zA-Z0-9\-]){3,}\.([a-zA-Z0-9\-]){2,}$/;
	} else 	if (v.length == 3) {
		re = /^([a-zA-Z0-9\-]){3,}\.([a-zA-Z0-9\-]){2,}\.([a-zA-Z0-9\-]){2,}$/;
	} else 	if (v.length == 4) {
		re = /^([a-zA-Z0-9\-]){3,}\.([a-zA-Z0-9\-]){2,}\.([a-zA-Z0-9\-]){2,}\.it$/;
	}
	if (!re.test(val)) {
		return false;
	}	
	return true;
}


$V = function(el, val) {
	if (el) {
		if (el.type == 'checkbox') {
			el.checked = Boolean(val);
		} else if (el[0] && el[0].type == 'radio') {
			for (var i = 0, n = el.length; i < n; i++) {
				if (el[i].value == val) {
					el[i].checked=true;
					break;
				}
			}
		} else {
			el.value = val;
		}
		return true;
	}
	return false;
}

