function check(formContato) {
    
    if (formContato.nome.value == "") {
        alert("Por favor, informe seu Nome!");
        formContato.nome.focus();
        formContato.nome.select();
        return false;
    }

    if (formContato.email.value == "") {
        alert("Por favor, informe seu E-mail.");
        formContato.email.focus();
        formContato.email.select();
        return false;
    }

    if (formContato.email.value.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1) {
        alert("Seu endereço de e-mail parece estar incorreto. \nPor favor, verifique se não está faltando '@', '.' e/ou dominio.");
        formContato.email.focus();
        formContato.email.select();
        return false;
    }
    
    return true;
}


document.getElementsByClassName = function(clsName){
    var retVal = new Array();
    var elements = document.getElementsByTagName("*");
    for(var i = 0; i < elements.length; i++){
        if(elements[i].className.indexOf(" ") >= 0){
            var classes = elements[i].className.split(" ");
            for(var j = 0;j < classes.length;j++){
                if(classes[j] == clsName){
                    retVal.push(elements[i]);
                }
            }
        } else if(elements[i].className == clsName){
            retVal.push(elements[i]);
        }
    }
    return retVal;
}


var quantidadeMinima = 0;
var quantidade = 0;



function setImagem(id, imagem){
    document.getElementById(id).setAttribute("src", imagem);
}
function ampliarImagem(imagem){
    setImagem('imagem', imagem);
    abreElemento('fundo_opacity');
    abreElemento('imagem_teste');
    document.getElementById('fundo_opacity').style.height = document.body.scrollHeight;
}
function setTamnhoTotal(id){
    document.getElementById(id).style.height = document.height;
}

function abreElemento(id){
    document.getElementById(id).style.display = "";
}

function fechaElemento(id){
    document.getElementById(id).style.display = "none";
}

function teste(id){
    var elementoId = document.getElementById(id);
    if(elementoId.style.display == "none"){
        elementoId.style.display = "";
    }else{
        elementoId.style.display = "none";
    }
}
function teste2(id, texto1, texto2){
    var elementoId = document.getElementById(id);
    if(elementoId.childNodes[0].nodeValue == texto1){
        elementoId.childNodes[0].nodeValue = texto2;
    } else {
        elementoId.childNodes[0].nodeValue = texto1;
    }
}
function teste3(id, img1, img2){
    var elementoId = document.getElementById(id);
    if(elementoId.src == img1){
        elementoId.src = img2;
    } else {
        elementoId.src = img1;
    }
}

function limparAllInputs(){
    for (var i1 = 0; i1 < document.forms.length; i1++) {
        for (var i2 = 0; i2 < document.forms[i1].elements.length; i2++) {
            var input = document.forms[i1].elements[i2];

            //if(input.type != 'submit' && input.type != 'buttom' && input.type != 'hidden'){
            if(input.type == 'text'){
                input.value = '';
            }
            if(input.type == 'select-one'){
                input.selectedIndex = 0;
                /*for (var i3 = 0; i3 < input.options.length; i3++) {
                    input.options[i3].selected = false;
                }*/
            }
        }
    }
}

function setQuantidadeMinima(quant){
    quantidadeMinima = quant;
}
function atualizaQuantidade(){
    var valor = document.getElementById("quantidade").value;
    if(valor.length > 0 && !isNaN(valor)){
        valor = parseInt(valor, 10);
        quantidade = valor;
    }else{
        //alert("Você digitou um valor inválido!\nPor favor, digite um número válido.");
        document.getElementById("quantidade").value = "";
    }
}
function verificarQuantidade(){
    if(quantidade < quantidadeMinima){
        window.alert("A quantidade esta abaixo da quantidade mínima de " + quantidadeMinima + ".");
        return false;
    }else{
        return true;
    }
}

function trim(str) {
    var i = 0;
    for(i = 0; i < str.length; i++) {
        if(str.charAt(i) == ' '){
            str = str.substr(1, str.length - 1);
        } else {
            break;
        }
    }
    for(i = (str.length - 1); i >= 0; i--) {
        if(str.charAt(i) == ' '){
            str = str.substr(0, str.length - 1);
        } else {
            break;
        }
    }
    return str;
}

function cotacao_verificar_quantidade_minima(form) {
    try {
        var elements = form.produtoId;
        if((!elements.length)) {
            elements = Array(elements);
        }
        var okcheck = false;
        for(var i in elements) {
            if(elements[i].nodeName == 'INPUT') {
                var produtoId = elements[i].value;
                var check       = form['produtos['+produtoId+']'].checked;
                var quant       = parseFloat(form['quantidades['+produtoId+']'].value);
                var quantmin    = parseFloat(form['quantidades_minimas['+produtoId+']'].value);
                if(check) {
                    okcheck = true;
                    if(isNaN(quant) || quant < quantmin) {
                        alert("Verifique a quantidade mínima dos produtos selecionado.")
                        return false;
                    }
                }
            } 
        }
//        if(!okcheck) {
//            alert("Não há produtos selecionados!\nPor favor, selecione um ou mais produtos.")
//            return false;
//        }
    } catch (exception) {
        return false;
    }
    return true;
}


function abreJanela(arquivo, tamx, tamy){
    // onde arquivo é html popUp, tamx é o tamanho horizontal em pixels e tamy tamanho vertical em pixels
    window.open(arquivo,"","resizable=yes,toolbar=no,status=no,menubar=no,scrollbars=yes,width=" + tamx + ",height=" + tamy)
}

function formataData(campo,teclapres) {
    var tecla = teclapres.keyCode;
    var vr = document.getElementById(campo).value;
    //alert(campo);
    vr = vr.replace( ".", "" );
    vr = vr.replace( "/", "" );
    vr = vr.replace( "/", "" );
    vr = vr.replace( "/", "" );
    var tam = vr.length + 1;

    if ( tecla != 9 && tecla != 8 ){
        if ( tam > 2 && tam < 5 ){
            document.getElementById(campo).value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
        }
        if ( tam >= 5 && tam <= 10){

            document.getElementById(campo).value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 );
            if ( tam >= 7 && tam <= 10 ){

            }
        }
    }
}


function formataHora(campo,teclapres) {
    var tecla = teclapres.keyCode;
    vr = document.getElementById(campo).value;
    //alert(campo);
    vr = vr.replace( ".", "" );
    vr = vr.replace( ":", "" );
    vr = vr.replace( ":", "" );
    vr = vr.replace( ":", "" );
    tam = vr.length + 1;

    if ( tecla != 9 && tecla != 8 ){
        if ( tam > 2 && tam < 5 ){
            document.getElementById(campo).value = vr.substr( 0, tam - 2  ) + ':' + vr.substr( tam - 2, tam );
        }
        if ( tam >= 5 && tam <= 10 ){

            document.getElementById(campo).value = vr.substr( 0, 2 ) + ':' + vr.substr( 2, 2 ) + ':' + vr.substr( 4, 4 );
            if ( tam >= 7 && tam <= 10 ){
        //alert(document.cadastro[campo].value + vr);
            }
        }
    }
}



