function el(e){return document.getElementById(e);}
function hide(e){el(e).style.display='none';}
function show(e){el(e).style.display='block';}
function showi(e){el(e).style.display='inline';}
function sw(sa,sb){hide(sa);show(sb);}
function swi(sa,sb){hide(sa);showi(sb);}
function ihel(e,h){e.innerHTML=h;}
function inner(e,h){el(e).innerHTML=h;}
function innerh(e,h){var a=el(e);a.innerHTML=h;a.style.display='none';}
function inners(e,h){var a=el(e);a.innerHTML=h;a.style.display='block';}
function innersi(e,h){var a=el(e);a.innerHTML=h;a.style.display='inline';}
function ce(e){return document.createElement(e);}
function sa(e,a,v){e.setAttribute(a,v);}
function val(e){return el(e).value;}
function sub(e){el(e).submit();}
function res(e){el(e).reset();}
var to_server=15000,alert_nc=true;

function sessExp() {alert_nc=false;alert('Сесията ви изтече!');}

function getReq(url,func){
	var _t=this;var moz;
	if (window.XMLHttpRequest){this.xmlreq=new XMLHttpRequest();moz=1;}
	else if (window.ActiveXObject){this.xmlreq=new ActiveXObject("Microsoft.XMLHTTP");moz=0;}
	else return false;
	this.xmlto=setTimeout((function() {_t.timeout()}),to_server);
	this.xmlreq.onreadystatechange=(function() {
		if (_t.xmlreq.readyState==4){
			clearTimeout(_t.xmlto);
			if (_t.xmlreq.status==200){
				alert_nc=true;
				var x=_t.xmlreq.responseXML;
				var r=x.getElementsByTagName("r")[0].firstChild.nodeValue;
				if (r==1) sessExp();
				else if (r==0) func.call(this,x);
					 else func.call(this, r);
			}
		}
	});
	this.xmlreq.open("GET",url,true);
	if (moz==1) this.xmlreq.send(null); else this.xmlreq.send();
}
getReq.prototype.timeout=function(){this.xmlreq.abort();}

function postReq(url,data,func){
	var _t=this;
	if (window.XMLHttpRequest) this.xmlreq=new XMLHttpRequest();
	else if (window.ActiveXObject) this.xmlreq=new ActiveXObject("Microsoft.XMLHTTP");
	else return false;
	this.xmlto=setTimeout((function() {_t.timeout()}),to_server);
	this.xmlreq.onreadystatechange=(function() {
		if (_t.xmlreq.readyState==4){
			clearTimeout(_t.xmlto);
			if (_t.xmlreq.status==200){
				alert_nc=true;
				var x=_t.xmlreq.responseXML;
				var r=x.getElementsByTagName("r")[0].firstChild.nodeValue;
				if (r==1) sessExp();
				else if (r==0) func.call(this,x);
			}
		}
	});
	this.xmlreq.open("POST",url,true);
	this.xmlreq.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
	this.xmlreq.send(data);
}
postReq.prototype.timeout=function(){this.xmlreq.abort();}

function clear(e){if (confirm('Наистина ли искаш да изтриеш написания от теб текст?')) el(e).value='';}

function esub(em,ev){
	var k;if (window.event) k=window.event.keyCode; else if (ev) k=ev.which; else return true;
	if (k==13) {em.form.submit();return false;} else return true;
}
function findPos(e){
	var l=0,t=0;if (e.offsetParent) {l=e.offsetLeft;t=e.offsetTop;while (e=e.offsetParent){l+=e.offsetLeft;t+=e.offsetTop;}}
	return {x:l,y:t};
}
function orderMenu(c,v,l){
	var i,e,s,f=true,m=ce('div'),n=ce('div');p=findPos(el(l));m.className='om';
	for (i in v) {e=ce('a');sa(e,'href',v[i].l);ihel(e,v[i].t);if (i%2==0) {if (f) f=false; else e.className='oms';}
		if (i==c) {s=ce('span');s.appendChild(e);n.appendChild(s);} else n.appendChild(e);}
	m.style.left=(p.x-2)+'px';m.style.top=(p.y-2)+'px';m.appendChild(n);el('content').appendChild(m);
}
function emptied(x) {
	inner('trash','<a href="/mail/folder.php?fid=250">Кошче</a> - 0/<span>0</span>');
	if (el('trash').className=='mc') window.location='/mail/folder.php?fid=250';
}
function emptyTrash(){new getReq('/xml/empty-trash.php',emptied);}
function mc(id,st) {el('m'+id).className='g'+st;}
function mn(id) {mc(id,el('cb'+id).checked?'m':'n');}
function getSel() {var i,s=new Array();for (i in cbids) if (el('cb'+cbids[i]).checked) s.push(cbids[i]); return s.join(',')}
function actSel(id,act) {
	var sel=getSel()
	if (sel=='') alert('Моля изберете поне едно съобщение!');
	else window.location='/mail/folder.php?fid='+id+'&act='+act+'&ids='+sel;
}
function allSel() {
	var i,s=new Array(),t=el('cb0').checked,st=(t?'gm':'gn');
	for (i in cbids) {el('cb'+cbids[i]).checked=t;el('m'+cbids[i]).className=st;}
}
function delBm(id){new getReq('/xml/del-bm.php?id='+id,reloadPage);}
function addLink(id){new getReq('/xml/link.php?add='+id,reloadPage);}
function removeLink(id){new getReq('/xml/link.php?remove='+id,reloadPage);}
function reloadPage(x){window.location.reload();}

function cheked(vars) {
	var status = document.getElementById('selectAll').checked;
	var field = document.getElementsByName(vars);
	var len = field.length;
	for (var i = 0; i < len; i++) {
		field[i].checked = status;
	}
}

function getfName(fname) {
	if (fname.length > 1 && fname != 'Име') {
		new getReq('/xml/getName.php?fname='+encodeURIComponent(fname), hintF);
	}
	else
		return false;
}

function getlName(lname) {
	var fname = el('fname').value;
	if (lname.length > 1 && fname.length > 1) {
		new getReq('/xml/getName.php?fname='+encodeURIComponent(fname)+'&lname='+encodeURIComponent(lname), hintL);
	}
	else
		return false;
}

function hintF(x) {
	inner('hintFname', x);
}

function hintL(x) {
	inner('hintLname', x);
}


function aC(w, c) {
	var elm = el(w);
	elm.value = c;
	inner('hintFname', '');
	inner('hintLname', '');
}

function clearFN(v) {
	var elm = el(v);
	if (elm.value == "Име" )		
		elm.value = '';
}

function clearLN(v) {
	var elm = el(v);
	if (elm.value == "Фамилия" )
	elm.value = '';
	var tmp = el('fname').value;
	if (tmp.length > 1 && tmp != 'Име')
		elm.readOnly = false;
	else
		alert('Първо въведете "Име"!');
}

function sSearch() {
	var fn = el('fname').value;
	var ln = el('lname').value;
	if (fn.length > 1 && fn != 'Име' && ln.length > 3 && ln != 'Фамилия')
		document.ngs.submit();
	else
		alert('Моля, попълнете полетата за "Име" и "Фамилия"!');
}

var win = null;
//popup window ;)
function PopUpWindow(mypage, myname, w, h, scroll, resizeable){
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  var settings  ='height='+h+',';
      settings +='width='+w+',';
      settings +='top='+wint+',';
      settings +='left='+winl+',';
      settings +='scrollbars='+scroll+',';
      settings +='resizable='+resizeable+',';
  win = window.open(mypage, myname, settings);
  if (win.opener == null) {  // for Nav 2.0x
      win.opener = self  // this creates and sets a new property
   }
  if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
}
