public static function buildURLFromParts($url_parts, $normalize = false)
  {
    // Host has to be set aat least
    if (!isset($url_parts["host"]))
    {
      throw new Exception("Cannot generate URL, host not specified!");
    }
    
    if (!isset($url_parts["protocol"]) || $url_parts["protocol"] == "") $url_parts["protocol"] = "http://";
    if (!isset($url_parts["port"])) $url_parts["port"]= 80;
    if (!isset($url_parts["path"])) $url_parts["path"] = "";
    if (!isset($url_parts["file"])) $url_parts["file"] = "";
    if (!isset($url_parts["query"])) $url_parts["query"]= "";
    if (!isset($url_parts["auth_username"])) $url_parts["auth_username"]= "";
    if (!isset($url_parts["auth_password"])) $url_parts["auth_password"]= "";
    
    // Autentication-part
    $auth_part = "";
    if ($url_parts["auth_username"] != "" && $url_parts["auth_password"] != "")
    {
      $auth_part = $url_parts["auth_username"].":".$url_parts["auth_password"]."@";
    }
    
    // Port-part
    $port_part = ":" . $url_parts["port"];
    
    // Normalize
    if ($normalize == true)
    {
      if ($url_parts["protocol"] == "http://" && $url_parts["port"] == 80 ||
          $url_parts["protocol"] == "https://" && $url_parts["port"] == 443)
      {
        $port_part = "";
      }
      
      // Don't add port to links other than "http://" or "https://"
      if ($url_parts["protocol"] != "http://" && $url_parts["protocol"] != "https://")
      {
        $port_part = "";
      }
    }
    
    // If path is just a "/" -> remove it ("www.site.com/" -> "www.site.com")
    if ($url_parts["path"] == "/" && $url_parts["file"] == "" && $url_parts["query"] == "") $url_parts["path"] = "";
    
    // Put together the url
    $url = $url_parts["protocol"] . $auth_part . $url_parts["host"]. $port_part . $url_parts["path"] . $url_parts["file"] . $url_parts["query"];
    
    return $url;
  }