//Funciones para descartar caracteres no numéricos.
//NO Permite decimales
function chars(tb,lang)
{
var validado = false;

re=/([^0-9])/
if(re.exec(tb.value))
{
		switch (lang)
			{
			case "es":
				alert("Carácter no válido, introduzca números"); //("+ RegExp.$1 + ") en " + tb.name
				tb.focus();// he quitado esto d antes para q el javascript no muestre el nombre del campo						
				break;
			case "en":
				alert("Character non valid, introduces numbers"); 
				tb.focus();
				break;
			case "cat":
				alert("Caràcter no vàlit,introduixca nombres"); 
				tb.focus();
				break;
			}
}				
   else {
      validado = true;
   }
return validado;
}

function checkEveryTextBox()
{
for(f in document.forms)
{
for(ele in document.forms[f])
{
if(ele.type=="text")
{
chars(ele)
}
}
}
}

//Funciones para descartar caracteres no numéricos.
//Permite decimales con el signo "."
function decim(tb,lang)
{
var validado = false;

re=/([^.,0-9])/
if(re.exec(tb.value))
{
	switch (lang)
			{
			case "es":
				alert("Carácter no válido, introduzca números (y si desea decimales)"); //("+ RegExp.$1 + ") en " + tb.name
				tb.focus();// he quitado esto d antes para q el javascript no muestre el nombre del campo						
				break;
			case "en":
				alert("Character non valid, introduces numbers (and if wishes decimals)"); 
				tb.focus();
				break;
			case "cat":
				alert("Caràcter no vàlit,introduixca nombres (i si vol decimals)"); 
				tb.focus();
				break;
			}
}
   else {
      validado = true;
   }
return validado;
}

function checkEveryTextBox()
{
for(f in document.forms)
{
for(ele in document.forms[f])
{
if(ele.type=="text")
{
decim(ele)
}
}
}
}

//Funciones para descartar caracteres no alfanuméricos.
//Permite los siguientes caracteres "-", "_" y mayúsculas y minúsculas ambos con acentos
function alfanum(tb)
{
var validado = false;

re=/([^0-9a-zA-Z_-áàéèíóòúÁÀÉÈÍÓÒÚñÑ ])/
if(re.exec(tb.value))
{
alert("Carácter no válido ("+ RegExp.$1 + ") en " + tb.name);
tb.focus();
}
   else {
      validado = true;
   }
return validado;
}

function checkEveryTextBox()
{
for(f in document.forms)
{
for(ele in document.forms[f])
{
if(ele.type=="text")
{
alfanum(ele)
}
}
}
}

//Funciones para chequear la longitud del campo descripción e impedir que un formulario se envíe
function validar(ta) {
	 defaultStatus	= '';
     maxlength=1000;
     if(document.formpromo.Descripcion.value.length > maxlength) {
          alert('La descripción no puede sobrepasar los ' + maxlength + ' caracteres. \n\t Elimine ' + (document.formpromo.Descripcion.value.length - maxlength) + ' caracteres o más');
          document.formpromo.Descripcion.focus();
          return false;
     } else if(document.formpromo.Memoria.value.length > maxlength) {
          alert('La memoria no puede sobrepasar los ' + maxlength + ' caracteres. \n\t Elimine ' + (document.formpromo.Memoria.value.length - maxlength) + ' caracteres o más');
          document.formpromo.Memoria.focus();
          return false;
     } else {
          return true;
     }
}

//Modifica valores de HTML detectándolos por el estilo.
function getObject(obj) {
  var theObj;
  if(document.all) {
    if(typeof obj=="string") {
      return document.all(obj);
    } else {
      return obj.style;
    }
  }
  if(document.getElementById) {
    if(typeof obj=="string") {
      return document.getElementById(obj);
    } else {
      return obj.style;
    }
  }
  return null;
}

//Contador de caracteres.
function Contar(salida,texto,t,medida,lang) {

	var displayStart, displayCounting, rtn=true;
	var salidaObj=getObject(salida);
	
	if (!t.maxlength) {
		t.maxlength = medida; 	//Por defecto para un textarea.
	}
	
	if (t.maxlength < t.value.length) {
		t.style.color='red';
		switch (lang)
			{
			case "es":
				displayCounting = ' - Elimine ' + (t.value.length - t.maxlength) + ' caracteres.'
				break;
			case "en":
				displayCounting = ' - Eliminate ' + (t.value.length - t.maxlength) + ' characters.'
				break;
			case "cat":
				displayCounting = ' - Elimini ' + (t.value.length - t.maxlength) + ' caràcters.'
				break;
			}		
    	texto='<span class="disable"> '+texto+' </span>';
		t.value=t.value.substr(0,t.maxlength);
		rtn = false;
	}	
	else {
		t.style.color='';
		switch (lang)
			{
			case "es":
				displayCounting = ' - Puede añadir hasta ' + (t.maxlength - t.value.length) + ' carácteres.'
				break;
			case "en":
				displayCounting = ' - You can add ' + (t.maxlength - t.value.length) + ' characters.'
				break;
			case "cat":
				displayCounting = ' - Pot afegir fins a  ' + (t.maxlength - t.value.length) + ' caràcters.'
				break;
			}		
	}
	
	displayStart 	= 'Campo: ' + t.name + '. Longitud: ' + t.value.length + '.  Max:' + t.maxlength ;
	t.title			= displayStart + displayCounting;
	defaultStatus	= displayStart + displayCounting;
	
	salidaObj.innerHTML = texto.replace("{CHAR}",t.maxlength - t.value.length);
	return rtn;
}


