/* JavaScript Document
##############################################################################################################################

												Colecsion completilla de javascripts

############################################################################################################################### */


// Imprimir páxina
function imprimeBaby(){
	
	var NS = (navigator.appName == "Netscape"); var VERSION = parseInt(navigator.appVersion);
	
	if (NS)window.print();  
	
	else {
    	var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
		document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
    	WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";  
	}
}


// Función para axustar a altura de 2 capas e igualalas
function setH() {
   var maxH = Math.max(xGetElementById('principal').offsetHeight,xGetElementById('menu').offsetHeight);
   xGetElementById('principal').style.height=maxH+'px';
   xGetElementById('menu').style.height=maxH+'px';
   } 
   
   

// Función para referenciar os obxectos dun formulario polo seu id, mellor que polo array elemnts.
// Funcionará con case que todos os browsers novos
function xGetElementById(e) {
	str = e;
	if(typeof(e)!='string') return e;
	
	if(document.getElementById) e=document.getElementById(e);
	
	else if(document.all) e=document.all[e];
	
	else e=null;
	
	if(e == null)alert('Error identificando elementos por su id ('+str+')'); 
	
	return e;
}




// Comproba si e un nº tde telefono válido. Devolve false si non é válido
//----------------------------------------------------------------------
function validaTelefono(str){
	var patron = new RegExp("[0-9]{9,}","gi");
	return patron.test(str);

	
}



// Comproba que unha dir. de email sexa correcta. Devolve false en caso de error
//-------------------------------------------------------------------------------
function validaEmail(str){
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(str)) return true; // email OK
	else return false;
}




// Función para restrinxir só a caracteres alfanumericos. False si hai caracteres raros
//--------------------------------------------------------------------------------------
function caracteresValidos(str, min,max) {
	str = str.replace(/^\s*|\s*$/g,""); // Faille un trim á cadena
	tmp = "^[a-zA-Z ñÑáéíóúü0-9_\.]{" + min + "," + max + "}$";
	var patron = new RegExp(tmp);
	//var patron = new RegExp("[a-x]{9,}","gi");
	return patron.test(str);
	
}



function validaDNI(dni) {
	dni = dni.toUpperCase();	
	numero = dni.substr(0,dni.length-1);
	let = dni.substr(dni.length-1,1);
	numero = numero % 23;
	letra='TRWAGMYFPDXBNJZSQVHLCKET';
	letra=letra.substring(numero,numero+1);
	if (letra!=let)return false; 
	else return true;
}



// Valida a hora. 
function validaHora(h){
	tmp = "^([0-9]|0[0-9]|1[0-9]|2[0-3])(:([0-5][0-9]))?$";
	var patron = new RegExp(tmp);
	return patron.test(h);
}






// Func. q pide confirmación sobre unha acción.(void)
//----------------------------------------------
function confirmar(texto,url){
	var res = window.confirm(texto);
	if ( res ){
		//redirecciona(url,0)
		document.location.href=url;
	}	
}





/* Busca caracteres que no sean espacio en blanco nunha cadea
--------------------------------------------------------------*/
function vacio(q) {
	for ( i = 0; i < q.length; i++ )if ( q.charAt(i) != " " ) return false;
	return true
}

/*Elimina espacios ao final e ao principio de unha cadena*/
function trim(str){
	str = str.replace(/^\s*|\s*$/g,"");
	return str;
}


// Comproba que se escolleu un campo válido nun <select>. 
// Recibe o valor do campo e o valor que NON debe ter para ser válido.
//-----------------------------------------------------------
function compSelect(q,valor){
	if( q == valor ) return false;
	else return true
}




// Redirección(void)
//----------------------------------------------
function redirecciona(url,tempo){
	if(tempo>0)	setTimeout("document.location.href='"+url+"';",tempo);else document.location.href=''+url+'';
}

// Cerrar ventana(void)
//----------------------------------------------
function cerrarse(){
	window.close();
}


// Texto da barra de estado(void)
//----------------------------------------------
function estatus(s){
	window.status = s;
}

// Maximizar ventana(void)
//----------------------------------------------
function maximiza(){
	// Maximizar Ventana por Nick Lowe (nicklowe@ukonline.co.uk)

	window.moveTo(0,0);
	if (document.all){
	      top.window.resizeTo(screen.availWidth,screen.availHeight);
		  
	}else if (document.layers||document.getElementById){
		
		if (top.window.outerHeight<screen.availHeight||top.window.outerWidth<screen.availWidth){
			
			top.window.outerHeight = screen.availHeight;
			top.window.outerWidth = screen.availWidth;
		}
	}
}

// Engadir páxina a favoritos(void)
//----------------------------------------------
function favoritos(url,titulo) {
   
if ((navigator.appName=="Microsoft Internet Explorer") && (parseInt(navigator.appVersion)>=4)) { 
      //var url="http://www.lagardebesada.com/"; 
      //var titulo=":: Bodega Lagar De Besada ::: ALBARIÑOS"; 
      window.external.AddFavorite(url,titulo); 
	  
}else if(navigator.appName == "Netscape") 
	alert ("Presione Crtl+D para agregar este sitio a favoritos"); 

}


// Convertir en páxina de inicio (só IE) (void)
//----------------------------------------------
function pdeinicio(url) {
	document.body.style.behavior="url(#default#homepage)"; 
	document.body.setHomePage(url);
}


// Abre unha nova ventana (void)
//----------------------------------------------
function novaJanela (paxina, nomeJanela, ancho, alto, scrollbars, posicion) {
	
	var win = null;
	// Calculo dimenisóns da pantalla por si acaso queremos full
	var anchoPantalla = screen.width; altoPantalla = screen.height;
	if(ancho == 'full') ancho = anchoPantalla; if(alto == 'full') alto = altoPantalla;
	
	if(posicion=="random"){
		LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-ancho)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-alto)-75)):100;
	}
	if(posicion=="center"){
		LeftPosition=(screen.width)?(screen.width-ancho)/2:100;
		TopPosition=(screen.height)?(screen.height-alto)/2:100;
		
	}else if((posicion!="center" && posicion!="random") || posicion==null){
		LeftPosition=0;TopPosition=20
	}
	
settings='width='+ancho+',height='+alto+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scrollbars+',location=no, directories=no, status=no, menubar=no, toolbar=no, resizable=no';

win = window.open(paxina, nomeJanela, settings);
win.focus();
} 


// Redimensiona e centra unha janela
function centrarJanela(ancho,alto){
	LeftPosition=(screen.width)?(screen.width-ancho)/2:100;
	TopPosition=(screen.height)?(screen.height-alto)/2:100;	
	
	resizeTo(ancho,alto);
	moveTo(LeftPosition,TopPosition);
	
}

//=============================================================================================================================
// Nova Janela by Dani
/*
O codigo correcto abrir un popup dende un enlace é este:
<a href="/index.htm" target="_blank" onClick="window.open(this.href, this.target, 'width=300,height=400'); return false;">Lanzar correctamente</a>
*/
