function confirmAction(url,msg){
	action = confirm(msg);
	if (action==true){
		window.open(url,'_self');
		return true;
	} else {
		return false;
	}
}

function addRow(idTbl) {
	var tbl = document.getElementById(idTbl);
	var tblBody = tbl.tBodies[0];
	var tblRows = tbl.rows;
	var lastRow = tblRows[tblRows.length - 1];
	var newRow = lastRow.cloneNode(true);
	tblBody.appendChild(newRow);
	var rowAnchors = lastRow.getElementsByTagName('a');
	rowAnchors[0].style.visibility = 'hidden';
	rowAnchors[1].style.visibility = 'hidden';
	var rowAnchors = newRow.getElementsByTagName('a');
	rowAnchors[1].style.visibility = 'visible';
}

function delRow(idTbl) {
	var tbl = document.getElementById(idTbl);
	var tblBody = tbl.tBodies[0];
	var tblRows = tbl.rows;
	var lastRow = tblRows[tblRows.length - 1];
	tblBody.removeChild(lastRow);
	var lastRow = tblRows[tblRows.length - 1];
	var rowAnchors = lastRow.getElementsByTagName('a');
	rowAnchors[0].style.visibility = 'visible';
	if (tblRows.length > 1) {
		if (tblRows[tblRows.length - 2].getElementsByTagName('img').length == 0) rowAnchors[1].style.visibility = 'visible';
	}
}

function add() {
	if (document.createElement && document.getElementById && document.getElementsByTagName && files<=10) {
		// crea elementi
		var div = document.createElement("div");
		var divsx = document.createElement("div");
		var divdx = document.createElement("div");
		var testo = document.createTextNode("Foto " + files);
		var inputField = document.createElement("input");
		
		// setta attributi
		div.setAttribute("class","campoform");
		divsx.setAttribute("class","campoformsx");
		divdx.setAttribute("class","campoformdx");
		inputField.setAttribute("type","file");
		inputField.setAttribute("name","file[]");
		inputField.setAttribute("class","forminput");
		inputField.setAttribute("size","54");
		inputField.setAttribute("onchange","add();");
		
		divsx.appendChild(testo);
		divdx.appendChild(inputField);
		div.appendChild(divsx)
		div.appendChild(divdx)
		
		// appendi al relativo padre
		var id_form = document.getElementById("id_form");
		var ins_foto = document.getElementById("ins_foto");
		document.getElementById('id_form').insertBefore(div,ins_foto);
		// incrementa variabile globale
		files++;
	}
}


function centerPopup(url, width, height){
	var x = (640 - width) / 2;
	var y = (480 - height) / 2;
	if (screen){
		y = (screen.height - height) / 2;
		x = (screen.width - width) / 2;
	}
	window.open (url, 'finestra', 'left = ' + x + ', top = ' + y + ', width = ' + width + ', height = ' + height + ', screenX = ' + x + ' , screenY = ' + y + ', status = no');
	return false;
}

function resizeInner(w, h){
	var wantedDisWidth = w; 
	var wantedDisHeight = h; 
	var disWidth = window.innerWidth || document.body.clientWidth; 
	var disHeight = window.inneHeight || document.body.clientHeight; 
	resizeBy( wantedDisWidth-disWidth, wantedDisHeight-disHeight );
	moveTo((screen.width-wantedDisWidth)/2, (screen.height-wantedDisHeight)/2);
	focus();
} 

// MANAGE PHOTOS
// Va alla sezione di gestione delle foto di un film
function managePhotos(id, numPhotos){
	path = 'movies_press.php' + '?cmd=manage_photos&id=' + id + '&num_photos=' + numPhotos;
	path = clearPath(path);
	window.location = path;
}

// MANAGE CANNES PHOTOS
// Va alla sezione di gestione delle foto di un film di Cannes 2008
function manageCannesPhotos(id, numPhotos){
	path = 'cannes_2008_press.php' + '?cmd=manage_photos&id=' + id + '&num_photos=' + numPhotos;
	path = clearPath(path);
	window.location = path;
}

//*******************************
//* CHANGING FILTER             *
//* Modifica filtro dell'elenco *
//*******************************
function changingFilter(obj, path){
	path = path + '?cmd=changing_show&filter_name=' + obj.name + '&filter_value=' + obj.value;
	path = clearPath(path);
	window.location = path;
}

//***********************************************
//* CHANGING PAGE                               *
//* Modifica la pagina visualizzata dell'elenco *
//***********************************************
function changingPage(startingRow, path){
	path = path + '?cmd=changing_show&starting_row=' + startingRow;
	path = clearPath(path);
	window.location = path;
}

//*********************************************************
//* CHANGING MAXROWS                                      *
//* Modifica il numero di record visualizzati dell'elenco *
//*********************************************************
function changingMaxRows(maxRows, path){
	path = path + '?cmd=changing_show&max_rows=' + maxRows;
	path = clearPath(path);
	window.location = path;
}

//************************************************
//* CHANGING ORDER                               *
//* Modifica chiave e verso della clausola ORDER *
//************************************************
function changingOrder(field, key, verse, path){
	if(field == key){	// Se il valore dell'input è uguale al campo selezionato, cambia il verso
		if(verse == 'ASC'){
			verse = 'DESC';
		} else {
			verse = 'ASC';
		}
	}
	path = path + '?cmd=changing_show&order=' + field + '&verse=' + verse;
	path = clearPath(path);
	window.location = path;
}

// CLEAR PATH
// Ripulisce un path se ci sono più di un punto interrogativo
function clearPath(path) {
	path = path.split('?');
	path1 = path[0];
	path.shift();
	path2 = path.join('');
	path = path1 + '?' + path2;
	return path;
}

// *********************************
// FUNZIONI PER IL TEST DEL POSITION
// *********************************

function startMove(event, obj){
	myTr = obj.parentNode.parentNode;
	var pointer = document.getElementById('pointer');
	param = findElementPosition(myTr);
	newY = param['curtop'];
	pointer.style.top = newY + 'px';
}

function stopMove(obj){
	myTr = obj.parentNode.parentNode;
}

function findElementPosition(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	var param = new Object ();
	param['curleft']  = curleft ;
	param['curtop']  = curtop ;
	return param;
}

function setPosition(obj, defY){
	obj.offsetTop = defY;
}

function startDrag(e,oCode,oButton,oElement) {
	if( oButton != 'left' ) { return; }
	if( !e ) { e = window.event; }
	if( e.preventDefault ) { e.preventDefault(); }
	//they have clicked down. must store position and watch to see if they drag it
	window.storeMousePos = MWJ_getMouseCoords(e);
	window.mouseIsDown = tableSortType ? oElement : oElement.parentNode;
	MWJ_monitorMouse(mouseismove);
	MWJ_monitorButton( document, 'mouseup', stopDrag );
/*	if( !window.debugElement ) {
		window.debugElement = document.createElement('textarea');
		debugElement.style.position = 'absolute';
		debugElement.style.width = '400px';
		debugElement.style.height = '200px';
		debugElement.style.border = '1px solid #F00';
		debugElement.style.left = '0px';
		debugElement.style.top = '0px';
		document.body.appendChild(debugElement);
	}
*/
	// Crea il div tratteggiato per evidenziare il tr che si sta draggando
	if( !window.dashedElement ) {
		window.dashedElement = document.createElement('div');
		dashedElement.style.position = 'absolute';
		dashedElement.style.width = (mouseIsDown.offsetWidth - 2) + 'px';
		dashedElement.style.height = (mouseIsDown.offsetHeight - 2) + 'px';
		dashedElement.style.border = '1px dashed #F00';
		document.body.appendChild(dashedElement);
	}
	var cellXY = MWJ_getPosition( oElement );
	dashedElement.style.left = cellXY[0] + 'px';
	dashedElement.style.top = cellXY[1] + 'px';
}

function mouseismove() {
	if( !window.mouseIsDown ) { return; }
	//they have started dragging
	//moving table cells causes many problems so create a false element for them to move
	if( !window.falseElement ) {
		window.falseElement = document.createElement('div');
		falseElement.style.position = 'absolute';
		falseElement.style.width = mouseIsDown.offsetWidth + 'px';
		falseElement.style.height = mouseIsDown.offsetHeight + 'px';
		falseElement.style.border = '1px solid #00f';
		falseElement.onselectstart = function () { return false; };
		document.body.appendChild(falseElement);
	}
	//move the false element to the calculated position
	var cellXY = MWJ_getPosition( mouseIsDown );
	falseElement.style.left = tableSortType ? ( ((MWJ_getMouse[0]-storeMousePos[0])+cellXY[0])+'px' ) : ( cellXY[0]+'px' );
	falseElement.style.top = tableSortType ? ( cellXY[1]+'px' ) : ( ((MWJ_getMouse[1]-storeMousePos[1])+cellXY[1])+'px' );
	// Crea il div che mostra il punto tra i due tr dove si posizionerà quello draggato
	if( !window.posElement ) {
		window.posElement = document.createElement('div');
		posElement.style.position = 'absolute';
		posElement.style.width = mouseIsDown.offsetWidth + 'px';
		posElement.style.height = '2px';
		posElement.style.fontSize = '0';
		posElement.style.backgroundColor = '#F00';
		posElement.onselectstart = function () { return pos; };
		document.body.appendChild(posElement);
	}
	//move the pos element to the calculated position
	var cellXY = MWJ_getPosition( mouseIsDown );
	posElement.style.left = tableSortType ? ( ((MWJ_getMouse[0]-storeMousePos[0])+cellXY[0])+'px' ) : ( cellXY[0]+'px' );

	var currentPosition = tableSortType ? ( (MWJ_getMouse[0]-storeMousePos[0])+MWJ_getPosition( mouseIsDown )[0] ) : ( (MWJ_getMouse[1]-storeMousePos[1])+MWJ_getPosition( mouseIsDown )[1] );
	//find out the index of the cell they dragged
	for( var dragCellPos = 0, x = mouseIsDown.parentNode.getElementsByTagName(tableSortType?'td':'tr'); x[dragCellPos] != mouseIsDown; dragCellPos++ ) { }
	var posLast = true;	// Se sì, posta il puntatore alla fine dei record
	//find out the index of the cell it must be put before (I could just swap directly, but this way, I can swap them for all rows)
	for( var i = 1, x = mouseIsDown.parentNode.getElementsByTagName(tableSortType?'td':'tr'), beforeThis = x.length; x[i]; i++ ) {
		var cellPosition = MWJ_getPosition( x[i] )[tableSortType?0:1];
		if( x[i] != mouseIsDown && currentPosition < cellPosition && !( beforeThis - x.length ) ) {
			//we are not dealing with the actual cell that is being dragged and the dragged cell must be inserted before this one
			posElement.style.top = tableSortType ? ( cellXY[1]+'px' ) : (cellPosition - 1) + 'px';
			posLast = false;
			break;
		}
	}
	if(posLast){
		lastCell = mouseIsDown.parentNode.getElementsByTagName('tr')[i-1];
		cellHeight = lastCell.offsetHeight;
		//window.debugElement.innerHTML = window.debugElement.innerHTML + "\n\n" + cellPosition;
		posElement.style.top = tableSortType ? ( cellXY[1]+'px' ) : (cellPosition - 1 + cellHeight) + 'px';
	}
}

function stopDrag() {
	if( window.falseElement ) {
		//stop monitoring the mouse
		document.onmousemove = null;
		document.body.removeChild(dashedElement);
		document.body.removeChild(posElement);
		document.body.removeChild(falseElement);
		window.falseElement = false;
		window.posElement = false;
		window.dashedElement = false;
		//work out what orders they should be in, based on the position of the left edge of the cell, or top position of the row
		var currentPosition = tableSortType ? ( (MWJ_getMouse[0]-storeMousePos[0])+MWJ_getPosition( mouseIsDown )[0] ) : ( (MWJ_getMouse[1]-storeMousePos[1])+MWJ_getPosition( mouseIsDown )[1] );
		//find out the index of the cell they dragged
		for( var dragCellPos = 0, x = mouseIsDown.parentNode.getElementsByTagName(tableSortType?'td':'tr'); x[dragCellPos] != mouseIsDown; dragCellPos++ ) { }
		//find out the index of the cell it must be put before (I could just swap directly, but this way, I can swap them for all rows)
		for( var i = 1, x = mouseIsDown.parentNode.getElementsByTagName(tableSortType?'td':'tr'), beforeThis = x.length; x[i]; i++ ) {
			var cellPosition = MWJ_getPosition( x[i] )[tableSortType?0:1];
			if( x[i] != mouseIsDown && currentPosition < cellPosition && !( beforeThis - x.length ) ) {
				//we are not dealing with the actual cell that is being dragged and the dragged cell must be inserted before this one
				beforeThis = i;
			}
		}
		//we now have the new ordering that is compulsory - stored as the index of the cell it must be inserted before

		if( beforeThis != dragCellPos + 1 ) {
			//a move is compulsory
			//now decide if we should re-arrange the whole table or just that row - this is up to the person who uses this script

			switch( tableSortType ) {

				case 0:
					//re-arrange rows in the body
					var trs = mouseIsDown.parentNode.getElementsByTagName('tr');
					if( trs[beforeThis] ) {
						mouseIsDown.parentNode.insertBefore(mouseIsDown,trs[beforeThis]);
					} else {
						mouseIsDown.parentNode.appendChild(mouseIsDown);
					}
					// Crea l'array per il passaggio degli id dei record all'ajax
					var arrId = new Array();
					// Cicla i tr della tabella
					for( var i = 1, x = theTable.getElementsByTagName('tr'); x[i]; i++ ) {
						var y = x[i].getElementsByTagName('td');
						// Aggiunge nell'array degli id quello del record corrente (contenuto in un input nascosto)
						arrId.push(y[0].getElementsByTagName('input')[0].value);
					}
					// Richiama la funzione ajax per il salvataggio delle nuove posizioni
					changePosition(table, field, startingRow, numRows, arrId);

					break;

				case 1:
					//re-arrange cells in all rows
					for( var i = 0, y = mouseIsDown.parentNode.parentNode.getElementsByTagName('tr'), oE; oE = y[i]; i++ ) {
						//for each row
						var tds = oE.getElementsByTagName('td');
						if( tds[beforeThis] ) {
							oE.insertBefore(tds[dragCellPos],tds[beforeThis]);
						} else {
							oE.appendChild(tds[dragCellPos]);
						}
					}
					break;

				default:
					//re-arrange cells in just this row
					var tds = mouseIsDown.parentNode.getElementsByTagName('td');
					if( tds[beforeThis] ) {
						mouseIsDown.parentNode.insertBefore(mouseIsDown,tds[beforeThis]);
					} else {
						mouseIsDown.parentNode.appendChild(mouseIsDown);
					}

			}

		}

	}
	window.mouseIsDown = false;
}

var win = null;
function NewWindow(mypage,myname,w,h,scroll){
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable';
	win = window.open(mypage,myname,settings);
	if(win.window.focus) win.window.focus();
}

