function validarCaracterFecha(){
    var objeto = validarCaracterFecha.arguments[0];
    var wKey = window.event.keyCode;
    valor = objeto.value;
    var nAnt = 0;
    n = valor.length;
    if (n > 0){
      for(var i=0; i< n ; i++){
        if(nAnt < n){ 
          if(n==2 && i==1) objeto.value = valor+"/";
          if(n==5 && i==3) objeto.value = valor+"/";
        }
      }
    }
    nAnt = n;
}
function fncclickMapa(which_div,i) {
	// find out which DIV we're dealing with
	this_div = eval('document.all.' + which_div)
	// find out if that div is open
	var isDivOpen = (this_div.style.display == "none");
	// open or hide the div as needed
	this_div.style.display = (isDivOpen) ? "block" : "none";
	// where is the arrow icon associated with the div
	var arrow_image = eval('document.all.image' + i)
	// display the correct arrow icon
	arrow_image.src = (isDivOpen) ? "Images/minus.gif" : "Images/plus.gif";
}

function fncclickLegBasica(which_div,i) {
	// find out which DIV we're dealing with
	this_div = eval('document.all.' + which_div)
	// find out if that div is open
	var isDivOpen = (this_div.style.display == "none");
	// open or hide the div as needed
	this_div.style.display = (isDivOpen) ? "block" : "none";
}

function fncInicioLegBasica() {	
	document.all["menu1"].style.display="none";
	document.all["menu2"].style.display="none";
	document.all["menu3"].style.display="none";
	document.all["menu4"].style.display="none";
	document.all["menu5"].style.display="none";
	document.all["menu6"].style.display="none";	
	document.all["menu7"].style.display="none";
}	

function fncInicioLeft()  {
	//var i=3
	//for(i=1;i<=5;i++)  {
		document.all["menu3"].style.display="none";
	//}
}

function fncInicioMapa()  {
	var i
	for(i=1;i<=5;i++) {document.all["menu"+i].style.display="none";}
}

function fncOpenVentana(theURL,winName,features){window.open(theURL,winName,features);}

function fncPrincipal(page){parent.frames['principal'].document.location = page;}

function fncGODataBase(flag)  {
	var url;
	switch (flag)  { //debe ir flag
		case 1 : url = "http://www.minjus.gob.pe/LEGCARGEN/lpext.dll?f=templates&fn=main-h.htm&2.0";break;
		case 1 : url = "http://www.minjus.gob.pe/LEGCARGEN/lpext.dll?f=templates&fn=main-h.htm&2.0";break;
		case 2 : url = "http://www.minjus.gob.pe/LEGCARPAR/lpext.dll?f=templates&fn=main-h.htm&2.0";break;
		case 3 : url = "http://www.minjus.gob.pe/LEGGOBREG/lpext.dll?f=templates&fn=main-h.htm&2.0";break;
		case 4 : url = "http://www.minjus.gob.pe/LEGGOBLOC/lpext.dll?f=templates&fn=main-h.htm&2.0";break;
		case 5 : url = "http://www.minjus.gob.pe/TUPA/lpext.dll?f=templates&fn=main-h.htm&2.0";break;
		case 6 : url = "http://www.minjus.gob.pe/LEGADMJUS/lpext.dll?f=templates&fn=main-h.htm&2.0";break;
		case 7 : url = "http://www.minjus.gob.pe/JURISP/lpext.dll?f=templates&fn=main-h.htm&2.0";break;
		case 8 : url = "http://www.minjus.gob.pe/LEGCOMP/lpext.dll?f=templates&fn=main-h.htm&2.0";break;
		case 9 : url = "http://www.minjus.gob.pe/spij/mantenimiento.htm";break;
		default : url = "http://www.minjus.gob.pe/spij";		
	}
	fncOpenVentana(url,'_blank','');
}

function fncGODataBaseCLP(flag)  {
	//alert(flag);
	var url;
	switch (flag)  { //debe ir flag
//		case 1 : url = "http://www.minjus.gob.pe/CLPLegcargen/login_legcargen.asp";break;
		case 1 : url = "http://spij.minjus.gob.pe/CLPLegcargen/login_legcargen.asp";break;
//		case 2 : url = "http://www.minjus.gob.pe/CLPlegcarpar/login_legcarpar.asp";break;
		case 2 : url = "http://spij.minjus.gob.pe/CLPlegcarpar/login_legcarpar.asp";break;
//		case 3 : url = "http://www.minjus.gob.pe/CLPleggobreg/login_leggobreg.asp";break;
		case 3 : url = "http://spij.minjus.gob.pe/CLPleggobreg/login_leggobreg.asp";break;
//		case 4 : url = "http://www.minjus.gob.pe/CLPleggobloc/login_leggobloc.asp";break;
		case 4 : url = "http://spij.minjus.gob.pe/CLPleggobloc/login_leggobloc.asp";break;
//		case 5 : url = "http://www.minjus.gob.pe/CLPtupa/login_tupa.asp";break;
		case 5 : url = "http://spij.minjus.gob.pe/CLPtupa/login_tupa.asp";break;
//		case 6 : url = "http://www.minjus.gob.pe/CLPcompodjud/login_compodjud.asp";break;
		case 6 : url = "http://spij.minjus.gob.pe/CLPcompodjud/login_compodjud.asp";break;
//		case 7 : url = "http://www.minjus.gob.pe/CLPjurisp/login_jurisp.asp";break;
		case 7 : url = "http://spij.minjus.gob.pe/CLPjurisp/login_jurisp.asp";break;
//		case 8 : url = "http://www.minjus.gob.pe/CLPlegcomp/login_legcomp.asp";break;
		case 8 : url = "http://spij.minjus.gob.pe/CLPlegcomp/login_legcomp.asp";break;
//		case 9 : url = "http://www.minjus.gob.pe/spij/mantenimiento.htm";break;
		case 9 : url = "http://spij.minjus.gob.pe/spij/mantenimiento.htm";break;
//		default : url = "http://www.minjus.gob.pe/spij";
		default : url = "http://spij.minjus.gob.pe/";
	}
	fncOpenVentana(url,'_blank','');
}

function fncStatus(msgStr)  {
  status=msgStr;
  document.MM_returnValue = true;
}

function fncBuscaSumilla()  {
  if (document.calform.datebox.value!='')  {
 	//document.ifrSumilla.location.replace("spij_MuestraSumilla.asp?FechaSel="+document.calform.datebox.value);
	document.calform.action="spij_MuestraSumilla.asp"
	document.calform.target="ifrSumilla"
	document.calform.submit();}
  else	
 		alert("Debe escoger una fecha...");
}

function fncOpenPDF(pdf){window.open(pdf,"FilePDF","scrollbars=0,toolbar=0,location=0,width=640,height=480 top=30 left=30")}

function trim(cadena)  {
	while (cadena.substr(0,1)==" ")
		cadena=cadena.substr(1);
	while (cadena.substr(cadena.length-1,1)==" ")
		cadena=cadena.substr(0,cadena.length-1);
	while (cadena.search(/  /)!=-1)
		cadena=cadena.replace("  "," ");
	return(cadena);
}

function fncValidaRecordatorio()  {
	if (document.frmRecordatorio.cboPregunta.options[document.frmRecordatorio.cboPregunta.selectedIndex].value=='00000'){
		alert("Seleccione una pregunta");
		document.frmRecordatorio.cboPregunta.focus();
		return;
	}
	if (!(fncValidaBlanco('document.frmRecordatorio.txtRespuesta','La respuesta')))
		return;
	document.frmRecordatorio.hdnPaginaOrigen.value='Recordatorio';
	document.frmRecordatorio.submit(); 
}
 
function fncValidaCambioClave()  {
	if (!(fncValidaBlanco('document.frmCambioClave.txtNuevaClave','La nueva clave')))
		return
	if (!(fncValidaBlanco('document.frmCambioClave.txtConfNuevaClave','La confirmacion de la nueva clave')))
		return
	if (document.frmCambioClave.cboPregunta.options[document.frmCambioClave.cboPregunta.selectedIndex].value=='00000'){
		alert("Seleccione una pregunta");
		document.frmCambioClave.cboPregunta.focus();
		return;
	}
	if (!(fncValidaBlanco('document.frmCambioClave.txtRespuesta','La respuesta recordatoria')))
		return
	if (trim(document.frmCambioClave.txtNuevaClave.value)!=trim(document.frmCambioClave.txtConfNuevaClave.value)){
		alert("Las claves no coinciden");
		document.frmCambioClave.txtConfNuevaClave.value='';
		document.frmCambioClave.txtConfNuevaClave.focus();
		return;
	}
	document.frmCambioClave.hdnPaginaOrigen.value='Contrasena';
	document.frmCambioClave.submit(); 
}
 
function fncValidaDescarga()  {
	document.frmDescarga.hdnPaginaOrigen.value='Descarga';
	document.frmDescarga.submit(); 
}

function fncValidaNumero(control,campo)  {
	var flag=true;
	var i,j,a,cadena;
	eval("cadena = " + control + ".value")
	for(i=0;i<cadena.length;i++)  {
	 	a=(cadena.substr(i,1));
		j=a.charCodeAt(0);
		if (!( (j>=48) && (j<=57)) )
			flag=false;
	}
	if (! flag)  {
		alert("El " + campo + " contiene caracteres no válidos");
		eval(control + ".focus()")
	}
  return flag;
}

function fncValidaEmail(control) {
	eval("emailStr = " + control + ".value")
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|[a-zA-Z][a-zA-Z]|COM|NET|ORG|EDU|INT|MIL|ARPA|BIZ|AERO|NAME|COOP|INFO|PRO|MUSEUM)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:!#$%&*=?+^|~\\/\\`\\'\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var atom=validChars + '+';
	var word="(" + atom + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var matchArray=emailStr.match(emailPat);
	var flag=true
	
	if (matchArray==null) {
		alert("La direccion email parece incorrecta (verifica el @ y .s)");
		return false
	}

	var user=matchArray[1];
	var domain=matchArray[2];
		
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			alert("Direccion Email: El nombre del usuario contiene caracteres invalidos.");
			return false
	   }
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("Direccion Email: El nombre del dominio contiene caracteres invalidos.");
			return false
	  }
	}
	if (user.match(userPat)==null) {
		alert("Direccion Email: El nombre de usuario no parece ser valido.");
		return false
	}

	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			alert("Direccion Email: El nombre del dominio no parece ser valido.");
			return false
	  }
	}
	if (checkTLD && domArr[domArr.length-1].search(knownDomsPat)==-1) {
		alert("El e-mail debe finalizar en un dominio conocido o en 2 letras de pais.");
		return false
	}
	if (len<2) {
		alert("Esta direccion e-mail no tiene un hostname!");
		return false
	}
	return true;
}

function fncValidaBlanco(control,campo)  {
	var flag=false
	eval("cadena = " + control + ".value")
	if (cadena!="" && cadena!=null)  {
		flag=true
	}
	if (! flag)  {
		alert( campo + " no puede ser vacio");
		eval(control + ".focus()")
	}
	return flag
}

function fncRegistraInvitado()  {
	if (!(fncValidaBlanco('document.frmRegistro.txtnombre','El nombre')))
		return;
	if (!(fncValidaBlanco('document.frmRegistro.txtempresa','La empresa')))
		return;
	if (!(fncValidaBlanco('document.frmRegistro.txttelefono','El telefono')))
		return;
	else
		if (!(fncValidaNumero('document.frmRegistro.txttelefono','Telefono')))
			return;
	if (!(fncValidaBlanco('document.frmRegistro.txtemail','El email')))
		return;
	else
		if (!(fncValidaEmail('document.frmRegistro.txtemail')))
			return;
	if (document.frmRegistro.cboPais.value=='00'){
		alert("Selecciones un pais");
		document.frmRegistro.cboPais.focus()
		return;
	}
	if (document.frmRegistro.cboPais.value=='4027'){
		if (document.frmRegistro.cboDpto.value=='00'){
			alert("Seleccione un departamento");
			document.frmRegistro.cboDpto.focus();
			return;
		}
	}
	document.frmRegistro.hdnNombrePais.value=document.frmRegistro.cboPais.options[document.frmRegistro.cboPais.selectedIndex].text;
	document.frmRegistro.hdnNombreDepartamento.value=document.frmRegistro.cboDpto.options[document.frmRegistro.cboDpto.selectedIndex].text;
	document.frmRegistro.submit()
}

function fncRegistraInvitadoNuevo()  {
	if (!(fncValidaBlanco('document.frmRegistro.txtnombre','El nombre')))
		return;
	if (!(fncValidaBlanco('document.frmRegistro.txtempresa','La empresa')))
		return;
	if (!(fncValidaBlanco('document.frmRegistro.txtemail','El email')))
		return;
	else
		if (!(fncValidaEmail('document.frmRegistro.txtemail')))
			return;
			
		if (document.frmRegistro.cboProfesion.value=='00'){
		alert("Selecciones una Profesion");
		document.frmRegistro.cboProfesion.focus()
		return;
	}		
			
	if (document.frmRegistro.cboPais.value=='00'){
		alert("Selecciones un pais");
		document.frmRegistro.cboPais.focus()
		return;
	}
	if (document.frmRegistro.cboPais.value=='4027'){
		if (document.frmRegistro.cboDpto.value=='00'){
			alert("Seleccione un departamento");
			document.frmRegistro.cboDpto.focus();
			return;
		}
	}
	document.frmRegistro.hdnNombrePais.value=document.frmRegistro.cboPais.options[document.frmRegistro.cboPais.selectedIndex].text;
	document.frmRegistro.hdnNombreDepartamento.value=document.frmRegistro.cboDpto.options[document.frmRegistro.cboDpto.selectedIndex].text;
	
//	alert("Antes del Submit");
	document.frmRegistro.submit();
	
}


function fncVerDpto()  {
	if (document.frmRegistro.cboPais.value!='4027')
		document.frmRegistro.cboDpto.disabled=true
	else
		document.frmRegistro.cboDpto.disabled=false
}

function fncIrInvitado(pValor)  {
	switch(pValor)  {
		case "0" : fncDescargaDEMO();break;
		case "1" : fncVerDEMO();break;
		case "2" : window.close();break;
		default : return;
	}
}

function fncVerDEMO()  {
//	document.frmRegistro.action="../Leg_Basica/spij_VerDemo.asp"
	document.frmRegistro.action="../Leg_Basica/spij_VerDemo1.asp" //Modificado MMC 07.04.2008
	document.frmRegistro.target="_blank"
	document.frmRegistro.submit();
	//parent.location.href="http://www.minjus.gob.pe/demo/lpext.dll?f=templates&fn=main-j.htm&2.0"
}

//function fncDescargaDEMO(){location.href="http://www.minjus.gob.pe/spij/Descargas/Instalador/Setup32D.exe"}
function fncDescargaDEMO(){location.href="http://spij.minjus.gob.pe/Descargas/Instalador/Setup32D.exe"} //Modificado MMC 07.04.2008

function fncValidarRadio(pValor)  {
	if(pValor == 0)	{MM_showHideLayers('Layer1','','show','Layer2','','hide')}
	else{MM_showHideLayers('Layer1','','hide','Layer2','','show')}
}

function fncBuscaSumillas()  {
	if (document.frmSumillas.rdOpcion1.checked)  {
		if (!(fncValidaBlanco('document.frmSumillas.datebox','La Fecha')))  {return;}
		document.frmSumillas.hdnFlag.value=0;
	}
	if (document.frmSumillas.rdOpcion2.checked)  {
		if (!(fncValidaBlanco('document.frmSumillas.datebox1','La Fecha inicial')))  {return;}
		if (!(fncValidaBlanco('document.frmSumillas.datebox2','La Fecha final')))  {return;}
		document.frmSumillas.hdnFlag.value=1;
	}
	
	if (document.getElementById("hdnTipoNorma").value == ""){	//Aumentado por: MMC 10.02.2011
		document.getElementById("hdnTipoNorma").value = "00"	//Aumentado por: MMC 10.02.2011
	}															//Aumentado por: MMC 10.02.2011

//	document.frmSumillas.hdnTipoNorma.value= document.getElementById("ifrTipo").Document.getElementById("cboTipoNorma").value	//Comentado por: MMC 10.02.2011
//	document.frmSumillas.hdnTipoNorma.value= document.getElementById("ifrTipo").Document.getElementById("cboTipoNorma").value
	document.frmSumillas.action="spij_listarSumillas.asp"
	document.frmSumillas.target="ifrResultado"
	document.frmSumillas.submit();
}

function fncDeshabilitaBotonDescarga(){document.frmDescarga.cmdSiguiente.disabled=true;}

function fncHabilitabotondescarga(){document.frmDescarga.cmdSiguiente.disabled=false;}

function fncVerificaEstado(obj)  {
	switch(obj.value)  {
		case "0" : fncDeshabilitaBotonDescarga();break;
		case "1" : fncHabilitabotondescarga();break;
		default : fncDeshabilitaBotonDescarga;break;
	}
}

function changeKey (textControl, evt, keyChecker) {
	var keyCode = evt.keyCode ? evt.keyCode : evt.charCode ? evt.charCode : evt.which ? evt.which : void 0;
	var key;
	if (keyCode) {key = String.fromCharCode(keyCode);}
	var keyCheck = keyChecker(keyCode, key);
	if (keyCode && window.event && !window.opera) {
		if (keyCheck.cancelKey) {return false;}
		else if (keyCheck.replaceKey) {
				window.event.keyCode = keyCheck.newKeyCode;
				if (window.event.preventDefault) {window.event.preventDefault();}
				return true;
				}
				else {return true;}
	}
	else if (typeof textControl.setSelectionRange != 'undefined') {
			if (keyCheck.cancelKey) {
			  if (evt.preventDefault) {evt.preventDefault();}
			  return false;
			}
			else if (keyCheck.replaceKey) {
	// cancel the key event and insert the newKey for the current
	// selection
					if (evt.preventDefault) {evt.preventDefault();}
					var oldSelectionStart = textControl.selectionStart;
					var oldSelectionEnd = textControl.selectionEnd;
					var selectedText = textControl.value.substring(oldSelectionStart,
																   oldSelectionEnd);
					var newText = typeof keyCheck.newKey != 'undefined' ? keyCheck.newKey : String.fromCharCode(keyCheck.newKeyCode);
					textControl.value = textControl.value.substring(0, oldSelectionStart) + newText +
										textControl.value.substring(oldSelectionEnd);
					textControl.setSelectionRange(oldSelectionStart + newText.length, oldSelectionStart + newText.length);
					return false;
				  }
				  else {return true;} 
			}
			else if (keyCheck.cancelKey) {
					if (evt.preventDefault) {evt.preventDefault();}
					return false;
				 }
	  else {return true;}
}

function lettersToUpperCase (keyCode, key) {
  var newKey = key.toUpperCase();
  if (newKey != key) {
	return { replaceKey: true,
			 newKeyCode: newKey.charCodeAt(),
			 newKey: newKey };
  }
  else {
	return {cancelKey: false};
  }
}

