/*FUNZIONI GENERICHE UTILIZZABILI DA QUALSIASI PAGINA*/
function sendNews(lan){
	if (!lan) {lan='it';}
	nWidth=490;
	nHeight=450;
	window.open("/dynnew/"+lan+"/popup/popSendNews.phtml?link="+escape(window.location.href),"WinPopUp","width="+nWidth+",height="+nHeight+",directories=no,toolbar=no,scrollbars=no,resizable=no,status=no");
}

function submitForm(button, form){
		button.disabled=true;
		form.submit();
}

/*FUNZIONI NON GENERICHE MA CONDIVISE DA DUE O PIU' PAGINE AVENTI SIMILE STRUTTURA*/

function ctrlNomeCognomeEmailPhoneNumber ()
{
	prefisso = document.ull.pref.value;
	numero = document.ull.tel.value;
	prefisso = trim_string(prefisso);
	numero = trim_string(numero);

	if ((prefisso == '') && (numero == '' ))
	{
		alert('Inserire il prefisso e il numero.');
		return(false);
	}

	if(prefisso == '')
	{
		alert('Inserire il prefisso.');
		return(false);
	}
	if(numero == '' )
	{
		alert('Inserire il numero telefonico.');
		return(false);
	}
	if(!Is_Number(prefisso))
	{
		alert('Inserire un prefisso valido');
		return(false);
	}

	if(prefisso.substr(0,1)!='0')
	{
		alert('Inserire un prefisso valido');
		return(false);
	}


	if(!Is_Number(numero))
	{
		alert('Inserire un numero di telefono valido');
		return(false);
	}

	if ((numero.substr(0,1)=='0') || (numero.substr(0,1)=='1'))
	{
		alert('Inserire un numero di telefono valido');
		return(false);
	}


	var espressione = /^[a-zA-Z][�����a-zA-Z\ ]*$/;
	var stringa = document.ull.nome.value;
	if (!espressione.test(stringa)  )
	{
		alert("Nome non valido!");
		return false;
	}

	if ( stringa.length<3 )
	{
		alert("Il nome deve contenere almeno 3 caratteri!");
		return false;
	}

	espressione = /^[a-zA-Z][�����a-zA-Z\ ]*$/;
	stringa = document.ull.cognome.value;
	if (!espressione.test(stringa))
	{
		alert("Cognome non valido!");
		return false;
	}


	if ( stringa.length<2 )
	{
		alert("Il cognome deve contenere almeno 2 caratteri!");
		return false;
	}



	espressione = /^[a-zA-Z][a-zA-Z1-9_\-\.]*$/;
	stringa = document.ull.username.value;
	if (!espressione.test(stringa))
	{
		alert("E-mail non valido!");
		return false;
	}
	if ( stringa.length<2 )
	{
		alert("Errore nel campo E-mail!");
		return false;
	}

	espressione = /^[a-zA-Z1-9_\-][a-zA-Z1-9._\-]*\.[a-zA-Z1-9]+$/;
	stringa = document.ull.domain.value;
	if (!espressione.test(stringa))
	{
		alert("Dominio non valido!");
		return false;
	}

	if ( stringa.length<3 )
	{
		alert("Errore nel campo E-mail!");
		return false; 
	}


	return(true);

}


function trim_string(string)
{
	var ichar, icount;
	var strValue = string;
	ichar = strValue.length - 1;
	icount = -1;
	while (strValue.charAt(ichar)==' ' && ichar > icount)
		--ichar;
	if (ichar!=(strValue.length-1))
		strValue = strValue.slice(0,ichar+1);
	ichar = 0;
	icount = strValue.length - 1;
	while (strValue.charAt(ichar)==' ' && ichar < icount)
		++ichar;
	if (ichar!=0)
		strValue = strValue.slice(ichar,strValue.length);
	return strValue;
}

function Is_Number(sDato)
{
	var i;
	var CodeChr;
	for (i = 0;  i < sDato.length;  i++)
	{
		CodeChr = sDato.charAt(i);
		if ((CodeChr<'0') || (CodeChr>'9'))
		return(false);
	}
	return(true);
}

function PhoneNumber()
{
	if (typeof(document.ull[0].pref) != "undefined")
	{
		prefisso = document.ull[0].pref.value;
		numero = document.ull[0].tel.value;
	}
	else
	{
		prefisso = document.ull.pref.value;
		numero = document.ull.tel.value;
	}
	prefisso = trim_string(prefisso);
	numero = trim_string(numero);

	if ((prefisso == '') && (numero == '' ))
	{
		alert('Inserire il prefisso e il numero.');
		return(false);
	}

	if(prefisso == '')
	{
		alert('Inserire il prefisso.');
		return(false);
	}
	if(numero == '' )
	{
		alert('Inserire il numero telefonico.');
		return(false);
	}
	if(!Is_Number(prefisso))
	{
		alert('Inserire un prefisso valido');
			return(false);
	}

	if(prefisso.substr(0,1)!='0')
	{
		alert('Inserire un prefisso valido');
			return(false);
	}


	if(!Is_Number(numero))
	{
		alert('Inserire un numero di telefono valido');
		return(false);
	}

/*
	    if ((numero.substr(0,1)=='0') || (numero.substr(0,1)=='1'))
	    {
		    alert('Inserire un numero di telefono valido');
			return(false);
	    }
*/

	if ( numero.length<4 ) {
		alert("Inserire un numero di telefono valido");
		return false;
	}

       if (typeof(document.ull[0].mkt) != "undefined"){
                mercato = document.ull[0].mkt.value;
        }else{
                mercato = document.ull.mkt.value;
        }

	if (mercato=='RES') {
		if (    (prefisso.length + numero.length > 10)
			&& !( (prefisso.length + numero.length == 11) && (numero.substr(0,1)=='1') )
		) {
			alert("Inserire un telefono valido");
			return false;
			}
	}

// *****************************

	document.getElementById('wait').style.display='block'; 
	if(document.getElementById('C701240658') != null)
		{
		document.getElementById('C701240658').style.visibility='visible';
		}
	document.getElementById('tastoverif').style.display='none'; 
	document.getElementById('tipo').style.display='none'; 

	return(true);
}


function Invia()
{
	if(PhoneNumber()) document.ull.submit();
}


/*utilizzato per le form di tutto incluso*/
function PhoneNumber_TI()
{

	if (typeof(document.ull1[0].indirizzo) != "undefined")
	{
		indirizzo = document.ull1[0].indirizzo.value;
		civico = document.ull1[0].civico.value;
		comune = document.ull1[0].comune.value;
		provincia = document.ull1[0].provincia.value;
		cap = document.ull1[0].cap.value;
		tipoindirizzo = document.ull1[0].tipoindirizzo.value;
	}
	else
	{
		indirizzo = document.ull1.indirizzo.value;
		civico = document.ull1.civico.value;
		comune = document.ull1.comune.value;
		provincia = document.ull1.provincia.value;
		cap = document.ull1.cap.value;
		tipoindirizzo = document.ull1.tipoindirizzo.value;
	}

	indirizzo = trim_string(indirizzo);
	civico = trim_string(civico);
	comune = trim_string(comune);
	provincia = trim_string(provincia);
	cap = trim_string(cap);
	tipoindirizzo = trim_string(tipoindirizzo);

	if(tipoindirizzo == '')
	{
		alert('Inserire il tipo indirizzo.');
		return(false);
	}
	if(indirizzo == '')
	{
		alert('Inserire l\'indirizzo.');
		return(false);
	}
	if(civico == '' || civico == 'N� civ')
	{
		alert('Inserire il civico .');
		return(false);
	}
	if(cap == '' )
	{
		alert('Inserire il cap .');
		return(false);
	}
	if(!Is_Number(cap))
	{
		alert('Inserire un cap valido');
		return(false);
	}
	if(comune == '')
	{
		alert('Inserire il comune.');
		return(false);
	}
	if(provincia == '' )
	{
		alert('Selezionare la provincia .');
		return(false);
	}

	document.getElementById('wait2').style.display='block';
	if(document.getElementById('C701240659') != null)
		{
		document.getElementById('C701240659').style.visibility='visible';
		}
	// document.getElementById('tastoverifTI').style.display='none'; 
	if($('tipoTI') != null)
		{
		document.getElementById('tipoTI').style.display='none'; 
		}
	return(true);
}

function Invia_TI()
{
		if(PhoneNumber_TI()) document.ull.submit();
}



function get_url_param(name)
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); 
	var regexS = "[\\?&]"+name+"=([^&#]*)"; 
	var regex = new RegExp( regexS ); 
	var results = regex.exec( window.location.href ); 
	if( results == null ) return ""; 
	else return results[1];
}


// Popup video verso Libero Video
function videoPopup(theID) {
	vPopup = window.open("about:Blank","videopopupid","width=420,height=353,status=no,menubar=no,resizable=no,scrollbars=no,toolb	ar=no,location=no,directories=no");
	vPopup.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
	vPopup.document.write('<html xmlns="http://www.w3.org/1999/xhtml">');
	vPopup.document.write('<head>');
	vPopup.document.write('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />');
	vPopup.document.write('<title>infostrada.it</title>');
	vPopup.document.write('</head>');
	vPopup.document.write('<body>');
	vPopup.document.write('<object width="400" height="333"><param name="movie" value="http://video.libero.it/static/swf/eltvplayer.swf?id='+ theID +'&ap=1" /><embed src="http://video.libero.it/static/swf/eltvplayer.swf?id='+ theID +'&ap=1" width="400" height="333" type="application/x-shockwave-flash"></embed></object>');
	vPopup.document.write('</body>');
	vPopup.document.write('</html>');
}

// Popup video verso Libero Video
// Si apre solo se chiamato con un url che abbia un parametro openvideo==OpenVideoValue
function videoPopupIf(theID, OpenVideoValue) {
	ovvalue = get_url_param("openvideo");
	if (ovvalue == OpenVideoValue) {
		videoPopup(theID);
	}
}

function validate_this(obj) {	 	
	
	var stringa = obj.value; 
	switch(obj.name) {
		case 'cap':  
		  var pattern_esatto   = new RegExp("^([0-9]?){5}$"); 
		  var pattern          = new RegExp("([0-9]?){5}"); 
		  break;
		case 'civico':  // NUM.CIVICO 123 || 123/A1
		  var pattern_esatto = new RegExp("^(([1-9]{1}[0-9]*)(/{1}[a-zA-Z]*[0-9]*[a-zA-Z]*)?|[^0-9/]{1}.*)$");
		  var pattern        = new RegExp("^(([1-9]{1}[0-9]*)(/{1}[a-zA-Z]*[0-9]*[a-zA-Z]*)?|[^0-9/]{1}.*)");   
		  break;
		
	}

	var result = stringa.search(pattern_esatto);
	var campo = obj.value;
	var new_campo='';	

	if (result<0) {
		if (stringa.match(pattern)) {
			if (obj.name=='civico') alert('Formato numero civico errato.');
			obj.value = stringa.match(pattern)[0];
		} else {
		  obj.value='';
		}
	} 
	
}	
function apridiffurl(nomeLink1,nomeLink2) {
		window.open(nomeLink1);
	}

/* FUNZIONI PSEUDO MODALBOX PER LOYALTY */

function pseudoModalBox(messaggioTrasmesso, modoConfirm, funzionePerYes){
	var divMascheramento = new Element('div', {
		'id' : 'loyalty_modal_wrapper',
		'styles' : {
			'width' : document.body.clientWidth,
			'height' : $('header').clientHeight+$('middle_part').clientHeight+$('footer').clientHeight
			}
		});
	var divContenitore = new Element('div', {
		'id' : 'loyalty_modal_box',
		'styles' : {
			'left' : (document.body.clientWidth-370)/2,
			'top' : (document.body.clientHeight-100)/2
			}
		});
	var divContenitoreSub1 = new Element('div', {
		'class': 'loyalty_modal_box_top'
		});
		divContenitoreSub1.innerHTML = '&nbsp;';
	var divContenitoreSub2 = new Element('div', {
		'class': 'loyalty_modal_box_content'
		})
		divContenitoreSub2.innerHTML = messaggioTrasmesso;
	var divContenitoreSub3 = new Element('div', {
		'class': 'loyalty_modal_box_bottom'
		});
		divContenitoreSub3.innerHTML = '&nbsp;';
	
	if(modoConfirm == true)
		{
			// alert('creazione tasti');
			var divSeparatore = new Element('div', {
			'class': 'clear f0'
			});
			divSeparatore.innerHTML = '&nbsp;';
			divSeparatore.inject(divContenitoreSub2, 'bottom');
			var divContenitoreTasti = new Element('div', {
				'class': 'loyalty_modal_box_content_confirm'
				});
			divContenitoreTasti.innerHTML = '<a onclick=' + funzionePerYes + 'href="#" class="loyalty_btn">S&igrave;</a><a onclick="$(\'loyalty_modal_wrapper\').destroy(); return false" href="#" class="loyalty_btn">No</a>';
			divContenitoreTasti.setStyle('padding-left' , '90px');
			divContenitoreTasti.inject(divContenitoreSub2, 'bottom');
		}
		else if(modoConfirm == false)
		{
			var divSeparatore = new Element('div', {
			'class': 'clear f0'
			});
			divSeparatore.innerHTML = '&nbsp;';
			divSeparatore.inject(divContenitoreSub2, 'bottom');
			var divContenitoreTasti = new Element('div', {
				'class': 'loyalty_modal_box_content_confirm'
				});
			divContenitoreTasti.innerHTML = '<a onclick="$(\'loyalty_modal_wrapper\').destroy(); return false" href="#" class="loyalty_btn">Chiudi</a>';
			divContenitoreTasti.setStyle('padding-left' , '132px');
			divContenitoreTasti.inject(divContenitoreSub2, 'bottom');
		}
	
	divContenitoreSub1.inject(divContenitore, 'top');
	divContenitoreSub2.inject(divContenitore, 'bottom');
	divContenitoreSub3.inject(divContenitore, 'bottom');
	divContenitore.inject(divMascheramento, 'top');
	divMascheramento.inject($('inner_content'), 'after');
}

function aggiornaDomande(valorePassato){
	// attivo seconda select
	$('loyalty_faq_select_domande').removeProperty('disabled');
	// azzero domande precedenti se presenti
	$$('#loyalty_faq_select_domande option').each(function(item, index){
				if(index != 0)
					{
						item.destroy();
					}
				});
	// ciclo ricerca domande per argomento passato
	$$('.loyalty_faq_domanda').each(function(item, index){
		// selezioni domanda solo se relativa all'argomento scelto
		if(item.getNext('div').getNext('div').innerHTML == arrayArgomenti[valorePassato])
			{
			// alert(item.getNext('div').getNext('div').innerHTML);
			var optionDaAggiungere = new Element('option', {
				'value' : index
				});
			optionDaAggiungere.innerHTML = item.innerHTML;
			optionDaAggiungere.inject($('loyalty_faq_select_domande'), 'bottom');
			}
		});
	// alert(arrayArgomenti[valorePassato]);
}

function mostraRisposta(valorePassato){
	// alert(valorePassato);	
	if(valorePassato != 'null')
		{
		$('loyalty_domanda_prescelta').innerHTML = $$('.loyalty_faq_domanda')[valorePassato].innerHTML;
		$('loyalty_risposta_prescelta').innerHTML = $$('.loyalty_faq_risposta')[valorePassato].innerHTML;
		}
		else
		{
		$('loyalty_domanda_prescelta').innerHTML = '&nbsp;';
		$('loyalty_risposta_prescelta').innerHTML = '&nbsp;';
		}
}

function WinPopUp(strPath,nWidth,nHeight){
	if(isNaN(nWidth)){nWidth=490;}
	if(isNaN(nHeight)){nHeight=340;}
	var nXMax = screen.width;
	var nYMax = screen.height;
	var nXOffset = (nXMax - nWidth)/2;
	var nYOffset = (nYMax - nHeight)/2;
	window.open(strPath,"win"+nWidth,"top="+nYOffset+",left="+nXOffset+",width="+nWidth+",height="+nHeight+",directories=no,toolbar=no,scrollbars=yes,resizable=no");
}

