String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

/**
 *
 */
function setTipoDisponibilita (select) {
	var libero = select.form.libero;
	var option = select.options[select.selectedIndex];
	if (option.value == '')
		libero.value = "";
	else
		libero.value = option.text;
}

/**
 *
 */
function setParentValue (idParentData, value) {
	if (opener == null)
		return;
	
	var outputId = document.getElementById ('output').name;
	var outputValue = document.getElementById ('output').value;
	var outputDescription = document.getElementById ('descrizione_output').value;
	opener.document.getElementsByName(outputId)[0].value = outputValue;
	opener.document.getElementsByName("descrizione_"+outputId)[0].value = outputDescription;
}

/**
 *
 */
function setFormsAction () {
	if (opener.location.href == location.href)
		return;
	for (var i=0; i<document.forms.length; i++) {
		document.forms[i].action += "&menu=false";
	}
}

function toggleIFrame (iFrameId) {
	if (isIFrameVisible(iFrameId))
		hideIFrame (iFrameId);
	else
		showIFrame (iFrameId);
}

function isIFrameVisible (iFrameId) {
	var iFrame = document.getElementById(iFrameId);
	
	return (iFrame.style.display != 'none');
}

/**
 *
 */
function showIFrame (iFrameId) {
	var iFrame = document.getElementById(iFrameId);
	iFrame.style.display = 'block';
}

/**
 *
 */
function hideIFrame (iFrameId) {
	var iFrame = document.getElementById(iFrameId);
	iFrame.style.display = 'none';
}


function popupSize (url, w, h) {
	popupOptions (url, w, h,"status=no, menubar=no, toolbar=no, scrollbar=yes");
}

function popupOptions (url, w, h, options) {
	var l = Math.floor((screen.width-w)/2);
   var t = Math.floor((screen.height-h)/2);
   window.open(url,"", options+", width=" + w + ",height=" + h + ",top=" + t + ",left=" + l);
}

/**
 *
 */
function popup(url) {
	var w = 400;
	var h = 400;
	popupSize (url, w, h);
}
 
 /**
  *
  */
function rendiPrincipale (imgObject) {
	var imgPrincipale = document.getElementById('principale');
	var swap = imgPrincipale.src.replace (/public\/mid_/, "public\/thmb_");
	imgPrincipale.src = imgObject.src.replace(/public\/thmb_/, "public\/mid_");
	imgObject.src = swap;	
}

/**
 *
 */
function popupImg (imgObject) {
	var url = imgObject.src;
	url = url.replace (/public\/mid_/, "public\/");
	popupOptions (url, 800, 600,"status=no, menubar=no, toolbar=no, scrollbar=yes, resizable=yes");
}


/**
 *
 */
function rimuoviImmagine (imgName) {
	var xmlhttp = new XMLHttpRequest();
	var url = "rimuovi-immagine.php?nome="+imgName;

    xmlhttp.open("GET", url , false);
    // if needed set header information 
    // using the setRequestHeader method
    xmlhttp.send('');
    var xml = xmlhttp.responseXML;
	var objects = document.getElementsByTagName ('span');
	for (var i=0; i<objects.length; i++) {
		if (objects[i].getAttribute('name') == imgName) {
			var parentNode = objects[i].parentNode;
			parentNode.removeChild (objects[i]);
			break;
		}
	}
	
	var immagini = document.getElementById ('immobile').immagini;
	var values = immagini.value.split('@');
	var newValue = '';
	for (var i=0; i<values.length; i++) {
		if (values[i] == imgName)
			continue;
		
		newValue += '@'+values[i];
	}
	
	immagini.value = newValue.substring (1, newValue.length);
}

/**
 *
 */
function deleteImmobile (id) {
	if (!confirm ("Quest'operazione eliminerà l'immobile e tutti i dati ad esso associati. Continuare?"))
		return;
	
	var xmlhttp = new XMLHttpRequest();
	var url = "?section=admin&page=immobile&action=delete&id="+id;

    xmlhttp.open("GET", url , false);
    // if needed set header information 
    // using the setRequestHeader method
    xmlhttp.send('');
	
	// TODO: qui non considero casi di errore!!
	var div = document.getElementById (id);
	var parent = div.parentNode;
	parent.removeChild (div);	
}

/*
 * 
 */
function aggiungiRicerca (idAcquirente) {
	var xmlhttp = new XMLHttpRequest();
	var url = "ricerche.php?section=admin&action=add&tipo_ricerca=1&acquirente="+idAcquirente;
	// evito la cache del browser:
	var d = new Date ();
	url += "&avoidBrowserCache="+d.getTime();
    xmlhttp.open("GET", url , false);
    xmlhttp.send('');
    var text = xmlhttp.responseText;
    text = text.trim ();
    var domParser = new DOMParser ();
    var xml = domParser.parseFromString (text, "text/xml");

	var idRicerca = xml.getElementsByTagName('ricerca')[0].getAttribute ('id');
	
	var listaRicerche = document.getElementById("ricerche");
	var templateRicerca = document.getElementById("ricerca_hidden");
	var	templateChild = getFirstChildElement(templateRicerca);	
	var nuovaRicerca = templateChild.cloneNode(true);
	nuovaRicerca.setAttribute ("id", "ricerca_"+idRicerca);
	var formRicerca = nuovaRicerca.getElementsByTagName('form')[0];
	formRicerca.id.value = idRicerca;

	listaRicerche.appendChild (nuovaRicerca);
}

/*
 * 
 */
function aggiungiRicercaImmobile (idAcquirente) {
	var xmlhttp = new XMLHttpRequest();
	var url = "ricerche.php?section=admin&action=add&tipo_ricerca=2&acquirente="+idAcquirente;
	// evito la cache del browser:
	var d = new Date ();
	url += "&avoidBrowserCache="+d.getTime();
    xmlhttp.open("GET", url , false);
    xmlhttp.send('');
    var text = xmlhttp.responseText;
    text = text.trim ();
    var domParser = new DOMParser ();
    var xml = domParser.parseFromString (text, "text/xml");
  
	var idRicerca = xml.getElementsByTagName('ricerca')[0].getAttribute ('id');
	
	var listaRicerche = document.getElementById("ricerche_immobili");
	var templateRicerca = document.getElementById("immobili_ricerca_hidden");
	var	templateChild = getFirstChildElement(templateRicerca);	
	var nuovaRicerca = templateChild.cloneNode(true);
	nuovaRicerca.setAttribute ("id", "ricerca_"+idRicerca);
	var formRicerca = nuovaRicerca.getElementsByTagName('form')[0];
	formRicerca.id.value = idRicerca;
	
	listaRicerche.appendChild (nuovaRicerca);
}


function copyValues (destForm, sourceForm) {
	for (var i=0; i<sourceForm.elements.length; i++) {
		var element = sourceForm.elements[i];

		if (element.type == 'text') {
			alert (element.value);
			destForm.elements[i].value = element.value;
			continue;
		}
		
		if (element.type == 'button')
			continue;
		
		if (element.type == 'hidden')
			continue;
			
		if (element.type == 'select-one') {
			if (destForm.elements[i].options.length > element.selectedIndex)
				destForm.elements[i].selectedIndex = element.selectedIndex;
				
			continue;
		}
		alert("after:"+element.name+" tipo:"+element.type);
	}
}

/*
 *
 */
function getFirstChildElement (node) {
	return getChildElement (node, 0);
}

/*
 *
 */
function getChildElement (node, number) {
	var childNodes = node.childNodes;
	var childNumber = -1;
	for (var i=0; i<childNodes.length; i++) {
		if (childNodes[i].nodeType == 1) {
			childNumber++;
			
			if (childNumber == number)
				return childNodes[i];
		}
	}
}

function getChildElementsCount (node) {
	var childNodes = node.childNodes;
	var childCount = 0;
	for (var i=0; i<childNodes.length; i++) {
		if (childNodes[i].nodeType == 1)
			childCount++;
	}
	
	return childCount;
}

function salvaRicerca (button) {
	var form = button.form;	
	var sarissa = new Sarissa();
	var url = "ricerche.php?page=ricerca&action=edit&"+Sarissa.formToQueryString(form);
	//alert (url);
	
	var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET", url , false);
    xmlhttp.send('');
    var text = xmlhttp.responseText;
    text = text.trim ();
    
    if (location.href.indexOf("action=incrocia") > 0) {
    	location.reload (true);
    }
}

/*
 *
 */
function eliminaRicerca (button) {
	var id = button.form.id.value;
	var xmlhttp = new XMLHttpRequest();
	var url = "ricerche.php?section=admin&action=delete&id="+id;
    xmlhttp.open("GET", url , false);
    xmlhttp.send('');
	
	// TODO: qui non considero casi di errore!!
	var div = document.getElementById ("ricerca_"+id);
	var parent = div.parentNode;
	parent.removeChild (div);
	
	if (location.href.indexOf("action=incrocia") > 0) {
    	location.reload (true);
    }
}

/* 
 *
 */
 function saveNote (button, id) {
 	var form = button.form;
 	var url = "?page=clienti&action=edit&section=admin&id="+id;
 	var xmlhttp = new XMLHttpRequest();
 	xmlhttp.open("POST", url , false);
 	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

 	xmlhttp.send('note='+form.note.value);

 	alert ("Note salvate.");
 }
