// JavaScript Document

//FUNCION QUE CREA UNA MASCARA SEGUN UN PATRON DADO
function mascara(d,sep,pat,nums){
	if(d.valant != d.value){
		val = d.value
		largo = val.length
		val = val.split(sep)
		val2 = ''
		for(r=0;r<val.length;r++){
			val2 += val[r]	
		}
		if(nums){
			for(z=0;z<val2.length;z++){
				if(isNaN(val2.charAt(z))){
					letra = new RegExp(val2.charAt(z),"g")
					val2 = val2.replace(letra,"")
				}
			}
		}
		val = ''
		val3 = new Array()
		for(s=0; s<pat.length; s++){
			val3[s] = val2.substring(0,pat[s])
			val2 = val2.substr(pat[s])
		}
		for(q=0;q<val3.length; q++){
			if(q ==0){
				val = val3[q]
			}
			else{
				if(val3[q] != ""){
					val += sep + val3[q]
				}
			}
		}
		d.value = val
		d.valant = val
	}
}


function opcionfrm(STRcomando, cod){
	
	if(STRcomando == 'borrar'){
		if (window.confirm("¿Esta seguro de borrar este elemento?")){
			document.getElementById('comando').value = STRcomando;
			document.getElementById('codigo').value = cod;
			document.getElementById('frmnot').submit();
		}
		else
			alert("Operacion Cancelada");
	}
	else{
		document.getElementById('opcion').value = STRcomando;
		document.getElementById('Codigo').value = cod;
		document.getElementById('frmnuevo').submit();
	}
}

function opcionfrmnoticias(STRcomando, cod){
	
	if(STRcomando == 'borrar'){
		if (window.confirm("¿Esta seguro de borrar este elemento?")){
			document.getElementById('opcion').value = STRcomando;
			document.getElementById('Codigo').value = cod;
			document.getElementById('frmnuevo').submit();
		}
		else
			alert("Operacion Cancelada");
	}
	else{
		document.getElementById('opcion').value = STRcomando;
		document.getElementById('Codigo').value = cod;
		document.getElementById('frmnuevo').submit();
	}
}

//FUNCION PARA VALIDAR EL FORMULARIO DE LOS CLIENTES
function validarfrm(){
	var enviar;
	var mensaje;
	var chkcamara;
	mensaje = "";
	enviar = true;
	if(document.getElementById('txtempresa').value == "")
		enviar = false;
	if(document.getElementById('txtusuario').value == "")
		enviar = false;
	if(document.getElementById('txtpass1').value == "")
		enviar = false;
	if(document.getElementById('txtpass2').value == "")
		enviar = false;
	if(document.getElementById('txtpass1').value != document.getElementById('txtpass2').value){
		mensaje = "Tiene que Introducir bien la contraseña, para ello tienen que ser iguales";
	}
	
	//VALIDAR SI HAY ALGUN VIDEOGRABADOR SELECCIONADO
	chkcamara=false;
	
	for(i=0; i<document.frmclientes.length; i++){
		if((document.frmclientes[i].name=="camara")&&(document.frmclientes.elements[i].checked)){
			chkcamara=true;
			break;
		}
	}
	
	if(chkcamara==false){
		alert("Seleccione al menos un dispositivo");
		enviar = false;
	}
		
	if(enviar == true){
		if(mensaje==""){
			relleno_camaras();
			return true;
		}
		else{
			alert(mensaje);
			return false;
		}
	}
	else{
		alert("Hay campos sin rellenar.  ");
		return false;
	}
}

//FUNCION PARA VALIDAR EL FORMULARIODE LOS VIDEOGRABADORES
function validarfrm2(){
	var enviar;
	var mensaje;
	mensaje = "";
	enviar = true;
	if(document.getElementById('txtdeno').value == "")
		enviar = false;
	if(document.getElementById('txtnumero').value == "")
		enviar = false;
	if(document.getElementById('txtip').value == "")
		enviar = false;
	if(document.getElementById('txtpuerto').value == "")
		enviar = false;
		
	if(enviar == true){
		if(mensaje=="")
			return true;
		else{
			alert(mensaje);
			return false;
		}
	}
	else{
		alert("Hay campos sin rellenar.  ");
		return false;
	}
}


function validarpresupuesto(){
	var enviar;
	var STRtemp;
	var mensaje;
	mensaje="";
	STRtemp = new String();
	enviar = true;
	if(document.getElementById('txtnom').value == "")
		enviar = false;
	if(document.getElementById('txttelf').value == "")
		enviar = false;
	if(document.getElementById('direccion').value == "")
		enviar = false;
	else{
		STRtemp = document.getElementById('direccion').value;
		if(STRtemp.indexOf('@') == (-1)){
			enviar = false;
			mensaje = "El correo electrónico no tiene el formato deseado";
		}
	}
	if(document.getElementById('departamento').value == "")
		enviar = false;
	if(document.getElementById('Detalle').value == "")
		enviar = false;
		
	if(enviar == true){
		if(mensaje == "")
			return true;
		else{
			alert(mensaje);
			return false;
		}
	}
	else{
		alert("Hay campos sin rellenar.  ");
		return false;
	}
}

function Ir_aPagina(STRurl){
	document.location.href = STRurl;
}

function animar_btn(boton, opt){
	boton.src = opt;
}

//FUNCION PARA RELLENAR LAS CAMARAS QUE VAN A IR ASOCIADAS A CADA USUARIO
function relleno_camaras(){
	var i;
	var pantalla;
	pantalla = new String();
	document.frmclientes.camaras.value="";
	for(i=0; i<document.frmclientes.length; i++){
		if((document.frmclientes[i].name=="camara")&&(document.frmclientes.elements[i].checked)){
			pantalla=document.frmclientes.elements[i].value;
			document.frmclientes.camaras.value += pantalla + ";";
		}
	}
	document.frmclientes.camaras.value = ";" + document.frmclientes.camaras.value;
}


function verNoticia(STRurl){
	var popup;
	popup = window.open(STRurl, '_blank', 'width=640, height=480, status=no, scrollbars=yes');
}


function VentanasPop(STRurl){
	var popup;
	popup = window.open(STRurl, '_blank', 'width=700, height=300, status=no, scrollbars=yes');
}


//FUNCION PARA EL POPUP GETIMAGE.ASP QUE ACEPTA LA IMAGEN Y LA METE EN EL EDITOR
function aceptar_imagen(){
	window.opener.document.getElementById('controlimg').innerHTML = document.getElementById('divImagen').innerHTML;
	window.opener.document.getElementById('tiponoticia').innerHTML = 'Imagen adjunta: &nbsp;<strong>' + document.getElementById('idimagen').value; + '</strong>'
	window.opener.document.getElementById('imagen').disabled = true;
	window.opener.document.getElementById('limpiar').disabled = false;
	window.opener.document.getElementById('boolimg').value = 1
	window.close();
}

function aceptar_imagen2(){
	document.getElementById('idimagen').value = document.getElementById('namearchivo').value;
	document.getElementById('pieimg').value = document.getElementById('txtpie').value;
	document.getElementById('valueimg').value = document.getElementById('fimagen').value;
}

function aceptar_pdf(){
	window.opener.document.getElementById('controlimg2').innerHTML = document.getElementById('divImagen').innerHTML;
	window.opener.document.getElementById('tiponoticia2').innerHTML = 'Archivo Adjunto: &nbsp;<strong>' + document.getElementById('idpdf').value; + '</strong>'
	window.opener.document.getElementById('imagen2').disabled = true;
	window.opener.document.getElementById('limpiar2').disabled = false;
	window.close();
	//document.getElementById('idpdf').value = document.getElementById('namearchivo').value;
	//document.getElementById('pieimg').value = document.getElementById('txtpie').value;
	//document.getElementById('valuepdf').value = document.getElementById('fpdf').value;
}

function aceptar_pdf2(){
	document.getElementById('idpdf').value = document.getElementById('namearchivo').value;
	//document.getElementById('pieimg').value = document.getElementById('txtpie').value;
	document.getElementById('valuepdf').value = document.getElementById('fpdf').value;
}


//FUNCION PARA COMPROBAR EL FORMULARIO DEL EDITOR DE NOTICIAS
function validarNoticia(){
	var enviar;
	enviar = true;
	
	if(document.getElementById('titulo').value == "")
		enviar = false;
	
	if(document.getElementById('date').value == "")
		enviar = false;
	
	if(document.getElementById('desc').value == "")
		enviar = false;
		
	if(document.getElementById('cuerpo').value == "")
		enviar = false;
		
	if(enviar == true){
		//if(document.getElementById('boolimg').value == 1){
//			document.getElementById('fimagen').value = document.getElementById('valueimg').value;
//		}
		return true;
	}
	else{
		alert("Faltan campos por introducir");
		return false;
	}
}


function antihundimiento(car){

	//FUNCION FULERA PARA QUE NO TE HUNDAN LOS DATOS QUE MANDAS
	//SE TIENE QUE LLAMAR CON LOS EVENTOS ONKEYDOWN Y ONKEYUP RESPECTIVAMENTE EN UN INPUT DE TEXTO
	//Y AÑADIR REPLACES TANTOS COMO CARACTERES QUIERAS QUITAR PARA QUE NO TE HUNDAN EN LA MISERIA
	var caracter;
	caracter = new String();
	caracter = car.value;
	
	//SOLO SE REEMPLAZA SI ENCUENTRA LOS CARACTERES QUE TU QUIERAS 
	if((caracter.indexOf("\"") > (-1)) || (caracter.indexOf("'") > (-1))){	
		//caracter=caracter.replace("\"", "");
		caracter=caracter.replace("'", "");
		car.value=caracter;
	}
	if((caracter.indexOf('&') != (-1))){
		caracter=caracter.replace("&", "");
		car.value=caracter;
	}
}

function listar_noticias(){
	document.getElementById('frmtipo').submit();
}

function aplicar(obj){
	obj.style.cursor = 'pointer';
	obj.className = 'sub';
}

function quitar(obj){
	obj.style.cursor = 'pointer';
	obj.className = 'nosub';
}