/**
 * @author r.roncolini
 */
var oDomDoc;
			var oldRowSelect;
			var r={
  					'special':/[\W]/g,
  					'quotes':/['\''&'\"']/g,
  					'notnumbers':/[^\d]/g
					}
			var indexRowSelect;		

			function valid(o,w){
  				o.value = o.value.replace(r[w],'');
 			}
			
		
			function resizeDivMain()
			{
			    									
				document.getElementById('idDivMain').style.height=document.documentElement.offsetHeight -100 ;
				
				if(IsNetScapeBrw)
				{					
					document.getElementById('idDivMain').style.width=document.documentElement.offsetWidth -20 ;				
				}
				
			}
			
			function IsNetScapeBrw()
			{
				if(navigator.appName.indexOf('Netscape')==0)
					return true;
				return false;														
			}
			
			function loadXml(xmlFile)
			{				
				oDomDoc = Sarissa.getDomDocument();
				oDomDoc.async=false;								
				oDomDoc.load(xmlFile);
				//alert(new XMLSerializer().serializeToString(oDomDoc));  
				 
				/*
				if(oDomDoc.parseError.errorCode != 0)
				{   
 					 alert("Errore");
				}   
				else
				{  
  					 alert("loaded ok");
			    }
			    */
     
			}
			function selNods(domNode, sXpath)
			{
				try
				{
				var valRit= new Array;
				var objNodeList = domNode.selectNodes(sXpath);
            	
            	for(i=0;i<objNodeList.length;i++)
				{                  
				  valRit[i]= new XMLSerializer().serializeToString(objNodeList[i]);		
				  //alert(valRit[i]);	
				  //alert(valRit.length)	; 
            	};
            	}
            	catch(error){}
            	return valRit;
			
				
			}
			function changeSelRow()
			{	
				
					
				if(oldRowSelect!=null)
				{
				if(this.rowIndex!=oldRowSelect.rowIndex)
				{
					if(this.style.backgroundColor='white')
					{
						this.style.backgroundColor='DDE5EB';
						this.childNodes[2].childNodes[0].style.backgroundColor='DDE5EB';
						if(oldRowSelect!=null)
						{
							oldRowSelect.style.backgroundColor='white';
							oldRowSelect.childNodes[2].childNodes[0].style.backgroundColor='E1EFFF';
						}	
					}
				}
				}
				else
				{
					if(this.style.backgroundColor='white')
					
						this.style.backgroundColor='DDE5EB';
						this.childNodes[2].childNodes[0].style.backgroundColor='DDE5EB';
				}
							
				oldRowSelect = this;
				indexRowSelect=this.rowIndex;
				
     		}
			
			function loadSelect(objID,arr)
			{
				var objTree=document.getElementById(objID);
				
				for(i=0;i<arr.length;i++)
				{					
					if(i%2==0)
					{
					 var objOpt=document.createElement('OPTION'); 
					 objOpt.appendChild(document.createTextNode(arr[i]));
					 objOpt.setAttribute('value',arr[i+1]);
					 objTree.appendChild(objOpt);					 
					}
					
					
						
					
				}
			}
			
			
			
			function getDettagliSos()
			{
				var arr=new Array;
				var objCmbMateriali=document.getElementById('idSelect')			
				if (objCmbMateriali.selectedIndex != -1)	
				{				
			 		arr[0] = objCmbMateriali.options[objCmbMateriali.selectedIndex].text;
					arr[1] = objCmbMateriali.options[objCmbMateriali.selectedIndex].value;
					
				}
				else
					return -1;
					//arr[0]=-1;
					
				return arr;					
			}
			function numeralsOnly(evt) {

    			evt = (evt) ? evt : event;
    			var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : 
        		((evt.which) ? evt.which : 0));

    			if (charCode > 31 && (charCode < 48 || charCode > 57)) {
					alert("Puoi inserire solo numeri!"); 
        			return false;
				}

    		return true;

			}
			function addRow()
			{
			  
                var s=getDettagliSos();
				if(s!=-1)
				{
					var materiale=s[0];
					var potereCal=s[1];
					
				}
				else
					return -1;
				
				var bodyTabMateriali=document.getElementById('idBodyTabMateriali');
				var row=document.createElement('TR');
				
				var celTipoMateriale=document.createElement('TD');
				celTipoMateriale.appendChild(document.createTextNode(materiale));
				
				var celPotereCalorico=document.createElement('TD');
				celPotereCalorico.style.textAlign='right';
				celPotereCalorico.appendChild(document.createTextNode(potereCal));
				
				
				var celQuantita=document.createElement('TD');
				var txt=document.createElement('INPUT');
				txt.setAttribute('type','text');
				//txt.setAttribute('style','width:100%;');
				txt.style.border='0';
				txt.style.backgroundColor='E1EFFF';
				txt.style.width='100%';
				txt.style.textAlign='right';								
				txt.setAttribute('mask','numeric');
				txt.setAttribute('value','0');				
				txt.className="editTxt";				
				txt.onblur=calcolaProdotto;
				txt.onkeypress= numeralsOnly;
				if(txt.addEventListener)
				{
					txt.addEventListener('click', function(){txt.select();}, false);
					//txt.addEventListener('keyup', function(){txt.value = txt.value.replace(r['notnumbers'],'');}, false);
					//txt.addEventListener('blur', function(){txt.value = txt.value.replace(r['notnumbers'],'');}, false);
					
				}
				else if(txt.attachEvent)
				{	
					txt.attachEvent('onclick',function(){txt.select();});					
					//txt.attachEvent('onkeyup',function(){txt.value = txt.value.replace(r['notnumbers'],'');});
					//txt.attachEvent('onblur',function(){txt.value = txt.value.replace(r['notnumbers'],'');});
				}
				
				celQuantita.appendChild(txt);
				
						
				var celProdotto=document.createElement('TD');
				celProdotto.style.textAlign='right';
				celProdotto.appendChild(document.createTextNode('0'));
				
				
				var celDelete=document.createElement('TD');
				celDelete.setAttribute('align','center');
				celDelete.onclick =deleteRow;				
				celDelete.className="delete";
				var btDelete=document.createElement('INPUT');
				btDelete.setAttribute('type','button');
				btDelete.setAttribute('title','Elimina riga selezionata.');
				btDelete.className='buttonDelete';
				btDelete.name='btDelete';
				btDelete.onmouseover=newImg;
				btDelete.onmouseout=oldImg;
				
				//btDelete.setAttribute('src','../images/delete.png');
				celDelete.appendChild(btDelete);
				
				
				row.appendChild(celTipoMateriale);
				row.appendChild(celPotereCalorico);
				row.appendChild(celQuantita); 
				row.appendChild(celProdotto);
				row.appendChild(celDelete);
				//row.onclick=changeSelRow;		
				
				
								
				bodyTabMateriali.appendChild(row);  
				row.id="idRow"+row.rowIndex;
				     
			}
			function oldImg()
			{
				this.style.backgroundImage= 'url(../images/delete.png)';
			}
			function newImg()
			{
				this.style.backgroundImage= 'url(../images/deleteDropShadow.png)';
				
			}
			function selRow(index)
			{
				if (index>0)
				{
					
					row = document.getElementById("idRow"+index);
					row.style.backgroundColor='DDE5EB';
					row.childNodes[2].childNodes[0].style.backgroundColor='DDE5EB';
					oldRowSelect=row;
				}												
			}
			function deleteRow()
			{
				
				var row=this.parentNode;
				var indexRow=row.rowIndex;
				var tabMateriali=document.getElementById("idTabMateriali");
				var numRows=tabMateriali.rows.length;
				tabMateriali.deleteRow(indexRow);
				document.getElementById('idLblCaricoIncendio').innerHTML=calcolaCarico();
				
				/*
				
				var numRows=tabMateriali.rows.length
				var flag;
				
				if(numRows>1)
					tabMateriali.deleteRow(indexRowSelect);
				
				if(indexRowSelect>1)
				{
					indexRowSelect=indexRowSelect-1;
					flag=true;					
					
				}
				else if(numRows>2)
				{					
					indexRowSelect=1
					flag=true;
				}
				if(flag)
				{
					var row=tabMateriali.rows[indexRowSelect]
					row.style.backgroundColor='DDE5EB';
					row.childNodes[2].childNodes[0].style.backgroundColor='DDE5EB';
					oldRowSelect=row;
					document.getElementById('idLblCaricoIncendio').innerHTML=calcolaCarico();
				}
				else
					oldRowSelect=null;
					*/				
			}
			
			function deleteAllRow()
			{
				
				var tabMateriali=document.getElementById("idTabMateriali");
				var iNumRow=tabMateriali.rows.length-1;
				
				//alert(iNumRow);
				for(i=iNumRow;i>=1;i--)
					tabMateriali.deleteRow(i);
				oldRowSelect=null;	
				document.getElementById('idLblCaricoIncendio').innerHTML="0";
				document.getElementById('idTxtsupLocale').value="0";
				
			}
			function calcolaProdotto()
			{
				
				var row=this.parentNode.parentNode   //this=Casella di testo  parentElement=Cella  parentElement=Riga;				
				var potereCalorico=parseFloat(row.childNodes[1].innerHTML);				
				var quantita=parseFloat(this.value);								
				var prodotto=potereCalorico*quantita;								
				//--------------------------------------------------------
				//RISOLVERE IL CASO NaN perchè in questo modo non funziona.
				//--------------------------------------------------------
				//if(!IsNan(prodotto))
					row.childNodes[3].innerHTML=prodotto;
				
				 document.getElementById('idLblCaricoIncendio').innerHTML=calcolaCarico();
			}
			function calcolaCarico()
			{
				var superfice=document.getElementById("idTxtsupLocale").value;
				superfice=superfice.replace(",",".")
				var sup=superfice*4400;
				var numMateriali=document.getElementById("idTabMateriali").rows.length-1;
				var prodotto;
				var sommaProdotti=0;
				var carico;
				
				if(numMateriali==0)
					carico=0;
					
				for (i=1;i<= numMateriali;i++)
				{
					prodotto=parseFloat(document.getElementById("idTabMateriali").rows[i].childNodes[3].innerHTML);
					sommaProdotti=sommaProdotti+prodotto;
					if(sup>0)
					{
						carico=Math.round(((sommaProdotti/sup)*100))/100;
					}
					else
						carico=0;
				}
				//alert(carico=='undefined');
				//if(carico.toUpperCase==undef.toUpperCase)
				//	carico=0;
					
				return carico;
			}
			function plus()
			{
				if(document.getElementById("idTabMateriali").rows.length>1)
				{
					var txtSupLocale= document.getElementById('idTxtsupLocale');
					var valSupLocale=parseFloat(txtSupLocale.value);				
					valSupLocale=valSupLocale+parseFloat("1");
					txtSupLocale.value=valSupLocale;																	
					document.getElementById('idLblCaricoIncendio').innerHTML=calcolaCarico();
				}
			} 
			function minus()
			{
				if(document.getElementById("idTabMateriali").rows.length>1)
				{
					var txtSupLocale= document.getElementById('idTxtsupLocale');
					var valSupLocale=parseFloat(txtSupLocale.value);				
					valSupLocale=valSupLocale-parseFloat("1");
					if(valSupLocale>=0)
						txtSupLocale.value=valSupLocale;
					
					document.getElementById('idLblCaricoIncendio').innerHTML=calcolaCarico();
				}
			}

		  function resizeTabMain()
			{							
			    document.getElementById('idTableMain').style.height=document.getElementById('idDivMain').style.height;			    
			}
			function init()
			{
				
				resizeDivMain();
				resizeTabMain();
				loadXml("../xml/Materiali.xml");
				var list=selNods(oDomDoc,"//dataroot/Materiali/tipo/text() | //dataroot/Materiali/potecalo/text()");				
				loadSelect("idSelect",list);
								
			}
			function resize()
			{
				resizeDivMain();
				resizeTabMain();
			}
			
			window.onload=init;
			window.onresize=resize;
