var d = document;

Object.extend(Element, {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth;
	},
	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src;
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href;
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	}
});

// funzione per pescare le dimensioni della pagina e della finestra
function getPageSize(){
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = d.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (d.body.scrollHeight > d.body.offsetHeight){ // all but Explorer Mac
		xScroll = d.body.scrollWidth;
		yScroll = d.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = d.body.offsetWidth;
		yScroll = d.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (d.documentElement && d.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = d.documentElement.clientWidth;
		windowHeight = d.documentElement.clientHeight;
	} else if (d.body) { // other Explorers
		windowWidth = d.body.clientWidth;
		windowHeight = d.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		var pageHeight = windowHeight;
	} else {
		var pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		var pageWidth = windowWidth;
	} else {
		var pageWidth = xScroll;
	}

	var arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

// Funzione per conoscere lo scroll verticale e orizzontale
function getPageScroll(){
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (d.documentElement && d.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = d.documentElement.scrollTop;
	} else if (d.body) { // all other Explorers
		yScroll = d.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop
	}

	arrayPageScroll = new Array('', yScroll)
	return arrayPageScroll;
}

function fluttua() {
	if ($('questapagina')) {
		// controllo se la pagina è stata scrollata
		var scroll = getPageScroll();
		if (scroll[1]) {
			// se lo scroll è maggiore di 400, riposiziono il menu
			if (scroll[1] > 400) {
				$('questapagina').setStyle({
					position: 'absolute',
					width: "155px"
				});
				new Effect.Move('questapagina', {y: scroll[1], mode: 'absolute', duration: 0.2});

			}
		} else {
			// risposiziono in alto
			$('questapagina').setStyle({
				position: 'absolute',
				width: "155px"
			});
			new Effect.Move('questapagina', {y: 330, mode: 'absolute', duration: 0.2});
		}
	}

	setTimeout(fluttua, 250);
}

// imposta il valore di questo campo
function impostaValore(campo, val) {
	if ($(campo)) {
		$(campo).value = val;
	}
}

// esegue un'altra funzione
function doOtherFunction(e, b) {
	eval(b.lang);
}

// visualizzo l'icona di caricamento
function showLoading(div) {
	if ($('datiloading')) {
		$('datiloading').remove();
	}
	//new Insertion.Top(div, '<div id="loading" style="padding: 5px 0 10px;"><img src="immagini/loading.gif" alt="loading" title="" style="vertical-align: middle;" /> Caricamento dati..</div>');
	$(div).innerHTML = '<div id="datiloading" style="padding: 5px 0 10px;"><img src="immagini/loading.gif" alt="loading" title="" style="vertical-align: middle;" /> Caricamento dati..</div>';
}

// nascondo l'immagine di caricamento
function hideLoading() {
	if ($('datiloading')) {
		$('datiloading').remove();
	}
}

// conto quanti immobili corrispondono al filtro
function calcolaImmobili() {
	if ($('result_check')) {
		$('result_check').show();
		// chiamata ad ajax per visualizzare il link della lingua
		var pars = "tipo_ricerca="+$F('tipo_ricerca')
			+ "&provincia="+$F('provincia')
			+ "&citta="+$F('citta')
			+ "&zona="+$F('zona')
			+ "&destinazione="+$F('destinazione')
			+ "&tipologia="+$F('tipologia')
			+ "&camere="+$F('camere')
			+ "&arredato="+$F('arredato')
			+ "&prezzo_da="+$F('prezzo_da')
			+ "&prezzo_a="+$F('prezzo_a');

		var url = "xml_calcola_corrispondenze.php";
		var MyAjax = new Ajax.Updater(
			{
				loading: showLoading('result_check'),
				success: 'result_check'
			},
			url,
			{
				method: 'POST',
				parameters: pars,
				onFailure: 'result_check',
				evalScripts: true
			}
		);
	}
}

// conto quanti immobili corrispondono al filtro
function mostraImmobili() {
	if ($('result')) {

		// chiamata ad ajax per visualizzare il link della lingua
		var pars = "tipo_ricerca="+$F('tipo_ricerca')
			+ "&provincia="+$F('provincia')
			+ "&citta="+$F('citta')
			+ "&zona="+$F('zona')
			+ "&destinazione="+$F('destinazione')
			+ "&tipologia="+$F('tipologia')
			+ "&arredato="+$F('arredato')
			+ "&prezzo_da="+$F('prezzo_da')
			+ "&prezzo_a="+$F('prezzo_a')
			+ "&riferimento="+$F('riferimento');

		var url = "do_ricerca.php";
		var MyAjax = new Ajax.Updater(
			{
				loading: showLoading('result'),
				success: 'result'
			},
			url,
			{
				method: 'POST',
				parameters: pars,
				onFailure: 'result',
				evalScripts: true
			}
		);
	}
}

// conto quanti immobili corrispondono al filtro
function ShowChisiamo(tipo) {
	if (tipo == 'privati') {
		var url = "xml_chi_siamo_privati.php";
	} else {
		var url = "xml_chi_siamo_agenzie.php";
	}
	var MyAjax = new Ajax.Updater(
		{
			loading: showLoading('xmlupd'),
			success: 'xmlupd'
		},
		url,
		{
			onFailure: 'xmlupd',
			evalScripts: 1
		}
	);
}

function checkCampi() {
	if (!$('nome') || $F('nome') == "") {
		alert("Il campo 'Nome e Cognome' è obbligatorio.");
		$('nome').focus();
		new Effect.Highlight('nome', {startcolor: '#F4651F'});
		return false;
	}
	if (!$('telefono') || $F('telefono') == "") {
		alert("Il campo 'Telefono' è obbligatorio.");
		$('telefono').focus();
		new Effect.Highlight('telefono', {startcolor: '#F4651F'});
		return false;
	}
	if (!$('email') || $F('email') == "") {
		alert("Il campo 'Email' è obbligatorio.");
		$('email').focus();
		new Effect.Highlight('email', {startcolor: '#F4651F'});
		return false;
	}
	if (!$('private_key') || $F('private_key') == "") {
		alert("Il campo 'Ricopia il codice' è obbligatorio.");
		$('private_key').focus();
		new Effect.Highlight('private_key', {startcolor: '#F4651F'});
		return false;
	}
	return true;
}

function checkCampiVendita() {
	if (!$('nome') || $F('nome') == "") {
		alert("Il campo 'Nome e Cognome' è obbligatorio.");
		$('nome').focus();
		new Effect.Highlight('nome', {startcolor: '#F4651F'});
		return false;
	}
	if (!$('telefono') || $F('telefono') == "") {
		alert("Il campo 'Telefono' è obbligatorio.");
		$('telefono').focus();
		new Effect.Highlight('telefono', {startcolor: '#F4651F'});
		return false;
	}
	if (!$('private_key') || $F('private_key') == "") {
		alert("Il campo 'Ricopia il codice' è obbligatorio.");
		$('private_key').focus();
		new Effect.Highlight('private_key', {startcolor: '#F4651F'});
		return false;
	}
	return true;
}

function checkPreventivo() {
	if (!$('nome') || $F('nome') == "") {
		alert("Il campo 'Nome e Cognome' è obbligatorio.");
		$('nome').focus();
		new Effect.Highlight('nome', {startcolor: '#F4651F'});
		return false;
	}
	if (!$('telefono') || $F('telefono') == "") {
		alert("Il campo 'Telefono' è obbligatorio.");
		$('telefono').focus();
		new Effect.Highlight('telefono', {startcolor: '#F4651F'});
		return false;
	}
	if (!$('email') || $F('email') == "") {
		alert("Il campo 'Email' è obbligatorio.");
		$('email').focus();
		new Effect.Highlight('email', {startcolor: '#F4651F'});
		return false;
	}
	if (!$('private_key') || $F('private_key') == "") {
		alert("Il campo 'Ricopia il codice' è obbligatorio.");
		$('private_key').focus();
		new Effect.Highlight('private_key', {startcolor: '#F4651F'});
		return false;
	}
	return true;
}

// slide delle proposte in home page
function slideProposte(val) {
	if (val == 1) {
		var url = 'xml_proposte_random.php';
	} else {
		var url = 'xml_proposte_last.php';
	}
	var MyAjax = new Ajax.Updater(
		{
			loading: showLoading('result'),
			success: 'result'
		},
		url,
		{
			onFailure: 'result'
		}
	);
}
