function validbi(bi, check){
    values = new Array(bi.length);
    temp = bi;
    sum = 0;
    for(a = 0; a< bi.length; a++){
        values[a] = temp % 10;
        temp = Math.floor(temp / 10);
        
    }
    mult    = 2;
    for(j = 0; j < bi.length; j++){
        
        sum += (values[j] * mult);
        mult++;
    }
    if(check > 0){
        var total = Number(sum)+Number(check);
        if(total % 11 == 0){
            return true
        }else{
            return false
        }
    }else{
        if((sum % 11 == 0) || ((sum + 10) % 11 == 0)){
            return true;
        }else{
            return false
        }
    }   
}

function validaNum(num) {
    var pos;
    var val = 0;

    // computar soma de controlo 
    for(pos = 0; pos < num.length-1; pos++)
        val += num[pos] * (9 - pos);

    val = val % 11 ? (11 - val % 11) % 10 : 0;

    // verificar soma de controlo 
    return val == num[pos];
}

function validcontribuinte(num) {
        var ret = false;
        var arr = new Array(num);
        arr = num.split('');
        // verificar validade do caracter inicial do NIF 
        switch(arr[0]) {
            case '1': case '2': case '5':
            case '6': case '8': case '9':
            //verificar validade do NIF 
            return validaNum(arr);
        }       
        return false;
}

function Trim(TRIM_VALUE){
    if(TRIM_VALUE.length < 1){
        return "";
    }
        TRIM_VALUE = RTrim(TRIM_VALUE);
        TRIM_VALUE = LTrim(TRIM_VALUE);
    if(TRIM_VALUE==""){
        return "";
    }
    else{
        return TRIM_VALUE;
    }
} //End Function

function RTrim(VALUE){
    var w_space = String.fromCharCode(32);
    var v_length = VALUE.length;
    var strTemp = "";
    if(v_length < 0){
        return"";
    }
    var iTemp = v_length -1;
            
    while(iTemp > -1){
        if(VALUE.charAt(iTemp) == w_space){
        }
        else{
            strTemp = VALUE.substring(0,iTemp +1);
            break;
        }
        iTemp = iTemp-1;
            
    } //End While
    return strTemp;
} //End Function

function LTrim(VALUE){
    var w_space = String.fromCharCode(32);
    if(v_length < 1){
        return"";
    }
    var v_length = VALUE.length;
    var strTemp = "";
    var iTemp = 0;
    while(iTemp < v_length){
        if(VALUE.charAt(iTemp) == w_space){
        }
        else{
            strTemp = VALUE.substring(iTemp,v_length);
            break;
        }
        iTemp = iTemp + 1;
    } //End While
    return strTemp;
} //End Function

function copyfieldvalues(fieldorigin, fielddestination) {
    var name_f = document.getElementById(fieldorigin);
    var name_m = document.getElementById(fielddestination);
    if(checkcopyaddress.checked) {
        name_m.value = name_f.value;
    } else {
        name_m.value = '';
    }
}

function validemail(fieldname) {
    var email = document.getElementById(fieldname).value;
    var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
    if(!email.match(emailExp)) {
        return false;         
    }
    return true;
}

function validphone(fieldname) {
    var phone = document.getElementById(fieldname).value;
    var regexp = /^[0-9]{9}$/;
    if(!phone.match(regexp)) {
        return false;         
    }
    return true;
}

function checkedbox(fieldname) {
    var field = document.getElementById(fieldname);
    if(!field.checked) {
        return false;
    }
    return true;
}

function validcp1(value) {
    if(value.length!=4)
        return false;
    if(value<1000 || value>9999)
        return false;
    return true;
}

function validcp2(value) {
    if(value.length!=3)
        return false;
    if(value<000 || value>999)
        return false;
    return true;
}

function validlocalidade(value) {
    if(value.length<2)
        return false;
    return true;
}

function validphone(value) {
    if (value.length<9)
        return false;
    return true;
}

function validemail(value) {
    var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
    if(!value.match(emailExp))
        return false;   
    return true;
}
