﻿/************************************************/
/* Select Option AJAX  v1.1.0                   */
/* 2007 Patrick Tang patrick.tangp(at)gmail.com */
/* www.startravel.com.tw                        */
/* Software Copyright Reserved                  */
/* 												*/
/* 20070917 V1.0.0 First Version				*/
/* 												*/
/* 20070930 V1.1.0 QueryString for Parameters   */
/*				   and Keys make by array 		*/
/* 												*/
/************************************************/

function AjaxSelectObj () {
    
    this.TargetSelectObj = null ;    
    this.SrcUrlPath = null ;
    this.UrlPath = null ;
    this.XmlHttp = null ;
    this.RndStr = "" ;
    this.TargetSelectObjDefault = "" ;
//    this.DefaultParentKey = "" ;
//    this.KeyValue = "" ;
    this.AsyncFlag = false ;
    this.ParamAry_Key = [] ;
    this.ParamAry_Value = [] ;
    this.URLParam_QueryStart = "?" ;
    this.URLParam_QueryConn = "&" ;

    
    this.CreateXmlHttpRequest = function () {

        try {
		    this.XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
//            alert("H1");
		    } catch (e01) {
			try {
				this.XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
//				alert("H2");
			    } catch (e02) {
				if(window.XMLHttpRequest)  {
                    this.XmlHttp = new XMLHttpRequest();
                    this.XmlHttp.overrideMimeType('text/xml'); //****************
//                    try {
//                        netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
//                        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
//                        } catch (err) {
//                            alert("Error initializing XMLHttpRequest.\n"+ err); 
//                            }
//                    alert("H3");
                    }
                else {
                    this.XmlHttp = null;
//                    alert("H4");
                    }
			    }
            }
        } ;

    this.GetXmlHttpData = function () { 
        // this.UrlPath = this.SrcUrlPath + this.KeyValue ;
        this.MakeURLString() ;
        this.RandUrl() ;        
        this.XmlHttp.open("GET", this.UrlPath , this.AsyncFlag );
        var selfObj = this;        
        this.XmlHttp.onreadystatechange = function(  ) {
            if ( selfObj.XmlHttp.readyState == 4 ) {
                if ( selfObj.XmlHttp.status == 200) {
                    selfObj.ClearSelectObj() ;
                    selfObj.UpdateSelectObj();
                    }
                }            
					
			};
        this.XmlHttp.send(null);   
        if ( this.AsyncFlag == false ) 
            this.PostReceivedProcess() ;
        } ;
    
    this.PostReceivedProcess = function () {
        this.ClearSelectObj() ;
        this.UpdateSelectObj();
        } ;
    
    this.ClearSelectObj = function () {
        for (var iiii = 0 ; iiii < this.TargetSelectObj.options.length ; iiii ++ ) {
            this.TargetSelectObj.options[iiii] = null ;
            } ;
        this.TargetSelectObj.options.length = 0 ;
        } ;
        
    this.UpdateSelectObj = function() {
        var DefaultIndex = 0 ;
        //alert( this.XmlHttp.responseText ) ;
        ResultData = this.XmlHttp.responseXML.getElementsByTagName("Item");
        for (var iiii = 0 ; iiii < ResultData.length ; iiii ++ ) {
            var NewOption = new Option( ResultData[iiii].getAttribute("Text") , ResultData[iiii].getAttribute("Value")  ) ;
            this.TargetSelectObj.options[iiii] = NewOption  ;
            if ( DefaultIndex == 0 ) 
                if ( this.TargetSelectObjDefault == ResultData[iiii].getAttribute("Value") )  
                    DefaultIndex = iiii ;
            }
        if ( this.TargetSelectObj.options.length > 0 ) 
            this.TargetSelectObj.options[DefaultIndex].selected = true ;
        } ;

    this.RandUrl = function() {
        this.Rand();
        if ( this.UrlPath.indexOf("?") > 0 ) {
            this.UrlPath =  this.UrlPath + this.RndStr ;
            }
        else {
            this.UrlPath =  this.UrlPath + "?" + this.RndStr ;
            } ;
        } ;

    this.Rand = function() {
        this.RndStr = "rnd=" + (Math.random()*5)  ;
        } ;
        
    this.ClearAndDisplayWait = function () {
        this.ClearSelectObj() ;
        var NewOption = new Option( "更新資料中 Loading..." , "" ) ;
        // this.TargetSelectObj.options.add ( NewOption ) ;
        this.TargetSelectObj.options[0] = NewOption ;
        } ;
    
    this.GetDefaultList = function () {
        this.ClearAndDisplayWait() ;
        this.GetXmlHttpData();        
        } ;

	this.AddOneParamKey = function ( NewKey ) {
		this.ParamAry_Key.push(NewKey) ;
		this.ParamAry_Value.push("") ;
		} ;

	this.MakeURLString = function () {
		var TmpUrlStr = "" ;
		TmpUrlStr = this.SrcUrlPath + this.URLParam_QueryStart ;
		for (var Tmpiiii = 0 ; Tmpiiii < this.ParamAry_Key.length ; Tmpiiii++) {
			TmpUrlStr = TmpUrlStr + this.ParamAry_Key[Tmpiiii] + "="  + escape(this.ParamAry_Value[Tmpiiii]) + this.URLParam_QueryConn ;
			} ;
		this.UrlPath = TmpUrlStr ;
		} ;
    
    this.InitObj = function() {
        this.CreateXmlHttpRequest();        
        } ;
        
  
    this.InitObj() ;

}

function SetOptionDefault ( InObj , KeyValue ) {
    //alert( "SetOptionDefault " + InObj.length );
    for (var iiii = 0 ; iiii < InObj.options.length ; iiii ++ ) {
        try { 
            if ( InObj.options[iiii].value == KeyValue )  {
                InObj.options[iiii].selected = true ;
                iiii = InObj.length + 100 ;
                }
            }  catch (e01) { } ;
        } ;



}

function CleanOption ( InObj ) {
    for (var iiii = 0 ; iiii < InObj.options.length ; iiii ++ ) {
        InObj.options[iiii] = null ;
        } ;
    InObj.options.length = 0 ;
}

