
/* Fichero con las funciones javascript necesarias para la web*/

function AbrirVentanaNueva(ruta,w,h){	

	popupWin = window.open(ruta, '_blank', 'resizable=no,scrollbars=yes,width='+w+',height='+h);
}

function OcultarDesplegarBloque(id){
  var identificador= document.getElementById(id);
  if (identificador){
      if (identificador.style.display == 'none'){
      	$('#'+id).fadeIn();
      }
      else{
      	$('#'+id).fadeOut();
      }
       
  }

}
function MarcarCheckbox(vector, actual){
 if (actual.checked == 1){
   for (i=0;i<vector.elements.length;i++)
      if(vector.elements[i].type == "checkbox")
         vector.elements[i].checked=1;
 }
 else{
   for (i=0;i<vector.elements.length;i++)
      if(vector.elements[i].type == "checkbox")
         vector.elements[i].checked=0;
 }

}

var nav4 = window.Event ? true : false;
function IsNumber(event){
//var key = nav4 ? evt.which : evt.keyCode;
var key; if(window.event){ key = event.keyCode;}else if(event.which){ key = event.which;}
return (key <= 13 || (key >= 48 && key <= 57) || key == 46 || !key);
}

 function validarDatosOferta(FormSolicitud) {
 	var er_cp = /(^([0-9]{5,5}))$/				//5 numeros 
	var reFecha = /^(\d{1,2})\-(\d{1,2})\-(\d{4})$/;
	var reFecha2 = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
	var MensajeBase = "Debe facilitar y/o comprobar la información que se detalla a continuación: \n\n <br />";
  	var Mensaje = MensajeBase;

	if ( FormSolicitud.puesto.value==''){
		Mensaje +=  "- PUESTO DE TRABAJO.\n<br />";
		document.getElementById('palabra_clave').className='error_input';
	}
	
	if ( FormSolicitud.descripcion.value==''){
		Mensaje +=  "- DESCRIPCIÓN.\n<br />";
		document.getElementById('ldescripcion').style.border='2px solid #bd3626';
	}	
	
	if ( FormSolicitud.numero_personas.value==''){
		Mensaje +=  "- NÚMERO DE PERSONAS.\n<br />";
		document.getElementById('lnum').className='error_input';		
	}
		
	if ( FormSolicitud.categoria_id.value==''){
		Mensaje +=  "- CATEGORÍA.\n<br />";
				document.getElementById('busquedaCategoria').className='error_input';
	}
	if ( FormSolicitud.subcategoria_id.value==''){
		Mensaje +=  "- SUBCATEGORÍA.\n<br />";
		document.getElementById('lSubcategoria').className='error_input';
	}			
	if ( FormSolicitud.lIslas.value==''){
		Mensaje +=  "- ISLA.\n<br />";
		document.getElementById('lIslas').className='error_input';
	}
	if ( FormSolicitud.provincia_id.value==''){
		Mensaje +=  "- PROVINCIA.\n<br />";
		document.getElementById('busquedaProvincia').className='error_input';
	}
	if ( FormSolicitud.municipio_id.value==''){
		Mensaje +=  "- MUNICIPIO.\n<br />";
		document.getElementById('lMunicipio').className='error_input';
	}	
	if ( FormSolicitud.ocultar_datos_empresa.checked && FormSolicitud.nombre_empresa.value==''){
		Mensaje +=  "<p>Debe rellenar un nombre de empresa si oculta su datos originales \n<br />";
		document.getElementById('lnombrempresa').className='error_input';
	}	

	if (MensajeBase == Mensaje) return true;
	else {
		//alert(Mensaje);
		validacion(Mensaje);
		return false;
	}
	
 }
 

	function esDigito(sChr){
		var sCod = sChr.charCodeAt(0);
		return ((sCod > 47) && (sCod < 58));
	}
	function valSep(oTxt){
		var bOk = false;
		bOk = bOk || ((oTxt.value.charAt(2) == "-") && (oTxt.value.charAt(5) == "-"));
		bOk = bOk || ((oTxt.value.charAt(2) == "/") && (oTxt.value.charAt(5) == "/"));
		return bOk;
	}
	function finMes(oTxt){
		var nMes = parseInt(oTxt.value.substr(3, 2), 10);
		var nRes = 0;
		switch (nMes){
			case 1: nRes = 31; break;
			case 2: nRes = 29; break;
			case 3: nRes = 31; break;
			case 4: nRes = 30; break;
			case 5: nRes = 31; break;
			case 6: nRes = 30; break;
			case 7: nRes = 31; break;
			case 8: nRes = 31; break;
			case 9: nRes = 30; break;
			case 10: nRes = 31; break;
			case 11: nRes = 30; break;
			case 12: nRes = 31; break;
		}
		return nRes;
	}
	function valDia(oTxt){
		var bOk = false;
		var nDia = parseInt(oTxt.value.substr(0, 2), 10);
		bOk = bOk || ((nDia >= 1) && (nDia <= finMes(oTxt)));
		return bOk;
	}
	function valMes(oTxt){
		var bOk = false;
		var nMes = parseInt(oTxt.value.substr(3, 2), 10);
		bOk = bOk || ((nMes >= 1) && (nMes <= 12));
		return bOk;
	}
	function valAno(oTxt){
		var bOk = true;
		var nAno = oTxt.value.substr(6);
		bOk = bOk && ((nAno.length == 2) || (nAno.length == 4));
		if (bOk){
			for (var i = 0; i < nAno.length; i++){
			bOk = bOk && esDigito(nAno.charAt(i));
			}
		}
		return bOk;
	}
	function valFecha(oTxt){
		var bOk = true;
		if (oTxt.value != ""){
			bOk = bOk && (valAno(oTxt));
			bOk = bOk && (valMes(oTxt));
			bOk = bOk && (valDia(oTxt));
			bOk = bOk && (valSep(oTxt));
			if (!bOk){
				validacion("Fecha inválida. El formato debe ser dd/mm/aaaa");
				oTxt.value = "";
				oTxt.focus();
			}
		}
	} 
	
function validacion(Mensaje){	
	document.getElementById('mensaje').style.display='none';
	document.getElementById('mensaje').innerHTML = 
		"<div class='caja_aviso'><div class='contenedor_aviso'><div class='"+						
		"aviso_alerta' style='text-align:left;'><h3>Aviso</h3>"+Mensaje+
		"<div class='aviso_cerrar'><a "+
		"href='#' onclick='cierraErrorValidacion();' >Cerrar</a></div><div class='clearer'></div></div></div></div>";
	$("#mensaje").fadeIn("slow");		

}
function cierraErrorValidacion(){
	$("#mensaje").fadeOut("slow");
}
function avisoFormulario(Mensaje, form, variable){	
    var valor_variable = '';
	if (variable){
		valor_variable = form+".aceptar_compra.value='1';";
	}

	document.getElementById('mensaje').style.display='none';
	document.getElementById('mensaje').innerHTML = 
		"<div class='caja_aviso'><div class='contenedor_aviso'><div class='"+						
		"aviso_alerta' style='text-align:left;'><h3>Aviso</h3>"+Mensaje+
		"<div class='botones_aviso'><input type='button' onclick=\""+valor_variable+""+form+".submit();this.disabled='true';\" class='boton_web' value='Aceptar'/> &nbsp;"+
		"&nbsp;&nbsp;<input type='button' name='botonis_cerrar' value='Cancelar' onclick='cierraErrorValidacion()' class='boton_web'/></div><div class='clearer'></div></div></div></div>";
	$("#mensaje").fadeIn("slow");		

}	
function avisoFormularioEliminar(Mensaje, form){	
	document.getElementById('mensaje').style.display='none';
	document.getElementById('mensaje').innerHTML = 
		"<div class='caja_aviso'><div class='contenedor_aviso'><div class='"+						
		"aviso_alerta' style='text-align:left;'><h3>Aviso</h3>"+Mensaje+
		"<div class='botones_aviso'><input type='button' onclick=\""+form+".operacion2.value='Eliminar';"+form+".submit();\" class='boton_web' value='Aceptar'/> &nbsp;"+
		"&nbsp;&nbsp;<input type='button' name='botonis_cerrar' value='Cancelar' onclick='cierraErrorValidacion()' class='boton_web'/></div><div class='clearer'></div></div></div></div>";
	$("#mensaje").fadeIn("slow");		

}	

function avisoEnlace(Mensaje, url){	
	document.getElementById('mensaje').style.display='none';
	document.getElementById('mensaje').innerHTML = 
		"<div class='caja_aviso'><div class='contenedor_aviso'><div class='"+						
		"aviso_alerta' style='text-align:left;'><h3>Aviso</h3>"+Mensaje+
		"<div class='botones_aviso'><input type='button' onclick=\"document.location.href='"+url+"'\" class='boton_web' value='Aceptar'/> &nbsp;"+
		"&nbsp;&nbsp;<input type='button' name='botonis_cerrar' onclick='cierraErrorValidacion()' value='Cancelar' class='boton_web'/></div><div class='clearer'></div></div></div></div>";
	$("#mensaje").fadeIn("slow");		

}	
	
function arrayVacio (array){
 if (array){
   for (var i=0;i<array.length;i++){
        if (array[i].checked){           
            return false;
        }
   }
 }
   return true;
}	


window.addEventListener?window.addEventListener("load",formularios,false):window.attachEvent("onload",formularios);
window.addEventListener?window.addEventListener("resize",redimensionar,false):window.attachEvent("onload",redimensionar);

function formularios(){ 
 redimensionar();
 for (var i=0; i<document.forms.length;i++){
	 var frm = document.forms[i];
	 if (frm){ 
	 	frm.autocomplete = 'Off';
	 	frm.setAttribute("autocomplete", "off");
	 }
 }
}

function redimensionar(){
var estiloPubli = document.getElementById('publicidad_dcha'); 
if (estiloPubli){
	if((dimension("width") <= 980) ){	
		estiloPubli.style.position = 'absolute';	  
//		estiloPubli.style.display = 'none';
	} else {
		var alineacion = parseInt((dimension("width") - 990)/2);
		if(!(/MSIE 6.0/i.test(navigator.userAgent))){
			estiloPubli.style.position = 'fixed';
			estiloPubli.style.top = '1px';
			var suma = 895+alineacion;
			estiloPubli.style.left = ""+suma+'px';
			estiloPubli.style.height= '500px';	
			estiloPubli.style.display = 'block';
		}
	}
}
}

function dimension(tipo) {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  if (tipo=='width') return myWidth;
  if (tipo=='height') return myHeight;
}

window.onresize = function()
{
  if(document.all)
  {
  window.resizeEnd = (window.resizeEnd ==null)?(new Object()):window.resizeEnd;
  clearTimeout(window.resizeEnd);
  window.resizeEnd = setTimeout(onResizeEnd,1);
  }
  else
  { onResizeEnd();}
 
}

function onResizeEnd()
{
 redimensionar();
}

function desplegarOpcion(id){
	var opcion = document.getElementById(id);
	if (opcion)
		if (opcion.style.display == 'none'){
			$("#"+id).fadeIn("slow");
		}	
		else
			$("#"+id).fadeOut("slow");
	return false;
}
function quitarOpcion(id){
	var opcion = document.getElementById(id);
	if (opcion)
		opcion.style.display = 'none';
}
