﻿function Login(InclCert,CertInstalado) {
	var data = "|login|";
	var firma = "";
	var NombrePfx;
	var certificadoPEM;
	if(InclCert){
		if(CertInstalado){
			var Certificados= document.getElementById("dlCertificados");
			if (Certificados.selectedIndex==-1){
				 alert("Por favor Seleccione un certificado");
				 return false;
				}

			NombrePfx = Certificados.options[Certificados.selectedIndex].value;
			firma = TSigner.SignData(NombrePfx, 0x00, data, 0x02);
			certificadoPEM = TSigner.GetCertificate("MY", NombrePfx);
			certificadoPEM = certificadoPEM.replace('-----BEGIN CERTIFICATE-----', '');
			certificadoPEM = certificadoPEM.replace('-----END CERTIFICATE-----', '');
			document.getElementById("txPEM").value=certificadoPEM;
			document.getElementById("txFirma").value=firma;
			document.getElementById("txPfx").value=NombrePfx;
			return true;
		}
		if(CertInstalado==false){
			NombrePfx=document.getElementById("fuCertificado").value;
			if(NombrePfx==""){
				alert("Por favor seleccione un archivo P12")
				return false;
			}
			
			if (NombrePfx.search("fakepath")>-1) {
				alert('No fue posible cargar el certificado, verifique la configuración del Explorador');
				return false;
			}
			certificadoPEM = TSigner.GetCertificateEx(NombrePfx, document.getElementById("txContCertificado").value);
			certificadoPEM = certificadoPEM.replace('-----BEGIN CERTIFICATE-----', '');
			certificadoPEM = certificadoPEM.replace('-----END CERTIFICATE-----', '');
			firma=TSigner.SignDataEx(NombrePfx,  document.getElementById("txContCertificado").value, 0, data, 2);
			if (firma.length < 5) {
				alert('Verifique la clave secreta del certificado');
				return false;
			}
			document.getElementById("txPEM").value=certificadoPEM;
			document.getElementById("txFirma").value=firma;
			document.getElementById("txPfx").value=NombrePfx;
			return true;
		}
	}
}

function Firmar(datos, tipo, cert,password) {
	 var firma = "";
	if (tipo == 'C') {
		firma = TSigner.SignData(cert, 0x00, datos, 0x02);
	}
	else{
		firma = TSigner.SignDataEx(cert, password, 0, datos, 2);
	}
	 document.getElementById('ctl00_ContentPlaceHolder1_hdFirma').value = firma;
	 
	 var Layout = document.getElementById('ctl00_ContentPlaceHolder1_hdLayout');
	 if(Layout!=null){
		 document.getElementById('ctl00_ContentPlaceHolder1_hdLayout').value = datos;
	 }

}

function GetCertificados(obj) {
	
	var certificates = TSigner.Certificates;
	var items = certificates.split("\n");
	for (var n = 0; n < items.length - 1; n++) {
		var optn = document.createElement("Option");
		optn.text = items[n];
		optn.value = items[n];
		obj.options.add(optn);
	}
}

function OnKeyDown(s, e) {

    if (s.readOnly) {
        var key = ASPxClientUtils.GetKeyCode(e.htmlEvent);
        if (key == ASPxKey.Backspace) {
            ASPxClientUtils.PreventEventAndBubble(e.htmlEvent);
        }
    }
}

function ValidarBuscar() {
    var obj = txRFC.GetText();
    if (obj.length < 10 || obj.length == 11 || obj.length == 12) {
        alert('Capture correctamente el R.F.C.');
        return false;
    }

}

function Cerrar() {
    $.modal.close();
    $('#dvCoincidencias').hide();
}

function sleep(milliSeconds){

var startTime = new Date().getTime(); // get the current time

while (new Date().getTime() < startTime + milliSeconds); // hog cpu

}
