var mostrarListasAzar = false;
function cargarMainOffline(idPerfil){
	mostrarListasAzar = true;
	new Ajax.Request('new_mainOffline.php',
			{
				//onLoading: showLoad,
				onSuccess: function(transport){
				var response = transport.responseText || "Se produjo un error en el servidor";
				$('loginChanger').innerHTML=response;
				$("boton_listas_ambas").style.display = "none";
				
				Event.observe('btnEnviarLogin', 'click', validarLoginND);
				Event.observe('lnkRegistracionMain', 'click', cargarRegistracion);
				Event.observe('buscarListaMain', 'click', cargarBuscarLista);
				Event.observe('invitarlaMain', 'click', cargarInvitarAmigosRed.curry('', '') , false);
				//Event.observe('btnWidget', 'click', function(){$('btnWidget').href = "./instalar/";}, false);
				
				Event.observe('lnkEnviarPassword', 'click', cargarEnviarPassword);
				Event.observe('solapaPerfilDe', 'click', cargarPerfilAlAzar);
				//Event.observe('btnVideoAyuda', 'click', function(){mostrarListasAzar=true;cargarVideoAyuda();});
				
				mostrarListasAzar = true;
				cargarPerfilAlAzar();
				
				if(idPerfil > 0){
					recibirPerfilH(idPerfil, 'home', "", "");
				}else{
					if(typeof(entraPorSeccion) != 'undefined'){
							switch(entraPorSeccion){
								case 'tarjetas':
									cargarEnviaTarjetas();
									break;
								case 'registrarse':
									cargarRegistracion();
									break;
							}
							
					}else{
						cargarHomeND();
						//mostrarVentanaFlotantePromo();
						//mostrarVideoPromoDiaDelAmigo();
					}
				}
			}
		});	
}

function cargarHomeND(){
	switchMenuOffline(1);
	new Ajax.Request('new_cargarHome.php',
			{
				//onLoading: showLoad,
				onSuccess: function(transport){
				var response = transport.responseText || "Se produjo un error en el servidor";
				$('divContainer').innerHTML=response;
				Event.observe('lnkRegistracion', 'click', cargarRegistracion);
				Event.observe('lnkBuscador', 'click', cargarBuscadorND);
				Event.observe('lnkBuscarLista', 'click', cargarBuscarLista);
				
				$("boton_listas_ambas").style.display = "none";
				switchSideBar(true, true, false);
			}
		});	
}


function cargarEnviarPassword(){
	switchMenuOffline(4);
	new Ajax.Request('cargarEnviarPassword.php', {
		onSuccess: function(transport){
			var response = transport.responseText || "Se produjo un error en el servidor";
			$('divContainer').innerHTML=response;
			Event.observe('btnEnviarPassword', 'click', validarPassword);
		}
	});
}


function recibirEnviarPasswordForm(){
	new Ajax.Request('recibirEnviarPasswordForm.php',
	  {	
		parameters: $('frmEnviarPassword').serialize(),
		onSuccess: function(transport){
		var response = transport.responseText || "Se produjo un error en el servidor";
		$('divErrorEnviarPassword').innerHTML=response;
		}
	  });
}

function cargarBuscadorND(){
	sumarclick(12);
	new Ajax.Request('new_cargarBuscador.php',
			{
				//onLoading: showLoad,
				onSuccess: function(transport){
				var response = transport.responseText || "Se produjo un error en el servidor";
				$('divContainer').innerHTML=response;
				Event.observe('btnBuscarUni', 'click', validarBuscarleND.curry(false), false);
				Event.observe('lnkVolver', 'click', cargarHomeND,false);
				
				$("boton_listas_ambas").style.display = "none";
			}
		});	
}

function cargarEstadisticasRegalos(regalos, locales, param){
	switchMenuOffline(1);
	sumarclick(13);
	new Ajax.Request("new_cargarEstadisticasRegalos.php?"+param,
		{
			onSuccess: function(transport){
				var response = transport.responseText || "Se produjo un error en el servidor";
				$('divContainer').innerHTML = response;
				$('divRegalos').innerHTML = regalos;
				$('divLocales').innerHTML = locales;
				
				Event.observe('lnkRegistracion', 'click', cargarRegistracion);
				Event.observe('lnkVolver', 'click', cargarBuscadorND, false);
				Event.observe('lnkVolver', 'click', sumarclick.curry(17), false);
				
				
				Event.observe('lnkEnviarListaMail', 'click', cargarEnviarListaMail.curry(param),false);
				Event.observe('lnkImprimir', 'click', cargarImprimirRegalos.curry(param),false);
				
		}
	});

}


function cargarEnviarListaMail(url){
	var hidden="";
	checkboxs = document.getElementsByName("check");
	for(index = 0; index < checkboxs.length; index++){
		if(checkboxs[index].checked){
			hidden+=checkboxs[index].value+"---";
		}
	}
	if(hidden != ""){
		switchMenuOffline(4);
		sumarclick(15);
		new Ajax.Request('new_cargarEnviarListaMail.php',
		{
			onSuccess: function(transport){
				var response = transport.responseText || "Se produjo un error en el servidor";
				$('divContainer').innerHTML=response
				$('hdnRegalos').value=hidden;
				
				Event.observe('lnkEnviarListaMail', 'click', validarListaMail);
				Event.observe('lnkVolver', 'click', recibirBuscarUniND.curry(url, false),false);
				Event.observe('lnkRegistracion', 'click', cargarRegistracion);
			}
		});
	}else{
		$("divError").innerHTML = "Ten&eacute;s que seleccionar los regalos de la lista";
	}
}

function cargarImprimirRegalos(url){
	var hidden="";
	var regalos = new Array;
	indexRegalos = 0;
	checkboxs = document.getElementsByName("check");
	for(index = 0; index < checkboxs.length; index++){
		if(checkboxs[index].checked){
			regalos[indexRegalos] = checkboxs[index].value;
			indexRegalos++;
		}
	}
	if(indexRegalos > 0){
		switchMenuOffline(4);
		sumarclick(14);
		new Ajax.Request('new_cargarImprimir.php',
		{
			onSuccess: function(transport){
				var response = transport.responseText || "Se produjo un error en el servidor";
				$('divContainer').innerHTML=response
				
				html = "<table class='textob'>";
				for(index = 0;index < regalos.length;index++){
					html += "<tr><td>&bull;"+regalos[index]+"</td></tr>";
				}
				html += "</table>";
				$("divElegidos").innerHTML = html;
				//Event.observe('lnkEnviarListaMail', 'click', validarListaMail);
				Event.observe('lnkVolver', 'click', recibirBuscarUniND.curry(url, false));
			}
		});
	}else{
		$("divError").innerHTML = "Ten&eacute;s que seleccionar los regalos de la lista";
	}
}

function cargarBuscarLista(){
	sumarclick(20);
	new Ajax.Request("new_cargarBuscarLista.php", {
		onSuccess: function(transport){
			var response = transport.responseText || "Se produjo un error en el servidor";
			$("divContainer").innerHTML=response;
			
			Event.observe('lnkRegistracion', 'click', cargarRegistracion);
			Event.observe('btnBuscarRegalos', 'click', validarBuscadorND.curry(false));
				Event.observe('lnkVolver', 'click', cargarHomeND,false);
			
			$("boton_listas_ambas").style.display = "none";
		}
	});
}

function cargarResultadosBuscadorLista(datos, param_url){
	sumarclick(21);
	new Ajax.Request("new_cargarResultadosBuscadorLista.php",{
		onSuccess: function(transport){
			var response = transport.responseText || "Se produjo un error en el servidor";
			$("divContainer").innerHTML=response;
			$("boton_listas_ambas").style.display = "none";
			
			if(datos.length > 6){
				$('divSabiasQue').style.display = "block";
			}
			// armo la estructura de los resultados
			htmlDatos = "";
			if(datos.length > 0 && datos[0].id > 0){
				
				mes = ['cero','enero', 'febrero', 'marzo', 'abril', 'mayo', 'junio', 'julio', 'agosto', 'septiembre', 'octubre', 'noviembre', 'diciembre'];
				
				for(i=0; i<datos.length; i++){
					htmlDatos += "<table width=\"100%\" bgcolor=\"#a8d5db\" style=\"margin-top:10px;\">";
					htmlDatos += "	<tr>";
					htmlDatos += "		<td rowspan=\"2\"><a href=\"#\" id=\"lnkVerPerfil"+datos[i].id+"\">";
					htmlDatos += 			datos[i].thumb;
					htmlDatos += "		 </a></td>";
					htmlDatos += "		<td class=\"textob\" width=\"60%\">";
					htmlDatos += 			datos[i].nombre+" "+datos[i].apellido;
					htmlDatos += "		 </td>";
					htmlDatos += "		 <td align=\"right\">";
					htmlDatos += "			<a href=\"#\" id=\"lnkVerPerfil1"+datos[i].id+"\"><img src=\"img/btn_vermislistas.gif\" border=\"0\"/></a>";
					htmlDatos += "		 </td>";
					htmlDatos += "	 </tr>";
					htmlDatos += "		<td class=\"textopequeb\">";
					
					fecha=datos[i].edad.split("-");
					htmlDatos += "			Mi cumplea&ntilde;os es el "+fecha[2]+" de "+mes[parseInt(fecha[1], 10)];
					htmlDatos += "		 </td>";
					htmlDatos += "		 <td align=\"right\">";
//					htmlDatos += "			<img src=\"img/seamosamigos.png\" border=\"no\"/>";
					htmlDatos += "		 </td>";
					htmlDatos += "	 </tr>";
					htmlDatos += "</table>";
				}
			}
			
			$("divResultados").innerHTML = htmlDatos;
			
			if(datos[0].id!=0){
				for(i=0; i<datos.length; i++){
					id=datos[i].id;
					nombre=datos[i].nombre+" "+datos[i].apellido;
					Event.observe('lnkVerPerfil'+id, 'click', recibirPerfilH.curry(id, 'buscador', nombre, param_url), false);
					Event.observe('lnkVerPerfil1'+id, 'click', recibirPerfilH.curry(id, 'buscador', nombre, param_url), false);
/*					if(datos[i].seguime!=2){
						Event.observe('lnkSeguime'+id, 'click', recibirSeguime.curry(id, nombre), false);
					}
*/				}
			}
			
			Event.observe('lnkRegistracion', 'click', cargarRegistracion);
			
		    Event.observe('lnkBuscador', 'click', sumarclick.curry(22));
			Event.observe('lnkBuscador', 'click', cargarBuscadorND);
			Event.observe('lnkInvitarla', 'click', cargarInvitarAmigosRed.curry('buscador', param_url) , false);
		    Event.observe('lnkInvitarla', 'click', sumarclick.curry(23));
			Event.observe('lnkVolver', 'click', cargarBuscarLista,false);
		    Event.observe('lnkVolver', 'click', sumarclick.curry(24));
			
		}
	});
}


function cargarEnviarAmigo(ubicacion, param_url){
	new Ajax.Request('new_cargarEnviarAmigo.php',{
			onSuccess: function(transport){
				var response = transport.responseText || "Se produjo un error en el servidor";
				$('divContainer').innerHTML=response;
				$("boton_listas_ambas").style.display = "none";
				
				Event.observe('lnkEnviarMail', 'click', validarAmigos);
				//Event.observe('lnkVolver', 'click', cargarHomeND);
				
				
				switch(ubicacion){
					case 'registrarse':
						sumarclick(6);
						$('lnkVolver').innerHTML = "<img src=\"img/btn_siguiente.jpg\" border=\"0\"/>";
						Event.observe('lnkVolver', 'click', cargarRegistracionSatisfactoria,false);
						Event.observe('lnkVolverRedes', 'click', cargarInvitarAmigosRed.curry(ubicacion, param_url),false);
						break;
					case 'buscador':
					default:
						sumarclick(27);
						$('lnkVolverRedes').innerHTML = "";
						$('lnkVolver').innerHTML = "<img src=\"img/btn_volver.jpg\" border=\"0\"/>";
						Event.observe('lnkVolver', 'click', cargarInvitarAmigosRed.curry(ubicacion, param_url),false);
						//$("lnkSaltear").style.display = "none";
						break;
				}
			}
		});
}


function validarLoginND(){
	var error=0;
	if($("txtNombreLogin").value==""){
		$("divErrorLoginN").innerHTML="ingres&aacute; tu usuario";
		Event.observe('txtNombreLogin', 'focus', limpiarError.curry('divErrorLoginN'),false);
		error=1;
	}else{
		var p, val;
		val=$("txtNombreLogin").value;
		p=val.indexOf('@');
       if (p<1 || p==(val.length-1)){
			$("divErrorLoginN").innerHTML="ingresa un e-mail valido";
			Event.observe('txtNombreLogin', 'focus', limpiarError.curry('divErrorLoginN'),false);
			error=1;
	   }
	}
	if($("txtPasswordLogin").value==""){
		$("divErrorLoginP").innerHTML="ingres&aacute; tu password";
		Event.observe('txtPasswordLogin', 'focus', limpiarError.curry('divErrorLoginP'),false);
		error=1;
	}
	if(error==0){
		recibirLoginND();
	}
}


function cargarListas(i, usr_id){
new Ajax.Request('new_cargarListas.php?registro='+i+'&usr_id='+usr_id,
	{
		onSuccess: function(transport){
		var response = transport.responseText || "Se produjo un error en el servidor";
		$('divListas').innerHTML=response;
		//Event.observe('btnOK', 'click', recibirComentarLista.curry(datos[0].lista_id, usr_id, i),false);
	}
  });	
}


function cargarLocales(usr_id){
new Ajax.Request('new_cargarLocales.php?usr_id='+usr_id,
	{
		onSuccess: function(transport){
		var response = transport.responseText || "Se produjo un error en el servidor";
		$('divLocales').innerHTML=response;
	}
  });	
}

function cargarDatosUsuario(usr_id){
new Ajax.Request('new_cargarDatosUsuario.php?usr_id='+usr_id,
	{
		onSuccess: function(transport){
		var response = transport.responseText || "Se produjo un error en el servidor";
		$('divDatos').innerHTML=response;
	}
  });	
}


function cargarMiraComerciales(){
	sumarclick(25);
	switchMenuOffline(2);
	new Ajax.Request('new_cargarMiraComerciales.php',
		{
			onSuccess: function(transport){
			var response = transport.responseText || "Se produjo un error en el servidor";
			$('divContainer').innerHTML=response;
			
			$("boton_listas_ambas").style.display = "none";
			switchSideBar(true, true, false);
		}
	  });	
	
}


function cargarEnviaTarjetas(){
	sumarclick(26);
	switchMenuOffline(3);
	new Ajax.Request('new_cargarEnviaTarjetas.php',
		{
			onSuccess: function(transport){
			var response = transport.responseText || "Se produjo un error en el servidor";
			$('divContainer').innerHTML=response;
			
			$("boton_listas_ambas").style.display = "none";
			switchSideBar(true, true, false);
			
			Event.observe('lnkEnviar', 'click', enviarTarjeta,false);
		}
	  });	
	
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

var tarjetaDiferida = false;
function setTarjetaDiferida(diferida){
		tarjetaDiferida = diferida;
}

function enviarTarjeta(){
	if(validarDatosAmigos() == 0){
		if(tarjetaDiferida){
			new Ajax.Request('new_recibirEnviarTarjetaDiferido.php',
				{
					parameters: $("frmTarjetas").serialize(),
					onSuccess: function(transport){
					var response = transport.responseText || "Se produjo un error en el servidor";
					
					new Ajax.Request('cargarFlotanteMensajeOK.php', {
				//		onLoading: showLoad,
						onSuccess: function(transport){
							var response = transport.responseText || "Se produjo un error en el servidor";
							mostrarVentanaFlotante(response);
							
							$('txtMensajeFlotante').innerHTML="&iexcl;T&uacute; tarjeta ser&aacute; enviada a "+$("txtNombre").value+" un dia despu&eacute;s del dia del amigo!<br><br>Muchas gracias";
					
							$("txtNombre").value = "";
							$("txtEmail").value = "";
							$("txtMensaje").value = "";
							
							Event.observe('btnOk', 'click', function(){ocultarVentanaFlotante();}, false);
						}
					});
					//$("divInfo").innerHTML = "Tu tarjeta ser&aacute; enviada a "+$("txtNombre").value+" el dia del amigo!";
				}
			  });
		}else{
			new Ajax.Request('new_recibirEnviarTarjeta.php',
				{
					parameters: $("frmTarjetas").serialize(),
					onSuccess: function(transport){
					var response = transport.responseText || "Se produjo un error en el servidor";
					
					new Ajax.Request('cargarFlotanteMensajeOK.php', {
				//		onLoading: showLoad,
						onSuccess: function(transport){
							var response2 = transport.responseText || "Se produjo un error en el servidor";
							mostrarVentanaFlotante(response2);
							
							$('txtMensajeFlotante').innerHTML="&iexcl;T&uacute; tarjeta se envi&oacute; a "+$("txtNombre").value+"!<br><br>Muchas gracias";
							$("txtNombre").value = "";
							$("txtEmail").value = "";
							$("txtMensaje").value = "";
							
							sumarclick(50);
							Event.observe('btnOk', 'click', function(){ocultarVentanaFlotante();}, false);
						}
					});
					
					//$("divInfo").innerHTML = "Tu tarjeta se envi&oacute; a "+$("txtNombre").value+"!";
				}
			  });
		}
	}
}

function cargarPerfilAlAzar(){
	//switchSolapasComercial(1);
	if(mostrarListasAzar){
		new Ajax.Request('cargarPerfilAzar.php',
			{
				onSuccess: function(transport){
				var response = transport.responseText || "Se produjo un error en el servidor";
				$('comercial').innerHTML = response;
				
				Event.observe('btnHaceTuLista', 'click', cargarRegistracion);
				setTimeout(cargarPerfilAlAzar, 15000);
			}
		  });	
	}
}
function cargarVideoAyuda(){
	switchSolapasComercial(1);
	mostrarListasAzar = false;
		new Ajax.Request('cargarVideoAyuda.php',
			{
				onSuccess: function(transport){
				var response = transport.responseText || "Se produjo un error en el servidor";
				$('comercial').innerHTML = response;
			}
		  });	
	
}