/**
 * @author jfdesgagne
 */
var InfoLettres = new Class({
    fValidator:null,
	leForm:null,
	campagneCourrielValues:{
		fields:{
			field_city:51,
			field_country:49,
			field_entreprise:45,
			field_function:44,
			field_name:43,
			field_phone:47,
			field_state:50,
			field_title:42,
			field_zip:52,
			field_adress:48,
			email:"email"
		},

		df_end_fr:11,
		df_end_en:12,

		end:[71, 72, 74, 73, 70, 69],

	  	architecte_fr:10,
		architecte_en:8,
		entrepreneur_fr:15,
		entrepreneur_en:26,
		consommateur_en:18,
		consommateur_fr:16

	},
	reference:{
		field_youare:$('field_youare')
	},

    initialize: function(options){

		for(var element in this.campagneCourrielValues.fields) {this.reference[element] = $(element);}

		this.leForm = $('leForm');
		if ($('field_country')) {
			this.fValidator = new fValidator(this.leForm, {
				"onValidClass": this,
				"language": lang,
				"onValidFunction": "submitForm"
			});
			this.reference['field_country'].addEvent('change', this.stageChangeHandler.bind(this));
			$('field_youare').addEvent('change', this.changeFormHandler.bindWithEvent(this));
			$$('.field').each(function(el) {
				el.store('realName', el.name);
			}.bind(this));

			this.updateForm(this.campagneCourrielValues["architecte_"+lang]);
		} else {
			this.fValidator = new fValidator(this.leForm, {
				"onValidClass": this,
				"language": lang,
				"onValidFunction": "submitForm"
			});
			//this.leForm.action = "http://www.campagne-courriel.com/fr/form.php?form="+this.campagneCourrielValues["df_end_"+lang];
		}


    },

	changeFormHandler:function(event) {
		this.updateForm(this.campagneCourrielValues[event.target.get('value')+"_"+lang]);
	},

	updateForm:function(formId) {
		$$('.field').each(function(el) {
			var fieldId = this.campagneCourrielValues.fields[el.retrieve('realName')];
			if (fieldId == "email") {
				el.name = "email";
			} else {
				el.name = "CustomFields[" + fieldId + "]";
				el.id = "CustomFields_" + fieldId + "_" + formId;
			}
		}.bind(this));
		this.leForm.action = "http://www.campagne-courriel.com/fr/form.php?form="+formId;
		this.leForm.id = "frmSS"+formId;
	},


	/*
	phoneChangeHandler:function(event) {
		if ((event.code > 47 && event.code < 58) || (event.code > 95 && event.code < 106)) {
			if (event.target.get('value').length == 3) {
				var nextField = $(this.id.substr(0, this.id.length-1)+(Number(this.id.substr(this.id.length-1, 1))+1));
					nextField.focus();
					nextField.select();
			}
		}
	},
	*/

	stageChangeHandler:function() {
		new Request.JSON({
			"url":root+"assets/php/class.infolettreajax.php",
			"method":"post",
			"onComplete":function(data) {this.initStates(new Hash(data));}.bind(this)
		}).get({
			"lang":lang,
			"callFunction":"getStates",
			"country":this.reference['field_country'].options[this.reference['field_country'].selectedIndex].value
		});

		this.reference['field_state'].empty();
		new Element("option", {"value":"--"}).set('text', "-- Loading --").inject(this.reference['field_state']);
	},

	submitForm:function() {
		this.leForm.submit();
	},

	initStates:function(data) {
		this.reference['field_state'].empty();
		data.each(function(value, key) {
			new Element("option", {"value":key}).set('text', value).inject(this.reference['field_state']);
		}.bind(this));
	}
});

