// JavaScript Document
// Esegue sumbit
var ns6=document.getElementById&&!document.all;
var in_color = '#FFFFFF';

function hov(loc,cls){
   if(loc.className)
	  loc.className=cls;
}

// Make div visible
function enableLoading() {
document.getElementById('div_loading').style.visibility = 'visible';
}

// Hide div
function disableLoading() {
document.getElementById('div_loading').style.visibility = 'hidden';
}

function visualizza_info (testo, colore)
{
	document.getElementById('BOX_MESSAGGI').innerHTML = testo;
	document.getElementById('BOX_MESSAGGI').style.color = colore;
	document.getElementById('BOX_MESSAGGI').style.visibility = 'visible';
	document.getElementById('BOX_MESSAGGI').style.fontweight = 'bold';
	setTimeout("document.getElementById('BOX_MESSAGGI').style.visibility = 'hidden';",10000);
}

function seleziona_cella(index_row,index_cell, id_grid)
	{
		var grid = getAjaxGrid(id_grid);
		if(grid.getSelectedCellIndex() != index_cell)
			grid.selectCell(grid.getRowIndex(index_row), index_cell, false, false, true, true);
	}


function go(where, fun) {
	var d = new Date();
	var time = d.getTime();
    document.body.style.cursor='wait';
	document.forms[0].fun.value = fun;
	document.forms[0].action = where+'?time='+time;
	document.forms[0].submit();
}

function showPage(layoutCollection, row4page, nextPage, action){
	document.forms[0].layoutCollection.value = layoutCollection;
	document.forms[0].nextPage.value = nextPage;
	document.forms[0].row4page.value = row4page;
	go(action, 'showPage');
}


function set_modificato(oggetto){
	if ( oggetto != null ) {
		in_color = "#FFF0F0";
	}
	return false;
}

function unevidenzia(oggetto){
	oggetto.style.background=in_color;
	in_color = "#FFFFFF";
	return false;
}

function evidenzia(oggetto){
	in_color = oggetto.style.backgroundColor;
	oggetto.style.background="#FFFF66";
	return false;
}

function getComuniPerProvincia(div_name, nome_combo, id_combo, ele){
	document.body.style.cursor='wait';
	if ( ele == '' ) {
		var srcHolder = document.getElementById(div_name);
		srcHolder.innerHTML = "";

		comuni=new dhtmlXCombo(div_name,nome_combo,200);
		comuni.clearAll();
		comuni.enableFilteringMode(true);

	} else {
		var url = "Ajax.do?fun=getComuniOfProvincia&_COD_ISTAT_PROV=" + ele + "&tipo=HTML" ;
		creaComboHtml(url, div_name, nome_combo, id_combo);
	}
	document.body.style.cursor='default';
}

function getProvincePerRegione(div_name, nome_combo, id_combo, ele){
	document.body.style.cursor='wait';
	var url = "Ajax.do?fun=getProvinceOfRegione&_COD_ISTAT_REG=" + ele + "&tipo=HTML" ;
	creaComboHtml(url, div_name, nome_combo, id_combo);

	// pulisce il combo dei comuni
	var srcHolder = document.getElementById('div_cb_comuni');
	srcHolder.innerHTML = "";

	comuni=new dhtmlXCombo('div_cb_comuni','_COD_ISTAT_COM',200);
	comuni.clearAll();
	comuni.enableFilteringMode(true);

	document.body.style.cursor='default';
}




function getRegioneOfProvincia(id_combo, ele){
	document.body.style.cursor='wait';
	if ( ele == '' ) {
		var obj = document.getElementById('_COD_ISTAT_REG');
		obj.options[0].selected=true;
	} else {
		var url = "Ajax.do?fun=getRegioneOfProvincia&_COD_ISTAT_PROV=" + ele;
//		var ret = setValore(url, '_COD_ISTAT_REG_SEL');
	}
	document.body.style.cursor='default';
}


// #######################################################################################################

function getValueOfRadioGroup(gruppo){
	var valore = '';
	for (i=0;i<gruppo.length;i++) {
		  if (gruppo[i].checked) {
				 valore = gruppo[i].value;
		  }
	}
	return valore;
}


// questo serve se cambio anno
function CaricaXml_cambioAnno(url,id_grid) {

		changed = true;
        var httpRequest;
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');

                // See note below about this line
            }
        }
        else if (window.ActiveXObject) { // IE
            try { httpRequest = new ActiveXObject("Msxml2.XMLHTTP"); }
			catch (e) {
               try { httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); }
               catch (e) {}
            }
        }

        if (!httpRequest) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        httpRequest.onreadystatechange = function() { letturaXml_cambioAnno(httpRequest,id_grid); };
        httpRequest.open('GET', url, true);
        httpRequest.send('');


}


function letturaXml_cambioAnno_2(xml,id_grid)
{
	//alert(xml);
		var xmlDoc=new ActiveXObject("Msxml2.DomDocument.4.0");
				//alert(xmlDoc);
				xmlDoc.async=false;
				//alert(testo);
				xmlDoc.loadXML(xml);
				//alert(xmlDoc.documentElement);
				readXML(xmlDoc.documentElement,id_grid);
		//readXML(xml);
		//nuova_riga(id_grid);// MESSO QUI PER OVVIARE AL TIME OUT CHE NON VA BENEEEEEEE!!!!
		getAjaxGrid(id_grid).cells(getAjaxGrid(id_grid).getSelectedRowId(),4).setValue(data_nuovo_mov);
		getAjaxGrid(id_grid).cells(getAjaxGrid(id_grid).getSelectedRowId(),2).setValue(num_nuovo_mov);
		getAjaxGrid(id_grid).cells(getAjaxGrid(id_grid).getSelectedRowId(),3).setValue(anno_nuovo_mov);


}

function letturaXml_cambioAnno(httpRequest,id_grid)
{
		var testo = "";
        if (httpRequest.readyState == 4) {
            if (httpRequest.status == 200) {
				testo = httpRequest.responseText;
				var xmlDoc=new ActiveXObject("Msxml2.DomDocument.4.0");
				//alert(xmlDoc);
				xmlDoc.async=false;
				//alert(testo);
				xmlDoc.loadXML(testo);
				//alert(xmlDoc.documentElement);
				readXML(xmlDoc.documentElement,id_grid);
				//nuova_riga(id_grid);// MESSO QUI PER OVVIARE AL TIME OUT CHE NON VA BENEEEEEEE!!!!
				getAjaxGrid(id_grid).cells(getAjaxGrid(id_grid).getSelectedRowId(),2).setValue('');
				getAjaxGrid(id_grid).cells(getAjaxGrid(id_grid).getSelectedRowId(),3).setValue(anno_nuovo_mov);
				getAjaxGrid(id_grid).cells(getAjaxGrid(id_grid).getSelectedRowId(),4).setValue(data_nuovo_mov);
				getAjaxGrid(id_grid).cells(getAjaxGrid(id_grid).getSelectedRowId(),2).setValue(num_nuovo_mov);
            }
		}

}


// questo serve quando inserisco un nuovo movimento


function CaricaXml(url,id_grid) {

		changed = true;
        var httpRequest;
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');

                // See note below about this line
            }
        }
        else if (window.ActiveXObject) { // IE
            try { httpRequest = new ActiveXObject("Msxml2.XMLHTTP"); }
			catch (e) {
               try { httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); }
               catch (e) {}
            }
        }

        if (!httpRequest) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        httpRequest.onreadystatechange = function() { letturaXml(httpRequest,id_grid); };
        httpRequest.open('GET', url, true);
        httpRequest.send('');


}


function letturaXml (httpRequest,id_grid)
{
		var testo = "";
        if (httpRequest.readyState == 4) {
            if (httpRequest.status == 200) {
				testo = httpRequest.responseText;
				var xmlDoc;
    			try{
      				//Internet Explorer
      				xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		    	}catch(e){
     				try{
        				//Firefox, Mozilla, Opera, etc.
			        	xmlDoc=document.implementation.createDocument("","",null);
      				}catch(e){
          				alert(e.message)
      			}
    		}
   		//alert(xmlDoc);
		xmlDoc.async=false;
		//alert(testo);
		xmlDoc.loadXML(testo);
		//alert(xmlDoc.documentElement);
		readXML(xmlDoc.documentElement,id_grid);
		nuova_riga(id_grid);// MESSO QUI PER OVVIARE AL TIME OUT CHE NON VA BENEEEEEEE!!!!
            }
		}

}

function readXML(node) {

  var cn = node.childNodes;
  var ct = node.childNodes.length;
  for (var i=0; i<ct; i++)  {
  	if(cn[i].nodeName == '#text')
  		{
  			var pn = cn[i].parentNode;
  			//alert(pn.nodeName);
  			//alert(cn[i].nodeValue);
  			if(pn.nodeName == 'anno_nuovo_mov')
				anno_nuovo_mov = cn[i].nodeValue;
			else if(pn.nodeName == 'num_nuovo_mov')
				num_nuovo_mov = cn[i].nodeValue;
			else
				data_nuovo_mov = cn[i].nodeValue;
  		}
  	if(cn[i].childNodes.length > 0) {
      		readXML(cn[i]);
    	}
   /* var cn = node.childNodes;
  var ct = node.childNodes.length;
  for (var i=0; i<ct; i++)  {
    document.write('---- Nodo ' + cn[i].nodeName +
    ' ----<br />');
    var pn = cn[i].parentNode;
    document.write('Nodo Padre: ' + pn.nodeName);
    document.write("<br />");
    document.write('Tipo:' + cn[i].nodeType);
    document.write("<br />");
    document.write('Nome: ' + cn[i].nodeName);
    document.write("<br />");
    document.write('Valore: ' + cn[i].nodeValue);
    document.write("<br />");

    if(cn[i].nodeType == 1) {
      var attrs = cn[i].attributes;
      document.write("---- Attributi ----<br />");
      for(x=0; x < attrs.length; x++) {
        document.write(attrs[x].name + ' = ' + attrs[x].value);
        document.write("<br />");
      }
      document.write("<br />");
    }
    var ps = cn[i].previousSibling;
    if(ps) {
      document.write('Fratello Prec.: ' + ps.nodeName);
      document.write("<br />");
    }
    var ns = cn[i].nextSibling;
    if(ns) {
      document.write('Fratello Succ.: ' + ns.nodeName);
      document.write("<br />");
    }
    document.write('N. Figli: ' + cn[i].childNodes.length);
    document.write("<br /><br />");
    if(cn[i].childNodes.length > 0) {
      readXML(cn[i]);
    }


	}*/
}
}

function toAsciiKeyCode(inStr) {
    var str=inStr;
    var code=new Array(str.length);
    for(var i=0;i<str.length;i++){
        code[i]=str.charCodeAt(i)+'$';
    }
    var newStr = '';
    for(var j=0;j<code.length;j++){
        newStr+=code[j];
    }
    return(newStr.substring(0,newStr.length-1));
}


