/*****
	JavaScript	:	general.js
	Proposito	:	Definir las funciones generales usadas por el aplicativo de Camara Online
*****/

// Variable que define si el usuario termino la sesión en Camara Online
cerrar 		= false;
url      	= "logout.htm";
opciones 	= "toolbar=no,location=no,directories=no,menubar=no,top=50,letft=300,width=500,height=50";

document.onkeydown = valTeclas;

document.onmousedown = valTeclas;

//document.oncontextmenu = function(){return false;}

function valTeclas(){
	// Click derecho
	if(event.button == 2){
		return false;
	}

	if(window.event){
		if(event.srcElement.tagName != "INPUT" && window.event.keyCode == 8){
			window.event.keyCode = 0;
			return false;
		}
		if(event.ctrlKey && (window.event.keyCode == 78 || window.event.keyCode == 85)){
			window.event.keyCode = 0;
			return false;
		}
		// Tecla F11
		if(window.event.keyCode == 122){
			window.event.keyCode = 0;
			return false;
		}
	}
}

// Se cambia el estado del suscriptor en Camara Online de conectado a desconectado
function salir() {
    if(cerrar) {
      self.close();
      window.open(url, "ventana", opciones);
    }
}

// Cierra la pagina padre que crea otra por medio de la función window.open, sin pedir
// confirmación de cierre al usuario
function closeMain() {
	if(buscarCookie("cerroindex") != "S") {
		window.opener.opener='a';
		window.opener.close();
	}
		
	ponerCookie("cerroindex", "S", "", "", "", "");

	return false;	  
}

var IE = document.all?true:false;

// Se obtiene el tipo de navegador NS o IE
if (!IE) document.captureEvents(Event.MOUSEMOVE);

// Se asigna la funcíon getMouseXY al evento
//document.onmousemove = getMouseXY;

var tempX = 0;
var tempY = 0;

// Función para obtener la posición del mouse
function getMouseXY(e) {
  if (IE) { // Posición del mouse para IE
    tempX = event.clientX + document.body.scrollLeft;
    tempY = event.clientY + document.body.scrollTop;
  }
	else {  // Posición del mouse para NS
    tempX = e.pageX;
    tempY = e.pageY;
  }  
  // Se obtienen posibles valores negativos para NS4
  if (tempX < 0){tempX = 0};
  if (tempY < 0){tempY = 0};
	
	if (tempX <= 0 || tempY <= 0)
		cerrar = true;
	else
		cerrar = false;
	
	// Se evalua si el usuario cerro la aplicación
	salir();
		
  return true;
}

function buscarCookie(Name) {
  var search = Name + "=";
  var returnvalue = "";
  if(document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    if(offset != -1) { 
      offset += search.length
      end = document.cookie.indexOf(";", offset);
      if(end == -1)
				end = document.cookie.length;
      returnvalue = unescape( document.cookie.substring( offset, end ));
      }
   }
  return returnvalue;
}

function valCertiEst(scfg) {
  if(scfg==undefined||scfg==null){var scfg = "";}  
	if(buscarCookie("matestab") == "") {
		alert("Debe seleccionar el establecimiento para generar el certificado");
		window.open("establecimientos", "_self");
		return false;
	}
	window.open("certificadoest" + scfg, "_self");
}

// Crear la cookie con el nombre y valor recibidos como parametro
function ponerCookie( name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape(value) +
											(( expires ) ? ";expires=" + expires.toGMTString() : "" ) +
											(( path ) ? ";path=" + path : "" ) + 
											(( domain ) ? ";domain=" + domain : "" ) +
											(( secure ) ? ";secure" : "" );
}

function valBuscar() {
	if(document.forms[0].textfield.value == "") {
		alert("Debe digitar el criterio de busqueda");
		document.forms[0].textfield.focus();
		return false;
	}
	return true;
}

function valUser() {
	if(document.forms[0].usuario.value == "") {
		alert("Debe digitar su código de usuario");
		document.forms[0].usuario.focus();
		return false;
	}
	if(document.forms[0].clave.value == "") {
		alert("Debe digitar su clave");
		document.forms[0].clave.focus();
		return false;
	}
	return true;
}

// Valida la fecha recibida como parametro
function valFecha(fechaini, fechafin) {
	if(fechaini == "" || fechafin == "") {
		alert("Debe digitar las fechas para generar la consulta");
		return false;
	}
	
	if(fechaini.indexOf("/") == -1 || fechaini.indexOf("/") != 4 || fechaini.length != 10 ||
	   fechafin.indexOf("/") == -1 || fechafin.indexOf("/") != 4 || fechafin.length != 10) {
		alert("Debe digitar la fecha en el formato aaaa/mm/dd");	
		return false;
	}

	var anoini, anofin, mesini, mesfin, diaini, diafin;
	anoini	= fechaini.substr(0,4);
	mesini  = fechaini.substr(5,2);
	diaini	= fechaini.substr(8,2);
	anofin	= fechafin.substr(0,4);
	mesfin  = fechafin.substr(5,2);
	diafin	= fechafin.substr(8,2);
	
	if(anoini < 1994 || anofin < 1994) {
		alert("Debe digitar un año mayor o igual a 1994");
		return false;
	}
	if(mesini < 1 || mesini > 12 || mesfin < 1 || mesfin > 12) {
		alert("Debe digitar un mes valido");	
		return false;
	}
	if(diaini < 1 || diaini > 31 || diafin < 1 || diafin > 31) {
		alert("Debe digitar un dia valido");	
		return false;
	}

	if(anoini > anofin) {
		alert("El año inicial para la consulta debe ser menor o igual al año final");	
		return false;
	}

	if(anoini < anofin) {
		alert("No debe digitar rangos superiores a 3 meses");	
		return false;
	}
	
	if((anoini == anofin) && (mesini > mesfin)) {
		alert("El mes inicial para la consulta debe ser menor o igual al mes final");
		return false;	
	}

	if((anoini == anofin) && (mesfin - mesini) > 3) {
		alert("No debe digitar rangos superiores a 3 meses");
		return false;	
	}

	if((anoini == anofin) && (mesini == mesfin) && (diaini > diafin)) {
		alert("El dia inicial para la consulta debe ser menor o igual al dia final");	
		return false;	
	}
	
	if(mesini == 4 || mesini == 6 || mesini == 9 || mesini == 11) {
		if(diaini > 30) {
			alert("El día para la fecha inicial es incorrecto");
			document.forms[0].htm_fecini.focus();
			return false;
		}
	}

	if(mesini == 2) {
		if(diaini > 29) {
			alert("El día para la fecha inicial es incorrecto");
			document.forms[0].htm_fecini.focus();
			return false;
		}
	}

	if(mesfin == 4 || mesfin == 6 || mesfin == 9 || mesfin == 11) {
		if(diafin > 30) {
			alert("El día para la fecha final es incorrecto");
			document.forms[0].htm_fecfin.focus();
			return false;
		}
	}

	if(mesfin == 2) {
		if(diafin > 29) {
			alert("El día para la fecha final es incorrecto");
			document.forms[0].htm_fecfin.focus();
			return false;
		}
	}
	
	return true;
} // Fin function valFecha(fechaini, fechafin) {

function valCertRmJur() {
	ponerCookie("certrmjur", "S", "", "", "", "");

	window.open("certificado", "_self");
}

function salidaSegura() {
    if(confirm("Desea terminar su sesión en Camara Online ?")) {
      //self.close();
      //window.open(url, "_self", opciones);
      window.open(url, "_self");
    }
}

function valKeyPress() {
	if(document.forms[0].htmDato.value.length == 9 )
		return false;

	iKeyCode = event.keyCode;
	if((iKeyCode >= 48 && iKeyCode <= 57) || iKeyCode == 13 ) return true;

	return false;
}

function valNros(){
	forma = document.forms[0];
	
	forma.htm_fecini.value 	= "";
	forma.htm_fecfin.value 	= "";
	forma.htmDato.value			= "";
		
	if(getInputValue(forma.htmConsPor) == "R"){
		forma.htmDato.onkeypress = "";
		forma.htmDato.size="55";
		forma.htm_tpriesgo.disabled = true;
		forma.htm_fecini.disabled 	= true;
		forma.htm_fecfin.disabled 	= true;
		forma.htmDato.disabled 			= false;
	}
	
	if(getInputValue(forma.htmConsPor) == "N"){
		forma.htmDato.onkeypress = valKeyPress;
		forma.htmDato.size="9";
		forma.htm_tpriesgo.disabled = true;
		forma.htm_fecini.disabled 	= true;
		forma.htm_fecfin.disabled 	= true;
		forma.htmDato.disabled 			= false;
	}

	if(getInputValue(forma.htmConsPor) == "T"){
		forma.htm_tpriesgo.disabled = false;
		forma.htm_fecini.disabled 	= false;
		forma.htm_fecfin.disabled 	= false;
		forma.htmDato.disabled 			= true;
	}
	if(getInputValue(forma.htmConsPor) == "M"){
		forma.htmDato.onkeypress = "";
		forma.htmDato.size="9";
		forma.htm_tpriesgo.disabled = true;
		forma.htm_fecini.disabled 	= true;
		forma.htm_fecfin.disabled 	= true;
		forma.htmDato.disabled 			= false;
	}
}

