var nav4 = window.Event ? true : false;
var enlace=0;
var cierra=0;

var MAX=180;
var count;
var count2;
//-----------------------------------------------------
function counter()
{
	var str = new String(document.form1.enunciado.value);
	count = MAX - str.length;
	var antstr = str.substring(0,(MAX))
	if (str.length <= MAX) {
		document.all.divCount.innerHTML=count;
	} else {
		alert("Se supero el numero de caracteres permitidos")
		document.form1.enunciado.value=antstr
		document.form1.enunciado.focus();
	}
}
//-----------------------------------------------------
function counter2()
{
	var str = new String(document.form1.sustento.value);
	count2 = MAX - str.length;
	var antstr = str.substring(0,(MAX))
	if (str.length <= MAX) {
		document.all.divCount2.innerHTML=count2;
	} else {
		alert("Se supero el numero de caracteres permitidos")
		document.form1.sustento.value=antstr
		document.form1.sustento.focus();
	}
}
//-----------------------------------------------------
function ValidarCorreo1(valor) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
		rpt = 1;
	} else {
		rpt = -1;
	}
	return rpt;
 }
//-----------------------------------------------------
function validadoc(son){
	if (son==1) {
		var er_cp = /(^([0-9]{8,8})|^)$/			//5 numeros o cadena vacia
		var rpta='1';
	
		//comprueba campo codigo postal
		if(!er_cp.test(document.form1.nrodoc.value)) { 
			rpta='-1'
		}  
	} else if (son==2) {
			var er_cp = /(^([0-9]{6,6})|^)$/		//5 numeros o cadena vacia
			var rpta='1';
		
			//comprueba campo codigo postal
			if(!er_cp.test(document.form1.nrodoc.value)) { 
				rpta='-1'
			}  
		}
return rpta
	
}
//-----------------------------------------------------
function acceptNum2(evt){ 
// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57 
var key = nav4 ? evt.which : evt.keyCode; 
return (key <= 13 || key == 46 || (key >= 48 && key <= 57));
}
//-----------------------------------------------------
function acceptNum(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 validacion3() {

	var er_cp = /(^([0-9]{5,5})|^)$/				//5 numeros o cadena vacia
	var rpta='1';

	//comprueba campo codigo postal
	if(!er_cp.test(document.form1.cmp.value)) { 
		alert('Mensaje de alerta')
        document.form1.cmp.focus();
		rpta='-1'
	}  
	return rpta
}
//-----------------------------------------------------
function buscausuario(){
cierra=1;
	estado=1;
	if ( document.form1.user.value == '' && estado == 1) {
		  alert("Ingrese su Usuario");
		  estado = -1;
		  document.form1.user.focus();
	}
	if ( document.form1.pwd.value == '' && estado == 1) {
		  alert("Ingrese su Contraseña");
		  estado = -1;
		  document.form1.pwd.focus();
	}
	if (estado== 1) {
		document.form1.action='validarusuario.php';	  
		document.form1.submit()
	}
}
//-----------------------------------------------------
function validar(){
cierra=1;
	estado=1;
	if ( document.form1.nombre.value == '' && estado == 1) {
		  alert("Ingrese su nombre");
		  estado = -1;
		  document.form1.nombre.focus();
	}
	if ( document.form1.apepat.value == '' && estado == 1) {
		  alert("Ingrese su apellido paterno");
		  estado = -1;
		  document.form1.apepat.focus();
	}
	if ( document.form1.apemat.value == '' && estado == 1) {
		  alert("Ingrese su apellido materno");
		  estado = -1;
		  document.form1.apemat.focus();
	}
	if ( document.form1.docident.value == '' && estado == 1) {
		  alert("Ingrese su documento de identidad");
		  estado = -1;
		  document.form1.docident.focus();
	}
	if ( document.form1.tel.value == '' && estado == 1) {
		  alert("Ingrese un número telefónico de contacto");
		  estado = -1;
		  document.form1.tel.focus();
	}

	if (estado== 1) {
		document.form1.action='perfilingresar.php';	  
		document.form1.submit()
	}
}
// -------------------------------------
function textomail(valor, quien){
	window.open("doc_textomail.php?correo="+valor+"&doc="+quien,"","scrollbars=no,resizable=no,width=520,height=240,left=100,top=5");
}
//--------------------------------------function enviacorreo(valor){
function textomail2(valor, quien){
	window.open("doc_ingresopwd.php?correo="+valor+"&doc="+quien,"","scrollbars=no,resizable=no,width=520,height=240,left=100,top=5");
}
///////
function enviamail(){
	estado=1;
	if ( document.form1.emailcopia.value != '' ) {
		if ( ValidarCorreo1(document.form1.emailcopia.value)!=1) {
			  alert("Error en el Correo electrónico");
			  estado = -1;
			  document.form1.emailcopia.focus();
		}
	}

	if ( document.form1.asunto.value=="" && estado == 1) {
        alert("No declaró ningún asunto");
        estado = -1;
        document.form1.asunto.focus();
    }
	
	if ( document.form1.mensaje.value=="" && estado == 1) {
        alert("Especifique el mensaje");
        estado = -1;
        document.form1.mensaje.focus();
    }
	
	if (estado== 1) {
		document.form1.action='enviamail.php';	  
		document.form1.submit()
	}
}
//-----------------------------------------------------
function enviaclave(){
	document.form1.action='doc_ingresarpwd.php';	  
	document.form1.submit()
}
//-----------------------------------------------------
function p_ingresar(){
cierra=1;
	estado=1;
	if ( document.form1.unidad.options[document.form1.unidad.selectedIndex].value == 0 && estado == 1) {
		  alert("Selecciones la Unidad a la cual pertenece la pregunta");
		  estado = -1;
		  document.form1.unidad.focus();
	}
	if ( document.form1.enunciado.value == '' && estado == 1) {
		  alert("Ingrese el enunciado de la pregunta");
		  estado = -1;
		  document.form1.enunciado.focus();
	}
	if ( document.form1.alt1.value == '' && estado == 1) {
		  alert("Ingrese la alternativa 1");
		  estado = -1;
		  document.form1.alt1.focus();
	}
	if ( document.form1.alt2.value == '' && estado == 1) {
		  alert("Ingrese la alternativa 2");
		  estado = -1;
		  document.form1.alt2.focus();
	}
	if ( document.form1.alt3.value == '' && estado == 1) {
		  alert("Ingrese la alternativa 3");
		  estado = -1;
		  document.form1.alt3.focus();
	}
	if ( document.form1.alt4.value == '' && estado == 1) {
		  alert("Ingrese la alternativa 4");
		  estado = -1;
		  document.form1.alt4.focus();
	}
	if ( document.form1.alt5.value == '' && estado == 1) {
		  alert("Ingrese la alternativa 5");
		  estado = -1;
		  document.form1.alt5.focus();
	}
	if ( document.form1.sustento.value == '' && estado == 1) {
		  alert("Ingrese la Sustentacion de la pregunta");
		  estado = -1;
		  document.form1.sustento.focus();
	}
	if ( document.form1.grado.options[document.form1.grado.selectedIndex].value == 0 && estado == 1) {
		  alert("Selecciones la Unidad a la cual pertenece la pregunta");
		  estado = -1;
		  document.form1.grado.focus();
	}
	if (estado== 1) {
		document.form1.action='p_ingresar.php';	  
		document.form1.submit()
	}
}
// -------------------------------------
function p_grabarpwd(){
estado = 1;
	if ( ( document.form1.npwd.value != document.form1.cnpwd.value || document.form1.npwd.value=='' ) && estado == 1) {
		  alert("Las contraseñas no coinciden");
		  estado = -1;
		  document.form1.npwd.focus();
	}
	
	if ( estado == 1) {
		document.form1.action='p_grabarpwd.php';	  
		document.form1.submit();
	}
}
//-----------------------------------------------------
function EnterHelp(e,obj,foc,tipo)
{
	plataforma=navigator.appName;

	if(plataforma=="Netscape")
	{tecla=e.which;}
	if(plataforma=="Microsoft Internet Explorer")
	{tecla=e.keyCode;}
	
	if(tipo=="L")
	{
		if(tecla==13)	
		{
			document.getElementById(foc).focus();	
		}
	}
	
	if(tipo=="N")
	{
		if((tecla<48 || tecla>57) && tecla!="8" && tecla!="32" && tecla!="42" && tecla!="13"&& tecla!="46")
		{
			return false;
		}
		else
		{			
			if(tecla==13)	
			{
				document.getElementById(foc).focus();	
			}
		}
	}
}
function verificaH(obj)
{
	if(!validaHora(obj))
	{
		//document.getElementById(obj).focus();
	}
}
function verificaM(obj)
{
	if(!validaHora(obj))
	{
		//document.getElementById(obj).focus();
	}
}
function validaHora(obj)
{
	cad=document.getElementById(obj).value;
	if(cad!="")
	{
		if(parseInt(cad)<0 || parseInt(cad)>=24 ){alert("Hora no Valida");return false;}	
	}	
	document.getElementById(obj).value=str_pad(cad,"2","0","STR_PAD_LEFT");
	return true;	
}
function validaMinutos(obj)
{
	cad=document.getElementById(obj).value;
	if(cad!="")
	{
		if(parseInt(cad)<0 || parseInt(cad)>=59 ){alert("Minutos no Validos");return false;}	
	}	
	document.getElementById(obj).value=str_pad(cad,"2","0","STR_PAD_LEFT");
	return true;	
}
function str_pad (input, pad_length, pad_string, pad_type)
{
  input = String (input);
  pad_string = pad_string != null ? pad_string : " ";
  if (pad_string.length > 0)
  {
    var padi = 0;
    pad_type = pad_type != null ? pad_type : "STR_PAD_RIGHT";
    pad_length = parseInt (pad_length);
    switch (pad_type)
    {
      case "STR_PAD_BOTH":
        input = str_pad (input
                       , input.length + Math.ceil ((pad_length - input.length) / 2.0)
                       , pad_string, "STR_PAD_RIGHT");
     // break;  // kein break!
      case "STR_PAD_LEFT":
        var buffer = "";
        for (var i = 0, z = pad_length - input.length; i < z; ++i)
        {
          buffer += pad_string.charAt(padi); // [padi] IE 6.x bug
          if (++padi == pad_string.length)
            padi = 0;
        }
        input = buffer + input;
        break;
      default:
        for (var i = 0, z = pad_length - input.length; i < z; ++i)
        {
          input += pad_string.charAt(padi);
          if (++padi == pad_string.length)
            padi = 0;
        }
        break;
    }
  }
  return input;
}
function ucwords( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // *     example 1: ucwords('kevin van zonneveld');
    // *     returns 1: 'Kevin Van Zonneveld'
    
    return str.toLowerCase().replace(/\w+/g, function(s){
        return s.charAt(0).toUpperCase() + s.substr(1);
    });
}
function isEmailAddress(theElement)
{
var s = theElement;
//var filter=/^[A-Za-z-][A-Za-z0-9_-]*@[A-Za-z0-9_-]+\.[A-Za-z0-9_.]+[A-za-z]$/;
var filter=/^[\w-\.]+\@[\w\.-]+\.[a-z]{2,4}$/;
if (s.length == 0 ) return true;
if (filter.test(s))
	return true;
else
	return false;
}
function validaIngreso()
{

	param="caso=ingreso&usuario="+document.getElementById("usuario").value+"&password="+document.getElementById("password").value;
	var ajax=new objetoAjax();	
	ajax.open("POST","contPHP/validadeposito.php?",true);
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	ajax.send(param);
	ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
				rpta=ajax.responseText.split("@@@");	
			if(rpta[0]=="OK")
			{
				alert("Bienvenido:"+rpta[2]);
				//window.open(rpta[1],"_PARENT","fullscreen");
				//window.location.href=rpta[1];
				document.frm.action=rpta[1];
				document.frm.target="_top";
				document.frm.submit();
				
			}
			else {alert(rpta);}
		}
	}
	isWorking = true;
}
function objetoAjax(){
var xmlhttp;
		/*@cc_on
		@if (@_jscript_version >= 5)
			try
				{
					xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				}
			catch (e)
				{
					try
						{
							xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
						}
					catch (E)
						{
							xmlhttp = false;
						}
				}
		@else
			xmlhttp = false;
		@end @*/


		if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
			{
				try
					{
						xmlhttp = new XMLHttpRequest(); 
					}
				catch (e)
					{
						xmlhttp = false; 
					}
			}
		return xmlhttp;  
}
var ajax=new objetoAjax();


function cerrarSession()
{
	param="caso=cerrar";
	
	var ajax=new objetoAjax();	
	
	ajax.open("POST","../contPHP/validadeposito.php?",true);
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	ajax.send(param);
	ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
				alert('Debe Cerrar');
		}
	}
	isWorking = true;
}
function cerrarSessionII()
{
	document.location.href="../usuarios/cpanelusuario/logout.php";
}
function str_replace(busca, repla, orig)
{
	str 	= new String(orig);

	rExp	= "/"+busca+"/g";
	rExp	= eval(rExp);
	newS	= String(repla);

	str = new String(str.replace(rExp, newS));

	return str;
}
function stristr( haystack, needle, bool ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfxied by: Onno Marsman
    // *     example 1: stristr('Kevin van Zonneveld', 'Van');
    // *     returns 1: 'van Zonneveld'
    // *     example 2: stristr('Kevin van Zonneveld', 'VAN', true);
    // *     returns 2: 'Kevin '
 
    var pos = 0;
 
    haystack += '';
    pos = haystack.toLowerCase().indexOf( (needle+'').toLowerCase() );
    if( pos == -1 ){
        return false;
    } else{
        if( bool ){
            return haystack.substr( 0, pos );
        } else{
            return haystack.slice( pos );
        }
    }
}