/* 
 * 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] = "04";
            }

            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;
    }

    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;
}

function in_array (needle, haystack, argStrict) {
    var key = '', strict = !!argStrict;

    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }

    return false;
}


function valInt(teclapres,campo){
    // chamada da função: onKeyPress="return valInt(event,'campo');"
    var tecla = teclapres.keyCode;
    if (tecla > 31 && (tecla < 48 || (tecla > 57 && tecla<96 || (tecla>105)))){
        return false;
    }

    return true;
}

function formata(objForm, strField, sMask, evtKeyPress){
    var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;
    if(document.all) {
        nTecla = evtKeyPress.keyCode;
    }else if(document.layers){
        nTecla = evtKeyPress.which;
    }
    sValue = objForm[strField].value;
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( " ", "" );
    sValue = sValue.toString().replace( " ", "" );
    sValue = sValue.toString().replace( ":", "" );
    fldLen = sValue.length;
    mskLen = sMask.length;
    i = 0;
    nCount = 0;
    sCod = "";
    mskLen = fldLen;
    while (i <= mskLen){
        bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/") || (sMask.charAt(i) == ":"))
        bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))
        if (bolMask) {
            sCod += sMask.charAt(i);
            mskLen++;
        }else{
            sCod += sValue.charAt(nCount);
            nCount++;
        }
        i++;
    }
    objForm[strField].value = sCod;
    if (nTecla != 8){
        if (sMask.charAt(i-1) == "9"){
            return ((nTecla > 47) && (nTecla < 58));
        }else{
            return true;
        }
    }else{
        return true;
    }
}

 
 function validarCPF(cpf){
   //var cpf = document.cadastro.cpf.value;
//   var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
//   if(!filtro.test(cpf)){
//	 return false;
//   }

   cpf = replaceAll(cpf, ".", "");
   cpf = replaceAll(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 replaceAll(string, token, newtoken) {
	while (string.indexOf(token) != -1) {
 		string = string.replace(token, newtoken);
	}
	return string;
}

