// fungsi cekKar dipakai pada fungsi dodacheck dan docheck
function cekKar(val,k) {
var strPass = val.value;
var strLength = strPass.length;
var lchar = val.value.charAt((strLength) - 1);
if(lchar.search(k) != -1) {
var tst = val.value.substring(0, (strLength) - 1);
val.value = tst;
   }
 }
// cek input hp
var krInvA = /[$\ \@\\\#%\^\&\*\(\)\/\[\]\+\_\{\}\`\~\=\|\"\'\;\:\,\.\!\?\<\>\-\a-z\A-Z]/;
function dodacheck(vA) {
cekKar(vA,krInvA);
}
// cek input website
var krWeb = /[$\ \@\#%\^\*\(\)\[\]\+\{\}\`\~\|\"\'\;\,\!\<\>]/;
function cekWeb(vW) {
cekKar(vW,krWeb);
}
// cek input username
var krInvB = /[$\@\\\#%\^\&\*\(\)\/\[\]\+\_\ \{\}\`\~\=\|\"\'\;\:\,\.\!\?\<\>\-]/;
function docheck(vB) {
cekKar(vB,krInvB);
}
// cek input juduliklan dan pengirim
var krJdlPgrm = /[@\\\#\^\&\*\/\[\]\+\_\{\}\.\`\~\=\|\"\'\;\<\>\-]/;
function cekJdlPgrm(vB) {
cekKar(vB,krJdlPgrm);
}
// cek input username
var krUserNm = /[$\@\\\#%\^\&\*\(\)\/\[\]\+\_\ \{\}\`\~\=\|\"\'\;\:\,\.\!\?\<\>\-]/;
function cekUserName(vB) {
cekKar(vB,krUserNm);
}

// cek input isiiklan
var mikExp= /[`\~\|\<\>]/; // karakter invalid
function textCounter(field, countfield, maxlimit) {
var strPass = field.value;
var strLength = strPass.length;
var lchar = field.value.charAt((strLength) - 1);
if (field.value.length > maxlimit ||lchar.search(mikExp) != -1 ) 
{ // pembatasan jumlah karakter
field.value = field.value.substring(0, maxlimit);
var tst = field.value.substring(0, (strLength) - 1);
field.value = tst;
} 
else 
{
var sisa =	maxlimit - field.value.length;
var persen = round_decimals(((field.value.length/maxlimit)*100), 1);
countfield.value = "Sisa karakter "+sisa+" (maks. "+maxlimit+"). Pemakaian "+persen+"%"; // jika karakter valid maka hitung sisa 
}
}

function textCount(f,counter,makslimit,linecounter) {
 // text width//
 var fieldWidth =  parseInt(f.offsetWidth);
 var charcnt = f.value.length;        
 // trim the extra text
 if (charcnt > makslimit) { 
  f.value = f.value.substring(0, makslimit);
 }
 else { 
 // progress bar percentage
 var percentage = parseInt(47 - (( makslimit - charcnt) * 47)/makslimit) ;
 document.getElementById(counter).style.width = parseInt(fieldWidth*percentage)/120+"px";
 //document.getElementById(counter).innerHTML=percentage+"%"
 // color correction on style from CCFFF -> CC0000
 setcolor(document.getElementById(counter),percentage,"background-color");
 }
}
function setcolor(obj,percentage,prop){
 obj.style[prop] = "rgb(80%,"+(100-percentage)+"%,"+(100-percentage)+"%)";
}

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}
function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}

