var events = false;
if (typeof document.addEventListener != "undefined") {
    events = 'w3c';
}else if (typeof document.attachEvent != "undefined") {
    events = 'ie';
}

	window.onload = function(){
	    enterKeyInputPassword();
	    checkLink();
	}

	function showEl(el){
	    var e = document.getElementById(el);
	    if (e.style.display == 'none'){
	        e.style.display = '';
	    }else{
	        e.style.display = 'none';
	    }
	}

	function limit(item, limit){
	    if (item.value.length >= limit){
	        item.value = item.value.slice(0, limit);
	    }
	}


	function logIn(){
	    var f = document.getElementById('loginForm');
	    if (!(f)){
	        f = document.getElementById('MainLoginForm');
	    }
	    if (f){
	        if (f.login.value != '' && f.pass.value != ''){
	            f.submit();
	        }else{
	            window.location="login.php";
	        }
	    }else{
	            window.location="login.php";
	    }
	}

	function enterKeyInputPassword(){
	    var inputs = document.getElementsByTagName("input");
	    for (var i = 0; i < inputs.length; i++){
	        if (inputs[i].type == "password"){
	            if (events == 'w3c'){
	                inputs[i].addEventListener("keypress", enterKeyInputPasswordSubmit, false);
	            }else if (events == 'ie'){
	                inputs[i].attachEvent("onkeypress", enterKeyInputPasswordSubmit);
	            }
	        }
	    }
	}

	function enterKeyInputPasswordSubmit(e){
	    if (!e) { var e = window.event;}
	
	    if (e.keyCode) {
	        var code = e.keyCode;
	    }else if (e.which) {
	        var code = e.which;
	    }
	    if (code == 13){
	        if (events == 'w3c'){
	            this.form.submit();
	        }else if (events == 'ie'){
	            window.event.srcElement.form.submit();
	        }
	    }
	}

	function checkLink(){
	    var links = document.getElementsByTagName("a");
	    for(var i = 0; i < links.length; i++){
	        switch (links[i].rel){
	            case 'Prev':
	                links[i].innerHTML = '&laquo; '+ links[i].innerHTML;
	                break;
	
	            case 'ZoomImage':
	                links[i].onclick = function (){
	                    showImage(this.getAttribute('image'));
	                    return false;
	                }
	                links[i].setAttribute('image', links[i].href.substr(links[i].href.indexOf('=')+1));
	                links[i].href="#";
	                break;
	        }
	    }
	}


	function help(topic){
	    var helpWindow = window.open('help.php?DOC='+topic, 'help', 'left=100, top=100, width=400, height=600');
	}

	function showImage(image){
	   var imageWindow = window.open('image.php?image='+image, 'image', 'left=100, top=100, width=400, height=600');
	}

	function chat(login){
		if (typeof login == 'undefined'){ login = ''; }
		var win = window.open('chat/index.html', 'chat', 'left=100, top=100, width=700, height=400');
	}

	function inviteToChat(item){
		var l = item.href.split('=');
		chat(l[l.length-1]);
	}

	function joinChat(room){
		var win = window.open('chat.php?room='+room, 'chat', 'left=100, top=100, width=700, height=400');
	}

	function simplesearchSubmit(){
		var f = document.getElementById('simplesearch');
		if (f.searchType[f.searchType.selectedIndex].value != ''){
			f.submit();
			return true;
		}
	
		if (f.searchCounry[f.searchCounry.selectedIndex].value != ''){
			f.submit();
			return true;
		}
	
		if (f.ageFrom.value != '' || f.ageTo.value != ''){
			f.submit();
			return true;
		}

		if (f.onlyPhotos.checked == true){
			f.submit();
			return true;
		}
		alert(translate('SearchEnterAtLeasOneCondition'));
	}

    function abuseFormSubmit(){
        
		var f = document.getElementById('abuseForm');        
        
		if (f.email.value != ''){
            reEmail=/.+\@.+\...+/;
            if (reEmail.test(f.email.value) != true || f.email.value.length < 6){
                alert(translate('AbuseEmail')); 
                return false;
            }
        }
        
        f.submit();
    }

    function addPhotoFormSubmit(){
        var f = document.getElementById('addPhotoForm');
        var status = true;
        
        if (f.title.value == ''){
            alert( translate('PhotosEnterTitle'));
            status = false;
            return false;
        }
        
        if (f.description.value == ''){
            alert(translate('PhotosEnterDesc'));
            status = false;
            return false;
        }
        
        if (f.Image1.value == ''){
            alert(translate('PhotosSelectPhoto'));
            status = false;
            return false;
        }        
        
        if (status == true){
            f.submit();
        } 
    }
    
    function addHumor(){
        var f = document.getElementById('CommentForm');
        var status = true;
   
        if (f.title.value == ''){
            f.title.focus();
            alert(translate('HumorEnterTitle'));
            status = false;
            return false;
        }

        if (f.catID[f.catID.selectedIndex].value == 'null' || f.catID[f.catID.selectedIndex].value == ''){
            alert(translate('HumorSelectCategory'));
            status = false;
            return false;
        }
        
        if (f.Body.value == ''){
            f.Body.focus();
            alert(translate('HumorEnterContent'));
            status = false;
            return false;
        }
 
        if (status == true){
            f.submit();
        }
    }

    function humorAddCat(type, cat, row){
        var t = document.getElementById(type+'Cats');        
        document.getElementsByName('type').item(0).value = type;
        
        //check with cat to add ... main or sub 
        if (cat == null){
            var name = 'newMainCat';
        }else{
            var name = 'newSubCat';
            document.getElementsByName('catID').item(0).value = cat;
        }
        
        //check if input for name exist if yes - submit form
        if (document.getElementsByName('newMainCat').item(0) || document.getElementsByName('newSubCat').item(0)){
            if (document.getElementsByName(name).item(0).value != ''){ 
                document.getElementById('catsForm').type.value = '';
                document.getElementById('catsForm').submit();
            }
        //create 
        }else{
            //insert row at the end of table
            if (cat == null){
                var i = t.rows.length;
                t.insertRow(i);
            //insert row after main cat
            }else{
                var i = row.parentNode.parentNode.rowIndex+1;
                t.insertRow(i);
            }
            
            var td = document.createElement('td');
            t.rows[i].appendChild(td);
    
            if (events == 'ie'){
                var inp = document.createElement('<input NAME="'+name+'">');
            }else{
                var inp = document.createElement('input');
                inp.name = name;    
            }
            td.appendChild(inp);
            inp.focus();
        }
    }
    
    function humorDelCat(catID){
        if (confirm(translate('HumorQDeleteCategory'))){
            var f = document.getElementById('catsForm');
            f.ACT.value = 'delCat';
            f.catID.value = catID;
            f.submit();
        }
    }

    function sendToFriendSubmit(){
        var f = document.getElementById('sendToFriend');
        var status = true;
        reEmail=/.+\@.+\...+/;

        if (f.mail.value == ''){ alert(translate('SendToFriendEnterTo')); status = false; return false}
        if (reEmail.test(f.mail.value) !=true || f.mail.value.length < 6) { alert(translate('EmailNotValid')); status = false; return false;}
        
        if (status == true){
            f.submit();   
        }
    }
    
    function addMovieFormSubmit(){
        var f = document.getElementById('addMovieForm');
        var status = true;
        
        if (f.title.value == ''){
            alert(translate('MoviesEnterTitle'));
            status = false;
            return false;
        }
        
        if (f.description.value == ''){
            alert(translate('MoviesEnterDesc'));
            status = false;
            return false;
        }
        
        if (f.Movie.value == ''){
            alert(translate('MoviesSelectMovie'));
            status = false;
            return false;
        }        
        
        
        if (status == true){
            f.submit();
        } 
    }
    
    function friendDelUser(login){
        if(confirm(translate('FOFQDelFriend', login))){
            window.location.href="mycontacts.php?ACT=removeFriend&login="+login;
        }
    }
    
    function foeDelUser(login){
        if(confirm(translate('FOFQDelFoe', login))){
            window.location.href="mycontacts.php?ACT=removeFoe&login="+login;
        }
    }   
    function newPassFormSubmit(){
        var f = document.getElementById('newPassForm');
        var status = true;
        
        if (f.pass1.value == ''){
            alert(translate('NewPassEnterPassword'));
            return false;
        }
        
        if (f.pass1.value == ''){
            alert(translate('NewPassRepeatPassword'));
            return false;
        }
        if (f.pass1.value != f.pass2.value){
            alert(translate('PasswordMissmath'));
            return false;
        }
        if (f.pass3 && f.pass4){        
            if (f.pass3.value == ''){
                alert(translate('NewPassEnterPassword'));
                return false;
            }
            if (f.pass4.value == ''){
                alert(translate('NewPassRepeatPassword'));
                return false;
            }
            if (f.pass3.value != f.pass4.value){
                alert(translate('PasswordMissmath'));
                return false;
            }
        }
        if (status == true){
            f.submit();
        }
    }
    
    function addSoundFormSubmit(){
        var f = document.getElementById('addSoundForm');
        var status = true;
        
        if (f.title.value == ''){
            status = false;
            alert(translate('SoundsEnterTitle'));
            return false;
        }
        
        if (f.description.value == ''){
            status = false;
            alert(translate('SoundsEnterDesc'));
            return false;
        }
        
        if (f.Sound.value == ''){
            status = false;
            alert(translate('SoundsSelectFile'));
            return false;
        }
        
        if (status == true){
            f.submit();
        }
    }
    
    function addStory(){
        var f = document.getElementById('CommentForm');
        var status = true;
   
        if (f.title.value == ''){
            f.title.focus();
            alert(translate('StoriesEnterTitle'));
            status = false;
            return false;
        }

        if (f.description.value == ''){
            if (!confirm(translate('StoriesQEmptyDesc'))){
                status = false;
                return false;                            
            }
        }
        
        if (f.catID[f.catID.selectedIndex].value == 'null' || f.catID[f.catID.selectedIndex].value == ''){
            alert(translate('StoriesSelectCategory'));
            status = false;
            return false;
        }
        
        if (f.Body.value == ''){
            f.Body.focus();
            alert(translate('StoriesEnterContent'));
            status = false;
            return false;
        }
 
        if (status == true){
            f.submit();
        }
    }

    function previewStory(){
        var f = document.getElementById('CommentForm');
        f.ACT.value = 'addPhoto_2';
        f.submit();
    }
    
    function profilePrewiev(){
    	var helpWindow = window.open('popup.php?ACT=profile_preview', 'preview', 'left=100, top=100, width=800, height=700, scrollbars=yes');
    }
    
    function ShowInProgressDiv(){
    	document.getElementById('inProgressDiv').style.display = 'block';
    }
	function przekierowanie(odnosnik) {
		window.location.href = odnosnik;
		return false;
	}
	/**
 * @author Slawomir Koklowski {@link http://www.kurshtml.boo.pl}
 * @copyright NIE usuwaj tego komentarza! (Do NOT remove this comment!)
 */

function Menu(id, style, otworz, wysun, czasRozwin, czasZwin, czasOtworz, czasZamknij, nieInicjalizuj)
{
	if (typeof czasRozwin == 'undefined' || czasRozwin < 0) czasRozwin = 25;
	if (typeof czasZwin == 'undefined' || czasZwin < 0) czasZwin = 25;
	if (typeof czasOtworz == 'undefined' || czasOtworz < 0) czasOtworz = 250;
	if (typeof czasZamknij == 'undefined' || czasZamknij < 0) czasZamknij = 500;
	
	var url = unescape(window.location.href.replace(/#.*/, ''));
	var base = window.location.protocol + '//' + window.location.host + window.location.pathname.replace(/[^\/\\]+$/, '');
	
	if (style)
	{
		if (style.indexOf(':') < 0)
		{
			document.getElementById(id).className += ' ' + style;
		}
		else
		{
			style = style.replace(/(^\s+|(\s|;)+$)/g, '').split(/\s*;\s*/);
			for (var i = 0; i < style.length; i++)
			{
				style[i] = style[i].split(/\s*:\s*/);
				for (var j = 0, c, property = ''; j < style[i][0].length; j++)
				{
					c = style[i][0].charAt(j);
					property += c == '-' ? style[i][0].charAt(++j).toUpperCase() : c.toLowerCase();
				}
				eval('document.getElementById("' + id + '").style.' + property + ' = "' + style[i][1].replace(/"/g, '\\"') + '"');
			}
		}
	}
	
	for (var i = 0; i < document.getElementById(id).getElementsByTagName('dt').length; i++)
	{
		var dd = new Array();
		var el = document.getElementById(id).getElementsByTagName('dt')[i].nextSibling;
		var nodeName;
		while (el && (nodeName = el.nodeName.toLowerCase()) != 'dt')
		{
			if (nodeName == 'dd')
			{
				el._dt = document.getElementById(id).getElementsByTagName('dt')[i];
				if (otworz)
				{
					el.onmouseover = function()
					{
						clearTimeout(this._dt._timoutID);
						this._dt._displayed = false;
						this._dt.onclick();
					}
					el.onmouseout = function()
					{
						clearTimeout(this._dt._timoutID);
						var dt = this._dt;
						this._dt._timoutID = setTimeout(function () { dt._displayed = true; dt.onclick(); }, czasZamknij);
					};
				}
				dd[dd.length] = el;
			}
			el = el.nextSibling;
		}
		document.getElementById(id).getElementsByTagName('dt')[i]._dd = dd;
		document.getElementById(id).getElementsByTagName('dt')[i]._timoutID = null;
		document.getElementById(id).getElementsByTagName('dt')[i]._displayed = false;
		document.getElementById(id).getElementsByTagName('dt')[i].onclick = function()
		{
			clearTimeout(this._timoutID);
			if (!this._displayed)
			{
				var el = this.parentNode.getElementsByTagName('dt')[0];
				while (el)
				{
					if (el.nodeName.toLowerCase() == 'dt' && el != this)
					{
						el._displayed = false;
						if (czasZwin) display(el, 0);
						else display(el);
					}
					el = el.nextSibling;
				}
			}
			this._displayed = !this._displayed;
			if (this._displayed && czasRozwin || !this._displayed && czasZwin) display(this, 0);
			else display(this);
		};
		if (otworz)
		{
			document.getElementById(id).getElementsByTagName('dt')[i].onmouseover = function()
			{
				clearTimeout(this._timoutID);
				var dt = this;
				this._timoutID = setTimeout(function () { dt._displayed = false; dt.onclick(); }, czasOtworz);
			};
			document.getElementById(id).getElementsByTagName('dt')[i].onmouseout = function()
			{
				clearTimeout(this._timoutID);
				var dt = this;
				this._timoutID = setTimeout(function () { dt._displayed = true; dt.onclick(); }, czasZamknij);
			};
		}
	}
	
	start(document.getElementById(id).getElementsByTagName('dt')[0]);
	
	function start(dt)
	{
		var hide = true;
		var el = dt;
		while (el)
		{
			var nodeName = el.nodeName.toLowerCase();
			if (nodeName == 'dt')
			{
				dt = el;
				hide = true;
			}
			if (nodeName == 'dt' || nodeName == 'dd')
			{
				if (!nieInicjalizuj && el.getElementsByTagName('a').length)
				{
					var active = el.getElementsByTagName('a')[0].href && unescape(el.getElementsByTagName('a')[0].href.replace(/#.*/, '')) == url;
					if (!active)
					{
						var rel = el.getElementsByTagName('a')[0].getAttribute('rel');
						if (rel)
						{
							var matches = (' ' + rel + ' ').match(/\s+Collection\(([^)]+)\)\s+/i);
							if (matches)
							{
								matches = matches[1].split(',');
								for (var k = 0, pos = -1; k < matches.length; k++)
								{
									if (matches[k].charAt(0) == '[' && (pos = matches[k].lastIndexOf(']')) > 0)
									{
										if (new RegExp(unescape(matches[k].substring(1, pos)), matches[k].substring(pos + 1)).test(url))
										{
											active = true;
											break;
										}
									}
									else
									{
										if (/^[\/\\]/.test(matches[k])) matches[k] = window.location.protocol + '//' + window.location.host + matches[k];
										else if (!/^[a-z0-9]+:/i.test(matches[k])) matches[k] = base + matches[k];
										if (unescape(matches[k].replace(/[\/\\]\.([\/\\])/g, '$1').replace(/[^\/\\]+[\/\\]\.\.[\/\\]/g, '').replace(/#.*/, '')) == url)
										{
											active = true;
											break;
										}
									}
								}
							}
						}
					}
					if (active)
					{
						el.className = (el.className ? el.className + ' ' : '') + 'active';
						dt._displayed = true;
						display(dt);
						hide = false;
						var el_parentNode = el.parentNode;
						while (el_parentNode != document.getElementById(id))
						{
							if (el_parentNode.nodeName.toLowerCase() == 'dd')
							{
								var el_sibling = el_parentNode.previousSibling;
								while (el_sibling)
								{
									if (el_sibling.nodeName.toLowerCase() == 'dt')
									{
										el_sibling._displayed = true;
										display(el_sibling)
										break;
									}
									el_sibling = el_sibling.previousSibling;
								}
							}
							el_parentNode = el_parentNode.parentNode;
						}
					}
				}
			}
			if (nodeName == 'dd')
			{
				if (hide) el.style.display = 'none';
				start(el.getElementsByTagName('dt')[0]);
			}
			el = el.nextSibling;
		}
	}
	
	function display(dt, i)
	{
		if (typeof i == 'undefined')
		{
			for (var i = 0; i < dt._dd.length; i++)
			{
				dt._dd[i].style.display = dt._displayed ? 'block' : 'none';
				if (!dt._displayed)
				{
					for (var j = 0; j < dt._dd[i].getElementsByTagName('dt').length; j++)
					{
						dt._dd[i].getElementsByTagName('dt')[j]._displayed = false;
						display(dt._dd[i].getElementsByTagName('dt')[j]);
					}
				}
			}
		}
		else if (i < dt._dd.length)
		{
			var dir = wysun ? !dt._displayed : dt._displayed;
			var n = dir ? i : dt._dd.length - 1 - i;
			dt._dd[n].style.display = dt._displayed ? 'block' : 'none';
			if (!dt._displayed)
			{
				for (var j = 0; j < dt._dd[n].getElementsByTagName('dt').length; j++)
				{
					dt._dd[n].getElementsByTagName('dt')[j]._displayed = false;
					display(dt._dd[n].getElementsByTagName('dt')[j]);
				}
			}
			dt._timoutID = setTimeout(function() { display(dt, i + 1); }, dt._displayed ? czasRozwin : czasZwin);
		}
	}
}
var timeout	= 500;
var closetimer	= 0;
var ddmenuitem	= 0;

// open hidden layer
function mopen(id)
{	
	// cancel close timer
	mcancelclosetime();

	// close old layer
	if(ddmenuitem) ddmenuitem.style.display = 'none';

	// get new layer and show it
	ddmenuitem = document.getElementById(id);
	ddmenuitem.style.display = 'block';

}
// close showed layer
function mclose()
{
	if(ddmenuitem) ddmenuitem.style.display = 'none';
}

// go close timer
function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

// close layer when click-out
document.onclick = mclose; 
