//URLエンコード
function encodeURL(str){
    var s0 = "",i,s,u;
    //scan the source
    for(i=0;i<str.length;i++){
        s = str.charAt(i);
        //get unicode of the char
        u = str.charCodeAt(i);
        
        // SP should be converted to "+"
        if(s == " "){
            s0 += "+";
            
        }else{
            //check for escape
            //don't escape
            if(u == 0x2a || u == 0x2d || u == 0x2e || u == 0x5f || ((u >= 0x30) && (u <= 0x39)) || ((u >= 0x41) && (u <= 0x5a)) || ((u >= 0x61) && (u <= 0x7a))){
                s0 = s0 + s;
                
            //escape
            }else{
                //single byte format
                if((u >= 0x0) && (u <= 0x7f)){
                    s = "0"+u.toString(16);
                    s0 += "%"+ s.substr(s.length-2);
                    
                //quaternary byte format (extended)
                }else if(u > 0x1fffff){
                    s0 += "%" + (oxf0 + ((u & 0x1c0000) >> 18)).toString(16);
                    s0 += "%" + (0x80 + ((u & 0x3f000) >> 12)).toString(16);
                    s0 += "%" + (0x80 + ((u & 0xfc0) >> 6)).toString(16);
                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                    
                //triple byte format
                }else if(u > 0x7ff){
                    s0 += "%" + (0xe0 + ((u & 0xf000) >> 12)).toString(16);
                    s0 += "%" + (0x80 + ((u & 0xfc0) >> 6)).toString(16);
                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                    
                //double byte format
                }else{
                    s0 += "%" + (0xc0 + ((u & 0x7c0) >> 6)).toString(16);
                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                }
            }
        }
    }
    return s0;
}
