﻿Array.prototype.remove=function(s){
  for(i=0;i<this.length;i++){
    if(s==this[i]) this.splice(i, 1);
  }
}

var forms = {
    'hiddenObj' : 'hidden',
    'hideOpen' : true,
    'openeditors' : [],
    'appendObject' : false,
    'closeAll' : function()
    {
        for(var i=0;i<forms.openeditors.length;i++)
        {
            forms.openeditors[i].revert();
        }
    },
    'insertAfter' : function (newElement,targetElement) 
    {
        if (forms.hideOpen)
            targetElement.style.display = 'none';
        var parent = targetElement.parentNode;
        if(parent.lastchild == targetElement) 
            parent.appendChild(newElement);
        else
        {
            parent.insertBefore(newElement, targetElement.nextSibling);
        }
    },
    'editor' : function(_pre,_editobj)
    {
        this.form = _editobj;
        this.isEditing = false;
        this.fieldPre = _pre;
        this.edit = function(obj,_data)
        {
            if (obj.data)
                _data = obj.data;
            this.currentData = _data;
            forms.setData(this.fieldPre,this.currentData);
            if (forms.appendObject)
                forms.insertAfter(this.form,obj);
            else
                forms.move(this.form,obj);
            forms.openeditors.push(this);
            obj.editor = this;
            this.isEditing = true;
            if (typeof(this.onLoad)=='function')
                this.onLoad(this.currentData);
        }
        this.erase = function()
        {
            if (typeof(this.onErase)=='function')
                this.onErase(this.currentData);
            this.revert();
        }
        this.save = function()
        {
            var newdata = forms.getData(this.fieldPre,this.currentData);
            var dorevert = true;
            if (typeof(this.onSave)=='function')
                dorevert = this.onSave(newdata);
            if (dorevert) 
                this.revert();
        }
        this.create = function(data)
        {
            if (typeof(this.onCreate)=='function')
                this.onCreate();
        }
        this.revert = function()
        {
            if (typeof(this.onRevert)=='function')
                this.onRevert();
            //alert(forms.hiddenObj);
            forms.move(this.form,document.getElementById(forms.hiddenObj));
            forms.openeditors.remove(this);
            this.isEditing = false;
        }
    },
    'move' : function(w,to,hide)
    {
        if (w)
        {
            if (hide)
                w.style.display = 'none';
            if (w.parentNode.id!=to)
            {
                w.parentNode.removeChild(w);
                to.appendChild(w);
                if (hide)
                    w.style.display = 'block';
            }
        }
        else
            alert('Redigerings rutan existerar inte, kontakta Webdoc');
        return w;
    },
    'getData' : function(pre,art)
    {
        for(var i in art)
        {
            if (typeof(art[i]) != 'function')
            {
                var input = document.getElementById(pre+i);
                if (input)
                {
                    var tagname = input.tagName.toLowerCase();
                    if (tagname=='textarea')
                    {
                        art[i] = input.value;
                    }
                    else
                    {
                        if (input.type.toLowerCase()=='checkbox')
                            art[i] = input.checked;
                        else
                            art[i] = input.value;
                    }
                }
            }
        }
        return art;
    },
    'setData' : function(pre,art)
    {
        for(var i in art)
        {
            if (typeof(art[i]) != 'function')
            {
                var input = document.getElementById(pre+i);
                
                if (input)
                {
                    var tagname = input.tagName.toLowerCase();
                    var value = art[i];
                    if (typeof(value)=='object' && value!=null)
                    {
                        dvalue = new Date(value);
                        value = dvalue.format('yyyy-MM-dd HH:mm');
                    }
                    if (tagname=='textarea')
                    {
                        input.value = value;
                    }
                    else
                    {
                        if (input.type.toLowerCase()=='checkbox')
                        {
                            input.value = 'true';
                            input.checked = value;
                        }
                        else
                            input.value = value;
                    }
                }
            }
        }
    }
}