﻿var aaSmartHost = "http://www.aasmart.com";
var simuHost = "http://www.aasmart.com";

jQuery(document).ready(function(){
	var sval = $("#search_searchType").val();
	$("#searchSelInput").html($("li.search_choose"+sval).find("a").html());
	$("#search_options_SelLink").hide();
	$("input[name='search_common']").each(function(){jQuery(this).hide();});
	$("#search_choose"+sval).show();
	
	$("#options_SelLink").bind("mouseleave", function(){
		$(this).hide();
	});
	$("#mySelInput").bind("click", function(){
		$("#options_SelLink").show();
	});
	
	$("#options_SelLink").find("a").bind("click",function(){
		$("#mySelInput").html($(this).html());
		$("#options_SelLink").hide();
		return false;
	});
	
	$("#search_options_SelLink").bind("mouseleave", function(){
		$(this).hide();
	});
	$("#searchSelInput").bind("click", function(){
		$("#search_options_SelLink").show();
	});
	
	$("#search_options_SelLink").find("li").bind("click",function(){
		$("#searchSelInput").html($(this).find("a").html());
		$("#search_options_SelLink").hide();
		$("input[name='search_common']").each(function(){jQuery(this).hide();});
		$("#search_searchType").val($(this).attr("class").substring(13,14));
	    $("#"+$(this).attr("class")).show();
		return false;
	});
	if (typeof(onloadHook)!="undefined"){
		onloadHook();
	}
	//文件名和文件内名称注意区分大小写
//	Suggestlk2.bind("search_choose0",{url:aaSmartHost+"/jsfiles/allFund.js", defaultval:"基金名称/代码/拼音首字母",valType:2,seglocation:'hock'});
	Suggestlk2.bind("search_choose1",{url:aaSmartHost+"/jsfiles/allFund.js", defaultval:"基金名称/代码/拼音首字母",valType:2,seglocation:'hock'});
	Suggestlk2.bind("search_choose2",{url:aaSmartHost+"/jsfiles/company.js",defaultval:"例：输入“华夏基金”或“hxjj”",valType:1,valType:2,tdhead:{"名称":"50%","拼音":"50%"},unshowType:1,seglocation:'hock'});
	Suggestlk2.bind("search_choose3",{url:aaSmartHost+"/jsfiles/fundmanager.js",defaultval:"例：输入“刘怡伟”或“lyw”",valType:1,valType:2,tdhead:{"名称":"50%","拼音":"50%"},unshowType:1,seglocation:'hock'});
	Suggestlk2.bind("search_choose4",{url:"",defaultval:"请输入关键字",valType:1,valType:2,tdhead:{"名称":"50%","拼音":"50%"},unshowType:1,seglocation:'hock'});
});	


function checkSearch(firstfrm) {
	var stype =  jQuery("#search_searchType").val();
//	var frm = document.getElementById("searchform");
//	var frm = jQuery("#searchform");
	
//	frm.action=aaSmartHost+"/channel/QuickSearch.aspx";
	firstfrm.action=aaSmartHost+"/channel/QuickSearch.aspx";
//	firstfrm.target = "_self";
//	if( stype == "0") {//公募基金
//		var obj = document.getElementById('search_choose0');
//		if(obj.value == obj.suggest.config.defaultval)
//		    return false;
//		var arrayResult = obj.suggest.load(obj.value);
//		if(arrayResult.length==1) {
//			var fcode = /\d{6}/.exec(arrayResult[0]);
//			if(fcode == null || fcode.length == 0) {
//				fcode =  /[\u4e00-\u9fa5]+([\u4e00-\u9fa5]|\w)*/.exec(arrayResult[0]);
//				firstfrm.action = simuHost+"/fundlist.htm?fundKey="+encodeURIComponent(fcode[0]);
//			}else {
//				firstfrm.action = aaSmartHost+"/Fund/FundNetAnalysis.aspx?jCode="+fcode[0];
//			}
//		}
//		jQuery("#search_skey").val(jQuery("#search_choose0").val());
//	}	
	if( stype == "1") {//基金
		var obj = document.getElementById('search_choose1');
		if(obj.value == obj.suggest.config.defaultval)
		    return false;
		var arrayResult = obj.suggest.load(obj.value);
		if(arrayResult.length==1) {
			var fcode = /\d{14}/.exec(arrayResult[0]);
			if(fcode == null || fcode.length == 0) {
				fcode =  /[\u4e00-\u9fa5]+([\u4e00-\u9fa5]|\w)*/.exec(arrayResult[0]);
				firstfrm.action = aaSmartHost+"/channel/QuickSearch.aspx?searchType=1&skey="+encodeURIComponent(fcode[0]);
			}else {
				firstfrm.action = aaSmartHost+"/channel/simu/simushow.aspx?icode="+fcode[0];
			}
		}
		jQuery("#search_skey").val(jQuery("#search_choose1").val());
	}
	if(stype == "2") {//公司
		var obj = document.getElementById('search_choose2');
		if(obj.value == obj.suggest.config.defaultval)
		    return false;
		var arrayResult = obj.suggest.load(obj.value);
		if(arrayResult.length==1) {
			var fcode = /\d{17}/.exec(arrayResult[0]);
			if (fcode != null || fcode.length != 0) {
//				if (fcode[0].indexOf("9") == 0) {
//					var smpy = /[\u4e00-\u9fa5]+[\u4e00-\u9fa5\d]*/.exec(arrayResult[0]);
//					firstfrm.action = aaSmartHost+"/channel/Company/CompanyDetail.aspx?CompanyID="+encodeURIComponent(smpy[0]);
//				}else {
					firstfrm.action = aaSmartHost+"/channel/Company/CompanyDetail.aspx?CompanyID="+fcode[0];
//				}
			}
		}
		jQuery("#search_skey").val(jQuery("#search_choose2").val());
	}
	if(stype == "3") {//经理
		var obj = document.getElementById('search_choose3');
		if(obj.value == obj.suggest.config.defaultval)
		    return false;
		var arrayResult = obj.suggest.load(obj.value);
		if(arrayResult.length==1) {
			var fcode = /\d{17}/.exec(arrayResult[0]);
			if (fcode != null || fcode.length != 0) {
				if (window.smfundmanager && window.smfundmanager.indexOf(fcode[0])>=0){
					firstfrm.action = simuHost+"/manager/"+fcode[0]+".html";
				}else {
					firstfrm.action = aaSmartHost+"/channel/Manager/ManagerDetail.aspx?ManagerID="+fcode[0];
				}
			}
		}
		jQuery("#search_skey").val(jQuery("#search_choose3").val());
	}
	if(stype == "4") {//新闻
		var searchChoose4 = jQuery("#search_choose4");
		if(searchChoose4.val() == "" || searchChoose4.val() == "请输入关键字")
		  return false;
		jQuery("#search_skey").val(searchChoose4.val());
	}
	if(firstfrm.action.indexOf("/search/quicksearch.aspx") == -1) {
		return true;
	}else {
		if(jQuery("#search_skey") != null && jQuery("#search_skey") != "") {
			firstfrm.submit();
		}
	}
	return false;
};


function getCookie(name)
{
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
	if(arr=document.cookie.match(reg)) 
		return unescape(arr[2]);
	else 
		return null;
}

function getDomain(){
	var domain = document.domain;
	var idx = domain.indexOf(".");
	domain = domain.substring(idx+1);
	return domain;
}

function setEnterUrl(){
	var enterUrl = getCookie("enterUrl");
	if (enterUrl==null){
		enterUrl = document.referrer;
		if (enterUrl==null){
			enterUrl = "";
		}
		//enterUrl = encodeURI(enterUrl);
		document.cookie="enterUrl="+enterUrl+";domain="+getDomain();
	}
	//console.log("enterUrl:"+enterUrl);
}

setEnterUrl();  	
