jQuery(
function($){
	var threePostInfo=function(data)
	{
		try
		{
			$.Buffer.push(data);
			$("#codeInfo").html($.getBuffer());
			$("#wait").hide();
			/* table css */
			if(_cookies != 'zjs')
			{
				var tables = document.getElementsByTagName('table');
				for(var i=0; i<tables.length; i++){
					var table = tables[i];
					if(table.className == 'datagrid4'){
						stripe(tables[i]);
					}
				}
			}
		}catch(e){}
	};
	$.Postjson=function(cid)
	{
		try
		{
			if(!cid)
			{
				alert('你选择快递时参数出错！请重新选择。');
				return false;
			}
			if(cid == 'ems'||cid == 'sto'||cid=='sf')
			{
				$.Buffer.push('正在生成快递验证码，请稍后...');
			}
			else
			{
				$.Buffer.push('正在获取快递信息，请稍后...');
			}
			$.Buffer.push("<br /><img id=\"codesn\" src=\"images/loadinganimation.gif\" align=\"bottom\" border=\"0\" />");
			$("#codeInfo").html($.getBuffer());
			$("#wait").show();
			$.getScript("./type.php?type="+cid,function(data){threePostInfo(data)},true);
		}
		catch(e){}
	};
	$.Postsearch=function(cid,url,kw)
	{
		try
		{
			if(!cid)
			{
				alert('你选择快递时参数出错！请重新选择。');
				return false;
			}
			$.Buffer.push('正在查找单号为【'+kw+'】的信息，请稍后...');
			$.Buffer.push("<br /><img id=\"codesn\" src=\"images/loadinganimation.gif\" align=\"bottom\" border=\"0\" />");
			$("#codeInfo").html($.getBuffer());
			$("#wait").show();
			$.getScript("./search.php"+url,function(data){threePostInfo(data)},true);
		}
		catch(e){}
	};
	$.listTitle=function(_title,_html)
	{
		var _cookiesName=$.cookie("chinaPostName",_html,{expires:7});
		var _same = 'f';
		$(".navlink span").each(function(i)
		{
			if(this.title == _title)
			{
				_same = 't';
				this.className = 'selected';
				$("#type").val(_title);
			}
			else
			{
				this.className = '';
			}
		});
		if(_same == 'f') select_other(_title, _html);
	};
	$.selPost=function(v)
	{
		var _cookies=$.cookie("chinaPost",v,{expires:7});
		$.Postjson(v);
		tb_remove();
	};
	$.search=function()
	{
		var _kw = $("#word").val();
		var _subtype = $("#type").val();
		if(_kw!="" && _subtype!="")
		{
			if(_subtype == 'ems' || _subtype == 'sto' || _subtype == 'sf')
			{
				var _code = $("#imgcode").val();
				if(_code=='')
				{
					alert("请输入验证码！");
					$("#imgcode").focus();
					return false;
				}
				if ($("#imgcode").length == 0)
				{
					$.Postjson(_subtype);
					return false;
				}
				var url = "?type="+_subtype+"&kw="+_kw+"&myEmsbarCode="+$("#myEmsbarCode").val()+"&namecode="+$("#namecode").val()+"&imgcode="+encodeURIComponent(_code);
				$.Postsearch(_subtype,url,_kw);
			}
			else
			{
				var url = "?type="+_subtype+"&kw="+_kw;
				$.Postsearch(_subtype,url,_kw);
			}
			
			var path = window.location.href;
			var arr2 = path.split("/");
			if(arr2[3]!='index2.html')
			{
			    addToHistoryBox(_kw,_subtype,$.cookie("chinaPostName"));
			    for_dd();
			}
			$('#' + _subtype + _kw).addClass("selected");
			$.cookie("chinaKw",_kw,{expires:7});
			$.cookie("chinaPost",_subtype,{expires:7});
		}
		else
		{
			if(_kw=='')
			{
				alert("请输入快递单号！");
				$("#word").focus();
				return false;
			}
			else
			{
				alert("提交参数出错！请联系管理员。");
				return false;
			}
		}
	};
	$("#sb").click(function(){$.search()});
	$.oncode_reload = function(type)
	{
		$.Postjson(type);
	};
	$.runkey = function(){
			document.onkeydown = function(e){
				e = e || window.event;
				keynum = e.which || e.keyCode;
				if (keynum == 13) {
					if(getFileName() != 'index20.html')
					{
					    $.search();
					}
					return false;
				}
			}
	};
	var _cookies=$.cookie("chinaPost");
	if(_cookies==null)
	{
		$.Postjson('yd');
	}
	else
	{
		$.Postjson(_cookies);
	}
});
function add_event(tr){
	tr.onmouseover = function(){
		tr.className += ' hover';
	};
	tr.onmouseout = function(){
		tr.className = tr.className.replace(' hover', '');
	};
}
function stripe(table) {
	var trs = table.getElementsByTagName("tr");
	for(var i=1; i<trs.length; i++){
		var tr = trs[i];
		tr.className = i%2 != 0? 'odd' : 'even';
		add_event(tr);
	}
}

function getFileName(){
	var url = this.location.href
	var pos = url.lastIndexOf("/");
	if(pos == -1){
	   pos = url.lastIndexOf("\\")
	}
	var filename = url.substr(pos +1)
	return filename;
}
