var TIMEOUT_TOTAL = 1500;
var TIMEOUT_AVISO = 60;

var COLOR_NEGRO = '#000000';
var COLOR_BLANCO = '#FFFFFF';
var COLOR_ROJO = '#FF0000';
var COLOR_VERDE = '#00FF00';
var COLOR_AMARILLO = '#FFFF00';
var COLOR_AZUL = '#0000FF';
var COLOR_AZUL_MARINO = '#24437D';
var COLOR_GRIS_CLARO = '#CCCCCC';
var COLOR_GRIS_OSCURO = '#666666';

var MSG_YA_CONECTADO = 'El usuario ya se encuentra conectado a este mercado desde otra terminal. ' +
	'Por lo tanto el modo de su conexion es de SOLO LECTURA. ' +
	'En este modo no se permite la realizacion de cambios.';
var MSG_ACCEDIDO_ADM_USU = 'Un administrador se encuentra en estos momentos realizando cambios sobre usuarios . ' +
	'Por lo tanto el modo de su conexion es de SOLO LECTURA. ' +
	'En este modo no se permite la realizacion de cambios.';
var MSG_ACCEDIDO_ADM_EMP = 'Un administrador se encuentra en estos momentos realizando cambios sobre empresas . ' +
	'Por lo tanto el modo de su conexion es de SOLO LECTURA. ' +
	'En este modo no se permite la realizacion de cambios.';
var MSG_ACCEDIDO_ADM_MRC = 'Un administrador se encuentra en estos momentos realizando cambios sobre este mercado . ' +
	'Por lo tanto el modo de su conexion es de SOLO LECTURA. ' +
	'En este modo no se permite la realizacion de cambios.';
var MSG_ACCEDIDO_CNT = 'Un usuario se encuentra conectado al sistema tomando sus decisiones. ' +
	'Para pasar jornada haga click en Aceptar. Los usuarios presentes en el sistema seran desconectados.';
var MSG_NO_CUENTA_AJENA = "A partir de ahora no puede trabajar a cuenta ajena.";


// Control temporal de sesion

var tiempo = 0;
var fin_sesion = 'fin_sesion.jsp';

function timeout()
{
	alert('Su sesion se acabara en ' + TIMEOUT_AVISO + ' segundos. Haga click para permanecer conectado.');
	if(new Date().getTime() - tiempo > TIMEOUT_TOTAL * 1000)
	{
		top.accion = 'fin_sesion';
		top.location.href = fin_sesion;
	}
	else inicio();
}

function inicio()
{
	tiempo = new Date().getTime();
	window.setTimeout('timeout()', (TIMEOUT_TOTAL - TIMEOUT_AVISO) * 1000);
}

function SESION(fs)
{
	fin_sesion = fs;
	inicio();
}

// Accion

var accion = '';

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

// Funciones dreamweaver

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function cadenaValida(cad)
{
	var i = 0;
		
	if(cad.length == 0 || cad.length > 12) return false;
		
	for(i = 0; i < cad.length;i++)
		if(cad.charAt(i) < '0' ||
			(cad.charAt(i) > '9' && cad.charAt(i) < 'A') ||
			(cad.charAt(i) > 'Z' && cad.charAt(i) < 'a') ||
			cad.charAt(i) > 'z') return false;
			   
	return true;
}

function hex(n)
{
	if(n < 10) return String.fromCharCode(48 + n);
	else return String.fromCharCode(55 + n);
}

function nohex(s)
{
	var c = s.charCodeAt(0);
	if(c < 58) return c - 48;
	else return c - 55;
}

function hex2(n)
{
	return '%' + hex(n/16) + hex(n%16);
}

function nohex2(s)
{
	return String.fromCharCode(nohex(s.charAt(1)) * 16  + nohex(s.charAt(2)));
}

function lineal(s)
{
	var i = 0;
	var res = '';
	var cod;

	i = 0
	while(i < s.length)
	{
		cod = s.charCodeAt(i);
		if(cod == 32 || 
			(cod >= 48 && cod <= 57) ||
			(cod >= 97 && cod <= 122) ||
			(cod >= 65 && cod <= 90)) res += s.charAt(i);
		else res += hex2(cod);

		i++;
	}

	return res;
}

function nolineal(s)
{
	var i = 0;
	var res = '';

	i = 0
	while(i < s.length)
	{
		car = s.charAt(i);
		if(car != '%')
		{
			res += car;
			i++;
		}
		else
		{
			res += nohex2(s.substring(i, i + 3));
			i = i + 3;
		}
 	}

	return res;
}

function porcentaje(x,y,m)
{
	var pctg;

	if(m == -1)
	{
		swap = x;
		x = y;
		y = swap;
	}

	if(y == 0 && x > 0) return 'Sup.';
	if(x == y) return '0.0%';
	if(x == 0) return '-100%';

	x *= 1;
	y *= 1;

	if(x > y)
	{
		pctg = Math.abs(Math.floor(x * 1000/y) - 1000);
		if(pctg > 10000) return 'Sup.';
		else return '+' + Math.floor(pctg/10) + '.' + pctg%10 + '%';
	}

	if(x < y)
	{
		pctg = Math.abs(1000 - Math.floor(x*1000/y));
		if(pctg > 10000) return 'Inf.';
		else return '-' + Math.floor(pctg/10) + '.' + pctg%10 + '%';
	}
}

function miles(n)
{
	var t  = '';
	var n1 = 0;
	var negativo = 0;
	
	if(n == 0) return '0';

	if(n < 0)
	{
		negativo = 1;
		n = Math.abs(n);
	}

	while(n > 0)
	{
		n1 = Math.floor(n / 1000);
		r = n - n1 * 1000;
		n = n1;

		if(n > 0)
		{
			st = '';
			i = 0;
			while(i < 3)
			{
				if(r > 0) st = r%10 + st;
				else st = '0' + st;
				r =  Math.floor(r/10);

				i++;
			}

			t = '.' + st + t;
		}
		else t = r + t;
	}

	if(negativo == 1) t = '-' + t;

	return t;
}

function componer_numero(n)
{
	var numero = '';
	var pent, pdec, pdec1, pdec2;

	pent = Math.floor(n / 100);
	numero = numero + pent + ".";
	pdec = n - (pent *100);
	pdec1 = Math.floor(pdec /10);
	numero  = numero + pdec1;
	pdec2 = pdec - (pdec1*10);
	numero = numero + pdec2;

	return numero;
}

function componer_numero1(n)
{
	var numero = '';
	var pent, pdec, pdec1;

	pent = Math.floor(n / 100);
	numero = numero + pent + ".";
	pdec = n - (pent * 100);
	pdec1 = Math.floor(pdec /10);
	numero  = numero + pdec1;

	return numero;
}

function componer_entero(n,ta)
{
	var tam_parte_entera = ta;

	var numero = '';
	var i = 0;

	while(i < tam_parte_entera)
	{
		if(n == 0) numero = '0' + numero;
		else
		{
			numero = '' + (n % 10) + numero; 
			n = Math.floor(n / 10);
		}
		i++;
	}
	return numero;
}

function quitarCeros(valor) {
	var j;
	var number = "";
	var num = 0;
	var i=0;
	while((valor.charAt(i) == '0') && (i<valor.length))
		i++;
	if(i<valor.length) {
		for(j=i;j<valor.length;j++)
			number += valor.charAt(j);
		num = parseInt(number);
	}
	return num;		
}

function imprimir_valor(t,x,y,c)
{	
	with(window.pantalla.document)
	{
	writeln('<div id="Layer21" style="position:absolute; left:' + x + '; top:' + y + 'px; width:100; height:27; z-index:150; visibility: visible; text-align:right">');
	writeln('<FONT face="Verdana, Arial, Helvetica, sans-serif" size=2 color="' + c + '">' + t + '</FONT>');
	writeln('</div>');
	}
}

//*******
// Salir
//*******

function salir()
{
	if(confirm('¿Desea salir? Se perderan los cambios que no hayan sido guardados'))
	{	
		top.accion = 'salir';
		top.location.href = '../../cerrar.jsp';
	}
	else return false;
}

function salirVentana(dir)
{
	window.open(dir,'Cerrar',
		'left=0,top=0,width=0,height=0,alwaysRaised=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,z-lock=1');
}

function accionado(){
		accion="si";
}

function comprobarSalir(dir){

	if (accion=='')
	{
		salirVentana(dir);
	}else{
		accion='';
	}

	return false;
}

function comprobarEmail(cadena){
	var textoPara = cadena;
	var indiceArroba = textoPara.indexOf("@");
	var indiceArroba2 = textoPara.indexOf("@",indiceArroba + 1);
	var indicePunto = textoPara.indexOf(".",indiceArroba + 1);

	if (indiceArroba == -1 || indiceArroba2 != -1 ||
		indicePunto == -1 ||
		indicePunto == textoPara.length - 1 ||
		indicePunto == textoPara.length - 2 ||
		indiceArroba == 0 || indicePunto == indiceArroba + 1){

		return false;

	}else{

		return true;

	}

}

function click() {
	return false;
}

function arrastre(){
	return false;
}

function keydown(n)
{
	var key_f5 = 116; // 116 = F5
	var key_retroceso = 8;
	if (key_f5 == event.keyCode ||
		(key_retroceso == event.keyCode && event.srcElement.type != 'text' &&
			event.srcElement.type != 'textarea' && event.srcElement.type != 'password')){
		event.keyCode = 27;
		return false;
	}
}

if(document.all)
{
	document.oncontextmenu = click;
	document.onkeydown = keydown;
	document.ondragstart = arrastre;
}


function cursorMano(nombreInicio){


	for (imagenes in document.images )
	{
		if (imagenes.indexOf(nombreInicio)==0)
		{
			document.images[imagenes].style.cursor='hand';
		}
	}
}

function estilosBotones(){
	var boton;
	var listaInputs;

	listaInputs = document.getElementsByTagName('INPUT');
	for (i = 0 ; i < listaInputs.length; i++)
	{
		boton = listaInputs.item(i);

		if (boton.getAttribute('type')== 'button' || boton.getAttribute('type')== 'submit' )
		{
			boton.style.textAlign='center';
			boton.style.backgroundColor='transparent';
			boton.style.borderWidth=1;
			boton.style.borderColor='white';
		}
	}
}

function estilosCheckBox(){
	for (elementos in document.getElementsByTagName('INPUT'))
	{
		if (elementos.indexOf('checkbox')==0)
		{
			document.all[elementos].style.backgroundColor='transparent';
		}
	}
}

function estilosInputText(){
	var listaInputs = document.getElementsByTagName('INPUT');
	for (i = 0; i < listaInputs.length; i++){
		elemento = listaInputs.item(i);

		if (elemento.getAttribute('type') == "text")
		{
			elemento.style.backgroundColor='#FFFFFF';
			elemento.style.borderWidth=0;

		}
	}
}

var aux;
function propiedadesObjeto(objeto){
	aux = "";
	for (elemento in objeto)
	{
		aux += elemento + ": " + objeto[elemento] + "\n";
	}
	alert(aux);
}

function crearSelect(opciones, idSelect){
	elementoSelect = document.getElementById(idSelect);
	
	for (i = 0; i < opciones.length; i++){
		nuevaOpcion = new Option(opciones[i][0],opciones[i][1]);
		elementoSelect.add(nuevaOpcion);
	}
	if (numProductoElegido != 0){
		elementoSelect.selectedIndex = numProductoElegido;
	}
}

function verArray(matriz, nombreMatriz)
{
	cadena="";
	for (i = 0; i < matriz.length; i++)
	{
		cadena += nombreMatriz + "[" + i + "]=" + matriz[i] + "\n";
	}
	alert(cadena);
}

function listarPropiedades(obj, objName) {
    var result = "";
    for (var i in obj) {
        result += objName + "." + i + "=" + obj[i] + "";
    }
    alert(result);
}

