function validate_integer(el, ev, warn_timeout)
{
    var warn_text = ' is not allowed here in a whole number';
    return validate_field(el, ev, warn_timeout,[ /^([0-9]){1,2}(:([0-9]{0,2})?)?$/ ], [ /^00\d/ ], warn_text);
}

function validate_email(el, ev, warn_timeout)
{
    var warn_text = ' is not allowed here in an email address';
    return validate_field(el, ev, warn_timeout, [ /^([a-zA-Z])[-\.\w]*(@(((\w[-\w]*)\.?)*)?)?$/ ], [], warn_text);
}


        function consisfec(xdia,xmes,xano) {
            var meses=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
            meses[1]=((xano % 4)==0) ? 29 : 28;
            if (xano=='-') return false;
            return ((xdia<=meses[xmes-1]) ? true : false );  // true -> OK     false -> KO
        }


        function noVacio(elm){
          if (elm.value == "" ||
              elm.value == null) 
          return false;
          else return true;
        }


function MM_findObj(n, d) { //v4.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);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_showHideLayers_espe() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers_espe.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
	
	if (v=='visible') {obj.display='';}
	else {obj.display='none'}
	
	obj.visibility=v;
  }
}

function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
	
	if (v=='visible') {obj.display='';}
	else {obj.display='none'}
	
	obj.visibility=v;
  }
/*  var i,p,v,obj,args=MM_showHideLayers.arguments,valorlayer;
  var expdate = new Date ();
  expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000 * 365));
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
        if(args[i+2]=='hide'){valorlayer='none';}
        else{valorlayer='show';}
       SetCookie (Trim(args[i]),valorlayer,expdate);
        //document.cookie =Trim(args[i])+'='+valorlayer;
   if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
	if (v=='visible') {obj.display='';}
	else {obj.display='none'}
	obj.visibility=v;
  }*/
}


        function LTrim(string){
          var i=0;
          var j=string.length-1;
          if(string==null) return (false);
          for(i=0;i< string.length;i++){
            if(string.substr(i,1)!=' ' && string.substr(i,1)!='\t' && string.substr(i,1)!='\n')  break;
          }
          if(i<=j) return(string.substr(i,(j+1)-i))
          else return('');
        }

        function RTrim(string){
          var i=0;
          var j=string.length-1;
          if(string==null) return (false);
          for(j=string.length-1;j>=0;j--){
            if(string.substr(j,1)!=' ' && string.substr(j,1)!='\t'&& string.substr(i,1)!='\n') break;
          }
          if(i<=j) return(string.substr(i,(j+1)-i))
          else return('');
        }

        function Trim(string){
          if(string==null) return (false);
          return RTrim(LTrim(string));
        }


function Set_Cookie( name, value, expires, path, domain, secure ) 
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );
/*
if the expires variable is set, make the correct 
expires time, the current script below will set 
it for x number of days, to make it for hours, 
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function SetCookie (name, value) {
         var argv = SetCookie.arguments;
         var argc = SetCookie.arguments.length;
         var expires = (argc > 2) ? argv[2] : null;
         var path = (argc > 3) ? argv[3] : null;
         var domain = (argc > 4) ? argv[4] : null;
         var secure = (argc > 5) ? argv[5] : false;
         document.cookie = name + "=" + escape (value) +
         ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
         ((path == null) ? "" : ("; path=" + path)) +
         ((domain == null) ? "" : ("; domain=" + domain)) +
         ((secure == true) ? "; secure" : "");
}

function DeleteCookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +";expires=Thu, 01-Jan-1970 00:00:01 GMT";

}

function Get_Cookie( name ) {
	
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) &&
( name != document.cookie.substring( 0, name.length ) ) )
{
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}

/*function DeleteCookie (name) {  
var exp = new Date();  
exp.setTime (exp.getTime() - 1);   
var cval = GetCookie (name);  
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}*/

function GetCookie( name ) {
	
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) &&
( name != document.cookie.substring( 0, name.length ) ) )
{
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}

/*function GetCookie (name) {  
var arg = name + "=";  
var alen = arg.length;  
var clen = document.cookie.length;  
var i = 0;  
while (i < clen) {
var j = i + alen;    
if (document.cookie.substring(i, j) == arg)      
return getCookieVal (j);    
i = document.cookie.indexOf(" ", i) + 1;    
if (i == 0) break;   
}  
return null;
}*/

        function noVacio(elm){
          if (elm.value == "" ||
              elm.value == null) 
          return false;
          else return true;
        }

if (document.images){

flechaon = new Image();
flechaon.src = "images/flechablanca_off.gif";
flechaoff = new Image();
flechaoff.src = "images/flechablanca.gif";

miproyectoson = new Image();
miproyectoson.src = "elems/bot_int/bt_misproyectos_roll.gif";
miproyectosoff = new Image();
miproyectosoff.src = "elems/bot_int/bt_misproyectos.gif";

contacton = new Image();
contacton.src = "elems/bot_int/bt_contactos_roll.gif";
contactoff = new Image();
contactoff.src = "elems/bot_int/bt_contactos.gif";

salirion = new Image();
salirion.src = "elems/bot_int/bt_salirdelaintranet_roll.gif";
salirioff = new Image();
salirioff.src = "elems/bot_int/bt_salirdelaintranet.gif";
          proyectoson = new Image();
          proyectoson.src = "elems/bot_int/proy_RO.gif";
          proyectosoff = new Image();
          proyectosoff.src = "elems/bot_int/proy.gif";
          horason = new Image();
          horason.src = "elems/bot_int/hora_RO.gif";
          horasoff = new Image();
          horasoff.src = "elems/bot_int/hora.gif";
          facturacionon = new Image();
          facturacionon.src = "elems/bot_int/fact_RO.gif";
          facturacionoff = new Image();
          facturacionoff.src = "elems/bot_int/fact.gif";
          clienteson = new Image();
          clienteson.src = "elems/bot_int/clie_RO.gif";
          clientesoff = new Image();
          clientesoff.src = "elems/bot_int/clie.gif";
          equipoon = new Image();
          equipoon.src = "elems/bot_int/equi_RO.gif";
          equipooff = new Image();
          equipooff.src = "elems/bot_int/equi.gif";
          directorioon = new Image();
          directorioon.src = "elems/bot_int/dire_RO.gif";
          directoriooff = new Image();
          directoriooff.src = "elems/bot_int/dire.gif";
          codificacionon = new Image();
          codificacionon.src = "elems/bot_int/copr_RO.gif";
          codificacionoff = new Image();
          codificacionoff.src = "elems/bot_int/copr.gif";
          valoron = new Image();
          valoron.src = "elems/distrib_horas/img_check_acti.gif";
          valoroff = new Image();
          valoroff.src = "elems/distrib_horas/img_check_inac.gif";
}


// implementation functions

function validate_field(el, ev, warn_timeout, match_list, no_match_list, warn_text)
{
    if (!ev) ev = window.event;
    // preserve the start value for replacement in IE if the
    // user enters an invalid character
    var start_text = el.value;
    // allow backspace, deletion and arrow keys
    var keycode = get_keycode(ev);
    if (keycode == 8 || keycode == 9)
    {
        return true;
    }

    // the following unusual code correctly distinguises Mozilla 
    // char key presses from non-char keypresses (and also detects
    // when we get an IE keypress) which allows us to process chars
    // locally, but allow the browser to handle arrow/delete keys etc
    // (For some reason, trying to refer to ev.keyCode in the test
    // below breaks the Moz test, so we use document.selection instead
    // which is IE only)

    if (ev.which)
    {
        // it's a Mozilla keypress - process it here

        ;
    }
    else if (document.selection)
    {
        // it's an IE keypress - process it here

        ;
    }
    else
    {
        return true;
    }
 
    var key = String.fromCharCode(keycode);
    var cur_pos = cursor_pos(el);

    var new_value = insert_at_cursor(el, key);

    // does the new string match one of the 'must match' list ?

    var no_match = true;
    for (var i = 0; i < match_list.length; i++)
    {
        var regex = match_list[i];

        if (new_value.match(regex))
        {
            no_match = false;
            break;
        }
    }
    
    if (no_match)
    {
        if (ev.keyCode) 
        {
            // IE support - replace original value to erase illegal char
            // and reset cursor position
    
            el.value = start_text;
            set_cursor_pos(el, cur_pos);
    
            ev.returnValue = false;
        }
    
        // illegal character - show the user a warning and prevent the
        // character from being shown
    
        //display_warning(el, warn_timeout, key + warn_text);
    
        return false;
    }

    // does the new string fail to match the 'must not match' list ?

    for (var i = 0; i < no_match_list.length; i++)
    {
        var regex = no_match_list[i];

        if (new_value.match(regex))
        {
            if (ev.keyCode) 
            {
                // IE support - replace original value to erase illegal char
                // and reset cursor position
    
                el.value = start_text;
                set_cursor_pos(el, cur_pos);
    
                ev.returnValue = false;
            }
    
            // illegal character - show the user a warning and prevent the
            // character from being shown
    
            //display_warning(el, warn_timeout, key + warn_text);
    
            return false;
        }
    }

    if (ev.keyCode)
    {
        // IE support - ensure we don't insert the valid character again

        ev.returnValue = false;
    }
    else
    {
        // Mozilla - just let the browser insert the valid character

        return true;
    }

}

function get_keycode(ev)
{
    if (ev.keyCode)
    {
        return ev.keyCode;
    }
    else
    {
        return ev.which;
    }
}

// insert_at_cursor is based on code released under the GPL in the
// PHPMySQLAdmin project.

function insert_at_cursor(el, str) {

    // IE support

    var new_value;
    if (document.selection) 
    {
        el.focus();
        var sel = document.selection.createRange();
        sel.text = str;
        new_value = el.value;
    }

    // Mozilla/Netscape support
 
    else if (el.selectionStart || el.selectionStart == '0') 
    {
        var start_pos = el.selectionStart;
        var end_pos = el.selectionEnd;
        new_value = el.value.substring(0, start_pos)
                        + str
                        + el.value.substring(end_pos, el.value.length);
    } 
    else 
    {
        new_value += str;
    }

    return new_value;
}

function set_cursor_pos(el, cur_pos)
{
    if (cur_pos == -1) return;
    el.focus();
    var rng = el.createTextRange();
    rng.move("Character", cur_pos - 1)
    rng.select();
}

function cursor_pos(el)
{
    var i = el.value.length + 1;

    if (el.createTextRange)
    {
        var cursor = document.selection.createRange().duplicate();
        while (cursor.parentElement() == el
               && cursor.move("character", 1) == 1)
        {
             --i; 
        }
    } 
    return (i == el.value.length+1) ? -1 : i;
}

function display_warning(el, timeout, text)
{
    var geom_obj = getGeom(el);

    var warning_box_HTML = get_warning_box_HTML(text);

    var warning_box = document.getElementById('warning_box');
    if (! warning_box)
    {
        add_warning_box();
    }

    warning_box.innerHTML = warning_box_HTML;
    warning_box.style.left = geom_obj.x;
    warning_box.style.top  = geom_obj.y - 165;
    warning_box.style.display = 'inline';

    setTimeout("remove_warning()", (1000 * timeout));
}

function add_warning_box()
{
    var warning_box_HTML = get_warning_box_HTML('dummy');

    document.body.innerHTML += 
        '<span id="warning_box" style="position:absolute; display: none;">' +
         warning_box_HTML                                                   +
        '</span>';
}

function get_warning_box_HTML(text)
{
    return '<table class="Warning_table" id="info_table_1" cellspacing="2" width="350px">' +
           '<tr class="Warning_table_row">'                                                +
           '<td class="Warning_table_data">'                                               +
           text                                                                            +
           '</td>'                                                                         +
           '</tr>'                                                                         +
           '</table>'; 
}

function remove_warning()
{
    var warning_box = document.getElementById('warning_box');
    warning_box.style.display = 'none';
}

function getGeom(el)
{
    var object   = new Object();
    object.x     = el.offsetLeft;
    object.y     = el.offsetTop;
    var parent    = el.offsetParent;
    object.width  = el.offsetWidth;
    object.height = el.offsetHeight;
    while(parent != null)
    {
        object.x += parent.offsetLeft;
        object.y += parent.offsetTop;
        parent   = parent.offsetParent;
    }
    return object;
}