$(document).ready(function(){
  SexyLightbox.initialize({color:'white', dir: 'img/sexyimages'});
});

function showAlert(strUrl, iAncho, iAlto){
	var URL_ROOT_CF = "";
	if(URL_ROOT.indexOf('hugo') == -1){
		URL_ROOT_CF = URL_ROOT.substr(0, URL_ROOT.length - 3) + URL_ROOT.substr(URL_ROOT.length - 4) + "/";
	}

	if(iAncho==undefined) iAncho = 460;
	if(iAlto==undefined) iAlto = 180;
	if(!isNaN(strUrl)) strUrl = strUrl.toString();

	if(strUrl.indexOf('.php')==-1 || (strUrl.length > 50 && strUrl.indexOf('popup-imagen.php')==-1 && strUrl.indexOf('alertas.php')==-1)){
		strUrl = URL_ROOT_CF + "alertas.php?alerta=" + escape(strUrl);
	}

	if(strUrl.indexOf('?')==-1){
		strUrl += '?';
	}else{
		strUrl += '&';
	}
	
	strUrl += 'height=' + iAlto + '&width=' + iAncho;
	if(document.getElementById('divAvisos') != null){
		$("#divAvisos").load(strUrl);
		window.scrollTo(0,0);
	}else{
		SexyLightbox.display(strUrl, '', true);
	}
}

function controlarSesion(strCallback){
	$.get("ajaxComprobarSesion.php", { callback: strCallback },
	  function(data){
	    eval(data);
	  });
}

function mostrarLoginAjax(){
	var URL_ROOT_CF = "";
	if(URL_ROOT.indexOf('hugo') == -1){
		URL_ROOT_CF = URL_ROOT.substr(0, URL_ROOT.length - 3) + URL_ROOT.substr(URL_ROOT.length - 4) + "/";
	}
	showAlert(URL_ROOT_CF + 'alertas.php?alerta=12', 600, 270);
}

function validaLogin(tipoLogin){

	if(tipoLogin==undefined) tipoLogin = '1';
  var strError = "";
  var objCheck = new check("login" + tipoLogin);
  objCheck.checkString("txtUsuario" + tipoLogin, "Usuario", 2, 50, false);
  objCheck.checkString("txtContrasenia" + tipoLogin, "Contraseña", 2, 50, false);
  strError = objCheck.toString();
  if (strError == "") {
		return true;
  } else {
    showAlert (strError.replace(/\n/g,'<br/>'));
    return false;
  }
}

function EnvioPin(flag){
	if(flag){
		if(document.getElementById('txtPinCode').value!=""){
			document.frmPincode.submit();
		}else{
			showAlert (9);
		}
	}else{
		mensajeLoginPincode();
	}
}

function mensajeLoginPincode(){
  showAlert (10);
  return false;
}

function loginLightbox(){
	var frm = document.frmLoginLightbox;
  var strError = "";
  var objCheck;

  objCheck = new check("frmLoginLightbox");
  objCheck.checkEmail("txtUsuario3", "Usuario", 2, 150, false);
  objCheck.checkString("txtContrasenia3", "Contraseña", 2, 50, false);
  strError = objCheck.toString();
  if (strError == "") {
  	$("#hidCampoErrorLogin").html("");
    frm.hidLogin.value = 3;
    return true;
  } else {
  	$("#hidCampoErrorLogin").html(strError.replace(/\n/g,'<br/>'));
  	return false;
  }
}

function recordar(){
	var frm = document.clave;
  var strError = "";
  var objCheck;

  objCheck = new check("clave");
  objCheck.checkEmail("email", "email", 2, 255, false);
  strError = objCheck.toString();
  if (strError == "") {
  	$("#campoErrorRecordar").html("<img src=\"" + $("#hidHostRoot").val() + "img/sexyimages/black/loading.gif\" border=\"0\" />");
		return true;
  } else {
  	$("#campoErrorRecordar").html(strError.replace(/\n/g,'<br/>'));
    return false;
  }
}

function enviarPinCode(){
	if($("#txtPinCode").val() != ""){
		return true;
	}else{
		return false;
	}
}

function guardarPerfil(){
	var frm = document.registro;
  var strError = "";
  var objCheck;

  objCheck = new check("registro");
  objCheck.checkEmail("email", "Email", 2, 100, false);
  objCheck.checkString("confirmarContrasena", "Confirmar Contraseña", 2, 50, false);
  objCheck.checkString("movil", "Teléfono Móvil", 2, 30, true);
  objCheck.checkString("apellidos1", "Apellidos", 2, 35, true);
  objCheck.checkString("apellidos2", "Apellidos", 2, 35, true);
  objCheck.checkString("contrasena", "Contraseña", 2, 35, false);
  objCheck.checkString("telFijo", "Teléfono Fijo", 2, 30, true);
  objCheck.checkString("nombre", "Nombre", 2, 35, true);
  objCheck.checkString("apodo", "Apodo", 2, 35, false);
  objCheck.checkDate("txtFechaNacimiento", "Fecha de Nacimiento", true);
  objCheck.checkString("numero", "Número", 0, 10, true);
  objCheck.checkString("provincia", "Provincia", 2, 50, true);
  objCheck.checkString("codigo", "Código postal", 2, 10, true);
  objCheck.checkString("dni", "DNI", 2, 20, true);
  objCheck.checkString("direccion", "Dirección", 2, 200, true);
  objCheck.checkString("restoDireccion", "Resto Dirección", 2, 100, true);
  objCheck.checkString("localidad", "Localidad", 2, 40, true);
  strError = objCheck.toString();
  
  if(frm.contrasena.value!=frm.confirmarContrasena.value){
  	strError += "Las contraseñas no coinciden.\n";
  }
  if(!frm.acepto.checked){
  	strError += "Debe aceptar los términos y condiciones.\n";
  }
  if (strError == "") {
    return true;
  } else {
    showAlert (strError.replace(/\n/g,'<br/>'));
    return false;
  }
}     	

function enviarMensaje(){
	var frm = document.contacto;
  var strError = "";
  var objCheck;

 
  objCheck = new check("contacto");
  objCheck.checkString("nombre", "Nombre", 2, 35, false);
  objCheck.checkString("apelido", "Apellidos", 2, 35, true);
  objCheck.checkString("apelido2", "Apellidos", 2, 35, true);
  objCheck.checkEmail("email", "Email", 2, 100, false);
  objCheck.checkString("telefono", "Teléfono Fijo", 2, 30, true);
  objCheck.checkString("movil", "Teléfono Móvil", 2, 30, true);
  objCheck.checkString("mensaje", "Mensaje", 2, 500, false);
  strError = objCheck.toString();
  if(!frm.acepto.checked){
  	strError += "Debe aceptar los términos y condiciones.\n";
  }
  if (strError == "") {
  	
  	
    return true;
  } else {
    showAlert (strError.replace(/\n/g,'<br/>'));
    return false;
  }
}     	

