var xmlHttp;
var obj1;
var obj2;
var obj3;
var viewpr
function showCustomer(str,ob1)
{
  obj1=ob1;
  xmlHttp=GetXmlHttpObject();
  if(xmlHttp==null)
  {
   alert("浏览器不支持 HTTP Request");
   return ;
  }
  var url="/tickets/city.asp?t="+str;
  xmlHttp.onreadystatechange=stateChanged;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}
//显示各个省的名称
function show_province(str,ob1)
{
  viewpr=ob1;
  xmlHttp=GetXmlHttpObject();
  if(xmlHttp==null)
  {
   alert("浏览器不支持 HTTP Request");
   return ;
  }
  var url="/tickets/provincecity.asp?t="+str;
  xmlHttp.onreadystatechange=Changed_provincedata;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}
function Changed_provincedata()
{
 switch(xmlHttp.readyState)
 {
	case 0:
	document.getElementById("province").innerHTML="未初始化..."; 
	break;
	case 1:
	document.getElementById("province").innerHTML="数据读取中..."; 
	break;
	case 2:
	document.getElementById("province").innerHTML="数据已读取..."; 
	break;
	case 3:
	document.getElementById("province").innerHTML="数据交互中..."; 
	break;
	case 4:
	document.getElementById("province").innerHTML="全国列车时刻查询"; 
	if (xmlHttp.status == 200) 
    {
		  var html;
		  var optGroup = unescape(xmlHttp.responseText).split(",");
          for (i=1; i<optGroup.length; i++)
          {
             html+="<a href=# onclick=trainstation('"+i+"') title=查关于["+optGroup[i]+"]地区的所有列车站点>"+optGroup[i]+'</a>,' ;
          }
		  html+="|";
		  html=html.replace(",|","");
		  html=html.replace("undefined","");
		  document.getElementById(viewpr).innerHTML="<hr>"+html+"<hr>";
    } 
    else 
    {
       alert("您所请求的页面有异常。");
    }
	break;
  }
}

//显示各个省的车站的名称
function trainstation(id)
{
  xmlHttp=GetXmlHttpObject();
  if(xmlHttp==null)
  {
   alert("浏览器不支持 HTTP Request");
   return ;
  }
  var url="/tickets/trainstation.asp?id="+id;
  xmlHttp.onreadystatechange=Changed_trainstation;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}
function Changed_trainstation()
{
 switch(xmlHttp.readyState)
 {
	case 0:
	document.getElementById("province").innerHTML="未初始化..."; 
	break;
	case 1:
	document.getElementById("province").innerHTML="数据读取中..."; 
	break;
	case 2:
	document.getElementById("province").innerHTML="数据已读取..."; 
	break;
	case 3:
	document.getElementById("province").innerHTML="数据交互中..."; 
	break;
	case 4:
	document.getElementById("province").innerHTML="全国列车时刻查询"; 
	if (xmlHttp.status == 200) 
    {
		  var html = unescape(xmlHttp.responseText);
		  document.getElementById(viewpr).innerHTML="<hr>"+html+"<hr>";
    } 
    else 
    {
       alert("您所请求的页面有异常。");
    }
	break;
  }
}




function showCustomer_province(str,ob1)
{
  obj3=ob1;
  xmlHttp=GetXmlHttpObject();
  if(xmlHttp==null)
  {
   alert("浏览器不支持 HTTP Request");
   return ;
  }
  var url="/tickets/city.asp?t="+str;
  xmlHttp.onreadystatechange=stateChanged_province;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}
//显示车站信息
function showTrainData(str,cc,str1)
{
  xmlHttp=GetXmlHttpObject();
  if(xmlHttp==null)
  {
   alert("浏览器不支持 HTTP Request");
   return ;
  }
  var url="/tickets/traindata.asp?traintype="+cc+"&data1="+escape(str1)+"&data="+escape(str);
  //alert(url);
  xmlHttp.onreadystatechange=traindata;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
  var title;
  if(cc=="cc")
  {
	  title=str+"次列车时刻票价 一览表  订全国3-9折机票 0871-8061111<br>";
  }
  else if(cc=="cz")
  {
	  title=str+"火车站 始发-中转 一览表   订全国3-9折机票 0871-8061111<br>";
  }
  else
  {
	  title=" 从 "+str+" 到 "+str1+" 的所有列车时刻一览表   订全国3-9折机票 0871-8061111<br>";
  }
  document.getElementById("dh").innerHTML=title;
}


function traindata()
{
 switch(xmlHttp.readyState)
 {
	case 0:
	window.statusL="未初始化..."; 
	break;
	case 1:
	window.status="正在从服务器读取列车数据,请稍候......"; 
	break;
	case 2:
	window.status="数据已读取..."; 
	break;
	case 3:
	window.status="数据交互中..."; 
	break;
	case 4:
	window.status="全国列车时刻查询"; 
	if (xmlHttp.status == 200) 
    {
       document.getElementById("traindata").innerHTML=unescape(xmlHttp.responseText);
    } 
    else 
    {
       window.status="您所请求的页面有异常。";
    }
	break;
  }
}
function stateChanged_province()
{
 switch(xmlHttp.readyState)
 {
	case 0:
	window.status="未初始化..."; 
	break;
	case 1:
	window.status="正在从服务器读取列车数据,请稍候......"; 
	break;
	case 2:
	window.status="数据已读取..."; 
	break;
	case 3:
	window.status="数据交互中..."; 
	break;
	case 4:
	dwindow.status="全国列车时刻查询"; 
	if (xmlHttp.status == 200) 
    {
		  var html;
		  var optGroup = unescape(xmlHttp.responseText).split(",");
          for (i=1; i<optGroup.length; i++)
          {
            html+=optGroup[i]+',' ;
          }
		  html+="|";
		  html=html.replace(",|","");
		  html=html.replace("undefined","");
          document.getElementById(obj3).innerHTML="全国各地列车时刻查询<br><hr>"+html+"<hr>";
    } 
    else 
    {
       window.status="您所请求的页面有异常。";
    }
	break;
  }
}

function stateChanged()
{
 switch(xmlHttp.readyState)
 {
	case 0:
	window.status="未初始化..."; 
	break;
	case 1:
	window.status="正在从服务器读取列车数据,请稍候......"; 
	break;
	case 2:
	window.status="数据已读取..."; 
	break;
	case 3:
	window.status="数据交互中..."; 
	break;
	case 4:
	window.status="全国列车时刻查询"; 
	if (xmlHttp.status == 200) 
    {
       addOptionGroup(obj1,unescape(xmlHttp.responseText));
    } 
    else 
    {
       window.status="您所请求的页面有异常。";
    }
	break;
  }
}

function GetXmlHttpObject()
{
 var objXMLHttp=null;
 if(window.XMLHttpRequest)
 {
  objXMLHttp=new XMLHttpRequest();
 }
 else if(window.ActiveXObject)
 {
  objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
 return objXMLHttp;
}

function City(sel,ob1,ob2) 
{
  //document.getElementById(ob2).value="";
  showCustomer(sel,ob1);
}

function inputDistrict(ob2) 
{
	var da=document.getElementById(ob2).options[document.getElementById(ob2).value].text;
	if(GetqueryTypeName()!="zz")
	{
        document.getElementById("input").value=da;
		document.getElementById("nowdata").value="cz";
		
	}
	else//zz
	{
		if(document.getElementById("nowdata").value=="zz")
		{
			document.getElementById("end").value=da;
			document.getElementById("nowdata").value="cz";
		}
		else
		{
			document.getElementById("input").value=da;
			document.getElementById("nowdata").value="zz";
		}
		
	}
}
//回车时运行
function sqlhc(obj)
{
  if(event.keyCode==13)
  {
     if(document.getElementById(obj).value!="")
     {
	   formsubmit(document.all.input.value,document.all.end.value) ;
     }
  }
}

function addOption(objSelectNow,txt,val)
{
   var objOption = document.createElement("OPTION");
   objOption.text= txt;
   objOption.value=val;
   objSelectNow.options.add(objOption);
}

function addOptionGroup(selectId,optGroupString)
{
  var optGroup = optGroupString.split(",");
  var objSelect = document.getElementsByTagName("SELECT");
  var objSelectNow = objSelect[selectId];
  objSelectNow.length = 1;
  for (i=1; i<optGroup.length; i++)
  {
    addOption(objSelectNow, optGroup[i], i);
  }
}
//查询类型设置
function queryType(type)
{
	 switch(type)   
      {   
      case  "cc":  //车次 
      document.all.nametype.innerText ="车次名称：";
	  document.all.zd.style.display="none";
	  document.all.zd2.style.display="none";
	  //showCustomer('cc','p');
	  break;
      case   "cz":  //车站 
      document.all.nametype.innerText ="车站名称：";
	  document.all.zd.style.display="none";
	  document.all.zd2.style.display="";
	  showCustomer('no','p');
	  break;
	  case   "zz":   //站站
      document.all.nametype.innerText ="起点站名称";
	  document.all.zd.style.display="";
	  document.all.zd2.style.display="";
	  showCustomer('no','p');
	  break;
      } 
	  document.getElementById("input").value="";
}

//返回查询类型
function GetqueryTypeName()
{
      var a =document.getElementsByName("querytype");  
	  var selval;
      for(i=0;i<a.length;i++)   
      {   
        if(a[i].checked)   
        selval= a[i].value;   
      }  
	  return selval;
}
function gotrain(data)
{
	
	   if(document.getElementById("dh")!=null)
	   {
		   showTrainData(data,'cz','');
	   }
	   else
	   {
          window.location.href="/tickets/trainlist.asp?traintype=cz&data="+escape(data);
	   }	
}
function formsubmit(data,data1)
{	 

    if(data!="")
	{
      switch(GetqueryTypeName())   
      {   
      case  "cc":  //车次 
      if(document.getElementById("dh")!=null)
	   {
		   showTrainData(data,'cc',data1);
	   }
	   else
	   {
          window.location.href="/tickets/trainlist.asp?traintype=cc&data="+escape(data);
	   }
	  break;
      case   "cz":  //车站 
	   if(document.getElementById("dh")!=null)
	   {
		   showTrainData(data,'cz',data1);
	   }
	   else
	   {
          window.location.href="/tickets/trainlist.asp?traintype=cz&data="+escape(data);
	   }
	   
	  break;
	  case   "zz":   //站站
	   if(document.getElementById("dh")!=null)
	   {
		   showTrainData(data,'zz',data1);
	   }
	   else
	   {
          window.location.href="/tickets/trainlist.asp?traintype=zz&data1="+escape(data1)+"&data="+escape(data);
	   }
	  break;
      } 
	}
	else
	{
		alert("请输入关键字");
	}
	
	
}