/* ========================================================
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
	 this.IDTab = null;	// Tab di appartenenza del controllo
	 this.ErrorMessage = null;
}

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() {
}

// Clear dell'elemento
function baseElement_clear() {
	this.IDElement.value = "";
}
 
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;
BaseElement.prototype.clear = baseElement_clear;

// Codice fiscale
CodiceFiscaleElement = function(IdElement, Label, Posizione, Obbligatorio, Lingua) {
	this.base = BaseElement;
	this.base(IdElement, IdElement, Label, Posizione, Obbligatorio, Lingua);	
}

CodiceFiscaleElement.prototype = new BaseElement;

// Funzione per la validazione del codice fiscale
CodiceFiscaleElement.prototype.checkElement = function() {
  // Controllo sulla correttezza del campo
  if (this.isCompiledElement()) {
		var stringa = new String(this.IDElement.value);
		var cf = stringa.toUpperCase();
		var cfReg = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/;
		if (!cfReg.test(cf)) {
      return false;
		}

		var set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		var set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
		var setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		var setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
		var s = 0;
		for( i = 1; i <= 13; i += 2 )
			s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
		for( i = 0; i <= 14; i += 2 )
			s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
		if ( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) ) {
			return false;
		}
	}	
  return true;
}

/* ==========================================================
   Oggetto di tipo Mail
   ========================================================== */
MailElement = function (IDElement, IDElementFocus, Label, Posizione, Obbligatorio, Lingua) {
  this.base = BaseElement;
  this.base(IDElement, IDElementFocus, Label, Posizione, Obbligatorio, Lingua);
}

MailElement.prototype = new BaseElement;

// Indica se il campo č stato compilato correttamente
MailElement.prototype.checkElement = function() {
  if (this.isCompiledElement()) {
		var stringa = new String;
		stringa = this.IDElement.value;
		if (stringa.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1) {
			return false;  
		}
	}  
	return true;
}

// CAMPO AGGREGAZIONE
AggregateBaseElement = function(arrayElement, posizione, obbligatorio, lingua, errorMessage) {
	this.base = BaseElement;
	this.base(null, null, null, posizione, obbligatorio, lingua);

	this.ArrayElement = arrayElement;
	this.ErrorMessage = errorMessage;	
}

AggregateBaseElement.prototype = new BaseElement;

// Il fuoco viene impostato sul primo elemento dell'aggrefazione
AggregateBaseElement.prototype.focus = function() {
	if (this.ArrayElement.length > 0) {
			this.ArrayElement[0].focus();
	}
}

// L'elemento č compilato quando almeno un elemento dell'aggregazione č compilato
AggregateBaseElement.prototype.isCompiledElement = function() {
	var isCompiled = false;
	for (var element in this.ArrayElement) {
			isCompiled = isCompiled || this.ArrayElement[element].isCompiledElement();			
	}	
	return isCompiled;
}

// L'elemento č compilato correttamente quando lo sono tutti gli elementi
// da cui č composto
AggregateBaseElement.prototype.checkElement = function() {
	var isChecked = true;
	for (var element in this.ArrayElement) {
			isChecked = isChecked && this.ArrayElement[element].checkElement();			
	}
	return isChecked;
}

AggregateBaseElement.prototype.impostaElement = function() {
	for (var element in this.ArrayElement) {
			this.ArrayElement[element].impostaElement();			
	}
}

// L'elemento č caricato quando lo sono tutti gli elementi da cui č composto
AggregateBaseElement.prototype.isLoad = function() {
	var isLoad = true;
	for (var element in this.ArrayElement) {
			isLoad = isLoad && this.ArrayElement[element].isLoad();			
	}
	return isLoad;
}

// Inizializza dell'elemento
AggregateBaseElement.prototype.init = function() {
	for (var element in this.ArrayElement) {
			this.ArrayElement[element].init();			
	}	
}

// Clear dell'elemento
AggregateBaseElement.prototype.clear = function() {
	for (var element in this.ArrayElement) {
			this.ArrayElement[element].clear();			
	}	
}

// CAMPO RELAZIONE
 /*
 	IDTemplateField = identifica la relazione
 */
function RelazioneElement(IDTemplateField, IDFolder, IDOggetto, IDElementCollection, TypeCollection, txtStrAgganciati, Label, Posizione, Obbligatorio, Lingua) {
	this.IDTemplateField = IDTemplateField || null;
	this.IDFolder = IDFolder || null;
	this.IDOggetto = IDOggetto || null;
	this.IDElementCollection = IDElementCollection || null;
	this.TypeCollection = TypeCollection || 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_setBoundElements(idElementCollection, typeCollection, strAgganciati) {
	this.IDElementCollection.value = idElementCollection;
	this.TypeCollection.value = typeCollection;
	this.txtStrAgganciati.innerHTML = strAgganciati;
}

// Restituisce l'elenco degli oggetti collegati alla relazione
/*
function relazioneElement_getBoundElements(idElementCollection, typeCollection) {
	idElemenCollection = this.IDElementCollection.value;
	typeCollection = this.TypeCollection.value;
}
*/

// Indica se il campo č stato compilato
function relazioneElement_isCompiledElement() {
  if (this.IDElementCollection.value == '') {
  	return false;
  } else {
	  return true;
	}
}

function relazioneElement_clear() {
	this.SetBoundElements('','', '');
}

// Apre la maschera di impostazione della relazione
function relazioneElement_openSetElementsWindow(lingua) {
	var url = '/admin/impostaRelazione.asp?LN=' + lingua + '&IDTemplateField=' + this.IDTemplateField + '&IDFolder=' + this.IDFolder + '&IDOggetto=' + this.IDOggetto + '&IDElementCollection=' + this.IDElementCollection.value + '&TypeCollection=' + this.TypeCollection.value;
	window.showModalDialog('container.asp?src=' + url.replace(/&/g, escape("&")).replace("?", escape("?")), this, 'dialogHeight:600px;dialogWidth:750px;resizable:yes;unadorned:yes');
}

new RelazioneElement(null, null, null, null, null, null, null, null, null);
RelazioneElement.prototype = new BaseElement;
RelazioneElement.prototype.SetBoundElements = relazioneElement_setBoundElements;
//RelazioneElement.prototype.GetBoundElements = relazioneElement_getBoundElements;
RelazioneElement.prototype.OpenSetElementsWindow = relazioneElement_openSetElementsWindow;
RelazioneElement.prototype.isCompiledElement = relazioneElement_isCompiledElement;
RelazioneElement.prototype.clear = relazioneElement_clear;

 // 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();
}

// Clear del controllo
function wordeditElement_clear() {
	this.write('');
}

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;
WordEditElement.prototype.clear = wordeditElement_clear;

/*==========================================================
	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;
  }
}

// Clear del controllo
function radioElement_clear() {
	for (var count = 0; count < this.IDElement.length; count++)
		this.IDElement[count].checked = false;	
}

new RadioElement(null,null,null,null,false,null);
RadioElement.prototype = new BaseElement;
RadioElement.prototype.isCompiledElement = radioElement_isCompiledElement;
RadioElement.prototype.clear = radioElement_clear;

/* ========================================================
	CHECK BOX
======================================================== */
CheckBoxElement = function(IDElement, IDElementFocus, Label, Posizione, Obbligatorio, Lingua) {
	this.base = BaseElement;
	this.base(IDElement, IDElementFocus, Label, Posizione, Obbligatorio, Lingua);
}

CheckBoxElement.prototype = new BaseElement;

CheckBoxElement.prototype.isCompiledElement = function() {
	return this.IDElement.checked;
}

/* ========================================================
	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;

/* ========================================================
	INTERO
======================================================== */
InteroElement = function(IDElement, IDElementFocus, Label, Posizione, Obbligatorio, Lingua) {
	this.base = BaseElement;
	this.base(IDElement, IDElementFocus, Label, Posizione, Obbligatorio, Lingua);
}
InteroElement.prototype = new BaseElement;
InteroElement.prototype.checkElement = function() {
	return (this.IDElement.value.match(/^\d*$/) != null);
}

/*===================================
	Validatore per il campo telefono
=====================================*/
function TelefonoElement(IDElement,IDElementFocus,Label,Posizione,Obbligatorio,Lingua) {
  this.base = BaseElement;
  this.base(IDElement,IDElementFocus,Label,Posizione,Obbligatorio,Lingua);
}

TelefonoElement.prototype = new BaseElement;

TelefonoElement.prototype.checkElement = function() {
	return (this.IDElement.value.match(/^\d*$/) != null);
}

/*===================================
	Validatore per un campo numerico
=====================================*/
function CAPElement(IDElement, IDElementFocus, Label, Posizione, Obbligatorio, Lingua) {
	this.base = BaseElement;
	this.base(IDElement, IDElementFocus, Label, Posizione, Obbligatorio, Lingua);
}

CAPElement.prototype = new BaseElement;

CAPElement.prototype.checkElement = function() {
	return (this.IDElement.value.match(/^\d{5}$/) != null);
}
