//Trim
String.prototype.trim = function()
{
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
	
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 valida_cpf(valor)
{

		var l_js_Contador;
		parNumero = valor;
		var l_js_Corpo = parNumero.substr(0, 9);
		var l_js_DV = parNumero.substr(9, 2);
		var d1 = 0;
		for (l_js_Contador = 0; l_js_Contador < 9; l_js_Contador++) {
			d1 += l_js_Corpo.charAt(l_js_Contador)*(10 - l_js_Contador);
		}
						
		if (d1 == 0) {
			return false;
		}
		d1 = 11 - (d1 % 11);
		if (d1 > 9) 
			d1 = 0;
		if (l_js_DV.charAt(0) != d1) {
			return false;
		}
	
		d1 *= 2;
		for (l_js_Contador = 0; l_js_Contador < 9; l_js_Contador++)	{
			d1 += l_js_Corpo.charAt(l_js_Contador)*(11 - l_js_Contador);
		}
		d1 = 11 - (d1 % 11);
		if (d1 > 9) 
			d1 = 0;
		if (l_js_DV.charAt(1) != d1) 
		{
			return false;
		}

		return true;
}

var phone_field_length=0;
function TabNext(obj,evento,len,next_field) {
			
			if (evento == "down") {
				phone_field_length=obj.value.length;
			}
			else if (evento == "up") {
				if (obj.value.length != phone_field_length) {
					phone_field_length=obj.value.length;
					if (phone_field_length == len) {
						next_field.focus();
					}
				}
			}
}
	
		
function validarForm(frm){
	
	var campo, i, j, caracter;
	
	for ( i = 0; i <= frm.elements.length; i++ ) {
		
		campo = frm.elements[i];
		campo.value = campo.value.trim();
		
		if (campo.getAttribute('validar') == 'sim'){
		
			if(campo.getAttribute('permitevazio') == 'nao'){
					 
				if (!campo.value) {
					
					alert('Por favor, preencha o campo ' + campo.title + '.');
					if (campo.getAttribute('divpai')){
						exibeForm(campo.getAttribute('divpai'));
					}
					campo.focus();
					return false;
					
				}
				
			}
			
			if (campo.value.length > 0){
			
				if ( !(campo.value.length >= parseInt(campo.getAttribute('minlength'))) ){
						
					alert('Por favor, preencha o campo ' + campo.title + ' com pelo menos ' + campo.getAttribute('minlength') + ' caracteres/digitos.');
					if (campo.getAttribute('divpai')){
						exibeForm(campo.getAttribute('divpai'));
					}
					campo.focus();
					return false;
				
				}
						
				if (campo.getAttribute('tipodedados') == 'numero'){
							
					if (!IsNumeric(campo.value)){
								
						alert('Por favor, preencha o campo ' + campo.title + ' apenas com numeros.');
						if (campo.getAttribute('divpai')){
							exibeForm(campo.getAttribute('divpai'));
						}
						campo.focus();
						return false;
								
					}
						
				}
					
				if (campo.getAttribute('deveconter') != ''){
				
					caracter = campo.getAttribute('deveconter').split('|');
				
					for (j = 0; j < caracter.length; j++){
					
						if (campo.value.indexOf(caracter[j]) == -1){
						
							alert('Por favor, preencha o campo ' + campo.title + ' corretamente.');
							if (campo.getAttribute('divpai')){
								exibeForm(campo.getAttribute('divpai'));
							}
							campo.focus();
							return false;
						
						}
					}
				
				} //if (campo.getAttribute('deveconter') != ''){ 
				
			} //if (campo.value.length > 0){

		} //if (campo.getAttribute('validar') == 'sim'){
		
	} //for ( i = 0; i <= frm.elements.length; i++ ) {
		
	return true;
  }
