//altezza blocco intestazione
var blockHeight = 23;
//recupero offset x
var offsetRecover = 4;
//recupero offset x pointer (layer freccia)
var offsetPointerRecover = 15;
//recupero offset y pointer (layer freccia)
var offsetYPointerRecover = 2;
//array di oggetti e valori
var classi = new Array();
/**
 * Aggiustamento posizione layer 
 */
function layadj(layName, laymaster, laypoint) {

	//verifica se il posizionamento ? gi? stato ricalcolato.
    i = 0;
    while (i < classi.length) {
        if (classi[i] == layName) {
            return;
        }
        i++;
    }
    var y = calcolaCentro(layName, blockHeight);
    document.getElementById(layName).style.top = y + "px";
    var x = findPosX(document.getElementById(laymaster));
    x = eval(x) + eval(document.getElementById(laymaster).offsetWidth) - offsetRecover;
    document.getElementById(layName).style.left = x + "px";
    
    //visualizza pointer (layer freccia)
    var ypoint = (eval(findPosY(document.getElementById(laymaster))) - eval(offsetYPointerRecover)) + eval(4);
    var xpoint = eval(findPosX(document.getElementById(laymaster))) + eval(document.getElementById(laymaster).offsetWidth) - offsetPointerRecover;
    document.getElementById(laypoint).style.top = ypoint + "px";
    document.getElementById(laypoint).style.left = xpoint + "px";
    
    
    //inserisce in array
    classi[classi.length] = layName;
}
/**
 * mostra il layer e  il layer con la freccia (pointer) di giunzione tra i menu
 */
function showLayers(layName, laypointer) {
	//apertura esclusiva
	
	
    if (document.getElementById(layName).style.display == "block") {
        document.getElementById(layName).style.display = "none";
        document.getElementById(laypointer).style.display = "none";
		modalWin(0);
    } else {
    	
        document.getElementById(laypointer).style.display = "block";
        document.getElementById(layName).style.display = "block";
        exclusiveOpen(layName, laypointer);
		modalWin(1);
    }
}
/**
 * mostra il layer e  il layer con la freccia (pointer) di giunzione tra i menu
 */
function exclusiveOpen(layName, laypointer) {
	var lay = new Array();
	var laypoi = new Array();
	
	lay[0] = "newdivcontinente";
	laypoi[0] = "cont-puntatore";
  	lay[1] = "newdiv1";
  	laypoi[1] = "point-nazione";
  	lay[2] = "newdivprovincia";
  	laypoi[2] = "point-provincia";
  	lay[3] = "newdivregione";
  	laypoi[3] = "point-regione";
  	lay[4] = "newdivcomune";
  	laypoi[4] = "point-comune";
  	lay[5] = "newdivtipologia";
  	laypoi[5] = "point-tipologia";
  	lay[6] = "newdivcontratto";
  	laypoi[6] = "point-contratto";
  	lay[7] = "newdivvani";
  	laypoi[7] = "point-vani";
  	lay[8] = "newdivprezzo";
  	laypoi[8] = "point-prezzo";

	 if (document.getElementById(layName).style.display == "block") {
			for (i = 0; i < lay.length; i++) {
				if (layName != lay[i]) {
						try {
							document.getElementById(lay[i]).style.display = "none";
							document.getElementById(laypoi[i]).style.display = "none";
						} catch(e) {
							 // alert(e.message);
						}

				}
			}
	 }
  	
}
/**
 * mostra i layer
 */
function showLayer(layName) {

 	//continente
    if (layName == "newdivcontinente") {
        showLayers("newdivcontinente", "cont-puntatore");
        layadj(layName, "continente-sel", "cont-puntatore");

    }
    if (layName == "newdiv1") {
        showLayers(layName, "point-nazione");
        layadj(layName, "nazione-sel", "point-nazione");

    }
    if (layName == "newdivprovincia") {
        showLayers(layName, "point-provincia");
        layadj(layName, "provincia-sel", "point-provincia");

    }
    if (layName == "newdivregione") {
        showLayers(layName, "point-regione");
        layadj(layName, "regione-sel", "point-regione");

    }
    if (layName == "newdivcomune") {
        showLayers(layName, "point-comune");
        layadj(layName, "comune-sel", "point-comune");

    }
    if (layName == "newdivtipologia") {
        showLayers(layName, "point-tipologia");
        layadj(layName, "tipologia-sel", "point-tipologia");

    }
    if (layName == "newdivcontratto") {
        showLayers(layName, "point-contratto");
        layadj(layName, "contratto-sel", "point-contratto");

    }
    if (layName == "newdivvani") {
        showLayers(layName, "point-vani");
        layadj(layName, "vani-sel", "point-vani");

    }
    if (layName == "newdivprezzo") {
        showLayers(layName, "point-prezzo");
        layadj(layName, "prezzo-sel", "point-prezzo");
    }
}
/**
  * caricamento esterno.
  */
function loadExternal(bufferID, url, idDivToUse, index) {
	//alert("divToPopulate: "+idDivToUse);
    if (window.frames[bufferID]) {
        window.frames[bufferID].location = url;
        var divToPopulate = document.getElementById ? document.getElementById(idDivToUse) : document.all ? document.all[idDivToUse] : null;
        if (divToPopulate && typeof divToPopulate.innerHTML != "undefined") {
            unloadDiv(divToPopulate, index);
            divToPopulate.innerHTML = "<div style=\"color: #990000;\">Caricando...</div>";
        }
        return false;
    }
    return true; // other browsers follow link
}
/**
 *	Visualizza testo dentro un div
 */
function textIndDiv(divname, text, inputname, valin, type) {
	try {
			if(type == 'container') {
				text = "<small>" + text + "</small>"
			} else {
				text = text.toUpperCase();
			}
		    //document.getElementById(divname).innerHTML = "";
		    bodyhtml = "<table width='200' border='0'  cellpadding='0' cellspacing='0' class='tastoScelta' id='tastoScelta" + type + "'>";
		    bodyhtml += "<tr>";
		    bodyhtml += "<td width='4' id='tastoSceltaLeft" + type + "' align='left' valign='top'><img src='common/images/background/form_title_sx2.gif' style='border: 0px solid' width='4' height='19' /></td>";
		    bodyhtml += "<td align='left' class='TestiGrigi' id='tastoSceltaMiddle' onClick='javascript: document.getElementById(\"frecciaDx" + inputname + "\").src=\"common/images/background/form_title_dx5.gif\";'><b>" + text + "</b></td>";
		    bodyhtml += "<td width='7' align='right' valign='top' id='tastoSceltaRight" + type + "'><img src='common/images/background/form_title_dx3.gif' id='frecciaDx" + inputname + "' style='border: 0px solid' width='16' height='19' /></td>";
		    bodyhtml += "</tr>";
		    bodyhtml += "</table>";
		    bodyhtml += "<input type='hidden' name='" + inputname + "' value='" + valin + "'>";
		    document.getElementById(divname).innerHTML = bodyhtml;
		    //document.getElementById(inputname).value = valin;
		    bodyhtml = null;
	    }
	    catch (e) {
			//alert(divname);
		}			
}
/**
 * gestione opacit?
 */
function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if (opacStart > opacEnd) {
        for (i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')", (timer * speed));
            timer++;
        }
    } else {
        if (opacStart < opacEnd) {
            for (i = opacStart; i <= opacEnd; i++) {
                setTimeout("changeOpac(" + i + ",'" + id + "')", (timer * speed));
                timer++;
            }
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}
/**
 * scrurisce o schiarisce una linea
 */
function darkline(id, color) {
    document.getElementById(id).style.backgroundColor = color;
}
/**
 * gestione i frame
 */
/**
 * carica il conenuto dell' iframe nel div
 */
    // Callback function
function displayExternal(idDiv, buffer) {
    var loadedDiv = document.getElementById ? document.getElementById(idDiv) : document.all ? document.all[idDiv] : null;
    if (window.frames[buffer] && loadedDiv && typeof loadedDiv.innerHTML != "undefined") {
        loadedDiv.innerHTML = window.frames[buffer].document.body.innerHTML;
    }
 
}
  
/**
 * elimina il contenuto di un layer div Container
 */
function unloadDiv(divToUnload, index, n, titlePrefix) {
    var loadedDiv = document.getElementById("id" + divToUnload + "Container");
    loadedDiv.innerHTML = "";
}



/** ricarica l'iframe **/
function caricaLista(parent, idParent, index, buffer) {
    loadExternal(buffer, "/common/components/microArea/" + parent.toLowerCase() + "Loader.jsp?parent=" + idParent, index);
}

/** ricarica l'iframe con il livello superiore al parent**/
function caricaListaParentUp(upparent, idParent, index) {
	var link = "/common/components/microArea/" + upparent.toLowerCase() + "Loader.jsp?parent=0&parentup=" + idParent;
    loadExternal("buffer3", link, 0);
}

/**
 * carica la lista prezzi
 */
function caricaListaPrezzi(idContratto, index) {
    loadExternal("buffer", "/sections/immobili/microarea/prezzoLoader.jsp?idContratto="+idContratto, "", index);
}

/**
 *  mostra il livello e lo riposiziona
 */
function displayAdj(layname, sublay, pointlay, menulay, buffer) {
    displayExternal(layname, buffer);
    unAdjLay(menulay);
    layadj(sublay, layname, pointlay);
    
}


/**
 * toglie il livello dall'array dei div riposizionati
 */
function unAdjLay(layname) {
	var i = eval(0);
    while (i < classi.length) {
        if (classi[i] == layname) {
            classi[i] = "";
        }
        i++;
    }
}
/**
* Ricarica i prezzi
*/
function reloadPrezzi(idContratto, form, index) {
    caricaListaPrezzi(idContratto, index);
}

/**
* Resetta la nazione
*/
function resetNazione() {
    textIndDiv("idNazioneContainer", "Nazione ...", "", "", "container");
}

/**
* Resetta la regione
*/
function resetRegione() {
    textIndDiv("idRegioneContainer", "Regione ...", "", "", "container");
}

/**
* Resetta la provincia
*/
function resetProvincia() {
    textIndDiv("idProvinciaContainer", "Provincia ...", "", "", "container");
}

/**
* Resetta il comune
*/
function resetComune() {
	if(document.getElementById("idComuneContainer")) {
    	textIndDiv("idComuneContainer", "Comune ...", "", "", "container");
	}
}

/**
* Ricarica le nazioni
*/
function reloadNazioni(idContinente, form, index) {
    //unloadDiv("Nazione", 1, 0, "Nazione");
   	resetRegione();
   	resetProvincia();
	resetComune();
    caricaLista("Nazione", idContinente, index, 'buffer1');
}

function reloadNazioniConFlash(idContinente, form, index) {
	reloadNazioni(idContinente, form, index);    
    if(idContinente != '')
    	gotoContinente(idContinente);
}
/**
 * ricarica le regioni 
 */
function reloadRegioni(idNazione, form, index) {

   // unloadDiv("Regione", 1, 0, "Regione");
    resetProvincia();
	resetComune();
	caricaLista("Regione", idNazione, index, 'buffer2');
    //unloadDiv("Provincia", 1, 0, "Provincia");
    caricaListaParentUp("Provincia", idNazione, index);
}
function reloadRegioniConFlash(idNazione, form, index) {
	reloadRegioni(idNazione, form, index)    
    if(idNazione != '')
    	gotoNazione(idNazione);
}
/**
 * ricarica le regioni  senza tutte le provincie
 */
function reloadRegioniNoProv(idNazione, form, index) {
    //unloadDiv("Regione", 1, 0, "Regione");
   // resetProvincia();
	//resetComune();
	caricaLista("Regione", idNazione, index, 'buffer2');
    
    if(idNazione != '')
    	gotoNazione(idNazione);
}
/**
 * ricarica le province
 */
function reloadProvince(idRegione, index, form, type) {
	//unloadDiv("Provincia", 1, 0, "Provincia");
 	resetComune();
	caricaLista("Provincia", idRegione, index, 'buffer3');
}
function reloadProvinceConFlash(idRegione, index, form, type) {
	reloadProvince(idRegione, index, form, type)
	try {
		if(idRegione != '') {
	    	gotoRegione(idRegione);
	    }
	} catch(e) {
	 //alert(e.message);
	}
	
}
/**
 * Reload Comuni
 */
function reloadEndComuniConFlash(idProvincia, index, form) {
}
function reloadComuniConFlash(idProvincia, index, form) {
	reloadComuni(idProvincia, index, form);
}
function reloadComuni(idProvincia, index, form) {
	try {
	    if(idProvincia != '') {
	    	gotoProvincia(idProvincia);
	    }
	} catch(e) {
	 //alert(e.message);
	}
	
    idDiv = "idComuneContainer";
    var loadedDiv = document.getElementById ? document.getElementById(idDiv) : document.all ? document.all[idDiv] : null;
    

	caricaLista("Comune", idProvincia, index, 'buffer4');

	resetComune();

	    if (document.forms[0].name == "immobiliForm") {
            doImmobileAction(document.forms[0], "SE");
        }
        if (document.forms[0].name == "agenzieForm") {
            doAgenziaAction(document.forms[0], "SE");
        }
  }


/**
 * esegue questa funzione dopo la selazione di una tipologia
 */
function reloadTipologieConFlash(idTipologia, index, form, operazione) {
	reloadTipologie(idTipologia, index, form, operazione);
}
function reloadTipologie(idTipologia, index, form, operazione) {

}
/**
 * Contratti
 */
function reloadContrattiConFlash(idContratto, index, form) {
	reloadContratti(idContratto, index, form);
}
function reloadContratti(idContratto, index, form) {
    if (document.getElementById("ricerca-avanzata")) {
        if (document.forms[0].name == "immobiliForm") {
            doImmobileAction(document.forms[0], "SE");
        }
        if (document.forms[0].name == "agenzieForm") {
            doAgenziaAction(document.forms[0], "SE");
        }
    }
}
/**
* Ricarica i vani
*/
function reloadVaniConFlash(form) {
	reloadVani(form);
}
function reloadVani(form) {
    var vanida = form.elements['numeroVaniDa'].value;
    var vana = form.elements['numeroVaniA'].value;
//    var vanida = form.numeroVaniA.options[form.numeroVaniDa.selectedIndex].value;
//    var vana = form.numeroVaniA.options[form.numeroVaniA.selectedIndex].value;
    if (vana == "") {
        return;
    }
    if (eval(vanida) > eval(vana)) {
    	form.elements['numeroVaniA'].value = "";
        alert("I locali 'A' devono essere maggiori di locali 'Da'!");
    }

}
/**
* Ricarica i prezzi
*/
function reloadPrezzoConFlash(form) {
	reloadPrezzo(form);
}
function reloadPrezzo(form) {
	var preda = form.elements['prezzoDa'].value;
	var prea = form.elements['prezzoA'].value;
//    var preda = form.idPrezzoDa.options[form.idPrezzoDa.selectedIndex].value;
//    var pra = form.idPrezzoA.options[form.idPrezzoA.selectedIndex].value;
    if (prea == "") {
        return;
    }
    if (eval(preda) > eval(prea)) {
        form.elements['prezzoA'].value = "";
        alert("Il prezzo A deve essere maggiore del prezzo Da!");
    }

}
/**
 * gotoComuni
 */
function gotoComune() {
}
/**
 * gotoProvincia
 */
function gotoProvincia() {
}
/**
 * gotoContratto
 */
function gotoContratto() {
}

/** seleziona menu */
function selectedMenu(layermaster) {
    if (layermaster == "Continente") {
        layertype = "Nazione";
    } else {
        if (layermaster == "Nazione") {
            layertype = "Regione";
        }
    }
    if (layermaster == "Regione") {
        layertype = "Provincia";
    }
    if (layermaster == "Provincia") {
        layertype = "Comune";
    }
    if (layermaster == "Comune") {
        layertype = "Comune";
    }
    if (layermaster == "Tipologia") {
        layertype = "Tipologia";
    }
    var layleft = "tastoSceltaLeft" + layertype;
    var laytable = "tastoScelta" + layertype;
    var layright = "tastoSceltaRight" + layertype;
    alert(document.getElementById(layleft).innerHTML);
    document.getElementById(laytable).style.backgroundImage = "url('/common/images/background/form_title_bg3.gif')";
    document.getElementById(layleft).innerHTML = "<img src=\"common/images/background/form_title_sx3.gif\" style=\"border: 0px solid\" width=\"4\" height=\"19\" />";
    document.getElementById(layright).innerHTML = "<img src=\"common/images/background/form_title_dx3.gif\" style=\"border: 0px solid\" width=\"16\" height=\"19\" />";
}

