﻿/**
LOGIN
**/

ESTA_ONLINE_url = window.location.toString();
ESTA_ONLINE_partes = ESTA_ONLINE_url.split('.')
ESTA_ONLINE_breadcrumb = ESTA_ONLINE_partes[0].replace('http://','');

if( ESTA_ONLINE_breadcrumb == 'nkotb' ){ // Offline
	//KIDS_TOPHAT_DBHOST_URL = 'http://admin.nkotb.discovery.bhdm.com.ar/PHPFRONT/tophat/';
	KIDS_TOPHAT_DBHOST_URL = 'http://origin-apps.tudiscoverykids.com/tophat/';
}else{ //Online
	KIDS_TOPHAT_DBHOST_URL = 'http://origin-apps.tudiscoverykids.com/tophat/';
}

ESTA_ONLINE_url_sin_http = ESTA_ONLINE_url.replace('http://','')
ESTA_ONLINE_partes_slash = ESTA_ONLINE_url_sin_http.split('/')


/*
secciones_que_muestran_topHat = new Array('video','party_theme','party%5Ftheme','users','juegos','programacion','actividades','padres','personajes','especiales','fiesta_dkids','fiesta%5Fdkids');
if( in_array(secciones_que_muestran_topHat, ESTA_ONLINE_partes_slash[1]) ) var mostrarTOPHAT = 1;
else var mostrarTOPHAT = 0;
*/

mostrarTOPHAT = 1;


function in_array(array,elemento){
	for(var i in array){ if(array[i]==elemento) return true;}
	return false;
}

function topHatRegistrar(displayname, password, nombre, apellido, sexo, email, pais, optins, optout){
	returnPath = readCookie("KIDS_TOPHAT_return") || null;
	$.getJSON( KIDS_TOPHAT_DBHOST_URL + 'registrar.php?nombre='+escape(nombre)+'&apellido='+escape(apellido)+'&sexo='+escape(sexo)+'&email='+escape(email)+'&pais='+escape(pais)+'&displayname='+escape(displayname)+'&password='+escape(password)+'&optins='+escape(optins)+'&optout='+escape(optout)+'&jsoncallback=?', function(datos){
		if( datos.errornum == '400' ){
			ocultar_msj_error('formulario_preload_label');
			//$('#formulario_no_usuario_label').html(datos.error);
			//$('#formulario_no_usuario_label').html(datos.error+'<hr>'+datos.url);	
			mostrar_msj_error('formulario_no_usuario_label');
		}
		//alert(datos.codigo+'-'+datos.token);
		if( datos.errornum == '100' ){
			recordame = null;
			writeCookie("KIDS_TOPHAT_codigoUsuarioPadre", datos.codigo, recordame, "/");
			writeCookie("KIDS_TOPHAT_CASTGCUsuarioPadre", datos.CASTGC, recordame, "/");
			writeCookie("KIDS_TOPHAT_tokenUsuarioPadre", datos.token, recordame, "/");
			writeCookie("KIDS_TOPHAT_nombreUsuarioPadre", datos.displayname, recordame, "/");
			writeCookie("KIDS_TOPHAT_hijosUsuarioPadre", '', recordame, "/");				

			if( returnPath == null ){
				location.href = 'hijos.shtml?r=REw'+datos.token+'SA1'; //Rand
			}else{
				writeCookie("KIDS_TOPHAT_return", null, null, "/");
				location.href = returnPath
			}			
		}
	});
}

function topHatLogin(){
	$('#preload_tophat').slideToggle('fast', function(){
		email = $('#KIDS_TOPHAT_input_usuario').val();
		password =  $('#KIDS_TOPHAT_input_clave').val();
		
		returnPath = readCookie("KIDS_TOPHAT_return") || null;
		
		var recordame = document.getElementById('KIDS_TOPHAT_input_recordame').checked == true ? 720 : null;
		var emailcheck1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; 
		var emailcheck2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	
		if(emailcheck1.test( email ) || !emailcheck2.test( email ) ){
			alert('Escreva um endereço de e-mail');
			topHatPreload();
			return false;
		}
		if(password == '' || password == 'senha' ){
			alert('Escreva uma senha');
			topHatPreload();
			return false;
		}
		$.getJSON( KIDS_TOPHAT_DBHOST_URL + 'login.php?email='+ escape(email) +'&password='+ escape(password) +'&jsoncallback=?', function(datos){
			
			if( datos.errornum == '400' ){
				alert('O usuário fornecido não é válido.');
				topHatPreload();
				return false;
			}
	
			if( datos.errornum == '100' ){
				writeCookie("KIDS_TOPHAT_recordameUsuarioPadre", (recordame==null?'':recordame), recordame, "/");

				writeCookie("KIDS_TOPHAT_codigoUsuarioPadre", datos.codigo, recordame, "/");
				writeCookie("KIDS_TOPHAT_CASTGCUsuarioPadre", datos.CASTGC, recordame, "/");
				writeCookie("KIDS_TOPHAT_tokenUsuarioPadre", datos.token, recordame, "/");
				writeCookie("KIDS_TOPHAT_nombreUsuarioPadre", datos.displayname, recordame, "/");
				writeCookie("KIDS_TOPHAT_hijosUsuarioPadre", datos.hijos, recordame, "/");	
				
				
				//Tomo el primer hijo y lo logueo
				arrayHijos1 = datos.hijos.split(',');
				//alert(arrayHijos1.length+'-'+datos.hijos);
				if( arrayHijos1.length == 1 ){ // Un solo hijo, lo logueo automáticamente
					arrayHijos2 = arrayHijos1[0].split('|');
					if( arrayHijos2.length == 5 ){
						if( arrayHijos2[2].length == 32 ){
							writeCookie("KIDS_TOPHAT_codigoUsuarioHijo", arrayHijos2[0], recordame, "/");
							writeCookie("KIDS_TOPHAT_nombreUsuarioHijo", arrayHijos2[1], recordame, "/");			
							writeCookie("KIDS_TOPHAT_tokenUsuarioHijo", arrayHijos2[2], recordame, "/");
							writeCookie("KIDS_TOPHAT_sexoUsuarioHijo", arrayHijos2[3], recordame, "/");
							writeCookie("KIDS_TOPHAT_nacimientoUsuarioHijo", arrayHijos2[4], recordame, "/");
						}
					}
					topHatPreload();
					if( mostrarTOPHAT == 1 ){
						topHatCargarHeaderLogin();
					}else if(returnPath != null){
						writeCookie("KIDS_TOPHAT_return", null, null, "/");
						location.href = returnPath;
					}else{
						location.href = '/users/hijos.shtml';
					}
				}else{
					//Sino lo mando a hijos
					if( returnPath == null ){
						location.href = '/users/hijos.shtml';
					}else{
						writeCookie("KIDS_TOPHAT_return", null, null, "/");
						location.href = returnPath
					}
				}
			}
		});
	}); //Del preload												  }}
}



function topHatAgregarHijo(nombre, nacimiento, sexo ){
	var usuario = readCookie('KIDS_TOPHAT_codigoUsuarioPadre') || "";
	var token = readCookie('KIDS_TOPHAT_tokenUsuarioPadre') || "";
	var recordame = readCookie("KIDS_TOPHAT_recordameUsuarioPadre") || null;

	$.getJSON( KIDS_TOPHAT_DBHOST_URL + 'hijos_registrar.php?usuario='+escape(usuario)+'&token='+escape(token)+'&nombre='+escape(nombre)+'&nacimiento='+escape(nacimiento)+'&sexo='+escape(sexo)+'&jsoncallback=?', function(datos){
		if( datos.errornum == '400' ){
			ocultar_msj_error('formulario_preload_label');
			mostrar_msj_error('formulario_no_usuario_label');
		}
		if( datos.errornum == '300' ){
			ocultar_msj_error('formulario_preload_label');
			mostrar_msj_error('formulario_datos_incorrectos_label');
		}		
		if( datos.errornum == '100' ){
			writeCookie("KIDS_TOPHAT_hijosUsuarioPadre", datos.hijos, recordame, "/");	
			ocultar_msj_error('formulario_contenido');
			var html = $('#formulario_usuario_ok').html();
			html = html.replace('%usuario%',nombre);
			$('#formulario_usuario_ok').html( html );

			ocultar_msj_error('botones_enviar-canelar');
			mostrar_msj_error('botones_volver');
			
			mostrar_msj_error('formulario_usuario_ok');
			ocultar_msj_error('formulario_contenido');
			ocultar_msj_error('formulario_preload_label');
		}
	});
}
function topHatModificarHijo(nombre, nacimiento, sexo ){
	var usuario = readCookie('KIDS_TOPHAT_codigoUsuarioPadre') || "";
	var token = readCookie('KIDS_TOPHAT_tokenUsuarioPadre') || "";
	var recordame = readCookie("KIDS_TOPHAT_recordameUsuarioPadre") || null;
	
	hijo = hijo = getQueryVariable('h') || '';	
	existeHijo = 0;
	arrayHijos1 = hijos.split(',');
	for( i=0; i<arrayHijos1.length; i++ ){ // 
		arrayHijos2 = arrayHijos1[i].split('|');
		if( arrayHijos2.length == 5 && arrayHijos2[0] == hijo ){
			if( arrayHijos2[2].length == 32 ){
				tokenHijo = arrayHijos2[2];
				existeHijo = 1;
			}
		}
	}	
	if( existeHijo == 0 ) location.href = 'hijos.shtml';
	$.getJSON( KIDS_TOPHAT_DBHOST_URL + 'hijos_actualizar.php?tokenHijo='+escape(tokenHijo)+'&hijo='+escape(hijo)+'&usuario='+escape(usuario)+'&token='+escape(token)+'&nombre='+escape(nombre)+'&nacimiento='+escape(nacimiento)+'&sexo='+escape(sexo)+'&jsoncallback=?', function(datos){
		if( datos.errornum == '400' ){
			ocultar_msj_error('formulario_preload_label');
			mostrar_msj_error('formulario_no_usuario_label');
		}
		if( datos.errornum == '300' ){
			ocultar_msj_error('formulario_preload_label');
			mostrar_msj_error('formulario_datos_incorrectos_label');
		}
		if( datos.errornum == '100' ){
			writeCookie("KIDS_TOPHAT_hijosUsuarioPadre", datos.hijos, recordame, "/");	

			hijoActual = readCookie("KIDS_TOPHAT_codigoUsuarioHijo") || 0; 
			if( hijo == hijoActual ) topHatSetearHijo(hijo, nombre, tokenHijo, sexo, 0);

			
			ocultar_msj_error('formulario_contenido');
			var html = $('#formulario_usuario_ok').html();
			html = html.replace('%usuario%',nombre);
			$('#formulario_usuario_ok').html( html );
			mostrar_msj_error('formulario_usuario_ok');
	
			ocultar_msj_error('botones_enviar-canelar');
			mostrar_msj_error('botones_volver');
			
			
			ocultar_msj_error('formulario_contenido');
			ocultar_msj_error('formulario_preload_label');
			
			
		}
	});
}

function topHatEliminarHijo(hijo, tokenHijo, nombre){
	var mensaje = $('#mensaje_borrar_hijo').html();	
	mensaje = mensaje.replace('%nombre%',nombre);
	var recordame = readCookie("KIDS_TOPHAT_recordameUsuarioPadre") || null;
	if( confirm(mensaje) ){  
		var usuario = readCookie('KIDS_TOPHAT_codigoUsuarioPadre') || "";
		var token = readCookie('KIDS_TOPHAT_tokenUsuarioPadre') || "";	
		$.getJSON( KIDS_TOPHAT_DBHOST_URL + 'hijos_borrar.php?hijo='+escape(hijo)+'&usuario='+escape(usuario)+'&tokenHijo='+escape(tokenHijo)+'&token='+escape(token)+'&jsoncallback=?', function(datos){
			if( datos.errornum == '100' ){
				writeCookie("KIDS_TOPHAT_hijosUsuarioPadre", datos.hijos, recordame, "/");
				location.href = '/users/hijos.shtml';
			}
		});
	}else{
		return false;	
	}
}

function topHatPreload(){
	$('#preload_tophat').slideToggle('fast');
}

function topHatLogout(){
//	alert(readCookie('KIDS_TOPHAT_codigoUsuarioPadre'));
	writeCookie("KIDS_TOPHAT_codigoUsuarioPadre", "", null, "/");
//	alert(readCookie('KIDS_TOPHAT_codigoUsuarioPadre'));
	writeCookie("KIDS_TOPHAT_CASTGCUsuarioPadre", "", null, "/");
	writeCookie("KIDS_TOPHAT_tokenUsuarioPadre", "", null, "/");
	writeCookie("KIDS_TOPHAT_nombreUsuarioPadre", "", null, "/");
	writeCookie("KIDS_TOPHAT_hijosUsuarioPadre", "", null, "/");	
	writeCookie("KIDS_TOPHAT_codigoUsuarioHijo", "", null, "/");
	writeCookie("KIDS_TOPHAT_nombreUsuarioHijo", "", null, "/");			
	writeCookie("KIDS_TOPHAT_tokenUsuarioHijo", "", null, "/");
	//topHatCargarHeaderLogin();
	writeCookie("KIDS_TOPHAT_return", location.href, null, "/");
	location.href='/users/login.shtml';
}

function topHatsetearValoresHijo(){
	hijo = hijo = getQueryVariable('h') || '';
	hijos = readCookie("KIDS_TOPHAT_hijosUsuarioPadre") || "";	
	existeHijo = 0;
	//Tomo el primer hijo y lo logueo
	arrayHijos1 = hijos.split(',');
	for( i=0; i<arrayHijos1.length; i++ ){ // 
		arrayHijos2 = arrayHijos1[i].split('|');
		if( arrayHijos2.length == 5 && arrayHijos2[0] == hijo ){
			if( arrayHijos2[2].length == 32 ){
				existeHijo = 1;
				$('#nombre').val(arrayHijos2[1]);
				$("#formulario input:radio[name=sexo][value="+arrayHijos2[3]+"]").attr("checked","checked");
				fecha = arrayHijos2[4].split('-');
				if( fecha.length == 3 ){
					$('#nacimiento_dia').val(fecha[2]);
					$('#nacimiento_mes').val(fecha[1]);
					$('#nacimiento_anio').val(fecha[0]);
				}
			}
		}
	}	
	if( existeHijo == 0 ) location.href = 'hijos.shtml';
}

function topHatSetearHijo(codigo, nombre, token, sexo, recargarPagina ){
	recordame = readCookie("KIDS_TOPHAT_recordameUsuarioPadre") || null;	
	writeCookie("KIDS_TOPHAT_codigoUsuarioHijo", codigo, recordame, "/");
	writeCookie("KIDS_TOPHAT_nombreUsuarioHijo", nombre, recordame, "/");			
	writeCookie("KIDS_TOPHAT_tokenUsuarioHijo", token, recordame, "/");
	writeCookie("KIDS_TOPHAT_sexoUsuarioHijo", sexo, recordame, "/");
	if( recargarPagina == 1 ) location.reload();
}
function topHatCargarHeaderLogin(){
	KIDS_TOPHAT_tokenUsuarioPadre = readCookie("KIDS_TOPHAT_tokenUsuarioPadre") || "";
	KIDS_TOPHAT_codigoUsuarioPadre = readCookie("KIDS_TOPHAT_codigoUsuarioPadre") || "";
	KIDS_TOPHAT_nombreUsuarioPadre = readCookie("KIDS_TOPHAT_nombreUsuarioPadre") || "";
	
	KIDS_TOPHAT_tokenUsuarioHijo = readCookie("KIDS_TOPHAT_tokenUsuarioHijo") || "";
	KIDS_TOPHAT_codigoUsuarioHijo = readCookie("KIDS_TOPHAT_codigoUsuarioHijo") || "";
	KIDS_TOPHAT_nombreUsuarioHijo = readCookie("KIDS_TOPHAT_nombreUsuarioHijo") || "";

	
	if( KIDS_TOPHAT_tokenUsuarioPadre == "" || KIDS_TOPHAT_codigoUsuarioPadre == "" || KIDS_TOPHAT_nombreUsuarioPadre == "" ){
		$('#contenedor_login_ok').hide();
		
		if( mostrarTOPHAT == 1 ){
			$('#contenedor_login_ko').show();
		}else{
			$('#contenedor_login_ko').remove();
			$('#contenedor_login_bobo').html('<div id="preload_tophat"></div>');
		}
		
		$('#contenedor_login_usuario').html('');
	}else{
		
		if( mostrarTOPHAT == 1 ) $('#contenedor_login_ok').show();
		else $('#contenedor_login_ok').remove();
		
		$('#contenedor_login_ko').hide();
		$('#contenedor_login_usuario').html(KIDS_TOPHAT_nombreUsuarioPadre);

		//Contenedor hijo
		if( KIDS_TOPHAT_tokenUsuarioHijo == "" || KIDS_TOPHAT_codigoUsuarioHijo == "" || KIDS_TOPHAT_nombreUsuarioHijo == "" ){
			$('#contenedor_login_hijo_ok').hide();
			$('#contenedor_login_usuario_hijo').html(KIDS_TOPHAT_nombreUsuarioHijo);
		}else{
			$('#contenedor_login_hijo_ok').show();
			$('#contenedor_login_usuario_hijo').html(KIDS_TOPHAT_nombreUsuarioHijo);
		}
	}
}


function topHatQueEsteLogueado(){
	KIDS_TOPHAT_tokenUsuarioPadre = readCookie("KIDS_TOPHAT_tokenUsuarioPadre") || "";
	KIDS_TOPHAT_codigoUsuarioPadre = readCookie("KIDS_TOPHAT_codigoUsuarioPadre") || "";
	KIDS_TOPHAT_nombreUsuarioPadre = readCookie("KIDS_TOPHAT_nombreUsuarioPadre") || "";
	
	KIDS_TOPHAT_tokenUsuarioHijo = readCookie("KIDS_TOPHAT_tokenUsuarioHijo") || "";
	KIDS_TOPHAT_codigoUsuarioHijo = readCookie("KIDS_TOPHAT_codigoUsuarioHijo") || "";
	KIDS_TOPHAT_nombreUsuarioHijo = readCookie("KIDS_TOPHAT_nombreUsuarioHijo") || "";

	if( KIDS_TOPHAT_tokenUsuarioPadre == "" || KIDS_TOPHAT_codigoUsuarioPadre == "" || KIDS_TOPHAT_nombreUsuarioPadre == "" ){
		writeCookie("KIDS_TOPHAT_return", location.href, null, "/");
		location.href = '/users/login.shtml';	
		return false;
	}else{
		//nothing
		return true;
	}
}
function topHatQueNoEsteLogueado(){
	KIDS_TOPHAT_tokenUsuarioPadre = readCookie("KIDS_TOPHAT_tokenUsuarioPadre") || "";
	KIDS_TOPHAT_codigoUsuarioPadre = readCookie("KIDS_TOPHAT_codigoUsuarioPadre") || "";
	KIDS_TOPHAT_nombreUsuarioPadre = readCookie("KIDS_TOPHAT_nombreUsuarioPadre") || "";
	
	KIDS_TOPHAT_tokenUsuarioHijo = readCookie("KIDS_TOPHAT_tokenUsuarioHijo") || "";
	KIDS_TOPHAT_codigoUsuarioHijo = readCookie("KIDS_TOPHAT_codigoUsuarioHijo") || "";
	KIDS_TOPHAT_nombreUsuarioHijo = readCookie("KIDS_TOPHAT_nombreUsuarioHijo") || "";

	
	if( KIDS_TOPHAT_tokenUsuarioPadre == "" || KIDS_TOPHAT_codigoUsuarioPadre == "" || KIDS_TOPHAT_nombreUsuarioPadre == "" ){
		//Nothing			
	}else{
		location.href = '/users/hijos.shtml';
	}
}

function tophatCambiarCampoPassword( aque ){
	if( aque == 'text'){
		$('#KIDS_TOPHAT_contenedor_input_clave').html('<input type="text" name="KIDS_TOPHAT_input_clave" value="senha" id="KIDS_TOPHAT_input_clave" onBlur="if(this.value==\'\'){tophatCambiarCampoPassword(\'text\');}" onFocus="if(this.value==\'senha\'){tophatCambiarCampoPassword(\'password\')}" class="KIDS_TOPHAT_input" />');
	}else{
		$('#KIDS_TOPHAT_contenedor_input_clave').html('<input type="password" name="KIDS_TOPHAT_input_clave" value="" id="KIDS_TOPHAT_input_clave" onBlur="if(this.value==\'\'){tophatCambiarCampoPassword(\'text\');}" onFocus="if(this.value==\'senha\'){tophatCambiarCampoPassword(\'password\')}" class="KIDS_TOPHAT_input" />');
		setTimeout("$('#KIDS_TOPHAT_input_clave').focus();",100);
	}
}

function preload(){
	$('#contenido_preload').hide('fast',function(){
		$('#contenido').show();
		}
	);	
}
function mostrar_msj_error(obj_id){
	$('#'+obj_id).show(); 
}
function ocultar_msj_error(obj_id){
	$('#'+obj_id).hide(); 
}
function ocultar_msjs_error(){
	$('.label_mensajes_errores').hide(); 
}

$(document).ready(function(){
	topHatCargarHeaderLogin();
});