/**
	** Muestra la capa de presupuestos y oculta la capa de  entrada de datos
	*/
	function mostrarPresupuesto(){
	  if ( $('#flagSimular').val() ==0){
				desactivarButtonAddCentro();
				//se oculta la capa de entrada 
			  //$('div#datosEntrada').fadeOut('slow'); 
			  $('div#datosEntrada').hide();
			   //se muestra la capa de presupuestos
			  $('div#presupuesto').fadeIn('fast');
			  //se oculta la capa donde se muestra el mensaje de error del máximo 50 en caso de estar siendo mostrado
				$('div#mensaje').hide();
			  
			  //para realizar el cálculo de la simulación, se pasan los ámbitos que 
			  // ha seleccionado el usuario.
			  
			  $.post('/simuladorSPA/calcularPresupuesto.aspx',{
				  					chkSeguridad:$('input[@name=chkSeguridad]:checked').val(),
				  					chkHigiene:$('input[@name=chkHigiene]:checked').val(),
				  					chkErgonomia:$('input[@name=chkErgonomia]:checked').val(),
				  					chkVSalud:$('input[@name=chkVSalud]:checked').val()
				  				},
				  		function(response){$('div#presupuesto').html(response).show();})
		
			  //utilizado para las estadísticas			  
			  $.post('/simuladorSPA/simuladorCosteTotalSpa.aspx',{
				  					chkSeguridad:$('input[@name=chkSeguridad]:checked').val(),
				  					chkHigiene:$('input[@name=chkHigiene]:checked').val(),
				  					chkErgonomia:$('input[@name=chkErgonomia]:checked').val(),
				  					chkVSalud:$('input[@name=chkVSalud]:checked').val()
				  				},
				  		function(response){ })
		
		}
}

/**
	** Muestra la capa de entrada de datos y oculta la capa de  presupuestos
	*/
	function mostrarEntrada(){
		
	  $('div#datosEntrada').fadeIn('slow'); 
	  $('div#presupuesto').hide();
	 // $('div#presupuesto').fadeOut('slow');
	  $('div#BotonCosteTotal').fadeIn('slow');
}


/**
	** Muestra la capa del listado de los centros
	*/
  function mostrarListadoCentros(){ 
  	//alert ("mostrarListadoCentros"); 	
  	
  	 var 	ind = parseInt(document.getElementById("numTIndustria").value);
		 var 	ofi  = parseInt(document.getElementById("numTOficina").value);
		 var 	cons  = parseInt(document.getElementById("numTConst").value);
		 var 	ane  = parseInt(document.getElementById("numTAnexo").value);
		 
		 var 	c1  = parseInt(document.getElementById("Centro1").value);
		 var 	c2  = parseInt(document.getElementById("Centro2").value);
		 var 	c3  = parseInt(document.getElementById("Centro3").value);
		 var 	c4  = parseInt(document.getElementById("Centro4").value);
		 var 	c5  = parseInt(document.getElementById("Centro5").value);
		 
		  var sumaCentros = 0;
		  
		  var nCentro = document.getElementById("flagnumCentros").value;
		  
		  if (nCentro ==1){
		  	sumaCentros = c1;
		  	}
		  if (nCentro ==2){
		  	sumaCentros = c1+c2;
		  	}	
		  if (nCentro ==3){
		  	sumaCentros = c1+c2+c3;
		  	}
		  if (nCentro ==4){
		  	sumaCentros = c1+c2+c3+c4;
		  	}
		  
		  
		
	//	alert("este es el centro numero:"+nCentro)
	//	alert("LA SUMA DE LOS CENTROS ES:"+sumaCentros)
		 
		  if  (isNaN(ind)){ ind=0;}
 	
		  if  (isNaN(ofi)){ofi=0;}
		  
		  if (isNaN(cons)){cons=0;}
		  
		  if (isNaN(ane)){ane=0;}	
  /*
  		var resultado = ntIndus + ntOfic + ntCons +ntAnex;
 	
 	  	var sumaG = parseInt(document.getElementById("SumaGlobal").value);
  	
  	
  	      var ind = $('input[@name=numTIndustria]').val();
			    var ofi = $('input[@name=numTOficina]').val();
			    var cons = $('input[@name=numTConst]').val();
			    var ane = $('input[@name=numTAnexo]').val();
			    
			    
		*/	 
		    var resTrab = (ind + ofi+ cons +ane);
		
		   
			  if (((ind + ofi+ cons +ane)<=50)&&((sumaCentros+resTrab )<=50)&&(nCentro<6)){ 
								if (  $('#flagCentro').get(0).value ==0){
									    //se oculta el mensaje de error en caso de estar siendo mostrado
									    $('div#mensaje').hide();
										  $('div#listado_centros').fadeIn('slow'); 
										  $('div#BotonCosteTotal').fadeIn('slow');
									   
										  $.post('/simuladorSPA/anyadirCentro.aspx',{
										  					numTIndustria:$('input[@name=numTIndustria]').val(),
										  					numTOficina:$('input[@name=numTOficina]').val(),
										  					numTConst:$('input[@name=numTConst]').val(),
										  					numTAnexo:$('input[@name=numTAnexo]').val()},
										  		function(response){$('div#tablaCentros').html(response).show();sumarFlagCentro();limpiarCampos();desactivarButtonAddCentro();})
								return false;
								}
					}else{
				  	$('div#mensaje').fadeIn('slow');
				  	$('div#mensaje').focus();
				  }
				  
				  
				//alert ("fin mostrarListadoCentros"); 	  
 }
 
 
 /**
	** oculta la capa del listado de los centros
	*/
  function ocultarListadoCentros(){
	  $('div#listado_centros').hide(); 
		$('div#BotonCosteTotal').hide();

 }
 
 
 function limpiarCampos(){
 	document.getElementById("numTIndustria").value=""; 
	document.getElementById("numTOficina").value="";       
	document.getElementById("numTConst").value="";        
	document.getElementById("numTAnexo").value="";  
 	
 	}
 	
 	
 	 function limpiarCamposInicio(){
 	
 	document.getElementById("numTIndustria").value=""; 
	document.getElementById("numTOficina").value="";       
	document.getElementById("numTConst").value="";        
	document.getElementById("numTAnexo").value="";  
	document.getElementById("Centro1").value="0"; 
	document.getElementById("Centro2").value="0";       
	document.getElementById("Centro3").value="0";        
	document.getElementById("Centro4").value="0"; 
	document.getElementById("Centro5").value="0"; 
	document.getElementById("SumaGlobal").value="0"; 
 	document.getElementById("flagnumCentros").value="0";
 
 	}
 	
 	
 	
 	
 
 function sumarFlagCentro(){
 
 		 var 	ntIndus = parseInt(document.getElementById("numTIndustria").value);
		 var 	ntOfic  = parseInt(document.getElementById("numTOficina").value);
		 var 	ntCons  = parseInt(document.getElementById("numTConst").value);
		 var 	ntAnex  = parseInt(document.getElementById("numTAnexo").value);
		  if  (isNaN(ntIndus)){ ntIndus=0;}
 	
		  if  (isNaN(ntOfic)){ntOfic=0;}
		  
		  if (isNaN(ntCons)){ntCons=0;}
		  
		  if (isNaN(ntAnex)){ntAnex=0;}	
  
  		var resultado = ntIndus + ntOfic + ntCons +ntAnex;
 	
 	  	var sumaG = parseInt(document.getElementById("SumaGlobal").value);
 	 
 	 //alert(ntIndus);
 	 //alert(ntOfic);
 	 //alert(ntCons);
 	 //alert(ntAnex);
 	 //alert(sumaG+resultado);
 	 
 	 
 	 
 	 var suma = sumaG+resultado;
 	 //alert('La suma es: .......'+suma);
 	 if (suma > 50){
 	 	$('div#mensaje').fadeIn('slow');
 	 	$('div#mensaje').focus();
 	 //	alert ('te pasas'); 
 	 	}else{ 
 	 		//alert ('no llegas'); 
 	   document.getElementById("SumaGlobal").value = suma;
 	    
 	    //alert('La suma2 es: .......'+document.getElementById("SumaGlobal").value );
		 	var num = parseInt(document.getElementById("flagnumCentros").value);
		 	document.getElementById("flagnumCentros").value = (num+1);
			}
 	
 	//alert(document.getElementById("flagnumCentros").value);
 
 	}



 function restarFlagCentro(){
 
 	var num = parseInt(document.getElementById("flagnumCentros").value);
 	document.getElementById("flagnumCentros").value = (num-1);
 	//alert(document.getElementById("flagnumCentros").value);
 
 	}



	/**
	** activa el estilo del botón encargado de añadir centros
	*/
	function activarButtonAddCentro(){
		
		//alert (" en activarButtonAddCentro()...");
		
		 var 	ntIndus = parseInt(document.getElementById("numTIndustria").value);
		 var 	ntOfic  = parseInt(document.getElementById("numTOficina").value);
		 var 	ntCons  = parseInt(document.getElementById("numTConst").value);
		 var 	ntAnex  = parseInt(document.getElementById("numTAnexo").value);
		  if  (isNaN(ntIndus)){ ntIndus=0;} 	
		  if  (isNaN(ntOfic)){ntOfic=0;}		  
		  if  (isNaN(ntCons)){ntCons=0;}		  
		  if  (isNaN(ntAnex)){ntAnex=0;}	
 
  		var resultado2 = ntIndus + ntOfic + ntCons +ntAnex;
/*
   try{
  		//Si solo se introducen ceros, no se activa el botón
 			if (isNaN (resultado2)||((!isNaN (resultado2)) && ( resultado2 <1)))	{
 			return false;
 			}
 		}catch(excepcion){		}
*/		 
		// alert("en activarButtonAddCentro ");
		if ( $('#flagCentro').get(0).value ==1){
				 $('#mybutton').removeClass("BotonAddCentroDisabled"); 
				 $('#mybutton').addClass("BotonAddCentro"); 
				 $('#flagCentro').get(0).value =0;
			}
		 return true;	
		}


/**
	** desactiva el estilo del botón encargado de añadir centros y 
	*/
	function desactivarButtonAddCentro(){
 //alert("en desactivarButtonAddCentro ");
  if ( $('#flagCentro').val() ==0){
		  $('#mybutton').removeClass("BotonAddCentro"); 
		  $('#mybutton').addClass("BotonAddCentroDisabled");
		   
		   	$('#flagCentro').get(0).value =1;
		  
	}	

	// alert( $('#flagCentro').val());
}
/** 1.1 */
	/**
	** utilizado para impedir que se introduzcan caracteres no deseados en el campo del número de empleados
	**
	*/
	
	
			var strUserAgent = navigator.userAgent.toLowerCase(); 
			var isIE = strUserAgent.indexOf("msie") > -1; 
			var isNS6 = strUserAgent.indexOf("netscape6") > -1; 
			var isNS4 = !isIE && !isNS6  && parseFloat(navigator.appVersion) < 5; 
			//mask function
						
			
function maskKeyUp(event) {

		var iKeyCode, strKey;  
			
			if (isIE) {
			    iKeyCode = event.keyCode;
			} else {
			    iKeyCode = event.which;
			}
		strKey = String.fromCharCode(iKeyCode);
	  
	  if ((iKeyCode < 45 || (iKeyCode > 57 && (iKeyCode < 96 || iKeyCode > 105)))&& (iKeyCode !=8) ) {
					return false;
			}else{
				return true;
				}

}



/**
* Máscara utilizada en los campos input, que solo permite introducir números 
* Compatible para los principales navegadores del mercado.
*/

 function numbersOnly(event)
      {
      	
        var keyCode = event.keyCode;
        if (keyCode==0)
          keyCode = event.which;

        // Only do the next bit if we're running in Mozilla
        if (!document.all)
        {
          // In Mozilla the keyCode will contain a function key ONLY IF the charcode IS 0.
          // The keyCode for 't' returns 116, which is the same as the keyCode for F5,
          // which is why we have to check the charCode. In IE pressing a function key
          // does not generate an onKeyPress event.
          if (event.charCode == null || event.charCode == 0)
            return true; // allow function keys
        }

        // If we got here then it was a real key that as pressed (not a function key in Mozilla)
        // We only want to allow the key press if it was a number (0-9) or a decimal point.
        if (keyCode>=48 && keyCode<=57)
          return true;
        else
          return false;
      }


/**
*	 Función encargada de determninar si no se ha introducido ningún trabajador 
*  Devuelve true si todos los valores son incorrectos, ess decir si no son 
*  números o todos son 0.
*
*
*/
function incorrectoNumTrabajadores()
{
	// alert(document.getElementById("numTIndustria").value);
	// alert(document.getElementById("numTOficina").value);
	// alert(document.getElementById("numTConst").value);
	// alert(document.getElementById("numTAnexo").value);
	 
 var 	ntIndus = parseInt(document.getElementById("numTIndustria").value);
 var 	ntOfic  = parseInt(document.getElementById("numTOficina").value);
 var 	ntCons  = parseInt(document.getElementById("numTConst").value);
 var 	ntAnex  = parseInt(document.getElementById("numTAnexo").value);
 
 if  (isNaN(ntIndus)&& isNaN(ntOfic)&&isNaN(ntCons)&&isNaN(ntAnex)){ 	
 	return true;
 	}else{
		  if  (isNaN(ntIndus)){ ntIndus=0;} 	
		  if  (isNaN(ntOfic)){ntOfic=0;}  
		  if (isNaN(ntCons)){ntCons=0;}  
		  if (isNaN(ntAnex)){ntAnex=0;}	  
		  var resultado = ntIndus + ntOfic + ntCons +ntAnex;
		
		 	if (isNaN (resultado)||( resultado <1))	{
		 		return true;	
		 	}else{
		 		return false;
		 		} 
	}
}

function checkNumtrabajadores(event) {

if (numbersOnly(event)){
		activarButtonAddCentro();
}else{
	if (incorrectoNumTrabajadores())
	     desactivarButtonAddCentro();
	}
}


/**
** comprueba que al menos un check de la página esté seleccionada
** en ese caso activa visualmente el botón de calculo de presupuesto
**
*/
function comprobarActivacionBotonCalculo(){
		//alert ("comprobarActivacionBotonCalculo"); 	 
	var activar;
					
	//alert($('input[@name=chkSeguridad]:checked').val());
	if ($('input[@name=chkSeguridad]:checked').val() != null ){
		//alert("La seguridad esta seleccionada");
		activar=true;
	}	
	
	if ($('input[@name=chkHigiene]:checked').val() != null ){
		//alert("La seguridad esta seleccionada");
		activar=true;
	}
	
	if ($('input[@name=chkErgonomia]:checked').val() != null ){
		//alert("La seguridad esta seleccionada");
		activar=true;
	}
	
	if ($('input[@name=chkVSalud]:checked').val() != null ){
		//alert("La seguridad esta seleccionada");
		activar=true;
	}
			
	if (activar){
		activarVisualButtoncalcularPresupuesto();
		
	}else{
		activar =false;
		desactivarVisualButtoncalcularPresupuesto();
	}

//alert ("fin comprobarActivacionBotonCalculo");
}



/**
* en caso de no tener centros, se desactiva el botón de calculo del presupuesto
* y se oculta la capa que contiene el listado de centros.
*
*/
function desactivarButtoncalcularPresupuesto(){		
  if ( document.getElementById("flagnumCentros").value == "0"){
  	  ocultarListadoCentros();
		  $('#botonTotal').removeClass("BotonCosteTotal"); 
		  $('#botonTotal').addClass("BotonCosteTotalDisabled");
		  //si no hay centros ocultamos la capa que los contiene
	}	
}

/**
* se utiliza esta función para activar visualemnte el botón de cálculo del presupuesto.
*
*/
function activarVisualButtoncalcularPresupuesto(){
	$('#botonTotal').removeClass("BotonCosteTotalDisabled"); 
	$('#botonTotal').addClass("BotonCosteTotal");
	addTitleActivo(); 
	$('#flagSimular').get(0).value =0;
}

/**
* en caso de no tener centros, se desactiva el botón de calculo del presupuesto
*
*/
function desactivarVisualButtoncalcularPresupuesto(){
  $('#botonTotal').removeClass("BotonCosteTotal"); 
	$('#botonTotal').addClass("BotonCosteTotalDisabled");
	$('#flagSimular').get(0).value =1;
  addTitleInActivo();
}

 function contactar(idioma){
window.location = '/formularios.aspx?formulario=0&ambito=5&lang='+idioma;
}  

function imprimir(){
   var vPagina = "/imprimir.aspx?toolact=imprimir";
   var sTarget = "_blank";
	 ventanades = open(vPagina,sTarget);
	 try{
		 	ventanades.focus(); 
		
		}catch(e){};
		return false;
 }

function recargar(){
 window.location = '/simuladorSpa.aspx';
}

/**
* Almacenamos en cada centro el número de trabajadores que se han añadido
*
*/
function addSumaGlobal(){
//alert("addSumaGlobal");
 var centros = document.getElementById("flagnumCentros").value;
 
// alert("El centro es ....."+ centros);
 var siguiente = parseInt(centros)+1;
 
 var 	ntIndus = parseInt(document.getElementById("numTIndustria").value);
 var 	ntOfic  = parseInt(document.getElementById("numTOficina").value);
 var 	ntCons  = parseInt(document.getElementById("numTConst").value);
 var 	ntAnex  = parseInt(document.getElementById("numTAnexo").value);
 
 		  if  (isNaN(ntIndus)){ ntIndus=0;} 	
		  if  (isNaN(ntOfic)){ntOfic=0;}  
		  if (isNaN(ntCons)){ntCons=0;}  
		  if (isNaN(ntAnex)){ntAnex=0;}	  
		  var resultado = ntIndus + ntOfic + ntCons +ntAnex;

 var ident="Centro"+siguiente;
// alert ("El centro es:..."+ ident);
 if (siguiente == "1"){
 		document.getElementById("Centro1").value = resultado;
 	}
 	if (siguiente == "2"){
 		document.getElementById("Centro2").value = resultado;
 	}
 	if (siguiente == "3"){
 		document.getElementById("Centro3").value = resultado;
 	}
 	if (siguiente == "4"){
 		document.getElementById("Centro4").value = resultado;
 	}
 	if (siguiente == "5"){
 		document.getElementById("Centro5").value = resultado;
 	}
 	
 	//alert("fin addSumaGlobal");
 	
}


/**
* Eliminamos de cada centro el número de trabajadores que se han borrado
*
*/

function borrarSumaGlobal(numCentro){
 
 //alert("en borrarSumaGlobal");
 var sumaGlobal = document.getElementById("SumaGlobal").value;
 
 
 var sumacentro = document.getElementById("Centro"+(numCentro)).value;
 
 document.getElementById("SumaGlobal").value = (sumaGlobal-sumacentro);
 
 //document.getElementById("Centro"+(numCentro)).value = 0;
 
 //alert ("El numero de centro es:...... "+numCentro);
 
// alert ("La suma Global es:...... "+document.getElementById("SumaGlobal").value);
 
  if (numCentro == "1"){
 		document.getElementById("Centro1").value ="0";
 	}
 	if (numCentro == "2"){
 		document.getElementById("Centro2").value ="0";
 	}
 	if (numCentro == "3"){
 		document.getElementById("Centro3").value ="0";
 	}
 	if (numCentro == "4"){
 		document.getElementById("Centro4").value ="0";
 	}
  if (numCentro == "5"){
 		document.getElementById("Centro5").value ="0";
 	}
  
 for (i=numCentro; i<5; i++){
    document.getElementById("Centro"+(i)).value = document.getElementById("Centro"+(i+1)).value;
}

 //se oculta el mensaje de error en caso de estar siendo mostrado
$('div#mensaje').hide();
 
}








