// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

/**
 * Formata o Campo de acordo com a mascara informada.
 * Ex de uso: onkeyup="AplicaMascara('00:00:00', this);".
 * @author Igor Escobar (blog@igorescobar.com)
 * @param Mascara String que possui a mascara de formatação do campo.
 * @param elemento Campo que será formatado de acordo com a mascara, voce pode informar o id direto ou o próprio elemento usando o this.
 * @returns {void}
 */
function AplicaMascara(Mascara, elemento){
    // Seta o elemento
    var elemento = (elemento) ? elemento : document.getElementById(elemento);
    if (!elemento) 
        return false;
    // Método que busca um determinado caractere ou string dentro de uma Array
    function in_array(oque, onde){
        for (var i = 0; i < onde.length; i++) {
            if (oque == onde[i]) {
                return true;
            }
        }
        return false;
    }
    // Informa o array com todos os caracteres que podem ser considerados caracteres de mascara
    var SpecialChars = [':', '-', '.', '(', ')', '/', ',', '_', ' '];
    var oValue = elemento.value;
    var novo_valor = '';
    for (i = 0; i < oValue.length; i++) {
        //Recebe o caractere de mascara atual
        var nowMask = Mascara.charAt(i);
        //Recebe o caractere do campo atual
        var nowLetter = oValue.charAt(i);
        //Aplica a masca
        if (in_array(nowMask, SpecialChars) == true && nowLetter != nowMask) {
            novo_valor += nowMask + '' + nowLetter;
        }
        else {
            novo_valor += nowLetter;
        }
    }
    // Retorna o valor do elemento com seu novo valor
    elemento.value = novo_valor;
}
    
/*
Author: Addam M. Driver
Date: 10/31/2006
 */

var sMax;	// Isthe maximum number of stars
var holder; // Is the holding pattern for clicked state
var preSet; // Is the PreSet value onces a selection has been made
var rated;

// Rollover for image Stars //
function rating(num){
    sMax = 0;	// Isthe maximum number of stars
    for(n=0; n<num.parentNode.childNodes.length; n++){
        if(num.parentNode.childNodes[n].nodeName == "A"){
            sMax++;	
        }
    }
	
    if(!rated){
        s = num.id.replace("_", ''); // Get the selected star
        a = 0;
        for(i=1; i<=sMax; i++){		
            if(i<=s){
                document.getElementById("_"+i).className = "on";
                document.getElementById("rateStatus").innerHTML = num.title;	
                holder = a+1;
                a++;
            }else{
                document.getElementById("_"+i).className = "";
            }
        }
    }
}

// For when you roll out of the the whole thing //
function off(me){
    if(!rated){
        if(!preSet){	
            for(i=1; i<=sMax; i++){		
                document.getElementById("_"+i).className = "";
                document.getElementById("rateStatus").innerHTML = me.parentNode.title;
            }
        }else{
            rating(preSet);
            document.getElementById("rateStatus").innerHTML = document.getElementById("ratingSaved").innerHTML;
        }
    }
}

// When you actually rate something //
function rateIt(me){
    if(!rated){
        document.getElementById("rateStatus").innerHTML = document.getElementById("ratingSaved").innerHTML + " :: "+me.title;
        preSet = me;
        rated=1;
        sendRate(me);
        rating(me);
    }
}

function elemento_existe (elemento) {
    return (elemento) ? elemento : document.getElementById(elemento);
}

function perde_foco(elemento){
    if (elemento_existe(elemento))
        elemento.value = elemento.value || elemento.defaultValue; elemento.style.color = '#999';
}
function foco(elemento){
    if (elemento_existe(elemento))
        elemento.value=''; elemento.style.color = '#000';
}
function esconde(elemento) {
    if(elemento.length < 1) { return; }
    document.getElementById(elemento).style.display = "none";
}
function mostra(elemento) {
    if(elemento.length < 1) { return; }
    document.getElementById(elemento).style.display = "block";
}
function inverte_visibilidade(elemento) {
    if(elemento.length < 1) { return; }
    if(document.getElementById(elemento).style.display == "none")
        document.getElementById(elemento).style.display = "block";
    else
        document.getElementById(elemento).style.display = "none";
}