
/* AjaxRequest class */

function AjaxRequest(url, method, args) {
  if(method == null) method = "POST";
  if(args == null) args = '';
  this._url = url;
  this._method = method;
  this.set_args(args);
}

AjaxRequest.prototype = {
  _url: '',
  _method: '',
  _args: '',
  _callback_func: '',
  _callback_args: '',
  _xmlhttp: null,
  _async: false,
  set_callback:
    function(func, args) {
      if(args == null) args = Array();
      this._callback_func = func;
      this._callback_args = args;
    },
  set_args:
    function(args) {
      if(object_type(args) == "array") {
        this._args = args.join("&");
      } else {
        this._args = args;
      }
    },
  xmlhttp_state_change:
    function () {
      if(this._xmlhttp.readyState != 4) return;
      if(this._callback_func) {
        var func_args = Array(this._xmlhttp.responseText == null ? '' : this._xmlhttp.responseText, this._xmlhttp.status);
        func_args.concat(this._callback_args);
        window[this._callback_func].apply(this, func_args);
      }
    },
  run:
    function(args) {
      if(this._xmlhttp == null) this.set_xmlhttp_object();
      if(args != null) this.set_args(args);
      if(this._async) {
        var ref = this;
        this._xmlhttp.onreadystatechange = function() {ref.xmlhttp_state_change()};
      } else {
        this._xmlhttp.onreadystatechange = null;
      }
      if(this._method == "POST") {
        this._xmlhttp.open("POST", this._url, this._async);
        this._xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        this._xmlhttp.send(this._args);
      } else {
        this._xmlhttp.open("GET", this._url + '?' + this._args, this._async);
        this._xmlhttp.send(null);
      }
      if(!this._async) {
        if(this._xmlhttp.status >= 400) {
          return null;
        }
        return this._xmlhttp.responseText == null ? '' : this._xmlhttp.responseText;
      }
      return null;
    },
  set_xmlhttp_object:
    function() {
      if(window.XMLHttpRequest) {
        this._xmlhttp = new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        this._xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
    },
  set_async:
    function(async) {
      if(async == null) async = true;
      this._async = async;
    }
};

function split_ajax_into_array(result) {
  var newarray = Array();
  if(result) {
    var lines = result.split("\n");
    for(var i=0;i<lines.length;i++) {
      newarray.push(lines[i].split("\t"));
    }
  }
  return newarray;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
//  traceback = stack_trace(arguments.callee);
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

var $ = MM_findObj;

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function set_field_value(field, val) {
  var obj = MM_findObj(field);
  if(obj) {
    if(obj.type == null) {
      if(obj[0] != null && obj[0].type != null) {
        var array_len = obj.length;
        for(var i=0;i<array_len;i++) {
          if(obj[i].value == val) {
            obj[i].checked = true;
            break;
          }
        }
      } else {
        obj.innerHTML = val;
      }
    } else if(obj.type == "text" || obj.type == "hidden" || obj.type == "textarea") {
      obj.value = val;
    } else if(obj.type.substr(0, 6) == "select") {
      for(var i=0;i<obj.options.length;i++) {
        if(obj.options[i].value == val) {
          obj.options[i].selected = true;
          break;
        }
      }
    }
  }
}

function get_field_value(field) {
  var obj = MM_findObj(field);
  if(obj) {
    if(obj.type == null) {
      if(obj[0] != null && obj[0].type != null) {
        // This looks like a radio button
        var array_len = obj.length;
        for(var i=0;i<array_len;i++) {
          if(obj[i].checked) {
            return obj[i].value;
          }
        }
      } else {
        return obj.innerHTML;
      }
    } else if(obj.type == "text" || obj.type == "hidden" || obj.type == "textarea" || obj.type == "password") {
      return obj.value;
    } else if(obj.type.substr(0, 6) == "select") {
      if(obj.options.length) {
        return obj.options[obj.selectedIndex].value;
      }
    }
  }
  return null;
}

function object_type(object) {
    var s = typeof object;
    if(s === 'object') {
      if(object) {
        if(object instanceof Array) {
          s = 'array';
        }
      } else {
        s = 'null';
      }
    }
    return s;
}

function field_focus(field) {
  if($(field + '_label')) {
    $(field + '_label').style.display = 'none';
  }
}

function field_blur(field) {
  if($(field + '_label')) {
    $(field + '_label').style.display = get_field_value(field) ? 'none' : 'block';
  }
}

function create_element(etype, attr, childtext) {
  var element = document.createElement(etype);
  if(attr) {
    for(var i=0;i<attr.length;i++) {
      element.setAttribute(attr[i][0], attr[i][1]);
    }
  }
  if(childtext) {
    element.appendChild(document.createTextNode(childtext));
  }
  return element;
}

function show_link_to_this_page() {
  $('link_to_this_page').style.display = $('link_to_this_page').style.display == 'block' ? 'none' : 'block';
}
