﻿

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

 
function stripHTML(str){
    var re = /(<([^>]+)>)/gi;
    str = str.replace(re, "");
    return str;
}



function converStrToParam (str) {
    var temp = stripHTML(str);
    temp = temp.toLowerCase().replace(/([.])/g,'');
    temp = temp.replace(/([ ])/g,'.');
    var result = '';
    var isDot = false;
    
    for (var i=0;i<temp.length;i++) {
        if ((temp.charAt(i) >= 'a' && temp.charAt(i) <= 'z') || (temp.charAt(i) >= '0' && temp.charAt(i) <= '9') || (temp.charAt(i) == '.')) {
            if (temp.charAt(i) == '.') {
                if (isDot == false) {
                    result = result + temp.charAt(i);
                }   
            } else {
                result = result + temp.charAt(i);
            }
            
            if (temp.charAt(i) == '.') {
                isDot = true;
            } else {
                isDot = false;
            }
        }
    }
    if (result.charAt(result.length - 1) == '.') {
        result = left(result, result.length - 1);
    }
    return result;
}



function MyURLDecode (clearString) {
    var output = '';
    var huruf = new Array(" ", "\"", "#", "$", "%", "&", "+", ",", "/", ":", ";", "<", "=", ">", "?", "@", "[", "\\", "]", "^", "`", "{", "|", "}", "~", String.fromCharCode(13), String.fromCharCode(10));            
    var hurufEncode = new Array("%20", "%22", "%23", "%24", "%25", "%26", "%2b", "%2c", "%2f", "%3a", "%3b", "%3c", "%3d", "%3e", "%3f", "%40", "%5b", "%5c", "%5d", "%5e", "%60", "%7b", "%7c", "%7d", "%7e", "%0d", "%0a");
            
    for (var j = 0; j < hurufEncode.length; j++) {
        while(clearString.indexOf(hurufEncode[j]) > -1)
            clearString = clearString.replace(hurufEncode[j], huruf[j]);
    }
    output = clearString;
    return output;
    
}

function MyURLEncode (clearString) {
    var output = '';
    
    var hurufEncode = new Array(" ", "\"", "#", "$", "&", "+", ",", "/", ":", ";", "<", "=", ">", "?", "@", "[", "\\", "]", "^", "`", "{", "|", "}", "~", String.fromCharCode(13), String.fromCharCode(10));            
    var huruf = new Array("%20", "%22", "%23", "%24", "%26", "%2b", "%2c", "%2f", "%3a", "%3b", "%3c", "%3d", "%3e", "%3f", "%40", "%5b", "%5c", "%5d", "%5e", "%60", "%7b", "%7c", "%7d", "%7e", "%0d", "%0a");

    for (var j = 0; j < hurufEncode.length; j++) {
        while(clearString.indexOf(hurufEncode[j]) > -1)
            clearString = clearString.replace(hurufEncode[j], huruf[j]);
    }
    output = clearString;
    return output;
}  



function countLengthTextArea(o,d,maxL){
	var n = o.value.length;
    var result = "";
    
    if (n <= maxL){
        try {
            d.innerHTML = (maxL - n) + " karakter tersedia";
        } catch(e) {
            d.innerText = (maxL - n) + " karakter tersedia";
        }
    } else {
        o.value = o.value.substring(0,maxL);
    }
}

function emailValidation(addr) {
    var emailReg = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    var regex = new RegExp(emailReg);
    return regex.test(addr);
}


function popupWindowImageEditor(strURL, intHeight, intWidth) {
    var newWindow;
    var intTop= (screen.height - intHeight) / 2;
    var intLeft= (screen.width - intWidth) / 2;
    var props = 'scrollBars=yes,resizable=yes,toolbar=no,menubar=no,location=no,directories=no,width='+intWidth+',height='+intHeight+',left='+intLeft+',top='+intTop+'';
    newWindow = window.open(strURL, "Lookup", props);
}

