/*
* Cabecera
*/
function showModalWin(arrayMensajes, botonera){
	var url="modalWindow.htm";
	if (arrayMensajes.length){
	 	finalUrl=url+"?comentario=";
		for (i=0; i<arrayMensajes.length; i++){
			finalUrl += arrayMensajes[i]+",";
		}
		finalUrl += "&botonera=" + botonera;
		document.getElementById('myUrl').src = finalUrl;
		document.getElementById('modal').style.display = 'block';
	}else{
	 	hiddeModalMessage('default');
	}
}

var mensajesModales = new Array();

function showModalMensajeFijo(botonera){
	var url="modalWindow.htm";
	var arrayMensajes=mensajesModales;
	if (arrayMensajes.length){
	 	finalUrl=url+"?comentario=";
		for (i=0; i<arrayMensajes.length; i++){
			finalUrl += arrayMensajes[i]+",";
		}
		finalUrl += "&mensajeFijo=1";
		finalUrl += "&botonera=" + botonera;
		document.getElementById('myUrl').src = finalUrl;
		document.getElementById('modal').style.display = 'block';
	}else{
	 	hiddeModalMessage('default');
	}
}



function showModalWinLoaded(botonera){
	showModalWin( mensajesModales, botonera)
}

function hiddeModalMessage(str){
 	document.getElementById('modal').style.display='none';
 	document.getElementById('myUrl').src="";
 	document.getElementById('modalResult').value=str;
}


/*
 * Constantes
 */
BOTON_DESHABILITADO_CLASS = "deshabilitado";


/*
 * Acciones
 */
function goBack() {
	doSubmit('X');
}

function doSubmit(action, formName) {

	if (document.estaSubmitiendo == null || document.estaSubmitiendo == 'undefined') {
		document.estaSubmitiendo = true;
		if ((formName == null)||(formName=='undefined')) {
			document.frm.action.value=action;
			document.frm.submit();
		}
		else {
			document.forms[formName].action.value=action;
			document.forms[formName].submit();
		}
	}
}

function doAceptar() {
	if (validaFormulario()) {
		doSubmit('A');
	}
}

function doAnyadir() {
	doSubmit('A');
}

function doBorrar() {
	if (confirm(getMessage('confirmarBorrar'))) {
		doSubmit('B');
	}
}

function doAnular() {
	if (confirm(getMessage('confirmarAnular'))) {
		doSubmit('AN');
	}
}

function doModificar() {
	doSubmit('M');
}

function doEntrar() {
	if (validaFormulario()) {
		doSubmit('E');
	}
}

function doGuardar() {
	if (validaFormulario()) {
		doSubmit('A');
	}
}

function doBuscar() {
	if (validaFormulario()) {
		doSubmit('BU');
	}
}

/*
 * Validaciones
 */
function addObjeto2Validacion(frmObj, descriptor, validatorHandler) {
	if (frmObj == null) {
		return;
	}
	if (document.elementosValidacion == null) {
		document.elementosValidacion = new Array();
	}
	var objVal = new Object();
	objVal.formObject = frmObj;
	objVal.descriptor = descriptor;
	objVal.handler = validatorHandler;
	document.elementosValidacion.push(objVal);
}

function validarVacio(frmObj) {
	if (frmObj == null) {
		return;
	}
	var msg = "";
	if (isVacio(frmObj)) {
		msg = getMessage(CAMPO_OBLIGATORIO);
	}
	return msg;
}

function isVacio(frmObj) {
	if (frmObj == null) {
		return true;
	}
	return getElementValue(frmObj.id) == "";
}

function validaFecha(frmObject) {
	var valor = getElementValue(frmObject.id);
	if (valor == "") {
		return;
	}
	var d = new Date();
	var msg = "";
//	try {
//		d.parse(valor);
//	} catch (e) {
//		msg = getMessage(FECHA_INVALIDA);
//	}
	return msg;
}

function validaFormulario() {
	if (document.elementosValidacion == null) {
		return true;
	}
	var mensajes = "";
	for (i=0;i<document.elementosValidacion.length;i++) {
		var frmObj = document.elementosValidacion[i];
		if (frmObj.handler == null) {
			var msgError = validarVacio(frmObj.formObject);
			if (msgError != null && msgError.length > 0) {
				mensajes += msgError.replace("%1", frmObj.descriptor);
			}
		}
		else {
			var msgError = frmObj.handler(frmObj.formObject);
			if (msgError != null && msgError.length > 0) {
				mensajes += msgError.replace("%1", frmObj.descriptor);
			}
		}
	}
	var returnValue = true;
	if (mensajes.length>0) {
		alert(getMessage("revisarCampos")+mensajes);
		returnValue = false;
	}

	setTimeout('document.elementosValidacion[0].formObject.select();document.elementosValidacion[0].formObject.focus();', 100);
	return returnValue;
}

function soloNumeros(evt) {
	evt = getEvent(evt);

	var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
		((evt.which) ? evt.which : 0));

	if (!(charCode >= 48 && charCode <= 57)) {
		evt.returnValue = false;
		return false;
	}
}

function soloMayusculas() {
	evt = (evt) ? evt : event;
	var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
		((evt.which) ? evt.which : 0));

	if (charCode >= 'a' && charCode <= 'z') {
		alert('aqui hay que hacer algo');
	}
}

function soloMinusculas() {
	evt = (evt) ? evt : event;
	var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
		((evt.which) ? evt.which : 0));

	if (charCode >= 'A' && charCode <= 'Z') {
		alert('aqui hay que hacer algo');
	}
}

function getEvent(e) {
	if (typeof e == 'undefined') e = window.event;
	return e;
}

/*
 * Display-tag
 */

function doSubmitDisplaytag(url) {
	var queryString = url.split("?")[1];
	var params = queryString.split("&");
	for (i=0; i<params.length; i++) {
		var idParam = params[i].split("=")[0];
		if (idParam.indexOf("d-") >= 0 || idParam == '6578706f7274') {
			oInput = document.getElementById(idParam);
			if (oInput != null && oInput.type == 'hidden') {
				var value = params[i].split("=")[1];
				if (value != null && value != "") {
					oInput.value = value;
					oInput.disabled = false;
				}
			}
		}
	}
	doSubmit('displaytag');
}

function addHandlers(tableName, noSeleccion) {
    var table = document.getElementById(tableName);
    if (table == null) {
    	return;
    }

    var rows = table.getElementsByTagName("tr");
    for (i=1; i < rows.length; i++) {
        var radio = rows[i].getElementsByTagName("input")[0];
		//comprobamos si hay filas deshabilitadas y cambiamos el estilo para que no se pueda clicar
		if ((radio != null) && (radio.type == "radio") && (radio.disabled)) {
			rows[i].disabled="disabled";
			rows[i].onmouseover = listadoMouseoverDeshabilitado;
        	rows[i].onmouseout = listadoMouseout;
			var cols = rows[i].getElementsByTagName("td");
			for(j=1; j < cols.length; j++){
				cols[j].className="deshabilitado";
			}
		}else{
			rows[i].onmouseover = listadoMouseover;
        	rows[i].onmouseout = listadoMouseout;
        	rows[i].onclick = listadoClick;
        }
	}
	if (noSeleccion == false || noSeleccion == null) {
		if (rows.length > 1) {
			var radio = rows[1].getElementsByTagName("input")[0];
			if (radio != null) {
				radio.checked = true;
			}
		}
	}
}

function addHighlight(tableName, noSeleccion) {
    var table = document.getElementById(tableName);
    if (table == null) {
    	return;
    }

    var rows = table.getElementsByTagName("tr");
    for (i=1; i < rows.length; i++) {
        rows[i].onmouseover = listadoHighlight;
        rows[i].onmouseout = listadoMouseout;
	}
}

function listadoClick(evt) {
	var cell = this.getElementsByTagName("td")[0];
    var oInput = cell.getElementsByTagName("input")[0];
    if (oInput != null) {
    	if (oInput.type == 'radio') {
		    oInput.checked = true;
    	}
    }
}

function listadoHighlight(evt) {
	this.style.cursor = "default";
	this.style.backgroundColor = "#eeb";
}

function listadoMouseoverDeshabilitado(evt) {
	this.style.backgroundColor = "#eeb";
}

function listadoMouseover(evt) {
	this.style.cursor = "pointer";
	this.style.backgroundColor = "#eeb";
}

function listadoMouseout(evt) {
	this.style.backgroundColor = "";
}


/*
 * Rollovers
 */
function addRollover(srcMouseover, srcMousedown) {
	if (document.rollovers == null) {
		document.rollovers = new Array();
	}
	if (srcMouseover != null && srcMouseover.length > 0) {
		addRolloverImage(srcMouseover);
	}
	if (srcMousedown != null && srcMousedown.length > 0) {
		addRolloverImage(srcMousedown);
	}
}

function addRolloverImage(src) {
	var oImg = new Image();
	oImg.src = src;
	document.rollovers.push(oImg);
}



/*
 * popUps
 */

function windowOpen(url, ancho, alto, name) {
	ancho = ancho==null?800:ancho;
	alto = alto==null?600:alto;
	var posX = window.screen.width / 2 - ancho / 2;
	var posY = window.screen.height / 2 - alto / 2;
	if (ancho + posX > window.screen.width) {
		posX = window.screen.width - ancho;
	}
	if (alto + posY > window.screen.height) {
		posY = window.screen.height - alto;
	}
	var vWin = window.open(url, name != null ? name : '', 'width='+ancho+',height='+alto+',status=no,resizable=yes,top='+posY+',left='+posX+'');
    vWin.opener = self;
    vWin.focus();
}

function showCalendar(evt, theObjectName, diasEspeciales) {
	evt = (evt) ? evt : event;
	var ancho = 230;
	var alto = 190;
	var posX = evt.screenX;
	var posY = evt.screenY;
	if (ancho + posX > window.screen.width) {
		posX = window.screen.width - ancho;
	}
	if (alto + posY > window.screen.height) {
		posY = window.screen.height - alto;
	}
	var vWinCal = window.open('calendar.htm?fieldName=' + theObjectName + '&value=' + escape(document.getElementById(theObjectName).value) + (diasEspeciales != null ? "&diasEspeciales=" + diasEspeciales:""), 'Calendar','width='+ancho+',height='+alto+',status=no,resizable=yes,top='+posY+',left='+posX+'');
    vWinCal.opener = self;
    vWinCal.focus();
}

function showLOV(evt, sender, pAncho, pAlto, parametros, modo) {
//	evt = (evt) ? evt : event;
//	evt = evt ? evt : event ? event : null;

	if (evt == null) {
		if (typeof event != 'undefined') {
			evt = event;
		}
	}
	var ancho = pAncho==null?600:pAncho;
	var alto = pAlto==null?500:pAlto;
	var posX = 0;
	var posY = 0;
	//if (evt) {
	//	posX = evt.screenX;
	//	posY = evt.screenY;
	//} else {
		posX = window.screen.width / 2 - ancho / 2;
		posY = window.screen.height / 2 - alto / 2;
	//}
	//if (ancho + posX > window.screen.width) {
	//	posX = window.screen.width - ancho;
	//}
	//if (alto + posY > window.screen.height) {
	//	posY = window.screen.height - alto;
	//}

	var paramString = "";
	if (parametros && parametros.length) {
		for (i=0;i<parametros.length;i++) {
			paramString += "&param" + i + "=" + parametros[i];
		}
	}
	var url = "listOfValues.htm?name=" + sender + paramString;

	if (modo == "radio") {
		url += "&modo=radio";
	}

	//var url = 'totaldisaster.htm?name=' + sender;
//	if (document.all) {
//		window.showModalDialog(url, self, 'width='+ancho+';height='+alto+';status=no;resizable=yes;top='+posY+';left='+posX+';');
//	} else {
		var vWinLOV = window.open(url, 'LOV', 'width='+ancho+',height='+alto+',scrollbars=yes,status=no,resizable=yes,top='+posY+',left='+posX+'');
	    vWinLOV.opener = self;
	    vWinLOV.focus();
//	}
}

function getElementValue(id) {
	var value = "";
	var frmObject = document.getElementById(id);
	if (frmObject != null) {
		switch (frmObject.tagName.toLowerCase()) {
			case "input":
			//alert('frmObject.type.toLowerCase() = ' + frmObject.type.toLowerCase());
				switch (frmObject.type.toLowerCase()) {
					case "label": value = frmObject.innerHTML; break;
					case "hidden":
					case "password":
					case "text": value = frmObject.value; break;
					case "radio": value = getRadioValue(id); break;
				}
				break;
			case "select-one":
			case "select": value = frmObject.value; break;
			case "div": value = frmObject.innerHTML; break;
		}
	}
	/*
	if (value != "") { // PC
		alert('value='+value);
	}*/
	return value;
}

function getRadioValue(id) {
	var valor = "";
	var nodo0 = document.getElementById(id);
	var radios = nodo0.form[nodo0.name];
	for (j=0;j<radios.length;j++) {
		if (radios[j].checked) {
			valor = radios[j].value;
		}
	}
	return valor;
}

function autocompleta(id, oCod) {
	if (oCod.value != '') {
		var ifrm = document.getElementById(id);
		if (typeof ifrm != 'undefined' && typeof ifrm.contentWindow.doSubmit != 'undefined') {
			ifrm.contentWindow.doSubmit(oCod.value);
		}
	}
}



/*
 * Gestion de eventos crossbrowser (cortesia de: http://www.scottandrew.com/weblog/articles/cbs-events)
 */
function addEvent(obj, evType, fn, useCapture){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, useCapture);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		alert("Handler could not be attached");
	}
}

function removeEvent(obj, evType, fn, useCapture){
	if (obj.removeEventListener){
		obj.removeEventListener(evType, fn, useCapture);
		return true;
	} else if (obj.detachEvent){
		var r = obj.detachEvent("on"+evType, fn);
		return r;
	} else {
		alert("Handler could not be removed");
	}
}

/*
 * Botones
 */
function guardarValoresBoton(boton) {
	var valores = new Object;
	valores.onclick = boton.onclick;
	valores.src = boton.src;
	valores.onmouseover = boton.onmouseover;
	valores.onmousedown = boton.onmousedown;
	valores.onmouseout = boton.onmouseout;
	valores.cursor = boton.style.cursor;
	eval('document.getElementById(\''+boton.id+'\').valores=valores;');
}

function habilitarBoton(boton) {
	eval('valores=document.getElementById(\''+boton.id+'\').valores;');
	boton.onclick = valores.onclick;
	boton.src = valores.src;
	boton.onmouseover = valores.onmouseover;
	boton.onmousedown = valores.onmousedown;
	boton.onmouseout = valores.onmouseout;
	boton.style.cursor = valores.cursor;
}


function deshabilitarBoton(boton) {
	guardarValoresBoton(boton);
	boton.onclick=function() {};
	boton.src=boton.src.substring(0, boton.src.indexOf('_') + 1) + 'deshabilitado.gif';
	boton.onmouseover=boton.src.substring(0, boton.src.indexOf('_') + 1) + 'deshabilitado.gif';
	boton.onmousedown=boton.src.substring(0, boton.src.indexOf('_') + 1) + 'deshabilitado.gif';
	boton.onmouseout=boton.src.substring(0, boton.src.indexOf('_') + 1) + 'deshabilitado.gif';
	boton.style.cursor='';
}

function disableButton(idBoton, deshabilitado) {
	if (idBoton != null) {
		var oBoton = document.getElementById(idBoton);
		if (oBoton != null) {
			if (deshabilitado == null) {
				deshabilitado = true;
			}
			if (deshabilitado && !oBoton.disabled) {
				oBoton.className = oBoton.className + " " + BOTON_DESHABILITADO_CLASS;
			} else if (!deshabilitado && oBoton.disabled) {
				oBoton.className = oBoton.className.replace(BOTON_DESHABILITADO_CLASS, "");
			}
			oBoton.disabled = deshabilitado;
		}
	}
}

/*
 * Seguridad
 */

function autocompletarPasswordOff() {
	if (document.getElementsByTagName) {
		var inputElements = document.getElementsByTagName("input");
		for (i=0; inputElements[i]; i++) {
			inputElements[i].setAttribute("autocomplete","off");
		}
	}
}


/*
 * Inclasificables
 */

function addOnblur(id, handler) {
	var frmObject = document.getElementById(id);
	if (typeof frmObject != 'undefined') {
		frmObject.onblur = handler;
	}
}

function doFocus(id) {
	if (id != 'undefined' && id != null) {
		frmObject = document.getElementById(id);
		if (frmObject != null && frmObject.focus) {
			frmObject.focus();
		}
	}
}

function modificaClassName(id){
	if(document.getElementById(id).className=="visible"){
		document.getElementById(id).className="noVisible";
		if(id == "datosFirma") {
			document.getElementById("masFirma").innerHTML="+";
			}
		if(id == "datosPaciente") {
			document.getElementById("masPaciente").innerHTML="+";
		}
		if(id == "datosEpisodio") {
			document.getElementById("masEpisodio").innerHTML="+";
		}
		if(id == "datosUsuariob") {
			document.getElementById("masUsuario").innerHTML="+";
		}
	}else{
		document.getElementById(id).className="visible";
	if(id == "datosFirma") {
			document.getElementById("masFirma").innerHTML="-";
		}
		if(id == "datosPaciente") {
			document.getElementById("masPaciente").innerHTML="-";
		}
		if(id == "datosEpisodio") {
			document.getElementById("masEpisodio").innerHTML="-";
		}
		if(id == "datosUsuariob") {
			document.getElementById("masUsuario").innerHTML="-";
		}
	}
}

function cambiarVisibilidadIFrame(id) {
	oIframe = document.getElementById(id);
	if (oIframe != 'undefined' && oIframe != null) {
		if (oIframe.className.indexOf("iFrameOculto") < 0) {
			oIframe.className += " iFrameOculto";
		} else {
			oIframe.className = oIframe.className.replace(" iFrameOculto", "");
		}
	}
}

function modificaLeyenda(id){
	if(document.getElementById(id).className=="visible"){
		document.getElementById(id).className="noVisible";
		document.getElementById("masLeyenda").innerHTML="+";
	}else{
		document.getElementById(id).className="visible";
		document.getElementById("masLeyenda").innerHTML="-";
	}
}



function makeFlash() {
	document.getElementById("pacientesConAvisos").style.position="absolute";
}
function cierraFlash(){
	document.getElementById("pacientesConAvisos").style.display ="none";
}


function abrirMenu() {
	document.getElementById ("menuGeneral").style.display="block";
	}
function cerrarMenu() {
	document.getElementById ("menuGeneral").style.display="none";
	}

function limpiar (form){
	for (i=0;i<form.elements.length;i++) {
		if(form.elements[i].type == 'text'){
			form.elements[i].value="";
		}
	}
}

function preguntaVista(msg1)
{
	return preguntaCerrar(msg1);
					}

/* funcion parpadear para explorer */
function shlayer(idlayer,visStr) { //v2.5

if (document.getElementById)
{ // Netscape 6 & IE 6
if (visStr == 'show') visStr = 'visible';
if (visStr == 'hide') visStr = 'hidden';
document.getElementById(idlayer).style.visibility=visStr;
} else {
if (navigator.appName == 'Netscape' && document.layers != null) // Netscape 4
{
document.layers[idlayer].style.visibility=visStr;
} else if (document.all != null) { //IE 4+
if (visStr == 'show') visStr = 'visible'; //convert vals
if (visStr == 'hide') visStr = 'hidden';
document.all[idlayer].style.visibility=visStr;}
}
}
ID2=0;
vid='hide';
function ActualizarPI()
{
if (vid=="hide") { vid="show"; } else { vid="hide"; }
shlayer('tieneVacunas',vid);
ID2=window.setTimeout("ActualizarPI();",200);
}
//FIN OOCC
function abrir(direccion, pantallacompleta, herramientas, direcciones, estado, barramenu, barrascroll, cambiatamano, ancho, alto, izquierda, arriba, sustituir){
     var opciones = "fullscreen=" + pantallacompleta +
                 ",toolbar=" + herramientas +
                 ",location=" + direcciones +
                 ",status=" + estado +
                 ",menubar=" + barramenu +
                 ",scrollbars=" + barrascroll +
                 ",resizable=" + cambiatamano +
                 ",width=" + ancho +
                 ",height=" + alto +
                 ",left=" + izquierda +
                 ",top=" + arriba;
     var ventana = window.open(direccion,"venta",opciones,sustituir);
}

function highlightElemento(el, className) {
	anyadirClass(el, className);
	el.valorActual = el.value;
}

function quitarHighlight(el, className) {
	quitarClass(el, className);
}

function quitarClass(el, className) {
	if (!(el && el.className)) {
		return;
	}
	var cls = el.className.split(" ");
	var ar = new Array();
	for (var i = cls.length; i > 0;) {
		if (cls[--i] != className) {
			ar[ar.length] = cls[i];
		}
	}
	el.className = ar.join(" ");
}

function anyadirClass(el, className) {
	quitarClass(el, className);
	el.className += " " + className;
}

function ponerEstilo(elem,estilo)
{
	elem.className=estilo;
}

function ponerSiVisible(id){
	var elem = document.getElementById(id);
	if (elem!=null)	{
		if (elem.className=="noVisibleCargando"){
			 elem.className="siVisibleCargando";
        }
	}
}

function ocultar(id){
	var elem = document.getElementById(id);
	if (elem!=null)	{
			 elem.className="noVisibleCargando";
        }
	}

