var checkOK;
var downList;
var pe1,pe2;
var folderOK="/chk_image.php?folder="+baseFolder+"&action=copied";
var folderRetrieve="/chk_image.php?folder="+baseFolder+"&action=retrieve";
var thumbCheck="/chk_image.php?folder="+baseFolder+"&action=thumbcheck&file=";
var photoList= new Array();
var updImage;
var contaImg=0;
var tentative=0;
var xmlHttp1,xmlHttp2,xmlHttp3,xmlHttp4;

function update_album()
{
	_id=$("in_id").value;
	_title=$("in_title").value;
	_contentdate=$("in_contentdate").value;
	_contentplace=$("in_contentplace").value;
	_contentdesc=$("in_contentdesc").value;
	_category=$("in_category").value;
	
	if (_contentdate!="") isvalid=isDateCheck(_contentdate); else isvalid=true;
	if ((isvalid)&&(trim(_title)!=""))
	{
		xmlHttp4 = new Ajax.Request('/upd_album.php',
								{
									method: 'post',
									asynchronous: false,
									parameters : 'title='+_title+'&contentdate='+_contentdate+'&contentplace='+_contentplace+'&contentdesc='+_contentdesc+'&category='+_category+'&id='+_id+'&action=upd',
									onComplete: function(resp)
									{
										// alert("L'album "+_title+" e' stato aggiornato con successo");
									}
								});
	};
	// else alert("Please, check date and title fields. They are mandatory!");
}

function create_album()
{
	_id=$("in_id").value;
	_title=$("in_title").value;
	_contentdate=$("in_contentdate").value;
	_contentplace=$("in_contentplace").value;
	_contentdesc=$("in_contentdesc").value;
	_category=$("in_category").value;
	
	// isvalid=isDateCheck(_contentdate);
	isvalid=true;
	
	if ((isvalid)&&(trim(_title)!=""))
	{
		xmlHttp4 = new Ajax.Request('/upd_album.php',
								{
									method: 'post',
									parameters : 'title='+_title+'&contentdate='+_contentdate+'&contentplace='+_contentplace+'&contentdesc='+_contentdesc+'&category='+_category+'&action=crt&uid='+_userid,
									onComplete: function(resp)
									{
										// alert(resp.responseText);
										res=xmlHttp4.transport.responseXML;
										var a=res.documentElement;
										if ((a!=null)&&(a.hasChildNodes()))
										{
											$("in_id").value=a.firstChild.nodeValue;
											// alert("Album "+_title+" has been created successfully");
											$("idpage").value=10;
											$("run_edit").submit();
										}
										else
										{
											// alert("Error creating album");
										}
										
									}
								});
	};
	// else alert("Please, check date and title fields. They are mandatory!");					
}

function delete_album()
{
	_id=$("in_id").value;
	_title=$("in_title").value;
	_rc=$("in_rc").value;
	xmlHttp4 = new Ajax.Request('/upd_album.php',
								{
									method: 'post',
									parameters : 'id='+_id+'&action=del',
									onComplete: function(resp)
									{
										// alert("L'album e i suoi contenuti"+_title+" sono stati cancellati");
										// alert($(_rc).remove());
										// $(_rc).childElements()[0].remove()
										window.location.href='/index.php?idpage=7' // &action=edit&sa=edit'; => non rimango in edit
									}
								});
}

function album_retrieve(id)
{
	var m = new Array("title","contentdate","contentplace","contentdesc","category");
	// id=1468;
	$("in_id").value=id;
	$("in_rc").value='a'+id;
	xmlHttp4 = new Ajax.Request('/upd_album.php',
									{
										method: 'post',
										parameters: 'action=rtv&id='+id,
										onComplete: function(resp)
										{
											var x;
											// controllo lo stato dei diversi campi che vengono restituiti
											// se non sono null allora posso impostare il corrispondente campo di input
											// da controllare: si suppone che ogni nodo che ha almeno un contenuto ha un figlio con il contenuto!!
											// alert(resp.responseText);
											// alert(xmlHttp4.transport.responseText);
											res=xmlHttp4.transport.responseXML;
											var a = res.documentElement;												// root element (dati)
											for (x=0;x<5;x++)
											{
												l=a.getElementsByTagName(m[x]);
												if ((l!=null)&&(l[0].hasChildNodes()))
												{
													v=l[0].firstChild.nodeValue;
													$("in_"+m[x]).value=v;
												}
												else
												{
													// Elemento vuoto e quindi input=""
													$("in_"+m[x]).value="";
												}
											};
											var tmp=document.getElementById("updatealbum");
											if (tmp) tmp.style.display="inline";
											
											var tmp=document.getElementById("deletealbum");
											if (tmp) tmp.style.display="inline";
											
											var tmp=document.getElementById("managephoto");
											if (tmp) tmp.style.display="inline";
											
											var tmp=document.getElementById("addphoto");
											if (tmp) tmp.style.display="inline";
										}
									});
}

function manage_photo(idp,idalbum)
{
	album_retrieve(idalbum);
	var f=document.getElementById("run_edit");
	$("idpage").value=idp;
	f.submit();
}

function done_edit()
{
	update_album();
	update_foto();
	window.location.href='/index.php?idpage=8&action=edit&sa=edit';
}
