﻿﻿function enviar_pregunta()
{if(comprovar())
{$('#enviarq').disabled=true;$.post("ajax/enviar_pregunta.php",{nom:$('#nompregunta').val(),mail:'',pregunta:$('#pregunta').val(),type:'pregunta',status:'pendiente'},function(data){if(data==='ok')
{alert("Su pregunta ha sido enviada correctamente");$('#nompregunta').val('');$('#pregunta').val('');$('#charinfopr').val('200 caract. restantes');}
else
{alert("Su pregunta no se ha podido enviar. Pruebe de nuevo más tarde.");}
$('#enviarq').disabled=false;});}}
function enviar_problema()
{if(comprovarp())
{$('#enviarp').disabled=true;$.post("ajax/enviar_pregunta.php",{nom:$('#nomtecnic').val(),mail:$('#email').val(),pregunta:$('#descripcio').val(),type:'problema',status:'pendiente'},function(data){if(data==='ok')
{alert("Su pregunta ha sido enviada correctamente");$('#nomtecnic').val('');$('#descripcio').val('');}
else
{alert("Su pregunta no se ha podido enviar. Pruebe de nuevo más tarde.");}
$('#enviarp').disabled=false;});}}
function comprovar()
{if($('#nompregunta').val()!=''&&$('#pregunta').val()!='')
{return true;}
else
{alert("Indique el nombre y su pregunta");return false;}}
function comprovarp()
{if($('#nomtecnic').val()!=''&&$('#descripcio').val()!='')
{return true;}
else
{alert("Indique el nombre y su pregunta");return false;}}
function limitChars(textid, limit, infodiv)
		{
			var text = $('#'+textid).val(); 
			var textlength = text.length;
			if(textlength > limit)
			{
				$('#' + infodiv).html('No se puede escribir más de '+limit+' caracteres!');
				$('#' + infodiv).css('color','#F00');
				$('#'+textid).val(text.substr(0,limit));
				return false;
			}
			else
			{
				$('#' + infodiv).html('Caracteres restantes: '+ (limit - textlength));
				$('#' + infodiv).css('color','#206b20');
				return true;
			}
		}
	  var lastactivequestion;
		function reload()
		{
			$("#hidden").load("ajax/getactivequestion.php");
			$("#hidden2").load("ajax/getactiveuser.php");

			if ($("#hidden").html() == ' ')
			{
				$(".activa").html(lastactivequestion);
				$(".autoractiva").html(lastactiveuser);
			}
			else
			{
				$(".activa").html($("#hidden").html());
				$(".autoractiva").html($("#hidden2").html());
				lastactivequestion = $("#hidden").html();
				lastactiveuser = $("#hidden2").html();
			}
			setTimeout(reload,1000);
		}
		
		$(document).ready(function(){
			reload();
			limitChars('pregunta', 200, 'charinfopr');
			
			$(".submit.question").click(function(){
				enviar_pregunta();
			});
			$("#pregunta").keyup(function(){
				limitChars('pregunta', 200, 'charinfopr');
			});
			/*
			limitChars('descripcio', 200, 'charinfote');
			$("#descripcio").keyup(function(){
				limitChars('descripcio', 200, 'charinfote');
			});
			$(".submit.problem").click(function(){
				enviar_problema();
			});
			$(".underline").click(function(){
				$("#tecnic form").slideToggle("slow");
			});*/
			updateCounter();
		});

	 comptador=null;
	 file="ajax/counter.php";
	 start=false;

	 if(!(document.cookie)) file+='?nocookie';

	 function updateCounter(){
		 $.get(file, function(data){
			if(comptador!=data){
				$("#counter").html(data);
				comptador=data;

				if(start==false){
					start=true;
				}else{
					$("#comptador").colorBlend([{fromColor:"#90B821", toColor:"white", param:"background-color", cycles:0.5}]);
				}
			}
			 setTimeout(updateCounter,500);		 
		});
	 }
