/**include
//_javascript/js-wrapper.js;
//_javascript/load-wrapper.js;
*/

var registration = function(aParam, winWr)
{
    this.winWr = winWr != undefined ? winWr : _wrapper;

    this.currentParam = aParam.shift();

    this.sElmId = this.currentParam.elm;

    if (this.currentParam.url != "") {
        this.loader = this.winWr.getLoadWrapper(this.currentParam.url);
    }


    if (aParam.length) {
        this.childElm = new registration(aParam);
    }

	this.winWr.setOnloadListener(this);
}


registration.prototype = {

	onload : function ()
	{
	    this.oElm = this.winWr.getElement(this.sElmId);
        if (this.currentParam.url.length) {
            this.listener();
    		this.loader.addListener(this, "ondataload", "onDataLoad");
    		this.loader.addListener(this, "ondataerror", "onDataError");
	    }
	},

	listener : function (json, text)
	{
	    this.oParentElm = this.winWr.getElmWrapper(this.oElm.elm.parentNode);
        this.oParentElm.show();

	    if (text == "ok") {
            var elm = this.winWr.doc.createElement("select");

            this.setAttributes(this.oElm.elm, elm);

            this.oParentElm.elm.replaceChild(elm, this.oElm.elm);
            this.oElm = this.winWr.getElmWrapper(elm);

            this.addOption(elm, 0, "");
            for(var i in json.state) {
    			this.addOption(elm, json.state[i].value, json[i].text);
    		}
        } else if (text == "none") {
            var elm = this.winWr.doc.createElement("input");
            this.setAttributes(this.oElm.elm, elm);
            elm.setAttribute("class", "inpText");
            elm.setAttribute("type", 'text');

            this.oParentElm.elm.replaceChild(elm, this.oElm.elm);
            this.oElm = this.winWr.getElmWrapper(elm);
            if (this.currentParam.url.length) {
                this.childElm.listener("", 'none');
            }
        }


        if (text == "ok" || text == "none" && this.currentParam.lable) {
            var lebel = this.winWr.getElement(this.currentParam.lable);
            lebel.write(json.name+':');
        }
        if (this.currentParam.url.length) {
            this.oElm.addListener(this, "onchange");
        }
	},

	onchange : function(evtWr)
	{
	    if (evtWr.elmWr.elm.value != 0) {
    		var fieldName = {};
    		fieldName['value'] = evtWr.elmWr.elm.value;
    		this.loader.send(fieldName);
	    }
	},

	onDataLoad : function(json, dom, text)
	{
        this.childElm.listener(json, text);
	},

	setAttributes : function(elmFrom, elmTo)
	{
        elmTo.setAttribute("id", elmFrom.getAttribute("id"));
        elmTo.setAttribute("tabindex", elmFrom.getAttribute("tabindex"));
        elmTo.setAttribute("name", elmFrom.getAttribute("name"));
	},

	onDataError : function(evtWr) {
		alert("Error");
	},

	addOption : function (htmlElement, value, text, defaultId)
	{
	    var opt = this.winWr.doc.createElement("option");
		opt.setAttribute("value", value);
		if (defaultId == value){
            opt.setAttribute("selected", "selected");
		}
		opt.appendChild(this.winWr.doc.createTextNode(text));
		htmlElement.appendChild(opt);
	}
}