
window.onload=function(){if(_DEBUG_){_console=new Console(50,600,350,350);}
document.getElementById('username').value='';document.getElementById('password').value='';document.getElementById('password_repeat').value='';document.getElementById('chat_input').value='';document.getElementById('username').focus();document.getElementById('login_button').onclick=init;document.getElementById('registration_button').onclick=register;document.getElementById('selector_button').onclick=Board.selectPieceDialog;};function empty(id,str){if(document.getElementById(id).value==str){document.getElementById(id).value='';}};function posToBoard(board,pos_old,pos_new){var pieceId_old=pos_old.split(":");var pieceId_new=pos_new.split(":");for(var i=0;i<64;i++){if(pieceId_old[i]!=''){board.placePiece(i,pieceId_old[i],false,0);}}
var diff=[];var j=0;for(i=0;i<64;i++){if(pieceId_old[i]!=pieceId_new[i]){diff[j++]=i;}}
for(i=0;i<j;i++){if(pieceId_old[diff[i]]!=''){board.removePiece(diff[i],true,i);}
if(pieceId_new[diff[i]]!=''){board.placePiece(diff[i],pieceId_new[diff[i]],true,i+j);}}};function resultToNotation(el,result){var nl1=document.createElement('br');var nl2=document.createElement('br');var res=document.createTextNode(result);el.appendChild(nl1);el.appendChild(nl2);el.appendChild(res);};function servermsgToWindow(el,text1,text2){el.innerHTML=el.innerHTML+"<span style='color:#ffff00'>"+text1+"</span>"+"&nbsp;"+text2+"<br>";el.parentNode.scrollTop=el.parentNode.scrollHeight;};function chatToWindow(el,text1,text2){var newline=document.createElement('div');var txt1=document.createTextNode(text1);var txt2=document.createTextNode(': ');var txt3=document.createTextNode(text2);var spn1=document.createElement('span');var spn2=document.createElement('span');var spn3=document.createElement('span');el.appendChild(newline);newline.appendChild(spn1);newline.appendChild(spn2);newline.appendChild(spn3);spn1.appendChild(txt1);spn2.appendChild(txt2);spn3.appendChild(txt3);spn1.className='span_name';spn2.className='span_name';el.scrollTop=el.scrollHeight;};function playersToTargets(el,string_players){var players_entries=[];var players_entry=[];if(string_players.indexOf("~")>0){players_entries=string_players.split("~");}else{players_entries[0]=string_players;}
var opt=document.createElement('option');var txt=document.createTextNode('all players');opt.appendChild(txt);el.appendChild(opt);opt=document.createElement('option');txt=document.createTextNode('my buddies');opt.appendChild(txt);el.appendChild(opt);for(var i=0;i<players_entries.length;i++){players_entry=players_entries[i].split("|");if(players_entry[0]!=document.getElementById('username').value&&players_entry[0].indexOf('crafty')==-1){opt=document.createElement('option');txt=document.createTextNode(players_entry[0]);opt.appendChild(txt);el.appendChild(opt);}}};function playersToWindow(el,string_players){var players_entries=[];var players_entry=[];if(string_players.indexOf("~")>0){players_entries=string_players.split("~");}else{players_entries[0]=string_players;}
var tbl=document.createElement('table');var tbdy=document.createElement('tbody');var id_ar=[];for(var i=0;i<players_entries.length;i++){players_entry=players_entries[i].split("|");if(players_entry[2]){var row=document.createElement('tr');for(var j=0;j<3;j++){var cell=document.createElement('td');if(j===0){var id_cell=document.createAttribute('id');id_cell.nodeValue='p_cell_'+i.toString(10);id_ar[i]=id_cell.nodeValue;cell.setAttributeNode(id_cell);}
cell.className='dynTableCell';var txt;if(j==2&&players_entry[j]=='0'){txt=document.createTextNode("available");}else if(j==2&&players_entry[j]=='1'){txt=document.createTextNode("playing");}else if(j==2&&players_entry[j]=='2'){txt=document.createTextNode("challenged");}else if(j==2&&players_entry[j]=='3'){txt=document.createTextNode("challenging");}else if(j==2&&players_entry[j]=='4'){txt=document.createTextNode("kibitzing");}else{txt=document.createTextNode(players_entry[j]);}
cell.appendChild(txt);if(j===0){cell.className='span_name dynTableCell';}else{cell.className='right dynTableCell';}
row.appendChild(cell);}
tbdy.appendChild(row);}}
tbl.appendChild(tbdy);tbl.className='dynTable';el.appendChild(tbl);for(i=0;i<players_entries.length;i++){if(document.getElementById(id_ar[i])&&document.getElementById(id_ar[i]).nextSibling.nextSibling.firstChild.nodeValue=="available"){document.getElementById(id_ar[i]).onmouseover=renderRed;document.getElementById(id_ar[i]).onmouseout=renderYellow;}else if(document.getElementById(id_ar[i])&&document.getElementById(id_ar[i]).nextSibling.nextSibling.firstChild.nodeValue=="playing"){document.getElementById(id_ar[i]).onmouseover=renderGreen;document.getElementById(id_ar[i]).onmouseout=renderYellow;}}
el.scrollTop=el.scrollHeight;};function offersToWindow(el,string_offers){var offers_entries=[];var offers_entry=[];if(string_offers.indexOf("~")>0){offers_entries=string_offers.split("~");}else{offers_entries[0]=string_offers;}
var tbl=document.createElement('table');var tbdy=document.createElement('tbody');var id_ar=[];for(var i=0;i<offers_entries.length;i++){offers_entry=offers_entries[i].split("|");if(offers_entry[4]){var row=document.createElement('tr');for(var j=0;j<5;j++){var cell=document.createElement('td');if(j===0){var id_cell=document.createAttribute('id');id_cell.nodeValue='o_cell_'+i.toString(10);id_ar[i]=id_cell.nodeValue;cell.setAttributeNode(id_cell);}
cell.className='dynTableCell';var txt;switch(j){case 3:txt=document.createTextNode(offers_entry[j]+' min');break;case 4:txt=document.createTextNode(offers_entry[j]+' sec');break;default:txt=document.createTextNode(offers_entry[j]);}
cell.appendChild(txt);if(j===0){cell.className='span_name dynTableCell';}else{cell.className='right dynTableCell';}
row.appendChild(cell);}
tbdy.appendChild(row);}}
tbl.appendChild(tbdy);tbl.className='dynTable';el.appendChild(tbl);for(i=0;i<offers_entries.length;i++){if(document.getElementById(id_ar[i])){document.getElementById(id_ar[i]).onmouseover=renderRed;document.getElementById(id_ar[i]).onmouseout=renderYellow;}}
el.scrollTop=el.scrollHeight;};function renderRed(e){var id;if(!document.all){id=e.target.id;}else{id=event.srcElement.id;}
if(document.getElementById(id)){document.getElementById(id).style.color='#ff0000';document.getElementById(id).style.cursor='pointer';}};function renderYellow(e){var id;if(!document.all){id=e.target.id;}else{id=event.srcElement.id;}
if(document.getElementById(id)){document.getElementById(id).style.color='#ffff00';document.getElementById(id).style.cursor='auto';}};function renderGreen(e){var id;if(!document.all){id=e.target.id;}else{id=event.srcElement.id;}
if(document.getElementById(id)){document.getElementById(id).style.color='#00ff00';document.getElementById(id).style.cursor='pointer';}};function showMessage(box,msg){if(box==1){document.getElementById('error_box').style.display='block';}else{document.getElementById('error_box').style.display='none';}
for(var i=1;i<msg.length+1;i++){if(msg[i-1]===0){document.getElementById('error_'+i.toString(10)).style.display='none';}else{document.getElementById('error_'+i.toString(10)).style.display='block';}}};function init(){var name=document.getElementById('username').value;var password=document.getElementById('password').value;name=name.replace(/\||/g,"");name=name.replace(/\~|/g,"");name=name.replace(/\'|/g,"");name=name.replace(/\"|/g,"");name=name.replace(/\;|/g,"");name=name.replace(/\,|/g,"");name=name.replace(/\ |/g,"");password=password.replace(/\||/g,"");password=password.replace(/\~|/g,"");password=password.replace(/\'|/g,"");password=password.replace(/\"|/g,"");password=password.replace(/\;|/g,"");password=password.replace(/\,|/g,"");password=password.replace(/\ |/g,"");document.getElementById('username').value=name;document.getElementById('password').value=password;if(name.length===0){showMessage(1,[0,0,0,0,0,1,0,0,0,0]);document.getElementById('username').focus();return;}else{showMessage(0,[0,0,0,0,0,0,0,0,0,0]);}
if(password.length===0){showMessage(1,[0,0,0,0,0,0,0,0,1,0]);document.getElementById('password').focus();return;}else{showMessage(0,[0,0,0,0,0,0,0,0,0,0]);}
var freq=2;var board=new Board(0,freq);var login_cmpnts=[];login_cmpnts[0]=board;var poller_cmpnts=[];poller_cmpnts[0]=board;document.getElementById('login_button').disabled=true;var login=new Login(login_cmpnts,poller_cmpnts,freq);};function registrationDialog(){showMessage(0,[0,0,0,0,0,0,0,0,0,0]);document.getElementById('password_repeat').style.display='block';document.getElementById('text_repeat').style.display='block';document.getElementById('registration_link').style.display='none';document.getElementById('login_link').style.display='block';document.getElementById('login_button').style.display='none';document.getElementById('registration_button').style.display='inline';document.getElementById('line_break').style.display='inline';document.getElementById('username').focus();showMessage(1,[0,0,0,0,1,0,0,0,0,0]);};function loginDialog(){showMessage(0,[0,0,0,0,0,0,0,0,0,0]);document.getElementById('password_repeat').style.display='none';document.getElementById('text_repeat').style.display='none';document.getElementById('registration_link').style.display='block';document.getElementById('login_link').style.display='none';document.getElementById('login_button').style.display='inline';document.getElementById('registration_button').style.display='none';document.getElementById('line_break').style.display='none';document.getElementById('username').focus();};function register(){showMessage(0,[0,0,0,0,0,0,0,0,0,0]);var name=document.getElementById('username').value;var password=document.getElementById('password').value;var password_repeat=document.getElementById('password_repeat').value;name=name.replace(/\||/g,"");name=name.replace(/\~|/g,"");name=name.replace(/\'|/g,"");name=name.replace(/\"|/g,"");name=name.replace(/\;|/g,"");name=name.replace(/\,|/g,"");name=name.replace(/\ |/g,"");password=password.replace(/\||/g,"");password=password.replace(/\~|/g,"");password=password.replace(/\'|/g,"");password=password.replace(/\"|/g,"");password=password.replace(/\;|/g,"");password=password.replace(/\,|/g,"");password=password.replace(/\ |/g,"");document.getElementById('username').value=name;document.getElementById('password').value=password;if(name.length===0){showMessage(1,[0,0,0,0,0,1,0,0,0,0]);document.getElementById('username').focus();return;}else{showMessage(0,[0,0,0,0,0,0,0,0,0,0]);}
if(password!=password_repeat){showMessage(1,[0,1,0,0,0,0,0,0,0,0]);return;}else{showMessage(0,[0,0,0,0,0,0,0,0,0,0]);document.getElementById('registration_button').disabled=true;var registrar=new Registrar();}};function tickDown(ref_ms){var now=new Date();var now_ms=now.getTime();if(g_b1){now_ms_old=ref_ms;g_b1=false;}
if(now_ms-now_ms_old>=1000){now_ms_old=now_ms;if(g_which_clock=='white'){decrement('s_clock1');}else if(g_which_clock=='black'){decrement('s_clock2');}}};function decrement(which){var strClock=document.getElementById(which).firstChild.nodeValue;var clockArray=[];clockArray=strClock.split(':');var hours=clockArray[0];var minutes=clockArray[1];var seconds=clockArray[2];if(seconds==0){seconds=59;if(minutes==0){minutes=59;hours--;}else{minutes--;}}else{seconds--;}
if(minutes.toString().length<2){minutes='0'+minutes;}
if(seconds.toString().length<2){seconds='0'+seconds;}
document.getElementById(which).firstChild.nodeValue=hours+':'+minutes+':'+seconds;if(document.getElementById(which).firstChild.nodeValue.indexOf('-')!=-1){document.getElementById(which).firstChild.nodeValue='0:00:00';}};function GameOver(board,result){if(g_timer){clearInterval(g_timer);g_timer=null;}
if(result=='1-0'){document.getElementById('s_result').firstChild.nodeValue='1:0';}else if(result=='0-1'){document.getElementById('s_result').firstChild.nodeValue='0:1';}else if(result=='1/2'){document.getElementById('s_result').firstChild.nodeValue='1/2';}
board.disableBoard();document.getElementById('b_claim_win').disabled=true;document.getElementById('b_accept_draw').disabled=true;document.getElementById('b_decline_draw').disabled=true;document.getElementById('b_resign').disabled=true;document.getElementById('b_draw').disabled=true;document.getElementById('b_reset_ui').disabled=false;g_playing=false;g_draw_offered=false;g_challenged=false;g_challenging=false;g_offering=false;g_isMate=false;g_isDraw=false;g_isStalemate=false;g_b1=true;};
