$(document).ready(function()
{
  function check_name(name)
  {
	var regx;
	regx = /^([a-zñáéíóú������A-ZÁÉÍÓÚÑ������\s])*$/;
    name = $.trim(name);
	if((name.length < 1) || (name.length > 20))
      return false;
	else if(regx.test(name) != true)
	  return false;
	  else
        return name;
  }
  function check_email(email)
  {
	var regx;
    regx = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
	email = $.trim(email);
	if((regx.test(email) != true)||(email == ""))
      return false;
	  else
        return email;
  }
  function check_telef(telef)
  {
	var regx;
    regx = /^([0-9])*$/;
	telef = $.trim(telef);
	if(telef == "")
      return false;
	else if(telef.length != 11)
	return false;
	else if(regx.test(telef) != true)
	  return false;
	  else
        return telef;
  }
  function check_niv(valor, campo, tok, mensaje)
  {
    var regx;
    regx = /^([0-9a-zA-Z])*$/;
	valor = $.trim(valor);
	if(	valor.length != 17){
	  $("#"+mensaje).html("NIV inválida.");
	  $("#"+mensaje).fadeIn("fast");
	  $("#"+tok).attr("value",0);
	  $("#"+campo).css("background","#FF5B60");
	  return false;
	}else if(regx.test(valor) != true){
	  $("#"+mensaje).html("NIV inválida.");
	  $("#"+mensaje).fadeIn("fast");
	  $("#"+tok).attr("value",0);
	  $("#"+campo).css("background","#FF5B60");
	  return false;
	}else{
		$.getJSON("../../controllers/vehiculo/check_register_niv.php",{niv:valor},
		  function(data)
		   {
		     if(data)
			 {
			   $("#"+mensaje).html("*");
			   $("#"+mensaje).fadeIn("fast");
			   $("#"+tok).attr("value",1);
			   return (valor.toUpperCase());
			 }
			 else
			 {
			  $("#"+mensaje).html("NIV registrado.");
			  $("#"+mensaje).fadeIn("fast");
			  $("#"+tok).attr("value",0);
	          $("#"+campo).css("background","#FF5B60");
			  return false;
			 }
            });
	  }
        
  }
  function check_placa(valor, campo, tok, mensaje)
  {
    var regx;
    regx = /^([0-9a-zA-Z])*$/;
	valor = $.trim(valor);
	if(	valor.length < 6 || valor.length > 7){
	  $("#"+mensaje).html("Placa inválida.");
	  $("#"+mensaje).fadeIn("fast");
	  $("#"+tok).attr("value",0);
	  $("#"+campo).css("background","#FF5B60");
	  return false;
	}else if(regx.test(valor) == false){
	  $("#"+mensaje).html("Placa inválida.");
	  $("#"+mensaje).fadeIn("fast");
	  $("#"+tok).attr("value",0);
	  $("#"+campo).css("background","#FF5B60");
	  return false;
	}else
	  {
       $.getJSON("../../controllers/vehiculo/check_register_placa.php",{placa:valor},
		  function(data)
		   {
		     if(data)
			 {
			   $("#"+mensaje).fadeOut("fast");
			   $("#"+tok).attr("value",1);
			   return (valor.toUpperCase());
			 }
			 else
			 {
			  $("#"+mensaje).html("Placa registrada.");
			  $("#"+mensaje).fadeIn("fast");
			  $("#"+tok).attr("value",0);
	          $("#"+campo).css("background","#FF5B60");
			  return false;
			 }
            });
	  }
  }
  function check_password(password1,password2)
  {
	password1 = $.trim(password1);
    password2 = $.trim(password2);
	if(	password1 != password2 || password1.length < 4 || password1.length > 8 || password2.length < 4 || password2.length > 8)
	  return false;
	  else
        return password1;
  }
  function check_password2(password1,password2)
  {
	password1 = $.trim(password1);
    password2 = $.trim(password2);
	if(	password1 != password2 ){
	  $("#msg_pass2").html("Las Claves no coinciden.");
	  $("#msg_pass2").fadeIn("fast");
	  $("#password2").css("background","#FF5B60");
	  return false;
	}else{
		$("#msg_pass2").html("*");
		$("#msg_pass2").fadeIn("fast");
        return password1;
	}
  }
  function check_select(valor)
  {
	if(valor == 0)
	  return false;
	  else
        return valor;
  }
  function check_cedula(cedula)
  {
	var regxp;
	regx = /^([0-9])*$/;
    cedula = $.trim(cedula);
	if((cedula.length < 5) || (cedula.length > 8))
      return false;
	else if(regx.test(cedula) == false)
	  return false;
	  else
	  {
       $.getJSON("../../controllers/usuario/check_cedula.php",{cedula:cedula},
		  function(data)
		   {
		     if(data)
			 {
			   $("#msg_cedula").html("*");
			   $("#msg_cedula").fadeIn("fast");
			   $("#can_login").attr("value",1);
			   return true;
			 }
			 else
			 {
			   $("#msg_cedula").html("Cédula registrada o inválida.");
			   $("#msg_cedula").fadeIn("fast");
	           $("#cedula").css("background","#FF5B60");
			   $("#can_login").attr("value",0);
			   return false;
			 }
            });
	  }
  }

  $("#form1").submit(function()
  {
	var name,apellido,cedula,email,telef1,telef2=true;
	var password,can_login,token=true;
	var modelo = new Array();
	var tipo = new Array();
	var anio = new Array();
	var placa = new Array();
	var niv = new Array();
	var fecha = new Array();
	var nivInv = new Array();
	var placaInv = new Array();
		
	name = check_name($("#nombre").val());
	apellido = check_name($("#apellido").val());
	cedula = check_cedula($("#cedula").val());
	password = check_password($("#password").val(),$("#password2").val());
	email = check_email($("#email").val());
	telef1 = check_telef($("#telef1").val());
	if($("#telef2").val())
		telef2 = check_telef($("#telef2").val());
	can_login = $("#can_login").val();
	can_twi = $("#can_twi").val();

	for(i = 1; i <= $("#num_vehic").val(); i++){	  
	  modelo[i] = check_select($("#modelo"+i).val());
	  tipo[i] = check_select($("#tipove"+i).val());
	  anio[i] = check_select($("#aniove"+i).val());
	  placa[i] = $("#placav"+i).val();
	  niv[i] = $("#nivveh"+i).val();
	  fecha[i] = $("#fechav"+i).val();
	  nivInv[i] = check_niv($("#nivveh"+i).val(), "nivveh"+i, "nivInv"+i, "msgniv"+i);
	  placaInv[i] = check_placa($("#placav"+i).val(), "placav"+i, "placaInv"+i, "msg_placa"+i);
	  if( (modelo[i]==false) || (tipo[i]==false) || (anio[i]==false) || (placa[i]==false) || (niv[i]==false) || (fecha[i]==false) || (nivInv[i]==false) || (placaInv[i]==false) ){
		token = false;
	  }
	}
	
	if((name == false)||(apellido == false)||(cedula == false)||(email == false)||(password == false)||(telef1 == false)||(telef2 == false)||(can_login == 0)||(can_twi == 0)||(token == false))
	{
	 if(name == false)
	 $("#nombre").css("background","#FF5B60");
	 if(apellido == false)
	 $("#apellido").css("background","#FF5B60");
	 if(cedula == false)
	 $("#cedula").css("background","#FF5B60");
	 if(password == false)
	 {
	   $("#password").css("background","#FF5B60");
	   $("#password2").css("background","#FF5B60");
	 }
	 if(email == false)
	 $("#email").css("background","#FF5B60");
	 if(telef1 == false)
	 $("#telef1").css("background","#FF5B60");
	 if(telef2 == false)
	 $("#telef2").css("background","#FF5B60");
	 
	 for(i = 1; i <= $("#num_vehic").val(); i++){
	   if(modelo[i] == false)
	   $("#modelo"+i).css("background","#FF5B60");
	   if(tipo[i] == false)
	   $("#tipove"+i).css("background","#FF5B60");
	   if(anio[i] == false)
	   $("#aniove"+i).css("background","#FF5B60");
	   if(placa[i] == false)
	   $("#placav"+i).css("background","#FF5B60");
	   if(niv[i] == false)
	   $("#nivveh"+i).css("background","#FF5B60");
	   if(fecha[i] == false)
	   $("#fechav"+i).css("background","#FF5B60");
	   if(placaInv[i] == false)
	   $("#placav"+i).css("background","#FF5B60");
	   if(nivInv[i] == false)
	   $("#nivveh"+i).css("background","#FF5B60");
	 }
	 	 
	 if(can_login == 0)
	 $("#cedula").css("background","#FF5B60");
	 
	 $("#msg_alert").fadeIn();
	 return false;
	}
	else{
		   return true;
	}
  });
  $("#cedula").keyup(function()
  {
   var cedula = $("#cedula").val();
   var regx = /^([0-9])*$/;
   $("#can_login").attr("value",0);
   if(cedula < 5 || cedula > 8)
   {
	 $("#msg_cedula").html("*");
     $("#can_login").attr("value",0);
   }
   else if(regx.test(cedula) == false)
   {
     $("#can_login").attr("value",0);
   }
     cedula = check_cedula(cedula);
  });
  $("#twitter").blur(function()
  {
   var twitter = $("#twitter").val();
   var regx = /^([a-zA-Z0-9])*$/;
   $("#can_twi").attr("value",0);
   if(twitter.length > 	1 && twitter.length > 15)
   {
	 $("#twitter").css("background","#FF5B60");
	 $("#msg_twitter").html("Usuario Twitter inválido.");
	 $("#msg_twitter").fadeIn("fast");
     $("#can_twi").attr("value",0);
   }
   else if(regx.test(twitter) == false)
   {
	 $("#twitter").css("background","#FF5B60");
	 $("#msg_twitter").html("Usuario Twitter inválido.");
	 $("#msg_twitter").fadeIn("fast");
     $("#can_twi").attr("value",0);
   }else{
	 $("#msg_twitter").html("");
	 $("#msg_twitter").fadeIn("fast");
	 $("#can_twi").attr("value",1);
   }	
  });
  $("#password2").blur(function()
  {
   var password2 = $("#password2").val();
   var password = $("#password").val();   
   password2 = check_password2(password,password2);
  });
  $("#placav1").blur(function()
  {
   var placa = $("#placav1").val();
   var regx = /^([0-9a-zA-Z])*$/;
   $("#placaInv1").attr("value",0);
   $("#placav1").val(this.value.toUpperCase());
   if(placa < 6 || placa > 7)
   {
     $("#placaInv1").attr("value",0);
   }
   else if(regx.test(placa) == false)
   {
     $("#placaInv1").attr("value",0);
   }
     placa = check_placa(placa, "placav1", "placaInv1", "msg_placa1");
  });
  $("#placav2").blur(function()
  {
   var placa = $("#placav2").val();
   var regx = /^([0-9a-zA-Z])*$/;
   $("#placaInv2").attr("value",0);
   $("#placav2").val(this.value.toUpperCase());
   if(placa < 6 || placa > 7)
   {
     $("#placaInv2").attr("value",0);
   }
   else if(regx.test(placa) == false)
   {
     $("#placaInv2").attr("value",0);
   }
     placa = check_placa(placa, "placav2", "placaInv2", "msg_placa2");
  });
  $("#placav3").blur(function()
  {
   var placa = $("#placav3").val();
   var regx = /^([0-9a-zA-Z])*$/;
   $("#placaInv3").attr("value",0);
   $("#placav3").val(this.value.toUpperCase());
   if(placa < 6 || placa > 7)
   {
     $("#placaInv3").attr("value",0);
   }
   else if(regx.test(placa) == false)
   {
     $("#placaInv3").attr("value",0);
   }
     placa = check_placa(placa, "placav3", "placaInv3", "msg_placa3");
  });
  $("#placav4").blur(function()
  {
   var placa = $("#placav4").val();
   var regx = /^([0-9a-zA-Z])*$/;
   $("#placaInv4").attr("value",0);
   $("#placav4").val(this.value.toUpperCase());
   if(placa < 6 || placa > 7)
   {
     $("#placaInv4").attr("value",0);
   }
   else if(regx.test(placa) == false)
   {
     $("#placaInv4").attr("value",0);
   }
     placa = check_placa(placa, "placav4", "placaInv4", "msg_placa4");
  });
  $("#placav5").blur(function()
  {
   var placa = $("#placav5").val();
   var regx = /^([0-9a-zA-Z])*$/;
   $("#placaInv5").attr("value",0);
   $("#placav5").val(this.value.toUpperCase());
   if(placa < 6 || placa > 7)
   {
     $("#placaInv5").attr("value",0);
   }
   else if(regx.test(placa) == false)
   {
     $("#placaInv5").attr("value",0);
   }
     placa = check_placa(placa, "placav5", "placaInv5", "msg_placa5");
  });
  
  //******************************
  $("#nivveh1").blur(function()
  {
   var niv = $("#nivveh1").val();
   $("#nivInv1").attr("value",0);
   $("#nivveh1").val(this.value.toUpperCase());//al escribir lleva a mayuscula
   niv = check_niv(niv, "nivveh1", "nivInv1", "msgniv1");
  });
  $("#nivveh2").blur(function()
  {
   var niv = $("#nivveh2").val();
   var regx = /^([0-9a-zA-Z])*$/;
   $("#nivInv2").attr("value",0);
   $("#nivveh2").val(this.value.toUpperCase());
   niv = check_niv(niv, "nivveh2", "nivInv2", "msgniv2");
  });
  $("#nivveh3").blur(function()
  {
   var niv = $("#nivveh3").val();
   var regx = /^([0-9a-zA-Z])*$/;
   $("#nivInv3").attr("value",0);
   $("#nivveh3").val(this.value.toUpperCase());
   niv = check_niv(niv, "nivveh3", "nivInv3", "msgniv3");
  });
  $("#nivveh4").blur(function()
  {
   var niv = $("#nivveh4").val();
   var regx = /^([0-9a-zA-Z])*$/;
   $("#nivInv4").attr("value",0);
   $("#nivveh4").val(this.value.toUpperCase());
   niv = check_niv(niv, "nivveh4", "nivInv4", "msgniv4");
  });
  $("#nivveh5").blur(function()
  {
   var niv = $("#nivveh5").val();
   var regx = /^([0-9a-zA-Z])*$/;
   $("#nivInv5").attr("value",0);
   $("#nivveh5").val(this.value.toUpperCase());
     niv = check_niv(niv, "nivveh5", "nivInv5", "msgniv5");
  }); 
  
  $("#fechav1").datepicker({changeMonth: true,changeYear: true, maxDate:'+0D',dateFormat:'dd-mm-yy',yearRange: '-21:+1'});
  $("#fechav2").datepicker({changeMonth: true,changeYear: true, maxDate:'+0D',dateFormat:'dd-mm-yy',yearRange: '-21:+1'});
  $("#fechav3").datepicker({changeMonth: true,changeYear: true, maxDate:'+0D',dateFormat:'dd-mm-yy',yearRange: '-21:+1'});
  $("#fechav4").datepicker({changeMonth: true,changeYear: true, maxDate:'+0D',dateFormat:'dd-mm-yy',yearRange: '-21:+1'});
  $("#fechav5").datepicker({changeMonth: true,changeYear: true, maxDate:'+0D',dateFormat:'dd-mm-yy',yearRange: '-21:+1'});
  $("#volver").click(function(){location.href="../../"});
  $("#tabs").tabs();
  //$("#accordion").accordion();
  $("#login,#password").keyup(function()
  {
    $("#msg").fadeOut("fast");
  });
  $("input.campostextos,select").focus(function()
  {
    $(this).css("background","#FCF972");
  });
  $("input.campostextos,select").blur(function()
  {
    $(this).css("background","#FFF");
  });
  $("#agregar1").click(function()
  {
    $("#newvehicle1").show();
	$("#agregar1").hide();
	$("#num_vehic").attr("value",2);
  });
  $("#agregar2").click(function()
  {
    $("#newvehicle2").show();
	$("#eliminar2").hide();
	$("#agregar2").hide();
	$("#num_vehic").attr("value",3);
  });
  $("#agregar3").click(function()
  {
    $("#newvehicle3").show();
	$("#eliminar3").hide();
	$("#agregar3").hide();
	$("#num_vehic").attr("value",4);
  });
  $("#agregar4").click(function()
  {
    $("#newvehicle4").show();
	$("#eliminar4").hide();
	$("#agregar4").hide();
	$("#num_vehic").attr("value",5);
  });
  $("#eliminar2").click(function()
  {
    $("#newvehicle1").hide();
	$("#agregar1").show();
	$("#num_vehic").attr("value",1);
  });
  $("#eliminar3").click(function()
  {
    $("#newvehicle2").hide();
	$("#eliminar2").show();
	$("#agregar2").show();
	$("#num_vehic").attr("value",2);
  });
  $("#eliminar4").click(function()
  {
    $("#newvehicle3").hide();
	$("#eliminar3").show();
	$("#agregar3").show();
	$("#num_vehic").attr("value",3);
  });
  $("#eliminar5").click(function()
  {
	$("#newvehicle4").hide();
	$("#eliminar4").show();
	$("#agregar4").show();
	$("#num_vehic").attr("value",4);
  });
});
