/* Funciones javascript asociadas a la plantilla wordPress PersonalTemplate */

//activación o desactivación cajas de portada
		function cambiarCaja(obj, img,color1, color2){
			if (img != '')
			{
				newImage = "url("+img+")";
				obj.style.backgroundImage = newImage;
			}
			if (color1 !='' && color2!='')
			{
				colorFade(obj,'text',color1,color2,50,10);
			}
		}

//activación o desactivación de solapas
		function cambiarSolapa(obj, img, color1, color2){
			newImage = "url("+img+")";
			obj.style.backgroundImage = newImage;
			colorFade(obj,'text',color1,color2,50,10)
		}

//activación o desactivación de botones
		function cambiarBoton(obj, accion){
			if (accion=='in') {
				obj.style.border= '1px solid #ffad2b';
				obj.style.backgroundColor='#ffd99e';
				obj.style.fontWeight='bold';
			}
			else {
				obj.style.border= '1px dashed #999999';
				obj.style.backgroundColor='#ffffff';
				obj.style.fontWeight='normal';
			}
		}

//control de longitud de textarea
		function ismaxlength(obj){
			var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : "";
			if (obj.getAttribute && obj.value.length>mlength)
				obj.value=obj.value.substring(0,mlength);
		}

//validación de emails
		function echeck(str) {
			var error = false;
			var at = "@";
			var dot = ".";
			var lat = str.indexOf(at);
			var lstr = str.length;
			var ldot = str.indexOf(dot);

			if (str.indexOf(at)==-1)
			   error = true; 

			if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
			   error = true;
			
			if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
				error = true; 
			
			 if (str.indexOf(at,(lat+1))!=-1)
				error = true; 
			 
			 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
				error = true; 
			 
			 if (str.indexOf(dot,(lat+2))==-1)
				error = true; 
			 
			 if (str.indexOf(" ")!=-1)
				error = true; 
			 
			 return !error;				
		}

//validación formulario de envío de comentario
		function validar_comentario(){
			var error = false;
			var mensaje_error='';
			
			var autor = document.getElementById('author');
			var email = document.getElementById('email');

			if (autor != null && autor.value =='') {
				error=true;
				mensaje_error='El nombre es obligatorio';
			}

			if (email != null && email.value =='') {
				if (error==true) 
					mensaje_error+='\n';			
				mensaje_error+='El email es obligatorio';
				error=true;
			}			

			else if (email != null && !echeck(email.value) ) {
				if (error==true) 
					mensaje_error+='\n';			
				mensaje_error+='El email es incorrecto';
				error=true;
			}

			comentario = document.getElementById('comment');
			if (comentario.value =='') {
				if (error==true) 
					mensaje_error+='\n';	
				mensaje_error+='El mensaje es obligatorio';
				error=true;
			}
			
			if (error==true)
				alert (mensaje_error);
			return !error;
		}

//validación formulario de contacto
		function validar_contacto(){

			var error = false;
			var mensaje_error='';
			
			var autor = document.getElementById('author');
			var email = document.getElementById('email');
			var motivo = document.forms['commentform'].elements['motivo'];

			if (autor != null && autor.value =='') {
				error=true;
				mensaje_error='El nombre es obligatorio';
			}

			if (email != null && email.value =='') {
				if (error==true) 
					mensaje_error+='\n';			
				mensaje_error+='El email es obligatorio';
				error=true;
			}			

			else if (email != null && !echeck(email.value) ) {
				if (error==true) 
					mensaje_error+='\n';			
				mensaje_error+='El email es incorrecto';
				error=true;
			}

			var valormotivo = valorRadio(motivo);
			if ( valormotivo =='') {
				if (error) 
					mensaje_error+='\n';	
				mensaje_error+='El motivo es obligatorio';
				error=true;
			}
			else {
				//document.getElementById('url').value = 'http://'+valormotivo;
				//alert(document.getElementById('url').value);
			}

			comentario = document.getElementById('comment');
			if (dcomentario.value =='') {
				if (error) 
					mensaje_error+='\n';	
				mensaje_error+='El mensaje es obligatorio';
				error=true;
			}
			
			if (error)
				alert (mensaje_error);
			else {
				dcomentario.value = 'Motivo:' +valormotivo+'\n' + dcomentario.value; 
			}
			return !error;
		}

//valor de un array de radios
		function valorRadio(obj) {
			var cnt = -1;
			var i=obj.length-1;
			for (; i > -1; i--) {
				if (obj[i].checked) { 
					cnt = i; 
					i = -1;
				}
			}
			if (cnt > -1) 
				return obj[cnt].value;
			else 
				return '';
}

//validación formulario de busqueda
function validar_busqueda(){
			var error = false;
			
			var buscar = document.getElementById('s');

			if (buscar != null && buscar.value =='') 
				error=true;
			
			return !error;
		}

// efecto fade sopre solapa
function colorFade(obj,element,start,end,steps,speed) {
	  var startrgb,endrgb,er,eg,eb,step,rint,gint,bint,step;
	  var target = obj;
	  steps = steps || 20;
	  speed = speed || 20;
	  clearInterval(target.timer);
	  endrgb = colorConv(end);
	  er = endrgb[0];
	  eg = endrgb[1];
	  eb = endrgb[2];
	  if(!target.r) {
			startrgb = colorConv(start);
			r = startrgb[0];
			g = startrgb[1];
			b = startrgb[2];
			target.r = r;
			target.g = g;
			target.b = b;
	  }
	  rint = Math.round(Math.abs(target.r-er)/steps);
	  gint = Math.round(Math.abs(target.g-eg)/steps);
	  bint = Math.round(Math.abs(target.b-eb)/steps);
	  if(rint == 0) { rint = 1 }
	  if(gint == 0) { gint = 1 }
	  if(bint == 0) { bint = 1 }
	  target.step = 1;
	  target.timer = setInterval( function() { animateColor(obj,element,steps,er,eg,eb,rint,gint,bint) }, speed);
}

// auxiliar para fade solapas
function animateColor(obj,element,steps,er,eg,eb,rint,gint,bint) {
	  var target = obj;
	  var color;
	  if(target.step <= steps) {
			var r = target.r;
			var g = target.g;
			var b = target.b;
			if(r >= er) {
				r = r - rint;
			} else {
				r = parseInt(r) + parseInt(rint);
			}
			if(g >= eg) {
				 g = g - gint;
			} else {
				g = parseInt(g) + parseInt(gint);
			}
			if(b >= eb) {
				 b = b - bint;
			} else {
				b = parseInt(b) + parseInt(bint);
			}
			color = 'rgb(' + r + ',' + g + ',' + b + ')';
			if(element == 'background') {
				target.style.backgroundColor = color;
			} else if(element == 'border') {
				target.style.borderColor = color;
			} else {
				target.style.color = color;
			}
			target.r = r;
			target.g = g;
			target.b = b;
			target.step = target.step + 1;
	  } else {
			clearInterval(target.timer);
			color = 'rgb(' + er + ',' + eg + ',' + eb + ')';
			if(element == 'background') {
				target.style.backgroundColor = color;
			} else if(element == 'border') {
				target.style.borderColor = color;
			} else {
				target.style.color = color;
			}
	  }
}

// conversión color
function colorConv(color) {
	  var rgb = [parseInt(color.substring(0,2),16), 
				parseInt(color.substring(2,4),16), 
				parseInt(color.substring(4,6),16)];
	  return rgb;
}