
function TrimLeft(str) {
	var resultStr = "";
	var i = len = 0;

	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null)	
		return null;

	// Make sure the argument is a string
	str += "";

	if (str.length == 0) 
		resultStr = "";
	else {	
  		// Loop through string starting at the beginning as long as there
  		// are spaces.
//	  	len = str.length - 1;
		len = str.length;
		
  		while ((i <= len) && (str.charAt(i) == " "))
			i++;

   	// When the loop is done, we're sitting at the first non-space char,
 		// so return that char plus the remaining chars of the string.
  		resultStr = str.substring(i, len);
  	}

  	return resultStr;
}

function TrimRight(str) {
	var resultStr = "";
	var i = 0;

	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null)	
		return null;

	// Make sure the argument is a string
	str += "";
	
	if (str.length == 0) 
		resultStr = "";
	else {
  		// Loop through string starting at the end as long as there
  		// are spaces.
  		i = str.length - 1;
  		while ((i >= 0) && (str.charAt(i) == " "))
 			i--;
 			
 		// When the loop is done, we're sitting at the last non-space char,
 		// so return that char plus all previous chars of the string.
  		resultStr = str.substring(0, i + 1);
  	}
  	
  	return resultStr;  	
}

function Trim(str) {
	var resultStr = "";
	
	resultStr = TrimLeft(str);
	resultStr = TrimRight(resultStr);
	
	return resultStr;
}

function compruebaNum(num)
{
 if(num!="")
 {
  var hayLet = false;
  for (var i=0; i<num.length; i++)
    {
     var ch=num.substring(i, i+1);

     if ((ch<"0" || ch>"9"))
       {
       hayLet= true;
       }
     }

  if (hayLet)
   {
    return false;
   }
   else
   {
    return true;
   }
 }
}

function compruebaLargo(num,largo)
 {
 if(num.length!=largo)
  {
   return false;
  }
 else
  {
  return true;
  }
}

function compruebaMax(num,max)
 {
 if(num>max)
  {
   return false;
  }
 else
 {
  return true;
 }
}

function compruebaMinMax(num,min,max)
{
 if ((num<min || num > max) && (max != 0))
 {
  return false;
 }
 else
 {
  return true;
 }
}

function compruebaNumericos(campo,largo,min,max)
{
if(campo.value!="")
{
 var cifra=campo.value;
 if(!compruebaLargo(cifra,largo) || !compruebaNum(cifra) || !compruebaMinMax(cifra,min,max))
  {
  mensaErrorUnicos(idioma,2);
  campo.focus();
  return false;
  }
  else
  {
   return true;
  }
 }
}


function compruebaCifra(campo)
{
if(campo.value!="")
{
 var cifra=campo.value;
 if(! compruebaNum(cifra))
 {
  mensaErrorUnicos(idioma,1);
  campo.focus();
  return false;
 }
 else
 {
  return true;
 }
 }
}

function compruebaEspacio(campo)
{   
   campo.value = Trim(campo.value);
   if(campo.value == "")
   {
    return false;
   }
   else
   {
    return true;
   }}
   
function CompruebaAlguno(campos)//Comprueba si alguno de los campos indicados esta lleno
{
var nCampos = campos.length;
var cont=0;
  for(i=0;i<nCampos;i++){
   if(campos[i].value != "")
   {
   		cont++;
    }
   }

if (cont<1) 
   {
   mensaErrorUnicos(idioma,0);
   campos[0].focus();
   return false;
	}
   else
   {
    return true;
   }
}

function CompruebaTodos(campos)
//Comprueba que ninguno de los campos indicados 
//esta vacio y si lo esta pasa su correspondiente mensaje de error
{
var nCampos = campos.length;
var posicion=0;
  for(i=0;i<nCampos;i++){
   compruebaEspacio(campos[i]);
   if(campos[i].value != "")
   {
   		posicion++;
    }else{
		break;
    }
   }
if (posicion<nCampos) 
   {
   mensaErrorVarios(idioma,(posicion));
   campos[posicion].focus();
   return false;
	}
   else
   {
    return true;
   }
 }
 