// JavaScript Document

//MENU PRINCIPAL - HIDE / SHOW
function menuBody(obj){
	if (obj.style.display == "none"){
		obj.style.display = "";
		document.getElementById("menuImg").src = "../../serv/images/btnMenuHide.gif";
		
	}
	else {
		obj.style.display = "none";
		document.getElementById("menuImg").src = "../../serv/images/btnMenuShow.gif";
	}
}

//SUB MENU
function subMenuBody(obj){
	if (obj.style.display == "none"){
		obj.style.display = "";
	}
	else {
		obj.style.display = "none";
	}
}

//LAYER
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

//
function validaForm(){
	frm = document.Form1;

	
	frm.submit();
}


MyWindow=null; //esta variável armazena o nome da última janela aberta por
//window.open()
function OpenWindow(theURL,winName) {
	if(MyWindow != null) { //Se já foi aberta uma janela anteriormente.
		if(!MyWindow.closed) {
			//se a janela aberta anteriormente ainda não foi fechada.
			// se a jenela que está sendo aberta é diferente da anterior
			if(MyWindow.name != winName) {
				MyWindow.close(); //fecha a janela anterior
				MyWindow = window.open(theURL,winName,'status=yes,scrollbars=yes,width=300,height=300'); //abre uma nova janela
			}
			else {
				//caso as janelas tenham o mesmo nome
				MyWindow.focus(); //muda o foco para a janela aberta tanteriormente.
			}
		}
		else {
			// caso já tenha sido fechada uma janela anteriormente
			// abre a nova janela.
			MyWindow = window.open(theURL,winName,'alwaysRaised=yes,status=yes,scrollbars=yes,width=300,height=300');
		}
	}
	else {
		//caso nenhuma janela tenha sido aberta anteriormente, i.e., MyWindow == null
		//abre uma nova janela.
		MyWindow = window.open(theURL,winName,'alwaysRaised=yes,status=yes,scrollbars=yes,width=300,height=300');
	}
}

function ResetWindow(bmp) //esta função deve ser escrita no arquivo que será aberto na
{   
	//pela função jczOpenWindow.
	self.moveTo(2,2); //move janela para canto superior esquerdo
	self.resizeTo(bmp.width+50,bmp.height+130); //redimensiona janela baseada em bmp.
}

//function popupOperadora(url){
//	window.open(url,"popup",'alwaysRaised=yes,status=yes,scrollbars=yes,width=890,height=300');
//}

function popup(theURL,width,height,features) { 
	//alert(width,height,features);
	if(window.screen){
		per_ancho=(width/screen.width)*100;
		per_alto=(height/width)*100;
		win_ancho=(screen.width*per_ancho)/100;
		win_alto=(win_ancho*per_alto)/100;
		x=(screen.width-win_ancho)/2;
		y=(screen.height-win_alto)/2;
	}else{
		x=0;
		y=0;
		win_ancho=w;
		win_alto=y;
	}
	winfeatures=("top="+y+",left="+x+",scrollbars=yes, width="+win_ancho+",height="+win_alto+","+features);
	window.open(theURL,'',winfeatures);
}

function popupRelatorio(url){
	window.open(url,"popup",'alwaysRaised=yes, status=yes,scrollbars=yes,width=700,height=450');
}

//Máscara para valores do tipo data.
function mask(fld, e) 
{
	if(fld.value.length == 2){
		fld.value = fld.value + "/";
	}
	if(fld.value.length == 5 ){
		fld.value = fld.value + "/";
	}
	var strCheck = '0123456789';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	key = String.fromCharCode(whichCode);
	if (strCheck.indexOf(key) == -1) return false;
}

function masc_fone(fld,e){ 
var fone = fld.value;
		if (fone.length == 04) {
			fone = fld.value + '-';
			//alert(nasc)
			fld.value = fone;
		}
		
		return IsNumeric(fld, e);
}

function IsNumeric(fld, e) 
{
	var strCheck = '0123456789';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	key = String.fromCharCode(whichCode);
	if (strCheck.indexOf(key) == -1) return false;
}

function IsNumericValor(fld, e) 
{
	var strCheck = '0123456789,';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	key = String.fromCharCode(whichCode);
	if (strCheck.indexOf(key) == -1) return false;
}

function closeRelatorio(){
	window.opener.location='../login/wfrmlogin.aspx';
	window.close();
}

function validaPreContrato()
{
	frm = document.Form1;
	if (frm.txtRazao.value == ""){
		alert("Digite o nome da sua empresa!");
		frm.txtRazao.focus();
		return false;
	}
	
	if (valida_CNPJ(frm.txtCGC.value) != true){
		alert("CNPJ inválido!");
		frm.txtCGC.focus();
		return false;
	}
	
	if (frm.txtEmail.value != ""){
		if (frm.txtEmail.value.indexOf("@") == -1){
			alert("Email Inválido!");
			frm.txtEmail.focus();
			return false;
		}
	}
	
	if (frm.txtEndereco.value == ""){
		alert("Digite o endereço!");
		frm.txtEndereco.focus();
		return false;
	}
	
	if (frm.txtBairro.value == ""){
		alert("Digite o bairro!");
		frm.txtBairro.focus();
		return false;
	}
	
	/*if (frm.txtComplemento.value == ""){
		alert("O campo Complemeto não pode ser vazio!");
		return false;
	}*/
	
	if (frm.txtCep.value == ""){
		alert("Digite o Cep!");
		frm.txtCep.focus();
		return false;
	}
	
	if (frm.txtCidade.value == ""){
		alert("Digite a cidade!");
		frm.txtCidade.focus();
		return false;
	}
	
	if (frm.drpEstado.value == 0){
		alert("Selecione o estado!");
		frm.drpEstado.focus();
		return false;
	}
	
	if (frm.txtFone.value == ""){
		alert("Digite o telefone!");
		frm.txtFone.focus();
		return false;
	}
	
	if (frm.txtContato.value == ""){
		alert("Digite um contato!");
		frm.txtContato.focus();
		return false;
	}
	frm.submit();
}

function valida_CNPJ(s)
{
	var i;
	var c = s.substr(0,12);
	var dv = s.substr(12,2);
	var d1 = 0;
	for (i = 0; i < 12; i++)
	{
		d1 += c.charAt(11-i)*(2+(i % 8));
	}
        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 < 12; i++)
	{
		d1 += c.charAt(11-i)*(2+((i+1) % 8));
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(1) != d1)
	{
		return false;
	}
        return true;
}

function validaFormVenda(){
	frm = document.formVenda;
	
	if (frm.drpOperadora.value == "0"){
		alert('Selecione uma Operadora !!!');
		frm.drpOperadora.focus();
		return false;
	}
	
	if (frm.drpValor.value == "0"){
		alert('Selecione um Valor de Crédito !!!');
		frm.drpValor.focus();
		return false;
	}
	
	frm.submit();
}

function habSenhaControl(form){
	frm=form;
	
	if (frm.chkAlterarSenha.checked == true){
		frm.txtSenha.disabled = false;
		frm.txtConfirmar.disabled = false;
	}
	else{
		frm.txtSenha.disabled = true;
		frm.txtConfirmar.disabled = true;	
	}
}

function alteraTipo(){
	//frm = document.Form1;
	//document.getElementById("usu").innerHTML="Usu&aacute;rio";
	
	if((document.getElementById("drpEntidade").value == 1)){
		document.getElementById("estabelecimento1").style.display = "block";
		document.getElementById("estabelecimento2").style.display = "block";
	}else if(document.getElementById("drpEntidade").value == 5){
		document.getElementById("estabelecimento1").style.display = "block";
		document.getElementById("estabelecimento2").style.display = "block";
		document.getElementById("usu").innerHTML="Usu&aacute;rio POS";
	}
	else{
		document.getElementById("estabelecimento1").style.display = "none";
		document.getElementById("estabelecimento2").style.display = "none";
	}
}

function habilitaOperadoras(){
	frm=document.formPDV;
	//alert(frm.drpPerfil.options[frm.drpPerfil.selectedIndex].innerText);
	frm.drpOperadoras.disabled=true;
	frm.lstDisponiveis.disabled=true;
	frm.lstRelacionados.disabled=true;
	if (frm.drpPerfil.options[frm.drpPerfil.selectedIndex].innerText=="OPERADORAS"){
		frm.drpOperadoras.disabled=false;
		frm.lstDisponiveis.disabled=false;
		frm.lstRelacionados.disabled=false;
	}
}

function maskDT(fld, e){
	if(fld.value.length == 2){
		fld.value = fld.value + "/";
	}
	if(fld.value.length == 5 ){
		fld.value = fld.value + "/";
	}
	
	var strCheck = '0123456789';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	key = String.fromCharCode(whichCode);
	if (strCheck.indexOf(key) == -1) return false;
}

//Máscara CNPJ
function mascara_cnpj(cnpj, e){
	//99.999.999/9999-99 
	
    if (cnpj.value.length == 2) {
        cnpj.value = cnpj.value + '.';
    }
    
    if (cnpj.value.length == 6) {
        cnpj.value = cnpj.value + '.';
    }
    
    if (cnpj.value.length == 10) {
        cnpj.value = cnpj.value + '/';
    }
    
    if (cnpj.value.length == 15) {
        cnpj.value = cnpj.value + '-';
    }
    
    return IsNumeric(cnpj, e);
}


function findObj(theObj, theDoc){
	var p, i, foundObj;
	if(!theDoc)
		theDoc = document;
	if((p = theObj.indexOf("?")) > 0 && parent.frames.length){
		theDoc = parent.frames[theObj.substring(p+1)].document;
		theObj = theObj.substring(0,p);
	}
	if(!(foundObj = theDoc[theObj]) && theDoc.all)
		foundObj = theDoc.all[theObj];
	for (i=0; !foundObj && i < theDoc.forms.length; i++)
		foundObj = theDoc.forms[i][theObj];
	for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++)
		foundObj = findObj(theObj,theDoc.layers[i].document);
	if(!foundObj && document.getElementById)
		foundObj = document.getElementById(theObj);
	return foundObj;
}

function tirarZerosEsquerda(STR){
	var sAux = '';
	STR = new String(STR);
	var i = 0;
	while (i < STR.length ){
		if ((STR.charAt(i)!='.') && (STR.charAt(i)!=',')){
			sAux += STR.charAt(i);
		}
		i++
	}
	STR = new String(sAux);
	sAux = '';
	i = 0;
	while (i < STR.length ){
		if (STR.charAt(i) != '0'){
			sAux = STR.substring(i,STR.length)
			i = STR.length;
		}
		i++;
	}
	return sAux;
}
	

function formatarValor(str){
	var decimal,inteiro;
	var i,count; STR = new String(str);
	STR = tirarZerosEsquerda(STR);
	//alert(STR);
	inteiro='';
	
	if (STR.length == 1){
		inteiro = '0';
		decimal = '0' + STR;
	}
	else {
		if (STR.length == 2){
			inteiro = '0';
			decimal = STR;
		}
		else{
			decimal = STR.substring(STR.length-2,STR.length);
			i=3;
			count=0;
			while (i<=STR.length){
				if (count==3) {
					inteiro = '.' + inteiro;
					count = 0;
				}
				inteiro = STR.charAt(STR.length-i) + inteiro;
				count++;
				i++;
			}
		}
	}
	
	if (inteiro == '') {
		inteiro = '0';
	}
	if (decimal == '') {
		decimal = '00';
	}
	return inteiro+','+decimal;
}

function soNumero(num) {
	var keyNumber = num;
	if (((keyNumber<48)||(keyNumber>57)) && (keyNumber!=13) && (keyNumber!="0") && (keyNumber!=8)) {
		return false;
	}
	return true;
}

function aplicacarFormatacaoCampos(e, namefield){
	var strCheck = '0123456789';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	var obj = findObj(namefield);
	key = String.fromCharCode(whichCode);
	
	if (obj!=null){
		if (soNumero(key)){	
			var valor = document.getElementById(namefield).value;
			
			document.getElementById(namefield).value = formatarValor(valor);
		}
	}
}

//onKeyDown="return goDown(ValidaForm)"
function goDown(fnc)
{
	var whichCode = (window.Event) ? event.which : event.keyCode;
	if (whichCode == 13){
		return fnc();
	}
}
