function aggiungiRivenditore(a) {
	obj=$(".myframe").eq(a).contents().find('#pd3');
	pol=$(obj).find('.moveElement');
	n=$(pol).length;
	
	if(n>0) {
		p=$(obj).find('.moveElement:last').attr("id").split('_');
		n=Math.abs(p[1]);
		n++;
	}
	
	str="<div id='melem_" + n + "' class='moveElement' onclick='makeEdit($(this));' ondblclick='makeDrag($(this));'></div>";
	$(obj).append(str);

	an=a + "_" + n;
	
	str= "<tr id='tMelem_" + an + "' class='tcMelem_" + an + "'>";
	str+="	<td><span>" + n + "</span><input type='checkbox'></td>";
	str+="	<td><input type='text' name='nome'><input type='hidden' name='coordinate' value=''></td>";
	str+="	<td><select></select></td>";
	str+="	<td><a href='' onClick='mostraMelem($(this)); return false;'>Vedi</a>";
	str+="</tr>";
	str+="<tr class='tcMelem_" + an + " melemHide'>";
	str+="	<td colspan='4'>Indirizzo:<br /><textarea></textarea></td>";
	str+="</tr>";
	str+="<tr class='tcMelem_" + an + " melemHide'>";
	str+="	<td colspan='4'>Descrizione:<br /><textarea></textarea></td>";
	str+="</tr>";
	str+="<tr class='tcMelem_" + an + " melemLast melemHide'>";
	str+="	<td colspan='4'><a href='' onclick='insertRivenditore($(this)); return false;'>Salva</a> | <a href='' onClick='mostraMelemChiudi($(this)); return false;'>Chiudi</a><br /><br /></td>";
	str+="</tr>";
	str+="<tr class='tcMelem_" + an + " melemHide'>";
	str+="	<td colspan='4'><div style='display: block;' height: 10px;>&nbsp;<div></td>";
	str+="</tr>";
	
	
	$(".elencoRivenditori").eq(a).append(str);
	
	$.get("salvaMappa.php", { "doit" : "stati" },
  	function(data){
    	$("#tMelem_" + an).find('td').eq(2).find('select').html(data);
  	}
  );
}

function insertRivenditore(a) {
	terzo_tr=$(a).parent().eq(0).parent().eq(0).prev();
	//terzo_tr=$(quarto_tr).prev()
	secondo_tr=$(terzo_tr).prev()
	primo_tr=$(secondo_tr).prev();
	
	o=$(primo_tr).find('td').eq(0).find('span').html();
	coordinate=$(primo_tr).find('td').eq(1).find('input').eq(1).val();
	descrizione=$(terzo_tr).find('td').eq(0).find("textarea").val();
	indirizzo=$(secondo_tr).find('td').eq(0).find("textarea").val();
	nome=$(primo_tr).find('td').eq(1).find('input').eq(0).val();
	//stato=$(terzo_tr).find('td').find('select').eq(0).val();
	stato=$(primo_tr).find('td').eq(2).find('select').eq(0).val();
	
	//alert(o + " -> " + coordinate);
	
	tid=$(primo_tr).attr("id").split('_');
	sezione=tid[1];
	oggetto=tid[2];
	
	$.get("salvaMappa.php", { "sezione": sezione, "doit": "aggiungi", "descrizione": descrizione , "coordinate": coordinate, "nome" : nome, "indirizzo" : indirizzo, "stato": stato },
  	function(data){
    	dati=data.split('|');
    	if(dati[0]=='y') {
    		/*
				alert("OK: \nID: " + dati[1]);
				*/
				$("#helper").html("Rivenditore (" + dati[1] + ") salvato con successo");
				$("#helper").dialog({
					bgiframe: true,
					modal: true,
					resizable: false,
					title: "Inserimento Eseguito",
					buttons: {
						Ok: function() {
							$(this).dialog("close");
							$(this).dialog("destroy");
						}
					}
				});
				
				$(a).attr("id","tMelem_" + sezione + "_" + dati[1]);
				$(".tcMelem_" + sezione + "_" + oggetto).removeClass(".tcMelem_" + sezione + "_" + oggetto).addClass(".tcMelem_" + sezione + "_" + dati[1]);
				$(a).attr("onclick","salvaRivenditore($(this))");
				$(a).find('td').eq(0).find('span').html($(a).attr("id"));
				
				mapinfo=$(".myframe").eq(sezione).contents().find('#pd3');
				$(mapinfo).find("#melem_" + oggetto).attr("id","melem_" + dati[1]);
				
				mostraMelmTR($(primo_tr));
			}
			else {
				//alert("NO:\n\nERRORE:\n" + dati[1]);
				$("#helper").html("Errore durante il salvataggio:<br /><br />" + dati[1]);
				$("#helper").dialog({
					bgiframe: true,
					modal: true,
					resizable: false,
					title: "Inserimento NON Eseguito",
					buttons: {
						Ok: function() {
							$(this).dialog("close");
							$(this).dialog("destroy");
						}
					}
				});
			}
  	}
	);
}

function salvaRivenditore(a) {
	terzo_tr=$(a).parent().eq(0).parent().eq(0).prev();
	//terzo_tr=$(quarto_tr).prev()
	secondo_tr=$(terzo_tr).prev()
	primo_tr=$(secondo_tr).prev();
	
	o=$(primo_tr).find('td').eq(0).find('span').html();
	coordinate=$(primo_tr).find('td').eq(1).find('input').eq(1).val();
	descrizione=$(terzo_tr).find('td').eq(0).find("textarea").val();
	indirizzo=$(secondo_tr).find('td').eq(0).find("textarea").val();
	nome=$(primo_tr).find('td').eq(1).find('input').eq(0).val();
	//stato=$(terzo_tr).find('td').find('select').eq(0).val();
	stato=$(primo_tr).find('td').eq(2).find('select').eq(0).val();
	
	//alert(o + " -> " + coordinate);
	
	tid=$(primo_tr).attr("id").split('_');
	sezione=tid[1];
	oggetto=tid[2];
	
	$.get("salvaMappa.php", { "sezione": sezione, "doit": "salva", "descrizione": descrizione , "coordinate": coordinate, "nome" : nome, "indirizzo" : indirizzo, "stato": stato, "uid" : oggetto },
  	function(data){
    	dati=data.split('|');
    	if(dati[0]=='y') {
				//alert("OK: \nID: " + dati[1]);
				$("#helper").html("Rivenditore (" + oggetto + ") salvato con successo");
				$("#helper").dialog({
					bgiframe: true,
					modal: true,
					resizable: false,
					title: "Aggiornamento Eseguito",
					buttons: {
						Ok: function() {
							$(this).dialog("close");
							$(this).dialog("destroy");
						}
					}
				});
				mostraMelmTR($(primo_tr));
			}
			else {
				//alert("NO:\n\nERRORE:\n" + dati[1]);
				$("#helper").html("Errore durante il salvataggio:<br /><br />" + dati[1]);
				$("#helper").dialog({
					bgiframe: true,
					modal: true,
					resizable: false,
					title: "Aggiornamento NON Eseguito",
					buttons: {
						Ok: function() {
							$(this).dialog("close");
							$(this).dialog("destroy");
						}
					}
				});
			}
  	}
	);
}

function mostraMelem(b) {
	a=$(b).parent().eq(0).parent().eq(0); // partiamo dall' A fino al TR
	po=$(a).attr('id').split('_');
	
	obj=$(".tcMelem_" + po[1] + "_" + po[2]);
	mapinfo=$(".myframe").eq(po[1]).contents().find('#pd3');
	
	if($(a).next().is(':visible')) {
		$(obj).hide();
		$(obj).find('input').attr("disabled","disabled");
		$(obj).find('input:checkbox').removeAttr("disabled");
		$(obj).find('select').attr("disabled","disabled");
		$(obj).find('textarea').attr("disabled","disabled");
		$(a).show();
		
		$(mapinfo).find("#melem_" + po[2]).removeClass("evidenzaEdit");
	}
	else {
		$(obj).show();
		$(obj).find('td').css("border-top","#000000 solid 1px");
		$(obj).find('td:first').css("border-left","#000000 solid 1px");
		$(obj).find('td:last').css("border-right","#000000 solid 1px");
		$(obj).find('input').removeAttr("disabled");
		$(obj).find('select').removeAttr("disabled");
		$(obj).find('textarea').removeAttr("disabled");

		$(".tcMelem_" + po[1] + "_" + po[2] + ":last").find('td').css("border","none");
		$(".tcMelem_" + po[1] + "_" + po[2] + ":last").prev().find('td').css("border","none").css("border-top","#000000 solid 1px");
		
		// coloro l'elemento chje sto modificando
		$(mapinfo).find("#melem_" + po[2]).addClass("evidenzaEdit");
	}
}

function eliminaRivenditori(a) {
	var eArray = new Array();
	eSelez=$(".elencoRivenditori").eq(a).find('input:checked');
	for(var c=0;c<$(eSelez).length;c++) {
		l=$(eSelez).eq(c).parent().eq(0).parent().eq(0).attr('id');
		u=l.split('_');
		eArray[c]=u[2];		
		sezione=u[1];
	}

	alert(eArray.length + " -> " + eArray.join('_'));
	uids=eArray.join('_');
	

	$.get("salvaMappa.php", { "sezione": sezione, "doit": "elimina", "uid" : uids },
  	function(data){
    	dati=data.split('|');
    	if(dati[0]=='y') {			
				$("#helper").html("Rivenditori eliminati con successo:\n" + dati[1]);
				$("#helper").dialog({
					bgiframe: true,
					modal: true,
					resizable: false,
					title: "Eliminazione Eseguita",
					buttons: {
						Ok: function() {
							$(this).dialog("close");
							$(this).dialog("destroy");
						}
					}
				});
				
				for(c=0;c<eArray.length;c++) {				
					mid=$(".tcMelem_" + sezione + "_" + eArray[c]);
					$(mid).hide();
					mapinfo=$(".myframe").eq(a).contents().find('#pd3');
					$(mapinfo).find("#melem_" + eArray[c]).hide();
				}
				
			}
			else {
				$("#helper").html("Errore durante l'eliminazione:<br /><br />" + dati[1]);
				$("#helper").dialog({
					bgiframe: true,
					modal: true,
					resizable: false,
					title: "Eliminazione NON Eseguita",
					buttons: {
						Ok: function() {
							$(this).dialog("close");
							$(this).dialog("destroy");
						}
					}
				});
			}
  	}
	);
	
	
}

function mostraMelemChiudi(a) {
	b=$(a).parent().eq(0).parent().eq(0).prev().prev().prev().find("td").eq(3).find('a').eq(0);
	mostraMelem($(b));
}

function mostraMelmTR(a) {
	b=$(a).find("td").eq(3).find('a').eq(0);
	mostraMelem($(b));	
}
