function verificaEmail(address) {
   var valido = 0;
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(address) == false) {
      valido += 1;
   }
   return valido;
}


function HD(val,c1,c2) {
	var a = document.getElementById(c2);
	if (val == 0) {
		a.style.backgroundColor = '#CCCCCC';
		a.value = '';
		a.disabled = true;
	}
	else if (val == 1) {
		a.disabled = false;
		a.style.backgroundColor = '#FFFFFF';
		a.focus();
	}
	
}
function HD2(val,c) {
	var a = c.split(",")
	var b = '';
	for (var i=0;i<a.length;i++){
		b = document.getElementById(a[i]);
		if (b) {
			if (val == 0) {
				b.style.backgroundColor = '#CCCCCC';
				if (b.type != 'select-one')	b.value = '';
				b.disabled = true;
			}
			else if (val == 1) {
				b.disabled = false;
				b.style.backgroundColor = '#FFFFFF';
				if (i == 0)	b.focus();
			}
		}
	}
}
function ExisteData(val,p) {
	var url = "/usuario/comum/DataExiste.asp?Data="+val+'&p='+p;
	var xmlRequest = ajax.create_http_request();
	ajax.get_xml_and_wait(xmlRequest,url);
	if(xmlRequest.readyState == 4){
		var xml = ajax.load_xml(xmlRequest.responseText);
		var obj2 = ajax_object_from_xml(xml,"verificacao")
		return obj2;
	}
}
function VerificaCPF(val) {
	var url = "/usuario/comum/cpf.asp?val="+val;
	var xmlRequest = ajax.create_http_request();
	ajax.get_xml_and_wait(xmlRequest,url);
	if(xmlRequest.readyState == 4){
		var xml = ajax.load_xml(xmlRequest.responseText);
		var obj2 = ajax_object_from_xml(xml,"verificacao")
		return obj2;
	}
}

function VerificaGenerico() {
	var formInputs = document.getElementsByTagName('input'); // Faz as verificações para os inputs
    var a = '';
	for (var i = 0; i < formInputs.length; i++) {
        var theInput = formInputs[i];
        if (theInput.type == 'text' && theInput.req == '1') {
			if (theInput.value == '') {
				alert('Por favor preencha os campos obrigatórios!')
				theInput.focus();
				return false;
			}
		}
	}
	var formTextArea = document.getElementsByTagName('textarea');
    for (var i = 0; i < formTextArea.length; i++) {
        var theTextArea = formTextArea[i];
		if (theTextArea.req == '1') {
			if (theTextArea.value.length < 5) {
				alert('Por favor selecione os campos obrigatórios!!')
				theTextArea.focus();
				return false;
			}
		}
	}
	var formSelects = document.getElementsByTagName('select'); //faz as verificacoes para os selects!
    for (var i = 0; i < formSelects.length; i++) {
        var theSelect = formSelects[i];
		if (theSelect.req == '1') {
			if (theSelect.value == '-1' || theSelect.value == '') {
				alert('Por favor selecione os campos obrigatórios!!')
				theSelect.focus();
				return false;
			}
		}
	}
	return true;
}





function MudaOrdemSQL(txt1,txt2,obj,form,url) {
	if (txt1 == obj.value) {
		obj.value = txt2;
	}
	else {
		obj.value = txt1;
	}
	var a = document.getElementById(form);
	submitForm('divConteudo',url,form);
}

function SaiSistema() {
	if (parent.location.href) {
		parent.location.href = '/default.asp';
	}
	else { document.location.href = '/default.asp'; }
}
function SaiSistemaAdmin() {
	if (parent.location.href) {
		parent.location.href = '/admin/default.asp';
	}
	else { document.location.href = '/admin/default.asp'; }
}


function replaceAll( str, from, to ) {
	var idx = str.indexOf( from );
	while ( idx > -1 ) {
		str = str.replace( from, to );
		idx = str.indexOf( from );
	}
	//str = str.replace(',','.')
	//str = Number(str);
	//str = Number(str.toFixed(2));
	return str;
}


function DesabilitaBotao(obj) {
	var botao = document.getElementById(obj);
	if (botao) {
		botao.disabled = true;
	}
}

function HabilitaBotao(obj) {
	var botao = document.getElementById(obj);
	if (botao) {
		botao.disabled = false;
	}
}

function addCommas(nStr)
	{
		nStr += '';
		x = nStr.split(',');
		x1 = x[0];
		x2 = x.length > 1 ? ',' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + '.' + '$2');
		}
		return x1 + x2;
	}

	function CalculaValor(txtQtd,txtValor,Valor) {
		txtValor.value = addCommas(Number(Valor.replace(",",".") * txtQtd.value).toFixed(2).replace(".",","));
	}

function MudaHeight(obj) {
	var windowheight = window.screen.height; 
	var windowwidth = window.screen.width;
	var divConteudo = document.getElementById(obj);
	
	if (windowheight == 600) {
		divConteudo.style.height =  207+'px';	
	}
	else if (windowheight == 768) {
		divConteudo.style.height = 375+'px';	
	}
	else if (windowheight == 800) {
		divConteudo.style.height =  305+'px';	
	}
	else if (windowheight == 900) {
		divConteudo.style.height =  507+'px';	
	}
	else if (windowheight == 1024) {
		divConteudo.style.height =  631+'px';	
	}
	else if (windowheight == 1200) {
		divConteudo.style.height =  600+'px';	
	}
	else {
		divConteudo.style.height =  375+'px';	
	}

	if (windowwidth == 1920) {
		divConteudo.style.width = 900+'px';
	}
	else if(windowwidth == 800) {
		divConteudo.style.width = 617+'px';
	}
	else if(windowwidth == 1024) {
		divConteudo.style.width = 841+'px';
	}
	else if(windowwidth == 1280) {
		divConteudo.style.width = 741+'px';
	}
	
}

function Carac(tipo,obj,evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode
	//alert(charCode);
	if (tipo == 'rg') {
		if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 45 && charCode != 120) {
			return false;
		}
	}
	if (tipo == 'num') {
		if (charCode > 31 && (charCode < 48 || charCode > 57)) {
		return false;
		}
	}
	if (tipo == 'valor') {
		if (charCode == 44) {
			posicao = obj.value.indexOf(',');
			if (posicao != '-1') {
				return false;
			}
		}
		if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode !=44) {
				return false;
		}
	}

}

function setaFoco(obj) {
	if (document.getElementById(obj)) {
		document.getElementById(obj).focus();
		document.getElementById(obj).focus();
		document.getElementById(obj).focus();
		if (document.getElementById(obj).type == 'text') {
			document.getElementById(obj).select();
		}
	}
}
function selecionaTexto(obj) {
	obj.select();
}

function Mascara (formato, keypress, objeto){
	if (event.keyCode == 13) {
		return true;	
	}
	if (! (event.keyCode >= 48 && event.keyCode <= 57) )  {
  		return false
	}
	else {
		campo = eval(objeto);
		// cep
		if (formato=='cep'){
			separador = '-';
			conjunto1 = 5;
			if (campo.value.length == conjunto1){
				campo.value = campo.value + separador;
			}
		}
		// telefone
		if (formato=='telefone'){
			separador1 = '(';
			separador2 = ') ';
			separador3 = '-';
			conjunto1 = 0;
			conjunto2 = 3;
			conjunto3 = 9;
			if (campo.value.length == conjunto1){
				campo.value = campo.value + separador1;
			}
			if (campo.value.length == conjunto2){
				campo.value = campo.value + separador2;
			}
			if (campo.value.length == conjunto3){
				campo.value = campo.value + separador3;
			}
		}
		if (formato=='cnpj') {
			separador = '.';
			conjunto1 = 3
			if (campo.value.length == conjunto1) {
				campo.value = campo.value + separador;
			}
			if (campo.value.length == conjunto1+conjunto1+1) {
				campo.value = campo.value + separador;
			}
			if (campo.value.length == conjunto1+conjunto1+conjunto1+2) {
				campo.value = campo.value + '-';
			}
		}
		if (formato=='data') {
			separador = '/';
			conjunto1 = 2
			if (campo.value.length == conjunto1) {
				campo.value = campo.value + separador;
			}
			if (campo.value.length == conjunto1+conjunto1+1) {
				campo.value = campo.value + separador;
			}
		}
	}
}





var dragapproved=false
var minrestore=0
var initialwidth,initialheight
var ie5=document.all&&document.getElementById
var ns6=document.getElementById&&!document.all

function iecompattest(){
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function drag_drop(e){
	//div = 'divAR';
	var div = document.getElementById('divMovable').value;
	if (ie5&&dragapproved&&event.button==1){
		document.getElementById(div).style.left=tempx+event.clientX-offsetx+"px"
		document.getElementById(div).style.top=tempy+event.clientY-offsety+"px"
	}
	else if (ns6&&dragapproved){
		document.getElementById(div).style.left=tempx+e.clientX-offsetx+"px"
		document.getElementById(div).style.top=tempy+e.clientY-offsety+"px"
	}
}

function initializedrag(div,e){
	offsetx=ie5? event.clientX : e.clientX
	offsety=ie5? event.clientY : e.clientY
	//document.getElementById(div+"1").style.display="none" //extra
	tempx=parseInt(document.getElementById(div).style.left)
	tempy=parseInt(document.getElementById(div).style.top)
	dragapproved=true
	document.getElementById(div).onmousemove = drag_drop
}

function loadwindow(div,url){
	document.getElementById(div).style.display=''
	document.getElementById(div).style.visibility = 'visible';
	//document.getElementById(div).style.width=initialwidth=width+"px"
	//document.getElementById(div).style.height=initialheight=height+"px"
	document.getElementById(div).style.left="200px"
	document.getElementById(div).style.top=ns6? window.pageYOffset*1+200+"px" : iecompattest().scrollTop*1+200+"px"
	//document.getElementById(div+"1").innerHTML=url
	CarregaPagina(div+"1",url);
}
function closeit(div){
	var divDet = document.getElementById(div);
	if (divDet) {
		divDet.innerHTML = '';
		divDet.style.display = 'none';
	}
	//document.getElementById(div).style.display="none"
}

function stopdrag(div){
	dragapproved=false;
	document.getElementById(div).onmousemove=null;
}

