trim = function(str) {
	return str.replace(/^\s+|\s+$/g,"");
}

trimStr = function(str) {
	return str.replace(/^\s+|\s+$/g,"");
}

verificarCheckbox = function(classe) {
	flg = false;
	if(classe == null)
		classe = '.checkboxVerificado';
	else
		classe = '.' + classe;
	$$(classe).forEach(function(chk) {
			if(chk.checked)
				flg = true;
		});
	return flg;
}

formataData = function(x, evento) {
           
    separador="/";
    evento = (window.event || evento); 
    tecla = (evento.which || evento.keyCode);    
    valor=x.value.split('');
    formatado="";    
    i=0;
    
    while(i<valor.length){
        caractere=valor[i];
        numeros=/^\d+$/;
        if(numeros.test(caractere) || caractere==separador){ formatado+=String(caractere);}
        if((formatado.length==2 || formatado.length==5) && tecla!=8){formatado+=separador; i++;}
        i++;
    }
    
    x.value=formatado;
}

checkEmail = function(texto) {
	return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(texto));
}

validarTelefone = function(texto) {
	return (/\([0-9]{2,2}\)(\ )[0-9]{4,4}\-[0-9]{4,4}/.test(texto));
}

formatarTelefone = function(obj) {
	var txt = obj.value;
	txt = txt.replace(/\D/g, "");

	var l = txt.length;
	var flg = false;

	if(l != 10)
		return false;

	txt = "(" + txt.substring(0, 2) + ") " + txt.substring(2, 6) + "-" + txt.substring(6, 10);
	flg = validarTelefone(txt);
	if(flg)
		obj.value = txt;

	return flg;
}

validarCep = function(texto) {
	return (/[0-9]{2,2}\.[0-9]{3,3}-[0-9]{3,3}/.test(texto));
}

formatarCep = function(obj) {
	var txt = obj.value;
	txt = txt.replace(/\D/g, "");

	var l = txt.length;
	var flg = false;

	if(l != 8)
		return false;

	txt = txt.substring(0, 2) + "." + txt.substring(2, 5) + "-" + txt.substring(5, 8);
	flg = validarCep(txt);
	if(flg)
		obj.value = txt;

	return flg;
}

/**
* DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
* Adaptado pela Auge de mm/dd/aaaa para dd/mm/aaaa e retidos os 'alert'
*/
//Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

isInteger = function(s){
	var i;
   for (i = 0; i < s.length; i++){   
       // Check that current character is number.
       var c = s.charAt(i);
       if (((c < "0") || (c > "9"))) return false;
   }
   // All characters are numbers.
   return true;
}

stripCharsInBag = function(s, bag){
	var i;
   var returnString = "";
   // Search through string's characters one by one.
   // If character is not in bag, append to returnString.
   for (i = 0; i < s.length; i++){   
       var c = s.charAt(i);
       if (bag.indexOf(c) == -1) 
       	returnString += c;
   }
   return returnString;
}

daysInFebruary = function(year){
	// February has 29 days in any year evenly divisible by four,
   // EXCEPT for centurial years which are not also divisible by 400.
   return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

DaysArray = function(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30;}
		if (i==2) {this[i] = 29;}
  } 
  return this;
}

isDate = function(dtStr) {
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strMonth=dtStr.substring(0,pos1);
	var strDay=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	day=parseInt(strMonth);
	month=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : dd/mm/yyyy")
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false;
	}
	
	return true;
}

a2i = function(txt) {
	return parseInt(txt.replace(/^0*/g, ""));
}

fixDateField = function(obj) {
	var txt = obj.value;
	var dia, mes, ano;
	var mydate = new Date();
	var l = txt.length;
	var flg = true;

	if(isInteger(txt)) {
		switch(l) {
		case 1:
		case 2:
			dia = a2i(txt);
			mes = mydate.getMonth() + 1;
			ano = mydate.getFullYear();
			break;
		case 3:
		case 4:
			dia = a2i(txt.substring(0, 2));
			mes = a2i(txt.substring(2, l));
			ano = mydate.getFullYear();
			break;
		default:
			dia = a2i(txt.substring(0, 2));
			mes = a2i(txt.substring(2, 4));
			ano = a2i(txt.substring(4, l));
			if(l < 8)
				ano += 2000;
		}
	}
	else {
		if(!/[0-9\/.-]/.test(txt))
			return false;

		v = txt.split(/[\/.-]/);
		switch(v.length) {
		case 2:
			dia = a2i(v[0]);
			if(v[1].length == 0)
				mes = mydate.getMonth() + 1;
			else
				mes = a2i(v[1]);
			ano = mydate.getFullYear();
			break;
		case 3:
			dia = a2i(v[0]);
			if(v[1].length == 0)
				mes = mydate.getMonth() + 1;
			else
				mes = a2i(v[1]);
			if(v[2].length == 0) {
				ano = mydate.getFullYear();
			}
			else {
				ano = a2i(v[2]);
				if(v[2].length < 4)
					ano += 2000;
			}
			break;
		default:
			return false;
		}
	}

	if(dia < 10)
		dia = "0" + dia;
	if(mes < 10)
		mes = "0" + mes;
	txt = (dia.toString()) + "/" + (mes.toString()) + "/" + (ano.toString());

	flg = isDate(txt);

	if(flg)
		obj.value = txt;

	return flg;
}

fixTimeField = function(obj) {
	var txt = obj.value;
	var hora, minuto;
	var l = txt.length;
	var flg = true;
	
	if(txt == '')
		return true;

	if(isInteger(txt)) {
		switch(l) {
		case 5:
			return false;
			break;
		case 1:
		case 2:
			if(txt == "0" || txt == "00") {
				hora = 0;
			}
			else {
				hora = a2i(txt);
			}
			minuto = 0;
			break;
		default:
			if(txt.substring(0, 2) == "00") {
				hora = 0;
			}
			else {
				hora = a2i(txt.substring(0, 2));
			}
			t = txt.substring(2, l);
			if(t == "00")
				minuto = 0;
			else
				minuto = a2i(t);
		}
	}
	else {
		v = txt.split("\:");
		if(v.length == 2) {
			if(v[0].length == 0)
				hora = 0;
			else
				if(v[0] == "0" | v[0] == "00")
					hora = 0;
				else
					hora = a2i(v[0]);
			if(v[1].length == 0)
				minuto = 0;
			else
				if(v[1] == "00")
					minuto = 0;
				else
					minuto = a2i(v[1]);
		}
		else {
			return false;
		}
	}
	
	if(isNaN(hora) || isNaN(minuto))
		return false;
	
	if(hora < 0 || hora > 23 || minuto < 0 || minuto > 59)
		return false;
	
	if(hora < 10)
		hora = "0" + hora;
	if(minuto < 10)
		minuto = "0" + minuto;
	txt = (hora.toString()) + ":" + (minuto.toString());

	if(flg)
		obj.value = txt;

	return flg;
}

formatarCPF = function(obj){
    cpf = obj.value.replace(/[\.-]/g, "");

    if (cpf.length != 11 || isNaN(cpf)) {
  		return false;
 	}
 	var i;
 	var c = cpf.substr(0,9);
 	var dv = cpf.substr(9,2);
 	var d1 = 0;

    for (i = 0; i < 9; i++) {
  		d1 += c.charAt(i)*(10-i);
 	}

    if (d1 == 0){
  		return false;
 	}

	d1 = 11 - (d1 % 11);

    if (d1 > 9) d1 = 0;
 	if (dv.charAt(0) != d1) {
  		return false;
 	}

    d1 *= 2;
 	for (i = 0; i < 9; i++) {
  		d1 += c.charAt(i)*(11-i);
 	}
 	d1 = 11 - (d1 % 11);

	if (d1 > 9) d1 = 0;
 	if (dv.charAt(1) != d1) {
		return false;
    }

    obj.value = cpf.substring(0, 3) + "." + cpf.substring(3, 6) + "." + cpf.substring(6, 9) + "-" + cpf.substring(9, 11);

    return true;
}

formatarCNPJ = function(obj){
	
	var numeros;
	var digitos;
	var soma;
	var i;
	var resultado;
	var pos; 
	var tamanho;
	var digitos_iguais;
	var cnpj;
	
	cnpj = obj.value.replace(/[\.-]/g, "");
	cnpj = cnpj.replace("/", "");

	if (cnpj.length == 0) {
	      return false;
	}
	
	digitos_iguais = 1;
	
	for (i = 0; i < cnpj.length - 1; i++){
	      if (cnpj.charAt(i) != cnpj.charAt(i + 1)){
	            digitos_iguais = 0;
	            break;
	      }
	}
	
	if (!digitos_iguais){
	      tamanho = cnpj.length - 2;
	      numeros = cnpj.substring(0,tamanho);
	      digitos = cnpj.substring(tamanho);
	      soma = 0;
	      pos = tamanho - 7;
	      for (i = tamanho; i >= 1; i--){
	            soma += numeros.charAt(tamanho - i) * pos--;
	            if (pos < 2){
	                  pos = 9;
	            }
	      }
	      
	      resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
	      
	      if (resultado != digitos.charAt(0)){
	            return false;
	      }
	      tamanho = tamanho + 1;
	      numeros = cnpj.substring(0,tamanho);
	      soma = 0;
	      pos = tamanho - 7;
	      for (i = tamanho; i >= 1; i--){
	            soma += numeros.charAt(tamanho - i) * pos--;
	            if (pos < 2){
	                  pos = 9;
	            }
	      }
	
	      resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
	      if (resultado != digitos.charAt(1)){
	            return false;
	      }
	      
	      obj.value = cnpj.toString().substring(0, 2) +"."+ cnpj.toString().substring(2, 5) +"."+ cnpj.toString().substring(5, 8) +"/"+ cnpj.toString().substring(8, 12)+"-"+ cnpj.toString().substring(12, 14);

	      return true;
	}
	else
	      return false;
}

