
var newWnd;
var is_debug = false;
var type_log = null;
var callback_queue = null;
var user_messages = null;

function AddLog(message, type)
{
    if (is_debug)
    {
        if (!newWnd || newWnd.closed)
            newWnd = window.open("", "debugWindow", "resizable,scrollbars,height=800,width=1200");

        var dt = new Date();
        if (newWnd && type_log == type)
            newWnd.document.write(dt.getMinutes() + ':' + dt.getSeconds() + ':' + dt.getMilliseconds() + ' - ' + message + '<br>');
    }
}


function WContainer76(ControlID, controls_xml)
{
    this.owner = null;
    this.level_gather_of_state = 0;
    this.is_link_state = false;
    this.url_to_images = '';
    
    this.linked_children = new Array();
    
    this.isIE = !!(window.attachEvent && !window.opera)
    this.isOpera = !!window.opera;
    this.isGecko = navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1;
    this.callback_StartTime = new Date().valueOf();
    this.callback_EndTime = new Date().valueOf();

   
}

WContainer76.prototype.initObjectCache = function()
{

    this.all_Objects = new Array();
    this.all_Builders = new Array();
    this.all_Builder_Types = new Array();
    
    AddLog(this.JSObject + '.initObjectCache()' + '*** SNV ****');  
}

WContainer76.prototype.init = function(ControlID, Prefix, controls_xml)
{
    this.ControlID = ControlID;
    this.controls_xml = controls_xml;
    
    this.Prefix = Prefix;
    this.main_cont = document.getElementById(this.Prefix + '_main');
    this.workarea = document.getElementById(this.Prefix + '_wa');
    this.footarea = document.getElementById(this.Prefix + '_ft');
    this.disabled_div = document.getElementById(this.Prefix + '_dd');
    
    this.NO_CLEAR_SESSION = "__NoClearSession";
    this.NO_RESTORE_STATE = "__NoRestoreState";
    
    this.is_pass_gather = false;
    this.is_pass_assign = false;

    //call control
    this.cc = null;
    this.cc_event_result = null;
    this.cc_event_error = null;
    
    if (this.owner && this.is_link_state)
        this.owner.AddLinkedChild(this);
     this.initObjectCache();
}

WContainer76.prototype.CallServer = function(
    //common parameters
    eventTarget, method_name, method_argument, context, event_result, event_error, 
    //queue controlling parameters
    queue_service,
    //services parameters
    event_call, queue_flag)
{
    this.callback_StartTime = this.callback_EndTime = new Date().valueOf();
    this.storeState_StartTime = this.storeState_EndTime = new Date().valueOf();
    if (method_name == 'RefreshContent')
        this.initObjectCache();
    
    var queue = this.get_queue();
    if (queue_flag != 1)
    {
        if (queue_service) queue_service.clear_queue(queue);
        
        var item = new Object();
        
        item.container = this;
        item.eventTarget = eventTarget;
        item.method_name = method_name;
        item.method_argument = method_argument;
        item.context = context;
        item.event_result = event_result;
        item.event_error = event_error;
		item.event_call = event_call;
		item.queue_service = queue_service;
        queue.push(item);

        if (queue.length > 1)
            return;
    }
        
    if (eventTarget.ReplaceAll)
        var type_call = this.get_type_call(eventTarget.ReplaceAll('$', '_'));
    this.reset_postback_flags();
    switch (type_call)
    {
        case 1: // Postback
            queue.shift();
            __doPostBack(eventTarget, method_argument);
            return;
        case 2: // Postback with flag "no clear session at first call"
            queue.shift();
            this.set_no_clear_session_flag();
            __doPostBack(eventTarget, method_argument);
            return;
        case 3: //no request. Close Window
            queue.shift();
            var cont = this;
            while(cont && !cont.close_window)
                cont = cont.owner;
            if (cont) cont.close_window();
            return;
        case 4: //common callback without gather state
            this.is_pass_gather = true;
            break;
    }
    
    
    
    //Common case. Ordinary callback
    AddLog('Begin WContainer76.prototype.CallServer');

    this.set_call_control(context, event_result, event_error);

    AddLog('begin this.disable_child_containers(block)');
    this.disable_child_containers('block');
    AddLog('end this.disable_child_containers(block)');
    AddLog('Begin gather_controls');
    var doc = this.gather_controls(this.level_gather_of_state);
    AddLog('End gather_controls');
    var doc_elem = doc.documentElement;
    
    var method = doc.createElement('method');
    doc_elem.appendChild(method);
    
    var attr = doc.createAttribute('ctrl');
    attr.value = eventTarget; 
    method.setAttributeNode(attr);

    attr = doc.createAttribute('name');
    attr.value = method_name; 
    method.setAttributeNode(attr);
    
    attr = doc.createAttribute('is_pass_state');
    attr.value = this.is_pass_assign ? 'True' : 'False'; 
    method.setAttributeNode(attr);
    
    var param = doc.createElement('param');
    method.appendChild(param);
    
    attr = doc_elem.ownerDocument.createAttribute('name');
    attr.value = ""; 
    param.setAttributeNode(attr);
    param.text = method_argument;
    
    
    AddLog('preparing callback');

    this.temp_ControlID = this.ControlID;
    this.temp_doc = doc;
    this.temp_on_callback_method = this.on_callback_method;
    this.temp_obj = this;
    this.temp_on_callback_error = this.on_callback_error;
	this.temp_event_call = event_call;
    
    this.store__theFormPostCollection = new Array();
    this.store__theFormPostCollection = this.store__theFormPostCollection.concat(__theFormPostCollection);
    this.store__theFormPostData = __theFormPostData;
    __theFormPostCollection.length = 0;
    __theFormPostData = '';

    AddLog('end preparing callback');

    setTimeout(this.JSObject + '.DoCallBack();', 0);
}

WContainer76.prototype.DoCallBack = function()
{
    AddLog('Begin WContainer76.prototype.DoCallBack - begin this.WebForm_InitCallback();');

    this.WebForm_InitCallback();
    
    AddLog('end this.WebForm_InitCallback();');

    AddLog('Do Callback');
    this.storeState_EndTime = new Date().valueOf();
    WebForm_DoCallback(this.temp_ControlID, this.temp_doc.xml, this.temp_on_callback_method, this.temp_obj, this.temp_on_callback_error, false);
	if (this.temp_event_call) this.temp_event_call();
} 

WContainer76.prototype.WebForm_InitCallback = function()
{
    var count = theForm.elements.length;
    var element;
    for (var i = 0; i < count; i++) 
    {
        element = theForm.elements[i];
        var type = element.type;
        var tag_name = element.tagName.toLowerCase();
        if (tag_name == "input" 
            && (type == "hidden" || 
                (type == "checkbox" && element.checked) || 
                type == "text")
            && (element.id != "__EVENTVALIDATION")) 
        {
            WebForm_InitCallbackAddField(element.name, element.value);
        }
        else if (tag_name == "select")
        {
            var selectCount = element.options.length;
            for (var j = 0; j < selectCount; j++) 
            {
                var selectChild = element.options[j];
                if (selectChild.selected == true)
                    WebForm_InitCallbackAddField(element.name, element.value);
            }
        }        
    }
}

WContainer76.prototype.get_queue = function()
{
    if (!callback_queue)
        callback_queue = new Array();
    return callback_queue;
}

WContainer76.prototype.process_queue = function()
{
    var queue = this.get_queue();
    queue.shift();
    if (queue.length > 0)
    {
        var item = queue[0];
        item.container.CallServer(item.eventTarget, item.method_name, item.method_argument, item.context, item.event_result, item.event_error, item.queue_service, item.event_call, 1);
    }
}

/* Returns:
    0 - common callback
    1 - Postback
    2 - Postback with flag "no clear session at first call"
    3 - no request. Close Window
    4 - common callback without gather state.*/
WContainer76.prototype.get_type_call = function(id)
{
    var elem = document.getElementById(id);
    if (elem)
    {
        var pb = elem.attributes.getNamedItem("Postback");
        if (pb)
        {
            var cl = elem.attributes.getNamedItem("PostbackNoClearSession");
            if (cl) return 2;
            return 1;
        }
        pb = elem.attributes.getNamedItem("CloseWindow");
        if (pb) return 3;
        pb = elem.attributes.getNamedItem("NoGatherState");
        if (pb) return 4;
    }
    return 0;
}

WContainer76.prototype.reset_postback_flags = function()
{
    var elem = document.getElementById(this.NO_CLEAR_SESSION);
    if (elem) elem.parentElement.removeChild(elem);
    theForm.__EVENTTARGET.value = '';
    theForm.__EVENTARGUMENT.value = '';
}

WContainer76.prototype.set_no_clear_session_flag = function()
{
    var elem = document.createElement('<input type="hidden">');
    elem.name = this.NO_CLEAR_SESSION;
    elem.id = this.NO_CLEAR_SESSION;
    elem.value = 'True';
    theForm.appendChild(elem);
}

WContainer76.prototype.set_no_restore_client_state = function()
{
    var elem = document.createElement('<input type="hidden">');
    elem.name = this.NO_RESTORE_STATE;
    elem.id = this.NO_RESTORE_STATE;
    elem.value = 'True';
    theForm.appendChild(elem);
}

WContainer76.prototype.set_call_control = function(context, event_result, event_error)
{
    this.cc = context;
    this.cc_event_result = event_result;
    this.cc_event_error = event_error;
}
    
WContainer76.prototype.clear_call_control = function()
{
    this.cc = null;
    this.cc_event_result = null;
    this.cc_event_error = null;
}


WContainer76.prototype.on_callback_method = function(val, context)
{
    context.restoreState_StartTime = context.restoreState_EndTime = new Date().valueOf();
    AddLog('WContainer76.prototype.on_callback_method - begin restore form post data;');

    __theFormPostCollection = new Array();
    __theFormPostCollection = context.store__theFormPostCollection.concat();
    __theFormPostData = context.store__theFormPostData;

    var queue = context.get_queue();
    var qi = queue[0];
    var stateXmlDoc = null;
    if (!qi.queue_service || !qi.queue_service.is_canceled)
    {
        AddLog('WContainer76.prototype.on_callback_method - begin assign_controls(val);');
        stateXmlDoc = context.assign_controls(val);
        AddLog('WContainer76.prototype.on_callback_method - end assign_controls(val);');
        var res = stateXmlDoc.documentElement.selectSingleNode('result');
        var res_text = res.childNodes[0].data;
        res_text = res_text.replace(/]]&gt;/gi, ']]>');
        if (context.cc_event_result)
        {
            AddLog('WContainer76.prototype.on_callback_method - Call owner method');

            context.cc_event_result(res.childNodes[0].data, context.cc);
        }
    }
    else
        AddLog('WContainer76.prototype.on_callback_method - Callback was been canceled. Callback result is ignored;');
    AddLog('WContainer76.prototype.on_callback_method - End restore form post data;');
    context.clear_call_control();
    AddLog('begin this.disable_child_containers(block)');
    context.disable_child_containers('none');
    AddLog('end this.disable_child_containers(block)');
    context.restoreState_EndTime = new Date().valueOf();
    if (typeof(OnEndRestorePostData) != 'undefined' && stateXmlDoc)
    {
        var PageStatisticsXmlNode = stateXmlDoc.selectSingleNode('packet/ExtStateNode/PageStatistics');
        if (PageStatisticsXmlNode)
        {
            context.callback_EndTime = new Date().valueOf();
            var callbackProcessing_Time = Math.round(context.callback_EndTime - context.callback_StartTime);
            var storeState_Time = Math.round(context.storeState_EndTime - context.storeState_StartTime);
            var restoreState_Time = Math.round(context.restoreState_EndTime - context.restoreState_StartTime);
            var stateProcessing_Time = storeState_Time + restoreState_Time;
            var PageStatisticsXml = xml.DOMDocument.create(); 
            var NewNode = PageStatisticsXmlNode.ownerDocument.createElement("CallbackProcessing_Time")
            NewNode.text = callbackProcessing_Time.toString();
            PageStatisticsXmlNode.appendChild(NewNode);
            
            NewNode = PageStatisticsXmlNode.ownerDocument.createElement("StateProcessing_Time")
            NewNode.text = stateProcessing_Time.toString();
            PageStatisticsXmlNode.appendChild(NewNode);
            
            PageStatisticsXml.loadXML(PageStatisticsXmlNode.xml); 
            OnEndRestorePostData(PageStatisticsXml);
        }
    }
    context.process_queue();
}
    
WContainer76.prototype.on_callback_error = function(val, context)
{
    AddLog('WContainer76.prototype.on_callback_error - begin restore form post data; Error: ' + val);

    if (val.substring(0, 11) == '__Redirect:')
        ///window.navigate(val.substring(11, val.length));
        window.location = val.substring(11, val.length);
    else
    {
        __theFormPostCollection = new Array();
        __theFormPostCollection = context.store__theFormPostCollection.concat();
        __theFormPostData = context.store__theFormPostData;
        var queue = context.get_queue();
        var qi = queue[0];
        if (!qi.queue_service || !qi.queue_service.is_canceled)
        {
            if (context.cc_event_error)
            {
                AddLog('WContainer76.prototype.on_callback_error - Call owner method');

                context.cc_event_error(val, context.cc);
            }
            else
			    context.show_message_box('WContainer76 error: ' + val, 'error');
                //alert('WContainer76 error: ' + val);
        }
        else
            AddLog('WContainer76.prototype.on_callback_error - Callback was been canceled. Callback result is ignored;');
		context.clear_call_control();
        context.disable_child_containers('none');

        context.process_queue();
    }
}

//To gather only self state
//WContainer76.prototype.gather_controls = function(child)
//{
//    var res = null;

//    res = this.newXMLDOM();
//    this.loadDoc(res, this.controls_xml);

//    if (this.is_pass_gather)
//    {
//        var ctrl = res.selectSingleNode('packet/controls');
//        while (ctrl.childNodes.length > 0)
//            ctrl.removeChild(ctrl.firstChild);
//    }
//    else
//    {
//        var ctrls = res.selectNodes('packet/controls/control');
//        for (var j = 0; j < ctrls.length; j++)
//            this.gather_control(ctrls[j]);
//    }

//    this.is_pass_gather = false;
//    return res;
//}


WContainer76.prototype.disable_child_containers = function(display_val, exclude_cont)
{
    if (!WContainer76.prototype.loading_win && !exclude_cont)
    {
        var win = document.createElement('div');
        win.appendChild(document.createTextNode('Loading...'));
        win.appendChild(document.createElement('br'));
        
        var img = document.createElement('img');
        img.src = this.url_to_images + 'loading.gif';
        win.appendChild(img);
        
        win.className = 'WPanel_callbackWin';
        //document.body.insertBefore(win, document.body.children[0]);
        document.body.insertBefore(win, document.body.firstChild);
        WContainer76.prototype.loading_win = win;
    }

    WContainer76.prototype.loading_win.style.top = document.body.clientHeight - 30;
    WContainer76.prototype.loading_win.style.display = display_val;
    
    var res = this.newXMLDOM();
    this.loadDoc(res, this.controls_xml);
    
    var ctrls = res.selectNodes('packet/controls/control');
    for (var j = 0; j < ctrls.length; j++)
    {
        var obj_attr = ctrls[j].attributes.getNamedItem('obj_id');
        var obj_id = obj_attr != null ? obj_attr.value : null;
        var client_obj = null;
        /*SNV[-]try { client_obj = eval(obj_id); }
        catch(ex) { client_obj = null; }*/
        try
        {
            client_obj = this.all_Objects[obj_id];
            if (!client_obj)
            {
                client_obj = eval(obj_id);
                client_obj.ExistsDisabledDIV  = client_obj && client_obj instanceof WContainer76 && client_obj != exclude_cont && client_obj != this && client_obj.disabled_div != null;
                this.all_Objects[obj_id] = client_obj;
            }
        }
        catch(ex) 
        { 
            client_obj = new Object(); 
            client_obj.ExistsDisabledDIV = false;
            this.all_Objects[obj_id] = client_obj;
        }
        /*SNV[-]if (client_obj && client_obj instanceof WContainer76 
            && client_obj != exclude_cont && client_obj != this && client_obj.disabled_div != null)*/
           if (client_obj.ExistsDisabledDIV)
                client_obj.disabled_div.style.display = display_val;
    }
    
    if (this.owner)
        this.owner.disable_child_containers(display_val, this);
}

WContainer76.prototype.gather_children = function(doc)
{
    if (!this.linked_children) return;
    for (var i = 0; i < this.linked_children.length; i++)
    {
        var child = this.linked_children[i];
        source = this.newXMLDOM();
        this.loadDoc(source, child.controls_xml)
        if (!child.is_pass_gather)
            child.gather_merge(doc, source);
        child.gather_children(doc);
    }
}

//To gather state from all hierarchy
WContainer76.prototype.gather_controls = function(level)
{
    var res = null;
   
    if (this.owner && level > 0)
        res = this.owner.gather_controls(level-1);
    else
    {
        res = this.newXMLDOM();
        this.loadDoc(res, this.controls_xml);
    }

    //move to second condition
    var source = this.newXMLDOM();
    this.loadDoc(source, this.controls_xml);

    if (!this.owner || level <= 0)
    {
        if (this.is_pass_gather)
        {
            var ctrl = res.selectSingleNode('packet/controls');
            while (ctrl.childNodes.length > 0)
                ctrl.removeChild(ctrl.firstChild);
        }
        else
        {
            var ctrls = res.selectNodes('packet/controls/control');
            for (var j = 0; j < ctrls.length; j++)
                this.gather_control(ctrls[j]);
            this.gather_children(res);
        }
    }
    else
    {
        if (!this.is_pass_gather)
            this.gather_merge(res, source);
        this.gather_children(res);
    }
    
    this.is_pass_gather = false;
    return res;
}

WContainer76.prototype.gather_merge = function(dest, source)
{
    var ctrls = source.selectNodes('packet/controls/control');
    var dest_ctrls = dest.selectSingleNode('packet/controls');
    for (var j = 0; j < ctrls.length; j++)
    {
        //Verify of content
        var is_exist = dest_ctrls.selectSingleNode('control[@name=\'' + ctrls[j].attributes.getNamedItem('name').value + '\']');
        if (!is_exist)
        {
            var dest_ctrl = ctrls[j].cloneNode(true);
            dest_ctrls.appendChild(dest_ctrl);
            this.gather_control(dest_ctrl);
        }
    }
}
    
WContainer76.prototype.assign_controls = function(xml)
{
    var doc = this.newXMLDOM();
    this.loadDoc(doc, xml);
    if (!this.is_pass_assign)
    {
        var ctrls = doc.selectNodes("packet/controls/control");
        for (var j = 0; j < ctrls.length; j++)
            this.assign_control(ctrls[j]);
    }
    this.is_pass_assign = false;

    var redir = doc.selectSingleNode("packet/redirect");
    var post_back = doc.selectSingleNode("packet/post_back");
    var submit = doc.selectSingleNode("packet/submit");
    var messages = doc.selectSingleNode("packet/messages");

    if (redir)
    {
        var attr = redir.attributes.getNamedItem("url");
        if (attr) window.navigate(attr.value);
    }
    else if (submit)
    {
        var tg = submit.attributes.getNamedItem("eventTarget");
        if (tg && theForm)
        {
            if (!theForm.__sb)
            {
                theForm.__sb = document.createElement('input');
                theForm.__sb.type = 'submit';
                //theForm.__sb.style.display = 'none';
                theForm.appendChild(theForm.__sb);
            }
            theForm.__sb.name = tg.value;
            theForm.__sb.click();
            abort();
        }
    }
    else if (post_back)
    {
        var tg = post_back.attributes.getNamedItem("eventTarget");
        var ar = post_back.attributes.getNamedItem("eventArgument");
        if (tg && ar) __doPostBack(tg.value, ar.value);
    }

    if (messages)
    {
        user_messages = new Array();
        var msgs = doc.selectNodes("packet/messages/i");
        for (var j = 0; j < msgs.length; j++)
            user_messages[user_messages.length] = {msg: msgs[j].text, type: msgs[j].getAttribute('type') };
        
        if (user_messages.length > 0)
        {
            var msg = user_messages.shift();
            this.show_message_box(msg.msg, msg.type);
        }
    }

    return doc;
}

WContainer76.prototype.show_message_box = function(message, type)
{
    var top = this.get_top_cont();
    var name = top.ControlID.replace('$', '_') + '_' + '__message_box_id';
    //Find window
    var win = eval(name);
    if (win)
    {
        //Set message
        var fname = '';
        switch (type)
        {
            case 'info': fname = 'good_shield.gif'; break;
            case 'error': fname = 'error_shield.gif'; break;
            case 'warning': fname = 'warning_shield.gif'; break;
        }
        win.workarea.childNodes[1].childNodes[0].childNodes[0].src = this.url_to_images + fname;
        win.workarea.childNodes[1].childNodes[1].childNodes[0].childNodes[0].rows[0].cells[0].innerHTML = message;
        
        win.parent_elem = theForm;
        
        //Show window
        win.set_visible('True');
    }
}

function __DoEditContent(container, func, func_args, event)
{
    if (container && event.shiftKey)
        container.CallServer('', func, func_args);
}

function __doWCPostBackF(container, func, func_args)
{
    if (container)
        container.CallServer('', func, func_args);
}

function WContainer76_EditContent(event, source_control)
{
    if (!event.shiftKey) return;
    var obj = WContainer76_GetContainer(source_control);
    var is_html = source_control.attributes.getNamedItem("is_html");
    is_html = is_html ? is_html.value : "false";
    if (!obj) return;
    var top = obj.get_top_cont();
    var name = top.ControlID.replace('$', '_') + '_' + '__content_box_id';
    var win = eval(name);
    if (!win) return;
    
    var ipt = win.workarea.childNodes[1].childNodes[0].childNodes[0].childNodes[0].childNodes[0];
    if (ipt.tagName == 'SPAN')
        ipt = ipt.childNodes[0];
    ipt.checked = is_html == "true";
    win.workarea.childNodes[1].childNodes[0].childNodes[1].childNodes[0].childNodes[0].innerText = is_html == "true" ? source_control.innerHTML : source_control.innerText;

    win.parent_elem = theForm;
    top._src_ctrl = source_control;
    
    win.set_visible('True');
	event.cancelBubble = true;
}

function WContainer76_OKEdit(top)
{
    var name = top.ControlID.replace('$', '_') + '_' + '__content_box_id';
    var win = eval(name);
    if (!win) return;
    
    var chk = false;
    var ipt = win.workarea.childNodes[1].childNodes[0].childNodes[0].childNodes[0].childNodes[0];
    if (ipt.tagName == 'SPAN')
        ipt = ipt.childNodes[0];

    var str = win.workarea.childNodes[1].childNodes[0].childNodes[1].childNodes[0].childNodes[0].innerText;
    if (ipt.checked)
    {
        top._src_ctrl.innerHTML = str;
        chk = true;
    }
    else
        top._src_ctrl.innerText = str;
    
    var res_name = top._src_ctrl.attributes.getNamedItem("C76Resource")
    
    if (top._src_ctrl.attributes['is_html'])
        top._src_ctrl.attributes['is_html'].value = chk;
    else
    {
        var attr = _src_control.createAttribute('is_html');
        attr.value = chk;
        _src_control.setAttributeNode(attr);
    }

    if (res_name)
    {
        res_name = top._src_ctrl.attributes.getNamedItem("C76Resource").value;
        top.CallServer('', 'SaveResource', res_name + '\n' + chk + '\n' + str, top, null, null);
        win.close_window();
    }
}

WContainer76.prototype.get_top_cont = function()
{
    var top = this;
    while (top != null)
    {
        if (top.owner == null)
            break;
        top = top.owner;
    }
    return top;
}

WContainer76.prototype.close_mb = function(window)
{
    if (window)
        window.set_visible('False');
    if (user_messages && user_messages.length > 0)
    {
        var msg = user_messages.shift();
        this.show_message_box(msg.msg, msg.type);
    }
}

WContainer76.prototype.gather_control = function(node)
{
    var obj_attr = node.attributes.getNamedItem('obj_id');
    var obj_id = obj_attr != null ? obj_attr.value : null;
    var client_obj = null;
    try { 
     
        //SN[-]client_obj = eval(obj_id); 
        client_obj = this.all_Objects[obj_id];
        if (!client_obj)
        {
            client_obj = eval(obj_id);
            this.all_Objects[obj_id] = client_obj;
        }
    }
    catch(ex) 
    { 
        //client_obj = null; 
        client_obj = new Object(); 
        this.all_Objects[obj_id] = client_obj;
    }

    //Store properties by StoreState function
    if (client_obj && client_obj.StoreState)
    {
        var state = client_obj.StoreState();
        if (state != null)
            node.appendChild(state.cloneNode(true));
            return;
    }
    
    //Store properties by builder classes
    var type = node.attributes.getNamedItem('type').value;
    type = type.replace(/\./gi, '_') + '_Builder';
    
    var _b_type = null;
    try {
           //SNV[-] _b_type = eval(type);
           _b_type = this.all_Builder_Types[type] 
           if (!_b_type)
           {
                _b_type = eval(type);
                _b_type.Succesfully = true;
                this.all_Builder_Types[type] = _b_type;
           }
        }
    catch (ex) 
    { 
        _b_type = new Object();
        _b_type.Succesfully = false;
        this.all_Builder_Types[type] = _b_type;
    } 
    
    if (_b_type != null && _b_type.Succesfully)
    {
        var name_attr = node.attributes.getNamedItem('name');
        var name = name_attr != null ? name_attr.value : null;
        
        //SN[-]var builder = eval('new ' + type + '(name, obj_id)');
        var bname = type + '_' + obj_id;
        var builder = this.all_Builders[bname];
        if (!builder)
        {
            builder = eval('new ' + type + '(name, obj_id)');
            this.all_Builders[bname] = builder;
        }
        
        for(var j = 0; j < builder.properties.length; j++)
        {
            var prop_name = builder.properties[j];
            var prop_value = builder.get_prop(builder.properties[j]);
            var prop = node.ownerDocument.createElement('prop');
            node.appendChild(prop);

            var attr = node.ownerDocument.createAttribute('name');
            attr.value = prop_name; 
            prop.setAttributeNode(attr);
            
            if (prop_value != null) prop.text = prop_value;
        }
    }
}
    
WContainer76.prototype.assign_control = function(node)
{
    var obj_attr = node.attributes.getNamedItem("obj_id");
    var obj_id = obj_attr != null ? obj_attr.value : null;
    var client_obj = null;
    try 
    { 
        //SN[-]client_obj = eval(obj_id); 
        client_obj = this.all_Objects[obj_id];
        if (!client_obj)
        {
            client_obj = eval(obj_id);
            this.all_Objects[obj_id] = client_obj;
        }
    }
    catch(ex) 
    { 
        //client_obj = null; 
        client_obj = new Object(); 
        this.all_Objects[obj_id] = client_obj;
    }
    //Restore properties by StoreState function
    if (client_obj && client_obj.RestoreState)
    {
        AddLog('WContainer76.prototype.assign_control - Begin RestoreState control: ' + obj_id);
        //SN[-] AddLog('WContainer76.prototype.assign_control - State:');
        //SN[-] AddLog('<pre>' + node.xml + '</pre>');
        client_obj.RestoreState(node);
        AddLog('WContainer76.prototype.assign_control - End RestoreState control: ' + obj_id);
    }
    else
    {
        var type = node.attributes.getNamedItem("type").value;
        type = type.replace(/\./gi, '_') + '_Builder';
        var _b_type = null;
        try 
        {
           //SNV[-] _b_type = eval(type);
           _b_type = this.all_Builder_Types[type] 
           if (!_b_type)
           {
                _b_type = eval(type);
                _b_type.Succesfully = true;
                this.all_Builder_Types[type] = _b_type;
           }
        }
        catch (ex) 
        { 
            _b_type = new Object();
            _b_type.Succesfully = false;
            this.all_Builder_Types[type] = _b_type;
        } 

        if (_b_type != null && _b_type.Succesfully)
        {
            var name_attr = node.attributes.getNamedItem("name");
            var name = name_attr != null ? name_attr.value : null;
            AddLog('WContainer76.prototype.assign_control - Begin Get Builder name: ' + name + '; obj_id: ' + obj_id);
            
            //SNV[-]var builder = eval('new ' + type + '(name, obj_id)');
            
            var bname = type + '_' + obj_id;
            var builder = this.all_Builders[bname];
            if (!builder)
            {
                builder = eval('new ' + type + '(name, obj_id)');
                this.all_Builders[bname] = builder;
            }
            
            AddLog('WContainer76.prototype.assign_control - End Get Builder name: ' + name + '; obj_id: ' + obj_id);
            AddLog('WContainer76.prototype.assign_control - Begin Restore by Builder name: ' + name + '; obj_id: ' + obj_id);
            for(var j = 0; j < builder.properties.length; j++)
            {
                var prop_name = builder.properties[j];
                var param = node.selectSingleNode('prop[@name=\'' + prop_name + '\']');
                if (param)
                {
                    var val = '';
                    if (param.childNodes.length == 1 && param.childNodes[0].nodeType == 1) // xml contents
                        val = param.childNodes[0].xml;
                    else
                        for (var i = 0; i < param.childNodes.length; i++)
                        {
                            if (this.isIE)
                                val += param.childNodes[i].text;
                            else
                                val += param.childNodes[i].textContent;
                        }
                    builder.set_prop(prop_name, val);
                }
            }
            AddLog('WContainer76.prototype.assign_control - End Restore by Builder name: ' + name + '; obj_id: ' + obj_id);
        }
    }
    
    //restore html
    if (client_obj)
    {
        var cont = node.selectSingleNode('content');
        if (cont)
        {
            var res_text = cont.childNodes[0].data;
            res_text = res_text.replace(/]]&gt;/gi, ']]>');
            
            var res_add = null;
            if (cont.childNodes.length > 1)
                res_add = cont.childNodes[1].data;
            
            this.on_callback_refresh_method(cont.childNodes[0].data, client_obj, res_add);
        }
    }
}
    
WContainer76.prototype.newXMLDOM = function()
{
    /*var dom = null;
    if (window.ActiveXObject) //IE
    {
        var dom = new ActiveXObject('Microsoft.XMLDOM');
        dom.async = true;
    } 
    else if (document.implementation && document.implementation.createDocument) // Mozilla
        dom = document.implementation.createDocument("" ,"", null);
    else //???
    	dom = new DOMParser(); 
    return dom;*/
    
    return xml.DOMDocument.create();
}

WContainer76.prototype.loadDoc = function(inst, doc)
{
    /*if (window.ActiveXObject) //IE
        inst.loadXML(doc);
    else //Mozilla
        inst.loadBindingDocument(doc);*/
    
    inst.loadXML(doc);
}

WContainer76.prototype.set_wait_state = function()
{
    AddLog('Begin WContainer76.prototype.set_wait_state');

    if (!this.workarea)
        return;
    
    var table = document.createElement('table');
//    table.width = this.workarea.clientWidth > 4 ? this.workarea.clientWidth - 4 : '100%';
//    table.height = this.workarea.clientHeight > 4 ? this.workarea.clientHeight - 4 : '100%';
    table.width = '100%';
    table.height = '100%';
    var tbody = document.createElement('tbody');
    
    while (this.workarea.hasChildNodes())
        this.workarea.removeChild(this.workarea.children[0]);
    this.workarea.appendChild(table);

//    this.workarea.insertAdjacentElement("AfterBegin", table);
//    var childs = this.workarea.childNodes;
//    for(var i = 1; i < childs.length; i++)
//        childs[i].style.display = 'none';
    
    table.appendChild(tbody);
    
    var tr = document.createElement('tr');
    tbody.appendChild(tr);
    var td = document.createElement('td');
    td.align = 'center';
    td.valign = 'middle';
    tr.appendChild(td);
    
    var sp = document.createElement('img');
    sp.src = this.url_to_images + 'spinner.gif';

    td.appendChild(sp);

    AddLog('End WContainer76.prototype.set_wait_state');
}

WContainer76.prototype.refresh_content = function()
{
    this.set_wait_state();
    AddLog('Call RefreshContent');
    this.is_pass_gather = true;
    this.is_pass_assign = true;
    this.CallServer('', 'RefreshContent', '', this, this.on_callback_refresh_method, this.on_callback_error_);
    
}

WContainer76.prototype.on_callback_refresh_method = function(val, context, footer)
{
    AddLog('WContainer76.prototype.on_callback_refresh_method begin');

    if (context.workarea)
    {
        AddLog('WContainer76.prototype.on_callback_refresh_method: context.workarea is exists. prepare.');

        h = val.replace(/<\/?script[^>]*>/gi,"\07");
        h = h.replace(/\07[^\07]+\07/g,"");

        pre = val.replace(/<\/script[^>]*>/gi,"\07");
        pre = pre.replace(/<\bscript[^>]*>/gi,"\06");
        pre = ("\07" + pre + "\06").replace(/\07[^\06]+\06/g,"");
        pre = pre.replace(/(\07|\06)/gi,"");
            
        AddLog('WContainer76.prototype.on_callback_refresh_method: context.workarea is exists. Replace Html.');
        context.workarea.innerHTML = h;
        AddLog('WContainer76.prototype.on_callback_refresh_method: execScript');
        
        if (pre) 
            if (window.execScript)
                window.execScript(pre);
            else
                window.eval(pre);                    
        AddLog('WContainer76.prototype.on_callback_refresh_method: execScript end');
    }
    
    if (footer != null && context.footarea)
    {
        h = footer.replace(/<\/?script[^>]*>/gi,"\07");
        h = h.replace(/\07[^\07]+\07/g,"");

        pre = footer.replace(/<\/script[^>]*>/gi,"\07");
        pre = pre.replace(/<\bscript[^>]*>/gi,"\06");
        pre = ("\07" + pre + "\06").replace(/\07[^\06]+\06/g,"");
        pre = pre.replace(/(\07|\06)/gi,"");
        
        context.footarea.innerHTML = h;
        if (pre) 
            if (window.execScript)
                window.execScript(pre);
            else
                window.eval(pre);                    
    }
    //SN[+]
    if (context && context.initObjectCache)
        context.initObjectCache();
    AddLog('WContainer76.prototype.on_callback_refresh_method end');
}

WContainer76.prototype.AddLinkedChild = function(cont)
{
    this.linked_children.push(cont);
}

function WContainer76_CallServer(source_control, obj_container, eventTarget, method_name, method_argument)
{
    var obj = obj_container;
    if (source_control)
        obj = WContainer76_GetContainer(source_control);
        
    if (obj)
        obj.CallServer(eventTarget, method_name, method_argument);
    else
        alert('Can not find WContainer76 object');
}

function WContainer76_GetContainer(source_control)
{
    var obj = null;
    if (source_control)
    {
        var container = source_control.attributes.getNamedItem("ContainerID")
        if (container)
            container = source_control.attributes.getNamedItem("ContainerID").value;
        else
        {
            alert('ContainerID attribute is not defined.');
            return;
        }
        obj = eval(container);
    }
    return obj;
}

function __doWCPostBack(container, eventTarget, eventArgument)
{
    if (container)
        container.CallServer(eventTarget, '', eventArgument);
}

function WebForm_DoWCPostBackWithOptions(container, options)
{
    var validationResult = true;
    if (options.validation) {
        if (typeof(Page_ClientValidate) == 'function') {
            validationResult = Page_ClientValidate(options.validationGroup);
        }
    }
    if (validationResult) {
        if ((typeof(options.actionUrl) != "undefined") && (options.actionUrl != null) && (options.actionUrl.length > 0)) {
            theForm.action = options.actionUrl;
        }
        if (options.trackFocus) {
            var lastFocus = theForm.elements["__LASTFOCUS"];
            if ((typeof(lastFocus) != "undefined") && (lastFocus != null)) {
                if (typeof(document.activeElement) == "undefined") {
                    lastFocus.value = options.eventTarget;
                }
                else {
                    var active = document.activeElement;
                    if ((typeof(active) != "undefined") && (active != null)) {
                        if ((typeof(active.id) != "undefined") && (active.id != null) && (active.id.length > 0)) {
                            lastFocus.value = active.id;
                        }
                        else if (typeof(active.name) != "undefined") {
                            lastFocus.value = active.name;
                        }
                    }
                }
            }
        }
    }
    if (options.clientSubmit) {
        __doWCPostBack(container, options.eventTarget, options.eventArgument);
    }
}

function Controls76_WContainer76_Builder(name, obj_id)
{
    this.name = name;

    this.obj = document.getElementById(obj_id + '_main');
    this.properties = new Array('Caption');
    
    this.get_prop = function(prop_name)
    {
        if (this.obj != null)
        {
            switch(prop_name)
            {
                case 'Caption':
                    if (this.obj.childNodes[1] && this.obj.childNodes[1].className == 'WPanel_headerDiv')
                        return this.obj.childNodes[1].innerText;
                    else if (this.obj.nodeName == 'TABLE' && this.obj.rows[0].firstChild.className == 'w76_panel_header')
                        return this.obj.rows[0].text;
                                                
                    break;
            }
            return '';
        }
    }

    this.set_prop = function(prop_name, prop_value)
    {
        if (this.obj != null)
        {
            switch(prop_name)
            {
                case 'Caption':
                    if (this.obj.childNodes[1] && this.obj.childNodes[1].className == 'WPanel_headerDiv')
                        this.obj.childNodes[1].innerText = prop_value;
                    else  if (this.obj.nodeName == 'TABLE' && this.obj.rows[0].firstChild.className == 'w76_panel_header')
                        this.obj.rows[0].firstChild.text = prop_value;
                    break;
            }
        }
    }
}

function Controls76_WPanel76_Builder(name, obj_id)
{
    this.name = name;

    this.obj = document.getElementById(obj_id + '_main');
    this.properties = new Array('IsUserVisible', 'Caption');
    
    this.get_prop = function(prop_name)
    {
        if (this.obj != null)
        {
            switch(prop_name)
            {
                case 'IsUserVisible':
                    return this.obj.style.display == 'none' ? 'False' : 'True';
                case 'Caption':
                    if (this.obj.childNodes[1] && this.obj.childNodes[1].className == 'WPanel_headerDiv')
                        return this.obj.childNodes[1].innerText;
                    else if (this.obj.nodeName == 'TABLE' && this.obj.rows[0].firstChild.className == 'w76_panel_header')
                        return this.obj.rows[0].text;
                                                
                    break;
            }
            return '';
        }
    }
    
    this.set_prop = function(prop_name, prop_value)
    {
        if (this.obj != null)
        {
            switch(prop_name)
            {
                case 'IsUserVisible':
                    this.obj.style.display = prop_value == 'True' ? '' : 'none';
                    break;
                case 'Caption':
                    if (this.obj.childNodes[1] && this.obj.childNodes[1].className == 'WPanel_headerDiv')
                        this.obj.childNodes[1].innerText = prop_value;
                    else  if (this.obj.nodeName == 'TABLE' && this.obj.rows[0].firstChild.className == 'w76_panel_header')
                        this.obj.rows[0].firstChild.text = prop_value;
                    break;
            }
        }
    }
}

function System_Web_UI_WebControls_TextBox_Builder(name1, obj_id)
{
    this.name = name1;
    this.obj = document.getElementById(obj_id);
    
    this.properties = new Array('Text', 'ReadOnly', 'Enabled');
    
    this.get_prop = function(prop_name)
    {
        if (this.obj != null)
        {
            switch(prop_name)
            {
                case 'Text':
                    return this.obj.value;
                case 'ReadOnly':
                    return this.obj.readOnly ? 'True' : 'False';
                case 'Enabled':
                    return this.obj.disabled ? 'False' : 'True';
            }
        }
        return "";
    }
    
    this.set_prop = function(prop_name, prop_value)
    {
        if (this.obj != null)
        {
            switch(prop_name)
            {
                case 'Text':
                    this.obj.value = decodeXML(prop_value);
                    break;
                case 'ReadOnly':
                    this.obj.readOnly = prop_value == 'True';
                    break;
                case 'Enabled':
                    this.obj.disabled = prop_value == 'False';
                    break;
            }
        }
    }
}

function System_Web_UI_WebControls_RadioButton_Builder(name, obj_id)
{
    if (arguments.length > 0)
        this.init(name, obj_id);
}

System_Web_UI_WebControls_RadioButton_Builder.prototype.init = function(name, obj_id)
{
    this.name = name;
    this.obj = document.getElementById(obj_id);
    
    this.properties = new Array('Checked', 'Enabled');
}

System_Web_UI_WebControls_RadioButton_Builder.prototype.get_prop = function(prop_name)
{
    if (this.obj != null)
    {
        switch(prop_name)
        {
            case 'Checked':
                return this.obj.checked ? 'True' : 'False';
            case 'Enabled':
                return this.obj.disabled ? 'False' : 'True';
        }
    }
    return "";
}
    
System_Web_UI_WebControls_RadioButton_Builder.prototype.set_prop = function(prop_name, prop_value)
{
    if (this.obj != null)
    {
        switch(prop_name)
        {
            case 'Checked':
                this.obj.checked = prop_value == 'True';
                break;
            case 'Enabled':
                this.obj.disabled = prop_value == 'False';
                this.obj.parentElement.disabled = this.obj.disabled;
                break;
        }
    }
}

System_Web_UI_WebControls_CheckBox_Builder.prototype = new System_Web_UI_WebControls_RadioButton_Builder();
System_Web_UI_WebControls_CheckBox_Builder.prototype.constructor = System_Web_UI_WebControls_CheckBox_Builder;
System_Web_UI_WebControls_CheckBox_Builder.superclass = System_Web_UI_WebControls_RadioButton_Builder.prototype;

function System_Web_UI_WebControls_CheckBox_Builder(name, obj_id)
{
    if (arguments.length > 0)
        System_Web_UI_WebControls_CheckBox_Builder.superclass.init.call(this, name, obj_id);
}

function System_Web_UI_WebControls_Label_Builder(name, obj_id)
{
    this.name = name;
    this.obj = document.getElementById(obj_id);
    
    this.properties = new Array('Text');

    this.get_prop = function(prop_name)
    {
        if (this.obj != null)
        {
            switch(prop_name)
            {
                case 'Text':
                    return this.obj.innerHTML.replace(/<\bbr[^>]*>/gi, '<br/>');
            }
        }
        return "";
    }
    
    this.set_prop = function(prop_name, prop_value)
    {
        if (this.obj != null)
        {
            switch(prop_name)
            {
                case 'Text':
                    this.obj.innerHTML = prop_value;
            }
        }
    }
}

function System_Web_UI_WebControls_LinkButton_Builder(name, obj_id)
{
    this.name = name;
    this.obj = document.getElementById(obj_id);
    
    this.properties = new Array('Text');

    this.get_prop = function(prop_name)
    {
        if (this.obj != null)
        {
            switch(prop_name)
            {
                case 'Text':
                    return this.obj.innerHTML.replace(/<\bbr[^>]*>/gi, '<br/>');
            }
        }
        return "";
    }
    
    this.set_prop = function(prop_name, prop_value)
    {
        if (this.obj != null)
        {
            switch(prop_name)
            {
                case 'Text':
                    this.obj.innerHTML = prop_value;
            }
        }
    }
}
