function popup(theURL,winName,features) { 
  var w = window.open(theURL,winName,features);
  w.focus();
};

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
};

function showClock(){
var clock=new Date();
var hours=clock.getHours();
var minutes=clock.getMinutes();
var seconds=clock.getSeconds();
if (hours<10){ hours="0" + hours; }
if (minutes<10){ minutes="0" + minutes; }
if (seconds<10){ seconds="0" + seconds; }
document.getElementById('showClock').innerHTML=hours+":"+minutes+":"+seconds;
t=setTimeout('showClock()',500);
};

function agregar(){
   if ((navigator.appName=="Microsoft Internet Explorer") && (parseInt(navigator.appVersion)>=4)) {
      var url="http://www.diakros.com"; 
      var titulo="diakros";
      window.external.AddFavorite(url,titulo);
   } else { 
      if(navigator.appName == "Netscape") 
         alert("Presione Crtl+D para agregar este sitio en sus Bookmarks"); 
   }
};

function confirmar_borrado(nombre){
	return confirm('CONFIRMAR BORRADO --- \nBorrar registro en BBDD nº: '+nombre+' ?');
};

function confirmar_borrado_noticia(titulo){
	return confirm('CONFIRMAR BORRADO --- \nBorrar noticia de título:\n'+titulo+' ?');
};

//pide confirmacion y lanza el borrado de todos los informes de un cliente dado
function borrarTodos(nombre,id){
	if (confirm('Realmente desea borrar todos los documentos \ndel usuario: '+nombre+' ?.'))
	{
		window.location = "http://www.tamborino.es/admin/documentos_deleteall.php?id_user="+id;
	}
};

//filtra los informes por el cliente seleccionado en el dropdown
function filtrarClientes(){
	id_user = document.users.users_form.options[document.users.users_form.selectedIndex].value;
	window.location = "http://www.tamborino.es/admin/documentos_admin.php?id_cliente="+id_cliente;
};

//filtra el loscaracteres y deja solo los numeros
//uso: <input name="precio_form" type="text" onkeypress="return numbersonly(event)" />
function numbersonly(e){
	var key;
	var keychar;
	if (window.event)
 		key = window.event.keyCode;
	else if (e)
 		key = e.which;
	else
 		return true;
	keychar = String.fromCharCode(key);
	// control keys
	if ((key==null) || (key==0) || (key==8) || 
 		(key==9) || (key==13) || (key==27) )
 		return true;
	// numbers
	else if ((("0123456789").indexOf(keychar) > -1))
		return true;
	else
 		return false;
};

//Comprueba que un campo de texto WHAT no tenga mas de LIMIT caracteres
//uso: onKeyPress="return maxchars(this, 280)" onBlur="return maxchars(this, 280)"
function maxchars(what,limit){
	if (what.value.length > limit){
		what.value = what.value.substring(0, limit);
	}
};

function comprobarFecha(campo){
	if(campo.value.match(/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/)){
		return true;
	}
	else{
		alert("Fecha no valida!");
		campo.focus();
		return false;
	}
};

function comprobarEmail(campo){
	if(campo.value.match(/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+.[a-z]{2,}$/i)){
		return true;
	}
	else{
		alert("e-mail no valido!");
		campo.focus();
		return false;
	}
};

function comprobarWeb(campo){
	if(campo.value.match(/[a-z0-9]+.[a-z]{2,4}$/i)){
		return true;
	}
	else{
		alert("URL no valida!");
		campo.focus();
		return false;
	}
};

function comprobarclave(campo){
	if(campo.value.match(/^[0-9a-no-z]+$/)){
		return true;
	}
	else{
		campo.focus();
		return false;
	}
};

/*
	Password Validator 0.1
	(c) 2007 Steven Levithan <stevenlevithan.com>
	MIT License
*/

function validatePassword (pw, options) {
	// default options (allows any password)
	var o = {
		lower:    0,
		upper:    0,
		alpha:    0, /* lower + upper */
		numeric:  0,
		special:  0,
		length:   [0, Infinity],
		custom:   [ /* regexes and/or functions */ ],
		badWords: [],
		badSequenceLength: 0,
		noQwertySequences: false,
		noSequential:      false
	};

	for (var property in options)
		o[property] = options[property];

	var	re = {
			lower:   /[a-z]/g,
			upper:   /[A-Z]/g,
			alpha:   /[A-Z]/gi,
			numeric: /[0-9]/g,
			special: /[\W_]/g
		},
		rule, i;

	// enforce min/max length
	if (pw.length < o.length[0] || pw.length > o.length[1])
		return false;

	// enforce lower/upper/alpha/numeric/special rules
	for (rule in re) {
		if ((pw.match(re[rule]) || []).length < o[rule])
			return false;
	}

	// enforce word ban (case insensitive)
	for (i = 0; i < o.badWords.length; i++) {
		if (pw.toLowerCase().indexOf(o.badWords[i].toLowerCase()) > -1)
			return false;
	}

	// enforce the no sequential, identical characters rule
	if (o.noSequential && /([\S\s])\1/.test(pw))
		return false;

	// enforce alphanumeric/qwerty sequence ban rules
	if (o.badSequenceLength) {
		var	lower   = "abcdefghijklmnopqrstuvwxyz",
			upper   = lower.toUpperCase(),
			numbers = "0123456789",
			qwerty  = "qwertyuiopasdfghjklzxcvbnm",
			start   = o.badSequenceLength - 1,
			seq     = "_" + pw.slice(0, start);
		for (i = start; i < pw.length; i++) {
			seq = seq.slice(1) + pw.charAt(i);
			if (
				lower.indexOf(seq)   > -1 ||
				upper.indexOf(seq)   > -1 ||
				numbers.indexOf(seq) > -1 ||
				(o.noQwertySequences && qwerty.indexOf(seq) > -1)
			) {
				return false;
			}
		}
	}

	// enforce custom regex/function rules
	for (i = 0; i < o.custom.length; i++) {
		rule = o.custom[i];
		if (rule instanceof RegExp) {
			if (!rule.test(pw))
				return false;
		} else if (rule instanceof Function) {
			if (!rule(pw))
				return false;
		}
	}

	// great success!
	return true;
}

function comprobarEditBanners(formulario) {
	if(document.getElementById(formulario).ref_form.value == '')
		return false;
};

function comprobarFormCatalogo(formulario) {
	var allOK = true;
	var textError = "";
	form = document.getElementById(formulario);
	if (form.cat_form.value=="0") {
		textError += "\nERROR: Se requiere una CATEGORIA.";
		form.cat_form.focus();
		allOK = false;
	}
	if (!allOK)
		alert(textError);
	return allOK;
};



function comprobarFormNoticias(formulario) {
	var allOK = true;
	var textError = "";
	form = document.getElementById(formulario);
	if (form.titulo_es_form.value=="") {
		textError += "\nERROR: Se requiere un TITULO de la noticia.";
		form.titulo_es_form.focus();
		allOK = false;
	}
	if (!allOK)
		alert(textError);
	return allOK;
};



function comprobarFormUsuarios(formulario) {
	var allOK = true;
	var textError = "";
	form = document.getElementById(formulario);
	if (form.grupo_form.value=="0") {
		textError += "\nERROR: Se requiere un GRUPO de Usuario.";
		form.grupo_form.focus();
		allOK = false;
	}
	if (form.usuario_form.value=="") {
		textError += "\nERROR: Se requiere un Nombre de usuario.";
		form.usuario_form.focus();
		allOK = false;
	}
	if (form.pass_form.value=="") {
		textError += "\nERROR: Se requiere una Clave de usuario.";
		allOK = false;
		form.pass_form.focus();
	} else {
		if (!comprobarclave(form.pass_form)) {
		textError += "\nERROR: Clave no valida!!! solo números y letras en minúsculas menos la [ ñ ]";
		allOK = false; }		
		
 	    if(form.pass_form.value.length < 6) {
		textError += "\nERROR: La Clave debe tener almenos 6 caracteres.";
		allOK = false; }
		
		form.pass_form.focus();
	}
	if (!allOK)
		alert(textError);
	return allOK;
};



function comprobarFormUsers(formulario) {
	var allOK = true;
	var textError = "";
	form = document.getElementById(formulario);
	if (form.nombre_form.value=="") {
		textError += "\nERROR: Se requiere un NOMBRE de usuario.";
		form.nombre_form.focus();
		allOK = false;
	}
	if (form.apellidos_form.value=="") {
		textError += "\nERROR: Se requiere unos APELLIDOS de usuario.";
		form.direccion_form.focus();
		allOK = false;
	}
	if (!form.email_form.value.match(/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+.[a-z]{2,}$/i)) {
		textError += "\nERROR: Email no valido!.";
		form.email_form.focus();
		allOK = false;
	}
	if (form.user_form.value=="") {
		textError += "\nERROR: Se requiere un NOMBRE DE USUARIO.";
		form.user_form.focus();
		allOK = false;
	}	
	if (form.password_form.value=="") {
		textError += "\nERROR: Se requiere una CONTRASEÑA de usuario.";
		form.password_form.focus();
		allOK = false;
	}
	if (!allOK)
		alert(textError);
	return allOK;
};

function comprobarFormDocumentos(formulario) {
	var allOK = true;
	var textError = "";
	form = document.getElementById(formulario);
	if (!form.fecha_form.value.match(/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/)) {
		textError += "\nERROR: FECHA no valida!.";
		form.fecha_form.focus();
		allOK = false;
	}
	if (form.titulo_form.value=="") {
		textError += "\nERROR: Se requiere un TITULO de documento.";
		form.titulo_form.focus();
		allOK = false;
	}
	if (form.documento_form.value=="") {
		textError += "\nERROR: Se requiere un Documento (PDF).";
		form.documento_form.focus();
		allOK = false;
	}
	if (!allOK)
		alert(textError);
	return allOK;
};

function comprobarFormUpdateDocumentos(formulario) {
	var allOK = true;
	var textError = "";
	form = document.getElementById(formulario);
	if (!form.fecha_form.value.match(/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/)) {
		textError += "\nERROR: FECHA no valida!.";
		form.fecha_form.focus();
		allOK = false;
	}
	if (form.titulo_form.value=="") {
		textError += "\nERROR: Se requiere un TITULO de documento.";
		form.titulo_form.focus();
		allOK = false;
	}
	if ((form.update_documento.checked) && (form.documento_form.value=="")) {
		textError += "\nERROR: Se requiere un Documento (PDF).";
		form.documento_form.focus();
		allOK = false;
	}
	if (!allOK)
		alert(textError);
	return allOK;
};
/*
function comprobarFormNoticias(formulario) {
	var allOK = true;
	var textError = "";
	form = document.getElementById(formulario);
	if (form.fecha_form.value=="") {
		textError += "\nERROR: Se requiere una fecha.";
		form.fecha_form.focus();
		allOK = false;
	}
	if (form.titulo_form.value=="") {
		textError += "\nERROR: Se requiere un titulo de noticia.";
		form.titulo_form.focus();
		allOK = false;
	}
	if (form.texto_form.value=="") {
		textError += "\nERROR: Se requiere un texto de noticia.";
		form.texto_form.focus();
		allOK = false;
	}
	if (!comprobarFecha(form.fecha_form)) {
		textError += "\nERROR: Fecha Invalida!.";
		allOK = false;
	}
	if (!allOK)
		alert(textError);
	return allOK;
};
*/
function comprobarFormClientes(formulario) {
	var allOK = true;
	var textError = "";
	form = document.getElementById(formulario);
	if (form.nombre_form.value=="") {
		textError += "\nERROR: Se requiere un nombre de empresa.";
		form.nombre_form.focus();
		allOK = false;
	}
	if (form.user_form.value=="") {
		textError += "\nERROR: Se requiere un nombre de usuario.";
		form.user_form.focus();
		allOK = false;
	}
	if (form.pass_form.value=="") {
		textError += "\nERROR: Se requiere un password.";
		form.pass_form.focus();
		allOK = false;
	}
	if (!allOK)
		alert(textError);
	return allOK;
};

function comprobarcontactar() {
	var allOK = true;
	var textError = "";
	if (document.contactar.nombre.value=="") {
		textError = "ERROR: Se requiere un Nombre.";
		document.contactar.nombre.focus();
		allOK = false;}
	if (document.contactar.telcontacto.value=="") {
		textError += "\nERROR: Se requiere un Teléfono de contacto.";
		document.contactar.telcontacto.focus();
		allOK = false;}
	if (document.contactar.acepto.checked == false) {
		textError += "\nERROR: Ha de aceptar haber leído la política de privacidad.";
		document.contactar.acepto.focus();
		allOK = false;}
	if (document.contactar.email.value=="") {
		textError += "\nERROR: Se requiere un e-mail.";
		document.contactar.email.focus();
		allOK = false;}
		if (!comprobarEmail(document.contactar.email)) {
		allOK = false;}
	if (!allOK)
		alert(textError);
	return allOK;
};

function comprobarpresupuesto() {
	var allOK = true;
	var textError = "";
	if (document.presupuesto.nombre.value=="") {
		textError = "ERROR: Se requiere un Nombre.";
		document.presupuesto.nombre.focus();
		allOK = false;}
	if (document.presupuesto.telcontacto.value=="") {
		textError += "\nERROR: Se requiere un Teléfono de contacto.";
		document.presupuesto.telcontacto.focus();
		allOK = false;}
	if (document.presupuesto.acepto.checked == false) {
		textError += "\nERROR: Ha de aceptar haber leído la política de privacidad.";
		document.presupuesto.acepto.focus();
		allOK = false;}
	if (document.presupuesto.email.value=="") {
		textError += "\nERROR: Se requiere un e-mail.";
		document.presupuesto.email.focus();
		allOK = false;}
		if (!comprobarEmail(document.presupuesto.email)) {
		allOK = false;}
	if (!allOK)
		alert(textError);
	return allOK;
};

function comprobarcomprar() {
	var allOK = true;
	var textError = "";
	if (document.comprar.nombre.value=="") {
		textError = "ERROR: Se requiere un Nombre.";
		document.comprar.nombre.focus();
		allOK = false;}
	if (document.comprar.telcontacto.value=="") {
		textError += "\nERROR: Se requiere un Teléfono de contacto.";
		document.comprar.telcontacto.focus();
		allOK = false;}
	if (document.comprar.acepto.checked == false) {
		textError += "\nERROR: Ha de aceptar haber leído la política de privacidad.";
		document.comprar.acepto.focus();
		allOK = false;}
	if (document.comprar.email.value=="") {
		textError += "\nERROR: Se requiere un e-mail.";
		document.comprar.email.focus();
		allOK = false;}
		if (!comprobarEmail(document.comprar.email)) {
		allOK = false;}
	if (!allOK)
		alert(textError);
	return allOK;
};

function desactivador(origen, valor, objetivo){
	if(origen.value == valor)
		objetivo.disabled = false
	else
		objetivo.disabled = true;
};

function terrenos(origen){
	desactivador(origen, "1", document.fincas.texto1_form);
	desactivador(origen, "1", document.fincas.texto2_form);
};
