var xmlhttp = null;
var page    = null;

function createPageList(count,selectedOption){
	var oSelect = document.getElementById("pageList");
	 oSelect.options.length=0;
	//alert("oS="+oSelect.value());
	for(var i = 0; i<count; i++){
		 oSelect.options[i] = new Option (i+1,i+1);
		//var op = document.createElement("OPTION");
		//op.value = (i + 1);
		//op.text = (i + 1);
		//oSelect.options.add(op);
		// oSelect.appendChild(op); 
		// service.options.length=0;
	}
	if(selectedOption)
		oSelect.options[selectedOption - 1].selected = true;
}

function getSelectID(object){
	if(document.implementation && document.implementation.createDocument){
		page.selectID = object.getAttribute("value");
	}else if(typeof window.ActiveXObject != 'undefined'){
		page.selectID = object.value;
	}
	//page.selectID =  window.event.srcElement.value;
}

function goToNextPage(){
	if(page){
		page.goNext();
		createPageList(page.getPageCount(),page.currPage);
		if(document.getElementById("RAD" + page.selectID) != null)
			document.getElementById("RAD" + page.selectID).checked = true;
	}
}

function goToPrePage(){
	if(page){
		
		page.goBack();
		createPageList(page.getPageCount(),page.currPage);
		if(document.getElementById("RAD" + page.selectID) != null)
			document.getElementById("RAD" + page.selectID).checked = true;
	}
}

function goToPage(evt){
	if(page){	   
		var pageNumEven = evt?evt:(window.event?window.event:null)
	    if(document.implementation && document.implementation.createDocument){
		var pageNum = pageNumEven.target.value;
		}else if(typeof window.ActiveXObject != 'undefined'){
		var pageNum =  pageNumEven.srcElement.value;
		}
		page.goToPage(pageNum);
		createPageList(page.getPageCount(),page.currPage);
		if(document.all("RAD" + page.selectID) != null){
			document.all("RAD" + page.selectID).checked = true;
		}
	}
}
function accessData(url){
	try
	{	
		
		document.getElementById("orderSearchResults").innerHTML="<div id='loading__' style='visibility:hidden;border:5px ridge #CCFFFF; position: absolute; width: 345px; height: 100px; z-index: 9999; left: 280px; top: 200px; padding-left:4px; padding-right:4px; padding-top:1px; padding-bottom:1px; background-color:#EFEFEF' id='layer1'>";
		document.getElementById("orderSearchResults").innerHTML+="<table border='0' width='100%' id='table1' style='border-collapse: collapse' height='100%' cellpadding='0' align='center'>";
		document.getElementById("orderSearchResults").innerHTML+="<tr>";
		document.getElementById("orderSearchResults").innerHTML+="<td width='74'>";
		document.getElementById("orderSearchResults").innerHTML+="<p align='center'>";
		document.getElementById("orderSearchResults").innerHTML+="<img border='0' src='../common/img/loading_circle.gif' width='40' height='40'></td>";
		document.getElementById("orderSearchResults").innerHTML+="<td>";
		document.getElementById("orderSearchResults").innerHTML+="<p align='center'><font color='#004D00' size='2'>";
		document.getElementById("orderSearchResults").innerHTML+="正在进行操作，请稍候...";
		document.getElementById("orderSearchResults").innerHTML+="</font></td>";
		document.getElementById("orderSearchResults").innerHTML+="</tr>";
		document.getElementById("orderSearchResults").innerHTML+="</table>";
		document.getElementById("orderSearchResults").innerHTML+="</div>";	
		$.ajax({
			type: "POST",
			url: url,
			dataType: "xml",
			async: false,
			complete: function(msg){ 			
				var data=msg.responseXML;		
					
				//alert(data.xml);
				if(document.implementation && document.implementation.createDocument){//判断支持Firefox   
					var oXmlSerializer =new XMLSerializer(); 

			    	var xmls = new XMLSerializer();
			    	
					var childNodes = data.getElementsByTagName("report");
					var count = childNodes.length;
					if(count >100)
					{
						alert("您查询到的结果超出100记录,系统只显示前一百条记录\n如果您想查看其他记录，请重新选择条件进行查询！");
					}
					//var url = "../../data/order/transform/searchOrder.xsl";
					var url = "/model/ajax/orderSearch.xsl";
					//var url = "/model/ajax/report.xsl";
					//var xslConfUrl = "../../data/order/language/"+language+"/searchOrder_xsl.xml";
					page = new PageList(orderSearchResults,count,10);
					//page.getparameters(xslConfUrl);
					page.init(data,url);
					page.renderList(page);
					createPageList(page.getPageCount());
					return;
					if(document.getElementsByName("radiobutton").length != 0){
						document.getElementsByName("radiobutton")[0].checked = true;
						page.selectID = document.getElementsByName("radiobutton")[0].value;
					}
					
					if(data.getElementsByTagName("ROW").length==0){
						document.getElementById("servicebutton").style.display="none";
						document.getElementById("operation").style.display="none";
					}else{
						document.getElementById("servicebutton").style.display="";
						document.getElementById("operation").style.display="";
					}	
						
									    	
				}else if(typeof window.ActiveXObject != 'undefined'){//判断支持IE
					
					var childNodes = data.selectNodes("//report");
					var count = childNodes.length;
					if(count >100)
					{
						alert("您查询到的结果超出100记录,系统只显示前一百条记录\n如果您想查看其他记录，请重新选择条件进行查询！");
					}
					var url = "/model/ajax/orderSearch.xsl";
					var xslConfUrl = "/model/ajax/orderSearch.xml";
					page = new PageList(orderSearchResults,count,10);
					page.init(data,url);
					page.renderList(page);
					createPageList(page.getPageCount());
					return;
					if(typeof(document.all.radiobutton) != "undefined"){
						document.getElementsByName("radiobutton")[0].checked = true;
						page.selectID = document.getElementsByName("radiobutton")[0].value;
					}
									
					if(data.getElementsByTagName("ROW").length==0){
						document.getElementById("servicebutton").style.display="none";
						document.getElementById("operation").style.display="none";
					}else{
						document.getElementById("servicebutton").style.display="";
						document.getElementById("operation").style.display="";
					}
				}
			}  
		});	
	}
	catch(exp)
	{
		throw( exp );
	}
}

function checkTime(){
	var re = /-/g;
	/*var depTime = document.getElementById("depTime").value;	
	var arrTime = document.getElementById("arrTime").value;
	if(depTime != "" && arrTime != ""){
		depTime = new Date(depTime.replace(re, "/"));
		arrTime = new Date(arrTime.replace(re, "/"));
		if(depTime > arrTime){
			alert("班次起始日期不能晚于班次截止日期");
			document.getElementById("depTime").focus();
			document.getElementById("depTime").select();
			return false;
		}
	}*/
	var bookTimeFrom = document.getElementById("bookTimeFrom").value;	
	var bookTimeTo = document.getElementById("bookTimeTo").value;
	if(bookTimeFrom != "" && bookTimeTo != ""){
		bookTimeFrom = new Date(bookTimeFrom.replace(re, "/"));
		bookTimeTo = new Date(bookTimeTo.replace(re, "/"));
		if(bookTimeFrom > bookTimeTo){
			alert("订单起始日期不能晚于订单截止日期");
			document.getElementById("bookTimeFrom").focus();
			document.getElementById("bookTimeFrom").select();
			return false;
		}
	}
	return true;
}

function doQuery(){
	/*if(!checkTime()){
		return;
	}
	*/
	if(document.getElementById("username").value==''){
		alert('请输入姓名');
		return;
	}
	try{
		
		var oderno=document.getElementById("orderno").value.toUpperCase();
		oderno=oderno.toString().replace(/\s/g,"")
		document.getElementById("orderno").value=oderno;
		var condition  = "";
	//if(document.implementation && document.implementation.createDocument){
		//if(document.getElementById("depTime").value !="")
		//	condition += "depTime="+document.getElementById("depTime").value+"&";
		//if(document.getElementById("arrTime").value !="")
		//	condition += "arrTime="+document.getElementById("arrTime").value+"&";
		if(document.getElementById("bookTimeFrom").value !="")
			condition += "bookTimeFrom="+document.getElementById("bookTimeFrom").value+"&";
		//if(document.getElementById("bookTimeTo").value !="")
		//	condition += "bookTimeTo="+document.getElementById("bookTimeTo").value+"&";
		//if(document.getElementById("status").value !="")
		//	condition += "status="+document.getElementById("status").value+"&";
		if(document.getElementById("orderno").value!="")
			condition += "orderno="+document.getElementById("orderno").value+"&";
		if(document.getElementById("mobile").value !="")
			condition += "mobile="+document.getElementById("mobile").value+"&";
		if(document.getElementById("username").value !=""){
			   
		  	var _str=encodeURI(document.getElementById("username").value);   
			condition += "username="+_str+"&";
		}if(condition != ""){
			condition=condition.substring(0,condition.length-1);
		}
		
	//}else if(typeof window.ActiveXObject != 'undefined'){
		
		//*sui
		/***condition = getKeyValues(document.getElementById("conditions"));
		**/
			
	//}	
		//condition += (condition == "")?"domesticindicate="+radiovalue:"&domesticindicate="+radiovalue;
		//var url = (condition == "") ? "../../data/order/searchOrder.xsql" :"http://ec.csair.com/B2C/data/order/transform/searchOrder.xsl?"+condition;
	}catch(exp){
		handleExcepion("获取查询条件出错","获取查询条件出错，详细信息如下：\n"+exp.message,false);
		return false;
	}
	try{
		url="/model/ajax/orderSearch.faces?"+condition;
		//url="/model/ajax/report.xml";
		accessData(url);
	}catch(exp){
		//handleExcepion("查询数据时出错","查询数据时出错,详细信息如下：\n"+exp.message,false);
		throw( exp );
	}
}
function doQueryByName(){
	if(chkname()==false||chkotno()==false){
		return;
	}
	try{
		var psgname=document.getElementById("queryname").value;
		var oderno=document.getElementById("otno").value.toUpperCase();
			oderno=oderno.toString().replace(/\s/g,"");
			document.getElementById("otno").value=oderno;
		var condition  = "";
		if(document.getElementById("ord_pst").value =="ticketno")
			condition +="psgname="+psgname+"&ticketno="+document.getElementById("otno").value+"&action=ticketno";
		if(document.getElementById("ord_pst").value =="orderno"){
			condition +="psgname="+psgname+"&orderno="+document.getElementById("otno").value+"&action=orderno";
			}
		//condition += (condition == "")?"domesticindicate="+radiovalue:"&domesticindicate="+radiovalue;
		var url = "../../data/order/nonmemberSearchOrder.xsql?"+condition;
	}catch(exp){
		handleExcepion("获取查询条件出错","获取查询条件出错，详细信息如下：\n"+exp.message,false);
		return false;
	}
	try{
		accessData(url);
	}catch(exp){
		handleExcepion("查询数据时出错","查询数据时出错,详细信息如下：\n"+exp.message,false);
	}
}
function clearInputValues()
{
/*
	if(objectid != null && typeof(objectid) != "undefined")
		objectid.reset();
	else
		return;
*/
		//document.getElementById("depTime").value = "";
		//document.getElementById("arrTime").value = "";
		document.getElementById("mobile").value = "";
		document.getElementById("bookTimeFrom").value = "";
		//document.getElementById("bookTimeTo").value = "";
		//document.getElementById("status").value = "";
		document.getElementById("orderno").value = "";
}
function cls()
{
		document.getElementById("queryname").value = "";
		document.getElementById("otno").value = "";
		
}
function modifyContactInfo(){
	if(isStatusInORE())
		window.location.href = "../customservice/modifyTransactor.jsp?orderno=" + page.selectID;
		//window.location.href = "orderManagementFrame.jsp?orderno=" + page.selectID+"&flag=U";
	else{
		alert("对不起！ 订单未出票，不能修改联系信息。");
		return;
	}
}

function printJourneyBill(){
	if(isStatusInORE()){
		var radiovalue="";
		for(i=0;i<document.getElementById.orderType.length;i++){
			if(document.getElementById.orderType[i].checked)
				radiovalue=document.getElementById.orderType[i].value;
		}
		if (radiovalue=="0"){
			window.open("../customservice/printJourneyBill4international.jsp?orderno=" + page.selectID);
		}else if(radiovalue=="1"){
			window.open("../customservice/printJourneyBill.jsp?orderno=" + page.selectID);
		}//*/
		//window.location.href = "../customservice/printJourneyBill.jsp?orderno=" + page.selectID;
		//window.open("../customservice/printJourneyBill.jsp?orderno=" + page.selectID);
		}
	else{
		alert("对不起！ 订单未出票，不能打印行程单。");
		return;
	}	
}

function printBarJourneyBill(){
	if(isStatusInORE()){
		var retDom = null;
		var domesticindicate;
		try{
			
			var urlXml = "../../data/order/retrievePayOrder.xsql?orderno="+page.selectID;
			//if(document.getElementById("loading__"))loadingShow();
			var xhttp = new bingo.XMLHttpRequest("GET",urlXml,false);
			retDom = xhttp.send(); 
			//alert(retDom.xml);
			//if(document.getElementById("loading__")) loadingHide();
		}catch(exp){
			if(document.getElementById("loading__")) loadingHide();
			processException("对不起！ 获取订单数据时出错。","获取订单数据时出错,详细信息如下:"+exp.message);
		}
		domesticindicate=retDom.selectSingleNode("page/domesticindicate").text;
		var psgInfos = retDom.selectNodes("//PSGINFO/ROW/PSGNAME");
		var target = "";
		for(var j=0;j<psgInfos.length;j++)
		{
			target += (target == "") ? psgInfos[j].text : ","+psgInfos[j].text;
		}
		target=encodeURI(target);
		if(domesticindicate=="1")
			window.open("../customservice/printBarJourneyOrder.jsp?orderno=" + retDom.selectSingleNode("page/orderno").text + "&psgname=" + target);
		else if(domesticindicate=="0")
			window.open("../customservice/printJourneyBill4international.jsp?orderno=" + retDom.selectSingleNode("page/orderno").text + "&psgname=" + target);
		//window.location.href = "../customservice/printBarJourneyOrder.jsp?orderno=" + page.selectID;
	}
	else{
		alert("对不起！ 订单未出票，不能打印条形码行程单。");
		return;
	}	
}

function alterFlight(){
    if(isStatusInORE()){
	   //window.location.href = "../customservice/modifyFlight.jsp?orderno=" + page.selectID;
/*
		if(isRefund()==true){
			alert("很抱歉，该订单已经提交了退票申请，不允许再提交航班变更申请，谢谢。");
			return;
		}else
	    	window.location.href = "../customservice/modifyFlight_SegmentList.jsp?orderno=" + page.selectID+"&flag=F";
//*/
	isRefund();
	}
	else{
	    alert("对不起！订单未出票，不能变更航班。");
		return;
	}
}

function isRefund(){

	/*var xmlhttpp =new ActiveXObject( "Microsoft.XMLHTTP" );
	var url	="/B2C/data/order/getRefundFlag.xsql?orderno="+page.selectID;						
	xmlhttpp.open( "post", url ,false);
	xmlhttpp.setRequestHeader("Content-Type","text/xml");
	xmlhttpp.send();
	var xmlDomm=xmlhttpp.ResponseXML;
	prompt("",xmlDomm.xml);
	if(xmlDomm.selectNodes("page/REFUNDNO/ROW").length>0)
		return true;
	else
		return false;*/

	var xsqlurl ="/B2C/data/order/getRefundFlag.xsql?orderno="+page.selectID;
	
	var successflag;
	var xmlDom;
	try{
		$.ajax({
			type: "POST",
			url: "/B2C/data/order/getRefundFlag.xsql?orderno="+page.selectID,
		    dataType: "xml",
			async: false,
			success: function(msg){ 
				if(document.implementation && document.implementation.createDocument){
					//var dataStr=msg.responseXML;
					xmlDom=msg;	
					if(xmlDom.getElementsByTagName("message").length != 0||xmlDom.getElementsByTagName("MESSAGE").length != 0){
						alert(xmlDom.getElementsByTagName("message")[0].childNodes[0].nodeValue);
						return;				
					}//*/
					
				}else if(typeof window.ActiveXObject != 'undefined'){//判断支持IE
					xmlDom= msg;
					if(xmlDom.getElementsByTagName("message").length != 0||xmlDom.getElementsByTagName("MESSAGE").length != 0){
						alert(xmlDom.getElementsByTagName("message")[0].childNodes[0].nodeValue);
						return;
					}		
				}
				
				if(xmlDom.getElementsByTagName("ROW").length>0)
					successflag= true;
				else
					successflag= false;
				
				if(successflag==true){
					alert("很抱歉，该订单已经提交了退票申请，不允许再提交航班变更申请，谢谢。");
					return;
				}else
			    	window.location.href = "../customservice/modifyFlight_SegmentList.jsp?orderno=" + page.selectID+"&flag=F";
		
			}
			
		});
	}catch(e){		
		alert("获取联系人信息出错。");
	}
}
function changeName(){
    if(isStatusInORE())
	    //window.location.href = "../customservice/modifyPassenger.jsp?orderno=" + page.selectID;
		window.location.href = "orderManagementFrame.jsp?orderno=" + page.selectID+"&flag=P";
	else{
	  alert("对不起！ 订单未出票，不能变更旅客姓名。");
		return;
	}	       
}


function cancelTicket(){
	if(isStatusInORE())
	    window.location.href = "../customservice/applyRefund.jsp?orderno=" + page.selectID;
	else{
	  alert("对不起！ 订单未出票，不能申请退票。");
		return;
	}	 
}

function remainSeat(){
	if(isInter()){
		if(isStatusInORE())			
		    window.location.href = "../customservice/remainSeat.jsp?orderno=" + page.selectID;
		else{
		  alert("对不起！ 订单未出票，不能预定座位。");
		  return;
		}
	}else{
		alert("对不起！国际客票暂不支持预定座位！");
		return;
	}	 
}

function isInter(){
	var isInter ;
	if(document.implementation && document.implementation.createDocument){
		isInter = document.getElementById("RAD" + page.selectID).getAttribute("isInter");
	}else if(typeof window.ActiveXObject != 'undefined'){
		isInter = document.getElementById("RAD" + page.selectID).isInter;
	}
	return (isInter == '1');
}

function isStatusInORE(){
	var status ;
	if(document.implementation && document.implementation.createDocument){
		status = document.getElementById("RAD" + page.selectID).getAttribute("state");
	}else if(typeof window.ActiveXObject != 'undefined'){
		status = document.getElementById("RAD" + page.selectID).state;
	}
	return (status == 'O' || status == 'R' || status == 'E');
}

function checkOrderno(thisobj){
	/*var regOrderNo=/(^[B,b,C,c]{1}[0-9]{10,11}$)|(^[B,b,C,c]{1}[0-9]{10,11}[/]{1}[A-Za-z0-9]{5}$)/;
	if(thisobj.value!=""){
		thisobj.value=thisobj.value.replace(/\s/g,"");
		if(regOrderNo.test(thisobj.value)==false){
			alert("订单编号为11位或12位字符，第一位是字母B或C，后跟10位或11位数字。");
			thisobj.focus();
			return false;
		}
	}*/
}
function chkname(){
	if(document.getElementById("queryname").value==""){
		//document.getElementById("xmts").style.display="";
		alert("请输入旅客姓名");
		return false;
	}else{
		document.getElementById("xmts").style.display="none";
		return true;
	}
}

function chkotno(){
	var regOrderNo=/(^[B,b,C,c]{1}[0-9]{10,11}$)|(^[B,b,C,c]{1}[0-9]{10,11}[/]{1}[A-Za-z0-9]{5}$)/;
	if(document.getElementById("otno").value==""){
		//document.getElementById("ts2").value="*请输入订单号或电子客票号";
		//document.getElementById("qtts").style.display="";
		alert("请输入订单号或电子客票号");
		return false;
	}else{
		if(regOrderNo.test(document.getElementById("otno").value)==false){
			//document.getElementById("ts2").value="*订单编号为11位或12位字符，第一位是字母B或C，后跟10位或11位数字。";
			//document.getElementById("qtts").style.display="";
			alert("单编号为11位或12位字符，第一位是字母B或C，后跟10位或11位数字。");
			return false;
		}else{
			document.getElementById("qtts").style.display="none"
			return true;
		}
	}
}



