var InsControl = function(){
	this.radioControl = null;
	this.selectControl = null ;
	this.inputControl = null;
	this.seatControl = null;
	this.xzControl = null ;
	this.beControl = null ;
	this.bzControl = null ;
	this.zkControl = null ;
	this.zbControl = null ;
	this.InsInfo   = null ;
	this.seating = null;
	this.gcode = null ; 
	this.fathercontrol = new Array() ;
	this.childcontrol = null ;

	this.addChild = function()
	{
		if ( this.gcode == 'C101') this.childcontrol = 'C204' ;
		if ( this.gcode == 'C102') this.childcontrol = 'C205' ;
		if ( this.gcode == 'C103') this.childcontrol = 'C211' ;
		if ( this.gcode == 'C104') this.childcontrol = 'C206' ;
		if ( this.gcode == 'C105') this.childcontrol = 'C207' ;
		if ( this.gcode == 'C201') this.childcontrol = 'C208' ;
		if ( this.gcode == 'C202') this.childcontrol = 'C209' ;
		if ( this.gcode == 'C203') this.childcontrol = 'C210' ;
	}
	
	this.addfather = function()
	{
		if ( this.gcode == 'C206_C207')
		{
			this.fathercontrol.add('C104');
			this.fathercontrol.add('C105');
		}
		if ( this.gcode == 'C208_C209_C210')
		{
			this.fathercontrol.add('C201');
			this.fathercontrol.add('C202');
			this.fathercontrol.add('C203');
		}
		if ( this.gcode == 'C204')
			this.fathercontrol.add('C101');
		if ( this.gcode == 'C205')
			this.fathercontrol.add('C102');
		if ( this.gcode == 'C211')
			this.fathercontrol.add('C103');
	}
	
	this.initInsControl = function(_inscode,father)
	{		
		if ( _inscode == 'C206' || _inscode == 'C207')
		{
			this.gcode = 'C206_C207';
		}
		else if ( _inscode == 'C208' || _inscode == 'C209' || _inscode == 'C210' )
		{
			this.gcode = 'C208_C209_C210';
		}
		else
		{
			this.gcode = _inscode ;
		}
		this.radioControl = father.getUT("select_" + this.gcode ) ;
		if ( this.gcode == 'C101' || this.gcode == 'C203')
			this.xzControl = father.getUT("xz_" + this.gcode);
		this.beControl = father.getUT("be_" + this.gcode);
		this.bzControl = father.getUT("bz_" + this.gcode);  
		this.zkControl = father.getUT("zk_" + this.gcode); 
		this.zbControl = father.getUT("zb_" + this.gcode);
		if ( this.gcode == 'C102' || this.gcode == 'C104' || 
				this.gcode == 'C105' || this.gcode == 'C202' || 
				this.gcode == 'C201' )
		{
			this.selectControl = father.getUT("item_" + this.gcode );
		}
		if ( this.gcode == 'C105')
		{
			this.seatControl = father.getUT("item_seat");
		}
		if ( this.gcode == 'C103' )
		{
			this.inputControl = father.getUT("input_C103") ;
		}
		
		if ( this.radioControl != null )
		{
			LEAP.checkbox.setValue(this.radioControl,null); 
			LEAP.checkbox.setDisabled( this.radioControl, true , null,'01');
			father.addEvent(this.radioControl,'valueChange',this.radioValueChange ,this ); 
		}
		this.addChild();
		this.addfather();
	}
	
	this.clearValue = function()
	{
		this.clearCalculateValue();
		this.clearInputValue();
		this.clearSeatValue();
		this.clearSelectValue();
		this.clearxzValue();
		this.InsInfo = null ;
		LEAP.checkbox.setDisabled( this.radioControl, true , null,'01');
	}
	
	this.radioValueChange = function(arg)
	{
		if ( arg.arg == null ) arg.arg=arg ;
		if ( arg.arg.radioControl == null ) return null ;
		
		var checkdata = LEAP.checkbox.getValue(arg.arg.radioControl);
		if ( checkdata == null || checkdata ==',')
		{
			arg.arg.clearCalculateValue();
			arg.arg.clearInputValue();
			arg.arg.clearSeatValue();
			arg.arg.clearSelectValue();
			arg.arg.clearxzValue();
			
			if ( arg.arg.childcontrol != null )
			{
				if (this.inscontrol.contains(arg.arg.childcontrol ))
				{
					var tempchild = this.inscontrol.getvalue( arg.arg.childcontrol ) ;
					if ( tempchild != null )
					{
						if ( tempchild.fathercontrol.length <= 1 )
						{
							LEAP.checkbox.setDisabled( tempchild.radioControl , true , null,'01');
						}
						else
						{
							var readonly = false;
							for( var i = 0 ; i < tempchild.fathercontrol.length ; i ++ )
							{
								var _code = tempchild.fathercontrol[i];
								if( _code != arg.arg.gcode && this.inscontrol.contains(_code))
								{
									var checkfather = LEAP.checkbox.getValue( this.inscontrol.getvalue(_code).radioControl );
									if ( checkfather != null && checkfather !=',')
									{
										readonly = true;
									}
								}
								_code = null;
							}
							if ( !readonly )
								LEAP.checkbox.setDisabled( tempchild.radioControl , true , null,'01');
						}
					}
					tempchild = null ;
				}
			}
		}
		else
		{
			arg.arg.setInputValue();
			arg.arg.setSeatValue(this.seating);
			arg.arg.setSelectValue();
			arg.arg.setxzValue();
			
			if ( arg.arg.childcontrol != null )
			{
				if (this.inscontrol.contains(arg.arg.childcontrol))
				{
					var tempchild = this.inscontrol.getvalue(arg.arg.childcontrol ) ;
					if ( tempchild != null && tempchild.InsInfo != null )
					{
						LEAP.checkbox.setDisabled( tempchild.radioControl , false , null,'01');
					}
					tempchild = null ;
				}
			}
		}
		checkdata = null;
	};
		
	this.setValue = function(_insData,otherData,father)
	{
		this.InsInfo = _insData ;
		this.seating = otherData ;
		if ( this.InsInfo != null )
		{
			if ( this.InsInfo.creator != "" )
			{
				if ( this.radioControl != null )
				{
					LEAP.checkbox.setDisabled( this.radioControl , false , null,'01');
					LEAP.checkbox.setValue(this.radioControl,"01"); 
					this.setSelectValue();
					this.setxzValue();
					this.setSeatValue();
					this.setInputValue();
					this.setCalculateValue();
					if ( this.childcontrol != null )
					{
						if (father.inscontrol.contains(this.childcontrol))
						{
							var tempchild = father.inscontrol.getvalue(this.childcontrol ) ;
							if ( tempchild != null && tempchild.InsInfo != null )
							{
								var checkdata = LEAP.checkbox.getValue(tempchild.radioControl);
								if ( checkdata == null || checkdata ==',')
									LEAP.checkbox.setDisabled( tempchild.radioControl , false , null,'01');
								checkdata = null;
							}
							tempchild = null ;
						}
					}
				}
			}
			else
			{
				if ( this.radioControl != null )
				{
					if ( this.fathercontrol.length == 0 )
						LEAP.checkbox.setDisabled( this.radioControl , false , null,'01');
					else
					{
						var setreadonly = true;
						for( var i = 0 ; i < this.fathercontrol.length ; i ++ )
						{
							var _code = this.fathercontrol[i];
							if( father.inscontrol.contains(_code))
							{
								var checkfather = LEAP.checkbox.getValue( father.inscontrol.getvalue(_code).radioControl );
								if ( checkfather != null && checkfather !=',' && father.inscontrol.getvalue(_code).InsInfo != null )
								{
									setreadonly = false;
								}
							}
							_code = null;
						}
						LEAP.checkbox.setDisabled( this.radioControl , setreadonly , null,'01');
						setreadonly = null;
					}
					LEAP.checkbox.setValue(this.radioControl,null); 
					this.clearCalculateValue();
				}
			}
		}
	};
	
	this.setCalculated = function(_insData)
	{
		this.InsInfo = _insData ;
		if ( this.InsInfo != null )
		{
			if ( this.InsInfo.creator != "" )
			{
				if ( this.radioControl != null )
				{
					LEAP.checkbox.setValue(this.radioControl,"01"); 
					this.setCalculateValue();
				}
			}
			else
			{
				if ( this.radioControl != null )
				{
					LEAP.checkbox.setValue(this.radioControl,null); 
					this.clearCalculateValue();
				}
			}
		}
	};
		
	this.setSelectValue = function()
	{
		if ( this.selectControl == null ) return ;
		if ( this.InsInfo == null ) return ;
		if ( this.InsInfo.subclass == null ) return ; 
		
		var setSelectValue = null;
		LEAP.select.clearItem(this.selectControl);
		for( var i = 0 ; i < this.InsInfo.subclass.length ; i ++ )
		{
			var tempsc = this.InsInfo.subclass[i];
			if ( tempsc != null )
			{
				LEAP.select.addItem(this.selectControl,tempsc.id,tempsc.parameterdesc);
			}
			tempsc = null;
		}
		LEAP.select.setValue(this.selectControl, this.InsInfo.creator );			
	};
	
	this.clearSelectValue = function()
	{
		if ( this.selectControl == null ) return ;
		LEAP.select.clearItem(this.selectControl);
	};
	
	this.setxzValue = function()
	{
		if ( this.xzControl == null ) return ; 
		if ( this.InsInfo == null ) return ;
		if ( this.InsInfo.subclass == null ) return ;
		if ( this.InsInfo.subclass.length == 0 ) return ;
		if ( this.InsInfo.subclass[0] == null ) return ;
		this.xzControl.innerText = this.changeNum(Number(this.InsInfo.subclass[0].remark),2) ;
	};
	
	this.clearxzValue = function()
	{
		if ( this.xzControl == null ) return ; 
		this.xzControl.innerText = "";
	};
	
	this.setInputValue = function()
	{
		if ( this.inputControl == null ) return ; 
		if ( this.InsInfo == null ) return ;
		if ( this.InsInfo.subclass == null ) return ;
		if ( this.InsInfo.subclass.length == 0 ) return ;
		if ( this.InsInfo.subclass[0] == null ) return ;
		if ( this.InsInfo.inputvalue != null )
		{
			if ( this.InsInfo.inputvalue > 0 )
			{
				this.inputControl.value =  this.changeNum(Number(this.InsInfo.inputvalue),2) ;
			}
			else
			{
				this.inputControl.value = this.changeNum(Number(this.InsInfo.subclass[0].remark),2) ;
			}
		}
		else
		{
			this.inputControl.value = this.changeNum(Number(this.InsInfo.subclass[0].remark),2) ;
		}
	};
	
	this.clearInputValue = function()
	{
		if ( this.inputControl == null ) return ; 
		this.inputControl.innerText = "";
	};
	
	this.setSeatValue = function()
	{
		if ( this.seatControl == null ) return ;
		LEAP.select.clearItem(this.seatControl);
		if ( this.seating != null )
		{
			for( var i = 1 ; i < this.seating ; i ++ )
			{
				LEAP.select.addItem(this.seatControl,i.toString(),i.toString()+'座');
			}
			if ( this.InsInfo.inputvalue != null )
			{
				if ( this.InsInfo.inputvalue > 0 )
				{
					LEAP.select.setValue(this.seatControl, this.InsInfo.inputvalue.toString() );
				}
				else
				{
					LEAP.select.setValue(this.seatControl, this.seating.toString() );
				}
			}
			else
			{
				LEAP.select.setValue(this.seatControl, this.seating.toString() );
			}
		}
	};
	
	this.clearSeatValue = function()
	{
		if ( this.seatControl == null ) return ;
		LEAP.select.clearItem(this.seatControl);
	};
	
	this.setCalculateValue = function()
	{
		if ( this.InsInfo == null ) return ;
		if ( this.InsInfo.premium == null ) return ;
		if ( this.InsInfo.premium <= 0 ) return ;
		
		if ( this.gcode == 'C101' || this.gcode == 'C102' || this.gcode == 'C103' || this.gcode == 'C104' || this.gcode == 'C105' || 
			this.gcode == 'C202' || this.gcode == 'C203' )
		{
			this.beControl.innerText = this.changeNum(this.InsInfo.suminsured,2) ;
		}
		
		if ( this.gcode == 'C206_C207' || this.gcode == 'C208_C209_C210' )
		{
			var tempk = this.bzControl.innerText ;
			if ( tempk == "---")
				this.bzControl.innerText = this.changeNum( this.InsInfo.premium,2) ;
			else
			{
				var tempn = Number(tempk) + this.InsInfo.premium ;
				this.bzControl.innerText = this.changeNum(tempn,2);
			}
			
			tempk = this.zbControl.innerText ;
			if ( tempk == "---")
				this.zbControl.innerText = this.changeNum(this.InsInfo.premium * this.InsInfo.discountrate,2) ;
			else
			{
				var tempn = Number(tempk) + this.InsInfo.premium * this.InsInfo.discountrate ;
				this.zbControl.innerText = this.changeNum(tempn,2) ;
				tempn = null;
			}
			tempk = null;
		}
		else
		{
			this.bzControl.innerText = this.changeNum(this.InsInfo.premium,2) ;
			this.zbControl.innerText = this.changeNum(this.InsInfo.premium * this.InsInfo.discountrate ,2) ;
		}
		this.zkControl.innerText = this.changeNum(this.InsInfo.discountrate,3) ;
	};
	
	this.getEasyValue = function()
	{
		if ( this.radioControl == null ) return null ;
		var checkdata = LEAP.radio.getValue(this.radioControl);
		
		if ( checkdata != null )
		{
			if ( checkdata == "01" )
			{
				var k = "1";
				if ( this.selectControl != null )
				{
					k = LEAP.select.getValue( this.selectControl );
				}
				if ( this.inputControl != null )
				{
					var tempvalue = this.changeNum(Number(this.inputControl.value),2) ;
					k = tempvalue.toString();
				}
				if ( this.xzControl != null )
				{
					var xzCont = this.changeNum(Number(this.xzControl.value),2) ;
					k = xzCont.toString();
				}
				return this.gcode + "," + k ;
			}
			else
			{
				return null;
			}
		}
		else
		{
			return null ;
		}
	}
	this.getValue = function()
	{
		if ( this.InsInfo == null ) return null ;
		if ( this.radioControl == null ) return null ;
		
		var checkdata = LEAP.checkbox.getValue(this.radioControl);
		if ( checkdata != null )
		{   
			if(checkdata !=','){
				if(checkdata.startWith(',')||checkdata.endWith(',')){
				checkdata = "01";
				}
			}
			if ( checkdata == "01" )
			{
				this.InsInfo.creator = "1" ;
			}
			else
			{
				this.InsInfo.creator = "" ;
				return this.InsInfo ;
			}
		}
		else
		{
			this.InsInfo.creator = "" ;
			return this.InsInfo ;
		}
		
		if ( this.selectControl != null )
		{
			this.InsInfo.creator = LEAP.select.getValue( this.selectControl );
		}
		
		if ( this.seatControl != null )
		{
			this.InsInfo.inputvalue = LEAP.select.getValue( this.seatControl );
		}
		
		if ( this.inputControl != null )
		{
			var tempvalue = this.changeNum(Number(this.inputControl.value),2) ;
			
			this.InsInfo.inputvalue = tempvalue.toString();
		}
		
		checkdata = null;
		return this.InsInfo ;
	};
	
	this.getSelectedValue = function()
	{
		if ( this.radioControl == null ) return null ;
		var checkdata = LEAP.checkbox.getValue(this.radioControl);
		if ( checkdata == null ||checkdata ==',')
		{
			return null;
		}
		else
		{
			return this.getValue();
		}
	};
	
	this.clearCalculateValue = function()
	{
		this.beControl.innerText = "---";
		this.zkControl.innerText = "---";
		this.bzControl.innerText = "---";
		this.zbControl.innerText = "---";
	};
	
	this.changeNum = function(num,n)  
	{
		var  dd=1;  
		var  tempnum;  
		for(i=0;i<n;i++)  
		{  
			dd*=10;  
		}  
		tempnum=num*dd;  
		tempnum=Math.round(tempnum);  
		return tempnum / dd;
	};
	
	this.initInsControl_ = function(_inscode,father)
	{		
		if ( _inscode == 'C206' || _inscode == 'C207')
		{
			this.gcode = 'C206_C207';
		}
		else if ( _inscode == 'C208' || _inscode == 'C209' || _inscode == 'C210' )
		{
			this.gcode = 'C208_C209_C210';
		}
		else
		{
			this.gcode = _inscode ;
		}
		this.radioControl = father.getUT("select_" + this.gcode ) ;
		if ( this.gcode == 'C101' || this.gcode == 'C203')
		this.xzControl = father.getUT("xz_" + this.gcode);
//		this.beControl = father.getUT("be_" + this.gcode);
//		this.bzControl = father.getUT("bz_" + this.gcode);  
//		this.zkControl = father.getUT("zk_" + this.gcode); 
//		this.zbControl = father.getUT("zb_" + this.gcode);
		if ( this.gcode == 'C102' || this.gcode == 'C104' || 
				this.gcode == 'C105' || this.gcode == 'C202' || 
				this.gcode == 'C201' )
		{
			this.selectControl = father.getUT("item_" + this.gcode );
		}
		if ( this.gcode == 'C105')
		{
			this.seatControl = father.getUT("item_seat");
		}
		if ( this.gcode == 'C103' )
		{
			this.inputControl = father.getUT("input_C103") ;
		}
		
		if ( this.radioControl != null )
		{
			LEAP.radio.setValue(this.radioControl,null); 
//			LEAP.radio.setDisabled( this.radioControl, true , null,'01');
//			father.addEvent(this.radioControl,'valueChange',this.radioValueChange ,this ); 
		}
		this.addChild();
		this.addfather();
	}
}
