/*-------------------- openPop ----------------------------------*/
function openPop(myIndirizzo, myTarget, popTitle, option ){ 
	if ((popTitle) && (popTitle!=null) && (popTitle.length>0))
		myIndirizzo += (myIndirizzo.indexOf("?")>=0 ? "&" : "?") + "popTitle="+popTitle;
	if (option==null) 
		option = 'width=500,height=580,status=yes,resizable=yes,scrollbars=yes,toolbar=no,menubar=no';

	popUpWindow = window.open(myIndirizzo, myTarget, option);
	popUpWindow.focus();
}  

/*-------------------- language ----------------------------------*/
var w3gItemAndSezione;
function language(idLanguage) {
	strUrl = location.pathname + "?idLanguage=" + idLanguage;
	if (w3gItemAndSezione!=null) strUrl += w3gItemAndSezione;
	strUrl += location.hash;
	self.location.replace( strUrl );
}

/*-------------------- openerWindow ----------------------------------*/
function openerWindow( url ) {
	if (opener!=null)
		win = opener.window.open( url, '_self','');
		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 ) {
	displayAdmin( winName, "block", "none" );
}

/*----------------------------- displayAdmin ----------------------------*/
function displayAdmin( winName, mode, modeInnerWin ) {

  if (winName == 'w3gAdminFrame') {
	   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';
    }
  }
}

/*---------------------------- 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);
}

/*--------------------------- onloadFunctionAppender ----------------------*/
function onloadAddFunction( fnctn ) {
if (window.addEventListener) 
	window.addEventListener( 'load', fnctn, false );
else if (window.attachEvent)  
	window.attachEvent( 'onload', fnctn );
else window.onLoad = fnctn;
}

/*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;
}
