﻿




function screenSize() {
      var w, h; // Объявляем переменные, w - длина, h - высота
      w = (window.innerWidth ? window.innerWidth : (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.offsetWidth));
      h = (window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight));
      sh = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
      sw = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft;
       mw = (window.innerWidth ? window.innerWidth : document.body.offsetWidth);
      mh = (window.innerHeight ? window.innerHeight :  document.body.offsetHeight);
      return { w: w, h: h, mw: mw, mh: mh, sh: sh, sw: sw };
}
IsNullOrEmpty = function(str){
    return str == null || str == "";
}
AddQueryParameter = function(queryString, parameterName, parameterValue){
    if(parameterValue == null || parameterValue == "") return queryString;
    return queryString 
           + (queryString != "" ? "&" : "") 
           + parameterName + "=" + escape(parameterValue);
}
CreateNotEmptyValueString = function(name, value){
    if(IsNullOrEmpty(value)) return "";
    return name + value;
}

//Dictionary Manager Class
//=========================================================================
DictionaryManager = function() { this.m_dictionaries = new Object();}
DictionaryManager.prototype.GetDictionary = function(dictionaryID){ return this.m_dictionaries[dictionaryID]; }
DictionaryManager.prototype.AddDictionary = function(dictionaryID,dictionary) { this.m_dictionaries[dictionaryID] = dictionary; }
DictionaryManager.prototype.SelectNames = function(dictionaryID,items){
    var dictionary = this.m_dictionaries[dictionaryID];
    var names = new Array();
    var ids = items;
    for(var indexItem in ids){
        for(var indexDic in dictionary){
            if(dictionary[indexDic][0] == ids[indexItem]) names.push(" " + dictionary[indexDic][1]);
        }
    }
    return names.join();
}
DictionaryManager.Instance = new DictionaryManager();

//Url Choiser Class
//=========================================================================
UrlChoiserSingle = function(url) { this.m_url = url; }
UrlChoiserSingle.prototype.GetUrl = function() { return this.m_url; }

UrlChoiserList = function(url) { 
    this.m_urls = new Array(); 
    this.m_urls.push(url);
    this.m_position = 0;
    }
UrlChoiserList.prototype.GetUrl = function() {return this.m_urls[this.m_position];}
UrlChoiserList.prototype.AddUrl = function(url) {this.m_urls.push(url);}
UrlChoiserList.prototype.SetCurrentUrl = function(position) {
    if( this.m_urls.length > position && position >= 0 ) this.m_position = position;
}

SubmitModes = { RealTime: "RealTime", SubmitButton: "SubmitButton" }




//SearchForm Class
//=========================================================================
//SearchForm = function(name, urlchoiser, subName, schemaSearch, schemaRender, schemaData, renderParameter/*,callBackResult,callBackLoading,callBackError*/, direct, autoRefresh) {
SearchForm = function(searchParameter) {
    this.m_containerName = searchParameter.resultsContainer;
    this.SubmitMode = SubmitModes.RealTime;
    this.m_name = searchParameter.parameterName!=null?searchParameter.parameterName:"filter";
    this.m_url = (searchParameter.searchServiceUrl != null) ? searchParameter.searchServiceUrl : "Searchcontroller.ashx";
    this.m_inputs = new Array();
    this.AddInput('page', new PageSortInput((searchParameter.pageNo != null) ? searchParameter.pageNo : 1,
                                          (searchParameter.pageCount != null) ? searchParameter.pageCount : 0,
                                          (searchParameter.pageSize != null) ? searchParameter.pageSize : 1,
                                          (searchParameter.sort != null) ? searchParameter.sort : "",
                                          (searchParameter.direct != null)?searchParameter.direct:""));

    this.m_schemaSearch = searchParameter.searchSchema;
    this.m_schemaRender = searchParameter.renderSchema;
    this.m_schemaData = searchParameter.dataSchema;

    this.m_renderParameter = searchParameter.render!=null?searchParameter.render:"" + (searchParameter.formName != null) ? ";SearchFormClientID:" + searchParameter.formName + ";" : ""; ;
    this.m_filterParameter = searchParameter.filter;
    if (searchParameter.isPaging == false) {
        this.m_renderParameter += ";IsWithoutPaging:true;";
    }
    if (searchParameter.isSorting == false) {
        this.m_renderParameter += ";IsWithoutSorting:true;";
    }
    if (searchParameter.isOrder == false) {
        this.m_renderParameter += ";IsWithoutOrder:true;";
    }
    if (searchParameter.isPageSize == false) {
        this.m_renderParameter += ";IsWithoutPageSize:true;";
    }

    this.m_isChange = false;

    //IT'S AJAX

    var thisObject = this;
    this.ObjectsCallbacks = {
        loading: function() { Mian.Lib.EventManager.Instance.RaiseEvent("loading", thisObject); },
        success: function(result) { thisObject.m_isLoading = false; Mian.Lib.EventManager.Instance.RaiseEvent("onload", thisObject, result); },
        error: function() { Mian.Lib.EventManager.Instance.RaiseEvent("onloadError", thisObject); }
    }
    
    Mian.Lib.EventManager.Instance.AddEventHandler("loading", thisObject, function() {
        var container = document.getElementById(thisObject.m_containerName);
        if (container == null) return;
        container.innerHTML = 'идет загрузка данных';
    });
    
    Mian.Lib.EventManager.Instance.AddEventHandler("onload", thisObject, function(result) {
        var container = document.getElementById(thisObject.m_containerName);
        container.innerHTML = result;
        var clientCode = document.getElementById("_clientScriptSearchResult");
        if (clientCode != null) eval(clientCode.innerHTML);
    });

    this.Loading = null; //callBackLoading;
    this.ajax = new Mian.Lib.HttpRequest();

    if (searchParameter.WithoutAutoRefresh == null || searchParameter.WithoutAutoRefresh == false) {
        Mian.Lib.EventManager.Instance.AddEventHandler("DOM_PostLoad", document, function() {
            Mian.Lib.EventManager.Instance.RaiseEvent("Pre_AutoRefresh", thisObject);
            thisObject.Refresh();
            Mian.Lib.EventManager.Instance.RaiseEvent("Post_AutoRefresh", thisObject);
        })
    }

}

SearchForm.prototype.SetButtonSubmitMode = function() {
    this.SubmitMode = SubmitModes.SubmitButton;
}

SearchForm.prototype.ToQueryParameters = function() {
    var result = this.m_filterParameter + ";";
    for (var index in this.m_inputs) {
        var item = this.m_inputs[index];
        var value = item.input.ToQueryParameters();
        if (value != '') result += item.name + ":" + value + ";";
    }

    return result;
}

SearchForm.prototype.GetUrl = function() {
    var url = this.m_url;
    var ret = '';
    if (url.indexOf('?') == -1)
        ret = url + '?';
    else
        ret = url + '&';
    return ret;
}
SearchForm.prototype.GetQueryParameter = function(param) {
    var nameForm = (this.m_name != "") ? this.m_name + "=" : "=";
    var pading = "";
    if (this.m_pagingsort != null) pading = ";page:" + this.m_pagingsort.ToQueryParameters();
    var result = "";
    if (param != "")
        return nameForm + param + pading;
    else
        return nameForm + pading;
}
SearchForm.prototype.GetShemaParameter = function() {
    var result = "";
    result = AddQueryParameter(result, "search", this.m_schemaSearch);
    result = AddQueryParameter(result, "render", this.m_schemaRender);
    result = AddQueryParameter(result, "result", this.m_renderParameter);
    result = AddQueryParameter(result, "data", this.m_schemaData);
    result = AddQueryParameter(result, "name", this.m_subName);
    return result;
}
SearchForm.prototype.GetSubmitUrl = function() {
    var qparam = this.GetQueryParameter(this.ToQueryParameters());
    if (qparam != "") qparam = '&' + qparam;
    return this.GetUrl() + this.GetShemaParameter() + qparam;
}
SearchForm.prototype.OnIsChange = function() {
    this.m_isChange = true;
}
SearchForm.prototype.Change = function() {
    if (this.ajax.IsLoading() == false) {
        this.m_isChange = false;
        this.OnChange();
    }
}
SearchForm.prototype.OnChange = function() {
    if (this.SubmitMode == SubmitModes.RealTime) this.Refresh();
}
SearchForm.prototype.IssueEvent = function(e) {
    for (var index in this.m_inputs) {
        var item = this.m_inputs[index];
        item.input.ProcessEvent(e);
    }
}
SearchForm.prototype.Refresh = function() {
    if (this.ajax.IsLoading() == false) {
        if (this.Loading) this.Loading();
        this.ajax.Get(this.GetSubmitUrl(), this.ObjectsCallbacks);
    }
}


SearchForm.prototype.AddInput = function(name, input) {
    var z = new Object();
    var thisObject = this;
    input.Refresh = function() { thisObject.Refresh(); }
    input.Change = function() { thisObject.OnChange(); }
    input.IsChange = function() { thisObject.OnIsChange(); }
    input.Event = function(e) { thisObject.IssueEvent(e); }
    z.name = name;
    z.input = input;
    var isNew = true;
    for (var index in this.m_inputs)
        if (this.m_inputs[index].name == name) {
        isNew = false;
        this.m_inputs[index] = z;
    }
    if (isNew == true) this.m_inputs.push(z);
}
SearchForm.prototype.GetInput = function(name) {
    var result = null;
    for (var index in this.m_inputs)
        if (this.m_inputs[index].name == name)
        result = this.m_inputs[index];

    return result.input;
}

//FilterForm
//=========================================================================
FilterForm = function(name, urlchoiser, subName, schemaSearch, schemaRender, schemaData, renderParameter/*,callBackResult,callBackLoading,callBackError*/, direct, autoRefresh,isHistory) {
    this.CurrentURL = '';
    this.SubmitMode = SubmitModes.RealTime;
    this.m_name = name;
    this.m_urlChoiser = urlchoiser;

    this.m_pagingsort = null;

    this.m_schemaSearch = schemaSearch;
    this.m_schemaRender = schemaRender;
    this.m_schemaData = schemaData;
    this.m_subName = subName;

    this.m_renderParameter = renderParameter;
    this.m_inputs = new Array();
    this.m_isChange = false;

    //IT'S AJAX
    var thisObject = this;
    this.ObjectsCallbacks = {
        loading: function() { Mian.Lib.EventManager.Instance.RaiseEvent("loading", thisObject); },
        success: function(result) { thisObject.m_isLoading = false; Mian.Lib.EventManager.Instance.RaiseEvent("onload", thisObject, result); },
        error: function() { Mian.Lib.EventManager.Instance.RaiseEvent("onloadError", thisObject); },
        update: function(result) { thisObject.SetFilterFromUrl(result); }
    }
    this.SubscribeCallbacks = {
        success: function(result) { alert("Запрос сохранен"); },
        loading: function() { Mian.Lib.EventManager.Instance.RaiseEvent("loading", thisObject); },
        error: function() { Mian.Lib.EventManager.Instance.RaiseEvent("onloadError", thisObject); }
    }
    this.Loading = null; //callBackLoading;
    this.ajax = new Mian.Lib.HttpRequest(); //Mian.Lib.HttpRequest();
    if (autoRefresh == true) {
        Mian.Lib.EventManager.Instance.AddEventHandler("DOM_PostLoad", document, function() {
            if (isHistory) Mian.History.Instance.Init();
            Mian.Lib.EventManager.Instance.RaiseEvent("Pre_AutoRefresh", thisObject);
            thisObject.SetFilterFromLocation();
            thisObject.Refresh();
            Mian.Lib.EventManager.Instance.RaiseEvent("Post_AutoRefresh", thisObject);
        })
    }
    Mian.Lib.EventManager.Instance.AddEventHandler("HashChanged", Mian.History.Instance,function(eventArgs) { thisObject.OnHashChanged(eventArgs); });
}
FilterForm.prototype.SetButtonSubmitMode = function(){
    //this.SubmitMode = SubmitModes.SubmitButton;
}

FilterForm.prototype.SetFilterFromString = function(filters) {
    var z = 1;
    if (filters != '') {
        var parameters = filters.split(";")
        for (var index in this.m_inputs) {
            input = this.m_inputs[index];
            var isClear = true;
            for (var index1 in parameters) {
                value = parameters[index1];
                if (value.indexOf(input.name) == 0) {
                    input.input.SetValue(value.split(":")[1]);
                    isClear = false;
                }
            }
            if (isClear) input.input.Clear();
        }
    }
}

FilterForm.prototype.AddInput = function(name, input, title) {
    var z = new Object();
    var thisObject = this;
    input.Refresh = function() { thisObject.Refresh(); }
    input.Change = function() { thisObject.OnChange(); }
    input.IsChange = function() { thisObject.OnIsChange(); }
    input.Event = function(e) { thisObject.IssueEvent(e); }
    z.name = name;
    z.title = title;
    z.input = input;
    var isNew = true;
    for (var index in this.m_inputs)
        if (this.m_inputs[index].name == name) {
        isNew = false;
        this.m_inputs[index] = z;
    }
    if (isNew == true) this.m_inputs.push(z);
}
FilterForm.prototype.GetInput = function(name){   
    var result = null;
    for(var index in this.m_inputs)
      if(this.m_inputs[index].name == name)
        result = this.m_inputs[index];

    return result.input;
}
FilterForm.prototype.ToQueryParameters = function(){
   var result = "";
   for(var index in this.m_inputs)
   {
        var item = this.m_inputs[index];
        var value = item.input.ToQueryParameters();
        if(value!='') result += item.name + ":" + value + ";"; 
   } 
    
   return result;
}
FilterForm.prototype.GetUrl = function(){
    var url = this.m_urlChoiser.GetUrl();
    var ret = '';
    if(url.indexOf('?')==-1) 
        ret = url + '?';
        else
        ret = url + '&';
    return ret;
}
FilterForm.prototype.GetQueryParameter  = function(param){
    var nameForm = (this.m_name!="") ? this.m_name + "=" : "=";
    var pading = "";
    if(this.m_pagingsort!=null) pading = "page:" + this.m_pagingsort.ToQueryParameters();
    var result = "";
    if(param!="")
        return nameForm + param + pading;
        else
        return nameForm +  pading;
}
FilterForm.prototype.GetShemaParameter  = function(){
    var result = "";
    result = AddQueryParameter(result, "search", this.m_schemaSearch);
    result = AddQueryParameter(result, "render", this.m_schemaRender);
    result = AddQueryParameter(result, "result", this.m_renderParameter);
    result = AddQueryParameter(result, "data", this.m_schemaData);
    result = AddQueryParameter(result, "name", this.m_subName);  
    return result;
}
FilterForm.prototype.GetSubmitUrl = function(){
    var qparam = this.GetQueryParameter(this.ToQueryParameters());  
    if(qparam != "") qparam = '&' + qparam;
    return this.GetUrl()+ this.GetShemaParameter() + qparam;
}
FilterForm.prototype.ClearFilters = function() {
for (var index in this.m_inputs) {
    var input = this.m_inputs[index].input;
    if (input.Clear != null)
        input.Clear();
    }
}
FilterForm.prototype.OnIsChange = function(){
    this.m_isChange = true;
}
FilterForm.prototype.Change = function(){
    if(this.ajax.IsLoading() == false){
        this.m_isChange = false;
        this.OnChange();
    }
}
FilterForm.prototype.OnChange = function() {
    if (this.SubmitMode == SubmitModes.RealTime) {
        this.Refresh();
    }
}
FilterForm.prototype.IssueEvent = function(e){
   for(var index in this.m_inputs)
   {
        var item = this.m_inputs[index];
        item.input.ProcessEvent(e);
   } 
}

FilterForm.prototype.OnRefresh = function() {
    var t = 1;
    if (this.ajax.IsLoading() == false) {
        if (this.Loading) this.Loading();
        if (this.m_urlChoiser.m_position == 0) {
            var parameter = this;
            this.ajax.Get(this.GetSubmitUrl(), this.ObjectsCallbacks);
        }
        else
            this.ajax.Get(this.GetSubmitUrl(), this.SubscribeCallbacks);
    }
}

FilterForm.prototype.Refresh = function() {
    this.OnRefresh();
    this.AddHistoryPoint('', this.GetQueryParameter(this.ToQueryParameters()));
}


FilterForm.prototype.AddHistoryPoint = function(title, url) {
    if (Mian.History.Instance && Mian.History.Instance.IsHistory) {
        Mian.History.Instance.SetHash(url);
    }
}

FilterForm.prototype.GetBackUrl = function(anchor){
    var cutStr = document.location.href;
    var cutIndex = cutStr.indexOf('?filter=');
    if(cutIndex == -1) cutIndex = cutStr.length;
    var resSrt = cutStr.substr(0, cutIndex);
    var page = this.GetInput('page');
    if(page!=null) page.m_Flag = true;
    return anchor.href + '&filter=' + this.ToQueryParameters();
}

FilterForm.prototype.SetFilterFromLocation = function()
{
    var tmp = document.location.hash.split('filter=');
    if (tmp.length > 1) {
        var filter = tmp[1];
        this.SetFilterFromString(filter);
    }
}

FilterForm.prototype.OnHashChanged = function() {
    this.SetFilterFromLocation();
    this.OnRefresh();
}

FilterForm.prototype.ToFilterValues = function() {
    var result = "";
    for (var index in this.m_inputs) {
        var item = this.m_inputs[index];
        var value = item.input.ToString();
        if (value != '' && item.title) result += "<br />" + item.title + ":" + value + ";";
    }
    return result;
}
//Input Class
//=========================================================================
Input = function(){}
Input.prototype.Clear = function(){}
Input.prototype.ToString = function(){ return ""; }
Input.prototype.IsEmpty = function(){ return this.ToString() == ""; }
Input.prototype.ToQueryParameters = function() { return ""; }
Input.prototype.AttachEvents = function() { }
Input.prototype.OnChange = function(){ 
        var t ='';
        if(this.Change != null) 
                    this.Change(); }
Input.prototype.OnIsChange = function(){ if(this.IsChange != null) this.IsChange(); }
Input.prototype.IssueEvent = function(e){ if(this.Event!=null) this.Event(e); }
Input.prototype.ProcessEvent = function(e){}
Input.prototype.OnRefresh = function() { if (this.Refresh != null) this.Refresh(); }
Input.prototype.SetValue = function(parameters) { }

// CheckListInput Class
//=========================================================================
CheckListInput = function(containerID, isChange, dictionaryID, checkeds, nameEvent, valueEvent) {
    this.m_nameEvent = nameEvent;
    this.m_valueEvent = valueEvent;
    this.m_isActive = true;
    this.m_container = document.getElementById(containerID);
    this.m_checkeds = checkeds;
    this.m_dictionaryID = dictionaryID;
    var ChecklistCallBackChange = this;

    for (var index in this.m_checkeds) {
        checked = document.getElementById(this.m_checkeds[index][0]);
        var e = new Object();
        e.name = dictionaryID;
        e.value = this.m_checkeds[index][1];
        if (isChange == true) checked.onclick = function() { e.value = ChecklistCallBackChange.GetValue(this.id); e.IsActive = this.checked; ChecklistCallBackChange.IssueEvent(e); ChecklistCallBackChange.OnChange(); };
    }
}
CheckListInput.prototype = new Input;
CheckListInput.prototype.ProcessEvent =function(e)
{
    if(e.name == this.m_nameEvent && e.value == this.m_valueEvent)
    {
        if(e.IsActive) 
        {
            this.m_isActive = true;    
            this.m_container.style.display='block'
        }
        else 
        {
            this.m_isActive = false;    
            this.m_container.style.display='none';
        }
    }
}
CheckListInput.prototype.ToString = function(){
    var checkeds = this.GetCheckedValue();
    return DictionaryManager.Instance.SelectNames(this.m_dictionaryID,checkeds);
}
CheckListInput.prototype.IsEmpty = function(){
   for(var index in this.m_checkeds)
        if(document.getElementById(this.m_checkeds[index][0]).checked) return false; 
   return true;
}
CheckListInput.prototype.GetValue = function(id){
   for(var index in this.m_checkeds)
        if(this.m_checkeds[index][0] == id) return this.m_checkeds[index][1]; 
   return '';
}
CheckListInput.prototype.ToQueryParameters = function(){
    if(this.m_isActive == false) return '';
    var checkeds = this.GetCheckedValue();
    if(checkeds.length > 0) return checkeds.join();
    return '';
}
CheckListInput.prototype.GetCheckedValue = function(){
    var checkeds = new Array();
    for(var index in this.m_checkeds)
        if(document.getElementById(this.m_checkeds[index][0]).checked)
        checkeds.push(this.m_checkeds[index][1]);
    return checkeds;
}

CheckListInput.prototype.Clear = function() {
    for (var index in this.m_checkeds)
        document.getElementById(this.m_checkeds[index][0]).checked = false;

    Mian.Lib.EventManager.Instance.RaiseEvent("Clear", this);
}

CheckListInput.prototype.SetValue = function(parameters) {
    values = parameters.split(',');
    for (var index in this.m_checkeds) {
        document.getElementById(this.m_checkeds[index][0]).checked = false;
        for (var index1 in values)
            if (this.m_checkeds[index][1] == values[index1])
            document.getElementById(this.m_checkeds[index][0]).checked = true;
    }
    Mian.Lib.EventManager.Instance.RaiseEvent("Update", this);
}

// HiddenInput Class
//=========================================================================        
ValueInput = function(data, isAddToQueryParameters) { 
    this.m_data = data; 
    this.m_isAddToQueryParameters = isAddToQueryParameters;
}
ValueInput.prototype = new Input;
ValueInput.prototype.ToQueryParameters = function(){
    if(this.m_isAddToQueryParameters == null || this.m_isAddToQueryParameters == false) return "";
    return this.m_data;
}
ValueInput.prototype.SetValue = function(parameters) {
    this.m_data = parameters;
}
ValueInput.prototype.Clear = function() {
    //this.m_data = "";
}
// TextInput Class
//=========================================================================        
TextInput = function(data) { 
    this.m_data = document.getElementById(data); 
    this.AttachEvents();
    this.isChange = false;
}
TextInput.prototype = new Input;
TextInput.prototype.ToQueryParameters = function(){
    return escape(this.m_data.value);
}
TextInput.prototype.AttachEvents = function() {
    var thisObject = this;
    thisObject.timerId = null;
    var changeHandler = function() { if (event.keyCode == 13) { thisObject.OnChange(); return false; } };
    if (document.addEventListener) {
        this.m_data.addEventListener("change", function() { thisObject.OnChange();}, true);
        this.m_data.addEventListener("keydown", changeHandler, true);
    } else if (document.attachEvent) {
        this.m_data.attachEvent("onchange", function() { thisObject.OnChange(); });
        this.m_data.attachEvent("onkeydown", changeHandler);
    }
}
TextInput.prototype.SetValue = function(parameters) {
    this.m_data.value = unescape(parameters);
}
TextInput.prototype.Clear = function() {
    this.m_data.value = '';
}
TextInput.prototype.ToString = function() {
    return this.m_data.value;
}
// LinkedSelectsInput Class
//=========================================================================
//Class used Array Of DictionaryItemWithChildsResult
//DictionaryItemResult = function(){
//    var Key = "childKey1";
//    var Value = "childValue1";
//}
//DictionaryItemWithChildsResult = function(){
//    var Key = "parentKey1";
//    var Value = "parentValue1";
//    var Childs = Array Of DictionaryItemResult
//}
LinkedSelectsInput = function(parentSelectID, childSelectID, data){
    this.m_parentSelect = document.getElementById(parentSelectID);
    this.m_childSelect = document.getElementById(childSelectID);
    this.m_data = data;
    this.AttachEvents();
} 
LinkedSelectsInput.prototype = new Input;
LinkedSelectsInput.prototype.ToQueryParameters = function(){
    return this.m_parentSelect.options[this.m_parentSelect.selectedIndex].value + "," + this.m_childSelect.options[this.m_childSelect.selectedIndex].value; 
}
LinkedSelectsInput.prototype.AttachEvents = function(){
    var thisObject = this;
    var changeParentHandler = function() { thisObject.OnParentChange(); }
    var changeChildHandler = function() { thisObject.OnChange(); }
    if(document.addEventListener){
        this.m_parentSelect.addEventListener("change", changeParentHandler, true);
        this.m_childSelect.addEventListener("change", changeChildHandler, true);
    }else if(document.attachEvent){
        this.m_parentSelect.attachEvent("onchange", changeParentHandler);
        this.m_childSelect.attachEvent("onchange", changeChildHandler);
    }                       
}
LinkedSelectsInput.prototype.OnParentChange = function(){
    var selectedParentKey = this.m_parentSelect.options[this.m_parentSelect.selectedIndex].value;
    for(var index in this.m_data){
        if(this.m_data[index].Key == selectedParentKey){
            this.m_childSelect.options.length = 0;
            var childs = this.m_data[index].Childs;
            for(var childIndex in childs){
                this.m_childSelect.options[this.m_childSelect.options.length] = new Option(childs[childIndex].Value, childs[childIndex].Key);    
            }
        }
    }
    if(this.Change != null) this.Change();
}          
//SelectInput Class
//=========================================================================
SelectInput = function(selectID,isChange){
    this.m_select = document.getElementById(selectID);
    if(isChange == true) this.AttachEvents();
} 
SelectInput.prototype = new Input;
SelectInput.prototype.ToQueryParameters = function(){
    return this.m_select.options[this.m_select.selectedIndex].value; 
}
SelectInput.prototype.AttachEvents = function(){
    var thisObject = this;
    var changeHandler = function() { thisObject.OnChange(); }
    if(document.addEventListener){
        this.m_select.addEventListener("change", changeHandler, true);
    }else if(document.attachEvent){
        this.m_select.attachEvent("onchange", changeHandler);
    }                       
}
SelectInput.prototype.OnChange = function(){ 
    if(this.Change != null && this.m_select.options[this.m_select.selectedIndex].value != "") this.Change();
}
SelectInput.prototype.SetValue = function(parameters) {
    for (var index = 0; index < this.m_select.options.length; index++) {
        if (this.m_select.options[this.m_select.selectedIndex].value == parameters)
            this.m_select.selectedIndex = index;
    }
}
SelectInput.prototype.Clear = function() {
    this.m_select.selectedIndex = -1;
}
SelectInput.prototype.ToString = function() {
    return this.m_select.options[this.m_select.selectedIndex].title;
}
//SelectInput Class
//=========================================================================
SelectInputAdmin = function(selectID){
    this.m_select = document.getElementById(selectID);
    this.AttachEvents();
}
SelectInputAdmin.prototype = new Input;
SelectInputAdmin.prototype.ToQueryParameters = function() {
    return this.m_select.options[this.m_select.selectedIndex].value; 
}
SelectInputAdmin.prototype.AttachEvents = function() {
    var thisObject = this;
    var changeHandler = function() { thisObject.OnChange(); }
    if(document.addEventListener){
        this.m_select.addEventListener("change", changeHandler, true);
    }else if(document.attachEvent){
        this.m_select.attachEvent("onchange", changeHandler);
    }                       
}
SelectInputAdmin.prototype.OnChange = function() { if (this.Change != null && this.m_select.options[this.m_select.selectedIndex].value != "") this.Change(); }
// PopupDialog Class
//=========================================================================
PopupDialog = function(dialogID,cssLayer,cssShadow){
    this.m_dialog = document.getElementById(dialogID);
    this.m_parent = this.m_dialog.parentNode;
    this.m_shadow = document.createElement("DIV");
    this.m_shadow.className = cssShadow;
    this.m_layer = document.createElement("DIV");
    this.m_layer.className = cssLayer;
    this.m_parent.appendChild(this.m_layer);
    this.m_parent.appendChild(this.m_shadow);
}
PopupDialog.prototype.ShowDialog = function(){
    this.m_dialog.style.Zindex = 2000;
    this.m_dialog.style.position='absolute';
    this.m_dialog.style.display='block';
    var size = screenSize();
    
    var height = this.m_dialog.clientHeight;
    var width = this.m_dialog.clientWidth;
    var top = size.h/2 - height/2 + size.sh;
    var left = size.w/2 - width/2 + size.sw;
    
    this.m_layer.style.position='absolute';
    this.m_layer.style.display='block';
    this.m_layer.style.top = 0;//coord.top+'px';
    this.m_layer.style.left = 0;//coord.left+'px';
    this.m_layer.style.width = size.mw + 'px';
    this.m_layer.style.height = size.mh + 'px';
    this.m_layer.style.filter = "alpha(opacity=80)";
    this.m_layer.style.opacity = 0.8;
        
    this.m_dialog.style.position='absolute';
    this.m_dialog.style.top  = top + 'px';
    this.m_dialog.style.left = left + 'px';
       
    this.m_shadow.style.position='absolute';
    this.m_shadow.style.top  = top + 5 + 'px';//coord.top+'px';
    this.m_shadow.style.left = left + 5 + 'px';//coord.left+'px';
    this.m_shadow.style.width = width;
    this.m_shadow.style.height = height;
    this.m_shadow.style.display='block';
}
PopupDialog.prototype.CloseDialog = function(){
    this.m_dialog.style.display='none';
    this.m_layer.style.display='none';
    this.m_shadow.style.display='none';
}
// MoneyInput Class
//=========================================================================
MoneyInput = function(dictionaryID, beginID, endID, currencyID, unitID){
    //this.m_container = document.getElementById(containerID);
    this.m_dictionaryID = dictionaryID;
    this.m_unit = document.getElementById(unitID);
    this.m_beginControl = document.getElementById(beginID);
    this.m_endControl = document.getElementById(endID);
    this.m_currency = document.getElementById(currencyID);
    this.AttachEvents();
}
MoneyInput.prototype = new Input;
MoneyInput.prototype.ToString = function(){
    if(this.IsEmpty()) return "";
    var strRet= "";
    strRet += CreateNotEmptyValueString(" от ", this.m_beginControl.value);
    strRet += CreateNotEmptyValueString(" до ", this.m_endControl.value);
    var selectedItemNames = DictionaryManager.Instance.SelectNames(this.m_dictionaryID,this.m_currency.options[this.m_currency.selectedIndex].value.split(','));
    strRet += CreateNotEmptyValueString(" ", selectedItemNames);
    return strRet;
}
MoneyInput.prototype.IsEmpty = function(){
    if(this.m_beginControl.value != "" || this.m_endControl.value != "" ) return false;
    return true;
}
MoneyInput.prototype.ToQueryParameters = function(){
    if(this.IsEmpty()) return "";
    return  this.m_currency.options[this.m_currency.selectedIndex].value 
            + "," + this.m_unit.value  
            + "," + Mian.Lib.ParseToInt(this.m_beginControl.value)
            + "," + Mian.Lib.ParseToInt(this.m_endControl.value);
}
MoneyInput.prototype.AttachEvents = function() {
    var thisObject = this;
    var changeHandler = function() { thisObject.OnIsChange(); }
    var enterHandler = function() { if (event.keyCode == 13) { thisObject.OnChange(); return false; } }
    if (document.addEventListener) {
        //this.m_beginControl.addEventListener("change", changeHandler, true);
        //this.m_endControl.addEventListener("change", changeHandler, true);
        this.m_beginControl.addEventListener("keydown", enterHandler, true);
        this.m_endControl.addEventListener("keydown", enterHandler, true);
    } else if (document.attachEvent) {
        //this.m_beginControl.attachEvent("onchange", changeHandler);
        //this.m_endControl.attachEvent("onchange", changeHandler);
        this.m_beginControl.attachEvent("onkeydown", enterHandler);
        this.m_endControl.attachEvent("onkeydown", enterHandler);
    }
}
MoneyInput.prototype.GetScaleName = function(){
    var result = this.m_currency.options[this.m_currency.selectedIndex].value 
            + "_" + this.m_unit.value;
    return result;
}
MoneyInput.prototype.SetValue = function(parameters) {
    var values = parameters.split(',');
    for (var index = 0; index < this.m_currency.options.length; index++) {
        if (this.m_currency.options[index].value == values[0])
            this.m_currency.selectedIndex = index;
    }
    this.m_unit.value = values[1];
    this.m_beginControl.value = values[2];
    this.m_endControl.value = values[3];
    Mian.Lib.EventManager.Instance.RaiseEvent("Update", this);
}
MoneyInput.prototype.Clear = function() {
    this.m_currency.selectedIndex = 0;
    this.m_beginControl.value = '';
    this.m_endControl.value = '';
    Mian.Lib.EventManager.Instance.RaiseEvent("Clear", this);
}
//PageInput Class
//=========================================================================
PageSortInput = function(pageNo,pageCount,pageSize,sort,direct){
    this.m_pageNo = pageNo;
    this.m_pageSize = pageSize; 
    this.m_pageCount = pageCount;
    this.m_sort = sort;
    this.m_direct = direct;
    this.m_Flag = false;
}
PageSortInput.prototype = new Input;
PageSortInput.prototype.ToString = function(){
    if(this.IsEmpty()) return "";    
    var strRet = "";
    if(this.m_current!="") strRet += " страница:" +  this.m_pageNo;
    if(this.m_pageSize!="") strRet += " размер страницы:" +  this.m_pageSize;    
    if(this.m_Sort!="") strRet += " сортировка:" +  this.m_sort;    
    return strRet;
}
PageSortInput.prototype.IsEmpty = function(){
    if(this.m_current != "" || this.m_pageSize != "" || this.m_sort != "") return false;
    return false;
}
PageSortInput.prototype.ToQueryParameters = function(){
    if(this.IsEmpty()) return "";
    if(this.m_Flag == false) this.m_pageNo = 1;
    return this.m_pageNo 
           + ',' + this.m_pageSize 
           + ',' + this.m_sort + '_' + this.m_direct;
}
PageSortInput.prototype.NextPage = function(){
    this.m_pageNo -= 0;
    if(parseInt(this.m_pageNo) < parseInt(this.m_pageCount)){
        this.m_pageNo += 1;
        this.m_Flag = true;
        this.OnRefresh();
    }
}
PageSortInput.prototype.PrevPage = function(){
    this.m_pageNo -= 0;
    if(parseInt(this.m_pageNo) > 1){ 
        this.m_pageNo -= 1;
        this.m_Flag = true;
        this.OnRefresh();
        //this.OnChange();
    }
}
PageSortInput.prototype.SizePage = function(pageSize){
      this.m_pageSize = parseInt(pageSize);
      this.m_Flag = true;
      this.OnRefresh();
}
PageSortInput.prototype.GotoPage = function(page) {
    if(parseInt(page)<= parseInt(this.m_pageCount)){
        this.m_pageNo = parseInt(page);
        this.m_Flag = true;
        this.OnRefresh();
    }
}
PageSortInput.prototype.Sorting = function(sort){
    this.m_pageNo = 1;
    this.m_direct = this.m_direct == "asc" ? "desc" : "asc";
    this.m_sort = sort;
    this.m_Flag = true;
    this.OnRefresh();
}
// RangeInput Class
//=========================================================================
RangeInput = function(beginID, endID, name, meas) {
    this.m_name = name;
    this.m_meas = meas;
    this.m_beginID = beginID;
    this.m_endID = endID;
    this.m_beginControl = document.getElementById(beginID);
    this.m_endControl = document.getElementById(endID);
    this.AttachEvents();
}
 RangeInput.prototype = new Input;
 RangeInput.prototype.AttachEvents = function() {
     var thisObject = this;
     var ischangeHandler = function() { thisObject.OnIsChange(); }
     var enterHandler = function() { if (event.keyCode == 13) { thisObject.OnChange(); return false; } }
     if (document.addEventListener) {
         //this.m_beginControl.addEventListener("change", ischangeHandler, true);
         //this.m_endControl.addEventListener("change", ischangeHandler, true);
         this.m_beginControl.addEventListener("keydown", enterHandler, true);
         this.m_endControl.addEventListener("keydown", enterHandler, true);
     } else if (document.attachEvent) {
         //this.m_beginControl.attachEvent("onchange", ischangeHandler);
         //this.m_endControl.attachEvent("onchange", ischangeHandler);
         this.m_beginControl.attachEvent("onkeydown", enterHandler);
         this.m_endControl.attachEvent("onkeydown", enterHandler);
     }
 }
RangeInput.prototype.ToString = function(){
    if(this.IsEmpty()) return "";
    var strRet = "";
    strRet += CreateNotEmptyValueString(" от:", this.m_beginControl.value);
    strRet += CreateNotEmptyValueString(" до:", this.m_endControl.value);
    strRet += CreateNotEmptyValueString(" ", this.m_meas);
    return strRet;
}
RangeInput.prototype.IsEmpty = function(){
    if(this.m_beginControl.value != "" || this.m_endControl.value != "") return false;
    return true;
}
RangeInput.prototype.ToQueryParameters = function(){
    if(this.IsEmpty()) return "";
    var result = "";
    result = this.m_beginControl.value 
             + "," + 
             this.m_endControl.value;
    result += CreateNotEmptyValueString(",", this.m_name);
    return  result;
}
RangeInput.prototype.OnChange = function(){
    if(this.Change != null) this.Change();
}
RangeInput.prototype.SetValue = function(parameters) {
    var values = parameters.split(',');
    this.m_beginControl.value = values[0];
    this.m_endControl.value = values[1];
}
RangeInput.prototype.Clear = function() {
    this.m_beginControl.value = '';
    this.m_endControl.value = '';
}

// RangeArrayInput Class
//=========================================================================
RangeArrayInput = function(dictionaryID, beginID, endID, unitID) {
    this.m_dictionaryID = dictionaryID;
    this.m_unit = document.getElementById(unitID);
    this.m_beginControl = document.getElementById(beginID);
    this.m_endControl = document.getElementById(endID);
    this.AttachEvents();
}
RangeArrayInput.prototype = new Input;
RangeArrayInput.prototype.ToString = function() {
    if (this.IsEmpty()) return "";
    var strRet = "";
    strRet += CreateNotEmptyValueString(" от ", this.m_beginControl.value);
    strRet += CreateNotEmptyValueString(" до ", this.m_endControl.value);
    var selectedItemNames = DictionaryManager.Instance.SelectNames(this.m_dictionaryID, this.m_unit.options[this.m_unit.selectedIndex].value.split(','));
    strRet += CreateNotEmptyValueString(" ", selectedItemNames);
    return strRet;
}
RangeArrayInput.prototype.IsEmpty = function() {
    if (this.m_beginControl.value != "" || this.m_endControl.value != "") return false;
    return true;
}
RangeArrayInput.prototype.ToQueryParameters = function() {
    if (this.IsEmpty()) return "";
    return  Mian.Lib.ParseToInt(this.m_beginControl.value)
            + "," + Mian.Lib.ParseToInt(this.m_endControl.value)
            + "," +this.m_unit.options[this.m_unit.selectedIndex].value;
}
RangeArrayInput.prototype.AttachEvents = function() {
    var thisObject = this;
    var changeHandler = function() { thisObject.OnIsChange(); }
    var enterHandler = function() { if (event.keyCode == 13) { thisObject.OnChange(); return false; } }
    if (document.addEventListener) {
        this.m_beginControl.addEventListener("change", changeHandler, true);
        this.m_endControl.addEventListener("change", changeHandler, true);
        this.m_beginControl.addEventListener("keydown", enterHandler, true);
        this.m_endControl.addEventListener("keydown", enterHandler, true);
    } else if (document.attachEvent) {
        this.m_beginControl.attachEvent("onchange", changeHandler);
        this.m_endControl.attachEvent("onchange", changeHandler);
        this.m_beginControl.attachEvent("onkeydown", enterHandler);
        this.m_endControl.attachEvent("onkeydown", enterHandler);
    }
}
RangeArrayInput.prototype.GetScaleName = function() {
    var result = this.m_unit.options[this.m_unit.selectedIndex].value;
    return result;
}
RangeArrayInput.prototype.SetValue = function(parameters) {
    var values = parameters.split(',');
    this.m_beginControl.value = values[0];
    this.m_endControl.value = values[1];
    for (var index = 0; index < this.m_unit.options.length; index++) {
        if (this.m_unit.options[index].value == values[2])
            this.m_unit.selectedIndex = index;
    }
}
RangeArrayInput.prototype.Clear = function() {
    this.m_beginControl.value = '';
    this.m_endControl.value = '';
    this.m_unit.selectedIndex = -1;
}

// RangeInput Class
//=========================================================================
RangeIntInput = function(beginID,endID,name,meas){
    this.m_name = name;
    this.m_meas = meas;
    this.m_beginID = beginID;
    this.m_endID = endID;
    this.m_beginControl = document.getElementById(beginID);
    this.m_endControl = document.getElementById(endID);
    var ChecklistCallBackChange = this;
    this.AttachEvents();
 }
 RangeIntInput.prototype = new Input;
 RangeIntInput.prototype.AttachEvents = function(){
    var thisObject = this;
    var changeHandler = function() {thisObject.OnChange();}
    var enterHandler = function() { if (event.keyCode == 13) { thisObject.OnChange(); return false; } }
    if (document.addEventListener) {
        //this.m_beginControl.addEventListener("change", changeHandler, true);
        //this.m_endControl.addEventListener("change", changeHandler, true);
        this.m_beginControl.addEventListener("keydown", enterHandler, true);
        this.m_endControl.addEventListener("keydown", enterHandler, true);
    } else if (document.attachEvent) {
        //this.m_beginControl.attachEvent("onchange", changeHandler);
        //this.m_endControl.attachEvent("onchange", changeHandler);
        this.m_beginControl.attachEvent("onkeydown", enterHandler);
        this.m_endControl.attachEvent("onkeydown", enterHandler);
    }                
}
RangeIntInput.prototype.ToString = function(){
    if(this.IsEmpty()) return "";
    var strRet = "";
    strRet += CreateNotEmptyValueString(" от:", this.m_beginControl.value);
    strRet += CreateNotEmptyValueString(" до:", this.m_endControl.value);
    strRet += CreateNotEmptyValueString(" ", this.m_meas);
    return strRet;
}
RangeIntInput.prototype.IsEmpty = function(){
    if(this.m_beginControl.value != "" || this.m_endControl.value != "") return false;
    return true;
}
RangeIntInput.prototype.ToQueryParameters = function(){
    if(this.IsEmpty()) return "";
    var result = "";
    result = Mian.Lib.ParseToInt(this.m_beginControl.value) 
             + "," + 
             Mian.Lib.ParseToInt(this.m_endControl.value);
    result += CreateNotEmptyValueString(",", this.m_name);
    return  result;
}
RangeIntInput.prototype.OnChange = function(){
    if(this.Change != null) this.Change();
}
RangeIntInput.prototype.SetValue = function(parameters) {
    var values = parameters.split(',');
    this.m_beginControl.value = values[0];
    this.m_endControl.value = values[1];
    this.m_name = values[2];
    Mian.Lib.EventManager.Instance.RaiseEvent("Update", this);
}
RangeIntInput.prototype.Clear = function() {
    this.m_beginControl.value = '';
    this.m_endControl.value = '';
    Mian.Lib.EventManager.Instance.RaiseEvent("Clear", this);
}
// MetroInput Class
//=========================================================================
MetroInput = function(containerID,hiddenID, dictionaryID,nameEvent,valueEvent){
    this.m_nameEvent = nameEvent;
    this.m_valueEvent = valueEvent;
    this.m_isActive = true;
    this.m_container = document.getElementById(containerID);
    
    this.m_hidden = document.getElementById(hiddenID);
    this.m_dictionaryID = dictionaryID;
    var ChecklistCallBackChange = this;
    //if(this.m_hidden != null)this.m_hidden.onchange = function(){ChecklistCallBackChange.OnChange();};
 }
MetroInput.prototype =  new Input;
MetroInput.prototype.Clear = function(){
     this.m_hidden.value = "";
}
MetroInput.prototype.ToString = function(){
    var result = "";
    result = DictionaryManager.Instance.SelectNames(this.m_dictionaryID,this.m_hidden.value.split(','));
    return result;
}
MetroInput.prototype.IsEmpty = function(){
    return this.m_hidden.value=="";
}
MetroInput.prototype.ToQueryParameters = function(){
    if(this.IsEmpty()|| this.m_isActive == false) return '';
    return this.m_hidden.value;
}
MetroInput.prototype.ProcessEvent = function(e){
    if(e.name == this.m_nameEvent && e.value == this.m_valueEvent){
        if(e.IsActive) {
            this.m_isActive = true;    
            this.m_container.style.display='block'
        } else {
            this.m_isActive = false;    
            this.m_container.style.display='none';
        }
    }
}
MetroInput.prototype.SetValue = function(parameters) {
    this.m_hidden.value = parameters;
}
MetroInput.prototype.Clear = function() {
    this.m_hidden.value = '';
    Mian.Lib.EventManager.Instance.RaiseEvent("Clear", this);
}
// FloorInput Class
//=========================================================================
FloorInput = function(beginID,endID,firstID,lastID){
    this.m_beginControl = document.getElementById(beginID);
    this.m_endControl = document.getElementById(endID);
    this.m_first = document.getElementById(firstID);
    this.m_last = document.getElementById(lastID);
    this.AttachEvents();
}
FloorInput.prototype = new Input;
FloorInput.prototype.Clear = function(){
    his.m_beginControl.value = "";
    this.m_endControl.value = "";
    this.m_first.checked = false;
    this.m_first.checked = false;
}
FloorInput.prototype.ToString = function(){
    if(this.IsEmpty()) return "";
    var strRet = "";
    strRet += CreateNotEmptyValueString(" от:", this.m_beginControl.value);
    strRet += CreateNotEmptyValueString(" до:", this.m_endControl.value);
    if(this.m_first.checked) strRet += " кроме первого";
    if(this.m_last.checked) strRet += " кроме последнего";
    
    return strRet;
}
FloorInput.prototype.IsEmpty = function(){
    if(this.m_beginControl.value != "" || this.m_endControl.value != "" || this.m_first.checked || this.m_last.checked) return false;
    return true;
}
FloorInput.prototype.ToQueryParameters = function(){
    if(this.IsEmpty()) return "";
    return this.m_beginControl.value + "," + this.m_endControl.value + "," + this.m_first.checked + "," + this.m_last.checked;
}
FloorInput.prototype.AttachEvents = function(){
    var thisObject = this;
    var changeHandler = function() { thisObject.OnChange(); }
    if(document.addEventListener){
        this.m_beginControl.addEventListener("change", changeHandlerr, true);
        this.m_endControl.addEventListener("change", changeHandler, true);
        this.m_first.addEventListener("click",changeHandler,true);
        this.m_last.addEventListener("click",changeHandler,true);       
    } else if(document.attachEvent){
        this.m_beginControl.attachEvent("onchange", changeHandler);
        this.m_endControl.attachEvent("onchange", changeHandler);
        this.m_first.attachEvent("onclick",changeHandler,true);
        this.m_last.attachEvent("onclick",changeHandler,true)
    }                       
}
FloorInput.prototype.OnChange = function(){
    if(this.Change != null) this.Change();
}
FloorInput.prototype.SetValue = function(parameters) {
    var values = parameters.split(',');
    this.m_beginControl.value = values[0];
    this.m_endControl.value = values[1];
    if (values[2] == 'true') this.m_first.checked = true;
    if (values[3] == 'true') this.m_last.checked = true;
}
FloorInput.prototype.Clear = function() {
    this.m_beginControl.value = '';
    this.m_endControl.value = '';
    this.m_first.checked = false;
    this.m_last.checked = false;
}



