﻿
function somenteNumeros(e) {
    //var whichCode = (window.event) ? event.keyCode : e.which;
    
////    try {
////        alert('window.event=' + window.event);
////    } catch (x) {

//    //    }
//    alert(e.which);


//    try {
//        alert('event.keyCode=' + event.keyCode);
//    } catch (x) {

//    }

//    try {
//        alert('event.which=' + event.which);
//    } catch (x) {

//    }

//    try {
//        alert('e='+e);
//    } catch (x) {

//    }
//        

    var tecla = (window.event) ? event.keyCode : e.which;
    
    if ((tecla > 47 && tecla < 58))
        return true;
    else {
        if (tecla != 8 && tecla != 0 && tecla != 44)
            return false;
        else
            return true;
    }
}


function validarCadastro() {

//    if ($('txtNome').value.trim() == '' || $('txtNome').value.trim() == 'Usuário') {
//        alert('Informe o Nome');
//        $('txtNome').focus();
//        return false;
//    }

//    if ($('txtEmail').value.trim() == '' || $('txtEmail').value.trim() == 'Senha') {
//        alert('Informe a Senha');
//        $('txtEmail').focus();
//        return false;
//    }

    /*if ((/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($('txtEmail').value.trim()) == false)) {
        alert("Informe um Email válido");
        $('txtEmail').focus();
        return (false);
    }*/

    return true;
}

function focusNome() {
    if ($('txtNome').value == 'Usuário') {
        $('txtNome').value = '';
        $('txtNome').className = 'input';
    }
}

function blurNome() {
    if ($('txtNome').value == '') {
        $('txtNome').value = 'Usuário';
        $('txtNome').className = 'input infoForm';
    }
}

function focusEmail() {
    if ($('txtEmail').value == 'Senha') {
        $('txtEmail').value = '';
        $('txtEmail').className = 'input';
    }

}

function blurEmail() {
    if ($('txtEmail').value == '') {
        $('txtEmail').value = 'Senha';
        $('txtEmail').className = 'input infoForm';
    }

}

function isCurrentBrowser(browserName){	
	if(navigator.userAgent.search(browserName) != -1)
		return true;
	else
		return false;
}
 

function navHoverAux() {
    var lis = $$('.headerControl_navmenu_h li');
    for (var i = 0; i < lis.length; i++) {
        $(lis[i]).addEvents({
            'mouseover': function() {
                $('map3d').setStyle('top', '-10000px');
                $('lsEstados').setStyle('top','379px');
                $('mapa').setStyle('height', $('map3d').getStyle('height'));
                
            },
            'mouseout': function() {
                $('map3d').setStyle('top', '0px');
                $('mapa').setStyle('height', '0px');
				
				if(isCurrentBrowser("MSIE"))
                	$('lsEstados').setStyle('top', '379px');
				else
					$('lsEstados').setStyle('top', '379px');
            }
        });

        /*lis[i].onmouseover = function() {
            this.className += " iehover";
            
            $('teste').setStyle('top', '-1000px');
        }
        lis[i].onmouseout = function() {
            this.className = this.className.replace(new RegExp(" iehover\\b"), "");
            $('teste').setStyle('top', '0px');
        }*/
    }

}


navHover = function() {
    if (null == document.getElementById("headerControl_navmenu_h")) return;
    
    var lis = document.getElementById("headerControl_navmenu_h").getElementsByTagName("LI");
    for (var i = 0; i < lis.length; i++) {
        lis[i].onmouseover = function() {
            this.className += " iehover";

        }
        lis[i].onmouseout = function() {
            this.className = this.className.replace(new RegExp(" iehover\\b"), "");
        }
    }

}
if (window.attachEvent) window.attachEvent("onload", navHover);


function getAddress(){
    $('txtCEP_2').addEvent('blur', function(e) {

	    if ($('txtCEP_1').value.trim() != '' && $('txtCEP_2').value.trim() != '') {
		    var url = 'services/CEP.aspx?cep=' + $('txtCEP_1').value + $('txtCEP_2').value;
		    var request = new Json.Remote(url,
	    {

		    onComplete: function(retorno) {
			    if (retorno.status == 0) {
				    $('txtEndereco').value = retorno.endereco;
				    $('cmbUF').value = retorno.UF;
				    __doPostBack('cmbUF', retorno.cidade)
			    }
			    else
				    alert(retorno.message);

		    } .bind(this)
	    }).send();

	    }
    });
}

function validarCpfCnpj(e, obj) {
	if (!Validador.validarCPF(obj.Value) && !Validador.validarCNPJ(obj.Value)) {
		e.style.display = 'inline';
		e.style.visibility = 'visible';
		return false;
	}
}
 
function validar(opt) {
	if($('txtInstituicao').value =='') {
		alert('Informe o Nome da Instituição');
		$('txtInstituicao').focus();
		return false;
	}
	
	if($('txtNome').value == '') {
		alert('Informe o nome da pessoa física ou jurídica');
		$('txtNome').focus();
		return false;
	}


	if (!Validador.validarCPF($('txtCNPJ').value.trim()) && !Validador.validarCNPJ($('txtCNPJ').value.trim())) {
		alert("Verifique o valor informado para o campo 'CPF/CNPJ'.");
		return false;
	}

	if($('cmbTipoInstituicao').value =='0') {
		alert('Selecione o tipo da instituição');
		$('cmbTipoInstituicao').focus();
		return false;
	}
	
	if($('txtSiglaInstituicao').value == '') {
		alert('Informe a sigla da instituição');
		$('txtNome').focus();
		return false;
	}
	
	if($('txtEmail').value == '') {
		alert('Informe o Email');
		$('txtEmail').focus();
		return false;
	}
	
	if(!RegExp(/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/).test($('txtEmail').value)) {
		alert('O email informado é inválido');
		$('txtEmail').focus();
		return false;
	}
	
	if($('txtCEP_1').value == '') {
		alert('Informe o CEP corretamente');
		$('txtCEP_1').focus();
		return false;
	}
	
	if($('txtCEP_2').value == '') {
		alert('Informe o CEP corretamente');
		$('txtCEP_2').focus();
		return false;
	}
    
	if($('cmbUF').value == '0') {
		alert('Selecione o Estado');
		$('cmbUF').focus();
		return false;
	}
	
	if($('cmbMunicipio').value == '0') {
		alert('Selecione o Município');
		$('cmbMunicipio').focus();
		return false;
	}
	
	if($('txtEndereco').value == '') {
		alert('Informe o Endereço');
		$('txtEndereco').focus();
		return false;
	}
	
	if($('txtNumero').value == '') {
		alert('Informe o Número');
		$('txtNumero').focus();
		return false;
	}
	
	if($('txtDD1').value == '') {
		alert('Informe o DDD do contato');
		$('txtDD1').focus();
		return false;
	}
	
	if($('txtTelefone1').value == '') {
		alert('Informe o telefone do contato');
		$('txtTelefone1').focus();
		return false;
	}
	
	if($('txtContato1').value == '') {
		alert('Informe o nome do contato');
		$('txtContato1').focus();
		return false;
	}
	
	if($('txtEmail1').value == '') {
		alert('Informe o email do contato');
		$('txtEmail1').focus();
		return false;
	}
	
	if
	(
		($('txtTelefone1').value != '' && ($('txtDD1').value == '' || $('txtContato1').value == '' || $('txtEmail1').value == ''))
		||
		($('txtDD1').value != '' && ($('txtTelefone1').value == '' || $('txtContato1').value == '' || $('txtEmail1').value == ''))
		||
		($('txtContato1').value != '' && ($('txtDD1').value == '' || $('txtTelefone1').value == '' || $('txtEmail1').value == ''))
		||
		($('txtEmail1').value != '' && ($('txtDD1').value == '' || $('txtContato1').value == '' || $('txtTelefone1').value == ''))
	) 
		{
			alert('Verifique os dados informados para o primeiro contato.\nÉ necessário que o contato possua um nome, telefone e e-mail.');
			return false;
		}


		if
	(
		($('txtTelefone2').value != '' && ($('txtDD2').value == '' || $('txtContato2').value == '' || $('txtEmail2').value == ''))
		||
		($('txtDD2').value != '' && ($('txtTelefone2').value == '' || $('txtContato2').value == '' || $('txtEmail2').value == ''))
		||
		($('txtContato2').value != '' && ($('txtDD2').value == '' || $('txtTelefone2').value == '' || $('txtEmail2').value == ''))
		||
		($('txtEmail2').value != '' && ($('txtDD2').value == '' || $('txtContato2').value == '' || $('txtTelefone2').value == ''))
	) {
			alert('Verifique os dados informados para o segundo contato.\nÉ necessário que o contato possua um nome, telefone e e-mail.');
			return false;

	}
	
	if($('txtInstituicao').value == '') {
		alert('Informe o Nome da instituição.');
		$('txtInstituicao').focus();
		return false;
	}

	if (opt == true) {
		if ($('rdAceitoProt').checked != true) {
			alert('Concorde com o Protocolo.');
			$('rdAceitoProt').focus();
			return false;
		}
	}
}

var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

function MascaraMoeda(fld, milSep, decSep, e) {
    if (fld.value.length >= 15) {
        return false;
    }

    var sep = 0;
    var key = "";
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = "0123456789";
    var aux = aux2 = "";
    var whichCode = "";
    var whichCode = (window.event) ? event.keyCode : e.which;
    
    if (isIE) {
        whichCode = e.keyCode;
    }
    else if (isWin) {
        whichCode = e.which;
    }
    else if (isOpera) {
        whichCode = e.charCode;
    }

    if ((whichCode == 13) || (whichCode == 0) || (whichCode == 8)) {
        return true;
    }

    key = String.fromCharCode(whichCode); // Valor para o c�digo da Chave 

    if (strCheck.indexOf(key) == -1) // Chave inv�lida 
    {
        return false;
    }

    len = fld.value.length;

    for (i = 0; i < len; i++)

        if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;

    aux = '';

    for (; i < len; i++)

        if (strCheck.indexOf(fld.value.charAt(i)) != -1) aux += fld.value.charAt(i);

    aux += key;

    len = aux.length;

    if (len == 0) fld.value = '';

    if (len == 1) fld.value = '0' + decSep + '0' + aux;

    if (len == 2) fld.value = '0' + decSep + aux;

    if (len > 2) {

        aux2 = '';

        for (j = 0, i = len - 3; i >= 0; i--) {

            if (j == 3) {
                aux2 += milSep;
                j = 0;
            }

            aux2 += aux.charAt(i);
            j++;
        }

        fld.value = '';

        len2 = aux2.length;

        for (i = len2 - 1; i >= 0; i--)

            fld.value += aux2.charAt(i);

        fld.value += decSep + aux.substr(len - 2, len);

    }

    return false;
}
