// the now standard browser sniffer class
function Browser(){
	this.dom=document.getElementById?1:0;
	this.ie=0;
	this.gecko=0;
	this.opera=0;
	this.ok=this.dom;
	this.platform = navigator.platform;

	this.version=-1;
	var ua=navigator.userAgent;
	var re=new RegExp("MSIE[/ ]([0-9]{1,}[\.0-9]{0,})");
	if (re.exec(ua)!=null)
		this.ie=1;
	var re=new RegExp("Gecko[/ ]([0-9]{1,}[\.0-9]{0,})");
	if (re.exec(ua)!=null)
		this.gecko=1;
	var re=new RegExp("Opera[/ ]([0-9]{1,}[\.0-9]{0,})");
	if (re.exec(ua)!=null)
		this.opera=1;
}

var browser = new Browser();

function oo_com(com,urlmethod)
{	_$Sv('c',com);
	document.oofrm.method=urlmethod;
	document.oofrm.submit();
}

function ooLoad(url)
{	window.location.replace(url); }

function print_report(what)
{	window.open('index.php?c=print'+what);
}

function switchhelp()
{	var help=document.getElementsByTagName('DIV');
	var i=0;
	while (help[i])
	{	if (help[i].className=='helphidden')
			help[i].className='helpshow';
		else if (help[i].className=='helpshow')
			help[i].className='helphidden';
		i++;
	}
}

function showerrmsg(errmsgid,condition)
{	errmsg=$(errmsgid);
	if (condition)
	{	errmsg.className='f-red'; return true;
	}
	else
	{	errmsg.className='hidden'; return false;
	}
}

function populateselect(selecttag,opts)
{	var i=0;
	sel=$(selecttag);
	while (opts[i])
	{	var opt=document.createElement('option');
		opt.setAttribute('value',opts[i].getAttribute('value'));
		var opttxt=document.createTextNode(opts[i].firstChild.nodeValue);// + ':' + opts[i].getAttribute('value'));
		opt.appendChild(opttxt);
		sel.appendChild(opt);
		i++;
	}
}

//get element by ID
function $(nm)
{	return document.getElementById(nm);
}


//get element value by ID
function $v(id)
{	return document.getElementById(id).value;
}

//get text content (data) by ID
function $d(id)
{	if (typeof(id)=='string') return document.getElementById(id).firstChild.data;
	else return id.firstChild.data;
}

//set text content (data) by ID
function $Sd(id,data)
{	if (typeof(id)=='string') document.getElementById(id).firstChild.data=data;
	else id.firstChild.data=data;
	return data;
}

//set element className by ID
function $Sc(id,value)
{	document.getElementById(id).className=value;
	return value;
}

//get value by form name
function _$v(nm)
{	return eval('document.oofrm.'+nm+'.value');
}

//set value by form name
function _$Sv(nm,val)
{	eval('obj=document.oofrm.'+nm+';');
	obj.value=val;
}

//gel elements by name attribute
function $nm(nm)
{	return document.getElementsByName(nm);
}

function checkemailaddress(addr)
{	if (addr==ANONYMOUS_USER_EMAIL)
		return 1;
	if ((atpos=addr.indexOf('@'))<1)
		return 0;
	if ((atpos1=addr.substr(atpos).lastIndexOf('.'))<1)
		return 0;
	if (addr.substr(atpos).substr(atpos1).length<2)
		return 0;
	return 1;
}

//attach document fragment to DOM tree coded as array
function cE(){
	var A = arguments;

	if(!cE.cache[A[0]]) cE.cache[A[0]]=document.createElement(A[0]);
	return cE.cache[A[0]].cloneNode(false);
}
cE.cache    = {};

function $DOM(A)
{	var aL = A.length, node, child, ref={}, bRef=false;

	if (aL>=1)
	{	node = cE(A[0]);
		if (aL>=2)
		{	for (var arg in A[1])
			{	if (arg.indexOf('on')==0)
				{	node[arg] = A[1][arg];
					node.setAttribute(arg, A[1][arg]);
				}
				else if (arg=='ref')
				{	ref[A[1][arg]] = node;
					ref['DOM']     = node;
					bRef           = true;
				}
				else
				{	if (arg=='style')
						node.style.cssText   = A[1][arg];
					else if (arg=='classname')
						node.className = A[1][arg];
					else
						node.setAttribute(arg, A[1][arg]);
				}
			}
		}
		for (var i=2; i<aL; i++)
		{	if (typeof(A[i])=='string')
				node.appendChild(document.createTextNode(A[i]));
			else
			{	child = $DOM(A[i]);
				if (child.DOM)
				{	bRef = true;
					for (n in child)
					{	if(n=='DOM')
							node.appendChild(child[n]);
						else
							ref[n] = child[n];
					}
					ref['DOM'] = node;
				}
				else
					node.appendChild(child);
			}
		}
		return bRef?ref:node;
	}
	return null;
}


//attach AJAX document fragment to DOM tree
function $AJAX2DOM(to,node,replace)
{	if (replace)
	{	var chNode=to.firstChild;
		while (chNode)
		{	to.removeChild(chNode);
			chNode=to.firstChild;
		}
	}
	if (node.nodeType==3) //text node
		to.appendChild(document.createTextNode(node.nodeValue));
	else
	{	var newnode=document.createElement(node.nodeName);
		var attrs=node.attributes;
		for (attr=0;attr<attrs.length;attr=attr+1)
		{	if (attrs.item(attr).name=='style')
				newnode.style.cssText=attrs.item(attr).value;
			else if (attrs.item(attr).name=='class')
				newnode.className=attrs.item(attr).value;
			else
				newnode.setAttribute(attrs.item(attr).name,attrs.item(attr).value)
		}
		var child=node.firstChild;
		while (child)
		{	$AJAX2DOM(newnode,child);
			child=child.nextSibling;
		}
		to.appendChild(newnode);
	}
	return to;
}


//capturing keystrokes in form input fields
var forminputsubmitcall='';

function setcall2input(funccall)
{	forminputsubmitcall=funccall;
}

function getKey(e)
{	var keyCode = (e.which) ? e.which : e.keyCode;
	if (keyCode==9 || keyCode==13)
		eval(forminputsubmitcall);
}

function getoffsetleft(element)
{	offset=0;
	el=element;
	while (el)
	{	offset+=el.offsetLeft;
		el=el.offsetParent;
	}
	return Number(offset);
}

function getoffsettop(element)
{	offset=0;
	el=element;
	while (el)
	{	offset+=el.offsetTop;
		el=el.offsetParent;
	}
	return Number(offset);
}

function savereg()
{	var inps=document.getElementsByTagName('INPUT');
	IAdata='';
	i=0; ok=1;
	while (inps[i])
	{	inp=inps[i].name;
		if (mandatory=inps[i].getAttribute('mandatory'))
		{	switch (mandatory)
			{ case 'email': ok=ok && (checkemailaddress(inps[i].value)); break;
			  case 'text': case 'textarea': ok=ok && inps[i].value>''; break;
			}
		}
		if (inp && (inp.substr(0,4)=='todo')) //user registration todo
			IAdata+='&'+inp+'='+((inps[i].checked)?'0':'2');
		if (inp && (inp.substr(0,4)=='ctrl'))
		{	if (inps[i].type=='checkbox')
				IAdata+='&'+inp+'='+((inps[i].checked)?'Y':'N');
			else if (inps[i].type=='radio')
				IAdata+='&'+inps[i].value+'='+((inps[i].checked)?'Y':'N');
			else
				IAdata+='&'+inp+'='+encodeURIComponent(inps[i].value);
		}
		if (inp && (inp.substr(0,4)=='food'))
			IAdata+='&'+inp+'='+((inps[i].checked)?'Y':'N');
		i++;
	}
	i=0; //admin registration modification todo
	while (todo=$('IAregistranteditparttodobutton'+i))
	{	IAdata+='&todo-'+$d('IAregistranteditparttodoID'+i)+'='+
			$d('IAregistranteditparttodostatus'+i);
		i++;
	}
	var inps=document.getElementsByTagName('SELECT');
	i=0;
	while (inps[i])
	{	if (inps[i].getAttribute('name')>'' && inps[i].getAttribute('name').substr(0,4)=='ctrl')
		{	if ('select'==inps[i].getAttribute('mandatory'))
				  ok=(ok && (Number(inps[i].value)!=-1));
			IAdata+='&'+inps[i].getAttribute('name')+'='+encodeURIComponent(inps[i].value);
			opts=inps[i].options;
			j=0;
			while (opts[j])
			{	if (optname=opts[j].getAttribute('name'))
				{	if (opts[j].value==inps[i].value)
						IAdata+='&'+optname+'=Y';
					else
						IAdata+='&'+optname+'=N';
				}
				j++;
			}
		}
		i++;
	}
	var inps=document.getElementsByTagName('TEXTAREA');
	i=0;
	while (inps[i])
	{	if ('textarea'==inps[i].getAttribute('mandatory'))
			  ok=(ok && (inps[i].value>''));
		IAdata+='&'+inps[i].getAttribute('name')+'='+encodeURIComponent(inps[i].value);
		i++;
	}
	if (!ok) return 'ERROR';
	else return IAdata;
}

var tblordersortcolno=0;
var tblordersortdirs=[];

function tblordercomparenum(a,b,dir)
{	return (dir=='down')?Number(a[1])>=Number(b[1]):Number(a[1])<=Number(b[1]); }

function tblordercomparestring(a,b,dir)
{	return (dir=='down')?a[1].toLowerCase()>=b[1].toLowerCase():a[1].toLowerCase()<=b[1].toLowerCase(); }

function tblorderpartition(array,begin,end,pivot,type,dir)
{
	Array.prototype.swapTR=function(a, b)
	{	tmp=this[a][0]; this[a][0]=this[b][0]; this[b][0]=tmp;
		tmp=this[a][1]; this[a][1]=this[b][1]; this[b][1]=tmp;
	};
	var piv=[];
	piv[0]=array[pivot][0];
	piv[1]=array[pivot][1];
	array.swapTR(pivot, end-1);
	var store=begin;
	var ix;
	for(ix=begin; ix<end-1; ++ix)
	{	if((type=='number' && tblordercomparenum(array[ix],piv,dir)) ||
			(type!='number' && tblordercomparestring(array[ix],piv,dir)))
		{	array.swapTR(store, ix);
			++store;
		}
	}
	array.swapTR(end-1, store);
	return store;
}

function tblorderqsort(array,begin,end,type,dir)
{
	if(end-1>begin) {
		var pivot=begin+Math.floor((end-begin)/2);
		pivot=tblorderpartition(array,begin,end,pivot,type,dir);
		tblorderqsort(array,begin,pivot,type,dir);
		tblorderqsort(array,pivot+1,end,type,dir);
	}
}

function tblorder(table,type,ordcolno)
{	tblordersortcolno=ordcolno;
	var tbody=$(table).firstChild;
	while (tbody.nodeName.toLowerCase()!='tbody') tbody=tbody.nextSibling;
	var tbl=tbody.parentNode;
	var colid=tbl.id+String(ordcolno);
	if (tblordersortdirs[colid]=='up')
		tblordersortdirs[colid]='down';
	else
		tblordersortdirs[colid]='up';
	var rows=tbl.rows;
	var rowarray=[];
	i=0;
	//last row is spatium need not be ordered
	while (rows[i] && rows[i+1])
	{	tds=rows[i].getElementsByTagName('TD');
		var rw=[];
		if (tds[ordcolno] && tds[ordcolno].firstChild && tds[ordcolno].firstChild.data)
		{	rw.push(i);
			rw.push(tds[ordcolno].firstChild.data);
			rowarray.push(rw);
		}
		i++;
	}
	if (type=='number')
		tblorderqsort(rowarray,0,i,tblordercomparenum,tblordersortdirs[colid]);
	else
		tblorderqsort(rowarray,0,i,tblordercomparestring,tblordersortdirs[colid]);
	tmptbl=document.createElement('TBODY');
	l=rowarray.length;
	i=0;
	while (i<l)
	{	tmptbl.appendChild(rows[rowarray[i][0]].cloneNode(true));
		i++;
	}
	while (i>0)
	{	tbl.deleteRow(0);
		i--;
	}
	while (tmptbl.firstChild)
	{	tbody.insertBefore(tmptbl.removeChild(tmptbl.firstChild),tbody.lastChild);
	}
}

function getdebuginfo()
{	AJAXIAbase('getdebuginfo','GET','');
}

function getdebuginfo1(r)
{	i=0;
	while (r[i])
	{	var x=$d($d(r[i]));
		var y=$d(r[i+1]);
		$Sd($d(r[i]),x+' | '+y);
		i+=2;
	}
}

//XMLHTTPRequest handling
function XHRnew()
{	if (window.XMLHttpRequest)
		obj = new XMLHttpRequest();
	else if (window.ActiveXObject)
		obj = new ActiveXObject('Microsoft.XMLHTTP');
	return obj;
}

//submiturl should be set before calling
function XHRInit(obj,method,data,eventhandler)
{	document.getElementsByTagName('body')[0].className='cursorwait';
	obj.onreadystatechange = eventhandler;
	if (method=='GET')
	{	url='index.php?'+data;
		obj.open('GET', url, true);
		obj.setRequestHeader( 'If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT' );
		obj.onreadystatechange = eventhandler;
		obj.send(null);
	}
	else
	{	obj.open('POST','index.php', true);
		obj.setRequestHeader('Content-Type',(method=='MULTIPART'?'multipart/form-data':'application/x-www-form-urlencoded'));
		obj.setRequestHeader( 'If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT' );
		obj.onreadystatechange = eventhandler;
		obj.send(data);
	}
	return obj;
}

var IAbase=XHRnew();
var IAbasecom;
var IAbaseprocessed=0;

function AJAXIAbase(com,method,reqdata)
{	if (IAbaseprocessed==0)
	{	IAbasecom=com;
		reqdata+=(reqdata>''?'&':'');
		reqdata+='c='+com;
		if (document.oofrm && document.oofrm.event)
			reqdata+='&event='+document.oofrm.event.value;
		IAbaseprocessed=1;
		IAbase=XHRInit(IAbase,method,reqdata,AJAXIAbaseresponse);
	}
}

function AJAXIAbaseresponse()
{	if (IAbase.readyState == 4)
	{	if ((IAbase.status == 200) && (IAbaseprocessed==2))
		{	r=IAbase.responseXML.documentElement.getElementsByTagName('r');
			switch (IAbasecom)
			{ case 'en1': w5check2('new',r); break;
			  case 'ee1': w5check2('edit',r); break;
			  case 'ec1': w5check2('clone',r); break;
			  case 'encheckuser': w1check2(r); break;
			  case 'usercheckeditdata': usercheckedit1(r); break;
			  case 'eventstatuschange': eventstatuschange2(r); break;
			  case 'eventpublicchange': eventpublicchange2(r); break;
			  case 'eventfeechange': eventfeechange1(r); break;
			  case 'eventfeechangeconfirm': eventfeechange4(r); break;
			  case 'eventfeeupload': eventfeeupload1(r); break;
			  case 'getdesignscheme': d_getCSSscheme1(r); break;
			  case 'isnewheader': d_getnewheader1(r); break;
			  case 'getnextdesign': //no break
			  case 'getprevdesign': designstep1(r); break;
			  case 'getnextlogo': //no break
			  case 'getprevlogo': logostep1(r); break;
			  case 'setfromlanguage': setfromlanguage1(r); break;
			  case 't_next': getnext1(r); break;
			  case 'changetranslationmodule': changetranslationmodule1(r); break;
			  case 'deleteuploadedfile': deleteuploadfile1(r); break;
			  case 'adminpwdcheck': eventloadmsg1(r); break;
			  case 'getdebuginfo': getdebuginfo1(r); break;
			  case 'uMainMoney': case 'uMainNews': userTab1(IAbase.responseXML.documentElement); break;
			}
			IAbaseprocessed=0;
			if (IAbasecom!='getdebuginfo' && $('debugarea'))
				getdebuginfo();
			document.getElementsByTagName('body')[0].className='cursorgo';
		}
	}
	else
		IAbaseprocessed=2;
}

//FX
function cancelBubble(e)
{	var evt = e ? e:window.event;
	if (evt.stopPropagation)    evt.stopPropagation();
	if (evt.cancelBubble!=null) evt.cancelBubble = true;
}

var fxObjects = new Array();

function fxResize(e,obj,toWd,toHt,toLeft,toTop,sec)
{	for (i in fxObjects)
		if (fxObjects[i] && fxObjects[i].obj==obj)
			fxObjects[i]=null;
	var fxObj=new Array();
	fxObj.action='resize';
	fxObj.obj=obj;
	var css=window.getComputedStyle(obj,null);
	fxObj.fromHt=Number(css.getPropertyValue('height').slice(0,-2));
	fxObj.fromWd=Number(css.getPropertyValue('width').slice(0,-2));
	if (toHt==-1) {fxObj.toHt=fromHt;} else {fxObj.toHt=toHt;}
	if (toWd==-1) {fxObj.toWd=fromWd;} else {fxObj.toWd=toWd;}
	fxObj.toLeft=toLeft;
	fxObj.toTop=toTop;
	fxObj.allSteps=Number(sec/0.05);
	fxObj.curStep=1;
	obj.style.overflow='hidden';
	if (fxObjects.push(fxObj)==1)
		setTimeout(fxCallBack,30);
}

function fxCallBack()
{	var objno=0;
	for (i in fxObjects)
	{	var fxOb=fxObjects[i];
		if (!fxOb) continue;
		if (fxOb.action=='resize')
		{	var ht=Math.floor(fxOb.fromHt+(fxOb.toHt-fxOb.fromHt)*fxOb.curStep/fxOb.allSteps);
			if ((ht-fxOb.fromHt)/(fxOb.toHt-fxOb.fromHt)>1) ht=fxOb.toHt;
			var wd=Math.floor(fxOb.fromWd+(fxOb.toWd-fxOb.fromWd)*fxOb.curStep/fxOb.allSteps);
			if ((wd-fxOb.fromWd)/(fxOb.toWd-fxOb.fromWd)>1) wd=fxOb.toWd;
			css=window.getComputedStyle(fxOb.obj,null);
			if (fxOb.toLeft)
				fxOb.obj.style.left=String(Number(css.getPropertyValue('left').slice(0,-2))+Number(css.getPropertyValue('width').slice(0,-2))-wd)+'px';
			if (fxOb.toTop)
				fxOb.obj.style.top=String(Number(css.getPropertyValue('top').slice(0,-2))+Number(css.getPropertyValue('height').slice(0,-2))-ht)+'px';
			fxOb.obj.style.width=String(wd)+'px';
			fxOb.obj.style.height=String(ht)+'px';
			if (ht==fxOb.toHt && wd==fxOb.toWd)
				fxObjects[i]=null;
			else
			{	fxObjects[i].curStep++;
				objno++;
			}
		}
	}
	if (!objno)
		fxObjects.splice(0,fxObjects.length);
	else
		setTimeout(fxCallBack,30);
}

function fxResize1(e,obj,toWd,toHt,toLeft,toTop)
{	if (obj.onmouseenter!=null) return; //onmouseenter is resizing the object
	var css=window.getComputedStyle(obj,null);
	var fromHt=Number(css.getPropertyValue('height').slice(0,-2));
	var fromWd=Number(css.getPropertyValue('width').slice(0,-2));
	obj.style.width=String(toWd)+'px';
	obj.style.height=String(toHt)+'px';
	if (toLeft)
		obj.style.left=String(Number(css.getPropertyValue('left').slice(0,-2))+fromWd-toWd)+'px';
	if (toTop)
		obj.style.top=String(Number(css.getPropertyValue('top').slice(0,-2))+fromHt-toHt)+'px';
}
