//==========================================================================================================
//=== TRIM
//==========================================================================================================
//---  LeftTrim: elimina tutti gli spazi a sinistra della stringa ------------------------------------------
function LeftTrim(theString)
{
	var RE;
	RE = /^\s+/gi; // tutti gli spazi, a partire dall'inizio della stringa
	return (theString.replace(RE, ""));
}
//---  RightTrim: elimina tutti gli spazi a destra della stringa -------------------------------------------
function RightTrim(theString)
{	var RE;

	RE = /\s+$/gi; // tutti gli spazi, a partire dalla fine della stringa
	return (theString.replace(RE, ""));
}
//---  Trim: elimina tutti gli spazi a sinistra e a destra della stringa -----------------------------------
function Trim(theString)
{
	theString = LeftTrim(theString);
	theString = RightTrim(theString);

	return (theString);
}

//==========================================================================================================
//=== Le funzioni di controllo hanno parametri di ingresso dati di tipo stringa; restituiscono:
//=== 	 true: se il controllo ha avuto esito positivo
//===    false: se il controllo ha avuto esito negativo
//==========================================================================================================
//---  IsStringUndefined: stringa non valorizzata ----------------------------------------------------------
function IsStringUndefined(theString)
{
	//alert("IsStringUndefined")
	return false;
}
//---  IsStringBlank: stringa contenente solo spazi --------------------------------------------------------
function IsStringBlank(theString)
{
	//alert("IsStringBlank")
	//... Blank ............................................
	if ((Trim(theString)) == "") return true;
	// ... Else ............................................
	return false;
}
//---  IsStringEmpty: stringa "vuota" ----------------------------------------------------------------------
function IsStringEmpty(theString)
{
	//alert("IsStringEmpty")
	//... Undefined ........................................
	if (IsStringUndefined(theString)) return true;
	//... Blank ............................................
	if (IsStringBlank(theString)) return true;
	// ... Else ............................................
	return false;
}

//==========================================================================================================
//---  IsNumberInteger: numero intero ----------------------------------------------------------------------
function IsNumberInteger(theNumber)
{
	var RE;
	var matchArray;

	theNumber += ""; // cast a stringa
	RE = /-?\d+/; // sequenza di un numero qualsiasi di cifre, eventualmente precedute da "-"
	matchArray = theNumber.match(RE);
	//--- Se il numero è diverso dalla sequenza estratta
	if ((matchArray == null) || (theNumber != matchArray[0])) return false;
	//--- Else
	return true;
}

//==========================================================================================================
//---  ValidatorNumerico(object): input solo determinati caratteri -----------------------------------------
function ValidatorNumber(object) {
	
 // Se window.event.returnValue è non definito oppure è false non si applica il validator
 // per non sovrascrivere l'azione di un eventuale validator scatenato prima di quello attuale
 if (window.event.returnValue == "undefined" || window.event.returnValue == false) return;
 
 // Il tasto Enter in caso di textbox multiline deve essere sempre consentito
 if (window.event.keyCode == 13) return;
 
 var values = "0123456789";
 if (values.indexOf(String.fromCharCode(window.event.keyCode)) == -1)
  window.event.returnValue = false;
}

//==========================================================================================================
//--- LenMax(Value, Len): Funzione interna per la lunghezza massima [AF, SS, 16 June 2003]
function LenMax(Value, Len)
{
	if ((!(Value == "")) && (!(Value.length <= Len)))
	{
		return (0);
	}
	return (1);
}

//==========================================================================================================
//---  checkMail(TextBox): controlla campo e-mail ----------------------------------------------------------
function checkMail(TextBox)
{
	return(controllaMail(TextBox, true));
}

//--- ControllaMail(TextBox,avviso): Funzione e-mail [AF, SS, 16 June 2003]
function controllaMail(TextBox,avviso)
{
	apos=TextBox.indexOf("@");
	dotpos=TextBox.lastIndexOf(".");
	lastpos=TextBox.length-1;

	if (TextBox.value == "")
		return(1);
		if (!(LenMax(TextBox, 50))) {
		if (avviso=true) alert("Inserire al più 50 caratteri nel campo.");
		//TextBox.focus();
		return 0;
	}
	if (apos<1 || dotpos-apos<2 || lastpos-dotpos>4 || lastpos-dotpos<2) {
		if (avviso=true) alert('Inserire un indirizzo e-mail valido');
		//TextBox.focus();
		return(0);
	}
	return 1;
}

function IsDateEmpty(dd, mm, yyyy)
{	
	//... Data vuota....................................................
	if ((dd == 0) && (mm == 0) && (IsStringEmpty(yyyy)))  return true;
	//... Else .........................................................
	return false;
}
//---  IsYearNotValid: anno non di quattro cifre
function IsYearNotValid(yyyy)
{	
	if (yyyy.search(/\d{4}/) == -1) return true; 
	//... Else ..........................................................
	return false;
}

//==========================================================================================================
//---  IsDateNotValid(dd, mm, yyyy): controlla campo e-mail ------------------------------------------------
function IsDateNotValid(dd, mm, yyyy)
{ 
 //... Data vuota: data valida ........................................
 if ((dd == 0) && (mm == 0) && (IsStringEmpty(yyyy)))  return false;
 //... Data incompleta: data non valida ...............................
 if ((dd == 0) || (mm == 0) || (IsStringEmpty(yyyy)))  return true;

 //... Anno non corretto: non valida ..................................
 if (IsYearNotValid(yyyy)) return true;
 //... Data non corretta (es. 31 giugno, 29 febbraio di anno non bisestile...): data non valida .............................
 var testDate = new Date(yyyy, mm-1, dd)
 if (!((yyyy==testDate.getFullYear()) && (mm==(testDate.getMonth()+1)) && (dd==testDate.getDate()))) return true;
 //... Else ..........................................................
 return false;
}

//==========================================================================================================
//--- IsDateGreaterThan: prima data più recente della seconda ----------------------------------------------
function IsDateGreaterThan(dd_1, mm_1, yyyy_1, dd_2, mm_2, yyyy_2)
{ 
 var date_1 = new Date(yyyy_1, mm_1-1, dd_1);
 var date_2 = new Date(yyyy_2, mm_2-1, dd_2);
 
 date_1 = date_1.getTime();
 date_2 = date_2.getTime();
 
 if (date_1 > date_2) return true;
 //... Else ..........................................................
 return false;
}

//--- PopUpStatistiche (documento): Apertura Pop Up statistiche [UG, MG, 21 July 2005]
function PopUpStatistiche(documento) {
	var customWindow
	customWindow = window.open(documento, "popUpStatistiche", "toolbar=no, scrollbars= yes, location=no, directories=no, status=no, menubar=no, resizable=no, width=800, height=400");
	customWindow.moveTo(100,150);
	customWindow.focus();
}

//--- checkNumAccessi(theForm): Controllo per form ultimi accessi statistiche [UG, MG, 21 July 2005]
function checkNumAccessi(theForm) {
	if (theForm.numAccessi.value== "") {
		alert("Compilare il campo.");
		theForm.numAccessi.focus();
		return;
	}
	
	if ( !(IsNumberInteger(theForm.numAccessi.value)) ) {
		alert ("E' necessario inserire un valore numerico.");
		theForm.numAccessi.value = '';
		theForm.numAccessi.focus();
		return;
	}
		
	theForm.action = "statistiche.asp?Idstep=1";
	theForm.submit();
}



function setFolderNavigationClass(idFolder) {

	switch (parseInt(idFolder)) {
		case 1:	//--- Serv Prov
			document.getElementById("label_1").className = "label_1_on";
			document.getElementById("label_2").className = "label_2_off";
			document.getElementById("label_3").className = "label_3_off";			

			document.getElementById("div_1").className = "div_1_on";
			document.getElementById("div_2").className = "div_2_off";
			document.getElementById("div_3").className = "div_3_off";
			
			document.getElementById("div_folder").className = "div_folder_1";
		break;
		
		case 2:	//--- Serv On Line
			document.getElementById("label_1").className = "label_1_off";
			document.getElementById("label_2").className = "label_2_on";
			document.getElementById("label_3").className = "label_3_off";

			document.getElementById("div_1").className = "div_1_off";
			document.getElementById("div_2").className = "div_2_on";
			document.getElementById("div_3").className = "div_3_off";
			
			document.getElementById("div_folder").className = "div_folder_2";			
		break;

		case 3:	//--- Sil
			document.getElementById("label_1").className = "label_1_off";
			document.getElementById("label_2").className = "label_2_off";
			document.getElementById("label_3").className = "label_3_on";

			document.getElementById("div_1").className = "div_1_off";
			document.getElementById("div_2").className = "div_2_off";
			document.getElementById("div_3").className = "div_3_on";
			
			document.getElementById("div_folder").className = "div_folder_3";			
		break;

	}		
	
}

//----------------------------------------------------
//--- mostraNascondiDiv(elem)
function mostraNascondiDiv(elem)
{	
	if(document.getElementById(elem).style.display=='block')
	{
		document.getElementById(elem).style.display='none';
	}
	else
	{
		document.getElementById(elem).style.display='block';
	}
}

//----------------------------------------------------
//--- printPage()
function printPage()
{
	document.getElementById(1).style.display='none';
	document.getElementById(2).style.display='none';
	window.print();
	//document.getElementById(1).style.display='block';
	//document.getElementById(2).style.display='block';
}

//----------------------------------------------------
//--- checkForumReg(TheForm)
function checkForumReg(TheForm)
{
	if(TheForm.txtNome.value.length<2)
	{
		alert("Inserire un nome valido");
		TheForm.txtNome.focus();
		return true;
	}
	if(TheForm.txtCognome.value.length<2)
	{
		alert("Inserire un cognome valido");
		TheForm.txtCognome.focus();
		return true;
	}
	if ((TheForm.txtEmail.value==null)||(TheForm.txtEmail.value==""))
	{
		alert("Inserire un email corretta")
		TheForm.txtEmail.focus()
		return true;
	}
	if (controllaMail(TheForm.txtEmail.value)==false)
	{
		TheForm.txtEmail.value=""
		TheForm.txtEmail.focus()
		return true;
	}
	if(TheForm.txtUsername.value.length<4)
	{
		alert("Inserire un username valido");
		TheForm.txtUsername.focus();
		return true;
	}
	if(TheForm.txtPassword.value.length<4)
	{
		alert("Inserire una password valida");
		TheForm.txtPassword.focus();
		return true;
	}
	if(TheForm.txtRepeatPassword.value.length<4)
	{
		alert("Inserire una password valida");
		TheForm.txtRepeatPassword.focus();
		return true;
	}
	if(TheForm.txtPassword.value!=TheForm.txtRepeatPassword.value)
	{
		alert("Le due password sono diverse fra loro");
		TheForm.txtPassword.focus();
		return true;
	}
	if(!TheForm.rdbPrivacy[0].checked)
	{
		alert("Devi accettare le informative sulla privacy");
		TheForm.rdbPrivacy[0].focus();
		return true;
	}
	if (confirm("Attenzione: \n\nSei sicuro di voler procedere?"))
	{
		TheForm.submit();
	}
}

//----------------------------------------------------
//--- checkNewsletter(TheForm)
function checkNewsletter(TheForm,azione)
{
	if(TheForm.txtNome.value.length<2)
	{
		alert("Inserire un nome valido");
		TheForm.txtNome.focus();
		return true;
	}
	if(TheForm.txtCognome.value.length<2)
	{
		alert("Inserire un cognome valido");
		TheForm.txtCognome.focus();
		return true;
	}
	if ((TheForm.txtEmail.value==null)||(TheForm.txtEmail.value==""))
	{
		alert("Inserire un email corretta");
		TheForm.txtEmail.focus();
		return true;
	}
	if (controllaMail(TheForm.txtEmail.value)==false)
	{
		TheForm.txtEmail.value="";
		TheForm.txtEmail.focus();
		return true;
	}
	if(azione==1)
	{
		if(!TheForm.rdbPrivacy[0].checked)
		{
			alert("Devi accettare le informative sulla privacy");
			TheForm.rdbPrivacy[0].focus();
			return true;
		}
		TheForm.action="prt_newsletter.asp?idStep=2";
	}
	else
	{
		TheForm.action="prt_newsletter.asp?idStep=4";
	}
	if (confirm("Attenzione: \n\nSei sicuro di voler procedere?"))
	{
		TheForm.submit();
	}
}

function changeLang(idLang,lPage)
{		
	if (lPage == 2) 
	{
		window.location.href='../includes/incChangeLang.asp?idLang='+ idLang;
	}
	else
	{
		window.location.href='includes/incChangeLang.asp?idLang='+ idLang;
	}
}

function setCookie(NameOfCookie, value, expiredays)
{
	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
	document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

function getCookie(NameOfCookie)
{
	if (!navigator.cookieEnabled)
	{
    	alert('Per poter visualizzare correttamente questo sito devi abilitare i cookies');
	}
	else
	{
		if (document.cookie.length > 0)
		{
			begin = document.cookie.indexOf(NameOfCookie+"=");
			if (begin != -1)
			{
				begin += NameOfCookie.length+1;
				end = document.cookie.indexOf(";", begin);
				if (end == -1) end = document.cookie.length;
				return unescape(document.cookie.substring(begin, end));
			}
			return null;
		}
	}
}

function NewWindow(mypage,myname,w,h)
{
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars=yes,location=no,menubar=no,status=yes,toolbar=no';
	win = window.open(mypage,null,settings);
	if(win.window.focus){win.window.focus();}
}

// funzione per assegnare l'oggetto XMLHttpRequest
// compatibile con i browsers più recenti e diffusi
function assegnaXMLHttpRequest()
{
	var XHR = null, browserUtente = navigator.userAgent.toUpperCase();
	
	// browser standard con supporto nativo non importa il tipo di browser
	if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
	{
		XHR = new XMLHttpRequest();
	}

	// browser Internet Explorer - è necessario filtrare la versione 4
	else if(window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0)
	{
 
		// la versione 6 di IE ha un nome differente per il tipo di oggetto ActiveX
		if(browserUtente.indexOf("MSIE 5") < 0)
		{
			XHR = new ActiveXObject("Msxml2.XMLHTTP");
		}
		// le versioni 5 e 5.5 invece sfruttano lo stesso nome
		else
		{
			XHR = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return XHR;
}
 
var http = assegnaXMLHttpRequest();
 
function changeStateSondaggio(id)
{
	var postData = "idStep=4&id="+id;
	http.open("POST", "cmsSondaggi.asp", true);
	http = setXHRProperties(http,postData);
	http.onreadystatechange = function() { getStateResponse(id,"imgSondaggio",5,6); };
	http.send(postData);
}

function changeStateTipologiaFAQ(id)
{
	var postData = "idStep=8&id="+id;
	http.open("POST", "cmsFAQ.asp", true);
	http = setXHRProperties(http,postData);
	http.onreadystatechange = function() { getStateResponse(id,"imgFAQTipologia",1,8); };
	http.send(postData);
}

function changeStateFAQ(id)
{
	var postData = "idStep=9&id="+id;
	http.open("POST", "cmsFAQ.asp", true);
	http = setXHRProperties(http,postData);
	http.onreadystatechange = function() { getStateResponse(id,"imgFAQ",1,8); };
	http.send(postData);
}

function changeStateTipologieDocumentazione(id)
{
	var postData = "idStep=9&id="+id;
	http.open("POST", "cmsDocumentazione.asp", true);
	http = setXHRProperties(http,postData);
	http.onreadystatechange = function() { getStateResponse(id,"imgTipoDoc",1,8); };
	http.send(postData);
}

function changeStateDocumentazione(id)
{
	var postData = "idStep=10&id="+id;
	http.open("POST", "cmsDocumentazione.asp", true);
	http = setXHRProperties(http,postData);
	http.onreadystatechange = function() { getStateResponse(id,"imgDoc",1,8); };
	http.send(postData);
}

function changeStateEventi(id)
{
	var postData = "idStep=4&id="+id;
	http.open("POST", "cmsEventi.asp", true);
	http = setXHRProperties(http,postData);
	http.onreadystatechange = function() { getStateResponse(id,"imgEventi",1,8); };
	http.send(postData);
}

function changeStateScadenziario(id)
{
	var postData = "idStep=4&id="+id;
	http.open("POST", "cmsScadenziario.asp", true);
	http = setXHRProperties(http,postData);
	http.onreadystatechange = function() { getStateResponse(id,"imgScadenziario",1,8); };
	http.send(postData);
}

function changeStateTipologieSiti(id)
{
	var postData = "idStep=10&id="+id;
	http.open("POST", "cmsSitiWeb.asp", true);
	http = setXHRProperties(http,postData);
	http.onreadystatechange = function() { getStateResponse(id,"imgTipoSito",1,8); };
	http.send(postData);
}

function changeStateSiti(id)
{
	var postData = "idStep=11&id="+id;
	http.open("POST", "cmsSitiWeb.asp", true);
	http = setXHRProperties(http,postData);
	http.onreadystatechange = function() { getStateResponse(id,"imgSito",1,8); };
	http.send(postData);
}

function spostaFileRepository(element, idTipologia)
{
	var idFile, strElementId
	strElementId = String(element.id)
	var iLen = String(strElementId).length;
	idFile = eval(String(strElementId).substring(11, iLen));
	var postData = "idStep=12&idFile="+idFile+"&idTipologia="+idTipologia;
	http.open("POST", "cmsRepository.asp", true);
	http = setXHRProperties(http,postData);
	http.onreadystatechange = function() { hideTR(idFile); };
	http.send(postData);
}

function hideTR(idTR)
{
	var trName = 'trFile' + String(idTR)
	var row = document.getElementById(trName);
	row.style.display = 'none';
}

function setXHRProperties(xhr,postData)
{
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.setRequestHeader("Content-length", postData.length);
	xhr.setRequestHeader("Connection", "close");
	return xhr;
}

function getStateResponse(id,imgID,stateAbil,stateDisab)
{
	if(http.readyState == 4)
	{  
		var response = http.responseText;
		if (response != "")
		{
			imgIDName = imgID + id;
			if(response == stateDisab)
			{
				document.getElementById(imgIDName).setAttribute("src","../Images/ico_abilita.gif");
			}
			else if(response == stateAbil)
			{
				document.getElementById(imgIDName).setAttribute("src","../Images/ico_disabilita.gif");
			}
			else
			{
				return;
			}
		}
		else
		{
			return;
		}
	}
}

function votaSondaggioAJAX(rdbRisposta)
{
	http.open('get', 'prt_sondaggi.asp?idStep=0&rdbRisposta='+rdbRisposta);
	http.send(null);
}

function CreateControl(id, fileName) //fg(10672)
{
	if (id == 1)
	{
		document.write('<object type="video/x-ms-wmv" data="upload/repository/<%= video %>" width="320" height="240">');
		document.write('<param name="src" value="upload/repository/'+fileName+'" />');
		document.write('<param name="autostart" value="true" />');
		document.write('<param name="controller" value="true" />');
		document.write('<param name="loop" value="true">');
		document.write('</object>');
	}
	else if (id == 2)
	{
		document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="100%" id="filmatoFlash">')
		document.write('<param name="movie" value="upload/repository/'+fileName+'">')
		document.write('<param name="quality" value="high">')
		document.write('<param name="allowScriptAccess" value="sameDomain">')
		document.write('<param name="menu" value="false">')
		document.write('<param name="scale" value="noscale">')
		document.write('<embed src="upload/repository/'+fileName+'" width="320" height="240" quality="high" bgColor="#FFFFFF" scale="noscale" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowScriptAccess="sameDomain" name="'+fileName+'" menu="false"></embed>')
		document.write('</object>')
	}
	else if (id == 3)
{
		document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="100%" id="filmatoFlash">')
		document.write('<param name="movie" value="FLVPlayer_Progressive.swf" />')
		document.write('<param name="salign" value="lt" />')
		document.write('<param name="quality" value="high" />')
		document.write('<param name="scale" value="noscale" />')
		document.write('<param name="FlashVars" value="&skinName=clearSkin_3&streamName=upload/repository/'+fileName+'&autoPlay=false&autoRewind=false" />')
		document.write('<embed src="FLVPlayer_Progressive.swf" flashvars="&skinName=clearSkin_3&streamName=upload/repository/'+fileName+'&autoPlay=false&autoRewind=false" quality="high" scale="noscale" width="320" height="240" name="FLVPlayer" salign="LT" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />')
		document.write('</object>')
	}
}


//==========================================================================================================
//---  FUNZIONI JS PER EFFETTO ROLLOVER  ----------------------------------------------------------
//==========================================================================================================
function MM_preloadImages() {
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() {
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() {
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//--- secondExample()
function MM_reloadPage(init) {
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

