function redirectparent(url) {
    if (url==undefined) {	
        if(window.opener) {
            window.opener.top.location.href = 'index.php';
            window.close();
        }else{
            window.open('index.php','enpazz_index','width=510,height=420,left=470,top=195,status=yes,toolbar=yes,menubar=yes,location=yes,directories=yes,resizable=yes');
        }
	  }else{
        if(window.opener) {
            window.opener.top.location.href = url;
            window.close();
        }else{
            window.open(url,'enpazz_index','width=510,height=420,left=470,top=195,status=yes,toolbar=yes,menubar=yes,location=yes,directories=yes,resizable=yes');
        } 
    }
}    

var nav4 = window.Event ? true : false;

function OnlyAcceptNumbers(evt) { 
    // NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57 
    var key = nav4 ? evt.which : evt.keyCode; 
    return (key <= 13 || (key >= 48 && key <= 57));
}

function ValidateEmail(valor) {
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)) {
        return (true)
    }else{
        alert("La dirección de email es incorrecta.");
        return (false);
    }
}

function ismaxlength(obj) {
    var mlength = obj.getAttribute ? parseInt(obj.getAttribute("maxlength")) : ""
    if(obj.getAttribute && obj.value.length>mlength)
        obj.value=obj.value.substring(0,mlength)
}

function redondear(num, dec) { 
    if (dec==2)	pow=100;
    if (dec==6)	pow=1000000;
    num = parseFloat(num); 
    dec = parseFloat(dec); 
    dec = (!dec ? 2 : dec); 
    return Math.round(num * pow) / pow; 
}

var aFinMes = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 

function finMes(nMes, nAno) {
    return aFinMes[nMes - 1] + (((nMes == 2) && (nAno % 4) == 0)? 1: 0); 
} 

function padNmb(nStr, nLen, sChr) {
    var sRes = String(nStr); 
    for (var i = 0; i < nLen - String(nStr).length; i++) sRes = sChr + sRes; return sRes;
}

function makeDateFormat(nDay, nMonth, nYear) {
    var sRes; sRes = padNmb(nDay, 2, "0") + "/" + padNmb(nMonth, 2, "0") + "/" + padNmb(nYear, 4, "0"); 
    return sRes; 
} 

function incDate(sFec0)
{ var nDia = parseInt(sFec0.substr(0, 2), 10); 
  var nMes = parseInt(sFec0.substr(3, 2), 10); 
	var nAno = parseInt(sFec0.substr(6, 4), 10); 
	nDia += 1; 
	if (nDia > finMes(nMes, nAno))
	{ nDia = 1; nMes += 1; if (nMes == 13){ nMes = 1; nAno += 1; }
	} 
	return makeDateFormat(nDia, nMes, nAno); } 
	
function decDate(sFec0) {
    var nDia = Number(sFec0.substr(0, 2)); 
    var nMes = Number(sFec0.substr(3, 2)); 
	  var nAno = Number(sFec0.substr(6, 4)); 
	  nDia -= 1; if (nDia == 0)
	  { nMes -= 1; if (nMes == 0){ nMes = 12; nAno -= 1; } 
	  nDia = finMes(nMes, nAno); } return makeDateFormat(nDia, nMes, nAno); 
} 

function addToDate(sFec0, sInc) {
    var nInc = Math.abs(parseInt(sInc)); 
    var sRes = sFec0; if (parseInt(sInc) >= 0) 
	  for (var i = 0; i < nInc; i++) 
	  sRes = incDate(sRes); else for (var i = 0; i < nInc; i++) 
	  sRes = decDate(sRes); return sRes; 
} 
	
function fecha_ok(ctl) { 
// enforce date later than sysdate 
    var cal = new Array(); 
    cal.JAN = "January"; 
    cal.FEB = "February"; 
    cal.MAR = "March"; 
    cal.APR = "April"; 
    cal.MAY = "May"; 
    cal.JUN = "June"; 
    cal.JUL = "July"; 
    cal.AUG = "August"; 
    cal.SEP = "September"; 
    cal.OCT = "October"; 
    cal.NOV = "November"; 
    cal.DEC = "December"; 

    var sysDate = new Date(); 
    var effDateChar = ctl.value; // P_EFFECTIVE_DATE 

    // translate date entered by user into JS format 
    var bufArray = effDateChar.split("-"); 
    var effDateMonth = cal[bufArray[1].toUpperCase()]; 
    var effDateDay = bufArray[0]; 
    var effDateYear = bufArray[2]; 
    var effDateDate = new Date(effDateMonth+" "+ effDateDay + 
                       ", "+effDateYear+" 23:59:59"); 

    // compare the 2 dates 
    if (effDateDate < sysDate) {
        //alert("The effective date must be today's date or a later date."); 
        ctl.focus(); 
        return false; 
    } 
    return true; 
}

function esDigito(sChr) {
    var sCod = sChr.charCodeAt(0); 
    return ((sCod > 47) && (sCod < 58)); 
} 

function valSep(oTxt) {
    var bOk = false; 
    bOk = bOk || ((oTxt.value.charAt(2) == "-") && (oTxt.value.charAt(5) == "-")); 
    bOk = bOk || ((oTxt.value.charAt(2) == "/") && (oTxt.value.charAt(5) == "/")); 
    return bOk; 
} 

function finMes(oTxt) { 
    var nMes = parseInt(oTxt.value.substr(3, 2), 10); 
    var nAno = parseInt(oTxt.value.substr(6), 10); 
    var nRes = 0; 
    switch (nMes) { 
        case 1: nRes = 31; break; 
        case 2: nRes = 28; break; 
        case 3: nRes = 31; break; 
        case 4: nRes = 30; break; 
        case 5: nRes = 31; break; 
        case 6: nRes = 30; break; 
        case 7: nRes = 31; break; 
        case 8: nRes = 31; break; 
        case 9: nRes = 30; break; 
        case 10: nRes = 31; break; 
        case 11: nRes = 30; break; 
        case 12: nRes = 31; break; 
    } 
    return nRes + (((nMes == 2) && (nAno % 4) == 0)? 1: 0); 
} 

function valDia(oTxt){
    var bOk = false; 
    var nDia = parseInt(oTxt.value.substr(0, 2), 10); 
    bOk = bOk || ((nDia >= 1) && (nDia <= finMes(oTxt))); 
    return bOk; 
} 

function valMes(oTxt) { 
    var bOk = false; 
    var nMes = parseInt(oTxt.value.substr(3, 2), 10); 
    bOk = bOk || ((nMes >= 1) && (nMes <= 12)); 
    return bOk; 
} 

function valAno(oTxt) {
    var bOk = true; 
    var nAno = oTxt.value.substr(6); 
    bOk = bOk && ((nAno.length == 2) || (nAno.length == 4)); 
    if (bOk) {
        for (var i = 0; i < nAno.length; i++) { 
            bOk = bOk && esDigito(nAno.charAt(i)); 
        } 
    } 
    return bOk; 
} 

function valFecha(oTxt) {
    var bOk = true; 
    if (oTxt.value != "") { 
        bOk = bOk && (valAno(oTxt)); 
        bOk = bOk && (valMes(oTxt)); 
        bOk = bOk && (valDia(oTxt)); 
        bOk = bOk && (valSep(oTxt)); 
        return bOk; 
    } 
} 

function fechaMayorOIgualQue(b_fch_ini, b_fch_fin) {
    var bRes = false; 
    var sDia0 = b_fch_ini.value.substr(0, 2); 
    var sMes0 = b_fch_ini.value.substr(3, 2); 
    var sAno0 = b_fch_ini.value.substr(6, 4); 
    var sDia1 = b_fch_fin.value.substr(0, 2); 
    var sMes1 = b_fch_fin.value.substr(3, 2); 
    var sAno1 = b_fch_fin.value.substr(6, 4); 
    if (sAno0 > sAno1) bRes = true; 
    else { 
        if (sAno0 == sAno1) { 
            if (sMes0 > sMes1) bRes = true; 
            else { 
                if (sMes0 == sMes1) 
                if (sDia0 >= sDia1) bRes = true; 
            } 
        } 
    } 
    return bRes; 
} 

function valFechas() {
    var bOk = false; 
    if (valFecha(document.form_difuntos_busqueda.b_fch_ini)) { 
        if (valFecha(document.form_difuntos_busqueda.b_fch_fin)) { 
            if (fechaMayorOIgualQue(document.form_difuntos_busqueda.b_fch_fin, document.form_difuntos_busqueda.b_fch_ini)) { 
                bOk = true; 
                return true;
            }else{ 
                alert("Rango inválido"); 
                document.form_difuntos_busqueda.b_fch_fin.focus(); 
                return false;
            } 
        }else{ 
            alert("Fecha inválida"); 
            document.form_difuntos_busqueda.b_fch_fin.focus(); 
            return false;
        } 
    }else{ 
        alert("Fecha inválida"); 
        document.form_difuntos_busqueda.b_fch_ini.focus(); 
        return false;
    } 
} 

function Rtrim(str) {
    var whitespace = new String(" \t\n\r");
    var s = new String(str);

    if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
        var i = s.length - 1;       // Get length of string
        while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1) { i--; }
        s = s.substring(0, i+1);
    }
    return s;
}

function checkEnter(e) {
    var characterCode
    if(e && e.which) {
        e = e
        characterCode = e.which
    }else{
        e = event
        characterCode = e.keyCode
    }	 
    if(characterCode == 13){
        return false
    }
    return true
}

function xmlhttp() {
    var xmlhttp;
		try{xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");}
		catch(e){
        try{xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}
        catch(e){
            try{xmlhttp = new XMLHttpRequest();}
				    catch(e){
					    xmlhttp = false;
				    }
        }
    }
    if (!xmlhttp) 
				return null;
    else
				return xmlhttp;
}

function ajaxLoad(url,container) {
    var A = document.getElementById(container);
    var ajax = xmlhttp();

    ajax.onreadystatechange=function() {
        if(ajax.readyState==1){
            A.innerHTML = "<img src='images/loading.gif' vspace='100' hspace='160' alt='Actualizando...'/>";
        }
				if(ajax.readyState==4) {
            A.innerHTML = ajax.responseText;
        }
    }
    ajax.open("GET",url,true);
    ajax.send(null);
    return false;
}

function obituario(codDifunto) {
    url = 'obituario.php?cod_difunto='+codDifunto;
    window.open(url,'Obituario','width=467,height=240,left=300,top=200,status=no,toolbar=no,menubar=no,location=no directories=no,resizable=no');
}

function misa(codMisa) {
    url= 'misa.php?cod_misa='+codMisa;
    window.open(url,'Misa','width=363,height=183,left=300,top=200,status=no,toolbar=no,menubar=no,location=no,directories=no,resizable=no');
}

function stateTab(tabinterfaceid,itemSelected) {
    tabs=document.getElementById(tabinterfaceid).getElementsByTagName("a")
    moduloLink=document.getElementById("modulo-link")
    defRec=document.getElementById("defunciones-recientes")
    if(itemSelected==0) { 
        moduloLink.className="modulo-link"
        defRec.className="defunciones-recientes"
    }
    if(itemSelected==1) {
        moduloLink.className="modulo-link-0"
        defRec.className="defunciones-recientes-0"
    }  
    for (var i=0; i<this.tabs.length; i++) {
        tabs[i].className=(i==itemSelected)? "selected" : ""
    }
}

