// Limite Texto
// EX: <textarea name="" onkeypress="javascript:return limitTextArea(this,1500)"></textarea>

function limitTextArea(objeto,limite){
         var cont = objeto.value.length;

         if(document.getElementById('dig')){
            document.getElementById('dig').innerHTML = cont > limite ? limite : cont;
         }
            
         if(document.getElementById('res')){
            document.getElementById('res').innerHTML = cont > limite ? 0 : (limite - cont);
         }
         
         if(cont > limite){
            objeto.value = objeto.value.substring(0,limite);
         }

}

// Somente Números
// EX: <input name="" type="text" onkeypress="return SomenteNumeros(event)" />

function SomenteNumeros(e){
    var tecla=(window.event)?event.keyCode:e.which;
    if((tecla > 47 && tecla < 58)) return true;
    else{
    if (tecla != 8) return false;
    else return true;
    }
}

// Mascara Moeda
// EX: <input name="" type="text" onKeyPress="return MascaraMoeda(this,'.',',',event)" />

function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if (whichCode == 13) return true;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
        objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}

function FormataData(object,teclapres){
    var tecla = teclapres.keyCode;
    vr = object.value;
    vr = vr.replace( ".", "" );
    vr = vr.replace( "/", "" );
    vr = vr.replace( "/", "" );
    tam = vr.length + 1;
    if( tecla != 9 && tecla != 8 )
    {
    if( tam > 2 && tam < 5 )
    object.value = vr.substr( 0, tam - 2 ) + '/' + vr.substr( tam - 2, tam );
    if( tam >= 5 && tam <= 10 )
    object.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 );
    }
}

var max = 10;
function setLimite(objeto){
         if(objeto.id == 'feminino'){
            var dest = document.getElementById('masculino');
            var limite = masculino;
         } else if(objeto.id == 'masculino'){
            var dest = document.getElementById('feminino');
            var limite = feminino;
         }

         var valor = objeto.value;
         var total = max - valor;
         total = total < limite ? total : limite;
         
         var tmp = dest.value;
         dest.innerHTML = '';
         
         for(i = 0 ; i <= total ; i++){
             option = document.createElement('option');
             option.value = i;
             
             if(i == tmp) option.selected = true;
             
             node = document.createTextNode(i);
             option.appendChild(node);
             dest.appendChild(option);
         }
         setPreco();
         return true;
}

function setPreco(){
         var feminino = document.getElementById('feminino').value;
         var masculino = document.getElementById('masculino').value;
         
         var total_masc = masculino * preco_masculino;
         var total_fem = feminino * preco_feminino;
         
         var total = total_masc + total_fem;
         

         if(document.getElementById('preco'))  document.getElementById('preco').innerHTML = formataPreco(total+'');
}

function formataPreco(preco){
         if(preco.indexOf('.') != -1){
            var dados  = preco.split('.');
            var cents = dados[1].substr(0,2);
            var preco = 'R$'+dados[0]+','+ (cents.length == 1 ? cents + '0' : cents);
         } else {
            var preco = 'R$'+preco+',00';
         }
         return preco;
}

function como(obj){
         if(obj.value == 'Outros'){
            document.getElementById('others').style.display = '';
            document.getElementById('outros').focus();
         } else {
            document.getElementById('others').style.display = 'none';
         }
}
