function shareThis(site,obj){
        u=encodeURIComponent(location.href);
        t=encodeURIComponent(document.title);
        switch(site){
                case "facebook":
                        obj.href = "http://www.facebook.com/sharer.php?u="+u+"&t="+t;
                break;
                case "segnalo":
                        obj.href = "http://segnalo.alice.it/post.html.php?url="+u+"&title="+t;
                break;
                case "wikio":
                        obj.href = "http://www.wikio.it/vote?url="+u;
                break;
                case "yahoo_myweb":
                        obj.href = "http://myweb2.search.yahoo.com/myresults/bookmarklet?u="+u+"&t="+t;
                break;
                case "google":
                        obj.href = "http://www.google.com/bookmarks/mark?op=edit&bkmk="+u+"&title="+t;
                break;
                case "win_live":
                        obj.href = "https://favorites.live.com/quickadd.aspx?marklet=1&mkt=en-us&url="+u+"&title="+t+"&top=1";
                break;
                case "ok_no":
                        obj.href = "http://oknotizie.alice.it/post.html.php?url="+u+"&title="+t;
                break;
        }
}

function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, true);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}

function stripHTML(oldString) {
   var newString = "";
   var inTag = false;
   for(var i = 0; i < oldString.length; i++) {
        if(oldString.charAt(i) == '<') inTag = true;
        if(oldString.charAt(i) == '>') {
              if(oldString.charAt(i+1)=="<")
              {
              		//dont do anything
	}
	else
	{
		inTag = false;
		i++;
	}
        }
        if(!inTag) newString += oldString.charAt(i);
   }
   return newString;
}

function buildUriOLD(obj){
  var strUri = "";
  for(var n=0; n<obj.elements.length; n++){
    if(obj.elements[n].name!=""){
		stri = obj.elements[n].value;
    	reg = new RegExp("\\+","g");
    	stri = stri.replace(reg,"caratzPiu");
    	reg = new RegExp("ï¿½","g");
    	stri = stri.replace(reg,"caratzEuro");
		strUri += "&"+obj.elements[n].name+"="+escape(stri);
		strUri = strUri.replace("caratzPiu","%2B");
		strUri = strUri.replace("caratzEuro","%26euro;");
    }
  }
  return strUri;
}

function buildUri(obj){
  var strUri = "";
  for(var n=0; n<obj.elements.length; n++){
    if(obj.elements[n].name!=""){
		stri = obj.elements[n].value;
    	reg = new RegExp("\\+","g");
    	stri = stri.replace(reg,"caratzPiu");
    	reg = new RegExp("€","g");
    	stri = stri.replace(reg,"caratzEuro");
      strUri += "&"+obj.elements[n].name+"="+escape(stri);
      strUri = strUri.replace("caratzPiu","%2B","g");
      strUri = strUri.replace("caratzEuro","%26euro;","g");
      strUri = strUri.replace("%u20AC","%26euro;","g");
		}
  }
  return strUri;
}

function normalizeCampo(obj){
  obj.style.borderColor = "#aaa";
}

function resetBorder(obj) {
	for(var n=0; n<obj.elements.length; n++){
	  if(obj.elements[n].name!=""){
    	obj.elements[n].style.borderColor = "#aaa";
	  }
	}
}

function ctrlKey(e,o){
	if(e.keyCode==13) document.getElementById(o).click();
}

function indirizzoEmailValido(indirizzo) {
  if (window.RegExp) {
    var nonvalido = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
    var valido = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
    var regnv = new RegExp(nonvalido);
    var regv = new RegExp(valido);
    if (!regnv.test(indirizzo) && regv.test(indirizzo))
      return true;
    return false;
  }else {
    if(indirizzo.indexOf("@") >= 0)
      return true;
    return false;
  }
}

function controlField(nome,id,id2,tipo){
	var colore = "#ff6600";
  switch(tipo){
    case "_":       // controlla se il campo ï¿½ vuoto
    if(document.getElementById(id).value == false){
    	alert(nome+": campo mancante");
    	document.getElementById(id).focus();
    	document.getElementById(id).style.border = "1px solid "+colore;
    	return false;
    }else{
    	return true;
    }
    break;
    case "__":      // controlla se il campo ï¿½ vuoto con ""
    if(document.getElementById(id).value == ""){
    	alert(nome+": campo mancante");
    	document.getElementById(id).focus();
    	document.getElementById(id).style.border = "1px solid "+colore;
    	return false;
    }else{
      return true;
    }
    break;
    case "@":       // controlla la validitï¿½ del campo mail
    if(!indirizzoEmailValido(document.getElementById(id).value)){
      alert(nome+": campo non valido");
      document.getElementById(id).style.border = "1px solid "+colore;
      document.getElementById(id).focus();
      return false;
    }else{
      return true;
    }
    break;
    case "=":       // confronta  due campi
    if(document.getElementById(id).value != document.getElementById(id2).value){
      alert("Le Password non Coincidono");
      document.getElementById(id).focus();
      document.getElementById(id).style.border = "1px solid "+colore;
      document.getElementById(id2).value = "";
      return false;
    }else{
      return true;
    }
    break;
    case "h":       // controlla se il campo hidden ï¿½ vuoto
    if(document.getElementById(id2).value == false){
    	alert(nome+": campo mancante");
    	document.getElementById(id).focus();
    	document.getElementById(id).style.border = "1px solid "+colore;
    	return false;
    }else{
    	return true;
    }
    break;
    case "hS":       // controlla se il campo autoSuggest ï¿½ vuoto
    if(document.getElementById(id2).value == false){
    	if(document.getElementById(id).value == false){
    		alert(nome+": campo mancante");
    	}else{
    		alert(nome+": non presente tra i dati disponibili");
    	}
    	document.getElementById(id).focus();
    	document.getElementById(id).style.border = "1px solid "+colore;
    	return false;
    }else{
    	return true;
    }
    break;
    case "c":       // controlla se il campo e' piu' lungo di un tot
    if(document.getElementById(id).value.length > id2){
        alert(nome+": il campo deve contenere un massimo di "+id2+" caratteri");
        document.getElementById(id).focus();
        document.getElementById(id).style.border = "1px solid "+colore;
        return false;
    }else{
        return true;
    }
    break;
    case "c-":       // controlla se il campo e' piu' lungo di un tot
    if(document.getElementById(id).value.length < id2){
        alert(nome+": il campo deve contenere almeno "+id2+" caratteri");
        document.getElementById(id).focus();
        document.getElementById(id).style.border = "1px solid "+colore;
        return false;
    }else{
        return true;
    }
    break;
    case "n":       // controlla se il campo e' un numero
    if(isNaN(document.getElementById(id).value)){
        alert(nome+": il campo deve contenere solo numeri");
        document.getElementById(id).focus();
        document.getElementById(id).style.border = "1px solid "+colore;
        return false;
    }else{
        return true;
    }
    break;
    case "img": // controllo se il file ï¿½ una img
	    obj = document.getElementById(id);
	    var extensions = new Array();  
		 	extensions[1] = "jpg";  
			extensions[0] = "jpeg";  
			extensions[2] = "gif";  
			/*extensions[3] = "png";  
			extensions[4] = "bmp";*/   
			var image_file = obj.value;
			var image_length = image_file.length;
			var pos = image_file.lastIndexOf('.') + 1;   
			var ext = image_file.substring(pos, image_length);   
			var final_ext = ext.toLowerCase();   
			  
			for (i = 0; i < extensions.length; i++){   
			    if(extensions[i] == final_ext){
					return true;   
			    }   
			}
			alert("Seleziona un'immagine con una delle seguenti estensioni:\n\n gif, jpeg, jpg");
			return false;
		break;
	  default:
	  alert("tipo non riconosciuto");
	  return false;
  }
}

function getPosX(obj) {
        var curleft = 0;
        if (obj.offsetParent) {
                while (obj.offsetParent) {
                        curleft += obj.offsetLeft
                        obj = obj.offsetParent;
                }
        }
        else if (obj.x)
        curleft += obj.x;
        return curleft;
}

function getPosY(obj) {
        var curtop = 0;
        if (obj.offsetParent) {
                while (obj.offsetParent) {
                        curtop += obj.offsetTop
                        obj = obj.offsetParent;
                }
        }
        else if (obj.y)
        curtop += obj.y;
        return curtop;
}

function mostraMenu(obj,n){
	this.subMenu = document.getElementById('subMenu');
	//obj.style.background = "#ffffff";
	if(obj){
		x = getPosX(obj) + 15;
		//x = getPosX(obj);
		y = getPosY(obj) + 17;
		document.getElementById('subMenu').style.top = y+"px";
		document.getElementById('subMenu').style.left = x+"px";
	}
	if(n){
		 div = document.getElementById('subMenu');
		 switch(n){
		 	case 1:
		 		div.innerHTML = "Storia<br>";
		 		div.innerHTML += "Geografia<br>";
		 		div.innerHTML += "Archeologia";
		 	break;
		 	case 2:
		 		div.innerHTML = "Balneare<br>";
		 		div.innerHTML += "Culturale<br>";
		 		div.innerHTML += "Termale<br>";
		 		div.innerHTML += "In Barca<br>";
		 		div.innerHTML += "Enologico<br>";
		 		div.innerHTML += "Naturalistico<br>";
		 	break;
		 	case 3:
		 		div.innerHTML = "Come arrivare<br>";
		 		div.innerHTML += "Muoversi<br>";
		 	break;
		 	case 4:
		 		div.innerHTML = "Diving<br>";
		 		div.innerHTML += "Pesca turismo<br>";
		 		div.innerHTML += "Scuola di vela<br>";
		 		div.innerHTML += "Locali<br>";
		 	break;
		}
		/*
		poststr = "&n="+n;
		myajax.post("?cmd=subMenu",poststr);
	  myajax.echoFunction=function(response){
	  	document.getElementById('subMenu').innerHTML = response;
	  }
	  */
  }
  if(obj){
  	changeOpac(0,"subMenu");
  }	
	document.getElementById('subMenu').style.display = "block";
	if(obj){
		opacity("subMenu", 0, 80, 500);
	}	
}

function selCal(anno,mese,campo,adj){
    this.win = document.getElementById("select");
    //ajaxGetRand('?cmd=selCal&mese='+mese+'&anno='+anno+'&campo='+campo,myHandler);
    myajax.post('?cmd=selCal&mese='+mese+'&anno='+anno+'&campo='+campo+'&adj='+adj,"");
    myajax.echoFunction=function(aa){
            if(adj==null) adj=1;
            xx = getPosX(document.getElementById(campo))+adj;
            yy = getPosY(document.getElementById(campo))+21;
            win.style.width = "";
            win.style.height = "";
            win.style.left = xx+"px";
            win.style.top = yy+"px";
            win.innerHTML = aa;
            win.style.display = "block";
    }
}


function nascondiMenu(){
	document.getElementById('subMenu').style.display = "none";
	
}

function nascondiTip(){
	document.getElementById('tip').style.display = "none";	
}

function opacity(id, opacStart, opacEnd, millisec) {
    var speed = Math.round(millisec / 100);
    var timer = 0;
    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++;
        }
    }
}

function setControl(val, obj){
  var parentObj = obj.parentNode.childNodes;
    for(i=0; i<parentObj.length; i++){
    	if(parentObj[i].nodeName == "DIV") {
    		parentObj[i].style.fontWeight = "normal";	
    		parentObj[i].firstChild.src = "img/off.gif";
    	}
    	if(parentObj[i].nodeName == "DIV") parentObj[i].style.color = "#555555";
	    if(parentObj[i].nodeName == "INPUT") parentObj[i].value = val;
    }
    //obj.style.fontWeight = "bolder";
    obj.firstChild.src = "img/on.gif";
    //obj.style.color = "orange";
}


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 + ")";
}

function mostraTip(obj,n){
	this.tip = document.getElementById('tip');
	//obj.style.background = "#ffffff";
	if(obj){
		/*
		konqueror=(/Konqueror[\/\s](\d+\.\d+)/.test(navigator.userAgent))?1:0;
		opera=(/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent))?1:0;
    	mozilla=(/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent))?1:0;
    	ie=(/MSIE (\d+\.\d+);/.test(navigator.userAgent))?1:0;
		*/
     	y = getPosY(obj.parentNode) - 93;
		x = getPosX(obj.parentNode) - 40;
		document.getElementById('tip').style.top = y+"px";
		document.getElementById('tip').style.left = x+"px";
	}
	if(n){
		dummy = obj.nextSibling.innerHTML;
				
		div = document.getElementById('tip')
		div.innerHTML = dummy;		 
		
		/*
		poststr = "&n="+n;
		myajax.post("?cmd=subMenu",poststr);
	  myajax.echoFunction=function(response){
	  	document.getElementById('subMenu').innerHTML = response;
	  }
	  */
  }
  if(obj){
  	changeOpac(0,"tip");
  }	
	document.getElementById('tip').style.display = "block";
	if(obj){
		opacity("tip", 0, 100, 500);
	}	
}

function viewTask(ii){
	//opacity(ii, 100, 0, 500);
	document.getElementById('myGallery').style.display="none";
	document.getElementById('myMap').style.display="none";
	document.getElementById('myContact').style.display="none";
	document.getElementById(ii).style.display="block";
	//document.getElementById('myContact').style.display="none";
	//opacity(ii, 0, 100, 500);
	
}

function openSelect(w,h,idName,idValue,urlAj,corrX,corrY,multi,any){
  this.win = document.getElementById('select');
  selIds = "";
  if(!multi){
    document.onmouseup=function(e){
      e = e || window.event;
      trg = e.srcElement || e.target;
      if(trg.id!="select"){
              document.getElementById('select').style.display='none';
              document.onmouseup='';
      }
    }
  }else {
    selIds = document.getElementById(idValue).value;
  }
  xx = getPosX(document.getElementById(idName))+corrX;
  yy = getPosY(document.getElementById(idName))+corrY;
  win.style.width = w+"px";
  win.style.height = h+"px";
  win.style.overflow = "auto";
  viewMex("Caricamento in Corso");
  myajax.post(urlAj+'&idName='+idName+'&idValue='+escape(idValue)+'&multi='+multi+'&any='+any+'&selIds='+selIds,"");
  myajax.echoFunction=function(aa){
    hideMex(aa,0);
    //if(win.style.display !="block"){
      win.style.top=yy+"px";
      win.style.left=xx+"px";
    //}
    win.innerHTML = aa;
    win.style.display="block";
     //Drag.init(document.getElementById("handle"),document.getElementById('win'));
  }
}

function viewMex(mex){
  dTop = document.body.scrollTop+200;
  document.getElementById('loader').innerHTML = "<img src=\"img/progress.gif\" style=\"vertical-align: -12px\">&nbsp;"+mex;
  document.getElementById('loader').style.top = dTop+"px";
  document.getElementById('loader').style.display = "block";
}

function viewMex2(mex){
  dTop = document.body.scrollTop+200;
  document.getElementById('loader').innerHTML = "<div style='margin-right: 10px; float: left'><img src=\"img/progress.gif\"></div>";
  document.getElementById('loader').innerHTML += "<div style='text-align: left; float: right; font-size: 15px'>"+mex+"</div>";
  document.getElementById('loader').style.top = dTop+"px";
  document.getElementById('loader').style.display = "block";
}

function hideMex(cont,dbg){
  if(dbg){
    document.getElementById('loader').innerHTML = cont;
  }else{
    document.getElementById('loader').style.display = "none";
    document.getElementById('loader').innerHTML = "";
  } 
}

function openSelectEvent(w,h,idName,idValue,urlAj,corrX,corrY,multi,any,doEvent){
	
	this.win = document.getElementById('select');
	selIds = "";
	if(!multi){
		document.onmouseup=function(){
			document.getElementById('select').style.display='none';
			document.onmouseup='';
		}
	}else {
		selIds = document.getElementById(idValue).value;
	}
	
	xx = getPosX(document.getElementById(idName))+corrX;
	yy = getPosY(document.getElementById(idName))+corrY;
	win.style.width = w+"px";
	win.style.height = h+"px";
	win.style.overflow = "auto";
	viewMex("Caricamento in corso...");
	myajax.post(urlAj,'idName='+idName+'&idValue='+escape(idValue)+'&multi='+multi+'&any='+any+'&selIds='+selIds);
	myajax.echoFunction=function(response){
		hideMex(response,0);
	    win.style.top=yy+"px";
	    win.style.left=xx+"px";
	    win.style.textAlign = "left";
	    win.innerHTML = response;
	    win.style.display="block";
	    doEvent();
	}
}

function registraUt(obj){
  flag = true;
  flag = flag && controlField("E-MAIL",'blabla','','_');
  flag = flag && controlField("E-MAIL",'blabla','','@');
  
  flag = flag && controlField('CATEGORIA','catM','idCatM','h');
  
  flag = flag && controlField("PASSWORD",'passR','','_');
  flag = flag && controlField("PASSWORD",'passR','6','c-');
  flag = flag && controlField("PASSWORD",'passR','pass2R','=');
  flag = flag && controlField("AUTORIZZAZIONE AL TRATTAMENTO DEI DATI PERSONALI",'concedo','','_');
  if(flag){
    strUri = buildUri(obj);
    viewMex("Registrazione in Corso");
    myajax.post("?cmd=exRegistrazione", strUri);
  }
   myajax.echoFunction=function(aa){
    hideMex(aa,0);
    if(aa=="user"){
      //document.getElementById('password').value = "";
      alert("Email presente nel nostro database per la categoria selezionata, utilizzarne una nuova per proseguire con la registrazione.");
    }else if(aa=="ok"){
    	alert("Abbiamo spedito un'email al tuo indirizzo. Clicca sul link nel testo dell'e-mail per completare la registrazione.");
    }
  }
}

function controllaLogin(obj){
	resetBorder(obj); 
	flag = true;
	flag = flag && controlField('USER','user','','_');
	flag = flag && controlField('PASSWORD','pass','','_');
	if(flag){
		strUri = buildUri(obj);
		viewMex("Autenticazione in corso...");
		myajax.post("?cmd=validation",strUri);
	}
	myajax.echoFunction=function(aa){
		hideMex(aa,0);
		if(aa=="no"){
			alert("Account non valido");
		}else if(aa=="oki"){
			//location.href="./?cmd=home";
			window.location.reload(false);
		}else if(aa=="condizioniProp"){
			location.href="./?cmd=condizioniUtilizzoProprietario";
		}else if(aa=="condizioniAsso"){
			location.href="./?cmd=condizioniUtilizzoAssociazione";
		}else if(aa=="condizioniAutoNo"){
			location.href="./?cmd=condizioniUtilizzoAutonoleggio";
		}else if(aa=="condizioniGenerico"){
			location.href="./?cmd=condizioniUtilizzoGenerico";
		}

		
		
	}
}

function modificaDati(obj){
	resetBorder(obj); 
	flag = true;
	flag = flag && controlField('TELEFONO','telefono','','n');
	if(flag){
		strUri = buildUri(obj);
		viewMex("Modifica in corso...");
		myajax.post("?cmd=exModificaDati",strUri);
	}
	myajax.echoFunction=function(aa){
		hideMex(aa,0);
		if(aa=="no"){
			alert("Errore durante la modifica");
		}else if(aa=="ok"){
			alert("Modifica effettuata");
		}
	}
}

function modificaPassword(obj){
  flag = true; 
  flag = flag && controlField("PASSWORD",'passR','','_');
  flag = flag && controlField("PASSWORD",'passR','6','c-');
  flag = flag && controlField("PASSWORD",'passR','pass2R','=');
  if(flag){
    strUri = buildUri(obj);
    viewMex("Modifica in Corso");
  myajax.post("?cmd=exModificaPassword", strUri);
  }
   myajax.echoFunction=function(aa){
    hideMex(aa,0);
    if(aa=="ok"){
	alert("Modifica effettuata");
    }
  }
}

function addDammuso(obj){
  flag = true; 
  flag = flag && controlField("NOME DAMMUSO",'nomeDam','','_');
  if(flag){
    strUri = buildUri(obj);
    viewMex("Modifica in Corso");
    myajax.post("?cmd=exAddDammuso", strUri);
  }
   myajax.echoFunction=function(aa){
    hideMex(aa,0);
    if(aa=="ok"){
    	alert("Dammuso inserito correttamente");
    	window.location.reload("false"); 
    }
  }
}

function modifyStato(id){
	alert(id);
}

function modifyStato(id){
    viewMex("Modifica in corso...");
    strUri = "&id="+id;
    myajax.post("?cmd=modifyStato",strUri);
    myajax.echoFunction=function(aa){
    	hideMex(aa,0);
    	if(aa=="ok") alert("Modifica effettuata");
    }
}

function deleDammuso(id,obj){
  if(confirm("Conferma eliminazione")){
     viewMex("Caricamento in corso...");
     myajax.post("?cmd=deleDammuso&id="+id,"");
  }
  myajax.echoFunction=function(aa){
    hideMex(aa,0);
    alert("Eliminazione effettuata");
    window.location.reload(false);
  }
}

/*google map*/
var COLORS = [["orange", "#ff8800"]];
var options = {};
var lineCounter_ = 0;
var shapeCounter_ = 0;
var markerCounter_ = 0;
var colorIndex_ = 0;
var featureTable_;
var map;
var cellX = 0;
var cellY = 0;
var marker = null;

function getColor(named) {
  return COLORS[(colorIndex_++) % COLORS.length][named ? 0 : 1];
}

function getIcon(color) {
  var icon = new GIcon();
  icon.image = "http://google.com/mapfiles/ms/micons/" + color + ".png";
  icon.iconSize = new GSize(32, 32);
  icon.iconAnchor = new GPoint(15, 32);
  return icon;
}

function initMap(x,y) {
	cellX = document.getElementById(x);
	cellY = document.getElementById(y);
	zoom = 12;
	if(cellX.value == "") cellX.value = "36.788941";
	if(cellY.value == "") {
		cellY.value = "11.993637";
		zoom=12;
	}; 
  if (GBrowserIsCompatible()) {
  	var opts = {resultList : G_GOOGLEBAR_RESULT_LIST_SUPPRESS, showOnLoad: true, onGenerateMarkerHtmlCallback:doGenerateMarkerHtmlCallback};
    map = new GMap2(document.getElementById("map"), {googleBarOptions: opts});
    map.setCenter(new GLatLng(Number(cellX.value),Number(cellY.value)), zoom);
    map.enableGoogleBar();
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.clearOverlays();
    placeMarker();
  }
}

function doGenerateMarkerHtmlCallback(mark,html,result) {
		mark.hide();
        html.innerHTML+="<b>Coordinate Trovate: "+result.lat+","+result.lng+"<\/b><br>";
        //html.innerHTML+="<b>Marker Location: "+marker.getLatLng().toUrlValue()+"<\/b>";
        marker.setLatLng(mark.getLatLng());
        var latlng = mark.getPoint();
	  cellY.value = String(Math.round(latlng.x * 1000000) / 1000000);
	  cellX.value = String(Math.round(latlng.y * 1000000) / 1000000);
        return html;
}



function placeMarker() {
	if(cellX.value=="" && cellY.value==""){
	  	var listener = GEvent.addListener(map, "click", function(overlay, latlng) {
	    	if (latlng) {
		      GEvent.removeListener(listener);
		      var color = getColor(true);
		      marker = new GMarker(latlng, {icon: getIcon(color), draggable: true});
		      map.addOverlay(marker);
		      updateMarker(marker);
		      GEvent.addListener(marker, "dragend", function() {
		        updateMarker(marker);
		      });
		      GEvent.addListener(marker, "click", function() {
		        updateMarker(marker, true);
		      });
	    	}
  		});
  	}else{
  	  var color = getColor(true);
      var latlng = new GLatLng(cellX.value, cellY.value);
      marker = new GMarker(latlng, {icon: getIcon(color), draggable: true});
      map.addOverlay(marker);
      GEvent.addListener(marker, "dragend", function() {
        updateMarker(marker);
      });
      GEvent.addListener(marker, "click", function() {
        updateMarker(marker, true);
      });
    }
}

function updateMarker(marker, opt_changeColor) {
  if (opt_changeColor) {
    var color = getColor(true);
    marker.setImage(getIcon(color).image);
  }
  var latlng = marker.getPoint();
  cellY.value = String(Math.round(latlng.x * 1000000) / 1000000);
  cellX.value = String(Math.round(latlng.y * 1000000) / 1000000);
}

function modificaDammuso(obj){
    for(var k in myEditor){
            //document.getElementById(k).value = myEditor[k].nicInstances[0].getContent();
    }
    document.getElementById('editor').value = myEditor.nicInstances[0].getContent();
    resetBorder(obj);
    flag = true;
    flag = flag && controlField("NOME",'nome','','_');
    flag = flag && controlField("POSTI LETTO",'postiLetto','','n');
    if(document.getElementById('imgS').value!=""){
    	flag = flag && controlField('IMG INDEX','imgS','','img');
    }
    flag = flag && controlField('PREZZO MINIMO','prezzo1M','','n');
    flag = flag && controlField('PREZZO MINIMO','prezzo1D','','n');
    
    flag = flag && controlField('PREZZO MASSIMO','prezzo2M','','n');
    flag = flag && controlField('PREZZO MASSIMO','prezzo2D','','n');
    
    if(flag){
            viewMex("Salvataggio in corso");
            document.getElementById('modDammuso').submit();
    }
}

var myEditor = new Array();
function openDivNic(w,idOpen,urlAj,corrX,corrY,editor){
  this.win = document.getElementById('win');
  xx = getPosX(document.getElementById(idOpen))+corrX;
  yy = getPosY(document.getElementById(idOpen))+corrY+document.body.scrollTop;;
  win.style.width = w+"px";
  win.style.overflow = "visible";
  viewMex("Caricamento in corso...");
  myajax.post(urlAj,"");
  myajax.echoFunction=function(aa){
    hideMex(aa,0);
    if(win.style.display !="block"){
        win.style.top=yy+"px";
        win.style.left=xx+"px";
    }
    win.innerHTML = aa;
    if(win.style.display!="block"){
        changeOpac(0, "win");
        win.style.display="block";
        opacity("win", 0, 100, 500);
    }
    Drag.init(document.getElementById("handle"),document.getElementById('win'));
    for(i=0;i<editor.length;i++){
      myEditor[editor[i]] = new nicEditor({buttonList: ['bold','italic','underline','left','center','right','forecolor']}).panelInstance(editor[i]);
    }
  }
}

function openDivNicPlus(w,idOpen,urlAj,corrX,corrY,editor){
  this.win = document.getElementById('win');
  xx = getPosX(document.getElementById(idOpen))+corrX;
  yy = getPosY(document.getElementById(idOpen))+corrY+document.body.scrollTop;;
  win.style.width = w+"px";
  win.style.overflow = "visible";
  viewMex("Caricamento in corso...");
  myajax.post(urlAj,"");
  myajax.echoFunction=function(aa){
    hideMex(aa,0);
    if(win.style.display !="block"){
        win.style.top=yy+"px";
        win.style.left=xx+"px";
    }
    win.innerHTML = aa;
    if(win.style.display!="block"){
        changeOpac(0, "win");
        win.style.display="block";
        opacity("win", 0, 100, 500);
    }
    Drag.init(document.getElementById("handle"),document.getElementById('win'));
    for(i=0;i<editor.length;i++){
      myEditor[editor[i]] = new nicEditor({buttonList: ['fontSize','bold','italic','underline','left','center','right','ol','ul','subscript','superscript','indent','outdent','link','unlink','bgcolor','forecolor','xhtml','image','upload','removeformat']}).panelInstance(editor[i]);
    }
  }
}

function inviaFoto(obj){
    for(var k in myEditor){
    	//alert(k);
            document.getElementById('testo').value = myEditor['testo'].nicInstances[0].getContent();
    }
    resetBorder(obj);
    flag = true;
    flag = flag && controlField('FOTO','grande','','_');
    flag = flag && controlField('FOTO','grande','','img');
    if(flag){
            viewMex("Salvataggio in corso");
            document.getElementById('addPhoto').submit();
    }
}

function deleFoto(id,idDamm){	
  if(confirm("Conferma eliminazione")){
	viewMex("Eliminazione in corso...");
	myajax.post("?cmd=deleFoto&id="+id+"&idDamm="+idDamm,"");
  }
  myajax.echoFunction=function(aa){
    hideMex(aa,0);
    alert("Eliminazione effettuata");
    window.location.reload(false);
  }
}

function saveGallery(obj, id){
  document.getElementById('descr'+id).value = myEditor['descr'+id].nicInstances[0].getContent();
  viewMex("Salvataggio in corso...");
  myajax.post("?cmd=saveFoto",buildUri(obj));
  myajax.echoFunction=function(response){
    hideMex(response,0);
    if(response=="ok"){
        alert("Salvataggio effettuato");
        //window.location.reload(false);
    }
  }
}

function inviaContact(obj){
  flag = true;
  flag = flag && controlField("E-MAIL",'blabla','','_');
  flag = flag && controlField("E-MAIL",'blabla','','@');
  flag = flag && controlField("NUMERO ADULTI",'adulti','','_');
  flag = flag && controlField("DATA ARRIVO",'dataArrivo','','_');
  flag = flag && controlField("DATA PARTENZA",'dataPartenza','','_');
  flag = flag && controlField("AUTORIZZAZIONE AL TRATTAMENTO DEI DATI PERSONALI",'concedo','','_');
  if(flag){
    strUri = buildUri(obj);
    viewMex("Invio email in Corso");
    myajax.post("?cmd=exInviaContact", strUri);
  }
  myajax.echoFunction=function(aa){
    hideMex(aa,0);
    if(aa=="ok"){
      //document.getElementById('password').value = "";
      alert("Richiesta inoltrata correttamente, hai ricevuto una notifica sulla tua casella di posta!");
      window.location.reload(false);
    }
  }
}

function inviaEmail(obj){
    document.getElementById('editor').value = myEditor.nicInstances[0].getContent();
    resetBorder(obj);
    strUri = buildUri(obj);
    if(stripHTML(document.getElementById('editor').value)==""){
    	alert("CORPO: campo mancante");
    }else{
        viewMex("Invio email in corso");
        myajax.post("?cmd=exInviaEmail", strUri);
    }
    myajax.echoFunction=function(aa){
    	hideMex(aa,0);
    	if(aa=="ok"){
    		//document.getElementById('password').value = "";
    		alert("Email inviata correttamente!");
    		window.location.reload(false);
    	}
    }
}

function modificaAssociazione(obj){
    for(var k in myEditor){
            //document.getElementById(k).value = myEditor[k].nicInstances[0].getContent();
    }
    document.getElementById('editor').value = myEditor.nicInstances[0].getContent();
    resetBorder(obj);
    flag = true;
    flag = flag && controlField("NOME",'nome','','_');
    if(document.getElementById('imgS').value!=""){
    	flag = flag && controlField('IMG INDEX','imgS','','img');
    }
    if(flag){
            viewMex("Salvataggio in corso");
            document.getElementById('modAssociazione').submit();
    }
}

function inviaContact2(obj){
  flag = true;
  flag = flag && controlField("E-MAIL",'blabla','','_');
  flag = flag && controlField("E-MAIL",'blabla','','@');
  flag = flag && controlField("AUTORIZZAZIONE AL TRATTAMENTO DEI DATI PERSONALI",'concedo','','_');
  if(flag){
    strUri = buildUri(obj);
    viewMex("Invio email in Corso");
    myajax.post("?cmd=exInviaContact2", strUri);
  }
  myajax.echoFunction=function(aa){
    hideMex(aa,0);
    if(aa=="ok"){
      //document.getElementById('password').value = "";
      alert("Richiesta inoltrata correttamente, hai ricevuto una notifica sulla tua casella di posta!");
      window.location.reload(false);
    }
  }
}

function inviaContact3(obj){
	  flag = true;
	  flag = flag && controlField("E-MAIL",'blabla','','_');
	  flag = flag && controlField("E-MAIL",'blabla','','@');
	  flag = flag && controlField("AUTORIZZAZIONE AL TRATTAMENTO DEI DATI PERSONALI",'concedo','','_');
	  if(flag){
	    strUri = buildUri(obj);
	    viewMex("Invio email in Corso");
	    myajax.post("?cmd=exInviaContact3", strUri);
	  }
	  myajax.echoFunction=function(aa){
	    hideMex(aa,0);
	    if(aa=="ok"){
	      //document.getElementById('password').value = "";
	      alert("Richiesta inoltrata correttamente, hai ricevuto una notifica sulla tua casella di posta!");
	      window.location.reload(false);
	    }
	  }
	}

function inviaEmailA(obj){
    document.getElementById('editor').value = myEditor.nicInstances[0].getContent();
    resetBorder(obj);
    strUri = buildUri(obj);
    if(stripHTML(document.getElementById('editor').value)==""){
    	alert("CORPO: campo mancante");
    }else{
        viewMex("Invio email in corso");
        myajax.post("?cmd=exInviaEmailA", strUri);
    }
    myajax.echoFunction=function(aa){
    	hideMex(aa,0);
    	if(aa=="ok"){
    		//document.getElementById('password').value = "";
    		alert("Email inviata correttamente!");
    		window.location.reload(false);
    	}
    }
}

function deleFotoA(id,idDamm){	
  if(confirm("Conferma eliminazione")){
	viewMex("Eliminazione in corso...");
	myajax.post("?cmd=deleFotoA&id="+id+"&idDamm="+idDamm,"");
  }
  myajax.echoFunction=function(aa){
    hideMex(aa,0);
    alert("Eliminazione effettuata");
    window.location.reload(false);
  }
}

function saveGalleryA(obj, id){
  document.getElementById('descr'+id).value = myEditor['descr'+id].nicInstances[0].getContent();
  viewMex("Salvataggio in corso...");
  myajax.post("?cmd=saveFotoA",buildUri(obj));
  myajax.echoFunction=function(response){
    hideMex(response,0);
    if(response=="ok"){
    alert("Salvataggio effettuato");
    //window.location.reload(false);
    }
  }
}

function inviaEvento(obj){
    for(var k in myEditor){
    	//alert(k);
            document.getElementById('testo').value = myEditor['testo'].nicInstances[0].getContent();
    }
    resetBorder(obj);
    flag = true;
    //flag = flag && controlField('FOTO','grande','','_');
    if(document.getElementById('grande').value!=""){
    	flag = flag && controlField('FOTO','grande','','img');
    }
    flag = flag && controlField('TITOLO EVENTO','titolo','','_');
	flag = flag && controlField('DATA EVENTO','data','','_');
    if(flag){
            viewMex("Salvataggio in corso");
            document.getElementById('addEvento').submit();
    }
}

function deleEvento(id){
  if(confirm("Conferma eliminazione")){
	viewMex("Eliminazione in corso...");
	myajax.post("?cmd=deleEvento&id="+id,"");
  }
  myajax.echoFunction=function(aa){
    hideMex(aa,0);
    alert("Eliminazione effettuata");
    window.location.reload(false);
  }
}

function modifyEvento(obj){
    for(var k in myEditor){
    	//alert(k);
     //       document.getElementById('testo').value = myEditor['testo'].nicInstances[0].getContent();
    }
    document.getElementById('testo').value = myEditor.nicInstances[0].getContent();
    resetBorder(obj);
    flag = true;
    //flag = flag && controlField('FOTO','grande','','_');
    if(document.getElementById('grande').value!=""){
    	flag = flag && controlField('FOTO','grande','','img');
    }
    flag = flag && controlField('TITOLO EVENTO','titolo','','_');
	flag = flag && controlField('DATA EVENTO','data','','_');
    if(flag){
            viewMex("Salvataggio in corso");
            document.getElementById('modEvento').submit();
    }
}

function inviaNews(obj){
    for(var k in myEditor){
    	//alert(k);
            document.getElementById('testo').value = myEditor['testo'].nicInstances[0].getContent();
    }
    resetBorder(obj);
    flag = true;
    flag = flag && controlField('TITOLO NEWS','titolo','','_');
		flag = flag && controlField('DATA NEWS','data','','_');
    if(flag){
      viewMex("Salvataggio in corso");
      document.getElementById('addNews').submit();
    }
}

function modifyNews(obj){
    for(var k in myEditor){
    	//alert(k);
     //       document.getElementById('testo').value = myEditor['testo'].nicInstances[0].getContent();
    }
    document.getElementById('testo').value = myEditor.nicInstances[0].getContent();
    resetBorder(obj);
    flag = true;
    flag = flag && controlField('TITOLO NEWS','titolo','','_');
	flag = flag && controlField('DATA NEWS','data','','_');
    if(flag){
            viewMex("Salvataggio in corso");
            document.getElementById('modNews').submit();
    }
}

function deleNews(id){
  if(confirm("Conferma eliminazione")){
	viewMex("Eliminazione in corso...");
	myajax.post("?cmd=deleNews&id="+id,"");
  }
  myajax.echoFunction=function(aa){
    hideMex(aa,0);
    alert("Eliminazione effettuata");
    window.location.reload(false);
  }
}

function viewCommento(){
	if(document.getElementById('insCommento').style.display=="none"){
		document.getElementById('insCommento').style.display='block';
	}else{
		document.getElementById('insCommento').style.display='none';
	}
}

function inviaCommento(obj){
  if(document.getElementById('nick').value==""){
	  alert("Il campo nome/nick e' vuoto di conseguenza il commente verra' firmato come anonimo.");
  }

  /*
  var browser=navigator.appName;
  retCarr = "\n"	
  if (browser.indexOf("Microsoft") >= 0) {
    retCarr = "\r\n";
  }
  text1 = document.getElementById('mex').value;
  reg = new RegExp(retCarr,"g");
  text1 = text1.replace(reg, "<br>");
  */
 
  flag = true;
  flag = flag && controlField('MESSAGGIO','mex','','_');
  if(flag){
	  viewMex("Eliminazione in corso...");
	  //myajax.post("?cmd=addCommento", buildUri(obj)+"&mex2="+text1);
      myajax.post("?cmd=addCommento", buildUri(obj));
  }
  
  myajax.echoFunction=function(aa){
    hideMex(aa,0);
    alert("Commento inviato");
    window.location.reload(false);
  }
}

function deleContactA(id,obj){
  if(confirm("Conferma eliminazione")){
     viewMex("Caricamento in corso...");
     myajax.post("?cmd=deleContactA&id="+id,"");
  }
  myajax.echoFunction=function(aa){
    hideMex(aa,0);
    alert("Eliminazione effettuata");
    window.location.reload(false);
  }
}

function deleContact(id,idDammuso){
  if(confirm("Conferma eliminazione")){
     viewMex("Caricamento in corso...");
     myajax.post("?cmd=deleContact&id="+id+"&idDammuso="+idDammuso,"");
  }
  myajax.echoFunction=function(aa){
    hideMex(aa,0);
    alert("Eliminazione effettuata");
    window.location.reload(false);
  }
}

function selOpPrezzo(opr,obj){
	if(opr=="uguale"){
		document.getElementById('op').value="2";
		//document.getElementById('finoA').style.fontWeight="normal";
		document.getElementById('finoA').style.color="#33789C";
		document.getElementById('finoAS').style.color="#fff";
		document.getElementById('ugualeAS').style.color="#550000";
	}else{
		document.getElementById('op').value="1";
		//document.getElementById('ugualeA').style.fontWeight="normal";
		document.getElementById('ugualeA').style.color="#33789C";
		document.getElementById('ugualeAS').style.color="#fff";
		document.getElementById('finoAS').style.color="#550000";
	}
	//obj.style.fontWeight="bolder";
	obj.style.color="#550000";
}

function cerca(obj){
	flag = true;
	flag = flag && controlField('PREZZO','prezzo','','n');
	if(flag){
		obj.submit();
	}
}

function modificaAutonoleggio(obj){
    for(var k in myEditor){
            //document.getElementById(k).value = myEditor[k].nicInstances[0].getContent();
    }
    document.getElementById('editor').value = myEditor.nicInstances[0].getContent();
    resetBorder(obj);
    flag = true;
    flag = flag && controlField("NOME",'nome','','_');
    if(document.getElementById('imgS').value!=""){
    	flag = flag && controlField('IMG INDEX','imgS','','img');
    }
    if(flag){
            viewMex("Salvataggio in corso");
            document.getElementById('modAutonoleggio').submit();
    }
}

function recuperaPassword(obj){
  resetBorder(obj);
  flag = true;
  flag = flag && controlField('EMAIL','blabla','','_');
  flag = flag && controlField('EMAIL','blabla','','@');
  flag = flag && controlField('CATEGORIA','catM','idCatM','h');
  if(flag){
    viewMex("Operazione in corso...");
    //myajax.post("?cmd=addCommento", buildUri(obj)+"&mex2="+text1);
    myajax.post("?cmd=eseguiRecuperaPassword", buildUri(obj));
  }
  myajax.echoFunction=function(aa){
    hideMex(aa,0);
    alert(aa);
    document.getElementById('blabla').value = "";
    document.getElementById('catM').value = "";
    document.getElementById('idCatM').value = "";
    //window.location.reload(false);
  }
}

function getAnalitycs(){
  myajax.post("?cmd=getAnalitycs", "");
  myajax.echoFunction=function(aa){
    document.getElementById('analitycsBox').innerHTML = aa;
  }
}
