var richiesta;
var who_button;

function do_init(){
	
	if ($('btnSend')){
		$('btnSend').onclick=controllaCampiObbligatori;
	}		
	
	if ($('btnSendRem')){
		$('btnSendRem').onclick=controllaCampiObbligatoriRem;
	}

	/*var form = document.getElementById("formNewsletter");
	form.nome.controlla=true;
	form.nome.sName="Nome";
	form.cognome.controlla=true;
	form.cognome.sName="Cognome";
	form.dataDiNascita.controlla=true;
	form.dataDiNascita.sName="Data di Nascita";
	form.idPaese.controlla=true;
	form.idPaese.sName="Paese";
	form.eMail.controlla=true;
	form.eMail.mail=true;
	form.eMail.sName="E-Mail";
	form.cellulare.controlla=true;
	form.cellulare.sName="Cellulare";
	form.fax.controlla=true;
	form.fax.sName="Fax";
	form.autorizzazione.controlla=true;
	form.autorizzazione.sName="Autorizzazione"*/

}

function controllaCampiObbligatori(){
	
	var contaCampiRequired=$$('input.required').length;
	var campiVuoti=0;
	

	for(var i=0;i<contaCampiRequired;i++){

		if($$('input.required')[i].value.length==0){
			
			var idCampo=$$('input.required')[i].id;
			
			$('warning_'+idCampo).setStyle('visibility','visible');
			
			campiVuoti++;
			
		}else{
			
			//bisogna fare i controlli sulla mail ecc			
			var idCampo=$$('input.required')[i].id;
			$('warning_'+idCampo).setStyle('visibility','hidden');
			
		}
	}
	
	if($('autorizzazione').checked==true) {
	
		if(campiVuoti==0){
			
			who_button = "registrazioneNewsletter";			
			activeOverlay();
		}
			
	} else {
		
		$('warning_autorizzazione').setStyle('visibility','visible');
		
	}
	
}

function controllaCampiObbligatoriRem(){
	if($('eMailUn').value.length!=0) {
		$('warning_eMailUn').setStyle('visibility','hidden');
		who_button = "cancellazioneNewsletter";
		activeOverlay();
	} else {
		$('warning_eMailUn').setStyle('visibility','visible');
	}
}

function activeOverlay(onComplete){
	 
	var overlay=document.createElement("div");
	overlay.id="overlay";
		
	var lightbox=document.createElement("div");
	lightbox.id="lightbox";
	
	var contents=document.createElement("div");
	contents.id="contsLightbox";
	
	document.body.appendChild(overlay);
	document.body.appendChild(lightbox);
	document.body.appendChild(contents);
	
	overlay.onclick=deactiveOverlay;
	
	$('overlay').setStyle('opacity', 0);
	$('contsLightbox').setStyle('opacity', 0);
	
	$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });
	
	var arrayPageSize = this.getPageSize();
	$('overlay').setStyles({width: arrayPageSize[0], height: arrayPageSize[1] });
	
	var fx1=new Fx.Tween('overlay',{duration:'long'});
	fx1.start('opacity',0.6);
	
	fx1.onComplete=function(){
		
		var fx4=new Fx.Tween('contsLightbox',{duration:'long'});
		fx4.start('opacity',1);
		
		if(who_button=="registrazioneNewsletter") {
		
			//********* richiesta ajax per l'iscrizione alla mailinglist
			richiesta = new insertDipnet("registrazioneNewsletter","contsLightbox",1);
			richiesta.post+="&nome="+$('nome').value;
			richiesta.post+="&cognome="+$('cognome').value;
			richiesta.post+="&dataDiNascita="+$('dataDiNascita').value;
			richiesta.post+="&eMail="+$('eMail').value;
			richiesta.post+="&idPaese="+$('idPaese').value;
			richiesta.post+="&cellulare="+$('cellulare').value;
			richiesta.post+="&fax="+$('fax').value;
			richiesta.post+="&idLingua="+$('idLingua').value;
			richiesta.settaOnComplete(onCompleteForm);
			richiesta.caricaXML();
		
		}
		
		if(who_button=="cancellazioneNewsletter") {
			
			//********* richiesta ajax per l'iscrizione alla mailinglist
			richiesta = new insertDipnet("cancellazioneNewsletter","contsLightbox",1);
			richiesta.post+="&eMailUn="+$('eMailUn').value;
			richiesta.post+="&idLingua="+$('idLingua').value;
			richiesta.settaOnComplete(onCompleteForm);
			richiesta.caricaXML();
		
		}
		
	}
	
}

function onCompleteForm(stringaXML){
	
	doc=creaDocsXML(stringaXML);
	var controllo=doc.getElementsByTagName("templates");
	var template=controllo[0].childNodes[1].firstChild.data;
	$('contsLightbox').innerHTML=template;
		
}


function deactiveOverlay(){
	
	$('overlay').onclick=null;
	var fx2=new Fx.Tween('contsLightbox',{duration:'long',onComplete:fadeOutOverlay});
	fx2.start('opacity',0);
	
}

function fadeOutOverlay(){
	
	var fx3=new Fx.Tween('overlay',{duration:'long',onComplete:cancellaDivLightBox});
	fx3.start('opacity',0);
	
}

function cancellaDivLightBox(){

	$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });
	
	document.body.removeChild($('contsLightbox'));
	document.body.removeChild($('overlay'));
	document.body.removeChild($('lightbox'));

	//$$('input[type=text]', 'textarea').each(function(node){ node.value = '' });
	$('autorizzazione').checked=false;

}

function  getPageSize() {
	        
	   var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	}
