﻿function Frame(obj) {
//sender.contepantWindow.document.body.offsetHeight
    c = new Control('iframe');
    if (obj != null) {
        if (obj.ID != null)
            c.Attribute('id', obj.ID);
        if (obj.Width != null)
            c.Attribute('width', obj.Width);
        if (obj.Height != null)
            c.Attribute('height', obj.Height);
        if (obj.Name != null)
            c.Attribute('name', obj.Name);
        if (obj.Scroll != null)
            c.Attribute('scrolling', obj.Scroll);
        if (obj.Border != null)
            c.Attribute('frameborder', obj.Border);
        var action = null;
        if (navigator.userAgent.indexOf("MSIE") > -1 && !window.opera) {
            c.Event('readystatechange', function (sender) {
                switch (sender.readyState) {
                    case 'loading':
                        {
                            if (obj.Loading != null)
                                obj.Loading(sender);
                        }
                        break;
                    case 'complete':
                        {
                            if (obj.Complete != null) {
                                obj.Complete(sender);
                            }
                        }
                        break;
                }
            });
        }
        else {
            c.Event('load', function (sender) {
                if (obj.Complete != null) {
                    obj.Complete(sender);
                }
            });
        } 
    }
    return c;
}

function Image(obj) {
    c = new Control('img');
    if (obj != null) {
        if (obj.ID != null)
            c.Attribute('id', obj.ID);
        if (obj.ImageURL != null)
            c.Attribute('src', obj.ImageURL);
        if (obj.Width != null)
            c.Css('width', obj.Width);
        if (obj.Height != null)
            c.Css('height', obj.Height);
        if(obj.Title !=null)
            c.Attribute('title', obj.Title);
        if (obj.Css != null)
            c.Attribute('class', obj.Css);
        if (obj.Error != null)
            c.Event('error', function (sender) {
                sender.Attribute('src', obj.Error);
            }); 
        if (obj.Click != null)
            c.Event('click', function (sender) {
                obj.Click(sender);
                return false;
            });

    }
    return c;
}
function Link(obj) {
    c = new Control('a');
    if (obj != null) {
        if (obj.ID != null)
            c.Attribute('id', obj.ID);
        if (obj.Url != null)
            c.Attribute('href', obj.Url);
        if (obj.Title != null)
            c.Attribute('title', obj.Title);
        if (obj.Css != null)
            c.Attribute('class', obj.Css);
        if (obj.Text != null)
            c.Property('innerHTML', obj.Text);
        if (obj.Click != null)
            c.Event('click', function (sender) {
                obj.Click(sender);
                return false;
            });
    }
    return c;
}
function P(obj) {
    c = new Control('p');
    if (obj != null) {
        if (obj.ID != null)
            c.Attribute('id', obj.ID);
        if (obj.Css != null)
            c.Attribute('class', obj.Css);
        if (obj.Text != null)
            c.Property('innerHTML', obj.Text);
    }
    return c;
}
function DropDown(obj) {
    c = new Control('select');
    if (obj != null) {
        if (obj.ID != null)
            c.Attribute('id', obj.ID);
        if (obj.Width != null)
            c.Css('width', obj.Width);
        if (obj.Width != null)
            c.Css('height', obj.Width);
        if (obj.Css != null)
            c.Attribute('class', obj.Css);
    }
    return c;
}
function ListItem(text, value, selected) {
    c = new Control('option');
    if(text!=null)
        c.Append(text);
    if (value != null)
        c.Attribute('value', value);
    if (selected != null)
        c.Attribute('selected', selected);
    return c;
}
function Hidden(obj) {
    c = new Control('input');
    c.Attribute('type', 'hidden');
    if (obj != null) {
        if (obj.ID != null)
            c.Attribute('id', obj.ID);
        if (obj.Width != null)
            c.Css('width', obj.Width);
        if (obj.Text != null)
            c.Attribute('value', obj.Text);
        if (obj.Width != null)
            c.Css('height', obj.Width);
        if (obj.Css != null)
            c.Attribute('class', obj.Css);
    }
    return c;
}
function Button(obj) {
    c = new Control('input');
    c.Attribute('type','button');
    if (obj != null) {
        if (obj.ID != null)
            c.Attribute('id', obj.ID);
        if (obj.Width != null)
            c.Css('width', obj.Width);
        if (obj.Text != null)
            c.Attribute('value', obj.Text);
        if (obj.Height != null)
            c.Css('height', obj.Height);
        if (obj.Css != null)
            c.Attribute('class', obj.Css);
        if (obj.Click != null)
            c.Event('click', function (sender) {
                obj.Click(sender);
            });
    }
    return c;
}
function CheckBox(obj) {
    c = new Control('input');
    c.Attribute('type', 'checkbox');
    if (obj != null) {
        if (obj.ID != null)
            c.Attribute('id', obj.ID);
        if (obj.ImageURL != null)
            c.Attribute('src', obj.ImageURL);
        if (obj.Name != null)
            c.Attribute('name', obj.Name);
        if (obj.Width != null)
            c.Css('width', obj.Width);
        if (obj.Value != null)
            c.Attribute('value', obj.Value);
        if (obj.Width != null)
            c.Css('height', obj.Width);
        if (obj.Css != null)
            c.Attribute('class', obj.Css);
        if (obj.Click != null)
            c.Event('click', function (sender) {
                obj.Click(sender);
            });
    }
    return c;
}
function RadioButton(obj) {
    c = new Control('input');
    c.Attribute('type', 'radio');
    if (obj != null) {
        if (obj.ID != null)
            c.Attribute('id', obj.ID);
        if (obj.ImageURL != null)
            c.Attribute('src', obj.ImageURL);
        if (obj.Name != null)
            c.Attribute('name', obj.Name);
        if (obj.Width != null)
            c.Css('width', obj.Width);
        if (obj.Value != null)
            c.Attribute('value', obj.Value);
        if (obj.Width != null)
            c.Css('height', obj.Width);
        if (obj.Css != null)
            c.Attribute('class', obj.Css);
        if (obj.Click != null)
            c.Event('click', function (sender) {
                obj.Click(sender);
            });
    }
    return c;
}
function TextBox(obj) {
    c = null;
    mode = 'single';
    if (obj != null && obj.Mode != null)
        mode = obj.Mode;
    switch (mode) {
        case 'single':
            {
                c = new Control('input');
                c.Attribute('type', 'text');
            }
            break;
        case 'multiline':
            c = new Control('textarea');
            break;
    }
    if (obj != null) {
        if (obj.ID != null)
            c.Attribute('id', obj.ID);
        if (obj.Width != null)
            c.Css('width', obj.Width);
        if (obj.Height != null)
            c.Css('height', obj.Height);
        if (obj.Css != null)
            c.Attribute('class', obj.Css);
        if (mode == 'multiline') {
            if (obj.Rows != null)
                c.Attribute('rows', obj.Rows);
            if (obj.Cols != null)
                c.Attribute('cols', obj.Cols);
            if (obj.Text != null)
                c.Property('innerHTML', obj.Text);
        }
        else {
            if (obj.TextChanged != null)
                c.Event('keyup', obj.TextChanged);
            if (obj.Text != null)
                c.Attribute('value', obj.Text);
            if (obj.Name != null)
                c.Attribute('name', obj.Name);
        }

    }
    return c;
} 
function Label(o,obj) {
    c = new Control('span');
    if (obj != null) {
        if (obj.ID != null)
            c.Attribute('src', obj.ImageURL);
        if (obj.Width != null) {
            c.Css('width', obj.Width); 
            c.Css('display', 'inline-block');
        }
        if (obj.Height != null) {
            c.Css('height', obj.Height);
            c.Css('display', 'inline-block');
        }
        if (obj.Weight != null)
            c.Css('font-weight', obj.Weight);
        if (obj.Title != null)
            c.Attribute('title', obj.Title);
        if (obj.Css != null)
            c.Attribute('class', obj.Css);
    }
    switch (typeof(o)) {
        case 'string':
            c.innerHTML = o;
            break;
        case 'object':
            c.appendChild(o);
            break;
    }
    return c;
} 
function Head(obj) {
    s = 4;
    if(obj!=null && obj.Size !=null)
        s = obj.Size;
    c = new Control('h'+s);
    if (obj != null) {
        if (obj.ID != null)
            c.Attribute('id', obj.ID);
        if (obj.Width != null)
            c.Css('width', obj.Width);
        if (obj.Width != null)
            c.Css('height', obj.Width);
        if (obj.Css != null)
            c.Attribute('class', obj.Css);
        if (obj.Text != null)
            c.Property('innerHTML', obj.Text);
    }
    return c;

}
function Table(obj) {
    c = new Control('table');
    if (obj != null) {
        if (obj.ID != null)
            c.Attribute('id', obj.ID);
        if (obj.Width != null)
            c.Css('width', obj.Width);
        if (obj.Height != null)
            c.Css('height', obj.Height);
        if (obj.Border != null)
            c.Attribute('border', obj.Border);
        if (obj.Cellspacing != null)
            c.Attribute('cellspacing', obj.Cellspacing);
        if (obj.Cellpadding != null)
            c.Attribute('cellpadding', obj.Cellpadding);
        if (obj.Css != null)
            c.Attribute('class', obj.Css);
    }
    return c;

}

function TableRow(o,obj) {
    c = new Control('tr');
    if (o != null) {
        if (o.length > 0) {
            for (var i = 0; i < o.length; i++) {
                c.Append(o[i]);
            }
        }
        else
            c.appendChild(o);
    }
    if (obj != null) {
        if (obj.Css != null)
            c.Attribute('class', obj.Css);
        if (obj.ID != null)
            c.Attribute('id', obj.ID);
    }
    return c;
}
function TableCell(o,obj) {
    c = new Control('td');
    if (o != null) {
        switch (typeof(o)) {
            case "string":
                c.innerHTML = o;
                break;
            case "object":
                c.Append(o);
                break;
        }
    }
    if (obj != null) {
        if (obj.ID != null)
            c.Attribute('id', obj.ID);
        if (obj.Width != null)
            c.Css('width', obj.Width);
        if (obj.Height != null)
            c.Css('height', obj.Height);
        if (obj.Align != null)
            c.Attribute('align', obj.Align);
        if (obj.Valign != null)
            c.Attribute('valign', obj.Valign);
        if (obj.ColSpan != null)
            c.Attribute('colspan', obj.ColSpan);
        if (obj.RowSpan != null)
            c.Attribute('rowspan', obj.RowSpan);
        if (obj.Css != null)
            c.Attribute('class', obj.Css);
    }
    return c;
}
function Panel(obj) {
    c = new Control('div');
    if (obj != null) {
        if (obj.ID != null)
            c.Attribute('id', obj.ID);
        if (obj.Width != null)
            c.Css('width', obj.Width);
        if (obj.Height != null)
            c.Css('height', obj.Height);
        if (obj.Css != null)
            c.Attribute('class', obj.Css);
    }
    return c;
}
function C(o) {
    var c = null;
    if (typeof (o) == "string") {
        switch (o.charAt(0)) {
            case '#':
                c = document.getElementById(o.substr(1, o.length));
                break;
            case '.':
                {
                    c = new Array();
                }
                break;
        }
    }
    else
        c = o;
    return c;
}
var Control = function (tag) {
    return document.createElement(tag);
}
this.Element.prototype.Attribute = function(k,v){
    if(v!=null)
        this.setAttribute(k,v);
    else
        return this.getAttribute(k);
}
this.Element.prototype.Property = function (k, v) {
    if(v!=null)
        this[k] = v;
}
this.Element.prototype.Css = function (k, v) {
    var css = this.getAttribute('style');
    if (v != null) {
        if (css != null) {
            var l1 = css.split(';');
            var b = false;
            for (var i = 0; i < l1.length; i++) {
                var l2 = l1[i].split(':');
                if (l2[0] == k) {
                    css = css.replace(l2[1], v);
                    b = true;
                }
            }
            if (!b) {
                if (css.charAt(css.length) != ';')
                    css += ';';
                css += k + ':' + v + ';';
            }
        }
        else
            css = k + ':' + v + ';';
        this.setAttribute('style', css);
    }
    else {
        if (css != null) {
            var l1 = css.split(';');
            for (var i = 0; i < l1.length; i++) {
                var l2 = l1[i].split(':');
                if (l2[0] == k)
                    return l2[1];
            }
        }
    }
}
this.Element.prototype.Append = function (obj, b) {
    if (b != null && b)
        this.Clear();
    switch (typeof (obj)) {
        case 'string':
            this.innerHTML += obj;
            break;
        case 'object':
            {
                if (obj.length != null && obj.length > 0) {
                    for (var i = 0; i < obj.length; i++) {
                        this.appendChild(obj[i]);
                    }
                }
                else
                    this.appendChild(obj);
            }
            break;
    }
}
this.Element.prototype.Clear = function (len) {
    if (len == null)
        len = 0;
    while (this.childNodes.length > len) {
        this.removeChild(this.childNodes[this.childNodes.length - 1]);
    }
}
this.Element.prototype.Attach = function (obj) {
    this.Attribute('parent',obj);
    obj.appendChild(this);
}
this.Element.prototype.Show = function () {
    this.style.visibility = 'visible';
}
this.Element.prototype.Hide = function () {
    this.style.visibility = 'hidden';
}
this.Element.prototype.Navigate = function (url) {
    if (this.tagName == 'IFRAME') {
        this.Attribute('src', url);
    }
}
this.Element.prototype.Event = function (name, fn) {
    this['on' + name] = function () {
        fn(this);
    }
}
