
function DYnamicOpts(Sel1,Sel2,Sel3,prace1,Reload_,groupID,amount_, DbTbType){
        this.IDOpt = 1;
        this.opts =null;

        this.tes1 = new Array(150);
        this.tes2 = new Array(150);
        this.tes3 = new Array(150);
        this.arrlinks1 =  new Array(150);
        this.arrlinks2 =  new Array(150);
        this.arrlinks3 =  new Array(150);
        this.arrcount1 =  new Array(150);
        this.arrcount2 =  new Array(150);
        this.arrcount3 =  new Array(150);

        this._splres = new Array(4);
        
        
        this.opdef1 = new Option("--Select--",0);
        this.opdef2 = new Option("--Select--",0);
        this.opdef3 = new Option("--Select--",0);

        this.Sel1 = Sel1;
        this.Sel2 = Sel2;
        this.Sel3 = Sel3;
        this.prace1 = prace1;
        this.amount = amount_;
        this.Reload_ = Reload_;
        this.groupID = groupID;
        this.DbTbType = DbTbType;
        
        this.oVDiv =null;
        var _this = this;
        
        
        this.Sel1.onchange = function(){
                _this.OptClear(_this.Sel2);
                var result = _this.conArg(_this.Sel1,1)
                if(result){
                    _this.ajaxRead(2 ,result,1,null);
                }else{
                var idsel_ = _this.conArg2(_this.Sel1,1);
                _this.ajaxRead(2 ,result,0,idsel_);
                }
        }

         this.Sel2.onchange = function(){
                _this.OptClear(_this.Sel3);
                var result = _this.conArg(_this.Sel2,2)
                if(result){
                    _this.ajaxRead(3 ,result,1,null);
                }else{
                var idsel_ = _this.conArg2(_this.Sel2,2);
                _this.ajaxRead(3 ,result,0,idsel_);
                }
        }

         this.Sel3.onchange = function(){
                _this.prace1.value=0;
                var result = _this.conArg(_this.Sel3,3)
                if(result){
                    _this.ajaxRead(4 ,result,1,null);
                }else{
                var idsel_ = _this.conArg2(_this.Sel3,3);
                _this.ajaxRead(4 ,result,0,idsel_);
                }
        }
        
        this.amount.onkeyup = function(){
           _this.prace1.value= _this._splres[0] * _this.amount.value;
       	   _this.GetAllValue();
        }

        this.Reload_.onclick = function()
        {
            _this.OptClear(_this.Sel1);
            _this.OptClear(_this.Sel2);
            _this.OptClear(_this.Sel3);
            _this.prace1.value=0;
            _this.ajaxRead(1,0,0,0);
        }
}

DYnamicOpts.prototype._request = undefined;

DYnamicOpts.prototype._getXMLHTTPRequest = function()
{
	var xmlHttp;
	try{xmlHttp = new ActiveXObject("Msxml2.XMLHttp");}
	catch(e)
	{
		try{xmlHttp = new ActiveXObject("Microsoft.XMLHttp");}
		catch(e2){}
	}
	if(xmlHttp == undefined && (typeof XMLHttpRequest != 'undefined'))
	{
		xmlHttp = new XMLHttpRequest();
	}
	return xmlHttp;
}

DYnamicOpts.prototype.ajaxRead = function(id_, arg_, ifidsel, idsel)
{
   this.IDOpt = id_;
   var _this = this;
   this._request = this._getXMLHTTPRequest();
   this._request.onreadystatechange = function(){_this.buildSelectBox()};

   if(ifidsel){
      var link_ = 'MakeOpt.php?TbType='+this.DbTbType+'&group_id='+
      this.groupID +'&optionID='+id_+arg_;
   } else{
      var link_ = 'MakeOpt.php?TbType='+this.DbTbType+'&group_id='+ 
      this.groupID +'&optionID='+id_+'&product_id='+idsel;
  }
  //alert(link_);
  this._request.open ('GET', link_, true);
  this._request.send ('');
}

DYnamicOpts.prototype.buildSelectBox =  function()
{

    if(this._request.readyState == 4){
     this.opts = this._request.responseXML.getElementsByTagName('option');
     this.Sel1.options[0] =  this.opdef1;
     this.Sel2.options[0] =  this.opdef2;
     this.Sel3.options[0] =  this.opdef3;

   for (var i = 0; i < this.opts.length; i++) {
       if(this.IDOpt == 4){
       	   var spl = unescape(this.opts[i].getElementsByTagName('model')[0].firstChild.data);
       	   this._splres = spl.split('_');
       	   this.prace1.value= this._splres[0] * this.amount.value;
       	   this.GetAllValue();
           //ConstructCart();
       }else{

          var op = new Option(unescape(this.opts[i].getElementsByTagName('model')[0].firstChild.data), i+1);

          if(this.IDOpt == 1){
              this.Sel1.options[i+1] = op;
              this.tes1[i] = unescape(this.opts[i].getElementsByTagName('product_id')[0].firstChild.data);
              this.arrcount1[i] = unescape(this.opts[i].getElementsByTagName('agrscount')[0].firstChild.data);
              if(this.arrcount1[i] > 1){
                    this.arrlinks1[i] = this.opts[i].getElementsByTagName('links')[0].firstChild.data;
              }
          }
          if(this.IDOpt == 2){
              this.Sel2.options[i+1] = op;
              this.tes2[i] = unescape(this.opts[i].getElementsByTagName('product_id')[0].firstChild.data);
              this.arrcount2[i] = unescape(this.opts[i].getElementsByTagName('agrscount')[0].firstChild.data);
              if(this.arrcount2[i] > 1){
                 this.arrlinks2[i] = this.opts[i].getElementsByTagName('links')[0].firstChild.data;
              }
          }
          if(this.IDOpt == 3){ 
              this.Sel3.options[i+1] = op; 
              this.tes3[i] = unescape(this.opts[i].getElementsByTagName('product_id')[0].firstChild.data);
              this.arrcount3[i] = unescape(this.opts[i].getElementsByTagName('agrscount')[0].firstChild.data);
              if(this.arrcount3[i] > 1){
                 this.arrlinks3[i] = this.opts[i].getElementsByTagName('links')[0].firstChild.data
              }
          }
       }
     }
  }

}


DYnamicOpts.prototype.conArg = function(SelOb,hist)
{
    var sel1 =SelOb;
    var id_ = sel1.selectedIndex -1;

    if(hist == 1){
        var Acount = this.arrcount1[id_];
        var Alinks = this.arrlinks1[id_];
        if(Acount > 1){
            var res = Alinks+'&argcount='+Acount;
            return res;
        }
    }
    if(hist == 2){
        var Acount2 = this.arrcount2[id_];
        var Alinks2 = this.arrlinks2[id_];
        if(Acount2> 1){
            var res2 = Alinks2+'&argcount='+Acount2;
            return res2;
        }
    }
return null;
}

DYnamicOpts.prototype.conArg2 = function(SelOb,hist)
{
    var sel2 =SelOb;
    var id_ = sel2.selectedIndex-1;
    var idsel_= null;
    if(hist == 1){idsel_ = this.tes1[id_]; }
    if(hist == 2){idsel_ = this.tes2[id_]; }
    if(hist == 3){idsel_ = this.tes3[id_]; }
    return idsel_;
}

DYnamicOpts.prototype.GetAllValue = function()
{
	
	var Valprs = this.prace1.value; //price
    var FVal = this.Sel1.options[this.Sel1.selectedIndex].text; //Opt1
    var SVal = this.Sel2.options[this.Sel2.selectedIndex].text; //Opt2
    var TVal = this.Sel3.options[this.Sel3.selectedIndex].text; //Opt3
    var Aname = this._splres[1];
    var Amnt = this.amount.value;
    var DbT_ = this.DbTbType;
    
    
    var res = '&product_id='+this.groupID+'&FVal='+FVal+'&SVal='+SVal+'&TVal='+TVal+'&Valprs='+Valprs+'&Aname='+Aname+'&Amount='+Amnt+'&PType='+DbT_;
    this.oVDiv = document.getElementById(this.groupID);
    document.getElementById('model_pic_'+this.groupID).innerHTML=Aname;
	this.oVDiv.name = res;
    return res;
}

DYnamicOpts.prototype.OptClear = function (opts_)
{
    var len = opts_.options.length;
    this.prace1.value=0;
    for(var kk=1;  kk <= len; kk++)
        opts_.options[kk] = null;
}
