var errors = {
	'pl': {//PL
		ERR_POPUP: 'Wyłącz blokadę wyskakujących okienek!',
		ERR_POST_RM: 'Zaznacz posty do usunięcia.',
		ERR_POST_MV: 'Zaznacz posty do przeniesienia.'
	},
	'en': {//EN
		ERR_POPUP: 'Allow this page to open pop-up windows!',
		ERR_POST_RM: 'Select posts to be removed.',
		ERR_POST_MV: 'Select posts to be moved.'
	}
}

var confirms = {
	'pl': {//PL
		AVATAR_RM: 'Czy na pewno chcesz usunąć avatara?',
		IMG_RM: 'Czy na pewno chcesz usunąć to zdjecie?',
		NEWS_RM: 'Czy na pewno chcesz usunąć tego newsa?',
		COMM_RM: 'Czy na pewno chcesz usunąć ten komentarz?',
		CLAN_RM: 'Czy na pewno chcesz się wypisać z tego klanu?',
		TH_RM: 'Cały wątek "%TH" i wszystkie jego posty zostaną teraz usunięte!',
		PS_RM: 'Post napisany %DT przez %USR zostanie teraz usunięty!',
		ASK: 'Czy na pewno?',
		POST_RM: 'Czy na pewno usunąć zaznaczone posty (%N)?'
	},
	'en': {//EN
		AVATAR_RM: 'Are you sure you want to remove the avatar?',
		IMG_RM: 'Are you sure you want to remove this photo?',
		NEWS_RM: 'Are you sure you want to remove this news?',
		COMM_RM: 'Are you sure you want to remove this comment?',
		CLAN_RM: 'Are you sure you want to sign out of this clan?',
		TH_RM: 'Cały wątek "%TH" i wszystkie jego posty zostaną teraz usunięte!',
		PS_RM: 'Post napisany %DT przez %USR zostanie teraz usunięty!',
		ASK: 'Czy na pewno?',
		POST_RM: 'Czy na pewno usunąć zaznaczone posty (%N)?'
	}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

window.addEvent('domready', function(){
	
	var set_form_bg = function(form, item, flag){
		var a = $(form).getElement('input[name='+item+']');
		if(flag) {
			a.setStyles({
				'background-image': 'url(img/form_'+item+(site_lang=='pl' ? '' : '_en')+'.png)',
				'background-repeat': 'no-repeat'
			});
		} else {
			a.setStyles({
				'background-image': 'none'
			});
		}
	}
	
	if($('form-login')) {
		var login = $('form-login').getElement('input[name=login]');
		var passwd = $('form-login').getElement('input[name=passwd]');
		if(login.value.length == 0) {
			set_form_bg('form-login', 'login', true);
		}
		if(passwd.value.length == 0) {
			set_form_bg('form-login', 'passwd', true);
		}
		login.addEvent('focus', function(){
			set_form_bg('form-login', 'login', false);
		});
		login.addEvent('blur', function(){
			if(login.value.length == 0) {
				set_form_bg('form-login', 'login', true);
			}
		});
		passwd.addEvent('focus', function(){
			set_form_bg('form-login', 'passwd', false);
		});
		passwd.addEvent('blur', function(){
			if(passwd.value.length == 0) {
				set_form_bg('form-login', 'passwd', true);
			}
		});
	}
	
	if($('form-search')) {
		var slogin = $('form-search').getElement('input[name=l]');
		var city = $('form-search').getElement('input[name=c]');
		if(slogin.value.length == 0) {
			set_form_bg('form-search', 'l', true);
		}
		if(city.value.length == 0) {
			set_form_bg('form-search', 'c', true);
		}
		slogin.addEvent('focus', function(){
			set_form_bg('form-search', 'l', false);
		});
		slogin.addEvent('blur', function(){
			if(login.value.length == 0) {
				set_form_bg('form-search', 'l', true);
			}
		});
		city.addEvent('focus', function(){
			set_form_bg('form-search', 'c', false);
		});
		city.addEvent('blur', function(){
			if(city.value.length == 0) {
				set_form_bg('form-search', 'c', true);
			}
		});
	}
	
	$$('a[rel=checkLogin]').each(function(el){
		el.addEvent('click', function(e){
			new Event(e).stop();
			var login = $('login').value;
			var req = new Request.HTML({
				method: 'post',
				url: site_url+'ajax/check_login',
				data: { 'login' : login },
				onRequest: function(){
					$('checkLoginRes').empty();
					$('checkLoginBox').setStyles({
						visibility: 'hidden'
					});
					$('checkLoginRes').setStyles({
						background: 'url('+site_url+'img/ajax-loader.gif) no-repeat center center'
					});
				},
				update: $('checkLoginRes'),
				onComplete: function(response) {
					$('checkLoginBox').setStyles({
						visibility: 'visible'
					});
					$('checkLoginRes').setStyles({
						background: 'none'
					});
					(function(){$('checkLoginRes').empty()}).delay(3000);
				}
			}).send();
		});
	});
	
	$$('a[rel^=confirm]').each(function(id) {
		switch(id.getProperty('rel')) {
			case 'confirmDelAvatar':
			id.addEvent("click", function(e) {
				var c=confirm(confirms[site_lang].AVATAR_RM);
				if(!c) {
					new Event(e).stop();
				}
			});
			break;
			case 'confirmDelImage':
			id.addEvent("click", function(e) {
				var c=confirm(confirms[site_lang].IMG_RM);
				if(!c) {
					new Event(e).stop();
				}
			});
			break;
			case 'confirmDelNews':
			id.addEvent("click", function(e) {
				var c=confirm(confirms[site_lang].NEWS_RM);
				if(!c) {
					new Event(e).stop();
				}
			});
			break;
			case 'confirmDelComment':
			id.addEvent("click", function(e) {
				var c=confirm(confirms[site_lang].COMM_RM);
				if(!c) {
					new Event(e).stop();
				}
			});
			break;
			case 'confirmDelFromClan':
			id.addEvent("click", function(e) {
				var c=confirm(confirms[site_lang].CLAN_RM);
				if(!c) {
					new Event(e).stop();
				}
			});
			break;
		}
	});
	
	$$('a[id^=vote_]').each(function(id) {
		var a={p: [], t: []};
		for(var i=0; i<10; i++) {
			a.p[i] = $('vote_p'+i).getElement('img').getProperty('src');
			a.t[i] = $('vote_t'+i).getElement('img').getProperty('src');
		}
		id.addEvent('mouseover', function(){
			var n = parseInt(this.getProperty('id').substr(-1));
			var type = this.getProperty('id').substr(-2,1);
			for(var i=0; i<10; i++) {
				$('vote_'+type+i).getElement('img').setProperty('src', site_url+(i <= n ? 'img/ocena_pelna.png' : 'img/ocena_pusta.png'));
			}
		});
		id.addEvent('mouseout', function(){
			var type = this.getProperty('id').substr(-2,1);
			for(var i=0; i<10; i++) {
				$('vote_'+type+i).getElement('img').setProperty('src', a[type][i]);
			}
		});
	});
	
	var win = null;
	var openWin = function(url, w, h, name, params){
		if(params==undefined) params='resizable=no,scrollbars=no';
		var l=(window.screen.width-w)/2;
		var t=(window.screen.height-h)/3;
		if(!win || win.closed) {
			win = window.open(url, name, 'width='+w+', height='+h+', left='+l+', top='+t+', '+params);
			if(win == null) {
				alert(errors[site_lang].ERR_POPUP);
			}
		} else {
			win.focus();
		}
	}
	
	$$('a[rel^=winModThread:]').each(function(el){
		var tmp = el.getProperty('rel').split(':');
		var id = tmp[1];
		el.addEvent('click', function(e){
			new Event(e).stop();
			openWin(site_url+'forum/moderuj_watek/'+id, 600, 400, 'modt'+id);
		});
	});
	
	$$('a[rel^=winModPost:]').each(function(el){
		var tmp = el.getProperty('rel').split(':');
		var id = tmp[1];
		el.addEvent('click', function(e){
			new Event(e).stop();
			openWin(site_url+'forum/moderuj_post/'+id, 600, 600, 'modp'+id);
		});
	});
	
	$$('select[id=modSectionsList]').each(function(el){
		el.addEvent('change', function(){
			var id = this.options[this.selectedIndex].value;
			var req = new Request({  
				method: 'get',
				url: site_url+'ajax/mod_get_threads_list/'+id, 
				onRequest: function() {
					$('modThreadsList').disabled = true;
				},
				onComplete: function(res) {
					$('modThreadsListBox').set('html', res);
					if($('create_thread')) {
						var s = $('create_thread').checked;
					} else {
						var s = false;
					}
					$('modThreadsList').disabled = s;
				}
			}).send();
		});
	});
	
	$$('a[rel^=modDelThread:]').each(function(el){
		var tmp = el.getProperty('rel').split(':');
		var id = tmp[1];
		el.addEvent('click', function(e){
			new Event(e).stop();
			if(confirm(confirms[site_lang].TH_RM.replace('%TH', $('threadTitle'+id).get('text')))){
				if(confirm(confirms[site_lang].ASK)){
					location.href = site_url+'forum/usun_watek/'+id;
				}
			}
		});
	});
	
	$$('a[rel^=modDelPost:]').each(function(el){
		var tmp = el.getProperty('rel').split(':');
		var id = tmp[1];
		el.addEvent('click', function(e){
			new Event(e).stop();
			if(confirm(confirms[site_lang].PS_RM.replace('%DT', $('postDate'+id).get('text')).replace('%USR', $('postUser'+id).get('text')))){
				if(confirm(confirms[site_lang].ASK)){
					location.href = site_url+'forum/usun_post/'+id;
				}
			}
		});
	});
	
	var countCheckedPosts = function(){
		var s = 0;
		$$('input[id^=checkPost:]').each(function(el){
			if(el.checked) {
				s++;
			}
		});
		return s;
	}
	
	var getCheckedPosts = function(){
		var a = new Array();
		var i = 0;
		$$('input[id^=checkPost:]').each(function(el){
			if(el.checked) {
				a[i++] = el.value;
			}
		});
		return a;
	}
	
	$$('input[id^=checkPost:]').each(function(el){
		var tmp = el.getProperty('id').split(':');
		var id = tmp[1];
		el.addEvent('click', function(e){
			var s = countCheckedPosts();
			$('modBtnPostsDel').disabled = s<1;
			$('modBtnPostsMove').disabled = s<1;
		});
	});
	
	if($('modBtnPostsDel')) {
		$('modBtnPostsDel').addEvent('click', function(e){
			var n = countCheckedPosts();
			if(n > 0) {
				if(!confirm(confirms[site_lang].POST_RM.replace('%N', n))){
					new Event(e).stop();
				}
			} else {
				alert(errors[site_lang].ERR_POST_RM);
				new Event(e).stop();
			}
		});
		$('modBtnPostsMove').addEvent('click', function(e){
			ids = getCheckedPosts().toString();
			if(ids.length > 0) {
				openWin(site_url+'forum/przenies_posty/'+ids, 600, 400, 'modmp');
			} else {
				alert(errors[site_lang].ERR_POST_MV);
				new Event(e).stop();
			}
		});
	}
	
	if($('roomList')) {
		
		function count_rooms_users() {
			new Request({
				url: site_url+'czat/ajax_count_users',
				onComplete: function(res) {
					var a = JSON.decode(res);
					if(a) {
						$$('span[id^=room]').each(function(el){
							var id = parseInt(el.getProperty('id').substring(4));
							el.set('text', !a[id] ? 0 : a[id]);
						});
					}
				}
			}).send();
		}
		count_rooms_users.periodical(5000);
		
		$$('a[rel^=openChat:]').each(function(el){
			var tmp = el.getProperty('rel').split(':');
			var id = tmp[1];
			el.addEvent('click', function(e){
				new Event(e).stop();
				openWin(site_url+'czat/pokoj/'+id, 780, 520, 'chatroom'+id);
			});
		});
		
	}

});

String.prototype.strReverse = function() {
	var newstring = "";
	for (var s=0; s < this.length; s++) {
		newstring = this.charAt(s) + newstring;
	}
	return newstring;
};

function checkPass(pwd) {
	var nScore = 0;
	var nLength = 0;
	var nAlphaUC = 0;
	var nAlphaLC = 0;
	var nNumber = 0;
	var nSymbol = 0;
	var nMidChar = 0;
	var nRequirements = 0;
	var nAlphasOnly = 0;
	var nNumbersOnly = 0;
	var nRepChar = 0;
	var nConsecAlphaUC = 0;
	var nConsecAlphaLC = 0;
	var nConsecNumber = 0;
	var nConsecSymbol = 0;
	var nConsecCharType = 0;
	var nSeqAlpha = 0;
	var nSeqNumber = 0;
	var nSeqChar = 0;
	var nReqChar = 0;
	var nReqCharType = 3;
	var nMultLength = 4;
	var nMultAlphaUC = 3;
	var nMultAlphaLC = 3;
	var nMultNumber = 4;
	var nMultSymbol = 6;
	var nMultMidChar = 2;
	var nMultRequirements = 2;
	var nMultRepChar = 1;
	var nMultConsecAlphaUC = 2;
	var nMultConsecAlphaLC = 2;
	var nMultConsecNumber = 2;
	var nMultConsecSymbol = 1;
	var nMultConsecCharType = 0;
	var nMultSeqAlpha = 3;
	var nMultSeqNumber = 3;
	var nTmpAlphaUC = "";
	var nTmpAlphaLC = "";
	var nTmpNumber = "";
	var nTmpSymbol = "";
	var sAlphas = "abcdefghijklmnopqrstuvwxyz";
	var sNumerics = "01234567890";
	var sComplexity = "?";
	var nMinPwdLen = 6;
	if (pwd.length >= nMinPwdLen) {
		nScore = parseInt(pwd.length * nMultLength);
		nLength = pwd.length;
		var arrPwd = pwd.replace (/\s+/g,"").split(/\s*/);
		var arrPwdLen = arrPwd.length;
		/* Loop through password to check for Symbol, Numeric, Lowercase and Uppercase pattern matches */
		for (var a=0; a < arrPwdLen; a++) {
			if (arrPwd[a].match(new RegExp(/[A-Z]/g))) {
				if (nTmpAlphaUC !== "") { if ((nTmpAlphaUC + 1) == a) { nConsecAlphaUC++; nConsecCharType++; } }
				nTmpAlphaUC = a;
				nAlphaUC++;
			}
			else if (arrPwd[a].match(new RegExp(/[a-z]/g))) { 
				if (nTmpAlphaLC !== "") { if ((nTmpAlphaLC + 1) == a) { nConsecAlphaLC++; nConsecCharType++; } }
				nTmpAlphaLC = a;
				nAlphaLC++;
			}
			else if (arrPwd[a].match(new RegExp(/[0-9]/g))) { 
				if (a > 0 && a < (arrPwdLen - 1)) { nMidChar++; }
				if (nTmpNumber !== "") { if ((nTmpNumber + 1) == a) { nConsecNumber++; nConsecCharType++; } }
				nTmpNumber = a;
				nNumber++;
			}
			else if (arrPwd[a].match(new RegExp(/[^a-zA-Z0-9_]/g))) { 
				if (a > 0 && a < (arrPwdLen - 1)) { nMidChar++; }
				if (nTmpSymbol !== "") { if ((nTmpSymbol + 1) == a) { nConsecSymbol++; nConsecCharType++; } }
				nTmpSymbol = a;
				nSymbol++;
			}
			/* Internal loop through password to check for repeated characters */
			for (var b=0; b < arrPwdLen; b++) {
				if (arrPwd[a].toLowerCase() == arrPwd[b].toLowerCase() && a != b) { nRepChar++; }
			}
		}
		/* Check for sequential alpha string patterns (forward and reverse) */
		for (var s=0; s < 23; s++) {
			var sFwd = sAlphas.substring(s,parseInt(s+3));
			var sRev = sFwd.strReverse();
			if (pwd.toLowerCase().indexOf(sFwd) != -1 || pwd.toLowerCase().indexOf(sRev) != -1) { nSeqAlpha++; nSeqChar++;}
		}
		/* Check for sequential numeric string patterns (forward and reverse) */
		for (var s=0; s < 8; s++) {
			var sFwd = sNumerics.substring(s,parseInt(s+3));
			var sRev = sFwd.strReverse();
			if (pwd.toLowerCase().indexOf(sFwd) != -1 || pwd.toLowerCase().indexOf(sRev) != -1) { nSeqNumber++; nSeqChar++;}
		}
	/* Modify overall score value based on usage vs requirements */
		/* General point assignment */
		if (nAlphaUC > 0 && nAlphaUC < nLength) {	
			nScore = parseInt(nScore + ((nLength - nAlphaUC) * 2));
			sAlphaUC = "+ " + parseInt((nLength - nAlphaUC) * 2); 
		}
		if (nAlphaLC > 0 && nAlphaLC < nLength) {	
			nScore = parseInt(nScore + ((nLength - nAlphaLC) * 2)); 
			sAlphaLC = "+ " + parseInt((nLength - nAlphaLC) * 2);
		}
		if (nNumber > 0 && nNumber < nLength) {	
			nScore = parseInt(nScore + (nNumber * nMultNumber));
			sNumber = "+ " + parseInt(nNumber * nMultNumber);
		}
		if (nSymbol > 0) {	
			nScore = parseInt(nScore + (nSymbol * nMultSymbol));
			sSymbol = "+ " + parseInt(nSymbol * nMultSymbol);
		}
		if (nMidChar > 0) {	
			nScore = parseInt(nScore + (nMidChar * nMultMidChar));
			sMidChar = "+ " + parseInt(nMidChar * nMultMidChar);
		}
		/* Point deductions for poor practices */
		if ((nAlphaLC > 0 || nAlphaUC > 0) && nSymbol === 0 && nNumber === 0) {  // Only Letters
			nScore = parseInt(nScore - nLength);
			nAlphasOnly = nLength;
			sAlphasOnly = "- " + nLength;
		}
		if (nAlphaLC === 0 && nAlphaUC === 0 && nSymbol === 0 && nNumber > 0) {  // Only Numbers
			nScore = parseInt(nScore - nLength); 
			nNumbersOnly = nLength;
			sNumbersOnly = "- " + nLength;
		}
		if (nRepChar > 0) {  // Same character exists more than once
			nScore = parseInt(nScore - (nRepChar * nRepChar));
			sRepChar = "- " + nRepChar;
		}
		if (nConsecAlphaUC > 0) {  // Consecutive Uppercase Letters exist
			nScore = parseInt(nScore - (nConsecAlphaUC * nMultConsecAlphaUC)); 
			sConsecAlphaUC = "- " + parseInt(nConsecAlphaUC * nMultConsecAlphaUC);
		}
		if (nConsecAlphaLC > 0) {  // Consecutive Lowercase Letters exist
			nScore = parseInt(nScore - (nConsecAlphaLC * nMultConsecAlphaLC)); 
			sConsecAlphaLC = "- " + parseInt(nConsecAlphaLC * nMultConsecAlphaLC);
		}
		if (nConsecNumber > 0) {  // Consecutive Numbers exist
			nScore = parseInt(nScore - (nConsecNumber * nMultConsecNumber));  
			sConsecNumber = "- " + parseInt(nConsecNumber * nMultConsecNumber);
		}
		if (nSeqAlpha > 0) {  // Sequential alpha strings exist (3 characters or more)
			nScore = parseInt(nScore - (nSeqAlpha * nMultSeqAlpha)); 
			sSeqAlpha = "- " + parseInt(nSeqAlpha * nMultSeqAlpha);
		}
		if (nSeqNumber > 0) {  // Sequential numeric strings exist (3 characters or more)
			nScore = parseInt(nScore - (nSeqNumber * nMultSeqNumber)); 
			sSeqNumber = "- " + parseInt(nSeqNumber * nMultSeqNumber);
		}
		/* Determine if mandatory requirements have been met and set image indicators accordingly */
		var arrChars = [nLength,nAlphaUC,nAlphaLC,nNumber,nSymbol];
		var arrCharsIds = ["nLength","nAlphaUC","nAlphaLC","nNumber","nSymbol"];
		var arrCharsLen = arrChars.length;
		nRequirements = nReqChar;
		if (pwd.length >= nMinPwdLen) { var nMinReqChars = 3; } else { var nMinReqChars = 4; }
		if (nRequirements > nMinReqChars) {  // One or more required characters exist
			nScore = parseInt(nScore + (nRequirements * 2)); 
			sRequirements = "+ " + parseInt(nRequirements * 2);
		}
		/* Determine if additional bonuses need to be applied and set image indicators accordingly */
		var arrChars = [nMidChar,nRequirements];
		var arrCharsIds = ["nMidChar","nRequirements"];
		var arrCharsLen = arrChars.length;
		/* Determine if suggested requirements have been met and set image indicators accordingly */
		var arrChars = [nAlphasOnly,nNumbersOnly,nRepChar,nConsecAlphaUC,nConsecAlphaLC,nConsecNumber,nSeqAlpha,nSeqNumber];
		var arrCharsIds = ["nAlphasOnly","nNumbersOnly","nRepChar","nConsecAlphaUC","nConsecAlphaLC","nConsecNumber","nSeqAlpha","nSeqNumber"];
		var arrCharsLen = arrChars.length;
		/* Determine complexity based on overall score */
		if (nScore > 100) { nScore = 100; } else if (nScore < 0) { nScore = 0; }
		if (nScore >= 0 && nScore < 20) { sComplexity = (site_lang=="pl" ? "Bardzo słabe" : "Very Weak"); }
		else if (nScore >= 20 && nScore < 40) { sComplexity = (site_lang=="pl" ? "Słabe" : "Weak"); }
		else if (nScore >= 40 && nScore < 60) { sComplexity = (site_lang=="pl" ? "Dobre" : "Good"); }
		else if (nScore >= 60 && nScore < 80) { sComplexity = (site_lang=="pl" ? "Silne" : "Strong"); }
		else if (nScore >= 80 && nScore <= 100) { sComplexity = (site_lang=="pl" ? "Bardzo silne" : "Very Strong"); }
	}
	/* Display updated score criteria to client */
	$('checkPassRes').set('html', (site_lang=="pl" ? 'Siła hasła' : 'Password strength')+': <strong>'+sComplexity+'</strong>');
	
}
