// (c)2010 Michael Holtstrom

var util={};util.mouse=function(e)
{dbgx('util.mouse');return[e.pageX||e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft,e.pageY||e.clientY+document.body.scrollTop+document.documentElement.scrollTop];};util.isrc=function(e)
{dbgx('util.isrc');var rc=false;if(e.which){rc=(e.which==3);}
else if(e.button){rc=(e.button==2);}
return rc;};util.elm=function(e)
{dbgx('util.elm');var elm=e.target||e.srcElement;if(elm.nodeType==3){elm=elm.parentNode;}
return elm;};util.pos=function(elm)
{dbgx('util.pos');var ary=[0,0];while(elm)
{if(elm.offsetLeft){ary[0]+=elm.offsetLeft;}
if(elm.offsetTop){ary[1]+=elm.offsetTop;}
elm=elm.parentNode;}
return ary;};util.screen=function()
{dbgx('util.screen');var ary=null;if(window.innerWidth){ary=[0,0,window.innerWidth,window.innerHeight+document.body.scrollTop];}
else{ary=[0,0,document.body.clientWidth,document.documentElement.clientHeight];}
ary[0]=document.documentElement.scrollLeft+document.body.scrollLeft;ary[1]=document.documentElement.scrollTop+document.body.scrollTop;return ary;};util.inview=function(elm)
{dbgx('util.inview');var pos=util.pos(elm);var see=util.screen();pos[2]=pos[0]+elm.offsetWidth;see[2]=see[0]+see[2];pos[3]=pos[1]+elm.offsetHeight;see[3]=see[1]+see[3];if(pos[2]>see[2])
{elm.style.left=(elm.offsetLeft-(pos[2]-see[2]))+'px';}
if(pos[3]>see[3])
{elm.style.top=(elm.offsetTop-(pos[3]-see[3]))+'px';}
pos=util.pos(elm)
if(pos[0]<see[0])
{elm.style.left=(elm.offsetLeft+(see[0]-pos[0]))+'px';}
if(pos[1]<see[1])
{elm.style.top=(elm.offsetTop+(see[1]-pos[1]))+'px';}};util.bind=function(obj,func,kind,elm)
{dbgx('util.bind');var bound=(function(e){e=e||window.event;return obj[func](e,this);});util.add(elm,kind,bound);return[elm,kind,bound];};util.add=function(obj,kind,func)
{dbg('util.add '+kind);if(obj.addEventListener)
{obj.addEventListener(kind,func,false);}
else
{obj['on'+kind]=func;}};util.remove=function(obj,kind,func)
{dbg('util.remove '+kind);if(obj.removeEventListener)
{obj.removeEventListener(kind,func,false);}
else
{obj['on'+kind]=null;}};util.remover=function(ary)
{dbgx('util.remover');util.remove(ary[0],ary[1],ary[2]);ary[0]=null;ary[1]=null;ary[2]=null;ary=null;};util.cancel=function(e)
{dbgx('util.cancel');if(!e){var e=window.event;}
e.returnValue=false;if(e.preventDefault){e.preventDefault();}
e.cancelBubble=true;if(e.stopPropagation){e.stopPropagation();}
return false;};util.lmin=function(v,m)
{dbgx('util.lmin');if(v<m){return m;}
else{return v;}};util.trim=function(s)
{dbgx('util.trim');return s.replace(/^\s+|\s+$/g,'');};util.hFix=function(s)
{dbgx('util.hFix');var v=s.replace(/&/g,"&amp;");v=v.replace(/>/g,"&gt;");v=v.replace(/</g,"&lt;");return v;};util.toBR=function(s,fix)
{dbgx('util.toBR');var v=s.replace(/>/g,"&gt;");v=v.replace(/</g,"&lt;");v=v.replace(/(\r\n|[\r\n])/g,"<br/>");return v;};util.toCR=function(s)
{dbgx('util.toCR');return s.replace(/<br\/>/g,"\r");};util.key=function(e)
{dbgx('util.key');if(e.which){return e.which;}
else{return e.keyCode;}};util.colon=function(e)
{dbgx('util.colon');if(e.keyCode==59&&e.shiftKey==true)
{return true;}
else{return false;}};util.tab=function(e)
{dbgx('util.tab');if(e.keyCode==9)
{return true;}
else{return false;}};util.crlf=function(e)
{dbgx('util.crlf');if(e.keyCode==10||e.keyCode==13)
{return true;}
else{return false;}};util.signed=function(i)
{dbgx('util.signed');if(i<0){return''+i;}
else{return'+'+i;}};util.cookie=function(s)
{dbgx('util.cookie');s=util.trim(s)+"=";var ca=document.cookie.split(';');for(var i=0;i<ca.length;i++)
{var c=util.trim(ca[i]);if(c.indexOf(s)==0)
{return util.decode(util.trim(c.substr(s.length)));}}
return'';};util.setCookie=function(name,value)
{var date=new Date();date.setTime(date.getTime()+25920000000);document.cookie=name+"="+value+"; expires="+date.toGMTString()+"; path=/; domain=.evilgoblin.com;";};util.now=function()
{dbgx('util.now');var now=new Date();return Math.round(now.getTime()/1000)-now.getTimezoneOffset()*60;};util.enterkey=function(e,func)
{dbgx('util.enterkey');e=e||window.event;if(util.crlf(e))
{func();return util.cancel(e);}
else{return true;}};util.checktxt=function(elm,name,len,req)
{dbgx('util.checktxt');if(elm.value.length>len)
{err.msg("Sorry, "+name+" can't be longer than "+len+" characters. Yours is "+elm.value.length+" characters.");elm.focus();return false;}
if(req&&util.trim(elm.value).length<1)
{err.msg("Sorry, the "+name+" is required.");elm.focus();return false;}
return true;};util.encode=function(str)
{dbgx('util.encode');var out='';var x=0;str=str.toString();var regex=/(^[a-zA-Z0-9_.]*)/;while(x<str.length){var match=regex.exec(str.substr(x));if(match!=null&&match.length>1&&match[1]!=''){out+=match[1];x+=match[1].length;}else{if(str[x]==' ')
out+='+';else{var charCode=str.charCodeAt(x);var hexVal=charCode.toString(16);if(charCode<256){out+='%'+(hexVal.length<2?'0':'')+hexVal.toUpperCase();}
else{out+='%5F';}}
x++;}}
return out;};util.decode=function(str)
{dbgx('util.decode');var out=str;var binVal,thisString;var myregexp=/(%[^%]{2})/;while((match=myregexp.exec(out))!=null&&match.length>1&&match[1]!=''){binVal=parseInt(match[1].substr(1),16);thisString=String.fromCharCode(binVal);out=out.replace(match[1],thisString);}
out=out.replace(/\+/g," ");return out;};util.hide=function(str)
{dbgx('util.hide');var out='';var num;for(var i=0;i<str.length;i++)
{num=str.charCodeAt(i);if(num<16){out+='%0'+num.toString(16).toUpperCase();}
else if(num<256){out+='%'+num.toString(16).toUpperCase();}
else{out+='%5F';}}
return out;};