// liveCode_ob.js
// Copyright 2008-2009 by Made Headway Limited

var liveTimeout=null;var liveTimeoutBase=5;var liveUserListHTML='';var onlineUserList=[];var LiveState={offline:0,online:1};var liveState=LiveState.offline;function LiveStart(){if(g_loggedIn){if(typeof(LiveChangeState)!='undefined'){LiveChangeState(LiveState.online);}
LivePoll();}
LiveInitPoll();}
function LiveStop(){LiveUninitPoll();if(typeof(LiveChangeState)!='undefined'){LiveChangeState(LiveState.offline);}}
function LiveUninitPoll(){if(liveTimeout){clearTimeout(liveTimeout);liveTimeout=null;}}
function LiveInitPoll(){LiveUninitPoll();if(g_loggedIn){liveTimeoutSeconds=(GetElem('liveMessageWindow')?liveTimeoutBase:liveTimeoutBase*2);liveTimeout=setTimeout("LivePoll();",liveTimeoutSeconds*1000);}}
function LivePoll(){RequestURL("live.php?f=poll",null,"liveMessageCache",LivePostPoll);}
function LivePostPoll(){var aa=GetElem('liveMessageCache');if(aa.hasChildNodes()){var ba=aa.childNodes[0];if(ba.getAttribute('type')=='liveData'){var ca=onlineUserList;var da=[];onlineUserList=[];var i;for(i=0;i<ba.childNodes.length;i++){var ea=ba.childNodes[i];if(ea.getAttribute('type')=='userData'){var fa=new Object();fa.userId=ea.getAttribute('userId');fa.username=ea.getAttribute('username');fa.loggedIn=ea.getAttribute('loggedIn');onlineUserList.push(fa);}
else if(ea.getAttribute('type')=='message'){var ga=new Object();ga.fromId=ea.getAttribute('fromId');ga.fromName=ea.getAttribute('fromName');ga.data=ea.getAttribute('message');ga.time=new Date(ea.getAttribute('time'));da.push(ga);}}
if(typeof(LiveUpdateUserList)!='undefined'){var ha=(ca.length==onlineUserList.length);if(ha){for(i=0;i<ca.length;i++){if(ca[i].userId!=onlineUserList[i].userId){ha=false;break;}}}
if(!ha){LiveUpdateUserList(onlineUserList);}}
ca=null;if(typeof(LiveMessageReceived)!='undefined'){for(i=0;i<da.length;i++){LiveMessageReceived(da[i]);}}
da=null;}}
LiveInitPoll();}
function LiveMessageSend(ia,ja,ka){var la=false;if(ka=='live'){var i;for(i=0;i<onlineUserList.length;i++){if(onlineUserList[i].userId==ja){la=true;break;}}
if(!la){la=confirm('User appears to be offline. Send anyway? (The message will be kept until they next log on)');}}
else{la=true;}
if(la){RequestURL("live.php?f=message&content="+encodeURIComponent(ia)+"&type="+encodeURIComponent(ka)+"&toId="+parseInt(ja),null,"liveMessageCache",LivePostMessageSend);}}
function LivePostMessageSend(){var ma=GetElem('liveMessageCache');if(ma.hasChildNodes()){var na=ma.childNodes[0];if(na.getAttribute('type')=='liveData'){if(na.hasChildNodes()){var oa=na.childNodes[0];if(oa.getAttribute('type')=='result'){if(oa.getAttribute('value')=='sent'){if(typeof(LiveMessageSent)!='undefined'){LiveMessageSent();}}
else{if(typeof(LiveMessageSendError)!='undefined'){LiveMessageSendError();}}}}}}}
function LiveOpenUICheckOnline(){if(liveUserListHTML==''){alert('No other users online! Try again later.');}
else{if(!GetElem('liveMessageWindow')){LiveOpenUI();}}}
function LiveChangeState(pa){liveState=pa;if(g_hasAnyEditPrivilege){if(pa==LiveState.online){writeCookie('cfsLive','online');GetElem('liveOnlineLogo').src='images/liveOnline.gif';GetElem('liveMessageButton').style.display='';}
else{writeCookie('cfsLive','offline');GetElem('liveOnlineLogo').src='images/liveOffline.gif';GetElem('liveMessageButton').style.display='none';}}}
function LiveToggleOnlineState(){if(liveState==LiveState.online){LiveStop();}
else if(liveState==LiveState.offline){LiveStart();}}
function LiveOpenUI(){if(g_hasAnyEditPrivilege){var qa=OpenDialog(350,320,'Live Message');qa.innerHTML=' <div style="padding:5px 20px 20px 20px;" id="liveMessageWindow"> <form> History:<br/> <div id="liveHistory" style="width:280px;height:90px;overflow-y:auto;border:1px solid #000;padding:4px 5px 4px 5px;"> </div><br/> <table><tr><td>Reply to:</td><td><select id="liveUsers">'+liveUserListHTML+'</select></td></tr></table> <textarea id="liveMessage" rows="4" cols="40"></textarea><br/> <div style="text-align:right;"> <input type="button" value="Send" onclick="LiveMessageSendWrapper();"/> <input type="button" value="Cancel" onclick="CancelDialog();"/> </div> </form> </div>';}
else{var qa=OpenDialog(460,250,'Ask Edwin...');var ra='#F0F0F0';qa.parentNode.style.backgroundColor=ra;qa.style.backgroundColor=ra;qa.innerHTML=' <div style="padding:5px 20px 20px 20px;" id="liveMessageWindow"> <form> <table><tr> <td><img style="margin:0px 10px 0px 0px;" src="images/edwin.jpg"/></td> <td> Send Edwin a message:<br/> <input type="text" id="liveMessage" size="35" value=""></input><br/> History:<br/> <div id="liveHistory" style="width:250px;height:90px;overflow-y:auto;border:1px solid #000;padding:4px 5px 4px 5px;background-color:#FFF;"> </div><br/> <select id="liveUsers" style="display:none;">'+liveUserListHTML+'</select> <div style="text-align:right;"> <input type="button" value="Send" onclick="LiveMessageSendWrapper();"/> <input type="button" value="Cancel" onclick="CancelDialog();"/> </div> </td> </tr></table> </form> </div>';}
if(g_loggedIn){LivePoll();}}
function LiveMessageSendError(){if(GetElem('liveMessageWindow')){var sa=GetElem('liveMessage');sa.disabled=false;var ta=GetElem('liveUsers');ta.disabled=false;alert('Error sending message. Sorry.');}}
function LiveMessageSent(){if(GetElem('liveMessageWindow')){var ua=GetElem('liveMessage');var va=ua.value;ua.disabled=false;ua.value='';var wa=GetElem('liveUsers');wa.disabled=false;var xa='[unknown]';var i;for(i=0;i<onlineUserList.length;i++){if(onlineUserList[i].userId==wa.value){xa=onlineUserList[i].username;break;}}
var ya=((g_hasAnyEditPrivilege)?' (to '+xa+')':'');var za=GetElem('liveHistory');za.innerHTML+='<div style="color:#F00;">'+g_loggedInName+ya+': '+va+'</div>';za.scrollTop=10000;}}
function LiveMessageReceived(Aa){if(!GetElem('liveMessageWindow')){LiveOpenUI();}
GetElem('liveUsers').value=Aa.fromId;hist=GetElem('liveHistory');hist.innerHTML+='<div style="color:#060;">'+Aa.fromName+': '+Aa.data+'</div>';hist.scrollTop=10000;}
function LiveUpdateUserList(Ba){liveUserListHTML='';var i;for(i=0;i<Ba.length;i++){if(Ba[i].userId!=g_loggedIn){liveUserListHTML+='<option value="'+Ba[i].userId+'">'+Ba[i].username+'</option>';}}
var Ca=GetElem('liveMessageButton');if(Ca){Ca.disabled=(liveUserListHTML=='');}
var Da=GetElem('liveUsers');if(Da){Da.innerHTML=liveUserListHTML;}
if(!g_hasAnyEditPrivilege){var Ea=GetElem('liveSuperuserOnlineLogo');if(Ba.length){Ea.src='images/liveOnline.gif';Ca.style.display='';}
else{Ea.src='images/liveOffline.gif';Ca.style.display='none';}}
if(Da&&(liveUserListHTML=='')){CancelDialog();}}
function LiveMessageSendWrapper(){var Fa=GetElem('liveMessage');var Ga=Fa.value;var Ha=GetElem('liveUsers');var Ia=Ha.value;if(Ga&&Ia){Fa.disabled=true;Ha.disabled=true;LiveMessageSend(Ga,Ia,'live');}}
function LiveDisplayMessage(){var Ja=OpenDialog(360,180,'About Live');Ja.innerHTML=' <div style="padding:5px 20px 20px 20px;" id="liveMessageWindow"> <form> \'Live\' is a way of talking with Charity Funding Solutions across the internet in real-time; when Edwin is online, the \'online\' light will come on, and a button will appear to send him a message. <br/><br/> He can then respond immediately to any query you might have. <div style="text-align:right;"> <input type="button" value="Close" onclick="CancelDialog();"/> </div> </form> </div>';}
var startLive=true;if(readCookie('cfsLive')=='offline'){startLive=false;}
if(!g_hasAnyEditPrivilege||startLive){loadHandlers.push(LiveStart);}
