﻿/*
 *@author ms_tripx htluo 2007.12
 *TravelAddress,TravelDepart,TravelDestination,TravelDestinationS
 *Depart 出发地
 *Destination 目的地
 *DestinationS 目的地小分类名称
 */

 /*
 *旅游渡假产品查询页面里，用户需要选中出发地，目的地，该函数对地址对象（TravelAddress）做初始化操作
 */
 function TravelAddress(varname,departid,destinationid,destinationSid,typename,routeDescID)
 {
    /*
    *页面里的脚本变量名
    */
    this.varname = varname;
    /*
    *出发地下拉框的ID
    */
    this.departid = departid;
    /*
    *目的地下拉框的ID
    */
    this.destinationid = destinationid;
    /*
    *目的地小分类下拉框的ID
    */
    this.destinationSid = destinationSid;
    this.routeDescID = routeDescID;
    /*
    *目的地类型
    */
    this.typename = typename;
    this.depart = document.getElementById(this.departid);
    this.destination = document.getElementById(this.destinationid);
    this.destinationS = document.getElementById(this.destinationSid);    
    this.destinationtype = document.getElementsByName(this.typename)
    this.routeDesc = document.getElementById(this.routeDescID);
    this.productLineType1 = document.getElementById(this.typename+"1");
    this.productLineType2 = document.getElementById(this.typename+"2");
    this.productLineType3 = document.getElementById(this.typename+"3");
    this.productLineType4 = document.getElementById(this.typename+"4");
    /*
    *清空出发地
    */
    this.destinations = new Array();  
    /*
    *下拉框触发事件
    */
    this.depart.onchange = new Function(this.varname+".saveRouteDes()");	
    this.destination.onchange = new Function(this.varname+".bindDestinationSs()");	
    this.destinationS.onchange = new Function(this.varname+".changeDestination()");	
    this.productLineType1.onclick   = new Function(this.varname+".productLineTypeChange()");
    this.productLineType2.onclick   = new Function(this.varname+".productLineTypeChange()");
    this.productLineType3.onclick   = new Function(this.varname+".productLineTypeChange()");
    this.productLineType4.onclick   = new Function(this.varname+".productLineTypeChange()"); 
 };
 
 function getRadioValue(name)
 {
    var temp=document.getElementsByName(name);
    if( temp )
    {
        for (i=0;i<temp.length;i++)
        { 
            if(temp[i].checked)
            {
                return temp[i].value;
            }
        }
    }
    else
    {
        return " ";
    }
 }
 
 function setRadioValue(name,valueStr)
 {
    var temp=document.getElementsByName(name);
    for (i=0;i<temp.length;i++)
    { 
        if(temp[i].value == valueStr)
        {
            temp[i].checked = true;
        }
     }
 }
 
 TravelAddress.prototype.productLineTypeChange = function()
 {
    clearSelectOptions(this.destinationS);
    clearSelectOptions(this.destination);
    
       
    var destinations = this.getDestinations(getRadioValue(this.typename));          
    if( destinations && destinations != "")
    {            
        this.addOptions(this.destination,destinations);     
        this.bindDestinationSs();         
    }
    else
    {
    }        
    
    this.saveRouteDes();
 }
 
 TravelAddress.prototype.saveRouteDes = function()
 {
    var departvalue = getSelectedText(this.depart); 
    var desvalue = getSelectedText(this.destination); 
    var des2value = getSelectedText(this.destinationS); 
    
    this.routeDesc.value = departvalue ;  
    
    if( desvalue != "" )
    {
        this.routeDesc.value += "-----" + desvalue;
        if( des2value != "" )
            this.routeDesc.value += "-----" + des2value;
    }
    
 }
 
 
 
  /*
  *查找某个出发地下的目的地集合（比如查找北京对应的，国内长途目的地集合）
  */
 TravelAddress.prototype.getDestinations = function(type)
 {        
    var destinationsArr = new Array();
    for( var j = 0 ; j < this.destinations.length ; j ++)
    {
        if( this.destinations[j].type == type )
        {
            destinationsArr.push(this.destinations[j]);
        }
    }
    //alert( destinationsArr.length);
    return destinationsArr;    
 };
 
 /*
 *查找某个目的地下的小分类集合（比如查找北京对应的，国内长途目的地集合）
 */
 TravelAddress.prototype.getDestinationSs = function(destinationvalue)
 {
    if(destinationvalue)
    {       
        for(var i = 0;i<this.destinations.length;i++)
        {
            //alert( destinations[i].destinationSs.length);
            if(this.destinations[i].value == destinationvalue)
            {
                return this.destinations[i].destinationSs;
            }
        }
    }
    return null;
 }
 
 /*
 *初始化TravelAddress，参数为一个字符串，所有出发地和目的地数据都在其中，用特殊分隔符进行区别
 */
 TravelAddress.prototype.initial = function(initialstr)
 {       
    /*
    *目的地之间的分隔符
    */
    var splitcc = "$^$";
    /*
    *目的地的名称和ID之间的分隔符
    */
    var splitcd = "#^#";
    /*
    *目的地小分类分隔符
    */
    var splitdd = "~^~";
    /*
    *地址的名称和ID之间的分隔符（这里的地址包括出发地和目的地）
    */
    var splitnv = "@^@";
    
               
    /*
    *Step1：分割目的地
    */
    var ccstrs = initialstr.split(splitcc);
    for(var j = 0;j<ccstrs.length;j++)
    {
        /*
        *Step2：分割目的地和其小分类
        */
        var cdstrs = ccstrs[j].split(splitcd);                    
        if(cdstrs.length>0)
        {
            var cnvstr = cdstrs[0].split(splitnv);                 
            var tempdestination = new TravelDestination(cnvstr[0],cnvstr[1],cnvstr[2]);
            
            if(cdstrs.length>1)
            {
                /*
                *Step3：分割目的地小分类
                */
                var ddstrs = cdstrs[1].split(splitdd);
                for(var m = 0;m<ddstrs.length;m++)
                {
                    var dnvstr = ddstrs[m].split(splitnv);
                    tempdestination.destinationSs.push(new TravelDestinationS(dnvstr[0],dnvstr[1]));                        
                }
            }
            this.destinations.push(tempdestination);
        }                                       
    }
         
 };

 TravelAddress.prototype.bind = function()
 {    
    clearSelectOptions(this.depart);    
    
    this.addOptions(this.depart,this.departs);
    
    this.saveRouteDes();
 }; 
 

 TravelAddress.prototype.bindDestinations = function()
 {     
    clearSelectOptions(this.destinationS);
    clearSelectOptions(this.destination);
    
    var destinations = this.getDestinations(getRadioValue(this.typename));  
    
    if( destinations && destinations != "")
    {       
        this.addOptions(this.destination,destinations);  
        this.bindDestinationSs();            
    }
    
    this.saveRouteDes();
 };
 
 TravelAddress.prototype.bindDestinationSs = function()
 {        
    clearSelectOptions(this.destinationS);     
    var coption = getSelectedValue(this.destination);   
    
    if(coption)
    {
        
        var destinationSs = this.getDestinationSs(coption);
        this.addOptions(this.destinationS,destinationSs);        
    }    
    this.saveRouteDes();
 };
 
 function getSelectedValue(object)
 {
    if( object  )
    {
        
        try
        {            
            if( object.options.length > 0 )
                return object.options[object.selectedIndex].value;
            else
                return null;
        }
        catch(er)
        {
            return null;
        }
    }
 }
 
 function getSelectedText(object)
 {
    if( object  )
    {
        try
        {
            if( object.options.length > 0 )
                return object.options[object.selectedIndex].text;
            else
                return "";
        }
        catch(er)
        {
            return "";
        }
    }
 }
 TravelAddress.prototype.changeDestination = function()
 { 
    this.saveRouteDes();
 };
 TravelAddress.prototype.Language = function(id)
 { 
     this.languageId=id;
 };
 TravelAddress.prototype.addOptions = function(select,objects)
 {
 	if (this.languageId == 0 || this.languageId == 2 )
 	{
 	   select.options[0]=new Option("全部",""); 
 	}
 	else
 	{
 	    select.options[0]=new Option("All",""); 
 	}
 	if(objects && objects.length)
	{
		for(var i = 0;i<objects.length;i++)
    	{
        	addSelectOption(select,objects[i].name,objects[i].value);   
    	}		
	}	
 };
 TravelAddress.prototype.setQueryInfo = function(departhiddenid,destinationhiddenid,destinationShiddenid,destinationtypeid)
 {
    var type = document.getElementById(destinationtypeid).value;
    if( type != "" )
    {        
        setRadioValue("productlineType" , type);
    }    
    var dep = document.getElementById(departhiddenid).value;
    
    if( dep != "" )
    {
        this.depart.value = dep;
    }
    this.bindDestinations();
    var des = document.getElementById(destinationhiddenid).value;
    if( des != "" )
    {
        this.destination.value = des;
    }
    this.bindDestinationSs();
    var des2 = document.getElementById(destinationShiddenid).value;
    
    if( des2 != "" )
    {
        this.destinationS.value = des2;
    }    
 }
 
 /*
 *TravelDepart
 */
 function TravelDepart(name,value)
 {
    this.name = name;
    this.value = value;
    this.destinations = new Array();            
 }; 
 /*
 *TravelDestination
 */
 function TravelDestination(name,value,type)
 {
    this.name = name;
    this.value = value;
    this.type = type;
    this.destinationSs = new Array();
 }; 
 /*
 *TravelDestinationS
 */
 function TravelDestinationS(name,value)
 {
    this.name = name;
    this.value = value;    
 };
 
 /*
 *旅游度假产品查询
 */
 function TravelQueryHelper(departid,destinationid,distinationSid,typename)
 { 	
	this.depart = document.getElementById(departid);
	this.destination = document.getElementById(destinationid);
	this.destinationS = document.getElementById(distinationSid);	
	this.destinationtype = document.getElementsByName(this.typename)
 };
 
 TravelQueryHelper.prototype.check = function()
 { 	
 	return true;
 };
 
 TravelQueryHelper.prototype.setDefault = function()
 { 	
	this.depart.selectedIndex = 0;	
	this.destinationtype.value = 1;
	if(traveladdress)
	{
		traveladdress.bindDestinations();
	}	
 };
 
 TravelQueryHelper.prototype.appendTimeOnblur = function(ctrl)
 {
 	if(ctrl)
	{
		ctrl.onblur = function()
		{
			if(ctrl.value == null || ctrl.value == "")
			{
				ctrl.value = "yyyy-mm-dd"
			}
		};
	} 	
 };
 
 TravelQueryHelper.prototype.appendTimeOnfocus = function(ctrl)
 {
 	if(ctrl)
	{
		ctrl.onfocus = function()
		{
			if(ctrl.value == "yyyy-mm-dd")	
			{
				ctrl.value = "";
			}	
			new WdatePicker(ctrl);
		};
	} 	
 };
 

 /*
 *初始化页面对象 gotoPageID:转到某页的文本框ID
 */
 function TravelOrderHelper(varName,containerid,pagesCountID,curPageID,gotoPageID)
 {
 	this.varName = varName;
 	this.container = document.getElementById(containerid); 
 	this.pagesCount = document.getElementById(pagesCountID);  
 	this.curPageNum = document.getElementById(curPageID); 
 	this.gotoPage = document.getElementById(gotoPageID);  
	this.xmlHttp = null;	
	this.language = null;
	this.maxpage = null;
	this.curpage = null;
	this.ordertype=null;
	this.orderdirect=null;	
 };
 /*
 *给分页按钮添加事件 firstid转到首页按钮ID，beforeid上一页按钮ID，nextid下一页按钮ID，lastid最后一页ID
 *gotoPageBtnID，转到某页的提交按钮
 */
 TravelOrderHelper.prototype.appendEvent=function(orderprice,ordername,ordertime,firstid,beforeid,nextid,lastid,gotoPageBtnID)
 { 	
    this.orderprice=orderprice;
	this.ordername=ordername;
	this.ordertime=ordertime;
    //sort 
    var likPrice=document.getElementById(orderprice);
    if(likPrice!=null)
    {
    likPrice.onclick=new Function(this.varName+".setOrder('sortprice')");
	document.getElementById(ordername).onclick=new Function(this.varName+".setOrder('sortname')");
	document.getElementById(ordertime).onclick=new Function(this.varName+".setOrder('sorttime')");
	}
 	//
 	document.getElementById(firstid).onclick = new Function(this.varName+".moveToFirst()");
	document.getElementById(beforeid).onclick = new Function(this.varName+".before()");
	document.getElementById(nextid).onclick = new Function(this.varName+".next()");
	document.getElementById(lastid).onclick = new Function(this.varName+".moveToLast()");	
	document.getElementById(gotoPageBtnID).onclick = new Function(this.varName + ".goToPage()");
 };
 /*
 *初始化语种，页数，当前页
 */
 TravelOrderHelper.prototype.initial=function(language,maxpage,curpage)
 { 	
 	//this.ordertype=ordertype;
	//this.orderdirect=orderdirect;
 	this.language=language;
	this.maxpage=maxpage; 	
	this.curpage=curpage;	
	this.pagesCount.innerText = maxpage;
	this.curPageNum.innerText = curpage;
	
	this.DisplayFlipButtons();
 };
TravelOrderHelper.prototype.DisplayFlipButtons = function()
{
    if (this.curpage <= 1)
    {
        document.getElementById("firstpage2").src = "/images/btn_firstpage_no.gif";
        document.getElementById("beforepage2").src = "/images/btn_prepage_no.gif";
    }
    
    if (this.curpage >= this.maxpage)
    {
        document.getElementById("nextpage2").src = "/images/btn_nextpage_no.gif";
        document.getElementById("lastpage2").src = "/images/btn_lastpage_no.gif";
    }
}
 //排序====start
  TravelOrderHelper.prototype.setOrder=function(ordertype)
 {
 	if(ordertype==this.ordertype)
	{
		if(this.orderdirect=="asc")
		{
			this.orderdirect="desc";
		}
		else{
			this.orderdirect="asc";
		}
	}
	else{
		this.ordertype=ordertype;
		this.orderdirect="desc";
	}	
	this.curpage=1;
	this.getQueryResult(this.curpage);
	this.markOrder(ordertype);
 };
 TravelOrderHelper.prototype.markOrder=function(ordertype)
 {
 	document.getElementById(this.orderprice).style.fontWeight="normal";
	document.getElementById(this.ordername).style.fontWeight="normal";
	document.getElementById(this.ordertime).style.fontWeight="normal";
	if(ordertype=="sortprice")
	{
		document.getElementById(this.orderprice).style.fontWeight="bold";
	}
	else if(ordertype=="sortname")
	{
		document.getElementById(this.ordername).style.fontWeight="bold";
	}
	else if(ordertype=="sorttime")
	{
		document.getElementById(this.ordertime).style.fontWeight="bold";
	}		
 };

 //=====end
 TravelOrderHelper.prototype.next=function()
 {
 	if(this.curpage<this.maxpage)
	{
		this.curpage++;
		this.getQueryResult(this.curpage);
		this.curPageNum.innerText = this.curpage;
	}
 }; 
 
 TravelOrderHelper.prototype.before=function()
 {
 	if(this.curpage>1)
	{
		this.curpage--;
		this.getQueryResult(this.curpage);
		this.curPageNum.innerText = this.curpage;
	}
 };

 TravelOrderHelper.prototype.moveToFirst=function()
 {
 	if(this.curpage!=1)
	{
		this.curpage=1;
		this.getQueryResult(this.curpage);
		this.curPageNum.innerText = this.curpage;
	} 	
 };
 
 TravelOrderHelper.prototype.moveToLast=function()
 {
 	if(this.curpage!=this.maxpage)
	{
		this.curpage=this.maxpage;
		this.getQueryResult(this.curpage);
		this.curPageNum.innerText = this.curpage;
	} 	
 };
 
 TravelOrderHelper.prototype.goToPage=function()
 {
    var pageNum = parseInt(this.gotoPage.value);
 	if(pageNum <= this.maxpage && pageNum > 0 )
	{
		this.curpage=pageNum;
		this.getQueryResult(this.curpage);
		this.curPageNum.innerText = this.curpage;
	} 	
 };
 
 
 TravelOrderHelper.prototype.getQueryResult=function(pageIndex)
 { 	
 	this.xmlHttp = new TripX.Ajax.XmlHttp();
    this.xmlHttp.Method = "post";
    this.xmlHttp.IsSync = false;	
    this.xmlHttp.Url = "TravelServ.aspx/GetTravelQueryResult";    
	this.xmlHttp.OnSucceed=this.varName+".sucGetQueryResult();"; 	
	this.xmlHttp.OnFailed=this.varName+".faiGetQueryResult();";	
    this.xmlHttp.Content = "orderType="+this.ordertype+"&orderDirect="+this.orderdirect+ "&language="+this.language+"&pageIndex="+pageIndex;
	this.xmlHttp.Request();
 };
 
 TravelOrderHelper.prototype.sucGetQueryResult=function()
 {
 	var responstr=this.xmlHttp.ResponseText;	
	if(responstr=="-1")	
	{
		document.location="travelsearchd.aspx";
	}
	else
	{
	    try
	    {
		  this.container.innerHTML = responstr ;	
		  travelorderhelper=new TravelOrderHelper("travelorderhelper","travelcontainer","pagesCount","curPage","pageNum");
          travelorderhelper.appendEvent("order1","order2","order3","firstpage2","beforepage2","nextpage2","lastpage2","gotoPage");        
		}
		catch(error)
		{
		  
		}
	}		
 };

 TravelOrderHelper.prototype.faiGetQueryResult=function()
 {
 	alert("Failed get ！");
 };
 
 
 
 function ctrlOnFocus(ctrl)
 {
    if(ctrl)
	{
        if(ctrl.value=="yyyy-mm-dd")	
	    {
		    ctrl.value="";
	    }	
	    new WdatePicker(ctrl);
	}
 }
