/*-------------------- openPop ----------------------------------*/
function openPop(myIndirizzo, myTarget, popTitle, option ){ 
	if ((popTitle) && (popTitle!=null) && (popTitle.length>0))
		myIndirizzo += (myIndirizzo.indexOf("?")>=0 ? "&" : "?") + "popTitle="+popTitle;
	if (option==null)
		if (myIndirizzo.indexOf("show?")>=0 ) {
			openPop4Media(myIndirizzo,myTarget);
			return;
		} else
			option = 'width=500,height=580,status=yes,resizable=yes,scrollbars=yes,toolbar=no,menubar=no';

	popUpWindow = window.open(myIndirizzo, myTarget, option);
	popUpWindow.focus();
}  
/* ---------------- openPop4Media with autosizing for true image objects -----------------*/
var cntPict=0;
var idPict=new Array();
function openPop4Media(img, target){
	cntPict++;
	idPict[cntPict] = new Image();
	idPict[cntPict].src = img;
	idPict[cntPict].target = target;
	targetWin = window.open("", target, "width=100,height=100"); //2006.05.03@FV fix "openpop from pop"
	if (targetWin!=window) targetWin.close(); //close pre-opened target window 
	var interrupt = "viewPop4Media("+cntPict+")";
	setTimeout( interrupt, 100 ); // set delay to allow data download
}
function viewPop4Media(id){
	var winOpt = "status=yes,resizable=yes,toolbar=no,menubar=no";
	if (idPict[id].width>50||idPict[id].height>50)	
		winOpt += ",width="+(idPict[id].width+20)+",height="+(idPict[id].height+25);

	popUpWindow = window.open(idPict[id].src, idPict[id].target, winOpt);
	popUpWindow.focus();
}
/*--------------------------- onloadFunctionAppender ----------------------*/
function onloadAddFunction( fnctn ) {
if (window.addEventListener) 
	window.addEventListener( 'load', fnctn, false );
else if (window.attachEvent)  
	window.attachEvent( 'onload', fnctn );
else window.onLoad = fnctn;
}

/*-------------------- getW3gDocumentURL ---------------------------*/
function getW3gDocumentURL(){
	var urlLimit = document.URL.indexOf("?");
	var strUrl = document.URL.substring( 0, (urlLimit>0 ? urlLimit : document.URL.length));
	if (w3gItemAndSezione!=null) strUrl += "?"+w3gItemAndSezione;
	return strUrl;
} 
/*-------------------- language ----------------------------------*/
var w3gItemAndSezione;
//2006.09.25@FV fix "location" value is not persistent on IE browser.
function language(idLanguage) {
	var strUrl = getW3gDocumentURL();
	strUrl += (strUrl.indexOf("?")>0 ? "&":"?") + "idLanguage=" + idLanguage;
	window.open(strUrl,'_self','');
}

/*-------------------- openerWindow ----------------------------------*/
function openerWindow( url ) {
var win;
try {
	if (opener!=null)
		win = opener.window.open( url, '_self','');
} catch (e) { 
	if (confirm("W3G lost synchronization. Please re-load main page"))
		window.close();
}
return win;
}

/*-------------------- formatData ----------------------------------*/
function formatData(campo){
   app = campo.value;
   lungh = app.length;
   // put the separator during the typing
   if(lungh == 3 || lungh == 6) campo.value = campo.value.substring(0,lungh-1) + "-";
   if(lungh == 11) campo.value = campo.value.substring(0,lungh-1) + " ";
   if(lungh == 14) campo.value = campo.value.substring(0,lungh-1) + ":";
} // formatData

/*------------------------- isDateTime ---------------------------------*/
function isDateTime(dateTime, messDescr, messFormat, messNumDays, messFebruary, messMonth, messYear, fullCondition) {
	var dateTimeStr = dateTime.value;
	var isOk = false;
	dateTime.value = (dateTimeStr.length>10) ? dateTimeStr.substring(0,10) : dateTimeStr; // isDate() need html object
	if (isDate(dateTime, messDescr, messFormat, messNumDays, messFebruary, messMonth, messYear, fullCondition) ) {
		dateTime.value = dateTimeStr;
		var timeStr  = (dateTimeStr.length>10) ? dateTimeStr.substring(11) : dateTimeStr;
		isOk = isTime( timeStr, messDescr, messFormat );
		if (!isOk) dateTime.focus();
	}
	return isOk;
}

/*------------------------- isTime ---------------------------------*/
function isTime(timeCrt, messDescr, messFormat ){
   var timePat = /^(\d{2})(\:)(\d{2})$/;
   var matchArray = timeCrt.match(timePat); // il formato è corretto?

   if(matchArray == null && timeCrt != "") {
	   alert(messDescr + " : " + messFormat);
      return false;
   }

   if(matchArray != null)  {    
	   hh = matchArray[1];
   	mm = matchArray[3];
   	if (!(hh.length==2 && hh>="00" && hh<"24" && mm.length==2 && mm>="00" && mm<"60" )) {
	   	alert(messDescr + " : " + messFormat);
      	return false;
      }
   }  

   return true;
}//isTime

/*------------------------- isDate ---------------------------------*/
function isDate(dateObj, messDescr, messFormat, messNumDays, messFebruary, messMonth, messYear, fullCondition) {
   dateCrt = dateObj.value;
   var datePat = /^(\d{2})(\-)(\d{2})(\-)(\d{4})$/;
   var matchArray = dateCrt.match(datePat); // il formato è corretto?
   if(matchArray == null && dateCrt != "") {
	   alert(messDescr + " : " + messFormat);
	   dateObj.focus();
      return false;
   }  
   if(matchArray != null)  {    
      day = matchArray[1];
      month = matchArray[3];
      year = matchArray[5];
    	// look the format
    	if (! checkDataValue("" + day, "" + month, "" + year, messDescr, messNumDays, messFebruary, messMonth, messYear, fullCondition) != "" ) {
		   dateObj.focus();
  			return false;
  		}
	}
   return true;
} // isDate



/*-------------------------- checkDataValue ------------------------*/
function checkDataValue(valGG, valMM, valYY, messDescr, messNumDays, messFebruary, messMonth, messYear, fullCondition){
   dataFormatted = "";
   if(valMM != "" && valGG != "" && valYY != "") {
      if (valGG > "31") { // more than 31 days
         alert (messDescr + " : "+ messNumDays );
         return dataFormatted;
      }
      if(valGG == "31") { // 31 days
         if(valMM == "04" || valMM == "06" || valMM == "09" || valMM == "11") {
            alert (messDescr + " : " + messNumDays );
            return dataFormatted;
         }
      }
      if(valMM == "02") { // feb with 28 days
         if (valGG > "29") {
            alert (messDescr + " : " + messNumDays );
            return dataFormatted;
         }
   	   // look if the number of days is 28 or 29 (leap year)
			var data = new Date(valYY, parseInt(valMM), 1);
         data = new Date(data - (24 * 60 * 60 * 1000));
         numDaysInMonth = data.getDate();
			if(parseInt(valGG) > parseInt("" + numDaysInMonth)) {
            alert (messDescr + " : " + messFebruary);
            return dataFormatted;
         }
      }
      // check the month
      if(valMM < "01" || valMM > "12") {
         alert (messDescr + " : " + messMonth);
         return dataFormatted;
      }
         
      if (fullCondition) {
        	// check the year only if fullCondition IS TRUE
      	if(valYY < "1900") {
         	alert (messDescr + " : " + messYear);
         	return dataFormatted;
      	}
      }
   }
   // returns the formatted data
   dataFormatted = valGG + "/" + valMM + "/" + valYY;
   return dataFormatted;
} // checkDataValue
  
/*-------------------------- adjustIFrameSize ------------------------*/
function adjustIFrameSize(iframeWindow) {
  if (iframeWindow.document.height) {
    var iframeElement = parent.document.getElementById(iframeWindow.name);
    iframeElement.style.height = iframeWindow.document.height + 'px';
    iframeElement.style.width = iframeWindow.document.width + 'px';
  }
  else if (document.all) {
    var iframeElement = parent.document.all[iframeWindow.name];
    if (iframeElement) {
	    if (iframeWindow.document.compatMode &&
	        iframeWindow.document.compatMode != 'BackCompat') 
	    {
	      iframeElement.style.height = iframeWindow.document.documentElement.scrollHeight + 'px';// + 5 + 'px';
	      iframeElement.style.width = iframeWindow.document.documentElement.scrollWidth + 'px';// + 5 + 'px';
	    }
	    else {
	      iframeElement.style.height = iframeWindow.document.body.scrollHeight + 'px';// + 5 + 'px';
	      iframeElement.style.width = iframeWindow.document.body.scrollWidth + 'px';// + 5 + 'px';
	    }
		 //move scrollbar to display window
		 parent.window.scrollTo(iframeWindow.screenLeft-iframeElement.style.pixelWidth,iframeWindow.screenTop-iframeElement.style.pixelHeight)
	}
  }
}

/*----------------------------- hideAdmin ----------------------------*/
function hideAdmin() {
	displayAdmin( window.name, "none", "none" );
}
  		  
/*----------------------------- showAdmin ----------------------------*/
function showAdmin( winName, noCover ) {
	displayAdmin( winName, "block", "none", noCover );
}

/*----------------------------- displayAdmin ----------------------------*/
function displayAdmin( winName, mode, modeInnerWin, noCover ) {

  if (winName == 'w3gAdminFrame') {

	   if (noCover==null || !noCover) {
		   // admin window blocks/releases browser page 
	  	   window.top.displayCover( 'Page', mode );
		   // admin window blocks/releases control panel
	  	   panel=window.open('','w3gPanel','height=1,width=1,status=no,toolbar=no,menubar=no,titlebar=no,scrollbar=no');
	  	   if (panel!=null)
		  	  try { panel.displayCover( 'Panel', mode ); } catch (e) { panel.close() }
	   }

	   window.parent.w3gAction.style.display=modeInnerWin;
	   window.parent.w3gList.style.display  =modeInnerWin;
	   window.parent.w3gAdmin.style.display =mode;

	   //reduce frame to 1x1 px, new content will resize window!  
	   hideFrame(window.parent.w3gActionFrame.window);
	   hideFrame(window.parent.w3gListFrame.window);
	   hideFrame(window.parent.w3gAdminFrame.window);
  }
  
  if (winName == 'w3gListFrame') {
	   window.parent.w3gAction.style.display=modeInnerWin;
	   window.parent.w3gList.style.display  =mode;
	   
	   hideFrame(window.parent.w3gActionFrame.window);
	   hideFrame(window.parent.w3gListFrame.window);
  }
  
  if (winName == 'w3gActionFrame') {
	   window.parent.w3gAction.style.display=mode;

  	   hideFrame(window.parent.w3gActionFrame.window);
  }	
}

/*---------------------------- hideFrame ------------------------------*/
function hideFrame (iframeWindow) {
  if (iframeWindow.document.height) {
    var iframeElement = parent.document.getElementById(iframeWindow.name);
    iframeElement.style.height = '1px';
    iframeElement.style.width = '1px';
  }
  else if (document.all) {
    var iframeElement = parent.document.all[iframeWindow.name];
    if (iframeWindow.document.compatMode &&
        iframeWindow.document.compatMode != 'BackCompat') 
    {
      iframeElement.style.height = '1px';
      iframeElement.style.width  = '1px';
    }
    else {
      iframeElement.style.height = '1px';
      iframeElement.style.width  = '1px';
    }
  }
}
/*2006.07.27@FV----------------------------- coverAdmin ----------------------------*/
coverStyleFilter="Alpha(Opacity=35, FinishOpacity=35, Style=2, StartX=50, StartY=50, FinishX=0, FinishY=0)"; 
function displayCover( name, status, msg ) {

	var cover = document.getElementById("w3g"+name+"Cover");
	var wait = document.getElementById("w3g"+name+"Wait");
	if (cover!=null) {
		if (status=='block') {
			try { // to extend cover to current window height&width.....
				cover.style.height= document.getElementById("w3gEndPage").offsetTop;
				cover.style.width= document.getElementById("w3gEndPage").offsetLeft;
			} catch (e) {}
			cover.style.filter = coverStyleFilter;
		}
		cover.style.display = status;
		//alert( "displayCover() w3g"+ name + "Cover = "+ status );
	}	
	if (wait!=null) {
		wait.style.display = status;
		//alert( "displayCover() w3g"+ name + "Wait = "+ status );
		if (msg!=null) wait.innerText = msg;
	}		
}
/*---------------------------- normalizeUTF8 ------------------------------*/
if (document.layers) { //in Netscape4 always filtered!
	window.captureEvents(Event.KEYPRESS);
	window.onkeypress = normalizeUTF8;
}
function normalizeUTF8( evt ) {
	wkc = (evt.which || evt.keyCode || evt.charCode);
	return (wkc<255)&&(wkc!=128);
}

/*2005.07.18@FV new--------------------------------- isEmail ------------------------------*/
function isEmail (s){
   if (s.length == 0) return (false);
   i = s.indexOf(" ");
   if (i > 0) return (false);
   indiceAt = s.indexOf("@");
   if (indiceAt <= 0) return (false);
   indiceUltimoPunto = s.lastIndexOf(".", s.length);
   if (indiceUltimoPunto <= 0) return (false);
   nomeDominio = s.substring((indiceAt+1), indiceUltimoPunto);
   if (!isDomainName(nomeDominio,1)) return (false);
   topLevelDomain = s.substring((indiceUltimoPunto+1), s.length);
   if (!isDomainName(topLevelDomain,2))  return (false);

   return (true);
}

/*2005.07.18@FV new------------------------------------- isDomainName ----------------------------*/
function isDomainName(checkStr, minLength){
  if (checkStr.length < minLength) return false;
  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.";
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  return allValid;
}

/*2005.09.01@FV new------------------------------------- myInnerText ----------------------------*/
function myInnerText( xStr ) {
   var regExp = /<\/?[^>]+>/gi;
   xStr = xStr.replace(regExp,"");
   return xStr;
}

/*2006.07.18@FV new------------------------------------- set/get cookie ----------------------------*/
function getCookie( cookieName ) {
	var cookieJar = document.cookie.split( "; " );
	for( var x = 0; x < cookieJar.length; x++ ) {
		var oneCookie = cookieJar[x].split( "=" );
		if( oneCookie[0] == escape( cookieName ) ) { 
		return unescape( oneCookie[1] ); 
		}
	}
	return null;
}

function setCookie( cookieName, cookieValue, lifeTime, path, domain, isSecure ) {
	if( !cookieName ) { return false; }
	if( lifeTime == "delete" ) { lifeTime = -1; } //this is in the past. Expires immediately.
	/* document.cookie = newValue is equivalent to document.cookie = newValue + "; " + document.cookie; */
	document.cookie = escape( cookieName ) + "=" + escape( cookieValue ) +
		( lifeTime ? ";expires=" + ( new Date( ( new Date() ).getTime() + ( 1000 * lifeTime ) ) ).toGMTString() : "" ) +
		( path ? ";path=" + path : "") + ( domain ? ";domain=" + domain : "") + 
		( isSecure ? ";secure" : "");
	//check if the cookie has been set/deleted as required
	if( lifeTime < 0 ) { if( typeof( getCookie( cookieName ) ) == "string" ) { return false; } return true; }
	if( typeof( getCookie( cookieName ) ) == "string" ) { return true; } return false;
}

/*2006.07.18@FV new------------------------------------- hotkey check ----------------------------*/
function isJSHotKeyActive(){
	var fname=''+document.onkeypress;
	var hkfname='HotKey';
	if (fname==''||fname==null) return false;
	// recupera il nome della funzione dal corpo
	if(fname.substr(0,'function'.length).toLowerCase()=='function')
		fname=fname.substr('function'.length+1,fname.indexOf('(')-('function'.length+1));
	return fname==hkfname?true:false;
}

