var IEBrowser = (navigator.appName=="Microsoft Internet Explorer")?true:false;
var myStyleSheet = document.styleSheets.item(0);

/*
if(!IEBrowser){
	myStyleSheet.insertRule("div#panelMenu{border-bottom:4px double white;}", myStyleSheet.cssRules.length);
	myStyleSheet.insertRule("div#panelBottom{border-top:4px double white;}", myStyleSheet.cssRules.length);
}else{
	myStyleSheet.addRule("div#panelMenu","border-bottom:4px double #C7000D;",myStyleSheet.rules.length);
	myStyleSheet.addRule("div#panelBottom","border-top:4px double #C7000D;",myStyleSheet.rules.length);
}
*/

function setMaxHeight(tag){
   var t=document.getElementsByTagName(tag);
   
   for(var i=0;i<t.length;i++){
      if(parseFloat(t[i].style.width)>620){
         t[i].style.width='620px';
       }
   }
}
      
      
function setObjHeight(obj){
   var minH=document.body.clientHeight;  
   
   if(minH<600)minH=600;
   
   if(IEBrowser){
      document.styleSheets[0].addRule(obj,"{height:"+minH+"px;}");
   }else{
      document.styleSheets[0].insertRule(obj+"{height:"+(minH)+"px;}",10);
   }
   
}


function statusMsg(msgStr){
 window.status=msgStr;
 return true;
}

function allCell(cella){
   cella.link=cella.getElementsByTagName("a")[0].href;
   if(cella.link.length>0){
      cella.className="liOver";
      cella.style.cursor="pointer";
      cella.onclick=function(){document.location.href=this.link};
      cella.onmouseout = function(){cella.className="";}
   }
}

var initwin;

function OpenPopup(winWID,winHEI,winURL,winSCR){
	var winLeft=(screen.availWidth-winWID)/2; 
  var winTop=(screen.availHeight-winHEI)/2; 
  
  if(initwin==null){opener=window;}
  
  if(opener.popwin!=null){opener.popwin.close();}
  
	opener.popwin = window.open(winURL,'zzz','status=no, menubar=no, toolbar=no, scrollbars=' + winSCR + ' , resizable=no,copyhistory=0,width=' + winWID + ',height=' + winHEI + ',top='+winTop+',left='+winLeft);
}

function checkFile(){
   var objForm=document.frmUpload;
   var fileUpl=objForm.txtUpload.value;
   var fileDesc=objForm.txtDesc.value;
   
   if(fileUpl.length==0 && objForm.action.indexOf('idFile')==-1 ){
      alert("Nessun elemento da caricare!");
      return;
   }
   
   if(fileUpl.length>=255){
      alert("Percorso non valido! Nome troppo lungo");
      return;
   }
   
   if(fileDesc.length==0){
      alert("Inserire la descrizione del file!");
      return;
   }
   
   objForm.txtTitle.value=document.getElementById('fileTitle').value;
   objForm.action=objForm.action+'&txtDesc='+fileDesc+'&txtTitle='+objForm.txtTitle.value+'&newfil='+fileUpl.length;
   
   objForm.submit();
   
}

function checkImage(){
   var objForm=document.frmImg;
   var fileUpl=objForm.txtUpload.value;
   var fileDesc=objForm.txtDesc.value;
   
   if(fileUpl.length==0 && objForm.action.indexOf('idFile')==-1 ){
      alert("Nessun elemento da caricare!");
      return;
   }
   
   if(fileUpl.length>=255){
      alert("Percorso non valido! Nome troppo lungo");
      return;
   }
   
   if(fileDesc.length==0){
      alert("Inserire la descrizione dell'imagine!");
      return;
   }
   
   objForm.txtTitle.value=document.getElementById('fileTitle').value;
   objForm.action=objForm.action+'&txtDesc='+fileDesc+'&txtTitle='+objForm.txtTitle.value+'&newfil='+fileUpl.length;
   
   objForm.submit();
   
}
function checkLink(){
   
   var lnkDesc=document.frmLink.txtDesc.value;
   
   if(lnkDesc.length==0){
      alert("Inserire la descrizione del file!");
      return;
   }
   
   document.frmLink.txtTitle.value=document.getElementById('lnkTitle').value;
   document.frmLink.submit();
   
}

function modFile(idf){
   var fdes=document.getElementById('fdes'+idf);
   var flnk=document.getElementById('flnk'+idf);
   var objShow=document.frmUpload;
   
   objShow.btnUpload.value='Modifica';
   
   if(objShow.action.indexOf("idFile")!=-1){
      objShow.action=objShow.action.substring(0,objShow.action.lastIndexOf('&'));
   }
   
   objShow.action=objShow.action+'&idFile='+idf;
   
   objShow.txtDesc.value=fdes.innerHTML;
}


function modLink(idf){
   var fdes=document.getElementById('fdes'+idf);
   var flnk=document.getElementById('flnk'+idf);
   var objShow=document.frmLink;
   
   objShow.btnUpload.value='Modifica';
   
   if(objShow.action.indexOf("idFile")!=-1){
      objShow.action=objShow.action.substring(0,objShow.action.lastIndexOf('&'));
   }
   
   objShow.action=objShow.action+'&idFile='+idf;
   
   objShow.txtDesc.value=fdes.innerHTML;
   objShow.txtLink.value=flnk.innerHTML.substring(7,flnk.innerHTML.length);      
}


function showDiv(showId,hideId){
   var divShow=document.getElementById(showId);
   var divHide=document.getElementById(hideId);
   
   divShow.style.display='';
   divHide.style.display='none';
}


/********************************************************************/
var listNews; 
var newsContainer; 
var interval = 10;
var showed = 7;

function parseNews(divId) {
  newsContainer = document.getElementById(divId);
  listNews = newsContainer.childNodes;
  
  for(var i=0;i<listNews.length;i++){
    var news = listNews[i];
    news.style.display='none';
  }
  
  showNews(0);
}

function showNews(cursor){
   
   if(cursor > 0){
      for(var i=0;i<showed;i++){
         if( typeof(listNews[(cursor-1)*showed+i])=='object'){
            listNews[(cursor-1)*showed+i].style.display='none';
         }else{
            cursor = 0;
            break;
        }
      }
   }
   
   if(cursor*showed>=listNews.length)
      cursor=0;
      
   for(var i=0;i<showed;i++){
      if( typeof(listNews[cursor*showed+i])=='object'){
         listNews[cursor*showed+i].style.display='';
      }else{
         break;
      }
   }
   
   cursor++;
   
   window.setTimeout("showNews('" + cursor + "')", interval*1000);
}

/********************************************************************/


function insValue(fields,values){
   var lstFields=fields.split("|");
   var lstValues=values.split("|");
   
   for(i=0;i<lstFields.length;i++){
      var d=document.getElementById(lstFields[i])
      
      if(d.tagName=='SELECT'){
         for(var k=0; k<d.options.length;k++){
            if(d.options[k].value==lstValues[i]){
               d.selectedIndex=k;
               break;
            }
         }
      }else
         d.value=lstValues[i];
   }
}

/*******************************************************/

function checkClass(){
   var msg='';
   var ooT=document.getElementById('objTeam');
//   var ooP=document.getElementById('objP');
   var ooG=document.getElementById('objG');
   var oPV=document.getElementById('objPV');
//   var oPP=document.getElementById('objPP');
   var oPF=document.getElementById('objPF');
   var oPS=document.getElementById('objPS');
   
   var vvT=ooT.value;
//   var vvP=parseInt(ooP.value);
   var vvG=parseInt(ooG.value);
   var vPV=parseInt(oPV.value);
//   var vPP=parseInt(oPP.value);
   var vPF=parseInt(oPF.value);
   var vPS=parseInt(oPS.value);
   
   if(vvT.length==0) msg=msg+"\t- la squadra\n";
//   if(vvP==0 || isNaN(vvP) ) msg=msg+"\t- il punteggio\n";
   if(isNaN(vvG) ) msg=msg+"\t- le partite giocate\n";
   if(isNaN(vPV) ) msg=msg+"\t- le partite vinte\n";
//   if(vPP==0 || isNaN(vPP) ) msg=msg+"\t- le partite perse\n";
   if(isNaN(vPF) ) msg=msg+"\t- i canestri fatti\n";
   if(isNaN(vPS) ) msg=msg+"\t- i canestri subiti\n";
   
//   if(msg.length==0){
//      if(vPV+vPP!=vvG)
//         msg=msg+"\t- le partite giocate non corrispondono alla somma tra vinte e perse\n";
//   }
   
   if(msg.length==0){
//      vvP.value=vvP;
      vvG.value=vvG;
      vPV.value=vPV;
//      vPP.value=vPP;
      vPF.value=vPF;
      vPS.value=vPS;
      document.frm.submit();
      return;
   }else{
      msg="Inserire:\n\n"+msg;
      alert(msg);
   }
}

function checkPlay(){
   var msg='';
   var n=document.getElementById('objNome');
   var r=document.getElementById('objRuolo');
   var a=document.getElementById('objAnno');
   var h=document.getElementById('objAlt');
   
   var nn=n.value;
   var rr=parseInt(r.value);
   var aa=parseInt(a.value);
   var hh=parseInt(h.value);
   
   if(nn.length==0) msg=msg+"\t- il nome\n";
   if(rr==0) msg=msg+"\t- il ruolo\n";
   
   if(rr!=7 && rr!=8 && rr!=9){
      if(aa==0 || isNaN(aa) ) msg=msg+"\t- l'anno di nascita\n";
      if(hh==0 || isNaN(hh) ) msg=msg+"\t- l'altezza \n";
   }
   
   if(msg.length==0){
      a.value=aa;
      h.value=hh;
      document.frm.submit();
      return;
   }else{
      msg="Inserire:\n\n"+msg;
      alert(msg);
   }
}

function checkTeam(){
   var msg='';
   var n=document.getElementById('objNome');
   var a=document.getElementById('objScore');
   
   var nn=n.value;
   var aa=parseInt(a.value);
   
   if(nn.length==0) msg=msg+"\t- il nome\n";
   if(aa==0 || isNaN(aa) ) msg=msg+"\t- il punteggio\n";
   
   if(msg.length==0){
      a.value=aa;
      document.frm.submit();
      return;
   }else{
      msg="Inserire:\n\n"+msg;
      alert(msg);
   }
}

function checkStagione(){
   var msg='';
   var n=document.getElementById('objStag');
   
   var nn=n.value;
   
   if(nn.length==0) msg=msg+"\t- la stagione\n";
   
   if(msg.length==0){
      document.frm.submit();
      return;
   }else{
      msg="Inserire:\n\n"+msg;
      alert(msg);
   }
}

function checkScore(){
   var msg='';
   var dd=document.getElementById('objData');
   var s1=document.getElementById('objS1');
   var s2=document.getElementById('objS2');
   var r1=document.getElementById('objR1');
   var r2=document.getElementById('objR2');
   
   var ddd=dd.value;
   var ss1=parseInt(s1.value);
   var ss2=parseInt(s2.value);
   var rr1=parseInt(r1.value);
   var rr2=parseInt(r2.value);
   
   if(ddd.length==0) msg=msg+"\t- la data\n";
   if(ss1==0) msg=msg+"\t- la squadra di casa\n";
   if(ss2==0) msg=msg+"\t- la squadra ospite\n";
   if(rr1==0 || isNaN(rr1) ) msg=msg+"\t- il risultato della squadra di casa\n";
   if(rr2==0 || isNaN(rr2) ) msg=msg+"\t- il risultato della squadra ospite\n";
   
   if(msg.length==0){
      r1.value=rr1;
      r2.value=rr2;
      document.frm.submit();
      return;
   }else{
      msg="Inserire:\n\n"+msg;
      alert(msg);
   }
}


function checkClas(){
   var msg='';
   var dd=document.getElementById('objDescr');
   var ll=document.getElementById('objLnk');
   
   var d=dd.value;
   var l=ll.value;
   
   if(d.length==0) msg=msg+"\t- la descrizione\n";
   if(l.length==0) msg=msg+"\t- il link\n";
   
   
   if(msg.length==0){
      document.frm.submit();
      return;
   }else{
      msg="Inserire:\n\n"+msg;
      alert(msg);
   }
}

function checkCal(){
   var msg='';
   var dd=document.getElementById('objDescr');
   var ll=document.getElementById('objLnk');
   var ic=document.getElementById('idClas');
   
   var d=dd.value;
   var l=ll.value;
   var i=ic.value;
   
   if(i.length>0) l=1;
   
   if(d.length==0) msg=msg+"\t- la descrizione\n";
   if(l.length==0) msg=msg+"\t- il file\n";
      
   if(msg.length==0){
      document.frm.action=document.frm.action+'&objDescr='+d+'&idClas='+i;
      document.frm.submit();
      return;
   }else{
      msg="Inserire:\n\n"+msg;
      alert(msg);
   }
}

function checkGallery(){
   var msg='';
   var n=document.getElementById('objNome');
   var d=document.getElementById('objDesc');
   
   var nn=n.value;
   var dd=d.value;
   
   if(nn.length==0) msg=msg+"\t- il nome\n";
   if(dd.length==0) msg=msg+"\t- la descrizione\n";
   
   if(msg.length==0){
      document.frm.submit();
      return;
   }else{
      msg="Inserire:\n\n"+msg;
      alert(msg);
   }
}

function refreshParent(){
   this.opener.document.location.href=this.opener.document.location.href; 
}

function showBanner(cntBanner){
   
   var banner=document.getElementById('banner');
   var srcBan=document.getElementById('banner_'+cntBanner);
   
   if(srcBan==null){
      cntBanner=1;
      srcBan=document.getElementById('banner_'+cntBanner);
   }
   
   banner.innerHTML="<img src='/public/banner/" + srcBan.value + "' border=0 />"
   
   cntBanner++;
   
   setTimeout("this.showBanner(" + cntBanner + ")", 3500);
}

function load() {
	var mp=document.getElementById("map");
	
	if(mp!=null){
		
	  if (GBrowserIsCompatible()) {
	    var map = new GMap2(mp);
	    
	    map.disableDragging();
	    map.enableContinuousZoom();
	    map.enableScrollWheelZoom();
	    map.addControl(new GSmallZoomControl());
	    map.addControl(new GOverviewMapControl());
	    map.addControl(new GMapTypeControl());
	    
	    map.setCenter(new GLatLng(45.718601,12.199654), 15); //45.424078,11.943622
	    
	    map.addOverlay(new GMarker(new GLatLng(45.718601,12.199654)));
	   
	  }
	}else{
		GUnload=function(){};
	}
}
/********************************************************************/
