var dateobj		= new Date();
var this_year	= dateobj.getFullYear();
var days		= ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"];
var months		= ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthdays	= [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var years		= new Array();

var day_sel		= 0;
var month_sel	= 0;
var year_sel	= 0;
var hours_pos	= 0;

function isLeapYear(year){
	if((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))){
		return 1;
	}
	return 0;
}

function fillYears(id, selected){
	if(id.options){
		var selector = id;
	}else{
		var selector = $(id);
	}
	while(selector.length){
		selector.remove(0);
	}
	for(var i = 0, j = this_year; j >= 2000; i++, j--){
		selector.options[i] = new Option(j, j);
		if(selected === j)
			selector.selectedIndex = i;
	}
}

function fillYearsToNow(id, selected){
	if(id.options){
		var selector = id;
	}else{
		var selector = $(id);
	}
	while(selector.length){
		selector.remove(0);
	}
	for(var i = 0; i < 100; i++){
		year = this_year - i;
		selector.options[i] = new Option(year, year);
		if(selected === year)
			selector.selectedIndex = i;
	}
}

function fillYearsFromNow(id, selected){
	if(id.options){
		var selector = id;
	}else{
		var selector = $(id);
	}
	while(selector.length){
		selector.remove(0);
	}
	for(var i = 0; i < 20; i++){
		year = this_year + i;
		selector.options[i] = new Option(year, year);
		if(selected === year)
			selector.selectedIndex = i;
	}
}

function fillMonths(id, selected){
	if(id.options){
		var selector = id;
	}else{
		var selector = $(id);
	}
	while(selector.length){
		selector.remove(0);
	}
	for(var i = 0; i < 12; i++){
		selector.options[i] = new Option(months[i], i + 1);
		if(selected === i + 1)
			selector.selectedIndex = i;
	}
}

function fillMonthsMax(id, selected, max){
	if(id.options){
		var selector = id;
	}else{
		var selector = $(id);
	}
	while(selector.length){
		selector.remove(0);
	}
	for(var i = 0; i < max && i < 12; i++){
		selector.options[i] = new Option(months[i], i + 1);
		if(selected === i + 1)
			selector.selectedIndex = i;
	}
	if(max === 0){
		selector.options[0] = new Option(months[11], 12);
	}
}

function fillDays(did, mid, yid, selected){
	if(did.options){
		var selector = did;
	}else{
		var selector = $(did);
	}
	if(mid.options){
		var month_sel	= mid.value;
	}else{
		var month_sel	= $(mid).value;
	}
	if(yid.options){
		var year_sel	= yid.value;
	}else{
		var year_sel	= $(yid).value;
	}
	var dayscount	= ((month_sel == 2) && isLeapYear(year_sel)) ? 29 : monthdays[month_sel];
	while(selector.length){
		selector.remove(0);
	}
	for(var i = 0; i < dayscount; i++){
		selector.options[i] = new Option(days[i], i + 1);
		if(selected === i + 1)
			selector.selectedIndex = i;
	}
}

function selectPeriod(){
	$('choose_day').style.display = 'none';
	$('choose_day1').style.display = 'none';
	$('choose_month').style.display = 'none';
	$('choose_month1').style.display = 'none';
	$('choose_period').style.display = 'none';
	$('choose_period1').style.display = 'none';
	switch($('frm_stat').period.value){
		case 'day':
			$('choose_day').style.display = 'block';
			$('choose_day1').style.display = 'block';
			break;
		case 'month':
			$('choose_month').style.display = 'block';
			$('choose_month1').style.display = 'block';
			break;
		case 'period':
			$('choose_period').style.display = 'block';
			$('choose_period1').style.display = 'block';
	}
}

// for mt

function $E(selector){
	return $(document.body).getElement(selector);
}

Element.implement({
	ajaxForm: function(responser, presubmit){
		this.addEvent('submit', function (e){
			new Event(e).stop();
			if(typeof presubmit === 'function'){
				if(!presubmit(this))
					return;
			}
			new Request({
				url: this.action,
				method: 'post',
				onSuccess: function (responseText, responseXML){
					eval(responser + '(responseText);');
				}
			}).send(this);
		});
	},
	ajaxRef: function(responser, presubmit){
		this.addEvent('click', function (e){
			new Event(e).stop();
			if(typeof presubmit === 'string'){
				if(presubmit && !confirm(presubmit))
					return;
			}
			if(typeof presubmit === 'function'){
				if(!presubmit(this))
					return;
			}
			new Request({
				url: this.href,
				method: 'get',
				onSuccess:	function (responseText){
					eval(responser + '(responseText);');
				}
			}).send();
		});
	},
	ajaxChBox: function(responser, presubmit){
		this.addEvent('click', function (e){
			if(typeof presubmit === 'string'){
				if(presubmit && !confirm(presubmit))
					return;
			}
			if(typeof presubmit === 'function'){
				if(!presubmit(this))
					return;
			}
			var surl = this.src + '&flag=' + (this.checked ? 1 : 0);
			new Request({
				url: surl,
				method: 'get'
			}).send();
		});
	},
	showUndergrounded: function(){
		$('underground').removeClass('no_display');
		this.scrollIt = function(e){
			var center	= $(document.body).getSize();
			var scroll	= $(document.body).getScroll();
			var thisCoo	= $('underground').subLayer.getCoordinates();
			$('underground').subLayer.setStyle('left', parseInt(scroll.x + (center.x - thisCoo.width) / 2) + 'px');
			$('underground').subLayer.setStyle('top', parseInt(scroll.y + (center.y - thisCoo.height) / 2) + 'px');
		}
		var body_size = window.getScrollSize();
		$('underground').subLayer = this;
		$('underground').setStyle('width', body_size.x + 'px');
		$('underground').setStyle('height', body_size.y + 'px');
		$('underground').addEvent('click', hideUndergrounded);
		this.removeClass('no_display');
		this.scrollIt();
		window.addEvent('scroll', this.scrollIt);
	},
	setAsUserList: function(formSend){
		if(!this.id || this.id === '')
			this.id = 'user_selector_' + Math.round(Math.random() * 10000);
		var this_id = this.id;
		this.drop_down_div = new Element('div', {'class': 'user_list no_display'}).inject(this, 'after');
		new Element('br').inject(this, 'after');
		this.user_list = new Array();
		this.user_list_reload	= 0;
		this.user_list_chosen	= 0;
		this.user_list_down		= 0;
		this.value_last	= '';
		this.formSend = (typeof formSend === 'function') ? formSend : function(){};
		this.reloadUserList = function(){
			if(!this.user_list_reload)
				return;
			this.user_list_reload = 0;
			this.user_list_chosen = 0;

			var search	= this.selectionStart && this.selectionEnd === this.value.length
				? this.value.substr(0, this.selectionStart) : this.value;
			if(!search.length){
				hide(this.drop_down_div);
				return;
			}
			var letter = search.substr(0, 1);
			var cur_UL = this.drop_down_div.getElement('a');
			for(var i = 0; i < this.user_list[letter].length; i++){
				var email	= this.user_list[letter][i].email;
				var id		= this.user_list[letter][i].id;
				if(email.toLowerCase().indexOf(search.toLowerCase()) !== -1){
					while(cur_UL && email > cur_UL.get('html')){
						var next = cur_UL.getNext('a');
						cur_UL.destroy();
						cur_UL = next;
					}
					if(cur_UL && cur_UL.get('html') === email){
						cur_UL = cur_UL.getNext('a');
						continue;
					}
					var new_a = new Element('a', {'href': ''}).set('html', email).addEvents({
						'click': function(e){
							new Event(e).stop();
							var email = this.get('html');
							this.field.value = email;
							this.field.value_last = email;
						},
						'mouseover': function(e){
							this.getParent('div').getElements('a').each(function(a){
								a.removeClass('hover');
							});
							this.addClass('hover');
						},
						'mouseout': function(e){
							this.removeClass('hover');
						}
					});
					new_a.field = this;
					if(cur_UL)
						new_a.inject(cur_UL, 'before');
					else
						new_a.inject(this.drop_down_div, 'bottom');
				}
			}
			while(cur_UL){
				var next = cur_UL.getNext('a');
				cur_UL.destroy();
				cur_UL = next;
			}

			var first_a = this.drop_down_div.getElement('a');
			if(first_a){
				if(this.drop_down_div.getElement('a.hover'))
					this.drop_down_div.getElement('a.hover').removeClass('hover');
				this.user_list_chosen = first_a;
				this.user_list_chosen.addClass('hover');
				if(this.selectionStart){
					this.value = first_a.get('html');
					this.selectionStart	= search.length;
					this.selectionEnd	= this.value.length;
				}
				show(this.drop_down_div);
			}else
				this.user_list_chosen = 0;

			if(this.user_list_reload)
				this.reloadUserList();
		}
		this.gotUsers = function(text){
			var lines = text.split('\n');
			switch(lines[0]){
				case 'got_users':
					var letter = lines[1];
					$(this_id).user_list[letter] = new Array();
					for(var i = 2; i < lines.length; i++){
						var line = lines[i].split('|');
						$(this_id).user_list[letter][i - 2] = {'id': parseInt(line[0]), 'email': line[1]};
					}
					$(this_id).reloadUserList();
					break;
				default:
					if(text)
						alert(text);
			}
		}
		this.addEvents({
			'keypress': function(e){
				e = new Event(e);
				switch(e.key){
					case 'down':
						this.user_list_down = 1;
						if(this.user_list_chosen){
							if(this.user_list_chosen.getNext('a')){
								this.user_list_chosen.removeClass('hover');
								this.user_list_chosen = this.user_list_chosen.getNext('a');
								this.user_list_chosen.addClass('hover');
							}
						}else{
							this.user_list_chosen = this.drop_down_div.getElement('a');
							this.user_list_chosen.addClass('hover');
						}
						var email = this.user_list_chosen.get('html');
						if(this.selectionStart){
							var sel_start	= this.selectionStart;
							this.value		= email;
							this.selectionStart	= sel_start;
							this.selectionEnd	= this.value.length;
							this.value_last	= email;
						}
						break;
					case 'enter':
						e.stop();
						if(this.user_list_chosen){
							var email = this.user_list_chosen.get('html');
							this.value = email;
							this.value_last = email;
						}else if(this.value !== ''){
							var first_a = this.drop_down_div.getElement('a');
							if(first_a){
								var email = first_a.get('html');
								this.value = email;
								this.value_last = email;
							}
						}
						this.formSend(e);
						break;
					case 'backspace':
						if(this.selectionStart)
							this.value = this.value.substr(0, this.selectionStart);
						break;
				}
			},
			'keyup': function(e){
				e = new Event(e);
				switch(e.code){
					case 40:
						if(this.user_list_down){
							this.user_list_down = 0;
							break;
						}
						if(this.user_list_chosen){
							if(this.user_list_chosen.getNext('a')){
								this.user_list_chosen.removeClass('hover');
								this.user_list_chosen = this.user_list_chosen.getNext('a');
								this.user_list_chosen.addClass('hover');
							}
						}else{
							this.user_list_chosen = this.drop_down_div.getElement('a');
							this.user_list_chosen.addClass('hover');
						}
						var email		= this.user_list_chosen.get('html');
						if(this.selectionStart){
							var sel_start	= this.selectionStart;
							this.value		= email;
							this.selectionStart	= sel_start;
							this.selectionEnd	= this.value.length;
							this.value_last	= email;
						}
						return;
					case 38:
						if(this.user_list_chosen){
							var prev = this.user_list_chosen.getPrevious('a');
							if(prev){
								this.user_list_chosen.removeClass('hover');
								this.user_list_chosen = prev;
								this.user_list_chosen.addClass('hover');
								var email		= this.user_list_chosen.get('html');
								if(this.selectionStart){
									var sel_start	= this.selectionStart;
									this.value		= email;
									this.selectionStart	= sel_start;
									this.selectionEnd	= this.value.length;
									this.value_last	= email;
								}
							}
						}
						return;
					default:
				}
				if(this.value_last === this.value)
					return;
				this.value_last = this.value;
				var letter = encodeURIComponent(this.value.substr(0, 1));
				this.user_list_reload = 1;
				if(typeof this.user_list[letter] !== 'undefined'){
					this.reloadUserList();
					return;
				}
				this.user_list[letter] = 1;
				new Request({
					url: '/ajax/moderator/users/get=' + letter,
					method: 'get',
					onSuccess: this.gotUsers
				}).send();
			},
			'blur': function(e){
				if(this.value !== ''){
					var first_a = this.drop_down_div.getElement('a');
					if(first_a && first_a.get('html') === this.value){
						var email = first_a.get('html');
						this.value = email;
						this.value_last = email;
					}
				}
				setTimeout("$('" + this.id + "').drop_down_div.addClass('no_display');", 100);
			},
			'focus': function(e){
				if(this.value !== ''){
					if(this.drop_down_div.getElement('a'))
						this.drop_down_div.removeClass('no_display');
				}
			}
		});
	}
});

function hideUndergrounded(e){
	if(e && e.target && (e.target.hasClass('undergrounded') || e.target.getParent('.undergrounded')))
		return;
	$('underground').subLayer.addClass('no_display');
	$('underground').addClass('no_display');
	$('underground').removeEvents('click');
}

window.addEvent('domready', function(){
	var body_size = window.getScrollSize();
	$('underground').setStyle('opacity', 0.5);
	$('underground').setStyle('width', body_size.x + 'px');
	$('underground').setStyle('height', body_size.y + 'px');

	$$('div.closePopup a').each(function(a){
		a.addEvent('click', function(e){
			new Event(e).stop();
			var div = a.getParent('div.undergrounded');
			if(div && !div.hasClass('no_display')){
				$('underground').addClass('no_display');
				div.addClass('no_display');
			}
		});
	});
});

// by Grin

var emailFilter = /^([\w\d_\.\-])+\@(([\w\d\-])+\.)+([\w\d]{2,4})+$/;
var icqFilter = /^\d+$/;
function checkRegForm(){
	var regForm			= document.forms.regForm;//$('registrationForm');

	var loginInp		= regForm.email.value;
	var passwordInp		= regForm.pwd1.value;
	var passwordConfInp	= regForm.pwd2.value;
	var icqConfInp		= regForm.icq.value;

	var regFormSubmit	= $('registrationFormSubmit');

	if(emailFilter.test(loginInp) && passwordInp.length > 2 && passwordInp == passwordConfInp && icqFilter.test(icqConfInp)){
		regFormSubmit.disabled = false;
		regForm.onsubmit = function() { return true;}
	}else{
		regFormSubmit.disabled = true;
		regForm.onsubmit = function() { return false;}
	}
}

function checkEmail(){
	var loginInp = $('loginInp').value;
	if(!emailFilter.test(loginInp) && loginInp != "")
		$('incorrectEmail').removeClass('no_display');
	else
		$('incorrectEmail').addClass('no_display');
}

function checkIcq(){
	var icqInp = $('icq').value;
	if(!icqFilter.test(icqInp) && icqInp != "")
		$('incorrectIcq').removeClass('no_display');
	else
		$('incorrectIcq').addClass('no_display');
}

function checkPass(){
	var passwordInp = $('passwordInp').value;
	if(passwordInp.length < 3)
		$('tooShortPass').removeClass('no_display');
	else
		$('tooShortPass').addClass('no_display');
}

function checkPassConf(){
	var passwordInp		= $('passwordInp').value;
	var passwordConfInp	= $('passwordConfInp').value;
	if(passwordInp.length < 3 || passwordInp != passwordConfInp)
		$('wrongPassConf').removeClass('no_display');
	else
		$('wrongPassConf').addClass('no_display');
}

function show(el){
	$(el).removeClass('no_display');
}

function hide(el){
	$(el).addClass('no_display');
}

function showHide(el){
	$(el).toggleClass('no_display');
}

function wait(){
	$('underground').addClass('loading').removeClass('no_display');
}

function stopWaiting(){
	$('underground').removeClass('loading').addClass('no_display');
}
