/***
 * Excerpted from "Advanced Rails Recipes",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material,
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose.
 * Visit http://www.pragmaticprogrammer.com/titles/fr_arr for more book information.
***/
function checkPresence(field) {
    var hint = $F(field).length == 0 ? "Try again!" : "Right on!";
    if ($(field + '_hint')) {
        $(field + '_hint').update(hint);
    } else {
        content = '<span class="validation" id="' + field + '_hint">' +
        hint + '</span>';
        new Insertion.After(field, content);
    }
}
//
//
// // Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
<!--        Script by hscripts.com          -->
<!--        copyright of HIOX INDIA         -->
<!-- Free javascripts @ http://www.hscripts.com -->


checked=false;
function checkedAll () {

    if (checked == false)
    {
        checked = true
    }
    else
    {
        checked = false
    }
    var inputs = document.getElementsByTagName('input');
    for (var i = 0; i < inputs.length; i++)
    {
        if (inputs[i].type == 'checkbox') {

            inputs[i].checked = checked;

        }
    }

}

<!-- Script by hscripts.com -->

function auto_complete_on_select(element, selectedElement)
{
	 
	
    var entityParts = selectedElement.id.split('::');
    var entityType = entityParts[0];
    var entityId   = entityParts[1];
	 
	   	  
    document.getElementById(entityType).value = entityId;
}


function valida(numero) {
    if(validaCPF(numero) || validaCNPJ(numero)) {
        return true;
    }
    else {
        alert("Digite CPF ou CNPJ válidos\n Com hífens e pontos!")
        return false;
    }

}

function validaCPF(cpf) {
    var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
    if(!filtro.test(cpf)){
        return false;
    }

    cpf = remove(cpf, ".");
    cpf = remove(cpf, "-");

    if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
        cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
        cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
        cpf == "88888888888" || cpf == "99999999999"){
        return false;
    }

    soma = 0;
    for(i = 0; i < 9; i++)
        soma += parseInt(cpf.charAt(i)) * (10 - i);
    resto = 11 - (soma % 11);
    if(resto == 10 || resto == 11)
        resto = 0;
    if(resto != parseInt(cpf.charAt(9))){
        return false;
    }
    soma = 0;
    for(i = 0; i < 10; i ++)
        soma += parseInt(cpf.charAt(i)) * (11 - i);
    resto = 11 - (soma % 11);
    if(resto == 10 || resto == 11)
        resto = 0;
    if(resto != parseInt(cpf.charAt(10))){
        return false;
    }
    return true;
}

function remove(str, sub) {
    i = str.indexOf(sub);
    r = "";
    if (i == -1) return str;
    r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
    return r;
}

function validaCNPJ(CNPJ) {
    erro = new String;
    if (CNPJ.length != 18) {
        erro += "E' necessarios preencher corretamente o numero do CNPJ! \n\n";
    }
    if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
        if (erro.length == 0) erro += "E' necessarios preencher corretamente o numero do CNPJ! \n\n";
    }
    //substituir os caracteres que nao sao numeros
    if(document.layers && parseInt(navigator.appVersion) == 4){
        x = CNPJ.substring(0,2);
        x += CNPJ.substring(3,6);
        x += CNPJ.substring(7,10);
        x += CNPJ.substring(11,15);
        x += CNPJ.substring(16,18);
        CNPJ = x;
    } else {
        CNPJ = CNPJ.replace(".","");
        CNPJ = CNPJ.replace(".","");
        CNPJ = CNPJ.replace("-","");
        CNPJ = CNPJ.replace("/","");
    }
    var nonNumbers = /\D/;
    if (nonNumbers.test(CNPJ)) erro += "A verificacao de CNPJ suporta apenas numeros! \n\n";
    var a = [];
    var b = new Number;
    var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
    for (i=0; i<12; i++){
        a[i] = CNPJ.charAt(i);
        b += a[i] * c[i+1];
    }
    if ((x = b % 11) < 2) {
        a[12] = 0
    } else {
        a[12] = 11-x
    }
    b = 0;
    for (y=0; y<13; y++) {
        b += (a[y] * c[y]);
    }
    if ((x = b % 11) < 2) {
        a[13] = 0;
    } else {
        a[13] = 11-x;
    }
    if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
        erro +="Digito verificador com problema!";
    }
    if (erro.length > 0){
        return false;
    }
    return true;
}


// -------------------------
// Multiple File Upload
// -------------------------
function MultiSelector(list_target, max) {
    this.list_target = list_target;this.count = 0;this.id = 0;if( max ){
        this.max = max;
    } else {
        this.max = -1;
    };this.addElement = function( element ){
        if( element.tagName == 'INPUT' && element.type == 'file' ){
            element.name = 'attachment[file_' + (this.id++) + ']';element.multi_selector = this;element.onchange = function(){
                var new_element = document.createElement( 'input' );new_element.type = 'file';this.parentNode.insertBefore( new_element, this );this.multi_selector.addElement( new_element );this.multi_selector.addListRow( this );this.style.position = 'absolute';this.style.left = '-1000px';
            };if( this.max != -1 && this.count >= this.max ){
                element.disabled = true;
            };this.count++;this.current_element = element;
        } else {
            alert( 'Error: not a file input element' );
        };
    };this.addListRow = function( element ){
        var new_row = document.createElement('li');var new_row_button = document.createElement( 'a' );new_row_button.title = 'Remover Arquivo';new_row_button.href = '#';new_row_button.innerHTML = '<img src="/stylesheets/images/icon_delete.gif" />';new_row.element = element;new_row_button.onclick= function(){
            this.parentNode.element.parentNode.removeChild( this.parentNode.element );this.parentNode.parentNode.removeChild( this.parentNode );this.parentNode.element.multi_selector.count--;this.parentNode.element.multi_selector.current_element.disabled = false;return false;
        };new_row.innerHTML = element.value.split('/')[element.value.split('/').length - 1];new_row.appendChild( new_row_button );this.list_target.appendChild( new_row );
    };
}

