/**
* Toggles the check state of a group of boxes
*
* Checkboxes must have an id attribute in the form cb0, cb1...
* @param The number of box to 'check'
* @param An alternative field name
*/
function checkAll( n, fldName ) {
  if (!fldName) {
     fldName = 'cb';
  }
	var f = document.formulario;
	var c = f.toggle.checked;
	var n2 = 0;
	for (i=0; i < n; i++) {
		cb = eval( 'f.' + fldName + '' + i );
		if (cb) {
			cb.checked = c;
			n2++;
		}
	}
	if (c) {
		document.formulario.boxchecked.value = n2;
	} else {
		document.formulario.boxchecked.value = 0;
	}
}

function listItemTask( id, task ) {
    var f = document.formulario;
    cb = eval( 'f.' + id );
    if (cb) {
        for (i = 0; true; i++) {
            cbx = eval('f.cb'+i);
            if (!cbx) break;
            cbx.checked = false;
        } // for
        cb.checked = true;
        f.boxchecked.value = 1;
        submitbutton(task);
    }
    return false;
}

function isChecked(isitchecked){
	if (isitchecked == true){
		document.formulario.boxchecked.value++;
	}
	else {
		document.formulario.boxchecked.value--;
	}
}

/**
* Default function.  Usually would be overriden by the component
*/
function submitbutton(pressbutton, valor) {
	if (!valor) valor = '';
	submitform(pressbutton, valor);
}

/**
* Submit the admin form
*/
function submitform(pressbutton, valor){
	document.formulario.task.value=pressbutton;
	document.formulario.value.value=valor;
	try {
		document.formulario.onsubmit();
		}
	catch(e){}
	document.formulario.submit();
}

/* window 'load' attachment */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			if (func) {
                func();
            }
		}
	}
}

function haySeleccionados(){
	if(document.formulario.boxchecked.value == 0) {
		alert("debe seleccionar algun elemento de la lista.");
		return false;
	}
	return true;
}

function vsVisible(valor) {
	if (haySeleccionados()) submitbutton("visible", valor);
}

function vsHabilitado(valor) {
	if (haySeleccionados()) submitbutton("habilitado", valor);
}

function vsEliminar() {
	if (haySeleccionados() && confirma("Está seguro que desea eliminar los registros seleccionados?")) submitbutton("eliminar");
}

function confirma(string){
	return window.confirm(string);
}

//How to Use:    For example, to return the answer for mod 2 of 16, type: mod(16,2);
function mod(divisee,base) {
	// Created 1997 by Brian Risk.  http://members.aol.com/brianrisk
	return Math.round(divisee - (Math.floor(divisee/base)*base));
}

function keysonly(fieldType, myfield, e) {
     var key;
     var keys;
     var keychar;

	 if (fieldType=='int') keys = "0123456789";
	 if (fieldType=='decimal') keys = "0123456789.,";
	 if (fieldType=='date') keys = "0123456789/";
	 if (fieldType=='hour') keys = "0123456789:";

     if (window.event){
        key = window.event.keyCode;}
     else if (e)
        key = e.which;
     else
        return true;
     keychar = String.fromCharCode(key);

     // control keys
     if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27))
        return true;
     // numbers
     else if ((keys.indexOf(keychar) > -1))
        return true;
     else
        return false;
}

function CheckLength(elemento, name, length) {
	var longitud;
	longitud = eval(elemento + '.value.length');
	if (longitud > length) {
		alert('Has superadon los ' + length + ' caracteres permitidos para '+ name +'. (' + longitud + ')');
		return false;                         
	}
return true;
}

function leftTrim(sString) {
	if (sString == "") return "";
	while (sString.substring(0,1) == ' '){
		sString = sString.substring(1, sString.length);
	}
	return sString;
}

function rightTrim(sString) {
	if (sString == "") return "";
	while (sString.substring(sString.length-1, sString.length) == ' '){
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function trimAll(sString)  {
	return leftTrim(rightTrim(sString));
}

