var ATF = new Object();
ATF.count = {
	auteur:0
	,citation:0
	,theme:0
	,source:0
	,email:0
	,email_confirm:0
};

ATF.clear = function (e) {
	if (!ATF.count[e.name]) e.value=''; 
	ATF.count[e.name]++;
	e.addClassName('selected');
}

ATF.checkInsert = function (e) {
	if ($('auteur') && $('auteur').type=="text") ATF.clear($('auteur'));
	ATF.clear($('citation'));
	if ($('source') && $('source').type=="text") ATF.clear($('source'));
	ATF.clear($('theme'));
	ATF.clear($('email'));
	ATF.clear($('email_confirm'));
	
	if (!$('source').value || !$('auteur').value || !$('citation').value || !$('theme').value) {
		Ext.MessageBox.show({
           title: 'Champs tous obligatoires',
           msg: 'Merci de renseigner une citation, un auteur, une source et au moins un thème !',
           buttons: Ext.MessageBox.WARNING,
           animEl: e
//           fn: showResult
       });
		return false;
	}
	if (!$('captcha').value) {
		Ext.MessageBox.show({
           title: 'Anti-robot',
           msg: 'Merci de renseigner les caractères demandés.',
           buttons: Ext.MessageBox.WARNING,
           animEl: e
//           fn: showResult
       });
		return false;
	}
	if (!$('email').value || $('email').value!=$('email_confirm').value) {
		Ext.MessageBox.show({
           title: 'Email',
           msg: 'Merci de renseigner votre email deux fois de manière identique !',
           buttons: Ext.MessageBox.WARNING,
           animEl: e
//           fn: showResult
       });
		return false;
	}
	return true;
}

Ext.onReady(function(){
	new Ext.form.ComboBox({
		store: new Ext.data.Store({
			proxy: new Ext.data.HttpProxy({
				url: 'citations.json'
				,method: 'POST'
			}),
			reader: new Ext.data.JsonReader({
				root: 'citations',
				totalProperty: 'totalCount'
			}, [
	//			{name: 'title', mapping: 'topic_title'},
				{name: 'url', mapping: 'url'},
				{name: 'auteur', mapping: 'auteur'},
				{name: 'citation', mapping: 'citation'},
	//			{name: 'lastPost', mapping: 'post_time', type: 'date', dateFormat: 'timestamp'},
	//			{name: 'excerpt', mapping: 'post_text'}
				{name: 'nb', mapping: 'nb'}
			])
		}),
		displayField:'citation',
		typeAhead: false,
		loadingText: 'Recherche...',
//		width: 570,
		pageSize:10,
		minChars:4,
		mode:'remote',
		hideTrigger:true,
		tpl: new Ext.XTemplate('<tpl for="."><div class="search-item"><h3>{citation}</h3>{auteur}</div></tpl>' ),
		applyTo: 'citation',
		itemSelector: 'div.search-item',
		onSelect: function(record){ // override default onSelect to do redirect
			window.location = record.data.url;
		}
	});
	
	if ($('auteur') && $('auteur').type=="text") {
		// Combo auteur
		new Ext.form.ComboBox({
			store: new Ext.data.Store({
				proxy: new Ext.data.HttpProxy({
					url: 'auteurs.json'
					,method: 'POST'
				}),
				reader: new Ext.data.JsonReader({
					root: 'auteurs',
					totalProperty: 'totalCount',
					id: 'id'
				}, [
		//			{name: 'title', mapping: 'topic_title'},
					{name: 'id_auteur', mapping: 'id_auteur'},
					{name: 'auteur', mapping: 'auteur'},
		//			{name: 'lastPost', mapping: 'post_time', type: 'date', dateFormat: 'timestamp'},
		//			{name: 'excerpt', mapping: 'post_text'}
					{name: 'nb', mapping: 'nb'}
				])
			}),
			displayField:'auteur',
			typeAhead: false,
			loadingText: 'Recherche...',
	//		width: 570,
			pageSize:10,
			minChars:2,
			mode:'remote',
			hideTrigger:true,
			tpl: new Ext.XTemplate(
				'<tpl for="."><div class="search-item">',
	//				'<h3><span>{lastPost:date("M j, Y")}<br />by {author}</span>{title}</h3>',
					'<h3><span>{nb} Citations</span>{title}</h3>',
					'{auteur}',
				'</div></tpl>' 
			),
			applyTo: 'auteur',
			itemSelector: 'div.search-item',
//			onSelect: function(record){ // override default onSelect to do redirect
//				//alert(record.data.id_auteur);
//			}
		});
	}
		
	if ($('source') && $('source').type=="text") {
		// Combo source
		new Ext.form.ComboBox({
			store: new Ext.data.Store({
				proxy: new Ext.data.HttpProxy({
					url: 'sources.json'
					,method: 'POST'
				}),
				reader: new Ext.data.JsonReader({
					root: 'sources',
					totalProperty: 'totalCount',
					id: 'id'
				}, [
		//			{name: 'title', mapping: 'topic_title'},
					{name: 'id_source', mapping: 'id_source'},
					{name: 'source', mapping: 'source'},
		//			{name: 'lastPost', mapping: 'post_time', type: 'date', dateFormat: 'timestamp'},
		//			{name: 'excerpt', mapping: 'post_text'}
					{name: 'nb', mapping: 'nb'}
				])
			}),
			displayField:'source',
			typeAhead: false,
			loadingText: 'Recherche...',
			width: 570,
			pageSize:10,
			minChars:2,
			mode:'remote',
			hideTrigger:true,
			tpl: new Ext.XTemplate(
				'<tpl for="."><div class="search-item">',
	//				'<h3><span>{lastPost:date("M j, Y")}<br />by {author}</span>{title}</h3>',
					'<h3><span>{nb} Citations</span>{title}</h3>',
					'{source}',
				'</div></tpl>' 
			),
			applyTo: 'source',
			itemSelector: 'div.search-item',
//			onSelect: function(record){ // override default onSelect to do redirect
//				//alert(record.data.id_source);
//			}
		});
	}
});