
  String.prototype.compareColor = function(){
    if((this.indexOf("#") != -1 && arguments[0].indexOf("#") != -1) || 
      (this.indexOf("rgb") != -1 && arguments[0].indexOf("rgb") != -1)){
      return this.toLowerCase() == arguments[0].toLowerCase()
    }
    else{
      xCol_1 = this;
      xCol_2 = arguments[0];
      if(xCol_1.indexOf("#") != -1)xCol_1 = xCol_1.toRGBcolor();
      if(xCol_2.indexOf("#") != -1)xCol_2 = xCol_2.toRGBcolor();
      return xCol_1.toLowerCase() == xCol_2.toLowerCase()
    }
  }


  String.prototype.toRGBcolor = function(){
    varR = parseInt(this.substring(1,3), 16);
    varG = parseInt(this.substring(3,5), 16);
    varB = parseInt(this.substring(5,7), 16);
    return "rgb(" + varR + ", " + varG + ", " +  varB + ")";
  }
  
  
function cambiarBgColor (tr, estado, anterior) {
  var color1 = tr.style.backgroundColor;
  var color2 = "#d5d5d5";
  
  if (!color1.compareColor(color2)) {
	  if (estado == 1) {
	    tr.style.backgroundColor = "#ebebeb";
	  }
	  else {
    	tr.style.backgroundColor = anterior;
	  }
	}
}

function cambiarBgColorAM (tr, estado, anterior, id_nuevo, hiddenId) {
	
  var color1 = tr.style.backgroundColor;
  var color2 = "#d5d5d5";

  if (!color1.compareColor(color2)) {
	  if (estado == 1) {
	    tr.style.backgroundColor = "#ebebeb";
	  }
	  else {
	  	if (estado == 2) {
	  		var valores = document.getElementById(hiddenId).value;
	  		if (valores.indexOf("|" + id_nuevo + "|") >= 0) {
	  			tr.style.backgroundColor = anterior;
	  		}
	  		else {
	  			tr.style.backgroundColor = "#ffffff";
	  		}
	  	}
	  	else {
	    	tr.style.backgroundColor = anterior;
	    }
	  }
	}
}

function cambiarBgColorClick (tr, id_nuevo, hiddenId) {
  var color1 = tr.style.backgroundColor;
  var color2 = "#d5d5d5";

  if (!color1.compareColor(color2)) {
    if (! blnCtrl) {
    	limpiarTodos();
    	document.getElementById(hiddenId).value = "|" + id_nuevo + "|";
    }
  	else {
	    var valores = document.getElementById(hiddenId).value;
	    if (valores == "0") {
	     document.getElementById(hiddenId).value = "|" + id_nuevo + "|";
	    }
	  	else {
	  		document.getElementById(hiddenId).value = valores + "|" + id_nuevo + "|";
	  	}
	  }
    tr.style.backgroundColor = "#d5d5d5";
    //cambiarStatus("Status: item seleccionado", 3000);
  }
  else {
    if (! blnCtrl) {
    	limpiarTodos();
    	document.getElementById(hiddenId).value = 0;
    }
  	else {
  		tr.style.backgroundColor = "#ffffff";
		document.getElementById(hiddenId).value = replaceSubstring(document.getElementById(hiddenId).value, "|" + id_nuevo + "|", "");
  	}
    //cambiarStatus("Status: item liberado", 3000);
  }
}

function resetTodos (hiddenId) {
	document.getElementById(hiddenId).value = 0;
	limpiarCeldas("c");
}

function limpiarTodos () {
  limpiarCeldas("c");
}

function cambiarStatus (valor, tiempo) {
  //document.getElementById("status").innerHTML = valor;
  limpiarStatus(tiempo);
}

function limpiarStatus (tiempo) {
  //setTimeout("cambiarStatus('&nbsp;')", tiempo);
}

function limpiarCeldas (tipo) {
  var loop = true;
  for (i = 1; loop; i++) {
    tr = document.getElementById("tr" + tipo + i);
    if (tr) {
      //busco el color original
      colorX = "#ffffff";
      tr.style.backgroundColor = colorX;
    }
    else {
      loop = false;
    }
  }
}