function hjsTrim(str)
{
        return str.replace(/(^\s*)|(\s*$)/g,"");
}


function hjsUrlEncode(str)
{
        var ret="";
        var strSpecial='!"#$%&\'()*+,/:;<=>?[]^`{|}~%"';
        for(var i=0;i<str.length;i++){
                var chr = str.charAt(i);
                var c=chr.charCodeAt(0).toString(16);;
                if(parseInt("0x"+c) > 0x7f){
                        ret+="%"+c.slice(0,2)+"%"+c.slice(-2);
                } 
                else {
                        if(chr==" ")
                                ret+="+";
                        else if(strSpecial.indexOf(chr)!=-1)
                                ret+="%"+c.toString(16);
                        else
                                ret+=chr;
                }
        }
        return ret;
}


function hjsUrlDecode(str){
        var ret="";
        for(var i=0;i<str.length;i++){
                var chr = str.charAt(i);
                if(chr == "+"){
                        ret+=" ";
                }else if(chr=="%"){
                        var asc = str.substring(i+1,i+3);
                        ret+= String.fromCharCode(parseInt("0x"+asc));
                        i+=2;
                }else{
                        ret+= chr;
                }
        }
        return ret;
}


function _hjsGetQueryString()
{
        var pos = 0;
        var str = "" + document.location.href;

        pos = str.indexOf('?');
        if (-1 == pos) {
                return "";
        }

        return "&" + str.substr(pos + 1) + "&";
}


function hjsGetVal(name) 
{
        var begin = 0;
        var end = 0;
        var nm = "" + name;
        var qstr = _hjsGetQueryString(); 

        if ("" == nm || "" == qstr) {
                return "";
        }

        nm = "&" + nm + "=";

        begin = qstr.search(new RegExp(nm));
        if (-1 == begin) {
                return "";
        }

        qstr = qstr.substr(begin + nm.length);

        end = qstr.indexOf('&');
        if (-1 == end) {
                return "";
        }

        return hjsUrlDecode(qstr.substring(0, end));
}


function hjsGetValTrim(name) 
{
        return hjsTrim(hjsGetVal(name));
}


function hjsGetValInt(name)
{
        var num = parseInt(hjsGetValTrim(name));  
        if (isNaN(num)) {
                return 0;
        }    
        
        return num;
}


function hjsToUtf8(code)
{
        var iByte=0;
        var i=0;
        result="";
        while(code>0x7f) {
                iByte=code%0x40;
                code=(code-iByte)/0x40;
                result="%"+(iByte|0x80).toString(16).toUpperCase()+result;
                i++;
        }
        prefix=[0x0,0xc0,0xe0,0xf0,0xf8,0xfc];
        if(i>prefix.length) {
                i=5;
        }
        result=""+(code|prefix[i]).toString(16).toUpperCase()+result;
        return result;
}