//recupera elemeto
function getElement(id_elemento) 
{
    var elemento;
 
   //controllo browser
   if(document.getElementById)
      elemento = document.getElementById(id_elemento);
   else
      elemento = document.all[id_elemento];
      
   return elemento;
}

// JavaScript Document
function setValue(key, val)
{
	$.post("ajax.php?ajax=setValue", { key : key, val : val }, 
												function(data)
												{
													getValue(key);
												}
   			, "json");
}

function getValue(key)
{
	$.post("ajax.php?ajax=getValue", { key : key }, 
												function(data)
												{
													getElement(key).value=data.value;
												}
   			, "json");
}

function delValue(key)
{
	$.post("ajax.php?ajax=delValue", { key : key }, 
												function(data)
												{
												
												}
   			, "json");
}

function delFromDB(table,id,session)
{
	$.post("ajax.php?ajax=delFromDB", { table : table, id : id }, 
												function(data)
												{
													if(data.resp==1)
													{
														$("#"+table+"_"+id).remove();
														var ntot=parseInt(getElement(session).value);
														ntot-=1;
														delValue(session);
														setValue(session,ntot);
														getElement(session).value=ntot;
													}
												}
   			, "json");
}

function AJAXPrint(id,element,fun)
{
	var str;
	var p='';
	if(element=='comune')
		str='<select onchange="AJAXPrint(this.value,\'loc\',\'getLocalita\');setValue(\'059\',this.value);">';
	if(element=='comunet')
		str='<select onchange="AJAXPrint(this.value,\'loct\',\'getLocalita\');setValue(\'059\',this.value);">';
	if(element=='com')
	{
		str='<select onchange="AJAXPrint(this.value,\'rloc\',\'getLocalitaR\')" name="comune">';
		p='dev/';
	}
	if(element=='rloc')
	{
		str='<select name="localita">';
		p='dev/';
	}
	if(element=='loc')
		str='<select onchange="if(this.value==0)showElement(\'nloc\');else hideElement(\'nloc\'); setValue(\'060\',this.value);">';
	if(element=='loct')
		str='<select onchange="if(this.value==0)showElement(\'nloct\');else hideElement(\'nloct\'); setValue(\'060\',this.value);">';		
	$.post(p+"ajax.php?ajax="+fun, { id : id }, 
												function(data)
												{
													str+=data.html
													$("#"+element).html(str);
													if(element=='loc' && data.n==0)
														showElement('nloc');
													/*var ntot=parseInt(getElement(session).value);
													ntot-=1;
													delValue(session);
													setValue(session,ntot);
													getElement(session).value=ntot;*/
												}
   			, "json");
}

function delArticolo(id)
{
	$.post("dev/ajax.php?ajax=delArticolo", { id : id }, 
												function(data)
												{
													if(data.resp==1)
													{
														$("#"+id).slideUp("normal");
														$("#"+id).html('ok');
														$("#"+id).slideDown("normal");
													}
												}
   			, "json");	
}

function editShowHome(id)
{
	img=new Array("icone/edit_add.png","icone/edit_remove.png");
	$.post("dev/ajax.php?ajax=editShowHome", { id : id }, 
												function(data)
												{
													if(data.resp==1)
													{
														getElement('img_'+id).src=img[data.bit];
													}
												}
   			, "json");	
}

function chooseNews(n)
{
    $.ajax({
        type: 		"post",
		url: 		"dev/ajax.php?ajax=chooseNews",
		data:		"n="+n,
		dataType:	"json",
		success:	function(g){if(g.resp==1) jQuery.each(g.rows,function(i,r){addRow(r);});},
        error:      function(){alert('unable to get Attributes');}
		});
}
/*
function chooseNews(n)
{
	$.post("dev/ajax.php?ajax=chooseNews", { n:n }, 
												function(data)
												{
													alert(data.resp);
													if(data.resp==1)
													{
														var html_pag='';
														for(var i=1; i<=data.pags; i++)
														{
															if(i==n)
																html_pag+=i+' | ';
															else
																html_pag+=' <a href="#" onclick="chooseNews(\''+i+'\');">'+i+'</a> | ';
														}
														alert(data.html);
														$("#c_news").html(data.html);
														$("#pag").html(html_pag);
													}
												}
   			, "json");
}
*/
function deleteNews(id)
{
	$.post("dev/ajax.php?ajax=deleteNews", { id : id }, 
												function(data)
												{
													if(data.resp==1)
													{
														$('#news_'+id).slideUp("normal");
														$('#resp').html('eliminazione news completata');
													}
												}
   			, "json");	
}

function editArchiviaNews(id)
{
	img=new Array("icone/edit_remove.png","icone/edit_add.png");
	$.post("dev/ajax.php?ajax=editArchiviaNews", {id : id }, 
												function(data)
												{
													if(data.resp==1)
													{
														getElement('imgn_'+id).src=img[data.bit];
														$('#resp').html('news archiviata correttamente');
													}
												}
   			, "json");	
}

function editArchiviaArticolo(id)
{
	$.post("dev/ajax.php?ajax=editArchiviaArticolo", {id : id }, 
												function(data)
												{
													if(data.resp==1)
													{
														$('#'+id).slideUp("normal");
														$('#'+id).html('articolo archiviato correttamente');
														$('#'+id).slideDown("normal");	
													}
												}
   			, "json");	
}

function loadNewsData(id)
{
	$.post("dev/ajax.php?ajax=loadNewsData", {id : id }, 
												function(data)
												{
													alert("e fin qua1...");
													if(data.resp==1)
													{
														alert("e fin qua2...");
														hideElement('c_news');
														hideElement('entry');
														showElement('e_news');
														getElement('n_title').value=data.title;
														getElement('n_body').value=data.text;
														getElement('n_id').value=id;
													}
												}
   			, "json");	
}

function updateNews(id,title,text)
{
	$.post("dev/ajax.php?ajax=updateNews", {id : id, title:title, text:text }, 
												function(data)
												{
													if(data.resp==1)
													{
														$('#resp').html('news aggiornata correttamente');
														/*hideElement('e_news');
														chooseNews(1);
														showElement('c_news');
														showElement('entry');*/
														top.location="g_news.php"
													}
												}
   			, "json");
}

function insertNews(title,text)
{
	$.post("dev/ajax.php?ajax=insertNews", {title:title, text:text }, 
												function(data)
												{
													if(data.resp==1)
													{
														$('#resp').html('news inserita correttamente');
														hideElement('i_news');
														chooseNews(1);
														showElement('c_news');
														showElement('entry');
													}
												}
   			, "json");
}

function choosePartner(n)
{
	$.post("dev/ajax.php?ajax=choosePartner", { n:n }, 
												function(data)
												{
													if(data.resp==1)
													{
														var html_pag='';
														for(var i=1; i<=data.pags; i++)
														{
															if(i==n)
																html_pag+=i+' | ';
															else
																html_pag+=' <a href="#" onclick="choosePartner(\''+i+'\');">'+i+'</a> | ';
														}
														$("#c_partner").html(data.html);
														$("#pag").html(html_pag);
													}
												}
   			, "json");
}

function deletePartner(id)
{
	$.post("dev/ajax.php?ajax=deletePartner", { id : id }, 
												function(data)
												{
													if(data.resp==1)
													{
														$('#par_'+id).slideUp("normal");
														$('#resp').html('eliminazione partner completata');
													}
												}
   			, "json");	
}

function loadPartnerData(id)
{
	$.post("dev/ajax.php?ajax=loadPartnerData", { id : id }, 
												function(data)
												{
													if(data.resp==1)
													{
														hideElement('entry');
														hideElement('c_partner');
														getElement('p_name').value=data.nome;
														getElement('p_indirizzo').value=data.indirizzo;
														getElement('p_tel').value=data.tel;
														getElement('p_citta').value=data.citta;
														getElement('p_email').value=data.email;
														getElement('p_sito').value=data.sito;
														getElement('p_id').value=id;
														getElement('p_img').src='dev/gallery/partners/'+data.img;
														showElement('e_partner');
													}
												}
   			, "json");	
}

function fillContactForm(email,path)
{
	$.post(path+"ajax.php?ajax=loadRubrica", { email : email }, 
												function(data)
												{
													if(data.resp==1)
													{
														getElement('nome').value=data.nome;
														getElement('cognome').value=data.cognome;
														getElement('tel').value=data.tel;
													}
												}
   			, "json");	
}

function insertContact(email,nome,cognome,tel,note,articolo,path)
{
	var regexp=/^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	if(!regexp.test(email))
		alert('indirizzo email non corretto');
	else if(nome=='' || cognome=='')
		alert('nome e/o cognome vuoti');
	else if(tel=='')
		alert('numero di telefono non valido');
	else
	{
		$('#resp').html('attendere....');
		showElement('resp');
		$.post(path+"ajax.php?ajax=insertContact", { email : email, nome:nome, cognome:cognome, tel:tel, note:note, articolo:articolo }, 
												function(data)
												{
													if(data.resp==1)
													{
														hideElement('contact');
														hideElement('resp');
														$('#resp').html('Richiesta di informazioni inviata');
														showElement('resp');
													}
												}
   			, "json");	
	}
}

function chooseNewsSmall(n)
{
	$.post("dev/ajax.php?ajax=chooseNewsSmall", { n:n }, 
												function(data)
												{
													if(data.resp==1)
													{
														var html_pag='';
														for(var i=1; i<=data.pags; i++)
														{
															if(i==n)
																html_pag+=i+' | ';
															else
																html_pag+=' <a href="#" onclick="chooseNewsSmall(\''+i+'\');">'+i+'</a> | ';
														}
														$("#news_menu").html(data.html);
														$("#pag").html(html_pag);
													}
												}
   			, "json");
}

function getCategoria(f,s)
{
	var html='Sottocategoria <select onchange="setValue(\'066\',this.value);">';
	$.post("ajax.php?ajax=getCategoria", { f : f , s:s}, 
												function(data)
												{
													if(data.resp==1)
													{
														html+=data.html;
														$("#row066").html(html);
													}
												}
   			, "json");		
}


function chooseContatto(n)
{
	$.post("dev/ajax.php?ajax=chooseContatto", { n:n }, 
												function(d)
												{
													
													var html_pag='';
													for(var i=1; i<=d.pags; i++)
													{
														if(i==n)
															html_pag+=i+' | ';
														else
															html_pag+=' <a href="#" onclick="chooseContatto(\''+i+'\');">'+i+'</a> | ';
													}
													$("#menu_segr > div").html(d.html);
													$("#pag_segr").html(html_pag);
												}
   			, "json");
}