//GLOBALS
var w3c = (document.getElementById) ? 1:0
var ns4 = (document.layers) ? 1:0  //browser detect for NS4 & W3C standards

function getObject(sObj) {
	if (w3c)
		theObj = document.getElementById(sObj);
	else if (ns4)
		theObj = eval("document." + sObj);
	return theObj;
}



function visual(aa){
	obj=getObject(aa);
	if (obj){
		if(obj.style.display=='none'){
			obj.style.display='block';
		}else{
			obj.style.display='none';
		}
	}
}




function motorericerca()
  {
  var tro=document.motore.f_cerca;
  if(tro.value=="")
    {
    alert('Devi inserire un testo per la ricerca');
    tro.focus();
    return (false);
    }
  else
    {
    return(true);
    }
  }

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}
function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}

function gb_inner(aaa,bbb){

	obj=getObject(aaa);
	obj.innerHTML = bbb;

}

function vai(url){
	if (confirm('Confermi la cancellazione?')){
	location.replace(url);
	}else{
	}
}



function apriPop(foto) {
	msg=open("","DisplayWindows"," toolbar=no,directories=no,menubar=no,resizable=no");
	d=msg.document;
	d.write("<head><SCRIP"+"T>");
	d.write("function ridimensiona(){");
	d.write("window.resizeTo(");
	d.write("document.im1.width+11,");
	d.write("document.im1.height+29);");
	d.write("};document.title=document.im1.src;</SCRIP"+"T></head>");
	d.write("<body topmargin=0 leftmargin=0 scroll=no>");
	d.write("<img id='im1' name='im1' src='"+foto+"' onLoad='ridimensiona()'>");
	d.write("</body>");
	d.close();
	msg.focus();
}


/*************************************************/
function xmlhttpPost(strURL, Theform, Thecontrol) {
	var xmlHttpReq = false;
	var self=this;
	//xmlhttprequest x mozilla/ie7
	if (window.XMLHttpRequest){
	  self.xmlHttpReq= new XMLHttpRequest();
	}
	//xmlhttprequest x ie6
	else if (window.ActiveXObject){
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}

	self.xmlHttpReq.open('POST',strURL,true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function() {
		if (self.xmlHttpReq.readyState==4){
	    	updatepage(self.xmlHttpReq.responseText,Theform,Thecontrol);
		}
	}
	self.xmlHttpReq.send(getquerystring(Theform));
}


  function getquerystring(Theform) {
    var form = document.forms[Theform];
    formNb = document.forms.length;
    qstr='';

  	if (formNb < 1) {
  		alert ("La pagina non contiene form !");
  		return;
  	}else if (formNb > 1 && !Theform) {
  			alert ("La pagina contiene pił di 1 form. Dare un nome alla form");
  			return;
  		}else {
  			formPath = formNb ? document.forms[Theform] : document.forms[0];
  			with (formPath) {
  				for (i=0; i<formPath.length; i++) {
  					 qstr += elements[i].name + "=" + escape(elements[i].value) + "&";
  				}
  			}
  		}
  	qstr += "end=1";
    return qstr;
  }

  function updatepage(str,Theform,Thecontrol) {
  	var msg='';
  	var test;
    var form = document.forms[Theform];
    test=str.split("|");
    if (test[0]=='ok') {
      form.reset();
    }

		for(i=1;i<test.length;i++)
		{
     msg+= test[i].split("|")+'<br />';
    }

    document.getElementById(Thecontrol).innerHTML=msg; //div dove mostrare il risultato

  }

/*************************************************/

function xmlhttpGet(strURL,funct) {
// per l'oggetto nativo XMLHttpRequest
 randomVal = Math.random()*1000000;
	if (window.XMLHttpRequest) {
		ricerca_pro = new XMLHttpRequest();
    if (funct) eval("ricerca_pro.onreadystatechange= function () { " + funct + " }");
		strURL = strURL + "&randomVal=" + randomVal;
    ricerca_pro.open("GET", strURL , true);
		ricerca_pro.send(null);
		// per IE
		} else if (window.ActiveXObject) {
			ricerca_pro = new ActiveXObject("Microsoft.XMLHTTP");
			if (ricerca_pro) {
			if (funct) eval("ricerca_pro.onreadystatechange= function () { " + funct + " }");
			strURL = strURL + "&randomVal=" + randomVal;
      		ricerca_pro.open("GET", strURL , true);
			ricerca_pro.send();
		}
	}
}

function ricevi() {
var strRes;
var arrValori;
/*
riceve una stringa in formato: testo#id|testo#id|
per popolare una select
*/
	if (ricerca_pro.readyState == 4) {
		strRes=ricerca_pro.responseText;
		arrValori=strRes.split("|");
		document.frmc.catid.options.length=0;
			for(i=0;i<arrValori.length;i++)
			{
				text = arrValori[i].split("|");
				text = text.toString();
				var str1=text;
				var str2=text;
				pos = str1.indexOf("#");
				text = str1.substring(pos,-str1.length);
				pos = str2.indexOf("#");
				val = str2.substring(pos+1,str2.length);
				document.frmc.catid.options[document.frmc.catid.options.length]= new Option(text,val);
			}
	}else{
	//	alert("Problemi con la ricerca!\r\nState:"+ ricerca_pro.readyState + " Status:" + ricerca_pro.status + " "+ ricerca_pro.statusText+"\r\n");
	}
}

function test(Thecontrol){
	if (ricerca_pro.readyState == 4) {
	  strRes=ricerca_pro.responseText;
	  document.getElementById(Thecontrol).innerHTML=strRes;
	}
}




/**/
function gestisci_risposta() {
/*
riceve un xml in formato:
<id>val1</id>
<ora>val2</ora>
<utente>val3</utente>
<messaggio>val4</messaggio>
*/
/*
var risposta = new ActiveXObject("Msxml2.DOMDocument.4.0");
risposta.async = false;
risposta.loadXML(ricerca_pro.responseXML.documentElement);
*/
alert(ricerca_pro.readyState);
var risposta =ricerca_pro.responseXML.documentElement;


	if (ricerca_pro.readyState == 4) {
		  try{

			  arr_id = risposta.getElementsByTagName("id"); //contiene l'array con tutti gli id trovati
			  arr_ora = risposta.getElementsByTagName("ora");
			  arr_utente = risposta.getElementsByTagName("utente");
			  arr_msg = risposta.getElementsByTagName("messaggio");

			  for(var conta=0; conta<arr_id.length; conta++){
					var ora = arr_ora.item(conta).firstChild.data.toString();
					var utente = arr_utente.item(conta).firstChild.data.toString();
					var messaggio = arr_msg.item(conta).firstChild.data.toString();

					var htmlMessage = "";
					htmlMessage += "<div class=\"messaggio\" >";
					htmlMessage += "[" + ora + "] " + utente + " ha detto: <br/>";
					htmlMessage += messaggio.toString();
					htmlMessage += "</div>";
					visualizza_messaggio (htmlMessage);
			  }

		  }catch(e){
			alert(e.toString());
		  }
	}
}

function visualizza_messaggio(messaggio){
  alert(messaggio);
  var scrolla = document.getElementById("scrolla_messaggi");
  scrolla.innerHTML += messaggio;
  scrolla.scrollTop = scrolla.scrollHeight;
}

/**/

