/* ========================================================
Marco Bevilacqua 20050323
Oggetto di tipo wordedit 
 ======================================================== */
function BaseElement(IDElement,IDElementFocus,Label,Posizione,Obbligatorio,Lingua) {
   this.IDElement = IDElement || null;				// Campo principale associato all'elemento. Esempio: Testo1
   this.IDElementFocus = IDElementFocus || null;	// Campo a cui verrā dato il focus
   this.PopupUpload = false;						// Indica se il campo richiede la visualizzazione del popup di Upload
   // Informazioni per i messaggi di output
   this.Label = Label  || '-';						// Label associata al campo
   this.Posizione = Posizione || '-';				// Posizione dell'elemento nella object
   this.Obbligatorio = Obbligatorio;				// Indica se occorre compilare il campo
   this.Lingua = Lingua || null;					// Lingua associata al campo
}

function baseElement_focus() {
  if (this.IDElementFocus != null)
		this.IDElementFocus.focus();
}

// Indica se il campo č stato compilato
function baseElement_isCompiledElement() {
  if (this.IDElement == null) {
  	return false;
  }else  if (eval(this.IDElement.value != "")) {
		return true;
  }  
  return false;
}

// Indica se il campo č stato compilato correttamente
function baseElement_checkElement() {
  return true;
}

function baseElement_impostaElement() {
	return true;
}

// Indica se il componente č stato caricato
function baseElement_isLoad() {
	return true;
}

// Inizializza l'elemento
function baseElement_init() {

}
 
 new BaseElement(null,null,null,null,false,null);
 BaseElement.prototype.isCompiledElement = baseElement_isCompiledElement;
 BaseElement.prototype.checkElement = baseElement_checkElement;
 BaseElement.prototype.impostaElement = baseElement_impostaElement;
 BaseElement.prototype.isLoad = baseElement_isLoad;
 BaseElement.prototype.init = baseElement_init;
 BaseElement.prototype.focus = baseElement_focus;
 
 // CAMPO RELAZIONE
 /*
 	IDTemplateField = identifica la relazione
	IDAgganciati = controllo cotenente gli ID degli oggetti collegati in relazione
	strAgganciati = controllo contenente le label degli oggetti collegati in relazione
 */
function RelazioneElement(IDTemplateField, txtIDAgganciati, txtStrAgganciati, Label, Posizione, Obbligatorio, Lingua) {
	this.IDTemplateField = IDTemplateField || null;
	this.txtIDAgganciati = txtIDAgganciati || null;
	this.txtStrAgganciati = txtStrAgganciati || null;
	// Ereditarietā del controllo
	this.base = BaseElement;
	this.base(this.IDTemplateField, null, Label, Posizione, Obbligatorio, Lingua);
}

// Imposta gli oggetti collegati alla relazione
function relazioneElement_setOggettiAgganciati(IDAgganciati, strAgganciati) {
	this.txtIDAgganciati.value = IDAgganciati;
	this.txtStrAgganciati.innerHTML = strAgganciati;
}

// Restituisce l'elenco degli oggetti collegati alla relazione
function relazioneElement_getOggettiAgganciati() {
	return this.txtIDAgganciati.value;
}

// Indica se il campo č stato compilato
function relazioneElement_isCompiledElement() {
  if (this.getOggettiAgganciati() == '') {
  	return false;
  } else {
	  return true;
	}
}

new RelazioneElement(null, null, null, null, null, null, null);
RelazioneElement.prototype = new BaseElement;
RelazioneElement.prototype.isCompiledElement = relazioneElement_isCompiledElement;
RelazioneElement.prototype.setOggettiAgganciati = relazioneElement_setOggettiAgganciati;
RelazioneElement.prototype.getOggettiAgganciati = relazioneElement_getOggettiAgganciati;

 // WORDEDIT ELEMENT
 
function WordEditElement(formField,frame) {
	// Campo hydden in cui č memorizzato il contenuto del wordedit
	this.formField = formField;

	// Frame di appoggio per la visualizzazione della preview
	this.frame = frame;

	// Href per l'eventuale foglio di stile
	this.stylesheet = null;
	// Elenco degli stili utilizzabili
	this.enstyles = null;
	
  // Ereditarietā del controllo
	this.base = BaseElement;
  this.base(this.formField,null,null,null,null,null);

}	 


// Dā il fuoco al controllo
function wordeditElement_focus() {
}

// Indica se il controllo č stato inizializzato
function wordeditElement_isLoad() {
	return true;
}

// Inializzazione del controllo
function wordeditElement_init() {
	this.sincroFrame();
}

// Legge il contenuto attuale del wordedit
function wordeditElement_read() {
	return this.formField.value;
}

// Scrivi l'HTML indicato nel wordedit
function wordeditElement_write(myHTML) {
	// Impostazione del campo nel form
	this.formField.value = myHTML;
	
	// Impostazione del frame di preview
	this.sincroFrame();
}

// Allinea il contenuto del frame al contenuto del campo hydden
function wordeditElement_sincroFrame() {
	this.frame.window.document.open();
	if (this.stylesheet != null)
		this.frame.window.document.writeln('<link rel="stylesheet" type="text/css" href="' + this.stylesheet + '">');
	this.frame.window.document.write(this.formField.value);
	this.frame.window.document.close();
}

new WordEditElement(null,null);
WordEditElement.prototype = new BaseElement;
WordEditElement.prototype.focus =  wordeditElement_focus;
WordEditElement.prototype.isLoad = wordeditElement_isLoad;
WordEditElement.prototype.init = wordeditElement_init;
WordEditElement.prototype.read =  wordeditElement_read;
WordEditElement.prototype.write =  wordeditElement_write;
WordEditElement.prototype.sincroFrame =  wordeditElement_sincroFrame;

/*==========================================================
	Oggetto tipo Radio
	=========================================================== */
function RadioElement(IDElement,IDElementFocus,Label,Posizione,Obbligatorio,Lingua) {
  this.base = BaseElement;
  this.base(IDElement,IDElementFocus,Label,Posizione,Obbligatorio,Lingua);
}

// Indica se il campo č stato compilato
function radioElement_isCompiledElement() {
  if (this.IDElement == null) {
  	return false;
  } else {
		var compilato = false;
		for (var count = 0; count < this.IDElement.length; count++)
			compilato = compilato || this.IDElement[count].checked;		
		if (compilato)
			return true;
		else
			return false;
  }
}

new RadioElement(null,null,null,null,false,null);
RadioElement.prototype = new BaseElement;
RadioElement.prototype.isCompiledElement = radioElement_isCompiledElement;

/* ========================================================
	NODE LIST
======================================================== */
function NodeListALivelli(Level,IDNode,Name,ParentNode) {
	this.Level = Level;							// Livello raggiunto nella struttura ad albero
	this.IDNode = IDNode;						// Identificativo - assieme al livello - del nodo corrente
	this.Name = Name;								// Nome associato al nodo corrente
	this.ParentNode = ParentNode;		// Nodo padre
	this.Child = new Array();				// Figli del nodo
}

// Aggiunge un nodo figlio
// children = oggrtto del tipo NodeListALivelli
function NodeList_addChild(children) {
	this.Child[this.Child.length] = children;
}

// Restituisce l'elenco dei figli nel formato IDNode, Name
function NodeList_toString() {
	var index;
	var aChild = new Array();
	for (index = 0; index < this.Child.length; index++) {
		aChild[index*2] = this.Child[index].IDNode;
		aChild[(index*2)+1] = this.Child[index].Name;
	}
	return aChild;
}

// Restituisce un nodo dato il suo identificatiore
// level => livello a cui appartiene il nodo
// IDNode => identificatore associato al nodo
function NodeList_getNode(Level,IDNode) {
	if ((this.Level == Level) && (this.IDNode == IDNode)) {
		return this;
	}
	else if (this.Child.length > 0) {
		var index;
		for (index = 0; index < this.Child.length; index++) {
			var appNode = null;
			appNode = this.Child[index].getNode(Level,IDNode);
			if (appNode != null ) {
				return appNode;
				break;				
			}
		}
	}
	else
		return null;
}

new NodeListALivelli(null,null,null,null);
NodeListALivelli.prototype.addChild = NodeList_addChild;
NodeListALivelli.prototype.toString = NodeList_toString;
NodeListALivelli.prototype.getNode = NodeList_getNode;