window.addEvent('domready', function(){
									 
	
	if($defined($('second-content-bis'))){
		$('second-content-bis').fade();
	}
	/*
	$('consultoria').set('styles',{
		'display':'none'
	});
	*/
	if($defined($('nombre'))){
		$('nombre').addEvent('keypress', function (e){
			return base.maximo25(e);
		});
	}
	
	if($defined($('apel'))){
	    $('apel').addEvent('keypress', function (e){
		    return base.maximo150(e);
	    });
	}
	
	if($defined($('email'))){
	    $('email').addEvent('keypress', function (e){
		   return base.maximo255(e);
	    });
	}
	
	if($defined($('empr'))){
	    $('empr').addEvent('keypress', function (e){
		   return base.maximo255(e);
	    });
	}
	
	if($defined($('phone'))){
	    $('phone').addEvent('keypress', function (e){
		  return base.solonumeros(e);
	    });
	}

});
	
var base = {
		
	_numbers :new Array('0','1','2','3','4','5','6','7','8','9','backspace','enter','tab'),
		
	_root : null,
	_foto : null,
	
	_focoConsulta : false,
				
	_myTimer0 : null,
	_myTimer1 : null,
	_myTimer2 : null,
	
	/* externalLinks */
	externalLinks : function () {
		
		
	 
		 if (!document.getElementsByTagName) return;
	 
	  var anchors = document.getElementsByTagName("a");
		 
		 for (var i=0; i<anchors.length; i++) {
		   var anchor = anchors[i];
			   if (anchor.getAttribute("href") && (anchor.getAttribute("rev") == "friend" || anchor.getAttribute("rel") == "external")){
				   	   anchor.target = "_blank";
				   	   
			   }   	   
		 }
	},
	
	/*
	 * Para controlar las entradas en el formulario del layout
	 */
	
	maximo25 : function (e){
		if (e.key == 'backspace' || e.key == 'enter' || e.key == 'tab')
			exito = true;
		else
			exito = ($('nombre').value.length == 25 ? false : true);
		
		return exito;
	},
	
	maximo150 : function (e){
		if (e.key == 'backspace' || e.key == 'enter' || e.key == 'tab')
			exito = true;
		else
			exito = ($('nombre').value.length == 150 ? false : true);
		
		return exito;
	},
	
	maximo255 : function (e){
		if (e.key == 'backspace' || e.key == 'enter' || e.key == 'tab')
			exito = true;
		else
			exito = ($('nombre').value.length == 150 ? false : true);
		
		return exito;
	},
	
	solonumeros : function(e){
		if (e.key == 'backspace' || e.key == 'tab' || e.key == 'enter')
			return true;
		else{
			if (this._numbers.indexOf(e.key) != -1 && $('phone').value.length < 15)
				return true;
			else
				return false;
		}
	},
	
	/*
	 * 
	 */
	 
		
	diapositivas : function(){
		this._foto = 0;
		this.hideDiapositiva.delay(4000,this);
	},
	
	hideDiapositiva : function(){
		$('imagen'+this._foto).fade(0);
		this._foto++;
		if (this._foto == 10){
			this._foto = 2;
			this.showDiapositiva.delay(100,this);
		}
		else {
			this.hideDiapositiva.delay(4000,this);
		}
	},
	
	showDiapositiva : function(){
		$('imagen'+this._foto).fade(1);
		this._foto++;
		if (this._foto == 10){
			this._foto = 2;
			//mostramos la imagen 3 porque siempre ha estado oculta tras venir de hideDiapositiva
			$('imagen3').fade(1);
			this.hideDiapositiva.delay(100,this);
		}
		else {
			this.showDiapositiva.delay(1000,this);
		}
	},
	
	showNewDiapositiva : function(){	
		$('image-header').fade(1);
		this.hideDiapositiva.delay(4000,this);
	},
	
	
	/*
	 * Para mostrar y esconde CONSULTA RAPIDA
	 */
	
	hiddenConsultaRapida : function (){
		$('second-content-bis').fade();
	},

	noneConsultaRapida : function (){
		$('second-content-bis').style.display = "none";
	},
	
	schedulehidden : function() {
		this._myTimer1 = $clear(this._myTimer2);
		this.hiddenConsultaRapida();
		this._myTimer0 = this.noneConsultaRapida.delay(500, this);
	},
	
	checkHidden : function (){
		if (!this._focoConsulta && $('nombre').value == '' && $('apel').value == ''
			&& $('email').value == '' && $('phone').value == ''
				&& $('empr').value == '' && $('comment').value == ''
					&& !$('serv1').checked && !$('serv2').checked
						&& !$('serv3').checked && !$('serv4').checked
							&& !$('serv5').checked && !$('serv6').checked
								&& !$('serv7').checked && !$('serv8').checked)
				
			this.schedulehidden();
					
	},
	
	scheduleCheckHidden : function (){
		this._myTimer2 = this.checkHidden.periodical(10000, this);
	},
	
	addEventsConsulta : function(){
		$('second-content').addEvent('mouseover', function(){
			$('second-content-bis').style.display = "block";
			$('second-content-bis').fade(1);
			base.scheduleCheckHidden();
		});
		
		$('nombre').addEvent('focus', function (){
			base._focoConsulta = true;
		});
		
		$('nombre').addEvent('blur', function (){
			base._focoConsulta = false;
		});
		
		$('apel').addEvent('focus', function (){
			base._focoConsulta = true;
		});
		
		$('apel').addEvent('blur', function (){
			base._focoConsulta = false;
		});
		
		$('email').addEvent('focus', function (){
			base._focoConsulta = true;
		});
		
		$('email').addEvent('blur', function (){
			base._focoConsulta = false;
		});
		
		$('phone').addEvent('focus', function (){
			base._focoConsulta = true;
		});
		
		$('phone').addEvent('blur', function (){
			base._focoConsulta = false;
		});
		
		$('empr').addEvent('focus', function (){
			base._focoConsulta = true;
		});
		
		$('empr').addEvent('blur', function (){
			base._focoConsulta = false;
		});
		
		$('comment').addEvent('focus', function (){
			base._focoConsulta = true;
		});
		
		$('comment').addEvent('blur', function (){
			base._focoConsulta = false;
		});
	},
	
	/*
	 * 
	 */
	
	initMemberForm : function(){
		$('formtunegocio').addEvent('submit', this.postMemberForm.bind(this));
	},
	
	postMemberForm : function(){
		var form = $('formtunegocio');
		form.set('send', {
						url: form.getProperty('action'),
						method: 'post',
						onFailure: function(){alert('error');},
						onRequest: function(){
							
						},
						onSuccess : this.postMemberResponse.bind(this)
						
		});
		
		form.send();
		
		return false;
	},
	
	postMemberResponse : function(response){
		//$('name').setStyles({border:'1px solid yellow'});
		
		response = JSON.decode(response, true);
		
		if (response['resul'] == 1){
			$('tunegocio').set('html','Su test se ha realizado con &eacute;xito, tras valorar sus respuestas nuestros consultores se pondr&aacute;n en contacto con usted. Muchas gracias.');
		}
		else {
			if (response['errors']['name'] == 1){
				$('name').setStyles({
				    border: '2px solid #f9a46b'
				});
			}
			else{
				if (response['errors']['name'] == 0)
				{
					$('name').setStyles({
						border: '1px solid #000000'
					});
				}
			}
			if (response['errors']['mail'] == 1){
				$('mail').set('styles',{
					'border': '2px solid #f9a46b'
				});
			}
			else {
				if (response['errors']['mail'] == 0)
				{
					$('mail').set('styles',{
						'border': '1px solid #000000'
					});
				}
			}
			if (response['errors']['phone'] == 1){
				$('phone').set('styles',{
					'border': '2px solid #f9a46b'
				});
			}
			else {
				if (response['errors']['phone'] == 0){
						$('phone').set('styles',{
							'borderColor': '#000000'
						});
				}
			}
			$('resultadofail').set('html','Por favor, rellene los campos que se le indican. Muchas gracias');
		}
	},
	
	initMemberConsulta : function(){
		$('consulta').addEvent ('submit', this.postMemberConsulta.bind(this));
	},
	
	postMemberConsulta : function(){
		var form = $('consulta');
		form.set('send', {
						url: form.getProperty('action'),
						method: 'post',
						onFailure: function(){alert('error');},
						onRequest: function(){
							
						},
						onSuccess : this.postMemberConsultaResponse.bind(this)
						
		});
		
		form.send();
		
		return false;
	},
	
	postMemberConsultaResponse : function(response){
		
		response = JSON.decode(response, true);
		
		switch (response['resul']){
			case 1:
				$('consulta-content').set('html', 'Su consulta se ha realizado con &eacute;xito. Muchas gracias.');
				break;
			case 2:
				if (response['errors']['serv'] == 1){
					$('servicios').set('styles',{
						'borderBottom':'1px solid #f9a46b'
					});
				}
				if (response['errors']['serv'] == 0){
					$('servicios').set('styles',{
						'border':'none'
					});
				}
				if (response['errors']['name'] == 1){
					$('nombre').set('styles',{
						'border': '2px solid #f9a46b'
					});
				}
				if (response['errors']['name'] == 0){
					$('nombre').set('styles',{
						'borderColor': '#000000'
					});
				}
				if (response['errors']['mail'] == 1){
					$('email').set('styles',{
						'border': '2px solid #f9a46b'
					});
				}
				if (response['errors']['mail'] == 0){
					$('email').set('styles',{
						'borderColor': '#000000'
					});
				}
				if (response['errors']['comentario'] == 1){
					$('comment').set('styles',{
						'border': '2px solid #f9a46b'
					});
				}
				if (response['errors']['comentario'] == 0){
					$('comment').set('styles',{
						'borderColor': '#000000'
					});
				}
				$('result').set('html','Por favor, rellene los campos que se le indican. Muchas gracias.');
				break;
		}
	},
	
	mostrarlegal : function (){
		$('legal').set('styles',{
			'display':'block'
		});
	
	},
	
	/////////////////////////////////////
	/*
	 * initHome();
	 */
	initEventButton : function(){
		$$('.activeCourse').addEvent('click',function(){
			window.location = baseurl + '/index.php/es/evento.html'
		});
	},
	
	initCourseRegistration : function (){
		
		$('submit-registration').addEvent('click',(function(event){
			
			$$('#courseRegistration input').removeClass('error');
			
			var form = $('courseRegistration');
			
			form.set('send', {
							url: form.getProperty('action'),
							method: 'post',
							onFailure: function(){alert('error');},
							onRequest: function(){
								
							},
							onSuccess : this.postCourseResponse.bind(this)
							
			});
			
			form.send();
			
		}).bind(this));
		
		return false;
	},
	
	postCourseResponse : function (responseText) {
		
		//FRONT DEBUG
		//alert(responseText);
		//return false;
		
		response= JSON.decode(responseText);
		
		if(response.success){
			
			window.location.href=base._root+'/index.php/es/gracias-cursos.html';
			
		}else{
			
			if(response.invalids){
				$each(response.invalids, function(input){
					$$('#courseRegistration input[name='+input+']').addClass('error');
				});
			}
		}
		
		
	}
}


