// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


/*
function max_len_textarea(e,elem,max)
{
    //cursor:37,38,39,40 apagar:46,8
    if( e.keyCode == 8 || e.keyCode == 46 || (e.keyCode >=37 && e.keyCode <= 40)){ //cursor e apagar
        return true;
    }
 
    if( elem.value.length >= max )
    {
        elem.value = elem.value.substring(0, max) 
        return false;
    }else{
        return true;
    }
}
*/

function max_len_textarea(field,maxlimit) {
    if (field.value.length > maxlimit){
        field.value = field.value.substring(0, maxlimit);
    }
}






function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }



function IsValidNIF(nif)
{
	//Check if is numeric and if has 9 numbers
	if (IsNumeric(nif) && nif.length == 9)
	{

		c = nif.charAt(0);
		
		//Check firt number is (1, 2, 5, 6, 8 or 9)
		if (c == '1' || c == '2' || c == '5' || c == '6' || c == '8' || c == '9')
		{
			checkDigit = parseInt(c.charCodeAt(0) * 9);
			
			for(i = 2; i <= 8; i++)
			{
				checkDigit += nif.charCodeAt(i - 1) * (10 - i);
			}
			checkDigit = 11 - (checkDigit % 11);
                         
			if (checkDigit >= 10) checkDigit = 0;
                         
			if (parseInt(checkDigit) == nif.charAt(8)) return true;
		}
	}
	return false;
}
