/*ajax_conector*/
var imagen_cargando='adjunto/cargando.gif';

function ltrim(cadena)
{
   var whitespace = new String(' \t\n\r');
   var s = new String(cadena);
   
   if (whitespace.indexOf(s.charAt(0)) != -1)
   {
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }
   return s;
}
function rtrim(cadena)
{
   var whitespace = new String(' \t\n\r');
   var s = new String(cadena);
   if (whitespace.indexOf(s.charAt(s.length-1)) != -1)
   {
      var i = s.length - 1;       // Get length of string
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }

   return s;
}

function trim( cadena )
{
   return rtrim( ltrim( cadena ) );
}

function verificarValor( cadena, valor )
{
	for (var i=0; i < valor.length; i++) 
	{
		if (cadena.indexOf(valor.charAt(i))==-1)
		{
//			alert( cadena.charAt(i) );
			return false; 
		}
	}
	
	return true;
}

function esEntero( valor )
{
	return verificarValor( '1234567890', valor );
}

function esAlfabetico( valor )
{
	return verificarValor( 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZñÑáéíóúÁÉÍÓÚ,;.:-_* ', valor );
}

function esAlfaNumerico( valor )
{
	return verificarValor( 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZñÑáéíóúÁÉÍÓÚüÜ,;.:-_*0123456789()[]/\'\" ', valor );
}

function esTelefono( valor )
{
	return verificarValor( '1234567890-/', valor );
}

function esNombreUsuario( valor ) //contrasenia
{
	return verificarValor( 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_0123456789[]', valor );
}

function esUrl( valor )
{
	//return verificarValor( 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_0123456789[]:/&=%()?@', valor );
	return true;
}

function esFecha( valor ) //dia-mes-anio
{
	var fecha= new String( valor );
//	var RealFecha= new Date();
	var anio= new String(fecha.substring(fecha.lastIndexOf('-')+1,fecha.length));
	var mes= new String(fecha.substring(fecha.indexOf('-')+1,fecha.lastIndexOf('-')));
	var dia= new String(fecha.substring(0,fecha.indexOf('-')));

	if (isNaN(anio) || anio.length<4 || parseFloat(anio)<1900)
		return false;
	if (isNaN(mes) || parseFloat(mes)<1 || parseFloat(mes)>12)
		return false;
	if (isNaN(dia) || parseInt(dia, 10)<1 || parseInt(dia, 10)>31)
		return false;
	if (mes==4 || mes==6 || mes==9 || mes==11 || mes==2)
	{
		if (mes==2 && dia > 28 || dia>30)
			return false;
	}  
	
	return true;
}

/*
* Inicializa el foco en algun campo del formulario
*/
function focoInicial( id_formulario, campo )
{
	if( existeElemento( id_formulario, campo ) )
	{
		var obj=document.forms[id_formulario];

		if( eval( 'obj.' + campo + '.readOnly' )==false )
			eval( 'obj.' + campo + '.focus();' );
	}
}

/*
* Verifica la existencia de un objeto dentro del formulario indicado
*/
function existeElemento( id_formulario, campo )
{
	var obj=document.forms[id_formulario];

	if( typeof( eval( 'obj.' + campo ) )=='object' )
		return true;
	else
		return false;
}

/*
* Oculta si existe la fila de la tabla especificada
*/
function ocultarFila( ocultar, fila )
{
	if( document.getElementById(fila)!=null )
	{
		if( ocultar )
			document.getElementById(fila).style.display ='none';
		else
			document.getElementById(fila).style.display ='';
	}
}

/*
* Presenta los mensajes de alerta para las validaciones
*/
function validarCampo( obj, mensaje )
{
	if( ! esObjeto( obj ) )
	{
		alert( 'El campo con el mensaje [' + mensaje + '] parece tener problemas.' )
		return false;
	}

	switch( obj.type )
	{
		case 'text': //caja
		case 'password': //caja
		case 'textarea': //textarea
			if( trim( obj.value )=='' )
			{
				alert( mensaje );
				
				if( obj.readOnly==false )
					obj.focus();

				return false;
			}
		break;
		case 'hidden':	//solo verifica si contiene algun valor
			if( trim( obj.value )=='' )
			{
				alert( mensaje );
				return false;
			}
		break;
		case 'select-one': //combo
			if( obj.value==0 )//if( obj.selectedIndex==0 )
			{
				alert( mensaje );
				obj.focus();

				return false;
			}
		break;
		
		case 'select-multiple': //lista
			if( obj.length==0 )
			{
				alert( mensaje );
				obj.focus();

				return false;
			}
		break;

		
		default:
			alert( 'No se ha definido validaciones para este tipo de campo de formulario: ' + obj.type );
			return false;
	}
	
	return true;
}

function esObjeto( obj )
{
	if( typeof( eval( 'obj' ) )!='object' )
		return false;
	
	return true;
}

/*
* Interactúa con verificarValor() y sus dependencias, para validar la entrada de caracteres 
* en un campo a la hora de validarlos
*/
function validarCampoEntrada( obj, funcion, mensaje )
{
	if( ! eval( funcion + '(trim(obj.value))' ) )
	{
		alert( mensaje );

		if( obj.readOnly==false )
			obj.focus();

		return false;
	}
	
	return true;
}

/*Revisar en el caso de textarea*/
function validarLogitud( obj, longitud, mensaje, caso )
{
	var retorno=true;

	if( ! esObjeto( obj ) )
	{
		alert( 'El campo con el mensaje [' + mensaje + '] parece tener problemas.' )
		return false;
	}

	switch( caso )
	{
		case 1: //mayor a
			if( trim(obj.value).length<=longitud )
				retorno=false; break;
		break;
		case 2: //menor a

			if( trim(obj.value).length>=longitud )
				retorno=false; break;
		break;
	}
	
	if( retorno )
	{
		alert( mensaje );

		if( obj.readOnly==false )
			obj.focus();
		
		return false;
	}

	return true;
}

function validarRadio( obj, mensaje )
{
	if( ! esObjeto( obj ) )
	{
		alert( 'El campo con el mensaje [' + mensaje + '] parece tener problemas.' )
		return false;
	}
	
	var seleccion=false;
	
	for( i=0; i<obj.length; i++ )
	{
		if( obj[i].checked )
			seleccion=true;
	}

	if( ! seleccion )
	{
		alert( mensaje );
		obj[0].focus();
	}
	
	return seleccion;
}

/*
* Devuelve una cadena con los ids seleccionados (checkbox) de un formulario, separados por una coma (',')
* chk 	objecto. Control tipo checkbox que se marca para seleccionar una lista
*/
function construirChkItem( chk )
{
	var chkItem='';

	if( typeof( chk )!='object' )
       	return chkItem;

	if( chk.checked )
       	chkItem =chk.value;
	else
	{
		for( i=0;i<( chk ).length;i++ )
		{
			if(chk[i].checked)
			{
				if ( chkItem=='' )
					chkItem =chk[i].value;
				else
					chkItem +=','+chk[i].value;
			}
		}
	}

	return chkItem;
}


/*
Depuración
*/

/**
 * Function : dump()
 * Arguments: The data - array,hash(associative array),object
 *    The level - OPTIONAL
 * Returns  : The textual representation of the array.
 * This function was inspired by the print_r function of PHP.
 * This will accept some data as the argument and return a
 * text that will be a more readable version of the
 * array/hash/object that is given.
 * Docs: http://www.openjs.com/scripts/others/dump_function_php_print_r.php
 */
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}
