function objetoAjax()
{
	var xmlhttp;
  	if (window.XMLHttpRequest)
  	{
  		// code for IE7+, Firefox, Chrome, Opera, Safari
  		xmlhttp=new XMLHttpRequest();
  	}
	else if (window.ActiveXObject)
  	{
  		// code for IE6, IE5
  		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	else
  	{
  		alert("Your browser does not support XMLHTTP!");
  	}

  	return xmlhttp;
}

function cambiarDia()
{
	var destino = document.getElementById('destino');
	var mes = document.getElementById('mes').value;
	var ano = document.getElementById('ano').value;
	var dia = document.getElementById('dia');
	var dia_seleccionado = document.getElementById('dia').value;
	
	var select = "<select name='dia' id='dia'>";
	if(mes == "1" || mes == "3" || mes == "5" || mes == "7" || mes == "8" || mes == "10" || mes == "12" )
	{
		for (i=1; i<32; i++)
		{
			select += "<option value="+i+">"+i+"</option>";	
		}
	}else if(mes == "4" || mes == "6" || mes == "8" || mes == "9" || mes == "11")
	{
		for (i=1; i<31; i++)
		{
			select += "<option value="+i+">"+i+"</option>";	
		}
	}else if(mes == "2" )
	{
		if ((ano % 4 == 0) && ((ano % 100 != 0) || (ano % 400 == 0)))
		{
			for (i=1; i<30; i++)
			{
				select += "<option value="+i+">"+i+"</option>";	
			}
		}else{
			for (i=1; i<29; i++)
			{
				select += "<option value="+i+">"+i+"</option>";	
			}
		}
	}
	select += "</select>";
	destino.innerHTML = select;
	document.getElementById('dia').value = dia_seleccionado;
}

var error = 0;
var passerror = 0;
var mailerror = 0;
var error_login = 0;
function vacio(valor,campo)
{
	if(valor == "")
	{
		error = 1;
		document.getElementById(campo).style.border="1px solid red";
	}
}

function vacioCorazon(valor,campo)
{
	if(valor == "")
	{
		error_login = 1;
		document.getElementById(campo).style.border="1px solid red";
	}
}

function error(valor,campo)
{
	error = 1;
	document.getElementById(campo).style.border="1px solid red";
}

function limpiaForm()
{
	error = 0;
	document.getElementById('nombre').style.border="1px solid black";
	document.getElementById('apellido').style.border="1px solid black";
	document.getElementById('sexo').style.border="1px solid black";
	document.getElementById('dni').style.border="1px solid black";
	document.getElementById('telefono').style.border="1px solid black";
	document.getElementById('mail').style.border="1px solid black";
	document.getElementById('pass1').style.border="1px solid black";
	document.getElementById('pass2').style.border="1px solid black";
	document.getElementById('direccion').style.border="1px solid black";
	document.getElementById('localidad').style.border="1px solid black";
	document.getElementById('selectProvincias').style.border="1px solid black";
}

function limpiaFormFinal()
{
	document.getElementById('nombre').value="";
	document.getElementById('apellido').value="";
	document.getElementById('sexo').value="";
	document.getElementById('dni').value="";
	document.getElementById('telefono').value="";
	document.getElementById('mail').value="";
	document.getElementById('pass1').value="";
	document.getElementById('pass2').value="";
	document.getElementById('direccion').value="";
	document.getElementById('localidad').value="";
	document.getElementById('selectProvincias').value="";
}


function limpiaFormCorazon()
{
	error = 0;
	document.getElementById('nombre').style.border="";
	document.getElementById('apellido').style.border="";
	document.getElementById('sexo').style.border="";
	document.getElementById('dni').style.border="";
	document.getElementById('telefono').style.border="";
	document.getElementById('mail').style.border="";
	document.getElementById('pass1').style.border="";
	document.getElementById('pass2').style.border="";
	document.getElementById('direccion').style.border="";
	document.getElementById('localidad').style.border="";
	document.getElementById('selectProvincias').style.border="";
	document.getElementById('rol_c').style.border="";
	document.getElementById('rol_p').style.border="";
}

function limpiaFormLogin()
{
	error_login = 0;
	document.getElementById('email').style.border="";
	document.getElementById('password').style.border="";
	
}

function eliminaEspacios(cadena)
{	
	while(cadena.charAt(cadena.length-1)==" ") cadena=cadena.substr(0, cadena.length-1);
	while(cadena.charAt(0)==" ") cadena=cadena.substr(1, cadena.length-1);
	return cadena;
}
function validaLongitud(valor, permiteVacio, minimo, maximo)
{
	var cantCar=valor.length;
	if(valor=="")
	{
		if(permiteVacio) return true;
		else return false;
	}
	else
	{
		if(cantCar>=minimo && cantCar<=maximo) return true;
		else return false;
	}
}
function validar_dni(valor){
  var checkOK = "0123456789";
  //var checkStr = formulario.edad.value;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  if(!validaLongitud(valor, 0, 7, 8)) allValid = false;
  else{
    for (i = 0; i < valor.length; i++) {
      ch = valor.charAt(i);
      for (j = 0; j < checkOK.length; j++)
        if (ch == checkOK.charAt(j))
          break;
      if (j == checkOK.length) {
        allValid = false;
        break;
      }
      allNum += ch;
    }
  }
  if(allValid) return true;
		else return false;
}

function validar_num(valor){
  var checkOK = "0123456789";
  
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  
    for (i = 0; i < valor.length; i++) {
      ch = valor.charAt(i);
      for (j = 0; j < checkOK.length; j++)
        if (ch == checkOK.charAt(j))
          break;
      if (j == checkOK.length) {
        allValid = false;
        break;
      }
      allNum += ch;
    }
  
  if(allValid) return true;
		else return false;
}

function validaCorreo(valor)
{
	if(valor != "")
	{
		var reg=/(^[a-zA-Z0-9._-]{1,30})@([a-zA-Z0-9.-]{1,30}$)/;
		if(reg.test(valor)) return true;
		else return false;
	} else
	{
		return false;
	} 
}

function erroneom(valor,campo)
{
	mailerror = 1;
	document.getElementById(campo).style.border="1px solid red";
}

function erroneod(valor,campo)
{
	dnierror = 1;
	document.getElementById(campo).style.border="1px solid red";
}

function validarPass(pass1,pass2)
{
	if(pass1 == "" || pass2 == "") error = 1;
	 
	if(pass1 != pass2 )
	{
		passerror = 1;
		document.getElementById("pass1").style.border="1px solid red";
		document.getElementById("pass2").style.border="1px solid red";
	} 
}

function validar()
{
	limpiaForm()
	error = 0;
	passerror = 0;
	mailerror = 0;
	dnierror = 0;
	
	var ip = document.getElementById('ip').value;
	var nombre = document.getElementById('nombre').value;
	var apellido = document.getElementById('apellido').value;
	var sexo = document.getElementById('sexo').value;
	var dia = document.getElementById('dia').value;
	var mes = document.getElementById('mes').value;
	var ano = document.getElementById('ano').value;

	
	var dni = document.getElementById('dni').value;
	var telefono = document.getElementById('telefono').value;
	var mail = document.getElementById('mail').value;
	var pass1 = document.getElementById('pass1').value;
	var pass2 = document.getElementById('pass2').value;
	var direccion = document.getElementById('direccion').value;
	var localidad = document.getElementById('localidad').value;
	var provincia_id = document.getElementById('selectProvincias').value;
	
	vacio(nombre,'nombre');
	vacio(apellido,'apellido');
	vacio(sexo,'sexo');
	vacio(dni,'dni');
	vacio(telefono,'telefono');
	vacio(mail,'mail');
	vacio(pass1,'pass1');
	vacio(pass2,'pass2');
	vacio(direccion,'direccion');
	vacio(localidad,'localidad');
	vacio(provincia_id,'selectProvincias');
	
	if(!validaCorreo(mail)) erroneom(mail,"mail");
	if(!validar_dni(dni)) erroneod(dni,"dni");

	validarPass(pass1,pass2);
		
	if(error == 1 || passerror == 1 || mailerror == 1 || dnierror == 1)
	{
		if(mailerror == 1) alert('Por favor introduzca un E-mail valido');
		if(dnierror == 1) alert('Por favor introduzca un D.N.I valido , sin puntos ni espacios');
		if(passerror == 1) alert('Por favor introduzca dos contraseņas iguales');
		if(error == 1) alert('Por favor complete todos los campos');
	}else
	{
		var ajax = objetoAjax();
		ajax.open("POST", "registrar.php", true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.send("ip="+ip+"&dia="+dia+"&mes="+mes+"&ano="+ano+"&nombre="+nombre+"&apellido="+apellido+"&sexo="+sexo+"&dni="+dni+"&telefono="+telefono+"&mail="+mail+"&pass="+pass1+"&direccion="+direccion+"&localidad="+localidad+"&provincia_id="+provincia_id+"&accion=registrar");
		ajax.onreadystatechange=function()
		{
			if (ajax.readyState==4)
			{
				var respuesta=ajax.responseText;
				//var respuesta="Mensaje Enviado correctmente";
				alert(respuesta);			
				if (respuesta == "Ya estas participando de la promo") 
				{
					window.document.location="consumidores-actualidad-promo.php";
				}
	  		}
		}
	}
}

function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function


function validarMesCorazon()
{
	limpiaFormCorazon()
	error = 0;
	passerror = 0;
	mailerror = 0;
	dnierror = 0;
	
	var ip = document.getElementById('ip').value;
	var nombre = document.getElementById('nombre').value;
	var apellido = document.getElementById('apellido').value;
	var sexo = document.getElementById('sexo').value;
	var dia = document.getElementById('dia').value;
	var mes = document.getElementById('mes').value;
	var ano = document.getElementById('ano').value;

	
	var dni = document.getElementById('dni').value;
	var telefono = document.getElementById('telefono').value;
	var mail = document.getElementById('mail').value;
	var pass1 = document.getElementById('pass1').value;
	var pass2 = document.getElementById('pass2').value;
	var direccion = document.getElementById('direccion').value;
	var localidad = document.getElementById('localidad').value;
	var provincia_id = document.getElementById('selectProvincias').value;
	
	var mas_info = document.getElementById('mas_info').checked;	
	var rol = getSelectedRadioValue(document.mes_corazon.rol);	
	
		
	vacio(nombre,'nombre');
	vacio(apellido,'apellido');
	vacio(sexo,'sexo');
	vacio(dni,'dni');
	vacio(telefono,'telefono');
	vacio(mail,'mail');
	vacio(pass1,'pass1');
	vacio(pass2,'pass2');
	vacio(direccion,'direccion');
	vacio(localidad,'localidad');
	vacio(provincia_id,'selectProvincias');
	
	vacio(rol,'rol_c');
	vacio(rol,'rol_p');
	
	if(!validaCorreo(mail)) erroneom(mail,"mail");
	if(!validar_dni(dni)) erroneod(dni,"dni");

	validarPass(pass1,pass2);
		
	if(error == 1 || passerror == 1 || mailerror == 1 || dnierror == 1)
	{
		if(mailerror == 1) alert('Por favor introduzca un E-mail valido');
		if(dnierror == 1) alert('Por favor introduzca un D.N.I valido , sin puntos ni espacios');
		if(passerror == 1) alert('Por favor introduzca dos contraseņas iguales');
		if(error == 1) alert('Por favor complete todos los campos');
	}else
	{
		var ajax = objetoAjax();
		ajax.open("POST", "registrar.php", true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.send("mas_info="+mas_info+"&rol="+rol+"&ip="+ip+"&dia="+dia+"&mes="+mes+"&ano="+ano+"&nombre="+nombre+"&apellido="+apellido+"&sexo="+sexo+"&dni="+dni+"&telefono="+telefono+"&mail="+mail+"&pass="+pass1+"&direccion="+direccion+"&localidad="+localidad+"&provincia_id="+provincia_id+"&accion=registrarCorazon");
		
		ajax.onreadystatechange=function()
		{
			if (ajax.readyState==4)
			{
				var respuesta=ajax.responseText;
				//var respuesta="Mensaje Enviado correctmente";
							
				if (respuesta == "Ya estas participando de la promo") 
				{
					window.document.location="consumidores-actualidad-promo-registro.php?ok=ok";
				}else alert(respuesta);
	  		}
		}
	}
}

function logearCorazon()
{
	limpiaFormLogin();
	var email = document.getElementById('email').value;
	var pass =  document.getElementById('password').value;

	vacioCorazon(email,'email');
	vacioCorazon(pass,'password');
	
	if(error_login == 1)alert('por favor complete todos los campos');
	else
	{
		var ajax = objetoAjax();
		ajax.open("POST", "registrar.php", true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.send("email="+email+"&pass="+pass+"&accion=loginCorazon");
		
		ajax.onreadystatechange=function()
		{
			if (ajax.readyState==4)
			{
				var respuesta=ajax.responseText;
				//var respuesta="Mensaje Enviado correctmente";
						
				if (respuesta == "ok") 
				{
					window.document.location="consumidores-actualidad-promo-recomendar.php";
				}else alert(respuesta);	
			}
		}
	}
}

function enviarPass()
{
	var email = document.getElementById('email').value;
	if(email == "") alert("Por favor ingrese su email");
	else
	{
		var ajax = objetoAjax();
		ajax.open("POST", "registrar.php", true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.send("email="+email+"&accion=enviarPass");
		
		ajax.onreadystatechange=function()
		{
			if (ajax.readyState==4)
			{
				var respuesta=ajax.responseText;
				if (respuesta == "ok") 
				{
					window.document.location="consumidores-actualidad-promo-pasword.php?ok=ok";
				}else alert(respuesta);	
			}
		}
	}
}

var enviar_mail = 0;

function recomendarCorazon()
{
	mailerror = 0;
	enviar_mail = 0;
	var nombre1 = document.getElementById('nombre1').value;
	var email1 = document.getElementById('email1').value;
	
	var nombre2 = document.getElementById('nombre2').value;
	var email2 = document.getElementById('email2').value;
	
	var nombre3 = document.getElementById('nombre3').value;
	var email3 = document.getElementById('email3').value;
	
	var nombre4 = document.getElementById('nombre4').value;
	var email4 = document.getElementById('email4').value;
	
	var nombre5 = document.getElementById('nombre5').value;
	var email5 = document.getElementById('email5').value;
	
	
	var parametros = "";
	
	if(nombre1 != "" && email1 != "" )
	{
		if(!validaCorreo(email1)) erroneom(email1,"email1");
		parametros += "&nombre1="+nombre1+"&email1="+email1;	
		enviar_mail = 1;
	}
	
	if(nombre2 != "" && email2 != "" )
	{
		if(!validaCorreo(email2)) erroneom(email2,"email2");
		parametros += "&nombre2="+nombre2+"&email2="+email2;	
		enviar_mail = 1;
	}
	
	if(nombre3 != "" && email3 != "" )
	{
		if(!validaCorreo(email3)) erroneom(email3,"email3");
		parametros += "&nombre3="+nombre3+"&email3="+email3;	
		enviar_mail = 1;
	}
	
	if(nombre4 != "" && email4 != "" )
	{
		if(!validaCorreo(email4)) erroneom(email4,"email4");
		parametros += "&nombre4="+nombre4+"&email4="+email4;	
		enviar_mail = 1;
	}
	
	if(nombre5 != "" && email5 != "" )
	{
		if(!validaCorreo(email5)) erroneom(email5,"email5");
		parametros += "&nombre5="+nombre5+"&email5="+email5;	
		enviar_mail = 1;
	}
	
	if(enviar_mail == 0)alert('Complete algun email con su nombre para continuar');
	else if(mailerror == 1) alert('Por favor introduzca un E-mail valido');
	else
	{
		var ajax = objetoAjax();
		ajax.open("POST", "registrar.php", true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.send("accion=recomendarCorazon"+parametros);
		
		ajax.onreadystatechange=function()
		{
			if (ajax.readyState==4)
			{
				var respuesta=ajax.responseText;
				if (respuesta == "ok") 
				{
					window.document.location="consumidores-actualidad-promo-recomendar.php?ok=ok";
				}else alert(respuesta);	
			}
		}
	}
}

function dejenosMail(rol)
{
	mailerror = 0;
	error = 0;
	document.getElementById('dejenos').style.border="";
	var email = document.getElementById('dejenos').value 
	vacio(email,'dejenos');  
	if(!validaCorreo(email)) erroneom(email,"dejenos");
	
	if(error == 1) alert("Por favor complete todos los campos");
	else if(mailerror == 1) alert('Por favor introduzca un E-mail valido');
	else
	{
		var ajax = objetoAjax();
		ajax.open("POST", "registrar.php", true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.send("accion=dejenos&email="+email+"&rol="+rol);
		
		ajax.onreadystatechange=function()
		{
			if (ajax.readyState==4)
			{
				var respuesta=ajax.responseText;
				if (respuesta == "ok") 
				{
					alert("Muchas gracias");
					document.getElementById('dejenos').value = "";
				}else alert(respuesta);	
			}
		}
	}	
}