// JavaScript Document

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function ChangeCategoria() {
  	document.form.parent.value = 0;
	document.form.editor.value = editor.getHTML();
	document.form.submit();
}

function ExecPreview() {
	var winl = (screen.width - 640) / 2;
	var wint = (screen.height - 480) / 2;
	win = window.open('preview.php', 'Preview', 'height=480,width=640,top='+wint+',left='+winl+',scrollbars=yes')
    win.window.focus(); 
}

function ExecUpload() {
	var winl = (screen.width - 500) / 2;
	var wint = (screen.height - 250) / 2;
	win = window.open('upload.php', 'Upload', 'height=250,width=500,top='+wint+',left='+winl+',scrollbars=yes,status=yes')
    win.window.focus(); 
}

function ExecFunction(task) {
	if (task == 'save_page') {
		document.form.editor.value = editor.getHTML();
	}
    else if (task == 'save_message') { 
		document.form.imagesprop.value = createListProps('imagespage');
		document.form.editor.value = editor.getHTML();
	}
	else  if (( (task == "pdf") || (task == "html") || (task == "edit")) && (document.form.radiochecked.value == 0)) {
		alert("Attenzione: occorre selezionare una riga per procedere all'operazione");
		exit();
	}
  	document.form.task.value = task;
   	document.form.submit();
}

function SetPathImage(path) {
	document.PreviewImage.src = path+getSelectedValue('form','listimage');
	document.form.sourceimg.value = getSelectedValue('form','listimage');
}

function SetPathImageSelected(path) {
	var ImageName = getSelectedValue('form','imagespage').split('|');
	document.SelectedImage.src = path+ImageName[0];
}

function InsertSelectedFile(srcListName, tgtListName) {
	var srcList = eval('document.form.' + srcListName);
	var tgtList = eval('document.form.' + tgtListName);

	var srcLen = srcList.length;
	var tgtLen = tgtList.length;
	var tgt = "x";

	//build array of target items
	for (var i=tgtLen-1; i > -1; i--) {
		tgt += "," + tgtList.options[i].value + ","
	}

	//Pull selected resources and add them to list
	for (var i=srcLen-1; i > -1; i--) {
		if (srcList.options[i].selected && tgt.indexOf( "," + srcList.options[i].value + "," ) == -1) {
			opt = new Option( srcList.options[i].text, srcList.options[i].value );
			tgtList.options[tgtList.length] = opt;
		}
	}
}

function moveInList(srcListName, index, to) {
	var srcList = eval('document.form.' + srcListName);
	var total = srcList.options.length-1;

	if (index == -1) {
		return false;
	}
	if ( (to == 1) && (index == total) ) {
		return false;
	}
	if ( (to == -1) && (index == 0) ) {
		return false;
	}

	var items = new Array;
	var values = new Array;

	for (i=total; i >= 0; i--) {
		items[i] = srcList.options[i].text;
		values[i] = srcList.options[i].value;
	}
	for (i = total; i >= 0; i--) {
		if (index == i) {
			srcList.options[i + to] = new Option(items[i],values[i], 0, 1);
			srcList.options[i] = new Option(items[i+to], values[i+to]);
			i--;
		} else {
			srcList.options[i] = new Option(items[i], values[i]);
	   }
	}
	srcList.focus();
}

function delSelectedFromList(srcListName) {
	var srcList = eval('document.form.' + srcListName);

	var srcLen = srcList.length;

	for (var i=srcLen-1; i > -1; i--) {
		if (srcList.options[i].selected) {
			srcList.options[i] = null;
		}
	}
}

function getSelectedValue( frmName, srcListName ) {
	var form = eval( 'document.' + frmName );
	var srcList = eval( 'form.' + srcListName );

	i = srcList.selectedIndex;
	if (i != null && i > -1) {
		return srcList.options[i].value;
	} 
	else  return null;
}

function setSelectedValue( frmName, srcListName, value ) {
	var form = eval( 'document.' + frmName );
	var srcList = eval( 'form.' + srcListName );

	var srcLen = srcList.length;

	for (var i=0; i < srcLen; i++) {
		srcList.options[i].selected = false;
		if (srcList.options[i].value == value) {
			srcList.options[i].selected = true;
		}
	}
}

function showDocProps() {
	var form = eval('document.form');
	value = getSelectedValue( 'form', 'docspage' );
	parts = value.split( '|' );
	form.sourcedoc.value = parts[0];
	if (parts[1]==1) form.docaccess[0].checked = true;
	else if (parts[1]==0) form.docaccess[1].checked = true;
	form.namedoc.value = parts[2];
}

function applyDocProps() {
	var form = eval('document.form');
	if (!getSelectedValue( 'form', 'docspage' )) {
		alert( "Seleziona un file dalla lista" );
		return;
	}
	if (form.docaccess[0].checked) valuedocaccess=1;
	else valuedocaccess=0;
 	value = form.sourcedoc.value + '|' + valuedocaccess;
	value += '|' + form.namedoc.value;
	chgSelectedValue( 'form', 'docspage', value );
}

function showImageProps() {
	var form = eval('document.form');
	value = getSelectedValue( 'form', 'imagespage' );
	parts = value.split( '|' );
	form.sourceimg.value = parts[0];
	setSelectedValue( 'form', 'align', parts[1] || '' );
	form.alt.value = parts[2] || '';
	form.border.value = parts[3] || '0';
}

function applyImageProps() {
	var form = eval('document.form');
	if (!getSelectedValue( 'form', 'imagespage' )) {
		alert( "Seleziona un immagine dalla lista" );
		return;
	}
	value = form.sourceimg.value + '|'
	+ getSelectedValue( 'form', 'align' ) + '|'
	+ form.alt.value + '|'
	+ parseInt( form.border.value );
	chgSelectedValue( 'form', 'imagespage', value );
}

function chgSelectedValue( frmName, srcListName, value ) {
	var form = eval( 'document.' + frmName );
	var srcList = eval( 'form.' + srcListName );

	i = srcList.selectedIndex;
	if (i != null && i > -1) {
		srcList.options[i].value = value;
		return true;
	} 
	else return false;
}

function createListProps(srcListName) {
	var srcList = eval( 'document.form.' + srcListName );
	var string = '';

	var srcLen = srcList.length;
	for (i=0; i < srcLen; i++) {
		if (i==0) {
			string = srcList.options[i].value;	
		}
		else { 
			string += ";" + srcList.options[i].value;
		}
	}
	return string;
}

function  ShowMessage(indx){
	var Message = new Array();
	Message[0] = "Il documento è accessibile solo previa autenticazione";

	alert(Message[indx]);
}

function OpenWinDoc(accesslimited, pathFile) {
	var xleft = screen.height;
	var xtop = screen.width;
	var XX = (xtop - 800)/2;
	var YY = (xleft - 600)/2;
	if (accesslimited == 'false') {
   		ShowMessage(0);
	}
	else { 
		Mywin = window.open('Documento','Documento','left='+XX+',top='+YY+',width=800,height=600,scrollbars=yes');
		Mywin.location.href = pathFile;
		Mywin.focus();
	}
}

function OpenImageGallery(pathFile, fileName, imgwidth, imgheight) {
	var xleft = screen.height;
	var xtop = screen.width;
	var XX = (xtop - imgwidth)/2;
	var YY = (xleft - imgheight)/2;
	Mywin = window.open('','Documento','left='+XX+',top='+YY+',width='+imgwidth+',height='+imgheight+',scrollbars=no');
	with (Mywin.document) {
		open();
		writeln('<html>');
		writeln('<head>');
		writeln('<title>'+fileName+'</title>');
		writeln('</head>');
		writeln('<body leftmargin="0" topmargin="0" marginheight="0" marginwidth="0">');
		writeln('<img src="'+pathFile+'" border="0">');
		writeln('</body>');
		writeln('</html>');
		close();
	}
	Mywin.focus();
}


function OrderBy(name) {
  	document.form.orderBy.value = name;
	document.form.submit();
}

function SetEnableRadioB(el) {
  	var  objPage = eval('document.form.pages');
  	var  objFunctions = eval('document.form.functions');
  	var  objGalleries = eval('document.form.galleries');
  	if (objPage != null) {
		if ((el == 'pages') && (objPage.disabled)) objPage.disabled = false; 
		else {
			objPage.disabled = true;
			objPage.value = '';
		}
	}
  	if (objFunctions != null) {
		if ((el == 'functions') && (objFunctions.disabled)) objFunctions.disabled = false; 
		else {
			objFunctions.disabled = true;
			objFunctions.value = '';
		}
	}
  	if (objGalleries != null) {
		if ((el == 'galleries') && (objGalleries.disabled)) objGalleries.disabled = false; 
		else {
			objGalleries.disabled = true;
			objGalleries.value = '';
		}
	}
}
