
var Kwo = {

  "toHash": function() {
    var ret = $H({}), n = arguments.length, arg;
    for (var i = 0; i < n; i++) {
      arg = arguments[i];
      if (arg === undefined || arg === null) continue;
      if ("string" == typeof arg) { 
        arg = arg.toQueryParams(); 
      }
      else if ("object" == typeof arg && "tagName" in arg) { 
          if (arg.tagName == "FORM") { 
            arg = $(arg).serialize(true); 
          }
          else if ($(arg)) { 
            var tmp = $(arg).up("form");
            if (tmp) arg = tmp.serialize(true);
          }
      }
      ret.merge(arg);
    }
    return ret;
  },

  "exec": function (action, args, options) {
    options = options || {};

    if ("confirm" in options) { 
      var msg;
      if ("object" == typeof options["confirm"] && "tagName" in options["confirm"]) {
        msg = $(options["confirm"]).getAttribute("confirm");
      }
      else {
        msg = options["confirm"];
      }
      if (msg.length >= 2 && !confirm(msg.ucfirst())) { return; }
    }

    var params = Kwo.toHash(args, {__token: Math.random()});
    if ("prompt" in options) {
      var tmp = {}; 
      tmp[options["prompt"]["key"]] = prompt(options["prompt"]["msg"].ucfirst(), 
                                             options["prompt"]["default"] || "");
      if (tmp[options["prompt"]["key"]] == null) return ;
      params.merge(tmp);
    }

    if ("container" in options && $(options["container"])) {
      if ($($(options["container"]).parentNode).hasClassName("deck")) {
        $(options["container"]).parentNode.raise(options["container"]);
      }
      params["of"] = "null";
      new Ajax.Updater(options["container"], action, {"parameters": params, "evalScripts": true});
      return ;
    }

    var opt = {
      requestHeaders: {"X-KWO-Referer": window.location.href},
      asynchronous: options.async || true,
      parameters: params,
      onCreate: function() { 
        if (window.top.$("loading")) { window.top.$("loading").show(); }
        if ("toggle" in options) { $(options.toggle).toggle(); }
      },
      onSuccess: function(t) {
        eval(t.responseText);
        if ("reset" in options) {
          var f = $(args).up("form");
          if (f) f.reset();
        }
      },
      on404: function(t) { 
        Kwo.warn("AJAX 404 : "+t.statusText+" was not found"); 
      },
      onFailure: function(t) { 
        Kwo.warn("AJAX Failure ["+t.status+"] : "+t.statusText); 
      },
      onException: function(t, e) { 
        Kwo.warn("AJAX Exception ["+e.name+"] : "+e.message); 
      },
      onComplete: function(t) {
        if (window.top.$("loading")) { window.top.$("loading").hide(); }
        if ("toggle" in options) { $(options.toggle).toggle(); }
      }
    };
    new Ajax.Request(action, opt);
  },

  "get": function(action, args, options) {
    alert("to implement");
  },

  "go": function(action, args, options) {
    var url = action;
    options = options || {};
    
    if ("confirm" in options) { 
      var msg;
      if ("object" == typeof options["confirm"] && "tagName" in options["confirm"]) {
        msg = $(options["confirm"]).getAttribute("confirm");
      }
      else {
        msg = options["confirm"];
      }
      if (msg.length >= 2 && !confirm(msg.ucfirst())) { return; }
    }


    if (args !== undefined && args != null) {
       url = action+"?"+Kwo.toHash(args, {"ie": "utf8"}).toQueryString();
    }

    if ("open_right_flag" in options && options.open_right_flag == 1) {
      _right_iframe.location = url;
      return ; 
    }

    if ("target" in options) {
      if (options["target"] == "blank") {
        window.open(url);
      }
      else {
        $(options["target"]).src = url;
      }
      return ;
    }

    if ("popup" in options && 
        window.location.href.lastIndexOf("__popup") == -1) {
          if ("object" == typeof options.popup && "blank" in options.popup) {
            window.open(url);
            return;
          }

          if("width" in options)
						var width = options.width;
					else
						var width = window._scheme ? "780" : "400";
					
          if("height" in options)
						var height = options.height;
					else
          	var height = "550";

          var popup_name = "_blank";
          if (url.indexOf("?") == -1) { url = url+"?"; }
          else { url = url+"&"; }
          url = url+"__popup="+popup_name;
          window.open(url, popup_name,
                      "width="+width+",height="+height+",directories=no,"+
                      "status=no,menubar=no,scrollbars=yes,"+
                      "resizable=no,copyhistory=no,hotkeys=no,"+
                      "toolbar=no,location=no");
          return;
        }
    else {
      window.location.assign(url); 
    }
    return false;
  },

  "home": function() {
    window.location.assign("/"); 
  },

  "reload": function(action) {
    window.location.reload(); 
  },

  "update": function(action, args, container) {
    container = container || top.$("container");
    new Ajax.Updater(container, action, {"parameters": Kwo.toHash(args)});
  },

  "openMessage": function(code) {
    Kwo.go("/sys/msg.view", {"code": code}, {"popup": true});
  }, 

  "warn": function(msg) {
    alert(msg.ucfirst()); 
  }

};

Object.extend(String.prototype, { 

  "preg_match": function(pattern, option) {
    if (option === undefined) option = "gi";
    return this.match(new RegExp(pattern, option));
  },

 "preg_replace": function(pattern, replacement, option) {
   if (option === undefined) option = "gi";
   return this.replace(new RegExp(pattern, option), replacement);
 },

 "between": function(min, max) { 
   var n = parseInt(this);
   return (n > min && n < max);
 },

 "toInt": function() {
   return parseInt(this);
 },

 "ucfirst": function() {
   return this.charAt(0).toUpperCase() + this.substring(1);
 }

});

function $$F(str){ // recupère l'objet radio sélectionné
	var o = ret = false;
	elts = $$(str);
	if(typeof elts != 'object') return false;
	elts.each( function(e){ if(!ret){ if(e.checked){ ret = true; o = e; } }else return ret; } );
	return o;
}
