/*
	forum.js
*/

var secimgurl = root_url + 'secimg/forumraport/';
var secimgcomment = root_url + 'secimg/forum/';

function raportAs(e, t, url){
	if(!userlogged){
		if(ajaxmode){
			activeElement.obj = t;
			activeElement.url = url;
			var code = createConfirmCode(url, submitRaport);
		}
		else{
			var code = createConfirmCode(url);
		}
		if(code){
			t.parentNode.appendChild(code);
			var pos = findPos(t);
			code.style.left = ((pos[0]+t.offsetWidth)-code.offsetWidth) + 'px';
			code.style.visibility = 'visible';
			document.getElementById('raportcode').focus();
			if(ajaxmode){
				activeElement.top = pos[1];
				activeElement.left = ((pos[0]+t.offsetWidth)-code.offsetWidth);
			}
		}
	}
	else{
		if(ajaxmode){
			activeElement.obj = t;
			activeElement.url = url;
			submitRaport(e);
			/*var rez = execRequest(url);
			if(typeof(rez.responseXML.documentElement)!='undefined'){
				var rezult = getValue(rez.responseXML.documentElement, 'rezult');
				if(rezult != 1){
					errorAlert(getValue(rez.responseXML.documentElement, 'msg'));
				}
			}*/
		}
		else{
			location.href = url;
		}
	}
}

function removeConfirmCode(){
	viewForm = false;
	var obj = document.getElementById('confirmSecCode');
	if(obj){
		obj.parentNode.removeChild(obj);
		if(ajaxmode){
			resetActivElement();
		}
	}
}

function createConfirmCode(url){
	if(!viewForm){
		viewForm = true;
	var c = document.createElement('div');
	c.className = 'confirmCode';
	
	var cd = document.createElement('div');
	cd.className = 'closeConfirm';
	c.appendChild(cd);
	var imgc = new Image();
	imgc.src = closeimg;
	imgc.onclick = function(){
		removeConfirmCode();
	};
	cd.appendChild(imgc);
	
	var p = document.createElement('p');
	p.className = 'codeText';
	p.innerHTML = codeConfirmText;
	c.appendChild(p);
	
	var cb = document.createElement('div');
	cb.className = 'codeBlock';
	c.appendChild(cb);
	var img = new Image();
	var now = new Date();
	img.src = secimgurl + now.getTime() + '.png';
	cb.appendChild(img);
	var inp = document.createElement('input');
	inp.type = 'text';
	inp.name = 'raportcode';
	inp.id = 'raportcode';
	inp.maxLength = "4";
	cb.appendChild(inp);
	var cs = document.createElement('div');
	cs.className = 'codeSubmit';
	c.appendChild(cs);
	
	/*var clb = document.createElement('input');
	clb.className = 'closeCode';
	clb.type = 'button';
	clb.value = ' ';
	clb.onclick = function(){
		removeConfirmCode();
	};
	cs.appendChild(clb);*/
	
	var btn = document.createElement('input');
	btn.className = 'submitCode';
	btn.type = 'submit';
	btn.value = codeConfirmButton;
	cs.appendChild(btn);
	
	var all = document.createElement('div');
	all.id = 'confirmSecCode';
	
	var f = document.createElement('form');
	f.setAttribute('action', url);
	f.setAttribute('method', "post");
	if(arguments[1]){
		if(typeof(arguments[1]) == 'function'){
			addEvent(f, 'submit', arguments[1]);
		}
	}
	all.appendChild(f);
	f.appendChild(c);
	
	if(ajaxmode){
		activeElement.form = f;
	}
	
	return all;
	}
	return null;
}

function commentForm(f){
	if(!uplfile){
	var flds = new Array('comment');
	if(!userlogged){
		flds.push('securecode');
		flds.push('author');
	}
	var err = false;
	var elist = new Array();
	for(var i=0; i<flds.length; i++){
		if(f.elements[flds[i]].value == ''){
			f.elements[flds[i]].focus();
			elist.push(formErrors[flds[i]]);
			 err = true;
		}
	}
	if(err){
		if(ajaxmode){
			var pos = findPos(f);
			errorAlert(elist.join("<br/>"), pos[0], pos[1]);
		}
		else{
			alert(elist.join("\n"));
		}
	}
	else{
		if(ajaxmode){
			for(var j=0; j<f.elements.length; j++){
				if(f.elements[j].type == 'checkbox' && /^uploadedfiles\[[0-9]+\]/.test(f.elements[j].name) && f.elements[j].checked){
					flds.push(f.elements[j].name);
				}
			}
			submitComment(f, flds);
		}
		else{
			return true;
		}
	}
	}
	return false;
}

function topicForm(f){
	var flds = new Array('subject');
	if(!userlogged){
		flds.push('securecode');
		flds.push('author');
	}
	var err = false;
	var elist = new Array();
	for(var i=0; i<flds.length; i++){
		if(f.elements[flds[i]].value == ''){
			f.elements[flds[i]].focus();
			elist.push(formErrors[flds[i]]);
			 err = true;
		}
	}
	if(err){
		alert(elist.join("\n"));
	}
	else{
		return true;
	}
	return false;
}

var blank_url = root_url+'application/site/static/blank.html';
var attachments = 0;
var viewForm = false;
var uplfile = false;

//var tba = false;

function addFileUpload(obj){
	
	if(!viewForm){
	if(maxattachments>attachments){
	viewForm = true;
	//document.getElementById('forframes').innerHTML += '<iframe name="flu" src="?niekas"/>';
	var now = new Date();
	var fid = 'fid'+now.getTime();
	var tid = 'target'+now.getTime();
	
	var fu = document.createElement('div');
	fu.className = 'fileUpload';
	fu.id = 'c_'+fid;
	document.body.appendChild(fu);
	
	fu.innerHTML = '<div class="closeConfirm"><img src="'+closeimg+'" alt="" onclick="removeUplfile(\''+fid+'\');"/></div><div class="uplText">'+upload_text+'</div><div class="uplTarget"><iframe name="'+tid+'" src="'+blank_url+'" onload="loadedFile(\''+fid+'\', this);" style="height: 1px;"/></div>';
	
	var fc = document.createElement('div');
	fc.className = 'uploadForm';
	fu.appendChild(fc);
	
	var f = document.createElement('form');
	f.action = uplurl;//'?fid='+fid+';';
	//f.method = "post";
	f.setAttribute('method', "post");
	//f.enctype = "multipart/form-data";
	f.setAttribute('enctype', "multipart/form-data");
	
	f.target = tid;
	f.onsubmit = function(){submitForm(f, fid);};
	fc.appendChild(f);
	if(BrowserDetect.id == 'ie' || BrowserDetect.id == 'op'){
		var ut = document.createElement('input');
		ut.type = "text";
		ut.disabled = true;
		ut.className = 'attdis';
		ut.size = "12";
		f.appendChild(ut);
	}
	
	var att = document.createElement('input');
	att.type = "file";
	att.name = "atachfile";
	att.className = 'attInp';
	//att.title = 'a';
	//att.readOnly = true;
	//att.disabled = true;
	//att.onclick = function(){this.blur();};
	att.size = "12";
	att.onchange = function(e){submitForm(f, fid);};
	//att.relatedElement = ut;
	//att.onkeydown = function(e){tba=true; this.blur();};
	//att.onchange = function(e){if(!tba){alert('laisva');}else{alert('uzimta');}};
	f.appendChild(att);
	
	
	
	var pos = findPos(obj);
	fu.style.top = (pos[1]+obj.offsetHeight) + 'px';
	fu.style.left = ((pos[0]+obj.offsetWidth)-fu.offsetWidth) + 'px';
	fu.style.visibility = 'visible';
	
	//f.target = window.top.frames['flu'];//"flu";
	}
	else{
		if(ajaxmode){
			var pos = findPos(obj);
			errorAlert(formErrors['maxattachments'], pos[0], pos[1]);
		}
		else{
			alert(formErrors['maxattachments']);
		}
	}
	}
}

function submitForm(f, id){
	//window.frames['flu'].window.onload = function(){alert('kuku');};
	//alert(f.getAttribute("target"));
	//alert(frames["flu"]);
	//f.target = window.frames['flu'];
	document.getElementById('c_'+id).style.visibility = 'hidden';
	viewForm = false;
	var div = document.createElement('div');
	var loa = loading_text;
	if(f.elements.atachfile){
		//loa = f.elements.atachfile.value.replace(/([\\\/]?([^\\\/]+)?\.[a-zA-Z0-9]{1,5})$/, '$1');
		if(/[\\\/]/.test(f.elements.atachfile.value)){
			loa = f.elements.atachfile.value.replace(/^.+?[\\\/]?([^\\\/]+?\.[a-zA-Z0-9]{1,5})$/, '$1');
		}
		else{
			loa = f.elements.atachfile.value;
		}
	}
	div.innerHTML = '<div id="'+id+'" class="loadingfile"><img src="'+loading_src+'" alt="" border="0"/><span> '+loa+'</span></div>';
	document.getElementById('files_list').appendChild(div);
	uplfile = true;
	attachments++;
	f.submit();
}

function loadedFile(id, obj){
	if(window.frames[obj.name].location.href != obj.src){
		var cont = window.frames[obj.name].document.body.innerHTML;
		if(window.frames[obj.name].document.getElementById('iserror')){
			attachments--;
		}
		document.getElementById(id).innerHTML = cont;
		document.getElementById(id).className = '';
		uplfile = false;
		setTimeout(function(){document.body.removeChild(document.getElementById('c_'+id));}, 200);
	}
}

function removeUplfile(id){
	viewForm = false;
	setTimeout(function(){document.body.removeChild(document.getElementById('c_'+id));}, 100);
}

function subscribe(e, t, url){
	if(!viewForm){
		viewForm = true;
		var pos = findPos(t);
		
		
		var c = document.createElement('div');
		c.className = 'subscribeForm';
		//c.id = 'subscF';
		
		
		
		
		var cd = document.createElement('div');
		cd.className = 'closeConfirm';
		c.appendChild(cd);
		var imgc = new Image();
		imgc.src = closeimg;
		imgc.onclick = function(){
			removeSubscribeForm();
		};
		cd.appendChild(imgc);
		
		var p = document.createElement('p');
		p.className = 'codeText';
		p.innerHTML = subcriptionText;
		c.appendChild(p);
		
		var mb = document.createElement('div');
		mb.className = 'mailBlock';
		//mb.innerHTML = '<span>'+subscriptionEmail+'</span>';
		c.appendChild(mb);
		
		var inpm = document.createElement('input');
		inpm.type = 'text';
		inpm.name = 'email';
		if(subscriptionEmail){
			inpm.value = subscriptionEmail;
		}
		mb.appendChild(inpm);
		
		if(!userlogged){
			var p = document.createElement('p');
			p.className = 'codeText';
			p.innerHTML = codeConfirmText;
			c.appendChild(p);
			var cb = document.createElement('div');
			cb.className = 'codeBlock';
			c.appendChild(cb);
			var img = new Image();
			var now = new Date();
			img.src = secimgurl + now.getTime() + '.png';
			cb.appendChild(img);
			var inp = document.createElement('input');
			inp.type = 'text';
			inp.name = 'raportcode';
			inp.id = 'raportcode';
			cb.appendChild(inp);
		}
		
		var cs = document.createElement('div');
		cs.className = 'codeSubmit';
		c.appendChild(cs);
		
		var btn = document.createElement('input');
		btn.className = 'submitCode';
		btn.type = 'submit';
		btn.value = subscribeButton;
		cs.appendChild(btn);
		
		
		var all = document.createElement('div');
		all.id = 'subscribePop';
		t.parentNode.appendChild(all);
		
		var pl = ((pos[0]+t.offsetWidth)-all.offsetWidth);
		var pt = pos[1];
		
		var f = document.createElement('form');
		//f.action = submitSubscribe(f, url, pl, pt);
		f.setAttribute('action', 'javascript: submitSubscribe("'+url+'", '+pl+', '+pt+');');
		f.setAttribute('method', "post");
		
		/*var f = document.createElement('form');
		f.setAttribute('action', url);
		f.setAttribute('method', "post");
		if(arguments[1]){
			if(typeof(arguments[1]) == 'function'){
				addEvent(f, 'submit', arguments[1]);
			}
		}*/
		all.appendChild(f);
		f.appendChild(c);
		
		
		all.style.left = pl + 'px';
		all.style.visibility = 'visible';
	}
	
}

function removeSubscribeForm(){
	var obj = document.getElementById('subscribePop');
	if(obj){
		obj.parentNode.removeChild(obj);
	}
	viewForm = false;
}

function submitSubscribe(url, pl, pt){
	var c = document.getElementById('subscribePop');
	var f = c.getElementsByTagName('form')[0];
	if(ajaxmode){
		var post = new Array();
		//var pos = findPos(f.parentNode);
		for(var i=0; i<f.elements.length; i++){
			if(typeof(f.elements[i].value) != 'undefined' && typeof(f.elements[i].name) != 'undefined' && f.elements[i].name!=''){
			post.push(f.elements[i].name + '=' + f.elements[i].value);
			}
		}
		c.style.visibility = 'hidden';
		rez = execPost(url, post);
		removeSubscribeForm();
		if(rez.responseXML && typeof(rez.responseXML.documentElement)!='undefined'){
			var result = getValue(rez.responseXML.documentElement, 'result');
			if(getValue(rez.responseXML.documentElement, 'msg') != ''){
				errorAlert(getValue(rez.responseXML.documentElement, 'msg'), pl, pt);
			}
			
		}
	}
	else{
		f.action = url;
		f.submit();
	}
}

function checkLength(t){
	if((t.value.length)>max_form_text){
		t.value=t.value.substring(0,max_form_text);
		if(ajaxmode){
			errorAlert(formErrors['max_form_text_length']);
		}
		else{
			alert(formErrors['max_form_text_length']);
		}
	}
}

function trigerTopic(){
	var obj = document.getElementById('topic_add_form');
	if(obj){
		if(arguments[0]){
			obj.style.display = arguments[0];
		}
		else{
			obj.style.display = (obj.style.display == 'block') ? 'none': 'block';
		}
	}
}

function trigerRules(){
	var obj = document.getElementById('rulesBlock');
	if(obj){
		if(arguments[0]){
			obj.className = arguments[0];
		}
		else{
			obj.className = (obj.className == 'openrule') ? 'closerule': 'openrule';
		}
	}
}

