//FUNCTION PROTOTYPES
String.prototype.trim=function(){  //trim leading or trailing whitespace and extra spaces
	return this.replace(/^\s*/, "").replace(/\s*$/, "").replace(/\s{2,}/, " ");
}

String.prototype.replaceAll=function(findstr,newstr){  //replace all occurences of string
	return this.replace(eval('/'+findstr+'/gi'),newstr);  //case insensitive
}

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

function isVisible(obj){  //verifica se o elemento está visivel
	if(!obj.parentNode){return false;}
	while(obj.parentNode!=null){
		if(obj.style.display.toLowerCase()=='none' || obj.style.visibility.toLowerCase()=='hidden'){return false;}
		obj=obj.parentNode;
	}
	return true;
}
/**
 * tetste
 * 
 * @param d
 * @param m
 * @param y
 * @return
 */
function checkdate(d,m,y)
{
	if(!IsNumeric(d) || !IsNumeric(m) || !IsNumeric(y) || d.length<2 || m.length<2 || y.length<4){return false;}
	var yl=1900; // least year to consider
	var ym=2500; // most year to consider
	if (m<1 || m>12) return(false);
	if (d<1 || d>31) return(false);
	if (y<yl || y>ym) return(false);
	if (m==4 || m==6 || m==9 || m==11)
	if (d==31) return(false);
	if (m==2)
	{
		var b=parseInt(y/4);
		if (isNaN(b)) return(false);
		if (d>29) return(false);
		if (d==29 && ((y/4)!=parseInt(y/4))) return(false);
	}
	return(true);
}

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 isHour(value){
	if(value==''){alert('Favor digite uma hora.');return false;}
	var splitted=value.split(':');
	var hora=splitted[0];
	var minuto=splitted[1];
	var segundo=splitted[2];
	if(!IsNumber(hora) || !IsNumber(minuto) || !IsNumber(segundo) || hora>24 || minuto>59 || segundo>59){return false;}
	return true;
}

function checaCampos(form){  //verifica o preenchimento obrigatorios dos campos antes de enviá-los
	if(!form){return false;}
	var element,enviar=true;
	for(var i=0;i<form.elements.length;i++){
		element=form.elements[i];
		if(element.className.indexOf('required')>=0){  //campo requerido
			if(!isVisible(element)){continue;}
			if(element.type.toUpperCase()=='CHECKBOX' || element.type.toUpperCase()=='RADIO'){  //se for um checkbox
				checkboxes=document.getElementsByName(element.name);
				for(var c=0;c<checkboxes.length;c++){
					if(checkboxes[c].checked){break;}
				}
				if(c<checkboxes.length){continue;}
				alert('Favor selecione pelo menos uma das opções apresentadas.');
				element.focus();
				return false;
			}
			if(element.value.length<=0){  //nao preenchido
				if(element.type.toUpperCase().indexOf('SELECT')>=0){
					alert('Favor selecione pelo menos uma opção.');
				}else{
					if(!isVisible(element)){continue;}
					alert('Favor preencha o campo corretamente');
				}
				element.focus();
				enviar=false;
				break;
			}
		}
		if(element.className.indexOf('email')>=0){  //campo tipo email
			if(!checkEmail(element.value.trim())){
				alert('Favor escreva um e-mail válido.');
				element.focus();
				enviar=false;
				break;
			}
		}
		if(element.className.indexOf('hour')>=0){  //campo tipo hora HH:mm:ss
			if(!isHour(element.value.trim())){
				alert('Favor digite uma hora válida.');
				element.focus();
				enviar=false;
				break;
			}
		}
		if(element.className.indexOf('number')>=0){  //campo tipo numero
			if(!IsNumeric(element.value.trim())){
				alert('Favor digite um valor numérico.');
				element.focus();
				enviar=false;
				break;
			}
		}
		if(element.className.indexOf('cnpj')>=0){  //campo tipo cnpj
			if(!valida_cnpj(element.value.trim())){
				alert('Favor digite um CNPJ válido.');
				element.focus();
				enviar=false;
				break;
			}
		}
		if(element.className.indexOf('cpf')>=0){  //campo tipo cpf
			if(!validarCPF(element.value.trim())){
				alert('Favor digite um CPF válido.');
				element.focus();
				enviar=false;
				break;
			}
		}
	}
	return enviar;
}

function valida_cnpj(cnpj){
  var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
  digitos_iguais = 1;
  if (cnpj.length < 14 && cnpj.length < 15)
		return false;
  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;
		return true;
		}
  else
		return false;
  } 

function numberConvert(value){
	value=(''+value).replaceAll(',','.');
	return parseFloat(value);
}

function get(id,tag,childOf,returnArray){  //getElementById
	if(!tag){tag="*";}
	if(typeof(childOf)=='string'){childOf=document.getElementById(childOf);}
	var anchs=document.getElementsByTagName(tag);
	var total_anchs=anchs.length;
	var regexp=new RegExp('\\b'+id+'\\b');
	var class_items=new Array();
	
	for(var i=0;i<total_anchs;i++){ //Go thru all the links seaching for the id name
		var this_item=anchs[i];
		if(regexp.test(this_item.id) || !id){
			if(childOf){
				pai=this_item.parentNode;
				if(!pai){continue;}
				while(pai!=null){
					if(pai==childOf){class_items.push(this_item);break;}
					pai=pai.parentNode;
				}
			}else{class_items.push(this_item);}
		}
	}
	if(returnArray){return class_items;}
	if(class_items.length<=0){return null;}
	else if(class_items.length==1){return class_items[0];}
	return class_items;
}


function validarCPF(cpf){
   if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
	  cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
	  cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
	  cpf == "88888888888" || cpf == "99999999999"){
	  return false;
   }

   soma = 0;
   for(i = 0; i < 9; i++)
   	 soma += parseInt(cpf.charAt(i)) * (10 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(9))){
	 window.alert("CPF inválido. Tente novamente.");
	 return false;
   }
   soma = 0;
   for(i = 0; i < 10; i ++)
	 soma += parseInt(cpf.charAt(i)) * (11 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(10))){return false;}
   return true;
}

//GOODCHARS
function getkey(e){
	if(window.event)return window.event.keyCode;
	else if(e)return e.which;
	else return null;
}

function goodchars(e,goods){
	var key,keychar;
	key=getkey(e);
	if(key==null)return true;
	
	//get character
	keychar=String.fromCharCode(key);
	keychar=keychar.toLowerCase();
	goods=goods.toLowerCase();
	
	//check goodkeys
	if(goods.indexOf(keychar)!=-1)return true;
	
	//control keys
	if(key==null||key==0||key==8||key==9||key==13||key==27)return true;
	return false;
}
