function salir(){
	window.close();
}
function validaComboIG(combo,opcion){
var flag=true
	if(combo.length==0){
		flag=false;
		alert("Favor de dar de alta al menos un concepto");
		abreConcepto(opcion);
	}
		
	return flag;
}

function borra(tipo,id){

var yy=document.frmFecha.selfechayy.selectedIndex;
var mm=document.frmFecha.selfechamm.selectedIndex+1;
var dd=document.frmFecha.selfechadd.selectedIndex+1;

var YY=document.frmFecha.selfechaYY.selectedIndex;
var MM=document.frmFecha.selfechaMM.selectedIndex+1;
var DD=document.frmFecha.selfechaDD.selectedIndex+1;



var st1="borraRegistro.php?tipo="+tipo+"&id="+id+"&yy="+yy+"&mm="+mm+"&dd="+dd+"&YY="+YY+"&MM="+MM+"&DD="+DD;
	if (tipo==1) {
		x=confirm("Esta Seguro de querer borrar el Ingreso");
		if (x){
			document.frmIngresos.method="post";
			document.frmIngresos.action=st1;
			document.frmIngresos.submit();
		}
	}else{
		x=confirm("Esta Seguro de querer borrar el Gasto");
		if (x){
			document.frmGastos.method="post";
			document.frmGastos.action=st1;
			document.frmGastos.submit();
		}
	}
	
}
function editaAntes(tipo,id){

var yy=document.frmFecha.selfechayy.selectedIndex;
var mm=document.frmFecha.selfechamm.selectedIndex+1;
var dd=document.frmFecha.selfechadd.selectedIndex+1;

var YY=document.frmFecha.selfechaYY.selectedIndex;
var MM=document.frmFecha.selfechaMM.selectedIndex+1;
var DD=document.frmFecha.selfechaDD.selectedIndex+1;

edita(tipo,id,yy,mm,dd,YY,MM,DD)
}

function edita(tipo,id,yy,mm,dd,YY,MM,DD) {
   var ancho=400;
   var alto=450;
   var derecha=(screen.width-ancho)/2;
   var arriba=(screen.height-alto)/2;
   if (tipo==1) {
window.open("editaIngreso.php?idingreso="+id+"&yy="+yy+"&mm="+mm+"&dd="+dd+"&YY="+YY+"&MM="+MM+"&DD="+DD
,"x","top="+arriba+",left="+derecha+",width="+ancho+",height="+alto);
   }else{
window.open("editaGasto.php?idgasto="+id+"&yy="+yy+"&mm="+mm+"&dd="+dd+"&YY="+YY+"&MM="+MM+"&DD="+DD
,"x","top="+arriba+",left="+derecha+",width="+ancho+",height="+alto);
   }
}

function abreConcepto(opcion) {
   var ancho=500;
   var alto=420;
   var arriba=(screen.width-ancho)/2;
   var derecha=(screen.height-alto)/2;

window.open("nuevoConcepto.php?opcion="+opcion,"plain","resizable, scrollbars, top="+arriba+",left="+derecha+",width="+ancho+",height="+alto);
}

function sigueGasto(){
	document.frmGastos.submit();
}


function sigueIngreso(){
	document.frmIngresos.submit();
}

function borraConcepto(id){
var s="Al borrar el concepto se borraran todos los registros con este concepto, desea continuar?";

var x=confirm(s);
	//alert('primer:' + x);
	if (x){
		s="La Informacion aqui borrada no podra ser recuperada, desea continuar?";
		//var y=confirm('segundo' + x);
		var y=confirm(s);
		//alert(y);
		if (y) {
			document.frmConcepto.action="borraConcepto.php?id="+id;
			document.frmConcepto.submit();
		}
	}
}

function revision(id,nombre,opcion){
	//alert(nombre);
	window.location.href="nuevoConcepto.php?idconcepto="+id+"&nombre="+nombre+"&opcion="+opcion;
}
function nuevo(){
	window.location.href="nuevoConcepto.php";
}

function abregraficaIngresos(){
   var ancho=400;
   var alto=400;
   var derecha=(screen.width-ancho)/2;
   var arriba=(screen.height-alto)/2;
   window.open("graficaIngresos.php","x","top="+arriba+",left="+derecha+",width="+ancho+",height="+alto);
}

function validaDescripcion(f_descripcion){
	f_descripcion.value=validaTexto(f_descripcion.value);
}

function validaTexto(texto){
var r,re;
	re=/[,;'"`´]/g;
	r=texto.replace(re, " ");

var match = r.match(/[a-zA-Z_0-9-ñÑ:!#$%&/()=?¿.+_\s]*/);
	if (match==null){
		match='';
	}
	return(match);
}

function validaNumero(numero){
	var r,re;
	re=/,/g;
	r=numero.replace(re, "");
	var match = r.match(/[1-9]{0,1}[0-9]*[.]{0,1}[0-9]{1,2}/);
	if (match==null){
		match=0
	}
	return(match)
}

function seleccionaCombo(objeto,seleccion){
	objeto.selectedIndex=seleccion;
}

function llenaDias( listname, size, month, isLeapYear, selectValue, handlerText ) {
	var numDays = 31;
	var resultStr = "";

	// Default to the string "DaysOfMonth"
	if (listname+"" == "undefined" || (listname == null))
		listname = "DaysOfMonth";
	// Default to a drop-down list
	if (size+"" == "undefined" || (size == null))
		size = 1;
	// Return null to report an error if no valid month entered
	if (month+"" == "undefined" || (month == null))
		return null;
	// Default to a non-leap year
	if (isLeapYear+"" == "undefined" || (isLeapYear == null))
		isLeapYear = false;

	// Call out to external Script Library function, DaysInMonth
	numDays = DaysInMonth(month, isLeapYear);
	
	for (var i=1; (i <= 31 && i <= numDays); i++) {
		listname.options[listname.length]=new Option(i);
	}
	
	listname.selectedIndex=selectValue-1
} // end llenaDias

function llenaMes( listname, size, selectValue, handlerText ) {
	var resultStr = "";
	var monthArr = new Array(12);
	
	// Initialize month array
	monthArr[0] = "Ene";
	monthArr[1] = "Feb";
	monthArr[2] = "Mar";
	monthArr[3] = "Abr";
	monthArr[4] = "May";
	monthArr[5] = "Jun";
	monthArr[6] = "Jul";
	monthArr[7] = "Ago";
	monthArr[8] = "Sep";
	monthArr[9] = "Oct";
	monthArr[10] = "Nov";
	monthArr[11] = "Dic";
	
	// Default to name of "Months"
	if (listname+"" == "undefined" || (listname == null))
		listname = "Months";
	// Default to a drop-down list
	if (size+"" == "undefined" || (size == null))
		size = 1;	
	
	for (var i=1; (i <= 12); i++) {
		listname.options[listname.length]=new Option(i)
	}
	for (var i=0; (i < 12); i++) {
		listname.options[i].text=monthArr[i];
	}
	
	for (var i=0; (i < 12); i++) {
                listname.options[i].value=i+1;
        }	
} // end llenaMes

function bisiesto(intYear) {
if (intYear % 100 == 0) {
if (intYear % 400 == 0) { return true; }
}
else {
if ((intYear % 4) == 0) { return true; }
}
return false;
}

function listaMes(objetomm){
llenaMes(objetomm,1,1)
}

function borraDias(objetodd){
var selLength =objetodd.length;
	for (i=selLength;i>=0;i--)
	{
		objetodd.remove(i);
	}
}
function listaDias(objetoyy,objetomm,objetodd){
borraDias(objetodd)
//var mSeleccionado=objetomm.options[objetomm.selectedIndex].value
mSeleccionado=objetomm.selectedIndex+1
var yy,leapYear;
//yy=parseInt(objetoyy.options[objetoyy.selectedIndex].value)
yy=parseInt(objetoyy.value)
var leapYear=bisiesto(yy)
	llenaDias(objetodd, 1, mSeleccionado,leapYear)
}


/* ======================================================================
FUNCTION: DaysInMonth 
 
INPUT: 	monthNum (integer)	: the number of the month (1-12)
			isLeapYear (boolean) : true indicates a leap year

RETURN:	the number of days in the month specified

PLATFORMS:	Netscape Navigator 3.01 and higher,
			  	Microsoft Internet Explorer 3.02 and higher,
			  	Netscape Enterprise Server 3.0,
			  	Microsoft IIS/ASP 3.0.
====================================================================== */
function DaysInMonth( monthNum, isLeapYear ) {
	var days = 31;
	
	// Return null to report an error if parameter not specified
	if (monthNum+"" == "undefined" || (monthNum == null))
		return null;
	// Default to a non-leap year if not specified
	if (isLeapYear+"" == "undefined" || (isLeapYear == null))
		isLeapYear = false;	

	if (monthNum == 1)
		days = 31;
	else if (monthNum == 2)
		if (isLeapYear)
			days = 29;
		else
			days = 28;
	else if (monthNum == 3)
		days = 31;
	else if (monthNum == 4)
		days = 30;
	else if (monthNum == 5)
		days = 31;
	else if (monthNum == 6)
		days = 30;
	else if (monthNum == 7)
		days = 31;
	else if (monthNum == 8)
		days = 31;
	else if (monthNum == 9)
		days = 30;
	else if (monthNum == 10)
		days = 31;
	else if (monthNum == 11)
		days = 30;
	else if (monthNum == 12)
		days = 31;
		
	return days;
} // end DaysInMonth

function suma(f_subtotal,f_iva,f_total,f_check,opcion){ 
var subtotal,iva=0

	f_subtotal.value=validaNumero(f_subtotal.value);
	f_iva.value=validaNumero(f_iva.value);
	
	subtotal=f_subtotal.value;
	if ((f_check.checked)&&(opcion==1)) {
		f_iva.value=calculaIva(subtotal)
	}
	iva=f_iva.value;
	
	f_total.value=eval(subtotal)+eval(iva);
	f_total.value=validaNumero(f_total.value);
}



function sumaH(f_subtotal,f_iva,f_retiva,f_retisr,f_total,f_check,opcion){
var subtotal,iva,retiva,retisr=0

        f_subtotal.value=validaNumero(f_subtotal.value);
        f_iva.value=validaNumero(f_iva.value);
        f_retiva.value=validaNumero(f_retiva.value);
        f_retisr.value=validaNumero(f_retisr.value);

        subtotal=f_subtotal.value;
        if ((f_check.checked)&&(opcion==1)) {
                f_iva.value=calculaIva(subtotal);
                f_retiva.value=calculaRetiva(subtotal);
                f_retisr.value=calculaRetisr(subtotal);
        }
        iva=f_iva.value;
        retiva=f_retiva.value;
        retisr=f_retisr.value;

        f_total.value=eval(subtotal)+eval(iva)-eval(retiva)-eval(retisr);
        f_total.value=validaNumero(f_total.value);
}



function calculaIva(subtotal){
	var iva=subtotal*0.15;
	return iva;
}
function calculaRetiva(subtotal){
	var retiva=subtotal*0.1;
	return retiva;
}
function calculaRetisr(subtotal){
	var retisr=subtotal*0.1;
	return retisr;
}

function checaIva(f_subtotal,f_iva,f_total,f_check){
	if (!f_check.checked) {
		f_iva.value=0;
	}else{
		f_iva.value=calculaIva(validaNumero(f_subtotal.value));
	}
 	suma(f_subtotal,f_iva,f_total,f_check,2);

}
function checaIvaH(f_subtotal,f_iva,f_retiva,f_retisr,f_total,f_check){
        if (!f_check.checked) {
                f_iva.value=0;
		f_retiva.value=0;
		f_retisr.value=0;
        }else{
                f_iva.value=calculaIva(validaNumero(f_subtotal.value));
		f_retiva.value=calculaRetiva(validaNumero(f_subtotal.value));
		f_retisr.value=calculaRetisr(validaNumero(f_subtotal.value));
        }
        sumaH(f_subtotal,f_iva,f_retiva,f_retisr,f_total,f_check,2);

}


