// fn.js v1.0 by Erik

var fn_debug = false;
var fn_url   = document.location.href;


function __fn_req() {
  // http://www.codepost.org/browse/snippets/59
  var types = [
    'Microsoft.XMLHTTP',
    'MSXML2.XMLHTTP.5.0',
    'MSXML2.XMLHTTP.4.0',
    'MSXML2.XMLHTTP.3.0',
    'MSXML2.XMLHTTP'
   ];

  for (var i = 0; i < types.length; i++) {
    try {
      return new ActiveXObject(types[i]);
    } catch(e) {}
  }

  try {
    return new XMLHttpRequest();
  } catch(e) {}

  return false; // XMLHttpRequest not supported
}


// convert a javascript variable to a php post argument
function __fn_topost(n, v) {
  var t = typeof v;
  
  if (t == 'number') {
    return '&'+n+'='+v;
  }

  else if (t == 'boolean') {
    if (v) {
      return '&'+n+'=1';
    } else {
      return '&'+n+'=0';
    }
  }

  else if (t == 'object') {
    if (v == null) { // null seems to be an object
      return '&'+n+'=null';
    } else if (v.constructor == Array) {
      var ret = '';
      var len = v.length;
      for (var i = 0; i < len; i++) {
        if (v[i] == null) { // __fn_topost(v[i]) will cause a warning
          ret += '&'+n+'['+i+']=null';
        } else {
          ret += __fn_topost(n+'['+i+']', v[i]);
        }
      }
      return ret;
    } else {
      return '&'+n+'='+escape(v.toString());
    }
  }

  /* this is not needed as the else case does teh same
  else if (t == 'function') {
    return '&'+n+'='+escape(v.toString());
  }*/
  
  else {
    return '&'+n+'='+escape(v);
  }
}


function __fn_call(debug, args) {
  var req =  __fn_req();

  if (typeof req == 'boolean') {
    if (debug) {
      alert('--fn-debug--\nXMLHttpRequest not supported');
    }
  } else {
    var post = 'fn='+args[0];
    var wret = 0;

    if ((typeof args[1] == 'function') ||
        (typeof args[1] == 'string')) {
      wret = 1; // we are interrested in the return value
    } else if (debug) {
      wret = 1;
    }
    
    post += '&ret=' + wret;

    // construct all the arguments
    var alen  = args.length;
    for (var i = 2; i < alen; i++) {
      post += __fn_topost(i-2, args[i]);
    }

    // need to do something with the return value?
    if (wret) {
      req.onreadystatechange = function() {
        if (req.readyState == 4) {
          if (req.status == 200) {
            if (debug) {
              var prnt = req.responseText;
              prnt = prnt.replace(/;/g, ';\n');
              prnt = prnt.replace(/\\n/g, '\n');
              alert('--fn-debug--responseText--\n'+prnt);
            }
            if (typeof args[1] == 'function') {
              var fn_ret;
              eval(req.responseText);
              args[1](fn_ret);
            } else if (typeof args[1] == 'string') {
              var fn_ret;
              eval(req.responseText);
              eval(args[1]);
            }
          } else {
            if (debug) {
              alert('--fn-debug--statusText--\n'+req.statusText);
            }
          }
        }
      };
    }

    if (debug) {
      alert('--fn-debug--post--\n'+post.replace(/&/g, '\n&'));
    }
    
    req.open('POST', fn_url, true);
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    req.send(post);
  }
}


function fn_call() {
  __fn_call(fn_debug, arguments);
}


function fn_call_debug() {
  __fn_call(true, arguments);
}

