// Documento JavaScript
// Objeto sobre el navegador usado
var anterior='';
function ShowMenu(capa)
    {
    var pagina="menu1.jsp?ms="+new Date().getTime();
    MostrarMapa(pagina, capa);    
    }

function objetoAjax(){
      var xmlhttp=false;
	if (window.XMLHttpRequest) {// Si es Mozilla, Safari etc
		xmlhttp = new XMLHttpRequest()
	} 
	else if (window.ActiveXObject)
	{ // pero si es IE
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP")
			} 
		catch (e){ // en caso que sea una versión antigua
			try{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
				}
			catch (e){}
			}
		}
	else
		xmlhttp = false;
      return xmlhttp;
}


//función que carga la página sobre el div indicado 
function MostrarConsulta(pagina, nomdiv, mapa, mapadiv){
      divResultado = document.getElementById(nomdiv);
      ajax=objetoAjax();
      ajax.open("GET", pagina, true);
	contador = 1;
      ajax.onreadystatechange=function() {

             if (ajax.readyState==4) {
                  divResultado.innerHTML = ajax.responseText
			contador = contador + 1;

			if (contador==2) 
			{
				MostrarMapa(mapa, mapadiv);
				
			}
             }
      }
      ajax.send(null);
}


 function visualiza(id){
   document.getElementById(id).style.display="block";	
   
   } 

function oculta(id){
   document.getElementById(id).style.display="none";	
   
					
   } 

function objetoAjaxMapa(){
      var xmlhttp=false;
	if (window.XMLHttpRequest) {// Si es Mozilla, Safari etc
		xmlhttp = new XMLHttpRequest()
	} 
	else if (window.ActiveXObject)
	{ // pero si es IE
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP")
			} 
		catch (e){ // en caso que sea una versión antigua
			try{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
				}
			catch (e){}
			}
		}
	else
		xmlhttp = false;
      return xmlhttp;
}

function MostrarMapa(pagina, nomdiv){
      divResultado = document.getElementById(nomdiv);
      majax=objetoAjaxMapa();
      majax.open("GET", pagina, true);
      majax.onreadystatechange=function() {
             if (majax.readyState==4) {
                    var ch=""+majax.responseText
                    var ch2=ch.substring(0,2);
                    
                var cadena = ch;    
                var arroba = cadena.indexOf('#*');
                if(arroba!=-1){
                            location.href='accesoDenegado.html';
                            alert('No se ha autentificado en el sistema o su sesión ha caducado');
                            self.close();
                  }else{
                        cadena = ch;    
                        arroba = cadena.indexOf('@*#');
                        if(arroba!=-1){
                                location.href='accesoDenegado.html';
                                alert('Se ha iniciado Sesión en otro equipo, reinicie Sesión a través del SIIP');
                                self.close();
                        }else{
                                divResultado.innerHTML = majax.responseText
                        }
                    }
                  
             }
      }
      majax.send(null);
}
function chequea(cual){	
	if(cual.checked == true) { 
			document.getElementById('Acuerdo').value='1'; 
	} else {
			document.getElementById('Acuerdo').value='0';
	}
}


function ObjetoXmlHttp() {

  var TXmlHttp;
  try {
    TXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e1) {
     try {
        TXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
     } catch (e2) {
        try {
        TXmlHttp = new XMLHttpRequest();
       } catch (e3) {
         TXmlHttp = false;
       }
     }
  }
  return TXmlHttp;

}

function LlenaContratosTipo(cual){

//alert('Dentro de la funcion LlenaContratosTipo indice: '+ cual);

  var Parametros;

 XmlHttp = ObjetoXmlHttp();  
  if(!XmlHttp)
  {
    return;
  }

  Parametros = "Lookup=" + cual;
  
//  alert('Se creo el objeto ajax');
  
  XmlHttp.open("POST" , "LlenaContTipo", false);
  XmlHttp.onreadystatechange = ContTipoStatus;
  XmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  XmlHttp.setRequestHeader("Content-length", Parametros.length);
  XmlHttp.setRequestHeader("Connection", "close");
  XmlHttp.send(Parametros);
  
  }


function ContTipoStatus(){
// alert('Dentro de la funcion ContTipoStatus');
      if(XmlHttp.readyState == 4 && XmlHttp.status == 200)
      {    
        TextoOut = XmlHttp.responseText;
        document.getElementById("ContratosTipo").innerHTML = TextoOut ;
      }

}

function LlenaNoms(cual){


  var Parametros;

 XmlHttp = ObjetoXmlHttp();  
  if(!XmlHttp)
  {
    return;
  }

  Parametros = "Lookup=" + cual;
  
  
  XmlHttp.open("POST" , "LlenaNOMS", false);
  XmlHttp.onreadystatechange = NomStatus;
  XmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  XmlHttp.setRequestHeader("Content-length", Parametros.length);
  XmlHttp.setRequestHeader("Connection", "close");
  XmlHttp.send(Parametros);
  
  }


function NomStatus(){
 
      if(XmlHttp.readyState == 4 && XmlHttp.status == 200)
      {    
        TextoOut = XmlHttp.responseText;
        document.getElementById("NOMS").innerHTML = TextoOut ;
      }

}

function ValidaCantidades() {  
	var tqty=0;
	var tipo;
	var qty;

	//Obtener naturaleza...
	
	var Naturaleza = document.form1.naturaleza.options[document.form1.naturaleza.selectedIndex].text;
	
	// Obtener modalidad del contrato...
	
	var Mod = document.getElementById("txt_mod").value;
	
	// Si la casilla esta vacia . . .
	
	if(document.getElementById("txtqtyx").value==''){
				document.getElementById("txtqtyx").value=0;
	}
	
	// Buscar los campos de cantidad en la forma actual
	
	for (var x=0; x<document.forms[1].elements.length; x++){
		if ((document.forms[1].elements[x].type=='text')&&(document.forms[1].elements[x].name.substr(0,6)=='txtqty')){
			// Si es txt y ademas de cantidades
			
			//	alert(document.forms[1].elements[x].name);
			
			// Obtener el ID...

			id_tipo = document.forms[1].elements[x].name.substr(6,document.forms[1].elements[x].name.length-6);	
			
//			alert(id_tipo);

			// Validar cuantos...
			
			qty = parseInt(document.getElementById('txtqty' + id_tipo).value);
					
			if (qty!=0){
	
					//	alert("Contratos tipo: " + tipo + " - " + qty);				
					// Agregar "N" contratos tipo ...
								
					// obtener el nombre del contrato tipo
					if (id_tipo=='x'){ 
					
						tipo = 'CONTRATO NO TIPO';
						
					} else {
						
						tipo = document.getElementById('txttip' + id_tipo).value;
						
					}
								
					// sumarlos al carro
					
					AgregaCarrito(tipo, qty, Naturaleza, Mod);					
					
					tqty += qty;
			} 
			
		}
	}
	 
  //alert('Total de contratos agregados: '+tqty);	

	document.form1.submit();
	
	return true;
}

function AgregaCarrito(Tip, Qty, Nat, Mod){

  var Parametros;

 ZmlHttp = ObjetoXmlHttp();  
  if(!ZmlHttp)
  {
    return;
  }

  Parametros = "txt_nat=" + Nat + "&txt_tip=" + Tip + "&txt_mod=" + Mod + "&txt_qty=" + Qty + "&id_op=1";
       
       
  ZmlHttp.open("POST" , "Detalle.jsp", false);
  ZmlHttp.onreadystatechange = CambiaCarro;
  ZmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  ZmlHttp.setRequestHeader("Content-length", Parametros.length);
  ZmlHttp.setRequestHeader("Connection", "close");
  ZmlHttp.send(Parametros);
  
  }


function CambiaCarro(){
 
      if(ZmlHttp.readyState == 4 && ZmlHttp.status == 200)
      {    
        TextoOut = ZmlHttp.responseText;
        //document.getElementById("ContratosTipo").innerHTML = TextoOut ;
      }

}

function Pagar(valor)
{

    document.Solicitud.id_orden.value = valor;
	 document.Solicitud.om_pp.value = 2;
	document.Solicitud.submit();
}


function IniciarSIIP(valor)
{

    document.Inicia.id_resp.value = valor;
	document.Inicia.submit();
}

function Verdetalle(valor)
{
    document.Detail.idc.value = valor;
	document.Detail.submit();
}

function VerDocs(valor, valor2)
{

  document.Docs.uid.value = valor;
  document.Docs.cid.value = valor2;
	document.Docs.submit();
}


function Altas(objeto){
	objeto.value = objeto.value.toUpperCase();
	return true;
}

function Bajas(objeto){
	objeto.value = objeto.value.toLowerCase();
	return true;
}

function AddDomicilio(tipo){
		if(tipo==1){
			document.Domicilios.tip_dom.value='SHIP_TO';
		} else {
			if(tipo==2){
				document.Domicilios.tip_dom.value='BILL_TO';
			}
		}
		document.Domicilios.id_op.value='1';
		document.Domicilios.submit();
}

function EditDomicilio(cual, tipo){
		if(tipo==1){
			document.Domicilios.tip_dom.value='SHIP_TO';
		} else {
			if(tipo==2){
				document.Domicilios.tip_dom.value='BILL_TO';
			}
		}
		document.Domicilios.id_dom.value=cual;
		document.Domicilios.id_op.value='2';
		document.Domicilios.submit();
		
}

function LlenaCaract(cual){


  var Parametros;

 XmlHttp = ObjetoXmlHttp();  
  if(!XmlHttp)
  {
    return;
  }

  Parametros = "Lookup=" + cual;
  
  
  XmlHttp.open("POST" , "LlenaCaracter", false);
  XmlHttp.onreadystatechange = CaractStatus;
  XmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  XmlHttp.setRequestHeader("Content-length", Parametros.length);
  XmlHttp.setRequestHeader("Connection", "close");
  XmlHttp.send(Parametros);
  
  }


function CaractStatus(){
 
      if(XmlHttp.readyState == 4 && XmlHttp.status == 200)
      {    
        TextoOut = XmlHttp.responseText;
        document.getElementById("Caracter").innerHTML = TextoOut ;
      }

}

function ChecaNum(cual){
	alert("Validando");

	if(cual==""){
		return;
	}

  var Parametros;



	XmlHttp = ObjetoXmlHttp();  
  if(!XmlHttp)
  {
    return;
  }

  Parametros = "num_reg=" + cual;
  
  
  XmlHttp.open("POST" , "ChkContrato", false);
  XmlHttp.onreadystatechange = ChecaNumStatus;
  XmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  XmlHttp.setRequestHeader("Content-length", Parametros.length);
  XmlHttp.setRequestHeader("Connection", "close");
  XmlHttp.send(Parametros);
  
  }


function ChecaNumStatus(){
 
      if(XmlHttp.readyState == 4 && XmlHttp.status == 200){    
        TextoOut = XmlHttp.responseText;
		
		if (TextoOut!='OK'){
			alert("El número de contrato que escribio ya existe, verifique!");
		}
      }

}


function GetDomicilio(cual){

  var Parametros;

 XmlHttp = ObjetoXmlHttp();  
  if(!XmlHttp)
  {
    return;
  }

  Parametros = "Domicilio=" + cual;
  
  
  XmlHttp.open("POST" , "TDomicilio", false);
  XmlHttp.onreadystatechange = TDomStatus;
  XmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  XmlHttp.setRequestHeader("Content-length", Parametros.length);
  XmlHttp.setRequestHeader("Connection", "close");
  XmlHttp.send(Parametros);
  
  }


function TDomStatus(){


	  if(XmlHttp.readyState == 1){
	
	  }
	  
	  if(XmlHttp.readyState == 4 && XmlHttp.status == 200)
      {    
	
		var divdm;
		
		divdm = document.getElementById("DIVSHOWDOM");
			
		divdm.style.display = 'block';		

		document.getElementById("xCalle").value = XmlHttp.responseText.substr(0,100);
		document.getElementById("xNumext").value = XmlHttp.responseText.substr(100,10);
		document.getElementById("xNumint").value = XmlHttp.responseText.substr(110,10);
		document.getElementById("xEstadoPE").value = XmlHttp.responseText.substr(120,20);
		document.getElementById("xMunicipioPE").value = XmlHttp.responseText.substr(140,60);
		document.getElementById("xColoniaPE").value = XmlHttp.responseText.substr(200,60);
		document.getElementById("xCPPE").value = XmlHttp.responseText.substr(260,10);

//		document.getElementById("DIVSHOWDOM").innerHTML = XmlHttp.responseText ;
		
      }

}

function Acceso(form){
	if(form.txtUserName.value.length==0){
		alert("El nombre de usuario debe tener mínimo 6 caracteres\n"); 
		form.txtUserName.focus();
		return false;
	}

	if(form.txtPassword.value.length==0){
		alert("La contraseña de usuario debe tener mínimo 6 caracteres\n"); 
		form.txtPassword.focus();
		return false;
	}
	return true;
}

function ValidaAccion(){

	if (document.getElementById("NoTipo").style.display=="block"){
//			alert("Abierto el div de NO tipo");
		if(document.getElementById("f_cnt").value==""){
			alert("Falta anexar el contrato NO tipo, verifique");
			document.getElementById("f_cnt").focus();
			return false;
		}
	}
	
	if (document.getElementById("DIVMOD").style.display=="block"){
		if(document.getElementById("txtcancela").value==0){
			alert("Escriba el numero de contrato a modificar");
			document.getElementById("txtcancela").focus();
			return false;
		}
	}
	

	if (document.getElementById("Autofinanciamiento").style.display=="block"){
//			alert("Abierto el div de Autofinanciamiento");
		document.getElementById("Adicional").style.display=="block"
	}

	if (document.getElementById("TiempoCompartido").style.display=="block"){
//			alert("Abierto el div de Tiempoc ompartido");
	}

	if (document.getElementById("nuevoDom").value==-1){
		
		if(document.getElementById("EstadoPE").value==0){
			alert("Seleccione el estado del domicilio");
			document.getElementById("EstadoPE").focus();
			return false;
		}

		if(document.getElementById("MunicipioPE").value==0){
			alert("Seleccione el municipio del domicilio");
			document.getElementById("MunicipioPE").focus();
			return false;
		}

		if(document.getElementById("ColoniaPE").value==0){
			alert("Seleccione la colonia del domicilio");
			document.getElementById("ColoniaPE").focus();
			return false;
		}

		if(document.getElementById("CPPE").value==0){
			alert("Seleccione el codigo postal del domicilio");
			document.getElementById("CPPE").focus();
			return false;
		}

		if(document.getElementById("Calle").value==0){
			alert("Escriba la calle del domicilio");
			document.getElementById("Calle").focus();			
			return false;
		}

		if(document.getElementById("NumExt").value==0){
			alert("Escriba el numero exterior del domicilio");
			document.getElementById("NumExt").focus();			
			return false;
		}

		//alert("Crear nuevo domicilio...");
		//alert(document.getElementById("id_cc").value);
		
		document.getElementById("numcontrato").value = document.getElementById("txtcancela").value + '-' + document.getElementById("anio_cancela")[document.getElementById("anio_cancela").selectedIndex].value;

		document.RegistroProveedores.id_op.value = "0"; 

		document.RegistroProveedores.submit();
		
	} else {
		
		document.RegistroProveedores.id_op.value = "2"; 
		
		document.getElementById("iddomicilio").value = document.getElementById("nuevoDom").value;
		document.getElementById("numcontrato").value = document.getElementById("txtcancela").value + '-' + document.getElementById("anio_cancela")[document.getElementById("anio_cancela").selectedIndex].value;
		
    	document.RegistroProveedores.submit();
	}


}


function UpdateDocs(cuantos){


	var losde1='';
	var losde2='';
	
	var procede='1';

for(a=1; a<cuantos+1; a++){
	var campo = 'iddoc_'+a;
	var campo2 = 'status_'+a;
	var valor = document.getElementById(campo).value; 
	var status = document.getElementById(campo2).selectedIndex;	
	if(status==0){
		alert('Antes de continuar es necesario validar todos los documentos.');
		return -1;
		exit;
		}
		
	if(status==1){
		losde1 = losde1 + valor + ',';
		}
	if(status==2){
		losde2 = losde2 + valor + ',';
		procede='0';
		}
	}

lon1= losde1.length - 1;
lon2= losde2.length - 1;

document.getElementById('uno').value=losde1.substring(0, lon1);
document.getElementById('dos').value=losde2.substring(0, lon2);


//alert('valor de uno= '+document.getElementById('uno').value);
//alert('valor de dos= '+document.getElementById('dos').value);

document.getElementById('procede').value=procede;

//alert ('valor de procede='+ document.getElementById('procede').value);
	
	
	document.Docs.submit();
}

function Editar(valor)
{

//	alert(document.frmEdicion.id_domicilio.innerHTML);
//    document.frmEdicion.id_opcion.value = 1;
	document.location.href="RDomicilio.jsp?idd=" + valor + "&o=0"
}

function Borrar(valor)
{

//	alert(document.frmEdicion.id_domicilio.innerHTML);
//    document.frmEdicion.id_opcion.value = 1;
	document.location.href="EDomicilio.jsp?idd=" + valor + "&o=4"
}

function NFiscal()
{

//	alert(document.frmEdicion.id_domicilio.innerHTML);
//    document.frmEdicion.id_opcion.value = 1;
	document.location.href="RDomicilio.jsp?o=1"
}

function NEnvio()
{

//	alert(document.frmEdicion.id_domicilio.innerHTML);
//    document.frmEdicion.id_opcion.value = 1;
	document.location.href="RDomicilio.jsp?o=2"
}


function ValidaDomicilio(form){

	if (document.getElementById("EstadoPE").value==0){
		alert("Seleccione un estado...");
		return false;
	} 

	if (document.getElementById("MunicipioPE").value==0){
		alert("Seleccione un municipio...");
		return false;
	} 

	if (document.getElementById("ColoniaPE").value==0){
		alert("Seleccione una colonia...");
		return false;
	} 

	if (document.getElementById("CPPE").value==0){
		alert("Seleccione un C.P...");
		return false;
	} 

	if (document.getElementById("Calle").value==""){
		alert("Escriba su calle...");
		return false;
	} 
	
	if (document.getElementById("NumExt").value==""){
		alert("Escriba su numero exterior...");
		return false;
	} 

	return true;
}


function ValidarPRERegistro(form){

	if (document.getElementById("User_name").value==""){
		alert("Escriba su nombre de usuario...");
		document.getElementById("User_name").focus();
		return false;
	} 

	if (document.getElementById("Contrasena").value==""){
		alert("Escriba su contraseña...");
		document.getElementById("Contrasena").focus();
		return false;
	} 

	if (document.getElementById("Verificacion").value==""){
		alert("Escriba su contraseña...");
		document.getElementById("Verificacion").focus();
		return false;
	} 

	if (document.getElementById("Verificacion").value!=document.getElementById("Contrasena").value){
		alert("Contraseña y verificación NO coinciden...");
		document.getElementById("Verificacion").focus();
		return false;
	} 

	if (document.getElementById("Email").value==""){
		alert("Escriba su correo eléctronico...");
		document.getElementById("Email").focus();
		return false;
	}
	
	if(document.getElementById("User_name").value.length<5 || document.getElementById("User_name").value.length>15){
		alert("El nombre de usuario debe de contener entre 5 y 15 caracteres");
		document.getElementById("User_name").focus();
		return false;
	}
	
	if(document.getElementById("Contrasena").value.length<5 || document.getElementById("Contrasena").value.length>15){
		alert("Su contraseña debe de contener entre 5 y 15 ");
		document.getElementById("Contrasena").focus();
		return false;
	} 
	
	if(document.getElementById("Verificacion").value.length<5 || document.getElementById("Verificacion").value.length>15){
		alert("Su contraseña debe de contener entre 5 y 15 caracteres");
		document.getElementById("Verificacion").focus();
		return false;
	}
	

	if (document.getElementById("Contrasena").value.indexOf(" ") != -1){
		alert("Se detecto un espacio en la contraseña, no es valido");
		return false;
	}
	
	if (document.getElementById("Verificacion").value.indexOf(" ") != -1){
		alert("Se detecto un espacio en la verificación de la contraseña, no es valido");
		return false;
	}
/*	
	if (){
		alert("Escriba su nombre de usuario...");
		document.getElementById("User_name").focus();
		return false;
	} 

	if (document.getElementById("Contrasena").value==""){
		alert("Escriba su contraseña...");
		document.getElementById("Contrasena").focus();
		return false;
	} 

	if (document.getElementById("Verificacion").value==""){
		alert("Escriba su contraseña...");
		document.getElementById("Verificacion").focus();
		return false;
	} 


	if (document.getElementById("Email").value==""){
		alert("Escriba su correo eléctronico...");
		document.getElementById("Email").focus();
		return false;
	}
*/ 

	//return true;
}

function ValidaDocs(formx) {  
	
	for (var x=0; x<document.forms[1].elements.length; x++){
		if ((document.forms[1].elements[x].type=='file')){
			// Si es type file
			if (document.forms[1].elements[x].value=="") {
				
				if (document.forms[1].elements[x].name=="f_acta"){
					alert("Falta adjuntar su documento de acta constitutiva...");
					document.forms[1].elements[x].focus();
					return false;
				}

				if (document.forms[1].elements[x].name=="f_ife"){
					alert("Falta adjuntar su documento de credencial del elector (IFE)...");
					document.forms[1].elements[x].focus();
					return false;
				}

				if (document.forms[1].elements[x].name=="f_rfc"){
					alert("Falta adjuntar su documento del registro federal de contribuyentes (RFC)...");
					document.forms[1].elements[x].focus();
					return false;
				}

				if (document.forms[1].elements[x].name=="f_poder"){
					alert("Falta adjuntar su documento del poder notarial...");
					document.forms[1].elements[x].focus();
					return false;
				}
			}
		}
	}
	
	return true;
}

function FCerrarSesion(){

  CerrarXmlHttp = ObjetoXmlHttp();
  if(!CerrarXmlHttp)
  {
    return;
  }

  CerrarXmlHttp.open("POST" , "CerrarSesion" , false);
  CerrarXmlHttp.onreadystatechange = EstadoCerrarSesion;
  CerrarXmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  CerrarXmlHttp.setRequestHeader("Connection", "close");
  CerrarXmlHttp.send(null);
}

function EstadoCerrarSesion() {
  if(CerrarXmlHttp.readyState == 4)
  {
   location.href='index.jsp';
  }
}

function Imprime(cual)
{
  var div = document.getElementById(cual);
  var frmprint = window.open(' ', 'popimpr');
  frmprint.document.write("<link rel='stylesheet' href='estilos/estilosrcal.css'>");
  frmprint.document.write( div.innerHTML );
  frmprint.document.close();
  frmprint.print( );
  frmprint.close();
} 


function PintaDetalle(cual){

  var Parametros;

 XmlHttp = ObjetoXmlHttp();  
  if(!XmlHttp)
  {
    return;
  }

  Parametros = "IDC=" + cual;
  
//  alert('Se creo el objeto ajax');
  
  XmlHttp.open("POST" , "LogContrato", false);
  XmlHttp.onreadystatechange = ContCronologia;
  XmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  XmlHttp.setRequestHeader("Content-length", Parametros.length);
  XmlHttp.setRequestHeader("Connection", "close");
  XmlHttp.send(Parametros);
  
  }

function ContCronologia(){
      if(XmlHttp.readyState == 4 && XmlHttp.status == 200)
      {    
		TextoOut = XmlHttp.responseText;
	//	alert(TextoOut);
        document.getElementById("detalles").innerHTML = TextoOut ;
      }

}

