/**
 * @author 王正平
 */
function Code(CodeName){
	$jq("<div id=\"div_"+CodeName+"\" class='mydiv'></div>").appendTo("body");
	$jq("#div_"+CodeName).width($jq("#input_"+CodeName).width());
	//$jq("#div_"+CodeName).height(200);
	$jq("#div_"+CodeName).css({"top":parseInt($jq("#input_"+CodeName).offset().top) + parseInt($jq("#input_"+CodeName).height()+5),"left":$jq("#input_"+CodeName).offset().left});
	$jq("#div_"+CodeName).hide();
	var isEnter=false;
	$jq("#input_"+CodeName).keypress(
		function(e){
			if (e.ctrlKey == true || e.shiftKey == true) {
				return false;
			}
			//数字
			if ((e.which >= 48 && e.which <= 57 && e.ctrlKey == false && e.shiftKey == false) || e.which == 0 || e.which == 8 ) {
				return true;
			}
			//字母
			else if((e.which >= 97 && e.which <=122 && e.ctrlKey == false && e.shiftKey == false) || e.which == 0 || e.which == 8 ){
				return true;
			}
			else{
				return false;
			}
		}
	);
	$jq("#input_"+CodeName).keyup(function(e){
		//数字
		if ((e.which >= 48 && e.which <= 57 && e.ctrlKey == false && e.shiftKey == false) || e.which == 0) {
			GetCode(CodeName);
		}
		//字母
		else if((e.which >= 65 && e.which <=77 && e.ctrlKey == false && e.shiftKey == false) || e.which == 0 ){
			GetCode(CodeName);
		}
		//小键盘
		else if((e.which >= 96 && e.which <=105 && e.ctrlKey == false && e.shiftKey == false) || e.which == 0 ){
			GetCode(CodeName);
		}
		else if(e.which == 8)
		{
			GetCode(CodeName);
		}
		else if(e.which == 13)
		{
			GoOpen(CodeName);
		}
		else if(e.which == 38)
		{
			//上滚
			var index = $jq("#mytable_"+CodeName+" tr").size();
			if(index>0){
				index--;
			}
			var count=index;
			for (var i = index; i > 0; i--) {
				if($jq("#mytable_"+CodeName+" tr:eq("+i+")").attr("class") == "mySelecttr")
				{
					count=i;
					count--;
					break;
				}
			}

			for(var i=index;i>0;i--){
				if(i == count && count > 0){
					$jq("#mytable_"+CodeName+" tr:eq("+count+")").attr("class","mySelecttr");
					$jq("#input_"+CodeName).val($jq("#mytable_"+CodeName+" tr:eq("+count+")").attr("id"));
					//break;
				}
				else{
					$jq("#mytable_"+CodeName+" tr:eq("+i+")").attr("class","mytr");
				}
			}
		}
		else if(e.which == 40)
		{
			//下
			var index = $jq("#mytable_"+CodeName+" tr").size();

			var count=1;
			for (var i = 1; i < index; i++) {
				if($jq("#mytable_"+CodeName+" tr:eq("+i+")").attr("class") == "mySelecttr")
				{
					count=i;
					count++;
					break;
				}
			}

			for(var i=1;i<index;i++){
				if(i == count){
					$jq("#mytable_"+CodeName+" tr:eq("+count+")").attr("class","mySelecttr");
					$jq("#input_"+CodeName).val($jq("#mytable_"+CodeName+" tr:eq("+count+")").attr("id"));
					//break;
				}
				else{
					$jq("#mytable_"+CodeName+" tr:eq("+i+")").attr("class","mytr");
				}
			}
		}
	})

	$jq(document).click(function(){
		$jq("#div_"+CodeName).hide();
	});
}

function GoOpen(CodeName){
	var codeValue = $jq("#input_"+CodeName).val();
	if(codeValue.length == 6){
		$jq("#div_"+CodeName).hide();
		window.open("/F10/"+codeValue+".html");
	}
	else if(codeValue.length >0)
	{
		$jq("#input_"+CodeName).val('');
		alert("股票代码格式不正确!");
	}
}

function GetCode(CodeName){
	var value = $jq("#input_"+CodeName).val();
	$jq.getJSON("http://www.shenguang.com/manage/SGAip/SGAip.Web/Suggest.aspx?search="+escape(value)+"&callback=?",function(data){
			var html = "<table id=\"mytable_"+CodeName+"\" width=\"100%\" cellpadding=\"1\" cellspacing=\"0\">";
			html += "<tr class=\"topRow\">";
			html += "<td class=\"mytd\">股票代码</td>";
			html += "<td class=\"mytd\">股票名称</td>";
			html += "<td class=\"mytd\">股票拼音</td>";
			html += "</tr>";
			var isDiv=false;
			$jq.each(data,function(n,v){
				if (n == "code") {
					for(var i=0;i<v.length;i++){
						html += "<tr class=\"mytr\" id=\""+v[i].codeId+"\">";
						html += "<td class=\"mytd\">"+v[i].codeId+"</td>";
						html += "<td class=\"mytd\">"+v[i].name+"</td>";
						html += "<td class=\"mytd\">"+v[i].abc+"</td>";
						html += "</tr>";
					}
					isDiv=true;
				}
			});
			html+="</table>";
			//alert(html);
			if (isDiv) {
				$jq("#div_"+CodeName).show();
				$jq("#div_"+CodeName).html(html);
				$jq("#div_"+CodeName+" tr").hover(function(){
					if (!$jq(this).hasClass("topRow")) {
						$jq("#mytable_"+CodeName+" tr[class = mySelecttr]").attr("class", "mytr");
						$jq(this).attr("class", "mySelecttr");

						$jq("#input_"+CodeName).val($jq(this).attr("id"));

					//$jq("#input_"+CodeName).val($jq(this).("td:eq[0]").text());
					}
				}, function(){
					if (!$jq(this).hasClass("topRow")) {
						$jq(this).attr("class", "mytr");
					}
				});
			}
			else{
				$jq("#div_"+CodeName).hide();
			}
	});
}