
/*
 * this triggers on the first time the chat is loaded
 * 
 * */
function go(){
	setCookie(document.getElementById("nickText").value,30);
	document.getElementById("nickText").style.display="none";
	document.getElementById("nickButton").style.display="none";
	document.getElementById("messageBox").style.display="inline";
	chatUpdate();
	window.setInterval("chatUpdate()",1000);
	}
	
	function setCookie(value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie="lc_nick=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	}	

/*
 * javascript inside-popup chat update event handler
 * 
 * */
function chatUpdate(nick,msg) {
	
	params = "?ajax=true";
	if (nick!=undefined && msg!=undefined) {
	params += "&nick="+nick+"&msg="+msg;
	}
	
var xmlHttp;
	try {
		xmlHttp=new XMLHttpRequest();
		}
	catch (e) {
	try {
    	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
	catch (e) {
	try {
    	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
    catch (e) {
    	alert("Your browser does not support AJAX!");
    	return false;
    	}
    }
	}
  	xmlHttp.onreadystatechange=function() {
    if(xmlHttp.readyState==4) {
      document.getElementById('mainChat').innerHTML=xmlHttp.responseText;
      if (document.getElementById("nickButton").style.display=="inline") {document.getElementById("messageBox").style.display="none";}
      }
    }
  xmlHttp.open("GET","chat.php"+params,true);
  xmlHttp.send(null);
  }
 
 /*
  * triggers chatUpdate when return key is pressed within the input box
  * 
  * */
 function sendMsg(e) {

   	var ch = e ? e.which : window.e.keyCode;
	if (ch==13) { 
		chatUpdate(document.getElementById("nickBox").nick.value,document.getElementById("inputBox").messageBox.value);
		document.getElementById("inputBox").messageBox.value="";
	}
}

/*
 * this manages onclick events that occur on the left menu in the index page updating the mainBox
 *  
 * */
function mainUpdate(menuEl) {
	
	params = "?ajax=true";
	if (menuEl!=undefined) {
	params += "&!="+menuEl;
	}
	
var xmlHttp;
	try {
		xmlHttp=new XMLHttpRequest();
		}
	catch (e) {
	try {
    	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
	catch (e) {
	try {
    	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
    catch (e) {
    	alert("Your browser does not support AJAX!");
    	return false;
    	}
    }
  }
  xmlHttp.onreadystatechange=function() {
  	 if(xmlHttp.readyState==1) {
      document.getElementById('mainBox').innerHTML='<div align="center"><br><br><img src="imgs/l.gif"></div>'; 
      }
    if(xmlHttp.readyState==4) {
      document.getElementById('mainBox').innerHTML=xmlHttp.responseText;
      }
    }
  xmlHttp.open("GET","main.php"+params,true);
  xmlHttp.send(null);
  }
/*
 * img upload popups
 * 
 * */
function popImg(a,hsh) {
window.open("up.php?a="+a+"&hsh="+hsh,"","top=200, left=200, width=300, height=300, status=no, menubar=no, toolbar=no scrollbar=no");
}
/*
 * toggle google maps
 * 
 */

function toggle(obj) {
  var toggle = document.getElementById(obj);
  if (toggle.style.display != "none") {
    toggle.style.display = "none";
  }
  else {
    toggle.style.display = "inline";
  }
}

/* bbcode */

function getSelection(ta)
  { var bits = [ta.value,'','','']; 
    if(document.selection)
      { var vs = '#$%^%$#';
        var tr=document.selection.createRange()
        if(tr.parentElement()!=ta) return null;
        bits[2] = tr.text;
        tr.text = vs;
        fb = ta.value.split(vs);
        tr.moveStart('character',-vs.length);
        tr.text = bits[2];
        bits[1] = fb[0];
        bits[3] = fb[1];
      }
    else
      { if(ta.selectionStart == ta.selectionEnd) return null;
        bits=(new RegExp('([\x00-\xff]{'+ta.selectionStart+'})([\x00-\xff]{'+(ta.selectionEnd - ta.selectionStart)+'})([\x00-\xff]*)')).exec(ta.value);
      }
     return bits;
  }

function matchPTags(str)
  { str = ' ' + str + ' ';
    ot = str.split(/\[[B|U|I].*?\]/i);
    ct = str.split(/\[\/[B|U|I].*?\]/i);
    return ot.length==ct.length;
  }

function addPTag(ta,pTag)
  { bits = getSelection(ta);
    if(bits)
      { if(!matchPTags(bits[2]))
          { alert('\t\tSelezione non valida!\nContiene dei tag aperti.');
            return;
          }
        ta.value = bits[1] + '[' + pTag + ']' + bits[2] + '[/' + pTag + ']' + bits[3];
      }
  }
