//Comprueba una cadena de texto si esta vacia		
function campo_vacio(cadena)
{
	if(cadena.length==0)
		return(true);
	else
		return(false);					
}
//Comprueba que una cadena es un numero, el minimo y maximo, y numero de cifras que tiene		
function comprueba_numero(numero,min,max,longitud)
{
	var resultado;
	if(!(isNaN(numero))&&(parseInt(numero)>=min)&&(parseInt(numero)<=max)&&(numero.length==longitud))
		resultado=true;
	else
		resultado=false;
	return(resultado);
}

//Comprueba partes de fecha, utilizado en "valida_fecha"		
/*function tres_trozos_validos(trozos)
{
	var dia,mes,anyo,resultado;
	dia=trozos[0];
	mes=trozos[1];
	anyo=trozos[2];
	if((comprueba_numero(dia,01,31,2))&&(comprueba_numero(mes,01,12,2))&&(comprueba_numero(anyo,1900,9999,4)))
		resultado=true;
	else
		resultado=false;
	return(resultado);
}
*/
//Valida fechas	
function valida_fecha(cadena)
{
	var trozos_fecha=cadena.split("-");
	if ((trozos_fecha.length==3) && (tres_trozos_validos(trozos_fecha)))
		return(true);
	else
	{
		alert("No es una fecha valida");
		return(false);
	}
}

//Comprueba que una cadena solo tenga los caracteres validos segun tipo:
//0 -> alfanumerico, 1 -> alfabeto español+espacio, 2 -> entre a-z minusculas (ext del email)
//3 -> caracteres validos para dominio email, 4 -> caracteres validos para el usuario email
function comprueba_tipo_cadena(cadena,tipo)
{
	var caracteres,devuelve,contador,contador_caracteres;
	switch(tipo)
	{
		case 0:caracteres="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
			break;
		case 1:caracteres=" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZáéíóúñüçÁÉÍÓÚÑÜÇ";
			break;
		case 2:caracteres="abcdefghijklmnopqrstuvwxyz";
			break;
		case 3:caracteres="abcdefghijklmnopqrstuvwxyz0123456789-_";
			break;
		case 4:caracteres="abcdefghijklmnopqrstuvwxyz0123456789.-_";
			break;
		default:alert("Error en tipo");
			break;
	}
	devuelve=true;
	contador=0;
	while((devuelve)&&(contador<cadena.length))
	{
		contador_caracteres=0;
		devuelve=false;
		while((contador_caracteres<caracteres.length)&&(!devuelve))
		{
			if(cadena.charAt(contador)==caracteres.charAt(contador_caracteres))
				devuelve=true;
			else
				contador_caracteres++;
		}
		contador++;
	}
	return(devuelve);
}

//Valida email
function valida_email(email)
{
	var trozos_email,trozos_dominio,contador,devuelve;
	email=email.toLowerCase(email);
	trozos_email=email.split("@");
	if(trozos_email.length!=2)
		return(false);
	if(!comprueba_tipo_cadena(trozos_email[0],4))
		return(false);
	if(trozos_email[1].indexOf(".")<=0)
		return(false);
	if(trozos_email[1].lastIndexOf(".")==(trozos_email[1].length-1))
		return(false);		
	trozos_dominio=trozos_email[1].split(".");
	if(trozos_dominio.length<2)
		return(false);
	contador=0;
	devuelve=true;
	while((contador<=(trozos_dominio.length-2))&&devuelve)
	{
		if(campo_vacio(trozos_dominio[contador]))
			devuelve=false;
		else
			if(!comprueba_tipo_cadena(trozos_dominio[contador],3))
				devuelve=false;
			else
				contador++;		
	}		
	if(!devuelve)
		return(false);
	if((trozos_dominio[trozos_dominio.length-1].length<2)||(trozos_dominio[trozos_dominio.length-1].length>4)||(!comprueba_tipo_cadena(trozos_dominio[trozos_dominio.length-1],2)))
		devuelve=false;
	return(devuelve);
}

//Calcula el digito de control de un número de cuenta
//se le pasan los parametros como cadenas->entidad,sucursal,dc,cuenta
//no comprueba que son números, hay que hacerlo antes
//devuelve true si el dc es correcto y false si no
function valida_dc(entidad,sucursal,dc,cuenta)
{
	var suma,a,b,d,c,d_mas_c;
	var pesos=Array(1,2,4,8,5,10,9,7,3,6);
	while(entidad.length<4)
		entidad="0"+entidad;
	while(sucursal.length<4)
		sucursal="0"+sucursal;
	while(dc.length<2)
		dc="0"+dc;
	while(cuenta.length<10)
		cuenta="0"+cuenta;
	var entidad_sucursal=entidad+sucursal;
	suma=0;
	b=2;
	for(a=0;a<8;a++)
	{
		suma=suma+(pesos[b]*parseInt(entidad_sucursal.charAt(a)));
		b++;
	}
	d=11-(suma%11);
	if(d==10)
		d=1;
	if(d==11)
		d=0;
	suma=0;
	for(a=0;a<10;a++)
		suma=suma+(pesos[a]*parseInt(cuenta.charAt(a)));
	c=11-(suma%11);
	if(c==10)
		c=1;
	if(c==11)
		c=0;
	d_mas_c=""+d+c;
	if(d_mas_c==dc)
		return(true);
	else
		return(false);
}

// - 
function seleccionarcheckbox(el_form, checkeo, nombresinid, min, max)
{
    for (var i = min; i < max; i++) {
        if (typeof(document.forms[el_form].elements[nombresinid + i]) != 'undefined') {
            document.forms[el_form].elements[nombresinid + i].checked = checkeo;
        }
        if (typeof(document.forms[el_form].elements[nombresinid + i + 'r']) != 'undefined') {
            document.forms[el_form].elements[nombresinid + i + 'r'].checked = checkeo;
        }
    }

    return true;
} 

//valor necesitado para la funcion de los colores de tablas
var marked_row = new Array;

function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 3.3 ... Opera changes colors set via HTML to rgb(r,g,b) format so fix it
    if (currentColor.indexOf("rgb") >= 0)
    {
        var rgbStr = currentColor.slice(currentColor.indexOf('(') + 1,
                                     currentColor.indexOf(')'));
        var rgbValues = rgbStr.split(",");
        currentColor = "#";
        var hexChars = "0123456789ABCDEF";
        for (var i = 0; i < 3; i++)
        {
            var v = rgbValues[i].valueOf();
            currentColor += hexChars.charAt(v/16) + hexChars.charAt(v%16);
        }
    }

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
            // Garvin: deactivated onclick marking of the checkbox because it's also executed
            // when an action (like edit/delete) on a single item is performed. Then the checkbox
            // would get deactived, even though we need it activated. Maybe there is a way
            // to detect if the row was clicked, and not an item therein...
            // document.getElementById('id_rows_to_delete' + theRowNum).checked = true;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
            // document.getElementById('id_rows_to_delete' + theRowNum).checked = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
            // document.getElementById('id_rows_to_delete' + theRowNum).checked = false;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function
