function executeMultiPrint(notClose){
	var count = countPrint();
	if (count==0){
	    executeMonoPrint(null);	    
	}else{
	    for (var i=0;i<count;i++){
			executeMonoPrint(i);
	    }
	}
	if (notClose!=null && notClose){
	    return;
	}
	window.close();
}
function executeMonoPrint(index){
	var fileField = "file";
	var dataField = "data";
	if (index!=null){
	   fileField = fileField + index;
	   dataField = dataField + index;
	}
	var location = evalPrintLocation(fileField);
	if (location!=null && location.length>0){
		writePrintData(fileField, dataField, location);
	}
}
function executePrint(notClose){
	executeMonoPrint(null);
	if (notClose!=null && notClose){
		return;
	}
	window.close();
}
function countPrint(){
        var countField = "fileCount";
        var count = 0;
        if (document.getElementById(countField)){
        	var str = document.getElementById(countField).value;
        	if (str.length>0){
        	   count = parseInt(str);
        	}
        } 
        return count;
}
function createPrintDialog(url){
	var popup = new PopupWindow();
	popup.setSize(100, 100);
	popup.autoHide();
	popup.setUrl(url);
	return popup;
}
function showPrintDialog(popup, anchor, url){
	if (popup==null){
		var msg = "Stampa non effettuata. Il popup di stampa non e' definito!";
		alert(msg);
		return;
	}
	if (url){
		popup.setUrl(url);
	}
	popup.showPopup(anchor);
}
function writePrintData(fileField, dataField, location){
		if (location==null || location.length==0){
			var msg = "Stampa non effettuata. Il percorso del file di stampa non e' valorizzato!";
			alert(msg);
			return;
		}
		if (!document.getElementById(dataField)) {
			var msg = "Stampa non effettuata. Il riferimento ai dati da stampare non e' valorizzato!";
			alert(msg);
			return;
		}
		var data = document.getElementById(dataField).value;
		if (data.length==0) {
			var msg = "Stampa non effettuata. Non ci sono dati da stampare!";
			alert(msg);
			return;
		}
		//04/06/2009-UGF05565
		document.getElementById('messagePrintOk').style.visibility="hidden";
		document.getElementById('messagePrint').style.visibility="visible";
		if (!writePrintFile(location,data)){
			return;
		}
		if (document.getElementById(fileField)) {
			document.getElementById(fileField).value = ''
		}
		document.getElementById(dataField).value = ''
		
		alert("Stampa effettuata. Salvataggio del file di stampa " + "'" + formatPrintFileName(location) + "'" + " completata!");
		
		document.getElementById('messagePrint').style.visibility="hidden";
		document.getElementById('messagePrintOk').style.visibility="visible";
}
function evalPrintLocation(fileField){
		if (!document.getElementById(fileField)) {
			var msg = "Stampa non effettuata. Il nome del file di stampa non e' valorizzato!";
			alert(msg);
			return "";
		}
		var file = document.getElementById(fileField).value;
		if (file.length==0) {
			var msg = "Stampa non effettuata. Il nome del file di stampa non e' valorizzato!";
			alert(msg);
			return "";
		}
		var createPathField = 'createPath';
		if (!document.getElementById(createPathField)) {
			var msg = "Stampa non effettuata. Il flag di creazione del percorso del file di stampa non e' valorizzato!";
			alert(msg);
			return "";
		}
		var createPath = document.getElementById(createPathField).value == 'true';
        var pathField = 'path';
        var pathFieldList = 'flatPathList';
		if (!document.getElementById(pathField) && !document.getElementById(pathFieldList)) {
			var msg = "Stampa non effettuata. Il percorso del file di stampa non e' valorizzato!";
			alert(msg);
			return "";
		}
        //06/11/2008-UGF05565
		var path;
		var pathList;
		if (document.getElementById(pathFieldList)) {
		   path = document.getElementById(pathFieldList).value;
		}else{
		   path = document.getElementById(pathField).value;
		}
		if (path.indexOf(";")==-1){
			pathList = new Array();
			pathList.push(path);
		}else{
			pathList = path.split(";");
		}
		/*
		var pathList;
		if (document.getElementById(pathFieldList)) {
			pathList = document.getElementById(pathFieldList).value;
			pathList = pathList.split(";");
		}else{
			pathList = new Array();
			pathList.push(document.getElementById(pathField).value);
		}
		*/
		var fs = getFileSystemObject();
		if (fs==null){
			return "";
		}
		for (i=0;i<pathList.length;i++){
			var path = pathList[i];
			if (!existsFilePath(path,fs)){
				//path = 'X:\\form\\Validazione\\Spool\\';
			    if (i==pathList.length-1){
				    if (createPath){
						if (!createRecursivelyFilePath(path,fs)){
							return "";
						}else{
							var location = path +  file + '.prt';
							return location;
						}
				    }else{
						var msg = "Stampa non effettuata. Il percorso del file di stampa " + "'" + path + "'" + " non e' definito o non e' valido!";
						alert(msg);
						return "";
				    }
				}
			}else{
				//path = 'C:\\form\\Validazione\\Spool\\';
				var location = path +  file + '.prt';
				return location;
			}
		}
}
function writePrintFile(file, data, fs){
		if (fs==null){
			 fs = getFileSystemObject();
		}
		if (fs==null){
			return;
		}
		var ts;
		if (fs.FileExists(file)) {
			ts = fs.GetFile(file);
			ts.Delete();
		}
		try{
			ts = fs.OpenTextFile(file, 2, true);
		}catch (exception){
			var str = "Stampa non effettuata. Errore durante l'apertura del file " + "'" + file + "'";
			alert(str);
			return false;
		}
		try{
			ts.WriteLine(data);
		}catch (exception){
			var str = "Stampa non effettuata. Le impostazioni del browser non consentono il salvataggio su disco locale del file di stampa " + "'" + file + "'.";
			str+=" Contattare l'amministratore del sistema per la modifica delle impostazioni.";
			alert(str);
			return false;
		}
		ts.close();
		return true;
}
function getFileSystemObject(){
	try{
        if (window.ActiveXObject){
		   return new ActiveXObject("Scripting.FileSystemObject");
		}else{
		  var str = "Stampa non effettuata. Browser non supportato!";
		  alert(str);
		  return null;
		}
	}catch (exception){
		var str = "Stampa non effettuata. Le impostazioni del browser non consentono l'interazione con il file system locale.";
		str+=" Contattare l'amministratore del sistema per la modifica delle impostazioni.";
		alert(str);
		return null;
	}
}
function existsFilePath(path,fs){
	if (fs==null){
		 fs = getFileSystemObject();
	}
	if (fs==null){
		return false;
	}
	try{
		return fs.FolderExists(path);
	}catch (exception){
		var str = "Stampa non effettuata. Errore durante la verifica dell'esistenza del path " + "'" + path + "'";
		alert(str);
		return false;
	}
}
function createFilePath(path,fs){
	if (fs==null){
		 fs = getFileSystemObject();
	}
	if (fs==null){
		return false;
	}
	try{
	  if (fs.FolderExists(path)){
	      return true;
	  }
	  fs.CreateFolder(path);
	  return true;
	}catch (exception){
		var str = "Stampa non effettuata. Errore durante la creazione del path " + "'" + path + "'";
		alert(str);
		return false;
	}
}
function createRecursivelyFilePath(path,fs){
	if (path==null||path.length==0){
		return true;
	}
	var index = path.lastIndexOf("/");
	if (index==-1){
		return true;
	}
	var parent = path.substring(0,index);
	var folder = path.substr(index);
	if (!createRecursivelyFilePath(parent,fs)){
		return false;
	}
	if (fs==null){
		fs = getFileSystemObject();
	}
	if (fs==null){
		return false;
	}
	try{
	  if (fs.FolderExists(path)){
	      return true;
	  }
	  fs.CreateFolder(path);
	  return true;
	}catch (exception){
		var str = "Stampa non effettuata. Errore durante la creazione del path " + "'" + path + "'";
		alert(str);
		return false;
	}
}

function formatPrintFileName(file){
	//29/05/2009-UGF05565
	//c:/form/spool/12736.prt
	try{
		if (file==null||file.length==0){
			return "";
		}
		var index = file.lastIndexOf("/");
		if (index!=-1){
			file = file.substr(index+1)
		}
		index = file.indexOf(".prt");
		if (index==-1){
			return file;
		}
		file = file.substring(0,index);
		return file;
	} catch (exception){
  		return ""
	}
}
