function setMaxLength() {
    var x = document.getElementsByTagName('textarea');
    var counter = document.createElement('div');
    counter.className = 'counter';

    for (var i = 0; i < x.length; i++) {
        if (x[i].getAttribute('MaxLen')) {
            x[i].onkeyup = x[i].onchange = checkMaxLength;
            x[i].onkeyup();
        }
    }
}

function checkMaxLength() {
    var maxLength = this.getAttribute('MaxLen');
    var currentLength = this.value.length;
    if (currentLength > maxLength) {
        this.value = this.value.substring(0, maxLength)
    }
}

window.onload = function() {
    $("input, textarea").change(function() {
        this.value = $.trim(this.value);
        this.value = stripHTML(this.value);
    });

    setMaxLength();
}

if (parent.window.frames.length != 0) {
    self.parent.location = '/';
    self.parent.tb_remove();
}

function isEmailAvailable(email) {
    var isAvl = false;
    $.ajax({
        type: "POST",
        url: "/Cadastro.aspx/IsEmailAvailable",
        data: "{'email':'" + email + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        success: function(msg) {
            isAvl = msg.d;
        }
    });

    return isAvl;
}

function CEPComplete(txtCep, txtEndereco, txtBairro, txtCidade, txtUf, txtNumero) {
    $.getScript("http://cep.republicavirtual.com.br/web_cep.php?formato=javascript&cep=" + txtCep.val(), function() {
        if (resultadoCEP["resultado"] && resultadoCEP["bairro"] != "") {
            txtEndereco.val(unescape(resultadoCEP["tipo_logradouro"]) + " " + unescape(resultadoCEP["logradouro"]));
            txtBairro.val(unescape(resultadoCEP["bairro"]));
            txtCidade.val(unescape(resultadoCEP["cidade"]));
            txtUf.val(unescape(resultadoCEP["uf"]));
            $("#panelEndereco").show();
            $("#panelLoadEndereco").hide();
            txtNumero.focus();
            $('#displayErro').html('');
        } else {
            $('#displayErro').html("Endereço não encontrado, verifique seu CEP ou preencha os campos abaixo.");
            $("#panelEndereco").show();
            $("#panelLoadEndereco").hide();
        }
    });
} 




function validateLogin() {
    if (!$('#txtEmail').isValidEmail() || $('#txtPassword').val() == "") {
        alert("Os campos E-mail e Senha não foram preenchido corretamente.");
        return false;
    }
    else
        return true;
}


function userLogin(email, password) {
    if (!isValidEmail(email) || password == "")
        alert("E-mail ou senha inválida");
    else
        $.ajax({
            type: "POST",
            url: "/Default.aspx/UserAuthorize",
            data: "{'email':'" + email + "','password':'" + password + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: false,
            success: function (data) {
  
                $('#txtEmail').val("");
                $('#txtPass').val("");

                if (data.d == 'ERROWATINGACCEPT') {
                    alert("Seu cadastro pendente de liberação pelo administrador.");
                    return;
                }

                if (data.d == 'ERRONOTACCEPT') {
                    alert("Seu cadastro foi bloqueado pelo administrador.");
                    return;
                }

                if (data.d == "ERROLOGIN") {
                    alert("Usuário ou senha inválida");
                    return;
                }

                location = data.d;
            }
        });
  
}


var maxWidth = 500;
var maxHeight = 500;
var fileTypes = ["bmp", "gif", "png", "jpg", "jpeg", "tif"];
var globalPic;

function imgVerify() {
    __doPostBack('lbtnSendPhoto', '');
}


function saveErro() {
    alert('Não foi possível salvar o formulário.');
}


function OpenModalPlayers(materialId, tipo, w, h) {
    var newA = document.createElement("A");
    newA.rel = "thickbox";
    newA.href = "/Players.aspx?materialId=" + materialId + "&tipo=" + tipo + "&TB_iframe=true&height=" + h + "&width=" + w;
    newA.title = "Visualização";
    tb_show(newA.title, newA.href, newA.rel);
    newA = null;
} 

