function AjaxRequest(url, callback_function) {
 			var req = null;
 			var msxmlhttp = new Array(
				'Msxml2.XMLHTTP.5.0',
				'Msxml2.XMLHTTP.4.0',
				'Msxml2.XMLHTTP.3.0',
				'Msxml2.XMLHTTP',
				'Microsoft.XMLHTTP');
			for (var i = 0; i < msxmlhttp.length; i++) {
				try {
					req = new ActiveXObject(msxmlhttp[i]);
				} catch (e) {
					req = null;
				}
			}
 			
			if(!req && typeof XMLHttpRequest != "undefined") {
				req = new XMLHttpRequest();
      }
      if (req != null) {
        req.open("POST", url, true);
        req.onreadystatechange = function() {
          if ((req.readyState == 4) && (req.status == 200)) {
            var json = req.responseText.parseJSON();
            var callback = callback_function + '(json)';
            eval(callback);
          }
        }
        req.send(null);
        delete req;
      }
			return true;
}

function UpdateForm(objects) {
  document.forms['add_frm']['fetch_meta'].disabled = false;
  for (var i in objects) {
    if (i != "toJSONString") {
      if (document.forms['add_frm'][i]) {
        document.forms['add_frm'][i].value = objects[i];
      }
    }
  }
}

function FetchMeta() {
  var url = document.forms['add_frm']['url'].value
  document.forms['add_frm']['fetch_meta'].disabled = true;
  AjaxRequest('../ajax.php?cmd=fetch_meta&url=' + url, 'UpdateForm');
}

function GetFields(fieldsbox) {
  var tbl = fieldsbox.options[fieldsbox.selectedIndex].value;
  AjaxRequest('../ajax.php?cmd=get_fields&tbl=' + tbl, 'UpdateField');
}

function UpdateField(objects) {
  var fieldsbox = document.forms['qry_frm'].select_field;
  fieldsbox.options.length = 0;
  for (var i in objects) {
    if (i != "toJSONString") {
      var obj_option = new Option(objects[i], objects[i]);
      fieldsbox.options.add(obj_option);
    }
  }
}

function GetRecipientFilterFields(fieldsbox) {
  var tbl = fieldsbox.options[fieldsbox.selectedIndex].value;
  if (tbl.indexOf("webmaster") != -1) {
    tbl = "idx_link";
  }
  else if (tbl.indexOf("mailing_list_subscriber") != -1) {
    tbl = "idx_mailing_list";
  }
  else if (tbl.indexOf("registered_user") != -1) {
    tbl = "idx_users";
  }
  else if (tbl.indexOf("editor") != -1) {
    tbl = "idx_users";
  }
  else {
    tbl = "";
  }
  AjaxRequest('../ajax.php?cmd=get_fields&tbl=' + tbl, 'UpdateField');
}
