var b = '';
if (!window.console) var console = {log: $empty};
var url_generator = new Class({
	initialize: function(input, output, url) {
		this.inp = $(input);
		this.out = $(output);
		this.url = url;
		this.inp.addEvent('keyup', function() {
			this.generate_url(this.inp.value);
		}.bind(this));
		this.inp.addEvent('blur', function() {
			this.check_url(this.out.value);
		}.bind(this));
		this.out.addEvent('blur', function() {
			this.check_url(this.out.value);
		}.bind(this));
	},
	generate_url: function(text) {
		var a = text.clean().toLowerCase().replace(/[ +]/g, '-');
		this.out.value = a;
	},
	check_url: function(check) {
		console.log("going to check "+this.url+'/'+check);
		new Request({
			url: this.url+'/'+check,
			onSuccess: function(html, xml) {
				if(html == "1") {
					queue.alert("<p>Indirizzo disponibile!</p>")
				}
				else {
					queue.alert('<p style="color:red">Indirizzo non disponibile!</p>')
				}
			}
		}).send();
		console.log(check);
	}
});

var contacts_selector = new Class({
	initialize: function(table) {
		this.table = $(table);
		this.row = this.table.getElements('tr.field');
		this.prov = $('NewsletterProvincia');
		this.sect = $('NewsletterSettore');
		this.count_span = $('count');
		$('sel_all').addEvent('click', function(e) {
			e.stop();
			this.all(1);
			this.count();
		}.bind(this));
		$('unsel_all').addEvent('click', function(e) {
			e.stop();
			this.all(0);
			this.count();
		}.bind(this));
		$$('input.vis').each(function(item) {
			item.addEvent('change', this.count.bind(this))
		}.bind(this));
		this.prov.addEvent('change', function() {
			this.choose_prov(this.prov.value);
			this.count();
		}.bind(this));
		this.sect.addEvent('change', function() {
			this.choose_sec(this.sect.value);
			this.count();
		}.bind(this));
	},

	all: function(value) {
		this.table.getElements('input').each(function(item) {
			item.checked = value
		});
	},
	choose_prov: function(pro) {
		this.row.each(function(item) {
			if(item.getElement('td.prov').get('html') == pro) {
				item.getElement('input.vis').checked = 1;
			};
		}.bind(this));
	},

	choose_sec: function(sect) {
		this.row.each(function(item) {
			if(item.getElement('td.sect').get('html') == sect) {
				item.getElement('input.vis').checked = 1;
			};
		}.bind(this));
	},

	count: function() {
		var c = 0;
		this.row.each(function(item) {
			if(item.getElement('input.vis').checked == 1) c++;
		});
		this.count_span.set('html', c);
	}
});

var contacts_check = new Class({
	initialize: function(form) {
		this.form = $(form);
		this.errors = '';
		console.log($(form));
		this.form.addEvent('submit', function(e) {
			e.stop();
			if(this.validate()) {
				this.form.submit();
			};
		}.bind(this));
	},

	validate: function() {
		wrong = [];
		ret_value = true;
		this.form.getElements('.filled').each(function(item) {
			if(item.value.clean() == '') {
				wrong.push(item);
				ret_value = false;
			}
		});
		if(ret_value) {
			return true;
		}
		wrong.each(function(item) {
			item.highlight();
		});
		queue.alert("<p style='font-size:16px;color:red'>Tutti i campi sono obbligatori!</p>");
		return false;
	}
});



document.addEvent('domready', function() {
	vt2 = new Virtual.Ajax.single('product_registration', {
		effect: 'slide',
		effectOptions: {
			duration: 1500,
			transition: 'back:out'
		}
	});
});
