/*************************************************
	-- CALCOLO DELL'ALTEZZA DEL MAIN --
*************************************************/

debug_msg = false;

function toDebug(string)
{
	if (debug_msg)
	{
		if((typeof window.console=="undefined"))
		{			
			alert(string);
		}
		else
		{		
			 window.console.log(string);
		}
	}
}
   
function mainHeight()
{
	try 
		{
	//	inizializzazione delle variabili
		var main = $('main');
		var head = $('head');
		var colonnaSx = $('colonnaSx');
		var container = $('container');
		var footer = $('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;
		
	//	settaggi proporietà dei box contenitori
		colonnaSx.setStyle({'top':head.offsetHeight+'px','height':altezza_comune+'px'});
		container.setStyle({'top':head.offsetHeight+'px','height':altezza_comune+'px'});
		footer.setStyle({'top':head.offsetHeight+altezza_comune+'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();
			}
		}
		//pulizia del contenuto: se "side_images" è vuoto, la nascondo e allargo il contenuto
		if($('side_images'))
		{
			var side_discendents = $('side_images').immediateDescendants();
			
			if(side_discendents.length == 0)
			{
				$('side_images').hide();
				$('contenuto').setStyle({'width': '534px'});
			}
			
		}
		
	//	settaggio active al menu, se non c'è nessun <li> già active
		var active = false;
		$$('#menu li').each(function(li){if(li.className.match('active')) active = true;});
		if(!active) $$('#menu li')[0].className = 'active';
		
	}
	catch(e) 
	{ 
		toDebug(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) 
	{ 
		toDebug(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';
				
			}});
		}
	}
	
}
*/








function showOnClick(element)
{
	if(element == 'frm_dettagli_richiesta')
	{
		Effect.toggle(element, 'blind');
		if($('frm_dettagli_preventivo').style.display != 'none') Effect.toggle($('frm_dettagli_preventivo'), 'blind');
		document.forms['guestbooksign'].frm_tipo_richiesta.value = 'richiesta informazioni';				
	}
	else
	{
		Effect.toggle(element, 'blind');
		if($('frm_dettagli_richiesta').style.display != 'none') Effect.toggle($('frm_dettagli_richiesta'), 'blind');
		document.forms['guestbooksign'].frm_tipo_richiesta.value = 'richiesta preventivo';
	}
	
}














/*EOF FORM CONTATTI*/



/* BOF SCROLL DEI BOX */

var timer;
var moving = false;
var element_to_move;
var max_move;


function scrollingContent(el, clipHeight)
{
	var element = $(el);
	var element_dimension = element.getDimensions();
	var clip_element = element.up();
	var clip_dimension = clip_element.getDimensions();
	var scroll_container = clip_element.up();

	clip_element.setStyle({'clip': 'rect(0px,'+clip_dimension.width+'px,'+clipHeight+'px,0px)'});
	if(element_dimension.height > clipHeight)
	{
		var goUp = $$('#'+scroll_container.id+' .goUp')[0];
		var goDown = $$('#'+scroll_container.id+' .goDown')[0];
		var down_top = scroll_container.offsetHeight - goDown.offsetHeight;
		// settaggi freccia in alto
		goUp.setStyle({
			'top': '30px',
			'cursor': 'pointer'
		});
		goUp.onmouseover =function()
		{
			element_to_move = element;
			max_move = clipHeight;
			scrollUp();
		};
		goUp.onmouseout = function()
		{
			
			moving = false;
			clearTimeout(timer);
		}
		goUp.show();
		
		
		// settaggi per freccia in basso
		goDown.setStyle({
			'top': '50px',
			'cursor': 'pointer'
		});
		goDown.onmouseover =function()
		{
			element_to_move = element;
			max_move = clipHeight;
			scrollDown();

		};
		goDown.onmouseout = function()
		{
			moving = false;
			clearTimeout(timer);
		}
		
		goDown.show();
	}
	
}

function scrollUp()
{	
	if(element_to_move)
	{
		if(element_to_move.offsetTop>=0) return false;
		moving = true;
		new Effect.Move(element_to_move,{x:0,y:1, duration: 0.01});
		if(moving) timer = setTimeout(scrollUp,10);
		else return false;
	}
	return false;
}
function scrollDown()
{
	if(element_to_move)
	{
		if(element_to_move.offsetTop < -(element_to_move.offsetHeight - max_move)) return false;
		moving = true;
		new Effect.Move(element_to_move,{x:0,y:-1, duration: 0.01});
		if(moving) timer = setTimeout(scrollDown,10);
		else return false;
	}
	return false;
}

/* EOF SCROLL DEI BOX */



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


function allFunctions() 
{
	scrollingContent('offerte', 226)
	mainHeight();
	$('page_loading').hide();
	
}


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