
var saved=new Array();var connections=new Array();var connections_news=new Array();var currentServer=0;var firstConn=true;var queryString="";var appTitle="";var loggedIn=null;var script=new Array();var recoverConn=false;var serverTab=true;var showTabs=true;var connTab=true;var styleTools=true;var scriptMainMenu=new Array();var scriptUserMenu=new Array();apiFunctions['send_line']=function(arguments){var server=getServer(arguments[0]);if(server==null)return false;server.parseSendLine(arguments[1]);return true;}
apiFunctions['send_channel_line']=function(arguments){var server=getServer(arguments[0]);if(server==null)return false;server.parseChannelSendLine(arguments[1],arguments[2]);return true;}
apiFunctions['get_nick']=function(arguments){var server=getServer(arguments[0]);if(server==null)return null;return server.username;}
apiFunctions['get_channel']=function(arguments){var server=getServer(arguments[0]);if(server==null)return null;return server.channel;}
apiFunctions['add_main_menu']=function(arguments){scriptMainMenu[scriptMainMenu.length]=new MenuEntry(arguments[0],arguments[1],arguments[2]);}
apiFunctions['add_user_menu']=function(arguments){scriptUserMenu[scriptUserMenu.length]=new MenuEntry(arguments[0],arguments[1],arguments[2]);}
apiFunctions['close_window']=function(arguments){window.onbeforeunload=null;window.close();}
apiFunctions['find_user_level']=function(arguments){var server=getServer(arguments[0]);if(server==null)return null;var win=server.wins.retreive(arguments[1]);if(win==null)return null;return win.findUserLevel(arguments[2]);}
apiFunctions['find_current_level']=function(arguments){var server=getServer(arguments[0]);if(server==null)return null;var win=server.wins.retreive(arguments[1]);if(win==null)return null;return win.currentUserLevel();}
apiFunctions['cleanup_irc_markup']=function(arguments){var source=arguments[0];var resMsg="";for(var i=0;i<source.length;i++){if(source.charCodeAt(i)>=32||source.charCodeAt(i)==1)resMsg+=source.charAt(i);else if(source.charCodeAt(i)==3){var inSecondCode=false;while(i<source.length){i++;if(source.charAt(i)==','&&inSecondCode)break;else if(source.charAt(i)==','&&!inSecondCode)inSecondCode=true;else if(source.charCodeAt(i)>=48&&source.charCodeAt(i)<=57){}
else
{break;}}
if(i<source.length)i--;}}
return resMsg;}
function MenuEntry(label,cmd,confirm){this.label=label;this.cmd=cmd;this.confirm=confirm;}
function DoReconn(conns){if(recoverConn)return;if(connTab==false)return;recoverConn=true;for(var i=0;i<conns.length;i++){var created=false;for(var j=0;j<connections.length;j++){if(connections[j]=="New Connection"){window.frames['CONN_'+j].window.location.replace("/serverPage.aspx?reconnServer="+conns[i]);created=true;connections[j]="Recover "+j;break;}}
if(!created)newConn("Recover "+i,"/serverPage.aspx?reconnServer="+conns[i]);}
activateServerTab(1);}
function getServer(server){for(var i=0;i<connections.length;i++){if(connections[i]==server)return window.frames['CONN_'+i].window;}
return null;}
function logIn(){loggedIn=true;refreshServerList();}
function logOut(){loggedIn=false;refreshServerList();}
function execFunction(name,args){var res=null;for(var i in script){if(i.substr(0,4)!="scr_"||script[i]==null)continue;try
{var r=script[i].ExecuteFunction(name,args);if(r!=null)res=r;}
catch(ex){}}
return res;}
function setScript(name,content){try
{var newScript=new WsScript();newScript.Parse(content);script['scr_'+name]=newScript;scriptMainMenu=new Array();scriptUserMenu=new Array();execFunction("init_menu",[]);}
catch(ex){}}
function newConn(url){for(var i=0;i<connections.length;i++){if(connections[i]=="New Connection"){activateServerTab(i);if(url!=null)window.frames['CONN_'+i].window.location.replace(url);return i;}}
return createNewConn("New Connection",url);}
function createWin(name,url,forcedID){if(forcedID==null){nextId=connections.length;connections[nextId]=name;connections_news[nextId]=false;}
else
nextId=forcedID;var iframe=null;try
{iframe=document.createElement('<iframe name="CONN_'+nextId+'">');}
catch(ex){iframe=document.createElement('iframe');}
iframe.setAttribute('border','0');iframe.setAttribute('frameborder','0');iframe.setAttribute('name','CONN_'+nextId);iframe.setAttribute('id','ICONN_'+nextId);iframe.setAttribute('src',url);iframe.style.position="absolute";iframe.style.left=0;if(connTab==true)iframe.style.top=23;else
iframe.style.top=0;document.getElementById('CONN_CONTAINER').appendChild(iframe);activateServerTab(nextId);}
function createNewConn(name,url){for(var i=0;i<connections.length;i++){if(connections[i]=="~~"){connections[i]=name;if(url!=null)document.getElementById('CONN_'+i).setAttribute('src',url);activateServerTab(i);return i;}}
nextId=connections.length;connections[nextId]=name;connections_news[nextId]=false;var iframe=null;try
{iframe=document.createElement('<iframe name="CONN_'+nextId+'">');}
catch(ex){iframe=document.createElement('iframe');}
iframe.setAttribute('scrolling','no');iframe.setAttribute('scrollbar','no');iframe.setAttribute('border','0');iframe.setAttribute('frameborder','0');iframe.setAttribute('name','CONN_'+nextId);iframe.setAttribute('id','ICONN_'+nextId);if(url!=null)iframe.setAttribute('src',url);if(firstConn==true){if(queryString.indexOf("cookie=")!=-1)iframe.setAttribute('src','/serverPage.aspx?'+queryString);else
iframe.setAttribute('src','/serverPage.aspx?cookie=yes&'+queryString);}
else
iframe.setAttribute('src','/serverPage.aspx?cookie=no&'+queryString);iframe.style.position="absolute";iframe.style.left=0;if(connTab==true)iframe.style.top=23;else
iframe.style.top=0;document.getElementById('CONN_CONTAINER').appendChild(iframe);activateServerTab(i);return i;}
function setName(win,name){for(var i=0;i<connections.length;i++){if(window.frames["CONN_"+i].window==win){connections[i]=name;if(name=="New Connection"){var nb=0;var sel=0;for(var j=0;j<connections.length;j++){if(j!=i&&connections[j]=="New Connection"){nb++;sel=i;}}
if(nb>0){connections[i]="~~";activateServerTab(sel);}}
refreshServerList();return;}}}
function refreshServerList(){var html="";for(var i=0;i<connections.length;i++){if(connections[i]=="~~")continue;if(i==currentServer){html+="<SPAN id='currentTabA'></SPAN><SPAN id=currentTabB>"+connections[i];if(connections[i].charAt(0)!="*")html+="<IMG SRC=images/close.gif width=12 height=12 STYLE='margin-left: 10px' OnClick='if(confirm(\"Are you sure you want to close this server connection?\")){window.frames[\"CONN_"+i+"\"].quit();}return false;'>";html+="</SPAN>";}
else
html+="<SPAN class='tab'></SPAN><SPAN id='stab_"+i+"' OnClick='activateServerTab("+i+");'>"+connections[i]+"</SPAN>";}
html+="<SPAN class='tab'></SPAN><SPAN OnClick='newConn()'>+</SPAN>";var label="Login";if(loggedIn==null)label="Initializing";else if(loggedIn)label="Options";if(currentServer=="opt")html+="<SPAN id='currentTabA' style='position: absolute; right: 70'></SPAN><SPAN id='currentTabB' OnClick='window.frames[\"CONN_opt\"].window.location.replace(\"/options.aspx\");activateServerTab(\"opt\");' style='position: absolute; right: 0; width: 67; text-align: center;'>"+label+"</SPAN>";else
html+="<SPAN class='tab' style='position: absolute; right: 70'></SPAN><SPAN OnClick='activateServerTab(\"opt\")' style='position: absolute; right: 0; width: 66; text-align: center;'>"+label+"</SPAN>";document.getElementById('serverList').innerHTML=html;}
function activateServer(win){for(var i=0;i<connections.length;i++){if(window.frames["CONN_"+i].window==win){activateServerTab(i);return;}}}
function newMessage(win,state){for(var i=0;i<connections.length;i++){if(window.frames["CONN_"+i].window==win){connections_news[i]=state;return;}}}
function blinkServer(){var today=new Date();var gotNews=false;for(var i=0;i<connections.length;i++){if(connections[i]=="~~"||connections[i]=="New Connection")continue;var tab=document.getElementById('stab_'+i);if(i==currentServer)tab=document.getElementById('currentTabB');if(connections_news[i]==true&&(today.getSeconds()*2+Math.floor(today.getMilliseconds()/500))%2){tab.style.color="#0000E0";gotNews=true;}
else
tab.style.color="#000000";}
if(gotNews&&!hasFocus)document.title="* NEW MESSAGES * - "+appTitle;else
document.title=appTitle;setTimeout("blinkServer()",500);}
function activateServerTab(i){document.getElementById('ICONN_'+currentServer).style.visibility='hidden';document.getElementById('ICONN_'+currentServer).style.width=1;document.getElementById('ICONN_'+currentServer).style.height=1;document.getElementById('ICONN_'+i).style.visibility='visible';try
{window.frames['CONN_'+i].window.getFocus();}
catch(ex){}
currentServer=i;refreshServerList();doResize();}
function storedData(name,data){this.name=name;this.data=data;}
function doResize(toRefresh){var height=document.body.clientHeight;var width=document.body.clientWidth;var content=document.getElementById('ICONN_'+currentServer);content.style.width=width;if(connTab)content.style.height=height-23;else
{content.style.height=height;content.style.top=0;}
if(toRefresh==null||toRefresh==true)setTimeout("doResize(false)",100);}
function doUpdate(){window.frames['ContentFrame'].doUpdate();}
function WindowOut(){hasFocus=false;}
function WindowIn(){hasFocus=true;}
var mobileDetect=["palm","blackberry","windows mobile","opera mobi","windows ce","android","symbian","iphone","ipod","htc"];function Init(){window.onblur=WindowOut;window.onfocus=WindowIn;var uagent=navigator.userAgent.toLowerCase();for(var i=0;i<mobileDetect.length;i++){if(uagent.search(mobileDetect[i])>-1){if(confirm('Do you want to use the mobile version of wsIRC?')){location.replace('http://www.wsirc.mobi');return;}
break;}}
document.getElementById('CONN_CONTAINER').innerHTML='';document.onresize=doResize;var url=""+window.location;if(url.indexOf("?")!=-1)queryString=url.substr(url.indexOf("?")+1);else if(url.indexOf("~")!=-1)queryString="network="+url.substr(url.indexOf("~")+1);if(queryString.indexOf("conntab=false")!=-1){document.getElementById('serverList').style.visibility="hidden";document.getElementById('serverList').style.postion="absolute";connTab=false;}
if(queryString.indexOf("confirm=false")==-1)window.onbeforeunload=CheckUnload;if(queryString.indexOf("servertab=false")!=-1)serverTab=false;if(queryString.indexOf("styletools=false")!=-1)styleTools=false;if(queryString.indexOf("showtabs=false")!=-1)showTabs=false;createWin('* Start *','/cms/');if(queryString.indexOf("conntab=false")==-1)createWin('opt','/options.aspx','opt');newConn();appTitle=document.title;setTimeout("checkVersion()",30000);if(url.indexOf("?")!=-1||url.indexOf("~")!=-1||queryString.indexOf("noservertab=true")!=-1)setTimeout("activateServerTab(1);",200);else
activateServerTab(0);blinkServer();}
function store(server,data){saved[saved.length]=new storedData(server,data);}
function recover(server){for(var i=0;i<saved.length;i++){if(saved[i].name==server){var res=saved.slice(i,1);saved.splice(i,1);return res[0].data;}}
return null;}
function checkVersion(){var http=null;try
{http=new XMLHttpRequest();}
catch(e){try
{http=new ActiveXObject("Microsoft.XMLHTTP");}
catch(e2){http=false;}}
if(!http){return false;}
var now=new Date();var url='/checkVersion.aspx?f=ui&r='+escape(now.toGMTString());http.open("GET",url);http.onreadystatechange=function(){if(http.readyState==4){if(http.responseText!="-"){for(var i=0;i<connections.length;i++){if(connections[i].charAt(0)=="*"||connections[i]=="~~"||connections[i]=="New Connection")continue;try
{window.frames["CONN_"+i].window.checkVersion(http.responseText);}
catch(ex){}}}}}
http.send(null);setTimeout("checkVersion();",30000);return false;}
function CheckUnload(){return"If you press Ok you will close wsIrc however\nthis will close all your current connections as well.";}