/*************************************************
	-- CALCOLO DELL'ALTEZZA DEL MAIN --
*************************************************/
function mainHeight()
{
try 
	{
//	inizializzazione delle variabili
	var main = $('main');
	var head = $('head');
	var colonnaSx = $('colonnaSx');
	var container = $('container');
	var footer = $('footer');
	var super_footer = $('super_footer');
	var container_offsetHeight = container.offsetHeight;
	if($('frm_dettagli_richiesta')) container_offsetHeight += 350;	
	var altezza_comune = Math.max(colonnaSx.offsetHeight, container_offsetHeight);
	

	var altezza_main = (head.offsetHeight) + altezza_comune + footer.offsetHeight + super_footer.offsetHeight;
	
	
/*-----------------------------------*/
//	settaggi proporietà dei box contenitori
	colonnaSx.setStyle({'top': (head.offsetHeight + 40) + 'px', 'height': altezza_comune + 'px'});
	container.setStyle({'top': (head.offsetHeight + 40) + 'px', 'height': altezza_comune + 'px'});
	footer.setStyle({'top': ((head.offsetHeight + 40) + (altezza_comune + 20)) + 'px'});


	super_footer.setStyle(
	{
		'position':'absolute',
		'left': '26px',
		'color':'#000000',
		'top': (footer.offsetTop + footer.offsetHeight + 25) + 'px'
	});
	
	main.setStyle({'height': altezza_main + 'px'});


	//pulizia del contenuto: eliminazione dell' utlimo paragrafo vuoto
	if($('contenuto'))
	{
		
		var discendenti = $('contenuto').immediateDescendants();
						
		var discendenti_p = discendenti.findAll(
				function(n) { if (n.nodeName == 'P') return n; }
				);

		var k = discendenti_p.size() - 1;

		if(discendenti_p[k].empty() || discendenti_p[k].innerHTML == "\n&nbsp;\n" || discendenti_p[k].innerHTML == "&nbsp; ")
		{
			discendenti_p[k].remove();
		}

	}
}
	catch(e) { /*alert(e.toString());*/ }
}



function tableBg(el)
{
try 
{
	var righe = $$('#'+el+' tr');
	
	for (var i=0;i<righe.length;i++)
	{
		
		if(i%2 == 0) righe[i].setStyle({'background':'#E8F3FF'});
		else righe[i].setStyle({'background':'#D3E7FF'});
		if(i == 0) righe[0].setStyle({'background':'#43A2F2', 'color': '#FFFFFF'});
	}
}
	catch(e) { alert(e.toString()); }
}





/*BOF FORM CONTATTI*/

//restituisco true in caso di errore
function validateFormElement(elem)
{
	if(elem.value=="")
	{
		errorOnFormElement(elem);
		return true;
	}
	else 
	{
		return false;
	}
}


function validateHiddenElement(element)
{
	if(validateFormElement(element))
	{
		errorOnFormElement('fieldset_dettagli_richiesta');
		errorOnFormElement('fieldset_dettagli_preventivo');
		return true;
	}
	else return false;



}

function validateCheckBoxElement(elem)
{
	if(elem.checked)
	{
		return false;
	}
	else
	{
		errorOnFormElement(elem.parentNode);	
		return true;
	}
}

function validateFormEmail(elem)
{
	var emailRegexp = RegExp('^[A-Za-z0-9._-]+[@]([A-Za-z0-9-]+[.])+([A-za-z]{2,4})$', 'i');
	
	if (emailRegexp.test(elem.value))
	{
		return false;
	}
	else
	{
		errorOnFormElement(elem);
		return true;
	}
}


function errorOnFormElement(elem)
{
	Effect.Shake(elem);
	new Effect.Highlight(elem);
}


function submitGuestBook()
{
	var d = new Date()
	param = d.getTime();
		
	var form = $('guestbooksign');
	document.forms['guestbooksign'].secure.value = param;


	//copio testo della text area
	if (document.forms['guestbooksign'].frm_tipo_richiesta.value=="richiesta preventivo")
	{
		document.forms['guestbooksign'].frm_messaggio.value=document.forms['guestbooksign'].frm_messaggio_a.value;
	}
	else
	{
		document.forms['guestbooksign'].frm_messaggio.value=document.forms['guestbooksign'].frm_messaggio_b.value;	
	}

//inizio validazione
	var error_found = 0;
			
	if(validateFormElement(form.frm_nome)) error_found++;
	if(validateFormEmail(form.frm_email)) error_found++;
	if(validateFormElement(form.frm_telefono)) error_found++;
	if(validateCheckBoxElement(form.frm_privacy_a) && validateCheckBoxElement(form.frm_privacy_b)) error_found++;
	//if(validateHiddenElement(form.frm_tipo_richiesta)) error_found++;
//fine validazione	


	if(error_found==0)
	{
		Effect.BlindUp('form_email_all_fields');

		form.request({
		
		  onComplete: function()
		  { 	
			Effect.BlindDown('form_email_thanks');
			Form.reset('guestbooksign');

			  return false; 
		   }
		
		});

/*
	document.forms['guestbooksign'].submit();		
*/	
	}	
	return false;
}



function showOnClick(element)
{
	
	if($(element).style.display == '') return false;
	else
	{
		if(element == 'frm_dettagli_richiesta')
		{
			Effect.BlindUp($('frm_dettagli_preventivo'), {afterFinish: down=function()
			{
				Effect.BlindDown(element, {afterFinish: down2=function()
				{
				
				}
				});				
												
				document.forms['guestbooksign'].frm_tipo_richiesta.value = 'richiesta informazioni';				
				
			}});
		}
		else
		{
			Effect.BlindUp($('frm_dettagli_richiesta'), {afterFinish: down=function()
			{
				Effect.BlindDown($('frm_dettagli_preventivo'), {afterFinish: down2=function()
				{
							
				}
				});
				document.forms['guestbooksign'].frm_tipo_richiesta.value = 'richiesta preventivo';
				
			}});
		}
	}
	
}

/*EOF FORM CONTATTI*/





/*************************************************
	-- RICHIAMO TUTTO UNA UNICA FUHNZIONE --
*************************************************/


function allFunctions() 
{
	tableBg('listino_prezzi');
	mainHeight();
	
	$('loading_page').hide();
}


if (window.attachEvent)	window.attachEvent("onload", allFunctions);
else window.onload=allFunctions;