
<!--
	// If True, a typed smiley (e.g. :-) will be replaced by a small image
	// representation.
	// Default Value: true
	var USE_IMAGE_SMILEY=true;
	
	// user browser will navigate to this page when user logges off
	var LOGOFF_PAGE="logoff.asp";
	
	// Time in milliseconds to ping server. Increase for greater scalability
	// Decrease for faster response time. If the value is changed, also
	// change the REQUEST_TIMEOUT_INT below accordingly.
	// Default Value: 1000 (1 Second)
	var PING_INTERVAL=1000

	var PING_INTERVAL_READ_ONLY=3000
	
	// number of intervals in which request to the server would timeout
	// increase this number if you decrease ping interval value proportianlly
	// Default Value: 10
	var REQUEST_TIMEOUT_INT=10;
	
	var g_remoteServer = "server.asp";
	var g_intervalID = 0;
	var g_bWaitingForResp = 0;
	var g_iMsgTo = -1;

	var g_iSkipCount = 0;
	function callServer(sParams,bFromTimer) {
		var sRn=Math.random();
	
		var head = document.getElementsByTagName('head').item(0);
		var old  = document.getElementById('srvCmnds');
		if (old){
			if (bFromTimer && old.readyState=="loading" && g_iSkipCount<REQUEST_TIMEOUT_INT){
				g_iSkipCount++;
				return;//let the old script complete
			}
			head.removeChild(old);
			g_iSkipCount=0;
		}				
		try{
			var s=document.createElement('script');
			s.src=g_remoteServer+'?r='+ sRn + sParams;
			s.type = 'text/javascript';
			s.defer = true;
			s.id = 'srvCmnds';
//********
//		if (document.test) document.test.testo.value = "CALL: " + s.src;
//********
			void(head.appendChild(s));
		}catch(e){
			var fr=window.frames["idHidSrv"];
			if (!fr){
				alert("server frame is not found");	return;
			}
			fr.document.open();
			fr.document.write("<s" + "cript src=\"" + g_remoteServer + "?i=1&r=" + sRn + sParams + "\" id=\"srvCmnds\" defer=\"true\" language=\"JavaScript1.2\" type=\"text/javascript\"></s" + "cript>");
			fr.document.close();
		}
	}

	function process_response(resp){
		var sResp=String(resp);
		var sMsg,sUsers,sRooms,sError='';
		
//********
//		if (document.test) document.test.testo.value = resp + " g_intervalID=" + g_intervalID;
//********
		
		if (sResp.length<1){
			sError='response is empty';
			return false;
		}
			
		aResp=sResp.split(String.fromCharCode(1));
		if (aResp.length<3 || aResp.length>4){
			timerClear();
			sError='response contains ' + aResp.length + ' parts';
			alert(sError);
			return false;
		}else{
			sMsg=aResp[0];
			sUsers=aResp[1];
			sRooms=aResp[2];
			if (aResp.length>3){
				sError=aResp[3];
			}
			refresh_layers(sMsg,sUsers,sRooms,sError);
			return true;
		}
	}
	function rewriteIFrame(id, html, bScroll){
		if (!window.frames[id]) return;
		var l=window.frames[id];
		//l.document.body.innerHTML=;
				
		l.document.open();
		l.document.write("<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"css/chat.css\"></head><body style=\"background-color:#F8F8F8; margin-top: 0px; margin-left: 0px; margin-right: 0px; margin-bottom: 0px;\">" + html + "</body></html>");
		l.document.close();
		
		if (bScroll && l.scrollBy) window.setTimeout("scrollIFrame('" + id + "')",10);
	}
	function scrollIFrame(id){
		var l=window.frames[id];
		if (l  && l.scrollBy) l.scrollBy(0,32000);
    }
    
	function refresh_layers(sMsg, sUsers, sRooms, sError){
		var sHtml;
			
		if (String(sMsg).length>0){
			if(g_bIsSound){
				if(document.soundnewmsg){
					if (navigator.plugins){
						if (navigator.plugins["LiveAudio"]){
							document.soundnewmsg.play(false);
						}else if(document.all){  
							try{
								if (document.soundnewmsg.IsSoundCardEnabled()){
									document.soundnewmsg.play();
								}else{
									window.status="Sound card is not enabled.";
								}
							}catch(e){
								window.status="Error playing sound: " + e.message;
							}
						}else{
							window.status="Browser does not support sound";
						}
					}
				}else{
					window.status="Sound control is not found";
				}
			}
			
			sHtml=format_msgs(sMsg);
			rewriteIFrame('idDivMsgs', sHtml, true);
			
			if(document.chatcontrols){
				if (g_bIsAlert && document.chatcontrols.mytext.value.length<1){
					window.focus();
					document.chatcontrols.mytext.focus();
				}
			}
		}

		if (String(sUsers).length>0){
			sHtml=format_users (sUsers);
			rewriteIFrame ('idDivUsers', sHtml, false);
			PrivateUserStyleUpd (g_iMsgTo);
		}
		if (String(sRooms).length>0){
			sHtml = format_rooms (sRooms);
			rewriteIFrame ('idDivRooms', sHtml, false);
		}
		if (String(sError).length>0){
			act_on_error (sError);
		}
		return;
	}
	function format_msgs(sRaw){
		var re,html=String(sRaw);
		if (html.length<1) return '';
		
		re = new RegExp("([Hh][Tt][Tt][Pp][Ss]?\://[A-Za-z0-9_@#\$\%\&\*\+=\?\.\:\;\/\-]{4,})","g");html = html.replace(re,"<a href=\"$1\" target=\"_new\">$1</a>");
		
		//re = new RegExp("([^><\000-\040]{50})(\\S)","g");
		//html = html.replace(re,"$1\n$2");
		
		re = new RegExp("\002\005","g");			
		html = html.replace(re,"</td></tr><tr><td class=\"msgAreaNmC\">");			
		re = new RegExp("\002","g");			
		html = html.replace(re,"</td></tr><tr><td class=\"msgAreaNm\">");
		re = new RegExp("\004\005","g");			
		html = html.replace(re,"</td></tr><tr><td class=\"msgAreaNmPC\">");			
		re = new RegExp("\004","g");			
		html = html.replace(re,"</td></tr><tr><td class=\"msgAreaNmP\">");
		
		re = new RegExp("([0-9]+)\003([^\003]+)\003([^\003]+)\003","g");
		html = html.replace(re,"<img src=\"images/av/$1.gif\" height=19 width=19 align='absmiddle' hspace=2><font color=\"$2\">$3:</font></td><td class=\"msgAreaMsg\" width=\"95%\"><font color=\"$2\">");

		re = new RegExp("([0-9]+)\003([^\003]+)\003([^\003]+)\003","g");
		html = html.replace(re,"<img src=\"images/av/$1.gif\" height=19 width=19 align='absmiddle' hspace=2><font color=\"$2\">$3:</font></td><td class=\"msgAreaMsg\" width=\"95%\"><font color=\"$2\">");

		re = new RegExp("\003","g");			
		html = html.replace(re,":</td><td class=\"msgAreaMsg\" width=\"95%\">");
		
		re = new RegExp("\006","g");			
		html = html.replace(re,"</td></tr><tr><td class=\"msgArealIn\" colspan=2><img src=\"images/door_in.gif\" height=16 width=16 align='absmiddle' hspace=2>");
		re = new RegExp("\007","g");			
		html = html.replace(re,"</td></tr><tr><td class=\"msgArealOt\" colspan=2><img src=\"images/door_out.gif\" height=16 width=16 align='absmiddle' hspace=2>");
		re = new RegExp("\010","g");			
		html = html.replace(re,"</td></tr><tr><td class=\"msgAreaInf\" colspan=2>");
			
		if (html.substring(0,5)=="</td>")	html=html.substring(5,html.length);
		if (html.substring(0,5)=="</tr>")	html=html.substring(5,html.length);
		
		html ="<table border=0 cellpadding=2 cellspacing=1 width=\"100%\">"+html+"</td></tr></table>";
		
		if (USE_IMAGE_SMILEY){
			var smArr=new Array(1,	"O:[\\+-]?\\)",		"O:+)",		2,	":[-o]?\\[",	":[",
								3,	":D",			":D",			4,	":o\\)",		":o)",
								5,	"xx\\(",		"xx(",			6,	":[-o]?\\]",	":]",
								7,	":[-o]?\\(",	":(",			8,	":-?O",			":O",
								9,	":-?\\)",		":)",			10,	":P",			":P",
								11, ";[-o]?\\)",	";)",			12,	"\\{\\)",		"{)")
			var i=0,iLen=smArr.length;
			while(i<iLen){
				re = new RegExp(smArr[i+1],"g");			
				html = html.replace(re,"<img src=\"images/smiley/" + smArr[i] + ".gif\" alt=\"" + smArr[i+2] + "\">");
				i+=3;
			}
		}
		return html;
	}
	function format_users(sRaw){
		var html=String(sRaw);
		if (html.length<1) return '';
		var sFormat = '';
		var re = new RegExp("\004([0-9]+)\003([0-9]+)\003([^\003]+)\003([^\003]*)\003([^\002]+)","g");//lets first find ourself there
		sFormat = '<tr height=22>';
		sFormat = sFormat + '<td class="usrAreaCur" width=25><img src="images/av/$4.gif" width=19 height=19 border=0 alt="$5"></td>';
		sFormat = sFormat + '<td class="usrAreaCur" width="100%" id="usr_$1"><font color="$3">$5</font></td>';
		sFormat = sFormat + '<td class="usrAreaCur" width=22 >&nbsp;</td>';

		sFormat = sFormat + '<td class="usrAreaCur">&nbsp;</td>';

		sFormat = sFormat + '<td class="usrAreaCur">&nbsp;</td>';
		sFormat = sFormat + '</tr>';
		html = html.replace(re, sFormat);
		re = new RegExp("\002([0-9]+)\003([0-9]+)\003([^\003]+)\003([^\003]*)\003([^\002\<]+)","g");
		sFormat = '<tr height=22 >'
		sFormat = sFormat + '<td class="usrAreaOther" width=25 id="usr_$1"><img src="images/av/$4.gif" width=19 height=19 border=0 alt="$5"></td>';
		sFormat = sFormat + '<td class="usrAreaOther" width=100% id="usr_$1_1"><font color="$3">$5</font></td>';
		sFormat = sFormat + '<td class="usrAreaOther" width=22 id="usr_$1_2"><a href="javascript:parent.PrivateModeSwitch($1,\'$5\')"><img src="images/prv_msg.gif" width=15 height=15 border=0 alt="$5"></a></td>';

		sFormat = sFormat + '<td class="usrAreaOther" width=22 id="usr_$1_3">&nbsp;</td>';
		sFormat = sFormat + '<td class="usrAreaOther" width=22 id="usr_$1_4">&nbsp;</td>';

		sFormat = sFormat + '</tr>';
		html = html.replace(re, sFormat);
		html = '<table border=0 cellpadding=1 cellspacing=0 width="100%">'+html+'</table>';
		return html;
	}

	function format_rooms(sRaw){
		var html=String(sRaw);
		if (html.length<1) return '';
		
		var re=new RegExp("\002\004([0-9]+)\003([^\002]+)","g");			
		sFormat = '<tr height=22 >';
		sFormat = sFormat + '<td class="roomAreaCur" width=25 ><img src="images/group.gif" width=25 height=22 border=0 alt="$2"></td>';
		sFormat = sFormat + '<td class="roomAreaCur" width=100% >$2</td>';
		sFormat = sFormat + '<td class="roomAreaCur" width=22 ><a href="javascript:parent.RemoveRoom_refresh($1)"><img src="images/delete.gif" width=15 height=15 border=0 alt="$2"></a></td>';
		sFormat = sFormat + '</tr>';
		html=html.replace(re, sFormat);

		re = new RegExp("\002([0-9]+)\003([^\002\<]+)","g");			
		sFormat = '<tr height=22 >';
		sFormat = sFormat + '<td class="roomAreaOther" width=25 ><img src="images/group.gif" width=25 height=22 border=0 alt="$2"></td>';
		sFormat = sFormat + '<td class="roomAreaOther" width=100% ><a href="javascript:parent.SwitchRoom_refresh($1)">$2</a></td>';
		sFormat = sFormat + '<td class="roomAreaOther" width=22 ><a href="javascript:parent.RemoveRoom_refresh($1)"><img src="images/delete.gif" width=15 height=15 border=0 alt="$2"></a></td>';
		sFormat = sFormat + '</tr>';
		html=html.replace(re, sFormat);

		html='<table border=0 cellpadding=1 cellspacing=0 width="100%">'+html+'</table>';
		return html;
	}
		
	function act_on_error(sError){
		var html=String(sError);
		if (html.length<1) return;
			
		var aResp=html.split(String.fromCharCode(2));
		if (aResp.length<2){
			alert("error:" + sError);
			return;
		}
		var errCode=parseInt(aResp[0]);
		if (errCode==1){
			//alert(aResp[1]);
			document.location="expired.asp"
		}else if (errCode==3){
			alert("Errore creando una nuova stanza. " + aResp[1]);
		}else if (errCode==4){
			alert("Errore cancellando una stanza. " + aResp[1]);
		}else if (errCode==5){
			alert("Errore cambiando stanza. " + aResp[1]);
		}else if (errCode==12 || errCode==13){
			alert("Errore inviando un messaggio. " + aResp[1]);
		}else if (errCode==100){
			alert("Attenzione: " + aResp[1]);
		}else{
			alert("error code='" + aResp[0] + "'");
		}
	}
		
	function send_msg(){	
		if(!document.chatcontrols) return;

		var sText=String(document.chatcontrols.mytext.value);
		if (sText.length<1) return;
		
		if (g_bIsBold) sText="<b>" + sText + "</b>";
		if (g_bIsItalic) sText="<i>" + sText + "</i>";
		if (g_bIsUnd) sText="<u>" + sText + "</u>";
		if (g_sTxtColor.length>0) sText="<font color=\"" + g_sTxtColor + "\">" + sText + "</font>";
		
		var re = new RegExp("\\%","g");	
		sText=sText.replace(re,"%25");
		re = new RegExp("\\+","g");	
		sText=sText.replace(re,"%2b");
		re = new RegExp("\\#","g");	
		sText=sText.replace(re,"%23");
		re = new RegExp("\\&","g");	
		sText=sText.replace(re,"%26");
		callServer('&m='+ sText+ '&ut='+g_iMsgTo, 0);
		document.chatcontrols.mytext.value='';
	}
		
	function sections_refresh(isForseMsgs,isForseUsers,isForseRooms){	
		if(!document.chatcontrols) return;

		var iFlags=isForseMsgs | isForseUsers<<1 | isForseRooms<<2;
		//alert(iFlags);
		callServer('&f='+iFlags,0);
		document.chatcontrols.mytext.value='';
		return;
	}
		
	function addRoom_refresh(){
		var sRoomName=document.frmAddRoom.addroomname.value;
			
		if (String(sRoomName).length<1){
			return;
		}
		callServer('&ra='+sRoomName,0);
		document.frmAddRoom.addroomname.value='';
	}
		
	function RemoveRoom_refresh(roomid){
		if (String(roomid).length<1){
			return;
		}
		if (!confirm('Sei sicuro di voler rimuovere la stanza?')){
			return;
		}
		callServer('&rr='+roomid,0);
	}
	function SwitchRoom_refresh(roomid){
		if(!document.chatcontrols) return;
		if (String(roomid).length<1){
			return;
		}
		if (!confirm('Sei sicuro di voler cambiare stanza?')){
			return;
		}
		callServer('&rs='+roomid,0);
		document.chatcontrols.mytext.focus();
	}
	
	function PrivateModeSwitch(iUserId, sUserName){
		if (iUserId!=-1 && !confirm('Sei sicuro di voler avere una comunicazione privata con l\'utente "' + sUserName + '"\r\nATTENZIONE:\r\nSe tu rispondi "Si" allora tutti i messaggi saranno inviati a "' + sUserName + '".\r\nPer tornare alla modalita\' normale clicca sul link "Utenti".')){
			return;
		}
		if (iUserId==-1 && iUserId==g_iMsgTo){
			alert('Sei gia\' in modalita\' normale.\r\n\r\nClicca su questo link per cambiare tra la modalita\' personale e normale.');
			return;
		}
		PrivateUserStyleUpd(iUserId);
	}
	
	function PrivateUserStyleUpd(iUserId){
		if (!window.frames["idDivUsers"]) return;
		var oUser;
		var oUsrWnd=window.frames["idDivUsers"];
		
		if(!oUsrWnd){
			alert("Lista Utenti non trovata. Viene rimpostata la Modalita' Normale.");
			g_iMsgTo = -1;
			return;
		}
		if (g_iMsgTo >= 0){
			oUser=oUsrWnd.document.getElementById('usr_'+ g_iMsgTo);
			if (oUser) oUser.className='usrAreaOther';
			oUser=oUsrWnd.document.getElementById('usr_'+ g_iMsgTo+'_1');
			if (oUser) oUser.className='usrAreaOther';
			oUser=oUsrWnd.document.getElementById('usr_'+ g_iMsgTo+'_2');
			if (oUser) oUser.className='usrAreaOther';
			oUser=oUsrWnd.document.getElementById('usr_'+ g_iMsgTo+'_3');
			if (oUser) oUser.className='usrAreaOther';
			oUser=oUsrWnd.document.getElementById('usr_'+ g_iMsgTo+'_4');
			if (oUser) oUser.className='usrAreaOther';
		}
		if (iUserId >= 0){
			oUser=oUsrWnd.document.getElementById('usr_'+ iUserId);
			if (oUser){
				oUser.className='usrAreaPrv';
				oUser=oUsrWnd.document.getElementById('usr_'+ iUserId+'_1');
				if (oUser) oUser.className='usrAreaPrv';
				oUser=oUsrWnd.document.getElementById('usr_'+ iUserId+'_2');
				if (oUser) oUser.className='usrAreaPrv';
				oUser=oUsrWnd.document.getElementById('usr_'+ iUserId+'_3');
				if (oUser) oUser.className='usrAreaPrv';
				oUser=oUsrWnd.document.getElementById('usr_'+ iUserId+'_4');
				if (oUser) oUser.className='usrAreaPrv';
			}else{
				alert("L'utente richiesto non e' stato trovato. Viene rimpostata la Modalita' Normale.");
				g_iMsgTo = -1
				return;
			}
		}
		g_iMsgTo = iUserId;
	}
		
	function TextBoxWantEnter(e) {
		var whichCode=e.keyCode;
		if (whichCode == 13) {
			send_msg();
		} 
		return true;  
	}
		
	function RoomBoxWantEnter(e) {
		var whichCode=e.keyCode;
		if (whichCode == 13) {
			addRoom_refresh();
		} 
		return true;  
	}
		
	function initWindow(){
		callServer('&f=7', 0);
		g_intervalID = setInterval("callServer('&f=0', 1)", PING_INTERVAL);
		if(document.chatcontrols) document.chatcontrols.mytext.focus();
	}

	function initWindowReadOnly(){
		callServer('&ro=1', 0);
		g_intervalID = setInterval("callServer('&ro=1', 1)", PING_INTERVAL_READ_ONLY);
		if(document.chatcontrols) document.chatcontrols.mytext.focus();
	}

	function termWindow(){
		timerClear();
		callServer('&dc=1', 0);
	}
				
	function openHelp(){
		window.open('help.asp', 'help', 'toolbar=no,location=no,resizable=yes,scrollbars=yes,width=420,height=480');
	}
		
	function timerClear(){
		if(g_intervalID!=0) window.clearTimeout(g_intervalID);
	}
		
	function doLogout(){
		timerClear();
		document.location=LOGOFF_PAGE;
	}
	
	function callLogout(){
		if (window.confirm("Sei sicuro di volerti scollegare dalla Chat SportAutoMoto?")){
			timerClear();
			callServer('&lo=1',0);
		}
	}
	
	var gPopupTimerId;
	var gMouseIn=0;
	function DivShow(sDivId,e,iXoffset,iYoffset){
		var divEl=document.getElementById(sDivId);
		if (divEl){
			divEl.style.left=e.clientX+iXoffset;
			divEl.style.top=e.clientY+iYoffset;
			divEl.style.visibility = "visible";
			gPopupTimerId=window.setTimeout("DivHide('" + sDivId + "',0)",4000);
		}
	}
	
	function DivHide(sDivId,bForce){
		if (gMouseIn && !bForce) return;
		var divEl=document.getElementById(sDivId);
		if (divEl) divEl.style.visibility = "hidden";
	}

	function textadd(sDivId,sTxt){
		document.chatcontrols.mytext.value=document.chatcontrols.mytext.value + sTxt;
		DivHide(sDivId,1);
		document.chatcontrols.mytext.focus();
	}
	
	function mOver(){
		if (gPopupTimerId)	window.clearTimeout(gPopupTimerId);
		gMouseIn=1;
	}
	
	function mOut(sDivId){
		gMouseIn=0;
		gPopupTimerId=window.setTimeout("DivHide('" + sDivId + "',0)",500);
	}
	
	var g_bIsBold=false;
	var g_bIsItalic=false;
	var g_bIsUnd=false;
	function txtTypeTougle(chType){
		switch(chType){
			case 'b': g_bIsBold=!g_bIsBold; document.imgbold.src=g_bIsBold?"images/t_b_d.gif":"images/t_b.gif"; break;
			case 'i': g_bIsItalic=!g_bIsItalic; document.imgitalic.src=g_bIsItalic?"images/t_i_d.gif":"images/t_i.gif"; break;
			case 'u': g_bIsUnd=!g_bIsUnd; document.imgund.src=g_bIsUnd?"images/t_u_d.gif":"images/t_u.gif"; break;
			default: return;
		}
		txtStyleChange();
	}
	function txtStyleChange(){
		var sClass="inputText";
		
		if (g_bIsBold) sClass+="b";
		if (g_bIsItalic) sClass+="i";
		if (g_bIsUnd) sClass+="u";
		document.chatcontrols.mytext.className=sClass;
	}

	g_sTxtColor = "";
	function txtColorChange(sColor){
		g_sTxtColor = sColor;
		document.chatcontrols.mytext.style.color = g_sTxtColor;
		var oColorBtn=document.getElementById("idColorChoice");
		if (oColorBtn) oColorBtn.style.backgroundColor = g_sTxtColor;
		DivHide("popColors",1);
	}

	var g_bIsSound=false;
	function ChatSoundTougle(){
		g_bIsSound=!g_bIsSound;
		document.imgsound.src=g_bIsSound?"images/btn_sound_down.gif":"images/btn_sound_up.gif";
	}
	
	var g_bIsAlert=false;
	function ChatAlertTougle(){
		g_bIsAlert=!g_bIsAlert;
		document.imgalert.src=g_bIsAlert?"images/btn_alert_down.gif":"images/btn_alert_up.gif";
	}

	function ChangeAv(iAv){
		var sMsg="Sei sicuro di cambiare avatar?";
		if (confirm(sMsg)) {
			callServer("&ca=1&idav="+iAv, 0) 
		}
		return false;
	}	


//-->
