if (typeof MVD == 'undefined') MVD = {};

if (typeof console == 'undefined') console = { log: function(msg) { alert(msg) } }

MVD.extend = function (base, ext) {
	var i;
	for (i in ext) if (ext.hasOwnProperty(i)) {
		if (!base[i]) base[i] = ext[i];
	}
}

MVD.get = function(id) {
	var e = document.getElementById(id);
	if(e) { 
		e.show = function() { this.style.display = (this.nodeName == 'SPAN') ? 'inline' : 'block'; }
		e.hide = function() { this.style.display = 'none'; }
	}
	return e;
}

MVD.Form = function (id) {
	this.id = id;
	this.form = null;
	this.errores = [];
	return this;
}
	
MVD.extend(MVD.Form.prototype, 
	{
		init: function() {
			if(!this.form) {
				this.form = document[this.id];
			}			
		},
	
		get : function(id) {
			return this.form[id].value;
		},
		
		getForm: function() {
			var e = this.form.elements, f = {};
			for(var i=0;i<e.length;i++) {												
				f[e[i].name] = e[i].value;
			}			
			return f;
		},
		
		clear: function() {
			var e = this.form.elements;
			for(var i=0;i<e.length;i++) {												
				e[i].value = '';
			}						
		},
		
		checkNoVacio : function(input, msg) {
			if(this.form[input].value.length == 0) {
				this.errores.push(msg);
				return false;
			}
			return true;
		},
		
		checkMail : function(input, name) {
			var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
			if(!this.form[input].value.match(emailExp)){
				var msg = 'La dirección de e-mail ' + (name ? name + ' ' : '') + 'no es válida'				
				this.errores.push(msg);
				return false;
			}
			return true;
		},
		
		mostrarErrores : function(id, texto) {
			var el = MVD.get(id);
			if (el) {
				if (texto || this.errores.length > 0) {					
					el.innerHTML = '<ul><li>' + (texto ? texto : this.errores.join('</li><li>')) + '</li></ul>';
					el.show();
					setTimeout(function() { el.hide() }, 10000);
					return false;
				} else {
					el.hide();
					return true;					
				}
			}
		},
		
		clearErrores: function() {
			this.errores = [];
		}
		
	});

MVD.InternetGratis = function() {
	var f = new MVD.Form('formenvio');

	var mensaje = function(texto) {
		MVD.get('tblform').hide();
		var mens = MVD.get('mensaje');
		mens.innerHTML = texto;
		mens.show();
		f.clear();
	}

	var resultado = function(txtres) {
		MVD.get('ajax_progress').hide();
		switch(txtres) {
		case '3': 
		case '0':
			MVD.get('img_envio').hide();
			mensaje('Las instrucciones para activar tu internet gratis fueron enviadas a <b>' + 
					 f.get('UsrEmail') + '</b><br /><br />Gracias'); break;
		case '2':
			f.mostrarErrores('errores', 'La dirección de e-mail no existe.');			
			MVD.get('btn_conf').show();
			break;
		default:
			mensaje('Hubo un error al procesar su pedido.<br />' +
				    'Inténtelo más tarde o comuníquese con el teléfono 4022516 o internetgratis@montevideo.com.uy.<br />Gracias.');
		}
	}

	return {
		validar : function() {
			f.init();
			f.clearErrores();
			f.checkNoVacio('UsrNom', 'Ingrese su nombre')
			f.checkNoVacio('UsrApe', 'Ingrese su apellido')
			if (f.checkNoVacio('UsrEmail', 'Ingrese su e-mail')) {
				f.checkMail('UsrEmail');
			}									
			if (f.mostrarErrores('errores')) {
				MVD.get('ajax_progress').show();
				MVD.get('btn_conf').hide();
				MVD.Ajax.post('aigaltausuario.cgi', f.getForm(), resultado, resultado);				
			}			
			return false;
		}
	}
}();

MVD.IGRecomendar = function () {
	var f = new MVD.Form('form_recomendar');

	return {
		validar : function() {			
			f.init();
			f.clearErrores();
			f.checkNoVacio('NombreDestino', 'Ingrese el nombre del destinatario')
			if (f.checkNoVacio('MailDestino', 'Ingrese la dirección de e-mail del destinatario')) {
				f.checkMail('MailDestino', 'destino');
			}					
			f.checkNoVacio('NombreOrigen', 'Ingrese su nombre')			
			if (f.checkNoVacio('MailOrigen', 'Ingrese su e-mail')) {
				f.checkMail('MailOrigen', 'origen');
			}			
			return f.mostrarErrores('errores');
		},
		ocultarMsg : function() {
			var msg = MVD.get('mensaje');
			if (msg && msg.style.display != 'none') {
				setTimeout(function() { msg.hide() }, 10000);			
			}
		}
	}
}();

