/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 *
 *
 * by: pls
 */


//funcoes js...

function $_(obj){
    return document.getElementById(obj);
}
function trim(str) {
    var newStr = new String(str);
    var achou = false;

    var i = newStr.length-1;
    for(; i>=0; i--) {
        if( newStr.charAt(i)!=' ' && newStr.charCodeAt(i)!=9 && newStr.charCodeAt(i)!=13 && newStr.charCodeAt(i)!=10 ) {
            achou = true;
            break;
        }
    }
    if( achou ) {
        newStr = newStr.substring(0, i+1);
    }

    achou = false;
    for(i=0; i<newStr.length; i++) {
        if( newStr.charAt(i)!=' ' && newStr.charCodeAt(i)!=9 && newStr.charCodeAt(i)!=13 && newStr.charCodeAt(i)!=10 ) {
            achou = true;
            break;
        }
    }
    if( achou ) {
        newStr = str.substring(i, newStr.length);
    } else {
        newStr = '';
    }

    return newStr;
}
// isDataValida(string)>> valida data dd/mm/aaaa
function isDataValida(string){
    try{
        if (string.length < 10){
                return false;
        }else{
            var strData = new Array();
            strData = string.split("/");

            // patch pro bug do javascript
            if(strData[0] == "09" || strData[0] == "08"){
                strData[0] = "01";
            }
            if(strData[1] == "08"){
                strData[1] = "01";
            }
            if(strData[1] == "09"){
                strData[1] = "03";
            }

            if(strData.length != 3){
                return false;
            }else if(parseInt(strData[0]) > 29 && parseInt(strData[1]) == 2 ){
                return false;
            }else if(parseInt(strData[0]) > 31 || parseInt(strData[0]) <= 0 || strData[0].length != 2){
                return false;
            }else if(parseInt(strData[0]) == 31 && (parseInt(strData[1]) == 4 || parseInt(strData[1]) == 6 || parseInt(strData[1]) == 9 || parseInt(strData[1]) == 11) ){
                return false;
            }else if(parseInt(strData[1]) > 12 || parseInt(strData[1]) <= 0 || strData[1].length != 2){
                return false;
            }else if(parseInt(strData[2]) <= 0 || strData[2].length != 4 ){
                return false;
            }else{
                return true;
            }
        }
    }catch(err){
        alert(err.description);
        return false;
    }
}
//valida email
function isEmail( email ) {
    if (typeof(email) != "string") {
        return false;
    } else if (!email.match(/^[A-Za-z0-9]+([_.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_.-][A-Za-z0-9]+)*\.[A-Za-z0-9]{2,4}$/)) {
        return false;
    }else{
        return true;
    }
}

function validaNumero(campo, event){

    var strValidos = '0123456789';
    var BACKSPACE = 8;
    var key;
    var tecla;
    CheckTAB = true;
    if(navigator.appName.indexOf('Netscape')!= -1)
    tecla= event.which;
    else tecla = event.keyCode;

    key = String.fromCharCode(tecla);
    if (tecla == "") return true;
    if (tecla == 13) return false;
    if (tecla == BACKSPACE) return true;
    if (strValidos.indexOf(key) == -1) return false;
}

//limpa as tags html da string
function stripHTML(str){
    var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
    str = str.replace(regexp,"");
    str = str.replace(/&nbsp;/g," ");
    return str;
}

//de float( 1000.00) para moeda (1.000,00)
function float2moeda(num){
    x = 0;
    if(num<0){
        num = Math.abs(num);
        x = 1;
    }
    if(isNaN(num)) num = "0";
    cents = Math.floor((num*100+0.5)%100);
    num = Math.floor((num*100+0.5)/100).toString();
    if(cents < 10) cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
        num = num.substring(0,num.length-(4*i+3))+'.'+num.substring(num.length-(4*i+3));
    ret = num + ',' + cents;
    if (x == 1) ret = ' - ' + ret;return ret;
}

//de moeda(1.000,00) para float (1000.00)
function moeda2float(moeda){
   moeda = moeda.replace(/\./g,"");
   moeda = moeda.replace(/\,/g,".");
   return parseFloat(moeda);
}