
function searchText(text,ua) 
{
	stringposition = ua.indexOf(text) + 1;
//	data = text;
	return stringposition;
}

function goToLocation(elem)
{
	var ua = navigator.userAgent.toLowerCase();	
	var son_link;
		
	if(searchText('msie',ua)) son_link = elem.firstChild;
	else son_link = elem.childNodes[1];
		
	if(son_link) 
	{
		window.location.href= son_link.href;
	}
}

//switcho le immagini
function preloadIMG(){	
	var images = document.getElementsByTagName('img');
	var immagini=new Array();	
	var cont = 0;
	//percorro tutte le immagini		
	for (var i=0; i<images.length; i++){
		var image = images[i];
		var relAttribute = String(image.getAttribute('name'));
		//se hanno match preload... associo funzioni di preload
		if (relAttribute.toLowerCase().match('preload')){
			immagini[cont]=new Image();
			immagini[cont].src=image.src;
			cont++;
		}
	}
}

function attachSwitch(){	
	var images = document.getElementsByTagName('img');
	//percorro tutte le immagini		
	for (var i=0; i<images.length; i++){
		var image = images[i];
		var relAttribute = String(image.getAttribute('name'));
		//se hanno match over... associo funzioni di swap img
		if (relAttribute.toLowerCase().match('over')){
			image.onmouseover = function(){
				if(this.className!="active"){
					this.src= this.src.replace('_off', '_on');
				}
			};
			image.onmouseout = function(){
				if(this.className!="active"){
					this.src= this.src.replace('_on', '_off');
				}
			};			
		}
		if(image.className == "active"){
			image.src= image.src.replace('_off', '_on');
		}
	}
}

function startMenu() {
	var node;
	var displaymenu;

	displaymenu = document.getElementById("menu");
	for (i=0; i<displaymenu.childNodes.length; i++) {
		node = displaymenu.childNodes[i];
		if (node.nodeName=="LI") {
			node.onmouseover=function() {
				this.className+=" over";
			}
			node.onmouseout=function(){                  
				this.className=this.className.replace("over", "");
			}
		}
	}
}
function startSezioni() {
	var node;
	var displaymenu;

	displaymenu = document.getElementById("sezioni");
	for (i=0; i<displaymenu.childNodes.length; i++) {
		node = displaymenu.childNodes[i];
		if (node.nodeName=="DIV") {
			node.onmouseover=function() {
				this.className+=" over";
				
			}
			node.onmouseout=function(){                  
				this.className=this.className.replace("over", "");
				
			}
		}
	}
}

/*
<!--[if gte IE 5.5000]>
 	<script type="text/javascript" src="pngfix.js"></script>
<![endif]-->
*/

function correctPNG() {
	for(var i=0; i<document.images.length; i++){
		var img = document.images[i];
		var imgName = img.src.toUpperCase();
		if (imgName.substring(imgName.length-3, imgName.length) == "PNG"){
			var imgID = (img.id) ? "id='" + img.id + "' " : "";
			var imgClass = (img.className) ? "class='" + img.className + "' " : "";
			var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
			var imgStyle = "display:inline-block;" + img.style.cssText ;
			if (img.align == "left") imgStyle = "float:left;" + imgStyle;
			if (img.align == "right") imgStyle = "float:right;" + imgStyle;
			if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;     
			var strNewHTML = "<span " + imgID + imgClass + imgTitle
			+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
			+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
			+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
			img.outerHTML = strNewHTML;
			i = i-1;
		}
	}
}

function mainHeight(contenitore){
	var main;
	var head;
	var colonnaSx;
	var container;
	var colonnaDx;
	var footer;
	var heightProvvisoria;
	var heightMaxColonna;
	var maxHeight;		
	
//	assegno le variabili
	main = document.getElementById(contenitore);
	head = document.getElementById('head');
	colonnaSx = document.getElementById('colonnaSx');
	container = document.getElementById('container');
	colonnaDx = document.getElementById('colonnaDx');
	footer = document.getElementById('footer');
//	confronto le altezze ...
//	prima di colonnaSx e container .. e assegno heightProvvisoria
	if(colonnaSx.offsetHeight > container.offsetHeight) heightProvvisoria = colonnaSx.offsetHeight;	
	else heightProvvisoria = container.offsetHeight;	
	
//	poi confronto heightProvvisoria con colonnaDx e la riassegno

	if(heightProvvisoria > colonnaDx.offsetHeight) heightMaxColonna = heightProvvisoria;
	else heightMaxColonna = colonnaDx.offsetHeight;	
	heightMaxColonna = heightProvvisoria;
// definisco l'altezza del main sommando le altre(compresa quella del footer) ...

	//maxHeight = head.offsetHeight + heightMaxColonna + footer.offsetHeight;
	maxHeight = head.offsetHeight + heightMaxColonna;
//	.. e la assegno al main

	if($('frm_dettagli_richiesta')) maxHeight += 350;

	main.style.height = (maxHeight) +121 + 'px';

//	posiziono al volo le 3 colonne dando top = altezza dell'head
	colonnaSx.style.top = head.offsetHeight + 'px';
	container.style.top = head.offsetHeight + 'px';
	colonnaDx.style.top = head.offsetHeight + 'px';
//	alert(main.style.height);

}


/*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 == '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*/


function allFunctions()
{
	var loading = document.getElementById('page_loading');		
	var main = document.getElementById('main');
	main.style.visibility="hidden";
		
	preloadIMG();
	attachSwitch();
	startMenu();
	startSezioni();
	mainHeight(contenitore);
//	correctPNG();	

	loading.style.display="none";	
	main.style.visibility="visible";	
}		