// JavaScript Document

function valEmail(valor)
{
    var re=/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/

    if(!re.exec(valor))    
        return false;
    else
        return true;    
}

function MostrarCombo(combo, datos, tipo)
{
	try{

	var oOption;
	var valor;

	$(combo).length=0;

	if(tipo==1)
	{
		oOption = document.createElement("OPTION");
		oOption.value = -1;
		oOption.text = "<Seleccione un Item>";
	}

		if (window.attachEvent) 
		  {
			 if(tipo==1)
	 			$(combo).add(oOption);

			for(var i=0; i<datos.length; i++){
				valor = datos[i];
				oOption =   document.createElement("OPTION");
				oOption.value = valor.id;
				oOption.text = valor.desc;
				$(combo).add(oOption);
			}
		  }
		  else if (window.addEventListener) 
		  {
			if(tipo == 1)
				$(combo).appendChild(oOption);

			for(var i=0; i<datos.length; i++){
				valor = datos[i];
				oOption = document.createElement("option");
				oOption.text = valor.desc;
				oOption.value = valor.id;
				$(combo).appendChild(oOption);
			 }
		  }   
	  }
		catch(e){
			alert(e.message);
		}	
}

function CargarComboBox(id_combo,combo,valor,tipo)
{				
	try{
		var opciones = {
			onSuccess: function(t){
				var datos = eval(t.responseText);
				MostrarCombo(combo, datos,tipo);
			}
		}
	var myAjax = new Ajax.Request('../../../phps/cargar_combo.php?id_combo='+id_combo+'&valor='+valor,opciones);
	}catch(e){
		alert(e.message);
	}
}

function CargarComboBoxUser(id_combo,combo,valor,tipo)
{				
	try{
		var opciones = {
			onSuccess: function(t){
				var datos = eval(t.responseText);
				MostrarCombo(combo, datos,tipo);
			}
		}
	var myAjax = new Ajax.Request('../phps/cargar_combo.php?id_combo='+id_combo+'&valor='+valor,opciones);
	}catch(e){
		alert(e.message);
	}
}

function InvertirFecha(dFecIni,dFormato){ 	//dFormato ES EL FORMATO QUE SE QUIERE
	try{
		switch(dFormato){
			case "yyyy-mm-dd":
				var dia = dFecIni.split("/")[0];
				var mes = dFecIni.split("/")[1];
				var anyo = dFecIni.split("/")[2];
				return anyo+"-"+mes+"-"+dia; 
				break;
			case "dd/mm/yyyy":	
		   		var anyo = dFecIni.split("-")[0];
				var mes = dFecIni.split("-")[1];
				var dia = dFecIni.split("-")[2];
				return dia+"/"+mes+"/"+anyo; 
				break;
		}
	}
	catch(e){
		alert(e.message);
	}
}

function FechaActual(fechaTxt){
	var opciones = {
		onSuccess: function(t){
			datos = eval(t.responseText);
			var valor = datos[0];
			$(fechaTxt).value = valor.dia+"/"+valor.mes+"/"+valor.ano;
		}
	}
	var myAjax = new Ajax.Request('../../../phps/hora_sistema.php', opciones);	
}

function fechaMayorOIgualQue(fec0, fec1)
{  
   // Devuelve 0 si fec0 > fec1
   //          1 si fec0 < fec1
   //		   2 si fec0 = fec1	
   //Formato: dd/mm/yyyy

	var bRes = 0;  
    var sDia0 = fec0.substr(0, 2);  
    var sMes0 = fec0.substr(3, 2);  
    var sAno0 = fec0.substr(6, 4);  
    var sDia1 = fec1.substr(0, 2);  
    var sMes1 = fec1.substr(3, 2);  
    var sAno1 = fec1.substr(6, 4);  

    if (sAno1 > sAno0) 
		bRes = 1;  
    else  
     	if (sAno0 == sAno1)
      		if (sMes1 > sMes0) 
				bRes = 1;  
		    else 
		       if (sMes0 == sMes1)  
        		if (sDia1 > sDia0) 
					bRes = 1;  
				else
					if (sDia1 == sDia0) 
						bRes = 2;  
    return bRes;  
} 

function formatCurrency(num) 
{
	if(num == '')
		return('0.00');
	else	
		if(num.indexOf('.') == -1)
			return(num+'.00');
		else
			return(num);
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function CompletarCeros(texto, num)
{
	var temp = "";

	if(texto != "")
	{
		var cant = texto.length;
		var falta = num - cant; 
	
		for(var i=0; i<falta; i++) 
			temp += "0";
	}
	return(trim(temp+texto));
}

function replaceChars(entry,out,add)
{
	temp = "" + entry;

	while (temp.indexOf(out)>-1) 
	{
		pos= temp.indexOf(out);
		temp = "" + ( temp.substring(0, pos) + add +
		temp.substring((pos + out.length), temp.length));
	}
	return(temp);
}

// Paneles de Configuraci贸n
var selec = 0;
var sel= "";

function seleccionar(id)
{
	if (sel != id){
		if(selec == 0)
		{			
			new Effect.SlideDown("menu_selc_"+id,{queue:{scope:'myscope', position:'end', limit: 0}});
		}
	}

	if (sel != "" & sel !=id){
		seleccionarMenos(sel);
	}
	sel = id;  
}

function seleccionarMenos(i) 
{
	new Effect.SlideUp("menu_selc_"+i,{queue:{scope:'myscope', position:'front', limit: 0}});
}

// End Paneles de Configuraci贸n

// Menu de Navegacion superior

function selecNav(nav) 
{
// End Menu de Navegacion superior
}

function mostrarMensaje(msg, div)
{
	$(div).style.visibility = 'visible';
	$(div).style.display = "";
	$(div).innerHTML = msg;
}

function RegistrarPersona()
{
	
	var nombre = $("nombre");
	var cedula = $("cedula");
	var telfofic = $("telfofic");
	var telfhab = $("telfhab");
	var celular = $("celular");
	var correo = $("correo");
	var calle = $("calle");
	var casa = $("casa");	
	var clave = $("clave");
	var repetir = $("repetir");
	var mensaje_error = "";
	var valida_registrar = true;
	InicializaBackground();

	try
	{		
			if(nombre.value==""){
				mensaje_error+="Debe ingesar el Nombre de la persona\n ";
				nombre.style.background= "#FFCCCC";
				valida_registrar = false;
			}			

			if(!cedula.present()){
				mensaje_error+="Debe ingesar el número de cédula de la persona\n ";
				cedula.style.background= "#FFCCCC";
				valida_registrar = false;
			}

			if(!celular.present()){
				mensaje_error+="Debe ingesar el número de celular de la persona\n ";
				celular.style.background= "#FFCCCC";
				valida_registrar = false;
			}

			if(!correo.present()){
				mensaje_error+="Debe ingesar el correo electrónico de la persona\n ";
				correo.style.background= "#FFCCCC";
				valida_registrar = false;
			}

			if(correo.present())
				if(!valEmail(correo.value))
				{			
					mensaje_error+="Formato de correo electrónico no válido\n ";
					correo.style.background= "#FFCCCC";
					valida_registrar = false;
				}

                        if(!calle.present()){
				mensaje_error+="Debe ingesar la calle en la que esta ubicada su casa.\n ";
				calle.style.background= "#FFCCCC";
				valida_registrar = false;
			}
			
			if(!casa.present()){
				mensaje_error+="Debe ingesar el número de casa.\n ";
				casa.style.background= "#FFCCCC";
				valida_registrar = false;
			}

			if(!clave.present())
			{
				mensaje_error+="Debe ingesar la cotraseña inicial para el usuario\n ";
				clave.style.background= "#FFCCCC";
				valida_registrar = false;
			}
			else
				if(clave.value != repetir.value)
				{
					mensaje_error+="La clave no coincide con la validación\n ";
					clave.style.background= "#FFCCCC";
					repetir.style.background= "#FFCCCC";
					valida_registrar = false;
				}			

			if(valida_registrar)
			{
				var opciones = {
					onSuccess: function(t){
						var datos = t.responseText;						
						if(datos == "1")
							{
								location.href="info_registro.php";
							}
						else
						{
							alert('Error en el envio, intentelo de nuevo');
							LimpiarFormularioPersonas();
						}
					}
				}
				var myAjax = new Ajax.Request('send_registro.php?nombre='+nombre.value+'&cedula='+cedula.value+'&telfofic='+telfofic.value+'&telfhab='+telfhab.value+'&celular='+celular.value+'&calle='+calle.value+'&casa='+casa.value+'&correo='+correo.value+'&clave='+base64_encode(clave.value), opciones);		
			}
			else
				alert(mensaje_error);
	}
	catch(e)
	{
		alert(e.message);	
	}
}

function LimpiarFormularioPersonas()
{	
	InicializaBackground();
	$("nombre").clear();
	$('cedula').clear();
	$("telfofic").clear();
	$("telfhab").clear();
	$("celular").clear();
	$("calle").clear();
	$("casa").clear();
	$("correo").clear();
	$("clave").clear();
	$("repetir").clear();
}

function LimpiarFormularioPagos()
{	
	InicializaBackgroundPagos();
	$("pago").clear();
	$('cuenta').clear();
	$("cheque").clear();
	$("nombre_titular").clear();
	$("comentario").clear();
	$("numero_deposito").clear();
	$("numero_tranferencia").clear();
	$("numero_control").clear();
	$("banco").value = "";
	$("tipo_pago").value = "1E";
	CopiarNombre();
}

function InicializaBackgroundPagos()
{
	$("pago").style.background= "#FFFFFF";
	$('cuenta').style.background= "#FFFFFF";
	$("cheque").style.background= "#FFFFFF";
	$("nombre_titular").style.background= "#FFFFFF";
	$("comentario").style.background= "#FFFFFF";
	$("numero_deposito").style.background= "#FFFFFF";
	$("banco").style.background= "#FFFFFF";
	$("numero_tranferencia").style.background= "#FFFFFF";
	$("numero_control").style.background= "#FFFFFF";
}

function InicializaBackground()
{
	$("nombre").style.background= "#FFFFFF";
	$('cedula').style.background= "#FFFFFF";
	$("telfofic").style.background= "#FFFFFF";
	$("telfhab").style.background= "#FFFFFF";
	$("celular").style.background= "#FFFFFF";
	$("calle").style.background= "#FFFFFF";
	$("casa").style.background= "#FFFFFF";
	$("correo").style.background= "#FFFFFF";
	$("clave").style.background= "#FFFFFF";
	$("repetir").style.background= "#FFFFFF";
}

function InicializaRegistroPersonas()
{
	$("Enviar").onclick = RegistrarPersona;
	$("Limpiar").onclick = LimpiarFormularioPersonas;
}

function CopiarNombre()
{
	if($("tipo_pago").value == '1E')
	{
		$("ocultar_cell_0").style.display = "";
		$("ocultar_cell_1").style.display = "none";
		$("ocultar_cell_2").style.display = "none";
		$("ocultar_cell_3").style.display = "none";
		$("ocultar_cell_4").style.display = "none";
		$("ocultar_cell_5").style.display = "none";
	}
	if($("tipo_pago").value == 'CH')
	{
		$("ocultar_cell_0").style.display = "";
		$("ocultar_cell_1").style.display = "";
		$("ocultar_cell_2").style.display = "";
		$("ocultar_cell_3").style.display = "";
		$("ocultar_cell_4").style.display = "none";
		if ($("banco").value == 'BANESCO')
			{
				$("ocultar_cell_5").style.display = "";
			}
	}
	if($("tipo_pago").value == 'TE')
	{
		$("ocultar_cell_0").style.display = "none";
		$("ocultar_cell_1").style.display = "none";
		$("ocultar_cell_2").style.display = "";
		$("ocultar_cell_3").style.display = "";
		$("ocultar_cell_4").style.display = "";
		$("ocultar_cell_5").style.display = "none";
	}
}

function CambiarBanco()
{
	if($("tipo_pago").value == 'CH' && $("banco").value == 'BANESCO')
	{
		$("ocultar_cell_5").style.display = "";
	}
	else
	{
		$("ocultar_cell_5").style.display = "none";
	}

}


function RegistrarPagos()
{
	var pago = $("pago");
	var tipo_pago = $("tipo_pago");
	var cuenta = $("cuenta");
	var cheque = $("cheque");
	var nombre_titular = $("nombre_titular");
	var comentario = $("comentario");
	var numero_deposito = $("numero_deposito");
	var banco = $("banco");
	var numero_tranferencia = $("numero_tranferencia");
	var numero_control = $("numero_control");
	var mensaje_error = "";
	var valida_registrar = true;

	InicializaBackgroundPagos();

	try
	{		
			if(!pago.present()){
				mensaje_error+="Debe ingesar un Monto a pagar.\n ";
				pago.style.background= "#FFCCCC";
				valida_registrar = false;
			}	
			if((tipo_pago.value == '1E' || tipo_pago.value == 'CH') && numero_deposito.value == ""){
				mensaje_error+="Debe indicar el Número de Deposito.\n ";
				numero_deposito.style.background= "#FFCCCC";
				valida_registrar = false;
			}
			if(tipo_pago.value != '1E' && nombre_titular.value == ""){	
				mensaje_error+="Debe ingesar el Nombre del Titular.\n ";
				nombre_titular.style.background= "#FFCCCC";
				valida_registrar = false;
			}
			if(tipo_pago.value != '1E' && banco.value == ""){	
				mensaje_error+="Debe seleccionar una Entidad Bancaria.\n ";
				banco.style.background= "#FFCCCC";
				valida_registrar = false;
			}
			if(tipo_pago.value == 'CH' && cuenta.value == ""){
				mensaje_error+="Debe indicar el Número de Cuenta.\n ";
				cuenta.style.background= "#FFCCCC";
				valida_registrar = false;
			}
			if(tipo_pago.value == 'CH' && cheque.value == ""){
				mensaje_error+="Debe indicar el Número de Cheque.\n ";
				cheque.style.background= "#FFCCCC";
				valida_registrar = false;
			}
			if(tipo_pago.value == 'CH' && banco.value == "BANESCO" && numero_control.value.length != 14){	
				mensaje_error+="El Número de Control debe contener 14 digitos.\n ";
				numero_control.style.background= "#FFCCCC";
				valida_registrar = false;
			}
			if(tipo_pago.value == 'TE' && numero_tranferencia.value == ""){
				mensaje_error+="Debe indicar el Núero de Transferencia.\n ";
				numero_tranferencia.style.background= "#FFCCCC";
				valida_registrar = false;
			}

			if(valida_registrar)
			{
				var opciones = {
					onSuccess: function(t){
						var datos = t.responseText;
						if(datos == "1")
							{
								alert('Su Pago fue enviado Exitosamente.');
								location.href="../phps/info_pago.php";
							}
						else
						{
							alert('Error en el envio, intentelo de nuevo.');
							
						}
						LimpiarFormularioPagos();
					}
				}
				if (tipo_pago.value == '1E')
					var myAjax = new Ajax.Request('../phps/send_pagos.php?pago='+pago.value+'&tipo_pago='+tipo_pago.value+'&comentario='+comentario.value+'&numero_deposito='+numero_deposito.value, opciones);		
				if (tipo_pago.value == 'CH')
					var myAjax = new Ajax.Request('../phps/send_pagos.php?pago='+pago.value+'&tipo_pago='+tipo_pago.value+'&cuenta='+cuenta.value+'&cheque='+cheque.value+'&nombre_titular='+nombre_titular.value+'&comentario='+comentario.value+'&numero_deposito='+numero_deposito.value+'&banco='+banco.value+'&numero_control='+numero_control.value, opciones);		
				if (tipo_pago.value == 'TE')
					var myAjax = new Ajax.Request('../phps/send_pagos.php?pago='+pago.value+'&tipo_pago='+tipo_pago.value+'&nombre_titular='+nombre_titular.value+'&comentario='+comentario.value+'&banco='+banco.value+'&numero_tranferencia='+numero_tranferencia.value, opciones);		
				}
			else
				alert(mensaje_error);
	}
	catch(e)
	{
		alert(e.message);	
	}
}

function InicializaInfoRegistro()
{
	$("Cerrar").onclick = function locationHref(){location.href = "http://www.avva.com.ve";}
}

function InicializaInfoPagos()
{
	$("Cerrar").onclick = function locationHref(){location.href = "../pagos/v_pagos_casa.php";}
}

function CerrarRedireccionar()
{
		location.href = "../index.php";	
}

function InicializaEnviarPagos()
{
	CargarComboBoxUser(4,"tipo_pago","",2);
	$("Enviar").onclick = RegistrarPagos;
	$("Limpiar").onclick = LimpiarFormularioPagos;
	$("tipo_pago").onchange = CopiarNombre;	
	$("banco").onchange = CambiarBanco;	
}
