﻿

ATI.Ajax.Scripts.SearchManager = function() {
    ATI.Ajax.Scripts.SearchManager.initializeBase(this);
}

ATI.Ajax.Scripts.SearchManager.prototype = {
    _searchMode: ATI.Ajax.Scripts.SearchMode.Default
    , _searchAttrubute: "searchName"
    , SetSearchMode: function(searchMode) {
        this._searchMode = searchMode;
    }
    
    , GetSearchMode: function() {
        return this._searchMode;
    }

    , DoSearch: function(p_sEntityType, p_sUrl, p_bBackwards, p_handler) {
        if (p_handler == null)
            p_handler = function(p_str) { window.location.href = p_str; }
        try {
            var str = BuildQueryString(String.format("{0}?EntityType={1}", p_sUrl, p_sEntityType), p_bBackwards);
            if (str != '') {
                p_handler(str);
                return false;
            }
        }
        catch (e) {
            Sys.Debug.traceDump(e);
        }
        return true;
    }
    , DoSearchTrace: function(p_sUrl, p_handler) {
        if (p_handler == null)
            p_handler = function(p_str) { window.location.href = p_str; }
        try {
            var str = BuildQueryString(String.format("{0}?EntityType=Trace", p_sUrl));
            if (str != '') {
                p_handler(str);
                return false;
            }
        }
        catch (e) {
            Sys.Debug.traceDump(e);
        }
        return true;
    }
    , GetSearchAttribute: function(ctl) {
        var searchAttr = ctl.getAttribute(this._searchAttrubute);
        if (searchAttr == null || searchAttr == '') {
            searchAttr = ctl.parentNode.getAttribute(this._searchAttrubute);
        }
        return searchAttr;
    }
    , SetSearchAttribute: function(ctl, value) {
        ctl.setAttribute(this._searchAttrubute, value);
    }
    , AppendValue: function(p_sb, p_Control, p_value) {
        if (p_value != null)
            p_sb.append(String.format("&{0}={1}", this.GetSearchAttribute(p_Control), p_value));
    }
    , GetDropDownListValue: function(p_chk) {
        return p_chk.checked ? true : null;
    }
    , GetAutoCompleteValue: function(p_ctl) {
        var txt = $get(p_ctl.getAttribute('TextBoxId'));
        var text = txt.value;
        if (text == '')
            return null;
        var val = $get(p_ctl.getAttribute('ValueHiddenId')).value;
        if (val != "0")
            return val;
        var watermark = $find(p_ctl.getAttribute('WatermarkId'));
        var wmtext = '';
        if (watermark != null)
            wmtext = watermark.get_WatermarkText();
        if (text != wmtext)
            return text;
        return null;
    }
    , GetNumericTextBoxValue: function(p_txt) {
        var value = p_txt.value;
        if (value != null)
            value = value.replace(',', '.');
        if (!isNaN(value) &&
		value > 0)
            return value.replace('.', ',');
        return null;
    }
    , GetDropDownListValue: function(p_ddl, p_iDefaultValue) {
        if (p_iDefaultValue == null) {
            p_iDefaultValue = 0;
        }
        if (p_ddl.selectedIndex >= 0
            && !isNaN(p_ddl.options[p_ddl.selectedIndex].value)
            && p_ddl.options[p_ddl.selectedIndex].value > p_iDefaultValue) {
            return p_ddl.options[p_ddl.selectedIndex].value;
        }
        return null;
    }
    , GetDate: function(p_txt) {
        var iDay = this.GetNumericTextBoxValue(p_txt);
        if (iDay != null &&
		iDay > 0) {
            var ddl = this._GetPairedControl(p_txt);
            var iMonth = this.GetDropDownListValue(ddl);
            if (iMonth != null &&
			iMonth > 0) {
                var date = new Date(new Date().getFullYear(), iMonth - 1, iDay);
                return this._GetDateString(date);
            }
        }
        return null;
    }
    , _GetDateString: function(p_date) {
        var datestr = p_date.format("s");
        return datestr.substring(0, datestr.indexOf('T'));
    }
    , GetValueFromCheckBoxList: function(p_ctl) {
        var iFieldValue = 0;
        var valuesList = '';
        var aControls = document.getElementByPartOfID(p_ctl.id, "input");
        var mode = p_ctl.getAttribute("mode");
        for (var i = 0; i < aControls.length; ++i) {
            if (aControls[i].checked) {
                var iValue = this.GetValueFromItemControl(aControls[i]);
                if (mode == "BitSum") {
                    if (iFieldValue == 0)
                        iFieldValue = iValue;
                    else
                        iFieldValue |= iValue;
                }
                else {
                    if (valuesList != '')
                        valuesList += ',';
                    valuesList += iValue;
                }
            }
        }
        if (iFieldValue > 0)
            return iFieldValue;
        else if (valuesList != '')
            return valuesList;
        return null;
    }
    , GetValueFromItemControl: function(p_ctl) {
        var iValue = parseInt(p_ctl.value);
        if (isNaN(iValue) &&
		p_ctl.parentNode != null)
            iValue = parseInt(p_ctl.parentNode.getAttribute('value'));
        return isNaN(iValue) ? 0 : iValue;
    }
    , OnChangeTextBox: function(p_txt) {
        var ddl = this._GetPairedControl(p_txt);
        if (!isNaN(p_txt.value) && p_txt.value > 0) {
            if (ddl.selectedIndex < 1)
                ddl.selectedIndex = 1;
        }
        else {
            ddl.selectedIndex = 0;
            p_txt.focus();
            p_txt.value = '';
        }
    }
    , OnChangeDropDownList: function(p_ddl) {
        var txt = this._GetPairedControl(p_ddl);
        if (p_ddl.selectedIndex < 1) {
            txt.focus();
            txt.value = '';
        }
        else {
            if (!(!isNaN(txt.value) && txt.value > 0)) {
                txt.focus();
                txt.value = 1;
            }
        }
    }
    , _GetPairedControl: function(p_ctl) {
        var sId = p_ctl.id;
        if (p_ctl.id.indexOf('ddl') != -1)
            sId = sId.replace('ddl', 'txt');
        else if (p_ctl.id.indexOf('txt') != -1)
            sId = sId.replace('txt', 'ddl');
        if (sId != p_ctl.id)
            return $get(sId);
        return null;
    }
    , GetCheckBoxValue: function(p_chk) {
        return p_chk && p_chk.checked ? true : null;
    }
    , GetEllipseRouteValue: function(p_txt) {
        var distance = this.GetNumericTextBoxValue(p_txt);
        if (distance != null && distance > 0) {
            var ddl = this._GetPairedControl(p_txt);
            var type = this.GetDropDownListValue(ddl);
            if (type != null && type > 0) {
                if (type == 1 && distance > 100) {
                    distance = 100;
                }
                return String.format("{0}_{1}", distance, type);
            }
        }
        return null;
    }
}

ATI.Ajax.Scripts.SearchManager.registerClass('ATI.Ajax.Scripts.SearchManager');

var SearchManager = new ATI.Ajax.Scripts.SearchManager();
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();