// -*- Mode: ecmascript; indent-tabs-mode: nil; Coding: utf-8 -*-

AttachBBS = function() {
    this.baseURI = 'http://attachBBS.pqrs.org/ajax.bbs.php?';
};

AttachBBS.prototype.getUniqStamp = function() {
    return (new Date()).getTime();
};

AttachBBS.prototype.asyncLoad = function(src) {
    var s = document.createElement('script');
    s.src = src;
    s.type = 'text/javascript';
    s.charset = "UTF-8";
    document.body.appendChild(s);
};

AttachBBS.prototype.onLoad = function(outerElemID, bbskey, ownername, showAllFlag) {
    if (ownername == undefined) ownername = 'guest';
    if (showAllFlag == undefined) showAllFlag = false;

    var enc_bbskey = encodeURIComponent(bbskey);
    var enc_ownername = encodeURIComponent(ownername);
    var enc_outerElemID = encodeURIComponent(outerElemID);
    var pars = 'bbskey=' + enc_bbskey + '&ownername=' + enc_ownername + '&outerElemID=' + enc_outerElemID;
    if (showAllFlag == true) {
        pars += '&showAllFlag=true';
    }
    // add dummy uniq ID to override cache.
    pars += '&dummy=' + this.getUniqStamp();

    this.asyncLoad(this.baseURI + pars);
};

AttachBBS.prototype.getForm = function(formElemID) {
    var pars = '';

    var elem = document.getElementById(formElemID);
    if (elem == null) return pars;

    var listTagName = ['input', 'textarea'];
    for (var i = 0; i < listTagName.length; ++i) {
        var list = elem.getElementsByTagName(listTagName[i]);
        for (var j = 0; j < list.length; ++j) {
            if (pars != '') pars += '&';
            pars += encodeURIComponent(list[j].name) + '=' + encodeURIComponent(list[j].value);
        }
    }

    return pars;
};

AttachBBS.prototype.refresh = function(formElemID, pars) {
    var url = this.baseURI + this.getForm(formElemID) + pars + '&dummy=' + this.getUniqStamp();
    this.asyncLoad(url);
};

attachBBS = new AttachBBS();
