﻿XSelect = function(divSelectId,ajaxMethods,valueName,textName,Values,selectText) 
{ 
this.selectObj = getIdTags(divSelectId,"select"); 
this.ajaxMethods = ajaxMethods; 
this.valueName = valueName; 
this.textName = textName; 
this.Values = (Values == undefined|| Values.length == 0) ? null : Values.split("|");
this.DefaultValue = this.DefaultValue ? this.DefaultValue : 0;
this.AllEnabledEmpty = true;
this.FirstEnabledEmpty = true;
this.selectText = (selectText == undefined|| selectText.length == 0) ? "-请选择-" : selectText;
}; 
XSelect.prototype.onload = function() 
{ 
    me = this; 
    var objLength = this.selectObj.length; 
    for(var i=0; i<objLength; i++) 
    {
      if(this.AllEnabledEmpty)
      {
        setStartValue(this.selectObj[i],"-1",this.selectText);
      }
      else
      {
        if(i == 0 && this.FirstEnabledEmpty) setStartValue(this.selectObj[i],"-1",this.selectText);
      }
      if(i<objLength-1) 
      { 
       this.selectObj[i].onchange = function(){ 
        me.Obj = this; 
        me.onchange(); 
       }; 
  } 
} 
this.onchange(); 
}; 
XSelect.prototype.onchange = function() 
{ 
   me = this; 
   var cValue = this.currentValue ? this.currentValue : (this.Obj ? this.Obj.value : this.DefaultValue); 
    this.nextObj = this.Obj ? nextSibling(this.Obj) : this.selectObj[0]; 

    get_currentObjId = function() 
    { 
      for(var i = 0; i < me.selectObj.length; i++) 
      { 
       if(me.selectObj[i] == me.nextObj) 
       { 
        return i; 
       } 
      } 
}; 

var ds = eval(this.ajaxMethods + "("+ cValue +")").value; 
this.nextObj.length = 0;
if(this.AllEnabledEmpty)
{
   setStartValue(this.nextObj,"-1",this.selectText);
}
else
{
    if(this.nextObj == this.selectObj[0] && this.FirstEnabledEmpty) setStartValue(this.nextObj,"-1",this.selectText);
}
if(ds != null && typeof(ds) == "object" && ds.Tables != null) 
{ 
  var isSelected = false;   
  for(var i=0;  i<ds.Tables[0].Rows.length; i++) 
     { 
      var optionsText = eval("ds.Tables[0].Rows[i]."+ this.textName); 
      var optionsValue = eval("ds.Tables[0].Rows[i]."+ this.valueName);   
   if(this.Values != null && this.Values[get_currentObjId()] && optionsValue.toString() ==this.Values[get_currentObjId()].toString()) 
   {   
    this.nextObj.options.add(new Option(optionsText,optionsValue,true,false));
    if(this.AllEnabledEmpty)
    {
       this.nextObj.options[i+1].selected = true ;
    }
    else 
    {
        if(this.nextObj == this.selectObj[0] && this.FirstEnabledEmpty) {this.nextObj.options[i+1].selected = true ; }
        else { this.nextObj.options[i].selected = true ;}    
    }     
    isSelected = true;   
   } 
   else 
   {
       try{ this.nextObj.options.add(new Option(optionsText,optionsValue));}
       catch(ex){}
   } 
     }   
  this.hide(); 
  if(isSelected && nextSibling(this.nextObj)) 
  { 
   this.Obj = this.nextObj; 
   this.onchange(); 
  }   
} 
}; 

XSelect.prototype.hide = function() 
{ 
var cObj = this.nextObj; 
var cAnonSiblings = anonSibling(cObj); 
if(!(cObj.length>1)){cObj.length = 0;cObj.style.display = "none";}else{cObj.style.display = "";} 
if(cAnonSiblings && cAnonSiblings.length > 0) 
{ 
  for(var i = 0;i < cAnonSiblings.length;i++) 
  { 
   cAnonSiblings[i].length = 0; 
   cAnonSiblings[i].style.display = "none"; 
  } 
} 
}; 
setStartValue = function(obj,value,text) 
{ 
obj.options.add(new Option(text,value)); 
}; 
function $$(id) 
{ 
  return document.getElementById(id); 
} 
function getIdTags(id,tagName) 
{ 
  return $$(id).getElementsByTagName(tagName); 
} 
function nextSibling(obj) 
{ 
var nextSiblingObj = obj.nextSibling; 
if(nextSiblingObj) 
{ 
  if(nextSiblingObj.nodeType!=1) 
  { 
   return nextSibling(nextSiblingObj); 
  } 
  return nextSiblingObj; 
} 
} 
function anonSibling(obj) 
{ 
var Objs = new Array(); 
var cObj = obj; 
var i = 0; 
while(nextSibling(cObj)) 
{ 
  cObj = nextSibling(cObj); 
  Objs[i] = cObj; 
  i++; 
} 
return Objs; 
}
String.prototype.trim = function()
{
	return this.replace(/(^\s*)|(\s*$)/g, "");
};