// JavaScript Document


var mensajeError = new Array();
mensajeError['campoVacio'] = "El campo no puede estar Vacío";
mensajeError['soloLetras'] = "Ingrese sólo carácteres Alfabéticos";
mensajeError['soloNumeros'] = "Ingrese sólo Números";
mensajeError['valorDecimal'] = "El dato debe ser Décimal";
mensajeError['cedulaIncorrecta'] = "El formato de la Cédula es incorrecto";
mensajeError['RIFIncorrecto'] = "El formato de la RIF es incorrecto";
mensajeError['NITIncorrecto'] = "El formato de la NIT es incorrecto";
mensajeError['fechaIncorrecta'] = "El formato de la Fecha es incorrecto (dd/mm/aaa)";
mensajeError['diaError'] = "El día debe estar entre 1 y 31";
mensajeError['mesError'] = "El mes debe estar entre 1 y 12";
mensajeError['anoError'] = "El año debe ser mayor a 1900";
mensajeError['soloLetrasonumeros'] = "Los caracteres no son validos";
mensajeError['soloUnNumero'] = "Ingrese sólo un Número";

// ****************************************************************
function verificarCampo(campo)
{
	if (campo.value=="")
	{
		/*if(campo.name=="tbNombreResponsable")
			mensaje(mensajeError['campoVacio'], mensajeCampos['Nombre0']);
		else*/
			mensaje(mensajeError['campoVacio']);
		seleccionar(campo);
	}
}
// ****************************************************************
function refrescar(forma)
{	
	alert(forma.precio);
	forma.submit();		
	
}
// ****************************************************************
function agregarMetodo(forma,metodo)
{
	forma.METODO.value = metodo;
}

// ****************************************************************
function formatearDecimal(numero)
{
	var Numero = new String(numero);
	var n_dec,i;

	Decimal="";
	n_dec=0;
	for(i=0; i<Numero.length; i++)
	{
		if(Numero.charAt(i)!="." && n_dec==0)
			Decimal+=Numero.charAt(i);
		else
		{
			Decimal+=Numero.charAt(i);
			n_dec++;				
			if(n_dec==3)
				break;
		}
	}
	return Decimal;
}

// ****************************************************************
function asignarValor(campo1,campo2,resultado)
{
	var Valor;
	
	if(campo1.value!="" && campo2.value!="")
	{
		Valor = eval( campo2.value / Math.pow(eval(campo1.value),2) );
		resultado.value = formatearDecimal(Valor);
	}
}

// ****************************************************************
function validarEnter(forma,campo,indiceCampo)
{
	var i;
	var texto = new String(campo.value);
	var textoNuevo = new String();
	for(i=0; i<texto.length; i++)
	
	{
		if(texto.charAt(i)=="\r")
			textoNuevo+="";
		else 
			if(texto.charAt(i)=="\n")
				textoNuevo+=" ";		
			else
				textoNuevo+=texto.charAt(i);
	}
	switch (indiceCampo)
	{
		case 1 : forma.direccion.value = textoNuevo; break;
		case 2 : forma.historiaPaciente.value = textoNuevo; break;
		case 3 : forma.resumenPaciente.value = textoNuevo; break;
		case 4 : forma.historialFamiliar.value = textoNuevo; break;
		case 5 : forma.examenFisico.value = textoNuevo; break;
		case 6 : forma.detalle.value = textoNuevo; break;
		case 7 : forma.resumen.value = textoNuevo; break;
	}
	
}
// ****************************************************************
function validarCodigo(campo)
{
	var Expresion = new RegExp("^[a-zA-Z0-9]+\.?$");
	var Texto = new String(campo.value);
	var i, codigoAscii;

	if(campo.value!="")
	{
		if (!Expresion.test(campo.value))
		{
			mensaje(mensajeError['soloLetrasonumeros']);
			seleccionar(campo);
		}
	}
}
// ****************************************************************
function validarNombre(campo)
{
	var Expresion = new RegExp("^[a-zA-Z]+\.?$");
	var Texto = new String(campo.value);
	var i, codigoAscii;

	if(campo.value!="")
	{
		if (!Expresion.test(campo.value))
		{
			mensaje(mensajeError['soloLetrasonumeros']);
			seleccionar(campo);
		}
	}
}
// ****************************************************************
function validarCPrecios(campo)
{
	var i;
	var Expresion = new RegExp("^[0-9]{1}$");

	if(campo.value!="")
	{
		if (!Expresion.test(campo.value))
		{
			mensaje(mensajeError['soloUnNumero']);
			seleccionar(campo);
		}
	}
}

// ****************************************************************
function validarTexto(campo)
{
	var Expresion = new RegExp("^[a-zñA-ZÑáéíóú]+\.?\s?[a-zñA-ZÑáéíóú]+\.?$");
	var Texto = new String(campo.value);
	var i, codigoAscii;

	if(campo.value!="")
	{
		if (!Expresion.test(campo.value))
		{
			mensaje(mensajeError['soloLetras']);
			seleccionar(campo);
		}
	}
}

// ****************************************************************
function validarNumero(campo)
{
	var i;
	var Expresion = new RegExp("^[0-9]+$");

	if(campo.value!="")
	{
		if (!Expresion.test(campo.value))
		{
			mensaje(mensajeError['soloNumeros']);
			seleccionar(campo);
		}
	}
}

// ****************************************************************
function validarDecimal(campo)
{
	var Expresion = new RegExp("^[0-9]{1,3}(\.|,)[0-9]{1,3}$");

	if(campo.value!="")
	{
		if (!Expresion.test(campo.value))
		{
			mensaje(mensajeError['valorDecimal']);
			seleccionar(campo);
		}	
	}
}
// ****************************************************************
function validarDecimal8(campo)
{
	var Expresion = new RegExp("^[0-9]{1,8}(\.|,)[0-9]{1,2}$");

	if(campo.value!="")
	{
		if (!Expresion.test(campo.value))
		{
			mensaje(mensajeError['valorDecimal']);
			seleccionar(campo);
		}	
	}
}

// ****************************************************************
function validarCedula(campo)
{
	
	var Expresion = new RegExp("^[0-9]{1,2}\.?[0-9]{3}\.?[0-9]{3}$");
	var Texto = new String(campo.value);
	//alert(Texto);
	if(campo.value!="")
	{
		if (!Expresion.test(campo.value))
		{
			mensaje(mensajeError['cedulaIncorrecta']);
			seleccionar(campo);
		}
	}
}
function validarRIF(campo)
{
	var Expresion = new RegExp("^[0-9]{1,2}\.?[0-9]{3}\.?[0-9]{3}$");
	var Texto = new String(campo.value);
	//alert(Texto);
	if(campo.value!="")
	{
		if (!Expresion.test(campo.value))
		{
			mensaje(mensajeError['RIFIncorrecto']);
			seleccionar(campo);
		}
	}
}
function validarNIT(campo)
{
	
	var Expresion = new RegExp("^[0-9]{1,2}\.?[0-9]{3}\.?[0-9]{3}$");
	var Texto = new String(campo.value);
	//alert(Texto);
	if(campo.value!="")
	{
		if (!Expresion.test(campo.value))
		{
			mensaje(mensajeError['NITIncorrecto']);
			seleccionar(campo);
		}
	}
}

// ****************************************************************
function validarFecha(campo)
{
	var Expresion = new RegExp("[0-9]{2}(/|-)[0-9]{2}(/|-)[0-9]{4}");
	var fechaCompleta = new String(campo.fechaNacimiento.value);
	var fechaActual = new Date();
	var dia, mes, ano, edad;

	if(campo.fechaNacimiento.value!="")
	{
		if (!Expresion.test(campo.fechaNacimiento.value))
		{
			mensaje(mensajeError['fechaIncorrecta']);
			seleccionar(campo.fechaNacimiento);
		}	
	
		dia = fechaCompleta.substring(0,2);
		mes = fechaCompleta.substring(3,5);
		ano = fechaCompleta.substring(6,10);

		if((parseInt(dia)>31 || parseInt(dia)==0) && (dia!="08" && dia!="09"))
		{
			mensaje(mensajeError['diaError']);
			seleccionar(campo.fechaNacimiento);
		}
		if((parseInt(mes)>12 || parseInt(mes)==0) && (mes!="08" && mes!="09"))
		{
			mensaje(mensajeError['mesError']);
			seleccionar(campo.fechaNacimiento);
		}
		if(parseInt(ano)<1900 || parseInt(ano)==0)
		{
			mensaje(mensajeError['anoError']);
			seleccionar(campo.fechaNacimiento);
		}
	
		edad = fechaActual.getFullYear() - parseInt(ano);
		if((fechaActual.getMonth()+1)<parseInt(mes))
			edad--;
		else
		{
			if((fechaActual.getMonth()+1)==parseInt(mes))
			{
				if(fechaActual.getDate()<parseInt(dia))
					edad--;
			}
		}
		campo.edad.value = parseInt(edad);
	}

}

// ****************************************************************
function mensaje(mensaje, a)
{
	alert(mensaje);
}
// ****************************************************************
function seleccionar(campo)
{
	campo.focus();
	campo.select();	
	return false;
}

// ****************************************************************
function deshabilitarCampo(forma)
{
	var i;
	var Valor = new String();
	for(i=0; i<forma.length; i++)
	{
		Valor = forma.elements[i].name;
		if(Valor.substring(0,11)=="Tratamiento")
		{
			forma.elements[i].disabled = true;
			if(forma.elements[i].checked)
			{
				i++;
				forma.elements[i].className = "imagenDeshabilitada";
				forma.elements[i].disabled = true;
			}
		}						
	}
}

// ****************************************************************
function habilitarCampo(forma)
{
	var i;
	var Valor = new String();
	for(i=0; i<forma.length; i++)
	{
		Valor = forma.elements[i].name;
		if(Valor.substring(0,11)=="Tratamiento")
		{
			forma.elements[i].disabled = false;
			if(forma.elements[i].checked)
			{
				i++;
				forma.elements[i].className = "imagenHabilitada";
				forma.elements[i].disabled = false;
			}
		}						
	}
}

// ****************************************************************
function habilitarBoton(forma,campo,numero)
{
	var i;

	switch (numero)
	{
		case 1 : Nombre="Dosis"; break;
		case 2 : Nombre="Exploracion"; break;	
	}
	for(i=0; i<forma.length; i++)
	{		
		if(forma.elements[i].name==Nombre+campo.id)
		{
			forma.elements[i].disabled = !forma.elements[i].disabled;
			if(forma.elements[i].disabled)
				forma.elements[i].className = "imagenDeshabilitada";
			else
				forma.elements[i].className = "imagenHabilitada";
		}			
			
	}
}

// ****************************************************************
function cargarPrincipal()
{
	window.open('CMP.htm', 'Inicio','width = 900, height = 670, resizable = no'); 
	window.opener = window.self;
}

// ****************************************************************
function cargarMonto(forma, idConcepto, costos, fila)
{
	var i, j, k, arreglo= new Array();
	var Valor = new String();
	var subTotal = 0;
	
	k=0;
	j=0;
	for(i=0; i<=costos.length; i++)
	{
		if(costos.substring(i,i+1)  ==',' || i==costos.length)
		{
			arreglo[j]=costos.substring(k,i-1);
			i++;
			k=i;
			j++;
		}
	}
	Nombre = "monto";
	for(i=0; i<forma.length; i++)
	{		
		if(forma.elements[i].name==Nombre+fila)
		{
				forma.elements[i].value = arreglo[idConcepto.value-1];
		}
	}
	realizarSumas(forma);
}


//****************************************************************
function realizarSumas(forma)
{
	var subt = 0;
	for(i=0; i<forma.length; i++)
	{		
		Valor = forma.elements[i].name;
		if(Valor.substring(0,5)=="monto")
		{						
			subt+= parseInt(forma.elements[i].value);
		}
	}
	forma.subTotal.value = subt;	
	forma.total.value = eval(subt - forma.descuento.value);	
}

// ****************************************************************
function cargarMetodo(forma,clase)
{	
	var i;
	switch (clase)
	{
		case 1: {
					forma.METODO.value="modificarProducto";
					forma.submit();
					break;
				}	
		case 2: {
					forma.METODO.value="agregarProductos";
					forma.submit();
					break;
				}	
         				
	}		
}

//******************************************************************************************
function validarPalabra(forma,palabra)
{
	
	if (forma.criterio.value==1)
		validarCodigo(palabra);
	if (forma.criterio.value==2)
		validarNombre(palabra);
	if (forma.criterio.value==4)
		validarDecimal(palabra);
	
}
//******************************************************************************************
function validarPalabraClientes(forma,palabra)
{
	
	if (forma.criterio.value==1)
		validarCodigo(palabra);
	if (forma.criterio.value==2)
		validarNombre(palabra);
	if (forma.criterio.value==3)
		validarCedula(palabra);
	if (forma.criterio.value==4)
		validarNombre(palabra);
	
}
//******************************************************************************************
function validarPalabraProveedores(forma,palabra)
{
	
	if (forma.criterio.value==1)
		validarCodigo(palabra);
	if (forma.criterio.value==2)
		validarNombre(palabra);
	if (forma.criterio.value==3)
		validarCedula(palabra);
	if (forma.criterio.value==4)
		validarCodigo(palabra);
	if (forma.criterio.value==5)
		validarCodigo(palabra);
	
}
//****************************************************************************************
function validarFechas(campo)
{

	var Expresion = new RegExp("[0-9]{2}(/|-)[0-9]{2}(/|-)[0-9]{4}");
	var fechaCompleta = new String(campo.value);
	var dia, mes, ano;
	
	if(campo.value!="")
	{
		if (!Expresion.test(campo.value))
		{
			mensaje(mensajeError['fechaIncorrecta']);
			seleccionar(campo);
		}	
		dia = fechaCompleta.substring(0,2);
		mes = fechaCompleta.substring(3,5);
		ano = fechaCompleta.substring(6,10);

		if((parseInt(dia)>31 || parseInt(dia)==0) && (dia!="08" && dia!="09"))
		{
			mensaje(mensajeError['diaError']);
			seleccionar(campo);
		}
		if((parseInt(mes)>12 || parseInt(mes)==0) && (mes!="08" && mes!="09"))
		{
			mensaje(mensajeError['mesError']);
			seleccionar(campo);
		}
		if(parseInt(ano)<1900 || parseInt(ano)==0)
		{
			mensaje(mensajeError['anoError']);
			seleccionar(campo);
		}
		
	}
}




