// JavaScript Document
var ie = (document.all)? true: false;

function cargarRegistracion(){
	cargarAltaRegalos();
}

function cargarDatosUsuarioRegustracion(){
	if($('regContentElegidos').firstChild && ($('regContentElegidos').firstChild.tagName == 'TABLE' || $('regContentElegidos').childNodes.length > 1)){
		switchMenuOffline(1);
		sumarclick(1);
		new Ajax.Request('new_cargarRegistracion.php',
			{
				//onLoading: showLoad,
				onSuccess: function(transport){
				var response = transport.responseText || "Se produjo un error en el servidor";
				$('divContainer').innerHTML=response;
				switchSideBar(false,false,true);
				$("boton_listas_ambas").style.display = "none";
				$("side_content").innerHTML="<img src=\"img/paso2de2.gif\">";
				
				Event.observe('lnkSiguiente', 'click', validar.curry('recibirRegistracion'), false);
				Event.observe('txtEmail', 'change', validarEmail);	
			}
		});	
	}else{
		cargarVentanaFlotanteOK('Ten&eacute;s que elegir al menos un regalo para poder registrarte. <br>Muchas gracias', null);
	}
}

function recibirRegistracion(email){
	Event.stopObserving('lnkSiguiente');
	serializados = $("frmRegistracion").serialize();
	loading_registracion();
	
	new Ajax.Request('recibirRegistracion.php',
	  {
		  parameters: serializados,
		  onSuccess: validarEntraPorInvitacion.curry(email)
	  });
	
	//cargarAltaLista();
}

function validarEntraPorInvitacion(email){
	new Ajax.Request('cargarUsuariosQueInvitaron.php?email='+email,
	  {
		  onSuccess: function(transport){
				var datos = transport.responseText.evalJSON() || "Se produjo un error en el servidor";
				/*if(datos.length > 0){
					armarAmigosDeInvitadores(datos);
				}else*/{
					//recibirAltaLista();
					cargarAltaLocalesND();
				}
		  }
	  });
}

function armarAmigosDeInvitadores(datos){
	$("divContenido").innerHTML = "";
	
	html = "";
	html += "<br><div class=\"texto\" style=\"margin:20px;font-size:12px;\">";
	html += "Las siguientes personas te invitaron a queleregalarias.com, pod&eacute;s ser amigo de ellos y enterarte de sus novedades haciendo click en el boton \"seamos amigos\".";
	html += "</div><br>";
	
	for(var i = 0; i < datos.length; i++){
		html += "<table class=\"texto\" style=\"margin:20px;\" cellpadding=\"0\" cellspacing=\"0\" >";
		html += "	<tr>";
		html +="    	<td background=\"img/barrapensamiento.jpg\" width=\"424\" height=\"8\" colspan=\"2\"></td>";
		html += "	</tr>";
		html += "	<tr bgcolor=\"#cdecee\" >";
		html += "		<td style=\"padding-left:10px;\">";
		html += 			datos[i].thumb;
		html += "		</td>";
		html += "		<td>";
		html += "			<table class=\"texto\" cellpadding=\"0\" cellspacing=\"0\" >"
		html += "				<tr>";
		html += "					<td>";
		html += 						datos[i].nombre;
		html += "						<br>Mi cumplea&ntilde;os es el " + datos[i].cumpletexto;
		html += "					</td>";
		html += "				</tr>";
		html += "			</table>";
		html += "		</td>";
		html += "	</tr>";
		html += "	<tr>";
		html += "     	<td background=\"img/barrapensamiento_ab.jpg\" width=\"424\" height=\"8\" colspan=\"2\"></td>";
		html += "	</tr>";
		html += "	<tr>";
		html += "		<td align=\"right\" colspan=\"2\">";
		html += "			<span id=\"spnEstado"+datos[i].id+"\" name=\"spnEstado"+datos[i].id+"\"><a href=\"#\" id=\"lnkSeguime"+datos[i].id+"\"><img src=\"img/seamosamigos.gif\" border=\"0\" /></a></span>";
		html += "		</td>";
		html += "	</tr>";
		html += "</table>";
	}
	
	html += "<br><table width=\"100%\"><tr><td align=\"right\" width=\"100%\" style=\"padding-right:10px;\" ><a href=\"#\" id=\"lnkSiguiente2\"><img src=\"img/btn_siguiente.jpg\" border=\"0\" /></a></td></tr></table><br>";
		
	$("divContenido").innerHTML = html;
	
	// Cargo los links
	Event.observe('lnkSiguiente2', 'click', cargarAltaLista, false);
	for(var i = 0; i < datos.length; i++){
		Event.observe('lnkSeguime'+datos[i].id, 'click', recibirSeguime.curry(datos[i].id, datos[i].nombre), false);
	}
	
	
	$('divTitulo').innerHTML="Te invitaron";
	$("divLeftTitulo").innerHTML="Agreg&aacute; a tus amigos";
	$("divLeftTop").innerHTML=unescape(encodeURIComponent("Ellos te invitaron a crear tu lista, ahora agregalos como amigos."));
	$("divLeftBottom").innerHTML=unescape(encodeURIComponent("Pod&eacute;s continuar creando tus listas en el siguiente paso."));
}

/*
function recibirAltaLista(){
	/*new Ajax.Request('new_recibirAltaLista.php?nombrelista=Mi%20lista',
	  {
		  onSuccess: function(transport){
				var datos = transport.responseText.evalJSON();
				cargarAltaRegalos(datos[0].id);
			  }
	  });
}
*/

function cargarAltaRegalos(){//(id_lista){
	sumarclick(2);
	new Ajax.Request('new_cargarAltaRegalos.php',//?id_lista='+id_lista,
			{
				onSuccess: function(transport){
				var response = transport.responseText || "Se produjo un error en el servidor";
				$('divContainer').innerHTML=response;
				switchSideBar(false,false,true);
				$("boton_listas_ambas").style.display = "none";
				$("side_content").innerHTML="<img src=\"img/paso1de2.gif\">";
				
				//$("lnkSiguiente").style.display = "none";

				Event.observe('lnkVolverHome', 'click', cargarHomeND, false);				
				Event.observe("lnkAgregar", 'click', validarPalabras.curry('txtNombreItemPropio', 'spnErrorNombreItem', 0));
				Event.observe('lnkSiguiente', 'click', cargarDatosUsuarioRegustracion, false);//;cargarAltaLocalesND, false);
			}
		});	
}

function vaciarContenidos(){
	$('regContentLocales').innerHTML = "";
	$('regContentItems').innerHTML = "";
}

function cargarContenidoItems(online){
//	retraerIngresarItemPropio();
	
	if(online){
		loading("divLoadItems", 0, 4);
	}else{
		loading("divLoadItems", 0, 1);
	}
	
	$('regContentLocales').innerHTML = "";
	$('regContentItems').innerHTML = "";
	
//	if($("regContentCategorias").value != "otro"){
		$("divLoadItems").style.visibility = "visible";
		new Ajax.Request('new_cargarContenidoItems.php?categoria_id='+$("regContentCategorias").value,
				{
					onSuccess: function(transport){
					var datos = transport.responseText.evalJSON();
	//				$("prueba").innerText = response;
					for(var i=0 ; i < datos.length ; i++){
						var option = new Option();
	
						option.value = parseInt(datos[i].id);
						option.text = datos[i].nombre;
						option.style.color = "#62AEBE";
						option.style.fontWeight = "bolder";
						if(ie){
							$('regContentItems').add(option);
						}else{	
							$('regContentItems').add(option, null);
						}
						
					}
					
					/*
					var option = new Option();
	
					option.value = "otro";
					option.text = "Otros";
					option.style.color = "#51ACBC";
					option.style.fontWeight = "bolder";
					if(ie){
						$('regContentItems').add(option);
					}else{	
						$('regContentItems').add(option, null);
					}
					
					*/
					
					$("divLoadItems").innerHTML = "";
					$("divLoadItems").style.visibility = "hidden";
				}
			});
/*	}else{
		cargarIngresarItemPropio();
	}*/

}

function cargarContenidoLocales(online){
	$('regContentLocales').innerHTML = "";
	contador = 0;
	$("txtNombreItemPropio").value = $("regContentItems").childNodes[$("regContentItems").selectedIndex].text;
	
//	if($('regContentItems').value != "otro"){
		$("divLoadLocales").style.visibility = "visible";
		
		if(online){
			loading("divLoadLocales", 0, 4);
		}else{
			loading("divLoadLocales", 0, 1);
		}
		
		//retraerIngresarItemPropio();
		
		new Ajax.Request('new_cargarContenidoLocales.php?item_preset_id='+$("regContentItems").value,
				{
					onSuccess: function(transport){;
					var datos = transport.responseText.evalJSON();
	//				$("prueba").innerText = response;
					for(var i=0 ; i < datos.length ; i++){
						var option = new Option();
	
						option.value = parseInt(datos[i].id);
						option.text = datos[i].nombre;
						option.style.color = "#62AEBE";
						option.style.fontWeight = "bolder";
						if(ie){
							$('regContentLocales').add(option);
						}else{	
							$('regContentLocales').add(option, null);
						}
						
					}
					
					$("divLoadLocales").innerHTML = "";
					$("divLoadLocales").style.visibility = "hidden";
	//				$('regContentLocales').innerHTML=response;
				}
			});	
/*	}else{
		cargarIngresarItemPropio();
	}*/
}

function cargarContenidoItemsAlta(){
	$("regContentElegidos").value = "";
	
	// habilito el boton siguiente para continuar con la registracion
/*	if($("lnkSiguiente")){
		$("lnkSiguiente").style.display = "";
	}
*/	
	if($("id_lista")){
		var parametros ='?lista_id='+$("id_lista").value;
	}else{
		var parametros = '';
	}
	
	new Ajax.Request('new_cargarContenidoItemsAlta.php'+parametros,
			{
				onSuccess: function(transport){
				var response = transport.responseText || "Se produjo un error en el servidor";
				$('regContentElegidos').innerHTML=response;
			}
		});	
}
/*
function cargarAltaItems(transport){
	$('divLoad').innerHTML="";
	var datos = transport.responseText.evalJSON();
	cargarDesplegarItems('Registro', datos[0].id, datos[0].nombre);
//	cargarItems(datos[0].id, datos[0].nombre);
	$('divTitulo').innerHTML="Tus regalos";
	$("divLeftTitulo").innerHTML="Ingres&aacute; tus regalos";
	$("divLeftTop").innerHTML=unescape(encodeURIComponent("Ya le dimos un nombre a tu lista, ahora agreg&aacute; los regalos que quieras."));
	$("divLeftBottom").innerHTML=unescape(encodeURIComponent("Cuando termines, hac&eacute; click en siguiente."));
}
*/

function cargarAltaLocalesND(){
	sumarclick(4);
	new Ajax.Request('new_cargarAltaLocales.php',
			{
				onSuccess: function(transport){
				var response = transport.responseText || "Se produjo un error en el servidor";
				$('divContainer').innerHTML=response;
				$("side_content").innerHTML="<img src=\"img/paso3.gif\">";
				
				cargarMarcarLocalesND('xxxx');
			}
		});	
}


function cargarMarcarLocalesND(filtro){
new Ajax.Request('cargarMarcarLocales.php?filtro='+filtro.value,
{
	onSuccess: function(transport){
		var datos = transport.responseText.evalJSON();
		
		Event.stopObserving('lnkFiltrar');
//		Event.observe('lnkSiguiente', 'click', cargarInvitarAmigosRed, false);
		Event.observe('lnkSiguiente', 'click', cargarInvitarAmigosRed.curry('registrarse', ''), false);
		
		generarTablaND(datos, filtro);
	}
//	onFailure: function(){ alert('Se produjo un error en el servidor');} 
});
}


function generarTablaND(datos, filtro){
	var col=0;

	var html="<form id=\"frmMarcarLocales\" name=\"frmMarcarLocales\" style=\"margin-bottom:0px;\">";
	html+="<table cellpadding=\"0\" cellspacing=\"7\">";

	for(i=0; i<datos.length; i++){

		if(col==0){
			html+="<tr>";
		}
		html+="<td id=\"td"+datos[i].id+"\" background=\"pics/"+datos[i].thumb+"\" width=\"76\" align=\"right\" valign=\"bottom\" height=\"76\" title=\""+datos[i].nombre+"\" style=\"background-repeat:no-repeat\">";

		if(datos[i].selected!=0){
			html+="<img src=\"img/tick.jpg\" height=\"12\" width=\"12\" border=\"0\" />";
		}

		col++;
		if(col==6){
			html+="</td></tr><tr><td colspan=\"6\" height=\"4\"></td></tr>";
			col=0;
		}else{
			html+="</td><td width=\"4\"></td>";
		}

	}

	html+="</table></form>";

	$("divMarcarLocales").innerHTML=html;
	html="";
	
	Event.stopObserving('txtFiltrarLocales');
	new Ajax.Autocompleter("txtFiltrarLocales", "txtAutocompleteLocales", "locales.php", {paramName:"caracteres"});
	
	if(filtro && filtro!='xxxx'){
		$("divNoFiltrar").innerHTML="<a href=\"#\" id=\"lnkNoFiltrar\" class=\"lnk_blanco\">ver todos</a>";
	}else{
		$("divNoFiltrar").innerHTML="";
	}

	
	var par_filtrar=cargarMarcarLocalesND.curry($('txtFiltrarLocales'));
	Event.stopObserving('lnkFiltrar');
	Event.observe('lnkFiltrar', 'click', par_filtrar);

	if(filtro && filtro!='xxxx'){
		Event.observe('lnkNoFiltrar', 'click', cargarMarcarLocalesND.curry(''), false);
	}
	
	for(i=0; i<datos.length; i++){
		var parametros=recibirMarcarLocalesND.curry(datos, filtro, i);
		Event.observe('td'+datos[i].id, 'click', parametros);
	}
}


function recibirMarcarLocalesND(datos, filtro, i){
	new Ajax.Request('recibirMarcarLocales.php?local_id='+datos[i].id,
	{
		onSuccess: function(transport){
			var response = transport.responseText || "Se produjo un error en el servidor";
			if(response=="INSERT"){
				$('td'+datos[i].id).innerHTML="<img src=\"img/tick.jpg\" height=\"12\" width=\"12\" border=\"0\" />";
			}else{
				$('td'+datos[i].id).innerHTML="";
			}
		}
	});
}

function cargarInvitarAmigosRed(ubicacion, param_url){
	$("boton_listas_ambas").style.display = "none";
	new Ajax.Request('new_cargarInvitarAmigosRed.php',
	{
		onSuccess: function(transport){
			var response = transport.responseText || "Se produjo un error en el servidor";
			$("divContainer").innerHTML = response;
			
				
			new Ajax.Request(' ./guiye/OpenInviter/example.php',
			{
				method: 'get',
				onSuccess: function(transport){
					var response = transport.responseText || "Se produjo un error en el servidor";
					$("formInvitarContactos").innerHTML = response;
					
					Event.observe('btnImportarContactos', 'click', window.parent.cargarBuscarContactosRed.curry(ubicacion, param_url), false);
				}
			});
			
			
				
			switch(ubicacion){
				case 'registrarse':
					sumarclick(5);
					$('lnkSiguiente').innerHTML = "<img src=\"img/btn_pasar.jpg\" border=\"0\"/>";
					Event.observe('lnkSiguiente', 'click', cargarRegistracionSatisfactoria,false);
					$("side_content").innerHTML="<img src=\"img/paso4.gif\">";
					break;
				case 'buscador':
					$('lnkSiguiente').innerHTML = "<img src=\"img/btn_volver.jpg\" border=\"0\"/>";
					Event.observe('lnkSiguiente', 'click', recibirBuscadorND.curry(param_url),false);
					break;
				default:
					sumarclick(18);
					$('lnkSiguiente').innerHTML = "<img src=\"img/btn_volver.jpg\" border=\"0\"/>";
					Event.observe('lnkSiguiente', 'click', sumarclick.curry(19));
					Event.observe('lnkSiguiente', 'click', cargarHomeND);
					//$("lnkSaltear").style.display = "none";
					break;
			}
			//Event.observe('lnkSiguiente', 'click', cargarRegistracionSatisfactoria, false);
			Event.observe('enviarMailAmigo', 'click', cargarEnviarAmigo.curry(ubicacion, param_url), false);
		}
	});
}

function cargarBuscarContactosRed(ubicacion, param_url){
	valores = $("frmInviteFriends").serialize();
	if($("formInvitarContactos")){
		loading("formInvitarContactos", 100, 3);
	}
	//document.forms.openinviter.submit()	
	new Ajax.Request('./guiye/OpenInviter/example.php',
	{
		parameters: valores,
		onSuccess: function(transport){
			var response = transport.responseText || "Se produjo un error en el servidor";
			new Ajax.Request('new_cargarInvitarAmigosRedResultados.php',
				{
					onSuccess: function(transport){
						var response2 = transport.responseText || "Se produjo un error en el servidor";
						$("divContainer").innerHTML = response2;
						$("divResultados").innerHTML = response;
				
						if($('send') && $('send2')){
							Event.observe('send', 'click', window.parent.cargarBuscarContactosRed.curry(ubicacion, param_url));
							Event.observe('send2', 'click', window.parent.cargarBuscarContactosRed.curry(ubicacion, param_url));
						}
						switch(ubicacion){
							case 'registrarse':
								sumarclick(9);
								$('lnkSiguiente').innerHTML = "<img src=\"img/btn_siguiente.jpg\" border=\"0\"/>";
								Event.observe('lnkSiguiente', 'click', cargarRegistracionSatisfactoria,false);
								Event.observe('lnkVolver', 'click', cargarInvitarAmigosRed.curry(ubicacion, param_url));
								break;
							case 'buscador':
								$('lnkSiguiente').innerHTML = "<img src=\"img/btn_volver.jpg\" border=\"0\"/>";
								Event.observe('lnkSiguiente', 'click', cargarInvitarAmigosRed.curry(ubicacion, param_url),false);
								$("lnkVolver").innerHTML = "";
								break;
							default:
								$('lnkSiguiente').innerHTML = "<img src=\"img/btn_volver.jpg\" border=\"0\"/>";
								Event.observe('lnkSiguiente', 'click', cargarInvitarAmigosRed.curry(ubicacion, param_url));
								$("lnkVolver").innerHTML = "";
								//$("lnkSaltear").style.display = "none";
								break;
						}
						
					}
				});
		}
	});
}


function cargarRegistracionSatisfactoria(){
	sumarclick(7);
	$('side_content').innerHTML = "";
	new Ajax.Request('new_cargarRegistracionSatisfactoria.php',
	{
		onSuccess: function(transport){
			var response = transport.responseText || "Se produjo un error en el servidor";
			$("divContainer").innerHTML = response;
			$("side_content").innerHTML="<img src=\"img/paso5.gif\">";
			
			recibirEnviarHabilitacion("");
			
//			setTimeout(cargarHomeND, 6000);
			Event.observe("lnkHome", 'click', cargarHomeND);
		}
	});
}

function loading_registracion(){
	content = '<div style="padding:20px;width:638px;height:280px;margin-bottom:10px;text-align:center;" id="divCargando"></div>';
	$("divContainer").innerHTML = content;
	loading("divCargando", 100, 4);
}