/**
 * <script type="text/javascript" src="/search/script/common.js">
 *      searchbar.joinGroup='/images/basic/necmcu/btn_add.gif';
 *      searchbar.width=120;
 *      searchbar.init(); 
 * </script>
 * @param {Object} $sid
 */
var searchbar = {
    values: new Array(),
    joinGroup:'-',
	width:200,
	
    g: function($sid){
        return document.getElementById($sid);
    },
    
    init: function(){
        this.values["dictionary"] = new Array('权威、完整的电子百科词典，阅读专业文章必查', 'http://www.eefocus.com/dict/', '_blank');
        this.values["site"] = new Array('电子业界资讯搜索', 'http://www.eefocus.com/search/', '_blank');
        this.values["Datasheet"] = new Array('元器件数据手册一网搜尽', 'http://www.eefocus.com/datasheet/', '_blank');
        
        var tpl = this.tpl();
        document.write(tpl);
    },
    
    check: function(){
        var k = this.g("searchselect").value;
        if (this.values[k][0] == this.g('keyword').value || this.g('keyword').value == '') {
            alert("请填写搜索关键字！");
            return false;
        }
        return true;
    },
    
    start: function(){
        if (this.g('keyword').value == this.values[this.g("searchselect").value][0]) {
            this.g('keyword').value = '';
            this.g('keyword').style.color = 'black';
            return;
        }
        if (this.g('keyword').value == '') {
            this.g('keyword').value = this.values[this.g("searchselect").value][0];
            this.g('keyword').style.color = 'gray';
            return;
        }
    },
    changform: function(obj){
        this.g("SearchForm").action = this.values[obj][1];
        this.g("SearchForm").target = this.values[obj][2];
        
        if (this.eiki_check()) {
            this.g("keyword").value = this.values[obj][0];
        }
    },
    
    eiki_check: function(){
        if (this.g("keyword").value == '') {
            return true;
        }
        var s = new Array("dictionary", "site", "Datasheet");
        for (i = 0; i < s.length; i++) {
            if (this.g("keyword").value == this.values[s[i]][0]) {
                return true;
            }
        }
        return false;
    },
    
    tpl: function(){
        var tpl = '';
        tpl += '<form id="SearchForm" name="SearchForm" method="get" action="http://www.eefocus.com/datasheet/" target="_blank" onsubmit="return searchbar.check();">';
        tpl += '  <img src="/images/ico_search.gif" align="absmiddle"/> ';
        tpl += '  <input name="keyword" id="keyword" type="text" value="元器件数据手册一网搜尽" onfocus="searchbar.start();;" onblur="if(this.value==\'\')searchbar.start();" style="color:gray;width:'+this.width+'px; margin:0 5px 0 0"/>';
        tpl += '  <select name="searchselect" id="searchselect" onchange="searchbar.changform(this.options[this.selectedIndex].value);">';
        tpl += '      <option value="dictionary" selected="selected">术语辞典</option>';
        tpl += '      <option value="site">主题文章</option>';
        tpl += '      <option value="Datasheet" selected="selected">数据手册</option>';
        tpl += '  </select>';
        tpl += '  <input type="submit" name="Submit" value=" 搜 索 " class="search_btn_total"/>';
		if(this.joinGroup!='-') tpl += '  <a href="#7" onclick="isgroupuser(\'isg()\');"  title="加入社区"><img src="'+this.joinGroup+'" align="absmiddle" border="0"/></a>';
        tpl += '</form>';
        
        return tpl;
    }
}
