/* (c) Jean Luc Biellmann - Groupe Ressources - 2010  */

var _FormCont = {
	ids : {
		'tabIndex'  : 'civ,fn,ln,adr,zip,city,tel,handy,fax,email,mess,security_code,change_code,button_submit'.split(','),
		'chkFields' : 'fn,ln,adr,zip,city,tel,handy,fax,email,mess,security_code'.split(','),
		'notNull'   : 'ln,email'.split(',')
	},
	err : function (mess) {
		alert(mess);
		return false;
	},
	chkField : function () {
		var id = 'mess';
		var label = _Form.getLabelFor(id);
		var value = _Form.getValue(id);
//console.log(label+':'+value);
		if (_Form.chkFieldEmpty(label,id,value))
			return false;
		if (_Form.chkFieldLengthMax(label,id,value,2000))
			return false;
		if (!_Form.chkFieldMatch(label,id,value,ALPHABET_FR+'0-9\\°\\!\\?\\/\\-\\ \\\'\\_\\,\\:\\;\\.\\t\\r\\n\\&\\~\\#\\{\\(\\[\\|\\`\\ç\\@\\)\\]\\=\\+\\$\\%\\µ\\*\\²\\<\\>'))
			return false;
		return true;
	},
	reset : function () {
		_FormUsr.reset();
		I($('mess'),'');
		_Captcha.reset();
		// we just delete mess field for future messages...
		_Cookie.set('mess','');
	},
	rec : function () {
		if (!_FormUsr.chkAll())
			return false;
		if (!$F('mess').length)
			return _FormCont.err('Message vide !');
		if (!$F('ln').length)
			return _FormCont.err('Le champ "Nom" est obligatoire !');
		if (!$F('email').length)
			return _FormCont.err('L\'adresse e-mail est obligatoire !');
		var thumbs = [];
		$$('img.slot').each(function (img) {
			thumbs.push(img.alt+'_'+img.src.replace(/^.*\//,'').replace(/\.jpg$/,''));
		});
		$('thumbs').value = thumbs.join(',');
//console.log($('thumbs').value);
		_JSON.send('m=contact&c=Contact&f=jsonRec',{
			formId: 'FormCont',
			url: 'index.php?json=true&',
			onSuccess : function (json) {
				if (json.jsonRec) {
					alert('Nous vous remerçions pour votre message ! Une copie vous a été envoyé par sécurité !');
					_FormCont.reset();
				}
			}
		});
	},
	resetMess : function () {
		if (confirm('Êtes-vous sûr de vouloir réinitialiser le champ message avec la sélection actuelle ?\n\nATTENTION : LE TEXTE DÉJÀ SAISI SERA DÉFINITIVEMENT EFFACÉ !')) {
			_JSON.send('m=contact&c=Contact&f=jsonResetMess',{
				formId: '',
				url: 'index.php?json=true&',
				onSuccess : function (json) {
					if (json.jsonResetMess) {
						var mess = String(json.mess);
						$('mess').value = mess.escapeHTML();
						_Cookie.set('mess',mess);
					}
				}
			});
		}
	},
	init : function (e) {
		if ($('FormCont')) {
			_Form.setTabIndex('FormCont',_FormCont.ids['tabIndex']);
			_Form.setNotNull('FormCont',_FormCont.ids['notNull']);
			_Form.setFields('FormCont',_FormCont.ids['tabIndex'],[],1); // use cookie to retrieve old datas
			_Form.setCheckedFields('FormCont',_FormCont.ids['chkFields']);

			$('fn').focus();
			$('col1').style.height = $('col2').style.height = Math.max($('col1').offsetHeight,$('col2').offsetHeight)+'px';
		}
	}
};

Event.observe(window, 'load', _FormCont.init);

