/*
@name: htdocs/js/sms/am_dialog.js
@desc: Dialogo modale per inserimento/modifica contatti
@authors: Marco Biondi
@lastauthor: Marco Biondi
*/

var AMDialog = Class.create();
AMDialog.prototype = {
	initialize: function(idbkg, idform, idtitle, errs, hArray) {
		var foo = $(idform).getElementsByTagName('FORM');
		this.curModify = -1;
		this._formNew = foo[0];
		this._title = $(idtitle);
		this._bkg = $(idbkg);
		this._formLabels = hArray;
		this._errFields = {};
		this._errFields['mail'] = new fx.Height(errs['mail']);
		this._errFields['tel'] = new fx.Height(errs['tel']);
		this._errFields['det'] = $(errs['det']);
		this._errFields['messT'] = $(errs['mesT']);
		this._errFields['detailT'] = $(errs['detT']);

		//Cerco il primo nodo di testo
		foo = this._errFields['detailT'].firstChild;
		while(foo)
		{
			if(foo.nodeType == 3)
			{
				this._errFields['detailT_txt'] = foo;
				break;
			}
			foo = foo.nextSibling;
		}

		this._fadeFX1 = new fx.Opacity(idbkg, {duration: 100});
//		this._fadeFX1.__ctrl = this;
		this._fadeFX2 = new fx.Opacity(idform, {duration: 300});
//		this._fadeFX2.__ctrl = this;
//		this.__next = null;
		this._rubrica = null;
		this._fadeFX1.hide();
		this._fadeFX2.hide();
		this._isOn = false;
		this._elist = new EffectList();
		$(idform).style.display = 'block';
		$(idbkg).style.display = 'block';
		Event.observe('f_tele', 'change', this.phoneChanged.bindAsEventListener(this), false);
		Event.observe('f_mail', 'change', this.mailChanged.bindAsEventListener(this), false);
	},

	bindDataSource: function(ds) {
		this._rubrica = ds;
	},

	fAddContact: function(numero) {
		if(this._rubrica.isInitialized && this._formNew)
		{
			this.curModify = -1;
			this._title.innerHTML = this._formLabels['add'];
			Field.clear('f_tele','f_nome','f_cogn','f_nick','f_mail','f_note');
			$('f_conf').checked = false;
			if(numero)
				$('f_tele').value = numero;
			this.showForm(true);
		}
	},

	fModContact: function(numero) {
		if(this._rubrica.isInitialized && this._formNew)
		{
			var dati = this._rubrica.getItemAt(numero);	//Prendo dalla view corrente
			this._title.innerHTML = this._formLabels['mod'];
			Form.reset(this._formNew);
			$('f_tele').value = dati.numero;
			$('f_nome').value = dati.nome;
			$('f_cogn').value = dati.cognome;
			$('f_nick').value = dati.nick;
			$('f_mail').value = dati.email;
			$('f_note').value = dati.note;
			this.curModify = dati.idT;
			this.showForm(true);
		}
	},

	chkPhone: function() {
		var err = false;
		var res = true;

		if(!this._rubrica.isValidNumber($('f_tele').value))
		{
			Element.hide(this._errFields['det']);
			this._errFields['messT'].innerHTML = this._formLabels['errT'];
			err = true;
			res = false;
		}
/*
		else
		{
			var foo = this._rubrica.findContact($('f_tele').value);
			if(foo && foo.idT != this.curModify && foo.folder > 0 )
			{
				Element.show(this._errFields['det']);
				this._errFields['messT'].innerHTML = this._formLabels['dupT'];
				this._errFields['detailT_txt'].nodeValue = foo.formatTip('');
				err = true;
				res = $('f_conf').checked;
			}
		}
*/
		if(err)
		{
			if(this._errFields['tel'].now == 0)
				this._errFields['tel'].toggle();
		}
		else
		{
			if(this._errFields['tel'].now != 0)
				this._errFields['tel'].toggle();
		}

		return(res);
	},

	chkMail: function() {
		var err = false;

		if($('f_mail').value)
		{
			var mexp = /^[a-z0-9\+][a-z0-9\.\_\-]+\@[a-z0-9\.\-]+\.[a-z]+/i;
	
			err = !mexp.test($('f_mail').value);
	
			this._errFields['mail'].el.innerHTML = this._formLabels['errM'];
		}

		if(err)
		{
			if(this._errFields['mail'].now == 0)
				this._errFields['mail'].toggle();
		}
		else
		{
			if(this._errFields['mail'].now != 0)
				this._errFields['mail'].toggle();
		}

		return(!err);
	},

	chkForm: function() {
		var res = this.chkPhone();
		res = res && this.chkMail();

		if(!res)
			return(false);

		var dati = {
			numero: $('f_tele').value,
			nome: $('f_nome').value,
			cognome: $('f_cogn').value,
			nick: $('f_nick').value,
			email: $('f_mail').value,
			note: $('f_note').value,
			folder: 1
		};

		if(this.curModify == -1)
			this._rubrica.addContact(dati);
		else
			this._rubrica.modContact(this.curModify, dati);

		this.showForm(false);
		return(false);
	},

	cancelForm: function() {
		if(this._isOn)
		{
			this.showForm(false);
			return(false);
		}
		else
			return(true);
	},

	showForm: function(attivo) {
		if(this._elist.isPlaying())
			return;

		if(attivo)
		{
			this._errFields['mail'].hide();
			this._errFields['tel'].hide();
			Element.hide(this._errFields['det']);
			this._elist.addEffect(this._fadeFX1, 0, 0.9);
			this._elist.addEffect(this._fadeFX2);
//			this._fadeFX1.custom(0, 0.9);
//			this.__next = this._fadeFX2;
		}
		else
		{
			this._elist.addEffect(this._fadeFX2);
			this._elist.addEffect(this._fadeFX1, 0.9, 0);
//			this._fadeFX2.toggle();
//			this.__next = this._fadeFX1;
		}
		this._elist.playList();
		this._isOn = attivo;
	},

	nextFX: function() {
		alert('nextFX '+this.__next);
		if(this.__next)
		{
			if(this.__next == this._fadeFX1)
				this.__next.custom(0.9, 0);
			else
				this.__next.toggle();
			this.__next = null;
		}
		else
		{
			if(this._fadeFX2.now != 0)
				Field.focus('f_tele');
		}
	},

	phoneChanged: function(evt) {
		this.chkPhone();
		$('f_conf').checked = false;
	},

	mailChanged: function(evt) {
		this.chkMail();
	}
}
