// JavaScript Document
function checkName(nome){
	if (nome.value != ''){
	nome_u = nome.value.search(new RegExp("^[a-záéíóúçãõüêîôûÁÉÍÓÚÃÔÊÔÛA-Z0-9]+([ ]+[a-záéíóúçãõüêîôûÁÉÍÓÚÃÔÊÔÛA-Z0-9]+)+"));
		if (nome_u < 0){
		alert("Nome inválido. Altere usando no mínimo um nome e um sobrenome.");
		nome_valido = false;
		nome.focus();
		} else {
		nome_valido = true;
		}
	}
}

function checkMail(q){
	parte1 = q.value.indexOf("@");
    parte2 = q.value.indexOf(".");
    parte3 = q.value.length;
    if (!(parte1 >= 3 && parte2 >= 6 && parte3 >= 9)) {
          alert ("O campo " + q.name + " deve ser conter um endereco eletronico!");
           q.focus();
    return false;
    }
	else{
		return true;
	}
}	

function SomenteNumero(e){
    var tecla=(window.event)?e.keyCode:e.which;
    if((tecla > 47 && tecla < 58)) 
		return true;
    else
	{
    	if ((tecla == 8 || tecla == 46 || tecla == 13 || tecla == 127)) 
			return true;
    	else 
			return false;
    }
}

function MM_openBrWindow(theURL,winName,features) { //v2.0

  window.open(theURL,winName,features);

}

function checaCPF (CPF) {
	if(CPF.length!=0){
		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)))
			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;
 	}else {return true;}
}
 
function validaCNPJ(cnpj) { 
var nonNumbers = /\D/;

if(cnpj.length!=0){

	if (cnpj.length!=14){
		alert("CNPJ inválido, o campo deve possuir 14 dígitos");
		return false;
	} 
	else if (nonNumbers.test(cnpj)){
			alert("O campo deve ser preenchido somente por números");
			return false;
		 }
		else if (cnpj=="00000000000000" || cnpj=="11111111111111" || cnpj=="22222222222222" || cnpj=="33333333333333" || cnpj=="44444444444444" || cnpj=="55555555555555" || cnpj=="66666666666666" || cnpj=="77777777777777" || cnpj=="88888888888888" || cnpj=="99999999999999"){
			alert("Numéro de cnpj inválido!");
			return false;
		  }
	else{
		var a1=eval(cnpj.slice(0,1));
		var a2=eval(cnpj.slice(1,2));
		var a3=eval(cnpj.slice(2,3));
		var a4=eval(cnpj.slice(3,4));
		var a5=eval(cnpj.slice(4,5));
		var a6=eval(cnpj.slice(5,6));
		var a7=eval(cnpj.slice(6,7));
		var a8=eval(cnpj.slice(7,8));
		var a9=eval(cnpj.slice(8,9));
		var a10=eval(cnpj.slice(9,10));
		var a11=eval(cnpj.slice(10,11));
		var a12=eval(cnpj.slice(11,12));
		var a13=eval(cnpj.slice(12,13));
		var a14=eval(cnpj.slice(13,14));
		soma= a1*5+ a2*4+ a3*3+ a4*2 + a5*9+ a6*8+ a7*7+ a8*6+ a9*5+ a10*4+ a11*3+ a12*2;
		resto=soma%11
		if (resto<2){
			result1=0
		}
		else{
			result1=11-resto
		}
		soma=a1*6+ a2*5+ a3*4+ a4*3+ a5*2+ a6*9+ a7*8+ a8*7+ a9*6+ a10*5+ a11*4+ a12*3+ a13*2;
		resto1=soma%11
		if (resto1<2){
			result2=0
		}
		else{
			result2=11-resto1
		}
		if (result1==a13 && result2==a14){
			return true;
		}
		else{
			alert("Numéro cnpj inválido!");
			return false;
		}
	}
}else{return true;}
	
}

function getPosicaoElemento(elemID){
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop};
}

function ietruebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function ddrivetip(thetext, thewidth, thecolor){
if (ns6||ie){
if (typeof thewidth!="undefined") tipobj.style.width=thewidth+"px"
if (typeof thecolor!="undefined" && thecolor!="") tipobj.style.backgroundColor=thecolor
tipobj.innerHTML=thetext
enabletip=true
return false
}
}

function positiontip(e){
if (enabletip){
var nondefaultpos=false
var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
//Find out how close the mouse is to the corner of the window
var winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20
var winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight-20

var rightedge=ie&&!window.opera? winwidth-event.clientX-offsetfromcursorX : winwidth-e.clientX-offsetfromcursorX
var bottomedge=ie&&!window.opera? winheight-event.clientY-offsetfromcursorY : winheight-e.clientY-offsetfromcursorY

var leftedge=(offsetfromcursorX<0)? offsetfromcursorX*(-1) : -1000

//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<tipobj.offsetWidth){
//move the horizontal position of the menu to the left by it's width
tipobj.style.left=curX-tipobj.offsetWidth+"px"
nondefaultpos=true
}
else if (curX<leftedge)
tipobj.style.left="5px"
else{
//position the horizontal position of the menu where the mouse is positioned
tipobj.style.left=curX+offsetfromcursorX-offsetdivfrompointerX+"px"
pointerobj.style.left=curX+offsetfromcursorX+"px"
}

//same concept with the vertical position
if (bottomedge<tipobj.offsetHeight){
tipobj.style.top=curY-tipobj.offsetHeight-offsetfromcursorY+"px"
nondefaultpos=true
}
else{
tipobj.style.top=curY+offsetfromcursorY+offsetdivfrompointerY+"px"
pointerobj.style.top=curY+offsetfromcursorY+"px"
}
tipobj.style.visibility="visible"
if (!nondefaultpos)
pointerobj.style.visibility="visible"
else
pointerobj.style.visibility="hidden"
}
}

function hideddrivetip(){
if (ns6||ie){
enabletip=false
tipobj.style.visibility="hidden"
pointerobj.style.visibility="hidden"
tipobj.style.left="-1000px"
tipobj.style.backgroundColor=''
tipobj.style.width=''
}
}

function mascaraData(campoData){
	var data = campoData.value;
	if (data.length == 2){
		data = data + '/';
		campoData.value = data;
		return true;
	}
	if (data.length == 5){
		data = data + '/';
		campoData.value = data;
		return true;
	}         
}

function mascara(o,f){
    v_obj=o
    v_fun=f
    setTimeout("execmascara()",1)
}

function execmascara(){
    v_obj.value=v_fun(v_obj.value)
}

function cep(v){
    v=v.replace(/\D/g,"")                //Remove tudo o que não é dígito
    v=v.replace(/^(\d{5})(\d)/,"$1-$2") //Esse é tão fácil que não merece explicações
    return v
}

function telefone(v){
    v=v.replace(/\D/g,"")                 //Remove tudo o que não é dígito
    v=v.replace(/^(\d\d)(\d)/g,"($1) $2") //Coloca parênteses em volta dos dois primeiros dígitos
    v=v.replace(/(\d{4})(\d)/,"$1-$2")    //Coloca hífen entre o quarto e o quinto dígitos
    return v
}
function data(v){
    v=v.replace(/\D/g,"")                //Remove tudo o que não é dígito
    v=v.replace(/^(\d{2})(\d)/,"$1/$2") //Esse é tão fácil que não merece explicações
	v=v.replace(/(\d{2})(\d)/,"$1/$2")
    return v
}

function doDate(pStr)
{
	var reDate = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/\d{4}$/;
	if ((!(reDate.test(pStr.value)))&&(pStr.value != null && pStr.value != "")) {
		alert(pStr.value + " NÃO é uma data válida.");
		pStr.value="";
		pStr.focus();
	}
} // doDate

function MostraQuantidade(obj,div)
{ 
  var txt   = new String(obj.value);
  var quant = (100 - txt.length);
  if (quant == 0)
  {
    window.event.keyCode = 0;
  }
  div.innerText = "Restam:" + quant;
}

function abrir(URL,largura,altura) {

  var width = largura;
  var height = altura;

  var left = 5;
  var top = 5;

  window.open(URL,'a', 'width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=yes, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');

}

function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
		obj.value=obj.value.substring(0,mlength)
}

