String.prototype.wordWrap = function(m, b, c){
	var i, j, l, s, r
	if (m<1) return this
	for(i=-1, l=(r = this.split('\n')).length; ++i<l; r[i]+=s)
		for(s = r[i], r[i] = ''; s.length > m; r[i] += s.slice(0, j) + ((s = s.slice(j)).length ? b : ''))
			j = c == 2 || (j = s.slice(0, m + 1).match(/\S*(\s)?$/))[1] ? m : j.input.length - j[0].length || c == 1 && m || j.input.length + (j = s.slice(m).match(/^\S*/)).input.length
	return r.join('\n').replace('\n ', '\n')
}

function sw_contact(){
	var n = el('notify')
	var e = el('contact')
	if (n && e){
		n = n.checked?1:0
		SetCookie('contact', n)
		e.style.display = n ? '' : 'none'
	}
	return false
}

function sw_tmpro(tm){
  var e = el('tmpro'+tm)
  var p = el('mpro'+tm)
  if (e && p)
	if (p.style.display == ''){
		e.className = 'pro'
		e.innerHTML = S_PROFILE
		p.style.display = 'none'
	}else{
		e.className = 'proex'
		e.innerHTML = S_COLLAPSE
		p.style.display = ''
	}
  return false
}

function check_vote(one_mark){
	if (chks == 0){
		alert(one_mark)
		return false
	}
}

function addform_CtrlEnter(e){
	var ctrl = e ? e.ctrlKey : event.ctrlKey
	var key = e ? e.keyCode : event.which
	if ((key == 13 && ctrl) || (key == 10)) checkadd(true)
}

function delform_CtrlEnter(e){
	var ctrl = e ? e.ctrlKey : event.ctrlKey
	var key = e ? e.keyCode : event.which
	if ((key == 13 && ctrl) || (key == 10)) checkdel(true)
}

function call_clear(){
	Input.clear(1)
}

function already_voted(){
	var e = el('already_voted')
	if (e) alert(e.innerHTML.replace(/<span><\/span>/i, '\n\n').wordWrap(70, '\n', 1))
}

function voted_users(c, m, y, q){
	exec_url('/inc/vote/voted_users.php?c='+c+'&m='+m+'&y='+y+'&q='+q+'&lang='+lang+'&rnd='+Math.random(), fin_voted_users, true)
	return false
}

function fin_voted_users(){
	if (!req.responseText) return
	var r = split1('~', req.responseText)
	if (!r[0] || !r[1]){
		alert(req.responseText)
		return
	}
	var e1 = el('rmv'+r[0])
	var e2 = el('rmc'+r[0])
	if (e1&&e2){
		e1.style.display = ''
		e2.innerHTML = r[1]
	}
}

function turn_vote(u, c, y, q){
	var e = el('voteact'+u)
	if (e)
		exec_url('/inc/vote/turn_vote.php?a='+(e.className==''?'Y':'N')+'&u='+u+'&c='+c+'&y='+y+'&q='+q+'&lang='+lang+'&rnd='+Math.random(), fin_turn_vote, true)
	return false
}

function fin_turn_vote() {
	if (!req.responseText) return
	var r = split1('~', req.responseText)
	if (!r[0] || !r[1]) return
	var p = el('voteact'+r[0])
	var b = el('turnbtn'+r[0])
	if (p && b) {
		p.className = r[1] == 'Y' ? '' : 'inact'
	}
}


// CHECKBOXES
var chks = 0
var blockop = 0
var mouse_el = false
var Input = {
	initialize: function(){
		chks = 0
		var e = el('vote_form')
		if (e) this.init_el(e)
	},

	init_el: function(e) {
			var id, cb, ch, cl, j, as
			var divs = e.getElementsByTagName('div')
			for(var i = 0; i < divs.length; i++) {
				if(divs[i].className.substr(0, 4) == 'cbox') {
					id = divs[i].id.substr(1)
					cb = el('vc'+id)
					ch = el('vh'+id)
					if (cb&&ch) {
						cl = el('vl'+id)
						if (cl) ch.innerHTML = cl.innerHTML
						if ((cb.checked = divs[i].className != 'cbox') && (divs[i].id.indexOf('_') < 0)) chks++
						divs[i].onmousedown = this.effect
						divs[i].onmouseup = this.handle
						ch.onmousedown = this.labeldown
						ch.onmouseup = this.labelup

						as = ch.getElementsByTagName('a')
						for (j=0;j<as.length;j++){
							as[j].onmousedown = this.noop
							as[j].onmouseup = this.noop
						}
					}
				}else if (divs[i].className.substr(0,4) == 'fbox')
					divs[i].onmousedown = already_voted
			}
	},

	noop: function(e) {blockop = 1},

	selbox: function(e) {
		return e.getElementsByTagName('input')[0]
	},

	is_checked: function(e) {
			if (!e) return false
			var c = Input.selbox(e)
			return c && c.checked
	},

	checkmark: function(e, no_subs, c) {
		if (!e) return
		var selector = Input.selbox(e)
		if (!selector) return false

		var id = e.id.substr(1)
		if (!id) return

		var subchk = id.indexOf('_') > 0
		if (subchk && no_subs) return

		var li = el('l'+id)
		var medal = el('medal'+id)

		var a = arguments.length == 3
		if(!selector.checked && (!a || c)){

			if (!subchk){
				var e0 = el('c0')
				if (e0)
					if (id == 0){
						if (chks) self.setTimeout('Input.abstain(1)', 1)
					}else if (Input.is_checked(e0))
						Input.checkmark(e0, 1, 0)

				if ((id != 0) && (chks >= max_votes)){
					self.setTimeout('call_clear()', 1)
					return
				}
			}

			if (!selector.checked) {
				if (!subchk) chks++
				selector.checked = true
				e.className = 'cbox selected'
				e.style.backgroundPosition = '0 -28px'
				if (li){
					li.className = li.className.charAt(1) == '1' ? 'l3' : 'l2'
					if (medal) medal.src = '/images/catblocks/m.png'
				}
			}
		}else if(selector.checked && (!a || !c)){
			if (!subchk) chks--

			selector.checked = false
			e.className = 'cbox'
			e.style.backgroundPosition = '0 0'

			if (li){
				if (medal){
					var sh = medal.style.fontSize.charAt(0)
					medal.src = li.className.charAt(1) == '3' ? '/images/catblocks/m'+sh+'1.png' : '/images/catblocks/m'+sh+'.png'
				}
				li.className = li.className.charAt(1) == '3' ? 'l1' : 'l'
			}
		}
		li = el('exp'+id)
		if (li)
			li.style.display = selector.checked ? '' : 'none'
		else{
			e = el('exp_tpl')
			li = el('exps'+id)
			if (e && li){
				li.innerHTML = e.innerHTML.replace(/%m%/g, id)
				this.init_el(li)
			}
		}
	},

	down: function(e, ev){
		if (!e || !e.id) return
		if (window.Event) { if (ev.which != 1) return }
		else { if (window.event.button > 1) return }

		if (mouse_el) Input.clear()
		mouse_el = e

		var id = e.id.substr(1)
		if (!id) return

		var li = el('l'+id)
		if (li && li.className.match('unvoteable')) {
			already_voted()
			return false
		}else e.style.backgroundPosition = e.className == 'cbox' ? '0 -14px' : '0 -42px'
	},

	up: function(e, ev){
		if (!e || !e.id) return
		if (window.Event) { if (ev.which != 1) {Input.clear();return} }
		else { if (window.event.button > 1) {Input.clear();return} }

		if (e.id != mouse_el) Input.clear()

		var id = e.id.substr(1)
		if (!id) return

		var li = el('l'+id)
		if (li && li.className.match('unvoteable'))
			return false

		Input.checkmark(e)
	},

	labeldown: function(e){
		if (!blockop) Input.down(el('c'+this.id.substr(2)), e)
	},

	labelup: function(e){
		if (!blockop) Input.up(el('c'+this.id.substr(2)), e)
		blockop = 0
	},

	effect: function(e){
		Input.down(this, e)
	},

	handle: function(e){
		Input.up(this, e)
	},

	clearbox: function(e){
		if(e.className == 'cbox')
			e.style.backgroundPosition = '0 0'
		else if(e.className == 'cbox selected')
			e.style.backgroundPosition = '0 -28px'
	},

	clear: function(al){
		var e = el('vote_form')
		if (!e) return

		if (mouse_el)
			Input.clearbox(mouse_el)
		else{
			var divs = e.getElementsByTagName('div')
			for(var i = 0; i < divs.length; i++)
				Input.clearbox(divs[i])
		}

		blockop = 0
		mouse_el = false
		if (al)	alert(warn_maxvotes)
	},

	abstain: function(){
		var e = el('vote_form')
		if (!e) return

		var divs = e.getElementsByTagName('div')
		for(var i = 0; i < divs.length; i++)
			if((divs[i].id != 'c0') && (divs[i].className == 'cbox selected'))
				this.checkmark(divs[i], 1, 0)

		alert(S_ABSENCE)
		blockop = 0
	}
}