// JavaScript Document
oDistribuidor = new function(){
	var erNumero = RegExp('^[0-9]+$');
	var errorClass = 'fieldsetError';
	
	var inited = false, reqType;
	var ingresarC, errorC, usuarioC, contrasenaC;
	
	var req = new Request();
	
	this.init = function () {
		if (inited) { return inited; }
		
		ingresarC = $('ingresarDistribuidor');
		errorC = $('errorDistribuidor');
		
		usuarioC = $('usuarioDistribuidor');
		usuarioC.onblur = function(){
			var v = trim(this.value);
			if(v == '' || v.length < 3){ this.parentNode.className = errorClass; }
			else{ this.parentNode.className = ''; }
		}.closure(usuarioC);
		
		contrasenaC = $('contrasenaDistribuidor');
		contrasenaC.onblur = function(){
			var v = trim(this.value);
			if(v == '' || v.length < 3){ this.parentNode.className = errorClass; }
			else{ this.parentNode.className = ''; }
		}.closure(contrasenaC);
		
		//
		layR = $('layRecuperar');
		enviarR = $('enviarRecuperar');
		errorR = $('errorRecuperar');
		oRecuperar = new layer('Recuperar', '');
		
		emailR = $('emailRecuperar');
		emailR.onblur = function(){
			var v = trim(this.value);
			if(!ER_EMAIL.test(v)){ this.parentNode.className = errorClass; }
			else{ this.parentNode.className = ''; }
		}.closure(emailR);
		
		//
		layI = $('layRegistrar');
		enviarI = $('enviarRegistrar');
		errorI = $('errorRegistrar');
		oRegistrar = new layer('Registrar', '');
		
		razonSocialI = $('razonSocialRegistrar');
		razonSocialI.onblur = function(){
			var v = trim(this.value);
			if(v == '' || v.length < 3){ this.parentNode.className = errorClass; }
			else{ this.parentNode.className = ''; }
		}.closure(razonSocialI);
		
		nombreI = $('nombreRegistrar');
		nombreI.onblur = function(){
			var v = trim(this.value);
			if(v == '' || v.length < 3){ this.parentNode.className = errorClass; }
			else{ this.parentNode.className = ''; }
		}.closure(nombreI);
		
		emailI = $('emailRegistrar');
		emailI.onblur = function(){
			var v = trim(this.value);
			if(!ER_EMAIL.test(v)){ this.parentNode.className = errorClass; }
			else{ this.parentNode.className = ''; }
		}.closure(emailI);
		
		telefonoI = $('telefonoRegistrar');
		telefonoI.onblur = function(){
			var v = trim(this.value);
			if(v == '' || v.length < 3){ this.parentNode.className = errorClass; }
			else{ this.parentNode.className = ''; }
		}.closure(telefonoI);
		
		paisI = $('paisRegistrar');
		paisI.onchange = function(){
			provinciaI.disabled = ciudadI.disabled = true;
			provinciaNI.style.display = ciudadNI.style.display = 'none';
			
			reqType = 'provincias';
			
			var v = 'tipo' + SEP_IGUAL + reqType + SEP_AND;
			v += 'idPais' + SEP_IGUAL + paisI.value + SEP_AND;
			
			req.pedir(DIR_SERVER_ROOT + 'requests/zonas_geograficas.php', v);
		};
		paisI.onblur = function(){
			var v = trim(this.value);
			if(v == '' || !erNumero.test(v)){ this.parentNode.className = errorClass; }
		}.closure(paisI);
		
		provinciaI = $('provinciaRegistrar');
		provinciaI.onchange = function(){
			
			var v = trim(provinciaI.value);
			
			ciudadI.disabled = true;
			provinciaNI.style.display = (v == '0')? '' : 'none';
			ciudadNI.style.display = 'none';
			
			if(erNumero.test(v) && v != ''){
				
				reqType = 'ciudades';
				
				var v = 'tipo' + SEP_IGUAL + reqType + SEP_AND;
				v += 'idProvincia' + SEP_IGUAL + provinciaI.value + SEP_AND;
				
				req.pedir(DIR_SERVER_ROOT + 'requests/zonas_geograficas.php', v);
			}
		};
		provinciaI.onblur = function(){
			var v = trim(this.value);
			if(v == '' || !erNumero.test(v)){ this.parentNode.className = errorClass; }
			else{ provinciaNI.onblur(); }
		}.closure(provinciaI);
		
		provinciaNI = $('provinciaNombreRegistrar');
		provinciaNI.onblur = function(){
			var v = trim(this.value);
			if(provinciaI.value == '0' && (v == '' || v.length < 3)){ this.parentNode.className = errorClass; }
			else{ this.parentNode.className = ''; }
		}.closure(provinciaNI);
		
		ciudadI = $('ciudadRegistrar');
		ciudadI.onchange = function(){
			var v = trim(ciudadI.value);
			ciudadNI.style.display = (v == '0')? '' : 'none';
		};
		ciudadI.onblur = function(){
			var v = trim(this.value);
			if(v == '' || !erNumero.test(v)){ this.parentNode.className = errorClass; }
			else{ ciudadNI.onblur(); }
		}.closure(ciudadI);
		
		ciudadNI = $('ciudadNombreRegistrar');
		ciudadNI.onblur = function(){
			var v = trim(this.value);
			if(ciudadI.value == '0' && (v == '' || v.length < 3)){ this.parentNode.className = errorClass; }
			else{ this.parentNode.className = ''; }
		}.closure(ciudadNI);
		
		vendenPisosSiI = $('vendenPisosSiRegistrar');
		vendenPisosNoI = $('vendenPisosNoRegistrar');
		
		colocacionesSiI = $('colocacionesSiRegistrar');
		colocacionesNoI = $('colocacionesNoRegistrar');
		
		otrosProductosI = $('otroProductosRegistrar');
		
		marcasComplementariasI = $('marcasComplementariasRegistrar');
		
		inited = true;
	}
	
	//
	this.detectKey = function(e, a){
		if(e.keyCode == 13) {
			if (!a) { FireEvent(ingresarC, 'click'); }
			else if (a == 'recuperar') { FireEvent(enviarR, 'click'); }
			else if (a == 'registrar') { FireEvent(enviarI, 'click'); }
		}
	}
	
	//
	this.login = function(evt){
		var error = false, t = '';
		
		if(evt){ StopEvent(evt); }
		req.cancelar();
		
		usuarioC.onblur();
		if(usuarioC.parentNode.className == errorClass){ error = true; }
		
		contrasenaC.onblur();
		if(contrasenaC.parentNode.className == errorClass){ error = true; }
		
		if(error){
			errorC.innerHTML = this.msjFildsMark;
			errorC.style.display = 'block';
			return false;
		}
		
		errorC.style.display = 'none';
		
		reqType = 'login';
		
		t += 'accion' + SEP_IGUAL + reqType + SEP_AND;
		t += 'usuario' + SEP_IGUAL + trim(usuarioC.value) + SEP_AND;
		t += 'contrasena' + SEP_IGUAL + trim(contrasenaC.value) + SEP_AND;
		
		req.pedir(DIR_SERVER_ROOT + 'requests/distribuidores.php', t);
	}
	
	//L
	req.listener = function(){
		var d = req.respuestaXML;
		
		//
		if (!d) {
			LayError.onAccept = LayError.onCancel = function(){
				LayError.hide();
				Blocker.hide();
			}
			Blocker.show();
			LayError.show('Error');
		}
		//
		else if (reqType == 'login') {
			
			Blocker.show();
			if (d.getAttribute('exito') != 'si') {
				LayError.onAccept = LayError.onCancel = function(){
					LayError.hide();
					Blocker.hide();
				}
				LayError.show(d.firstChild.data);
			}
			else {
				Exito.onAccept = function(){ Blocker.hide(); };
				Exito.show(d.firstChild.data);
				document.location.href = document.location;
			}
		}
		//
		else if (reqType == 'recuperar') {
			
			layR.style.visibility = 'hidden';
			if (d.getAttribute('exito') != 'si') {
				LayError.onAccept = LayError.onCancel = function(){
					LayError.hide();
					layR.style.visibility = 'visible';
				}
				LayError.show(d.firstChild.data);
			}
			else {
				emailR.value = '';
				Exito.onAccept = function(){
					oDistribuidor.closeRecuperar();
				};
				Exito.show(d.firstChild.data);
			}
		}
		//
		else if(reqType == 'provincias'){
			while(provinciaI.hasChildNodes()){ provinciaI.removeChild(provinciaI.firstChild); }
			for(var i = 0, t = d.childNodes.length; i < t; i++){
				var opt = document.createElement('option');
				opt.value = d.childNodes[i].getAttribute('value');
				opt.innerHTML = d.childNodes[i].firstChild.data;
				
				provinciaI.appendChild(opt);
			}
			provinciaI.disabled = false;
		}
		//
		else if(reqType == 'ciudades'){
			while(ciudadI.hasChildNodes()){ ciudadI.removeChild(ciudadI.firstChild); }
			for(var i = 0, t = d.childNodes.length; i < t; i++){
				var opt = document.createElement('option');
				opt.value = d.childNodes[i].getAttribute('value');
				opt.innerHTML = d.childNodes[i].firstChild.data;
				
				ciudadI.appendChild(opt);
			}
			ciudadI.disabled = false;
		}
		//
		else if (reqType == 'registrar') {
			
			layI.style.visibility = 'hidden';
			if (d.getAttribute('exito') != 'si') {
				LayError.onAccept = LayError.onCancel = function(){
					LayError.hide();
					layI.style.visibility = 'visible';
				}
				LayError.show(d.firstChild.data);
			}
			else {
				razonSocialI.value = nombreI.value = emailI.value = telefonoI.value = 
				otrosProductosI.value = marcasComplementariasI.value = '';
				Exito.onAccept = function(){
					oDistribuidor.closeRegistrar();
				};
				Exito.show(d.firstChild.data);
			}
		}
		//
	}.closure(this);
	
	
	//
	
	
	var oRecuperar;
	var layR, errorR, emailR;
	
	this.openRecuperar = function (el, evt) {
		Blocker.show();
		oRecuperar.open(el);
		StopEvent(evt);
	}
	this.closeRecuperar = function (evt) {
		Blocker.hide();
		oRecuperar.close(evt);
	}
	this.sendRecuperar = function (evt) {
		var error = false, t = '';
		
		if(evt){ StopEvent(evt); }
		req.cancelar();
		
		emailR.onblur();
		if(emailR.parentNode.className == errorClass){ error = true; }
		
		if(error){
			errorR.innerHTML = this.msjFildsMark;
			errorR.style.display = 'block';
			return false;
		}
		
		errorR.style.display = 'none';
		
		reqType = 'recuperar';
		
		t += 'accion' + SEP_IGUAL + reqType + SEP_AND;
		t += 'email' + SEP_IGUAL + trim(emailR.value) + SEP_AND;
		
		req.pedir(DIR_SERVER_ROOT + 'requests/distribuidores.php', t);
	}
	
	
	//
	
	var oRegistrar;
	var enviarI, razonSocialI, nombreI, emailI, telefonoI, paisI, provinciaI, provinciaNI, ciudadI, ciudadNI;
	var errorI, layI, vendenPisosSiI, vendenPisosNoI, colocacionesSiI, colocacionesNoI, otrosProductosI, marcasComplementariasI;
	
	this.openRegistrar = function (el, evt) {
		Blocker.show();
		oRegistrar.open(el);
		StopEvent(evt);
	}
	this.closeRegistrar = function (evt) {
		Blocker.hide();
		oRegistrar.close(evt);
	}
	this.sendRegistrar = function (evt) {
		var error = false, t = '';
		
		if(evt){ StopEvent(evt); }
		req.cancelar();
		
		razonSocialI.onblur();
		if(razonSocialI.parentNode.className == errorClass){ error = true; }
		
		nombreI.onblur();
		if(nombreI.parentNode.className == errorClass){ error = true; }
		
		emailI.onblur();
		if(emailI.parentNode.className == errorClass){ error = true; }
		
		telefonoI.onblur();
		if(telefonoI.parentNode.className == errorClass){ error = true; }
		
		paisI.onblur();
		if(paisI.parentNode.className == errorClass){ error = true; }
		
		provinciaI.onblur();
		if(provinciaI.parentNode.className == errorClass){ error = true; }
		
		provinciaNI.onblur();
		if(provinciaNI.parentNode.className == errorClass){ error = true; }
		
		ciudadI.onblur();
		if(ciudadI.parentNode.className == errorClass){ error = true; }
		
		ciudadNI.onblur();
		if(ciudadNI.parentNode.className == errorClass){ error = true; }
		
		if(error){
			errorI.innerHTML = this.msjFildsMark;
			errorI.style.display = 'block';
			return false;
		}
		
		errorI.style.display = 'none';
		
		reqType = 'registrar';
		
		t += 'accion' + SEP_IGUAL + reqType + SEP_AND;
		t += 'razonSocial' + SEP_IGUAL + trim(razonSocialI.value) + SEP_AND;
		t += 'nombre' + SEP_IGUAL + trim(nombreI.value) + SEP_AND;
		t += 'email' + SEP_IGUAL + trim(emailI.value) + SEP_AND;
		t += 'telefono' + SEP_IGUAL + trim(telefonoI.value) + SEP_AND;
		t += 'idPais' + SEP_IGUAL + trim(paisI.value) + SEP_AND;
		t += 'idProvincia' + SEP_IGUAL + trim(provinciaI.value) + SEP_AND;
		t += 'nombreProvincia' + SEP_IGUAL + trim(provinciaNI.value) + SEP_AND;
		t += 'idCiudad' + SEP_IGUAL + trim(ciudadI.value) + SEP_AND;
		t += 'nombreCiudad' + SEP_IGUAL + trim(ciudadNI.value) + SEP_AND;
		t += 'vendenPisos' + SEP_IGUAL + ((vendenPisosSiI.checked)? 1 : 0) + SEP_AND;
		t += 'colocaciones' + SEP_IGUAL + ((colocacionesSiI.checked)? 1 : 0) + SEP_AND;
		t += 'otrosProductos' + SEP_IGUAL + trim(otrosProductosI.value) + SEP_AND;
		t += 'marcasComplementarias' + SEP_IGUAL + trim(marcasComplementariasI.value) + SEP_AND;
		
		req.pedir(DIR_SERVER_ROOT + 'requests/distribuidores.php', t);
	}
}

