String.prototype.Contains = function( textToCheck )
{
	return ( this.indexOf( textToCheck ) > -1 ) ;
}

var s = navigator.userAgent.toLowerCase() ;

var BrowserInfo = 
{
	IsIE		: s.Contains('msie'),
	IsIE7		: s.Contains('msie 7'),
	IsGecko		: s.Contains('gecko/'),
	IsSafari	: s.Contains('safari'),
	IsOpera		: s.Contains('opera')
};

BrowserInfo.IsGeckoLike = BrowserInfo.IsGecko || BrowserInfo.IsSafari || BrowserInfo.IsOpera ;

if ( BrowserInfo.IsGecko )
{
	var sGeckoVersion = s.match( /gecko\/(\d+)/ )[1] ;
	BrowserInfo.IsGecko10 = sGeckoVersion < 20051111 ;
}
/****************************************************************/

var JsTools = new Object() ;

JsTools.AddEventListenerEx = function( sourceObject, eventName, listener, paramsArray )
{
	var o = new Object() ;
	o.Source = sourceObject ;
	o.Params = paramsArray || [] ;	
	o.Listener = function( ev )
	{
		return listener.apply( o.Source, [ ev ].concat( o.Params ) ) ;
	}
	
	sourceObject.attachEvent( 'on' + eventName, o.Listener ) ;

	sourceObject = null ;
	paramsArray = null ;
}

JsTools.CancelEvent = function( e )
{
	return false ;
}

JsTools.AppendStyleSheet = function( documentElement, cssFileUrl )
{
	return documentElement.createStyleSheet( cssFileUrl ).owningElement ;
}

JsTools.GetElementPosition = function( el, relativeWindow )
{
	var c = { X:0, Y:0 } ;
	
	var oWindow = relativeWindow || window ;

	while ( el )
	{
		c.X += el.offsetLeft - el.scrollLeft ;
		c.Y += el.offsetTop - el.scrollTop  ;

		if ( el.offsetParent == null )
		{
			var oOwnerWindow = JsTools.GetElementWindow( el ) ;
			
			if ( oOwnerWindow != oWindow )
				el = oOwnerWindow.frameElement ;
			else
			{
				c.X += el.scrollLeft ;
				c.Y += el.scrollTop  ;
				break ;
			}
		}
		else
			el = el.offsetParent ;
	}

	// Return the Coordinates object
	return c ;
}

JsTools.GetElementDocument = function ( element )
{
	return element.ownerDocument || element.document ;
}

JsTools.GetElementWindow = function( element )
{
	return this.GetDocumentWindow( this.GetElementDocument( element ) ) ;
}

JsTools.GetParentWindow = function( document )
{
	return document.contentWindow ? document.contentWindow : document.parentWindow ;
}

JsTools.AppendElement = function( target, elementName )
{
	return target.appendChild( this.GetElementDocument( target ).createElement( elementName ) ) ;
}

JsTools.DisableSelection = function( element )
{
	element.unselectable = 'on' ;

	var e, i = 0 ;
	while ( e = element.all[ i++ ] )
	{
		switch ( e.tagName )
		{
			case 'IFRAME' :
			case 'TEXTAREA' :
			case 'INPUT' :
			case 'SELECT' :
				/* Ignore the above tags */
				break ;
			default :
				e.unselectable = 'on' ;
		}
	}
}

JsTools.GetViewPaneSize = function( win )
{
	var oSizeSource ;
	
	var oDoc = win.document.documentElement ;
	if ( oDoc && oDoc.clientWidth )				// IE6 Strict Mode
		oSizeSource = oDoc ;
	else
		oSizeSource = top.document.body ;		// Other IEs
	
	if ( oSizeSource )
		return { Width : oSizeSource.clientWidth, Height : oSizeSource.clientHeight } ;
	else
		return { Width : 0, Height : 0 } ;
}

JsTools.GetScrollPosition = function( relativeWindow )
{
	var oDoc = relativeWindow.document ;

	var oPos = { X : oDoc.documentElement.scrollLeft, Y : oDoc.documentElement.scrollTop } ;
	
	if ( oPos.X > 0 || oPos.Y > 0 )
		return oPos ;

	return { X : oDoc.body.scrollLeft, Y : oDoc.body.scrollTop } ;
}

/****************************************************************/

var JsPanel = function( parentWindow )
{
	this.IsContextMenu	= false ;
	this._LockCounter	= 0 ;
	
	this._Window = parentWindow || window ;
	
	var oDocument ;
	
	if ( BrowserInfo.IsIE )
	{
		this._Popup	= this._Window.createPopup() ;
		oDocument = this.Document = this._Popup.document ;
	}
	else
	{
		var oIFrame = this._IFrame = this._Window.document.createElement('iframe') ; 
		oIFrame.src					= 'javascript:void(0)' ;
		oIFrame.allowTransparency	= true ;
		oIFrame.frameBorder			= '0' ;
		oIFrame.scrolling			= 'no' ;
		oIFrame.style.position		= 'absolute';
		oIFrame.style.zIndex		= 10000 ;
		oIFrame.width = oIFrame.height = 0 ;

		if ( this._Window == window.parent )
			window.frameElement.parentNode.insertBefore( oIFrame, window.frameElement ) ;
		else
			this._Window.document.body.appendChild( oIFrame ) ;
		
		var oIFrameWindow = oIFrame.contentWindow ; 
		
		oDocument = this.Document = oIFrameWindow.document ;

		oDocument.open() ;
		oDocument.write( '<html><head></head><body style="margin:0px;padding:0px;"><\/body><\/html>' ) ;
		oDocument.close() ;

		JsTools.AddEventListenerEx( oIFrameWindow, 'focus', JsPanel_Window_OnFocus, this ) ;
		JsTools.AddEventListenerEx( oIFrameWindow, 'blur', JsPanel_Window_OnBlur, this ) ;
	}
	
	oDocument.oncontextmenu = JsTools.CancelEvent ;

	this.MainNode = oDocument.body.appendChild( oDocument.createElement('DIV') ) ;

	this.MainNode.style.cssFloat = 'left' ;

}


JsPanel.prototype.AppendStyleSheet = function( styleSheet )
{
	JsTools.AppendStyleSheet( this.Document, styleSheet ) ;
}

JsPanel.prototype.Preload = function( x, y, relElement )
{
	if ( this._Popup )
		this._Popup.show( x, y, 0, 0, relElement ) ;
}

JsPanel.prototype.Show = function( x, y, relElement, width, height )
{
	if ( this._Popup )
	{
		this._Popup.show( x, y, 0, 0, relElement ) ;

		this.MainNode.style.width	= width ? width + 'px' : '' ;
		this.MainNode.style.height	= height ? height + 'px' : '' ;
		
		var iMainWidth = this.MainNode.offsetWidth ;
	
		this._Popup.show( x, y, iMainWidth, this.MainNode.offsetHeight, relElement ) ;
	}
	else
	{
		//FCKFocusManager.Lock() ;

		if ( this.ParentPanel )
			this.ParentPanel.Lock() ;

		this.MainNode.style.width	= width ? width + 'px' : '' ;
		this.MainNode.style.height	= height ? height + 'px' : '' ;

		var iMainWidth = this.MainNode.offsetWidth ;

		if ( !width )	this._IFrame.width	= 1 ;
		if ( !height )	this._IFrame.height	= 1 ;

		iMainWidth = this.MainNode.offsetWidth ;

		var oPos = JsTools.GetElementPosition( ( relElement.nodeType == 9 ? relElement.body : relElement), this._Window ) ;

		x += oPos.X ;
		y += oPos.Y ;

		var oViewPaneSize = JsTools.GetViewPaneSize( this._Window ) ;
		var oScrollPosition = JsTools.GetScrollPosition( this._Window ) ;
		
		var iViewPaneHeight	= oViewPaneSize.Height + oScrollPosition.Y ;
		var iViewPaneWidth	= oViewPaneSize.Width + oScrollPosition.X ;

		if ( ( x + iMainWidth ) > iViewPaneWidth )
			x -= x + iMainWidth - iViewPaneWidth ;

		if ( ( y + this.MainNode.offsetHeight ) > iViewPaneHeight )
			y -= y + this.MainNode.offsetHeight - iViewPaneHeight ;
		
		if ( x < 0 )
			 x = 0 ;

		this._IFrame.style.left	= x + 'px' ;
		this._IFrame.style.top	= y + 'px' ;
		
		var iWidth	= iMainWidth ;
		var iHeight	= this.MainNode.offsetHeight ;
		
		this._IFrame.width	= iWidth ;
		this._IFrame.height = iHeight ;

		this._IFrame.contentWindow.focus() ;
	}

	this._IsOpened = true ;
}

JsPanel.prototype.Hide = function()
{
	if ( this._Popup )
		this._Popup.hide() ;
	else
	{
		if ( !this._IsOpened ) return ;
		
		//FCKFocusManager.Unlock() ;

		this._IFrame.width = this._IFrame.height = 0 ;

		this._IsOpened = false ;
		
		if ( this.ParentPanel ) this.ParentPanel.Unlock() ;

	}
}

JsPanel.prototype.CheckIsOpened = function()
{
	if ( this._Popup )
		return this._Popup.isOpen ;
	else
		return this._IsOpened ;
}

JsPanel.prototype.CreateChildPanel = function()
{
	var oWindow = this._Popup ? JsTools.GetParentWindow( this.Document ) : this._Window ;

	var oChildPanel = new JsPanel( oWindow, true ) ;
	oChildPanel.ParentPanel = this ;
	
	return oChildPanel ;
}

JsPanel.prototype.Lock = function()
{
	this._LockCounter++ ;
}

JsPanel.prototype.Unlock = function()
{
	if ( --this._LockCounter == 0 && !this.HasFocus )
		this.Hide() ;
}

function JsPanel_Window_OnFocus( e, panel )
{
	panel.HasFocus = true ;
}

function JsPanel_Window_OnBlur( e, panel )
{
	panel.HasFocus = false ;
}

/****************************************************************/

function JsTree(object,container,expandDepth,multiple)
{
    var _object=object;
    
    this.items=[];
    this.container=container;
    this.expandDepth=expandDepth;
    this.multiple=(multiple==null)?false:multiple;
    this.document=container._Panel.Document;
    
    this.values=[];
    this.texts=[];
    
    if(container.valueField.value) this.values=container.valueField.value.split(",");
    if(container.textField.value) this.texts=container.textField.value.split(",");
    
    var _tree=this.tree=this;
    var _depth=this.depth=0;
    
    	this.isInValue=function(v)
	{
	    for(var i=0;i<this.values.length;i++)
	    {
	        if(v==this.values[i])
	        {
	            return true;
	        }
	    }
	    return false;
	}
	
    for(var n=0;n<_object.childNodes.length;n++)
	{
		var node=_object.childNodes[n];
		if(node.nodeName=="DIV")
		{
		    node.style.textIndent=(_depth*1)+"em";
			this.items[this.items.length]=new JsTreeNode(_tree,node);
		}
	}
}

/****************************************************************/

function JsTreeNode(parent,object)
{
	var _parent=this.parent=parent;
	var _object=this.object=object;
	
	var _items=this.items=[];
	var _tree=this.tree=_parent.tree;
	
	var _treeNode=this;
	var _depth=this.depth=_parent.depth+1;
	var _expandDepth=this.tree.expandDepth;
	
	this.expanded=(_depth<_expandDepth);	

	
	
	this.imageNode=null;
	this.textNode=null;
	
	for(var i=0;i<_object.childNodes.length;i++)
	{
	    if(_object.childNodes[i].nodeName=="SPAN") this.treeItem=new JsTreeItem(_treeNode,_object.childNodes[0]);
	    if(_object.childNodes[i].nodeName=="DIV")
	    {
	        var _subItem=this.subItem=_object.childNodes[i];
        	
	        _subItem.style.textIndent=(_depth*2)+"em";
        	
	        for(var n=0;n<_subItem.childNodes.length;n++)
	        {
		        var node=_subItem.childNodes[n];
		        if(node.nodeName=="DIV") _treeNode.items[_treeNode.items.length]=new JsTreeNode(_treeNode,node);
	        }
	    }
	}

	this.expandItem=function()
	{
        if(_treeNode.expanded)
        {
            _treeNode.subItem.style.display="block";
	    }
	}
	
	this.collapseItem=function()
	{
        if(!(_treeNode.expanded))
        {
            _treeNode.subItem.style.display="none";
	    }
	}
	
	this.checkChild=function(v)
	{
	    for(var i=0;i<_treeNode.items.length;i++)
        {
            _treeNode.items[i].checkNode.checked=v;
            _treeNode.items[i].checkChild(v);
        }
	}
	
	this.checkParent=function(v)
	{
	    var _pChecked=true;
        if(!v)
        {
            _pChecked=false
        }
        else
        {
            var _brothers=_treeNode.parent.items;
            
            for(var i=0;i<_brothers.length;i++)
            {
                if(!(_brothers[i].checkNode.checked))
                {
                    _pChecked=false;
                    break;
                }
            }
            if(_pChecked)
            {
                if(_treeNode.parent.checkNode!=null)
                {
                    _treeNode.parent.checkNode.checked=true;
                }
            }
        }
        if(_treeNode.parent.checkNode!=null)
        {
            _treeNode.parent.checkNode.checked=_pChecked;
            _treeNode.parent.checkParent(_pChecked);
        }
	}
	
    this.getMultiValue=function()
	{
	    if(_treeNode.checkNode.checked)
	    {
	        _tree.values[_tree.values.length] = _treeNode.value;
	        _tree.texts[_tree.texts.length] = _treeNode.text;
	        return;
	    }
	    else
	    {
	        for(var i=0;i<_treeNode.items.length;i++)
            {
                _treeNode.items[i].getMultiValue();
            }
	    }
	}
	
	this.refresh=function()
	{
	    if(_items.length==0)
	    {
	        _treeNode.expanded=false;
	        _treeNode.imageNode.className="treeTransparent";
	    }
	    else 
	    {
	        if(_treeNode.expanded)
	        {
	            _treeNode.expandItem();
	            _treeNode.imageNode.className="treeExpanded";
	        }
	        else
	        {
	            _treeNode.collapseItem();
	            _treeNode.imageNode.className="treeCollapsed";
	        }
	        if(_tree.multiple)
	        {
	            var isIn=_tree.isInValue(_treeNode.value);
	            if(isIn)
	            {
	                _treeNode.checkNode.click();
	            }
	        }
	    }
	}
	
	this.refresh();
}

function JsTreeItem(parent,object)
{
    var _treeNode   = this.treeNode         =parent;
    var _object     = this.object           =object;
    var _tree       = _treeNode.tree;
    var _imageNode  = _treeNode.imageNode   =_object.childNodes[0];
    var _checkNode  = _treeNode.checkNode   =_object.childNodes[1];
    var _textNode   = _treeNode.textNode    =_object.childNodes[2];
    
    
    _imageNode.className="treeExpanded";
    _imageNode.onclick=function()
        {
            _treeNode.expanded=!_treeNode.expanded;
            if(_treeNode.subItem!=null)
            {
                if(_treeNode.expanded)
                {
                    _imageNode.className="treeExpanded";
                    _treeNode.expandItem();
                }
                else
                {
                    _imageNode.className="treeCollapsed";
                    _treeNode.collapseItem();
                }
            }
            else
            {
                _imageNode.className="treeTransparent";
            }           
        };
        
    _textNode.className="treeText";
    var txt=_textNode.getAttribute("MenuText");
    var val=_textNode.getAttribute("MenuValue");
    _treeNode.text=txt;
    _treeNode.value=val;
    _textNode.onclick=function()
        {
            this.className="treeText";

            if(typeof(_tree.container.setLabel) == 'function')
            {
                if(_tree.multiple)
                {
                    _checkNode.click();
                }
                else
                {
                    _tree.container.setLabel(val,txt);
                }
            }
        };
    _textNode.onmouseover=function()
        {
            this.className="treeTextOver";
        };
    _textNode.onmouseout=function()
        {
            this.className="treeText";
        };
        
    if(_tree.multiple)
    {
        _checkNode.onclick=function()
        {
            _treeNode.checkChild(this.checked);
            _treeNode.checkParent(this.checked);
            _tree.values=[];
            _tree.texts=[];
            for(var i=0;i<_tree.items.length;i++)
            {
                _tree.items[i].getMultiValue();
            }
            
            var val="";
            var txt="";
            if(_tree.values.length>0)
            {
                val=_tree.values[0];
                txt=_tree.texts[0];
                
                for(var i=1;i<_tree.values.length;i++)
                {
                    val=val+","+_tree.values[i];
                    txt=txt+","+_tree.texts[i];
                }
            }
            _tree.container.setLabel(val,txt);
        }
    }
    else
    {
        _checkNode.style.display="none";
    }
}

/****************************************************************/

var JsCombo = function(fieldWidth, panelWidth, panelMaxHeight,cssPath,parentWindow)
{
	this.FieldWidth		= fieldWidth || 100 ;
	this.PanelWidth		= panelWidth || 150 ;
	this.PanelMaxHeight	= panelMaxHeight || 150 ;
	this.Label			= '&nbsp;' ;

    this.textField=null;
    this.valueField=null;
	this.Enabled = true ;
	this.init    = false;
	
	this.Items = new Object() ;
	this.onBeforeClick=null;
	this.onChange=null;
	this._Panel = new JsPanel( parentWindow || window, true ) ;
	
	this._Panel.AppendStyleSheet(cssPath+'tree.css') ;
	this._PanelBox = this._Panel.MainNode.appendChild( this._Panel.Document.createElement( 'DIV' ) ) ;
	
	this._PanelBox.className = 'SC_Panel' ;
	this._PanelBox.style.width = this.PanelWidth + 'px' ;

	this._PanelBox.innerHTML = '<table cellpadding="0" cellspacing="0" width="100%" style="TABLE-LAYOUT: fixed"><tr><td nowrap></td></tr></table>' ;
	
	var oDiv = this._ItemsHolderEl = this._PanelBox.getElementsByTagName('TD')[0].appendChild( this._Panel.Document.createElement( 'DIV' )) ;
	oDiv.className = 'SC_Item' ;
	if ( BrowserInfo.IsIE )	oDiv.style.width = '100%' ;
}

JsCombo.prototype.SetHtml = function(html)
{
	var oDiv = this._ItemsHolderEl ;
	oDiv.innerHTML = html ;
    return oDiv ;
}

JsCombo.prototype.Create = function( targetElement )
{
	var oDoc = JsTools.GetElementDocument(targetElement ) ;
	var eOuterTable = this._OuterTable = targetElement.appendChild( oDoc.createElement( 'TABLE' ) ) ;
	
	eOuterTable.cellPadding = 0 ;
	eOuterTable.cellSpacing = 0 ;
	
	eOuterTable.insertRow(-1) ;
	
	var sClass ;
	
	var oField = JsTools.AppendElement( eOuterTable.rows[0].insertCell(-1), 'div' ) ;
	
	oField.className = 'SC_Field' ;
	oField.style.width = this.FieldWidth + 'px' ;
	oField.innerHTML = '<table width="100%" cellpadding="0" cellspacing="0" style="TABLE-LAYOUT: fixed;"><tbody><tr><td class="SC_FieldLabel"><label>&nbsp;</label></td><td class="SC_FieldButton">&nbsp;</td></tr></tbody></table>' ;

	this._LabelEl = oField.getElementsByTagName('label')[0] ;
	this._LabelEl.innerHTML = this.Label ;


	var dropdownTree = this ;
	
	oField.onmouseover = function()
	{
	    if(dropdownTree.Enabled) this.className = 'SC_Field SC_FieldOver' ;
	};
	oField.onmouseout = function()
	{
	    this.className='SC_Field';
	};
	oField.onclick = function()
	{
	    if (dropdownTree.Enabled )
	    {
	        if(typeof(dropdownTree.onBeforeClick) == 'function') dropdownTree.onBeforeClick(dropdownTree);
		    var oPanel			= dropdownTree._Panel ;
		    var oPanelBox		= dropdownTree._PanelBox ;
		    var oItemsHolder	= dropdownTree._ItemsHolderEl ;
		    var iMaxHeight		= dropdownTree.PanelMaxHeight ;
    		
		    if ( BrowserInfo.IsIE )
			    oPanel.Preload( 0, this.offsetHeight, this ) ;

		    oPanelBox.style.height = iMaxHeight + 'px' ;

		    oPanel.Show( 0, this.offsetHeight, this) ;
	    }
	};
	
	JsTools.DisableSelection( this._Panel.Document.body ) ;
}

JsCombo.prototype.setLabel = function(value,text)
{
    var oValue="";
	this.Label = text.length == 0 ? '&nbsp;' : text ;
	if(this.textField!=null) this.textField.value=text;
	if(this.valueField!=null)
	{
	    oValue=this.valueField.value;
	    this.valueField.value=value;
	}
	if (this._LabelEl) this._LabelEl.innerHTML = this.Label ;
	this._Panel.Hide();
	
	if(typeof(this.onChange) == 'function'&&oValue!=value) this.onChange();
}

JsCombo.prototype.setEnabled = function(isEnabled )
{
	this.Enabled = isEnabled ;
	this._OuterTable.className = isEnabled ? '' : 'SC_FieldDisabled' ;
}

/****************************************************************/

function JsSelect(object,container)
{
    var _object=object;
    
    this.items=[];
    this.container=container;
    
    var _select=this.select=this;
    
    for(var n=0;n<_object.childNodes.length;n++)
	{
		var node=_object.childNodes[n];
		if(node.nodeName=="DIV")
		{
			this.items[this.items.length]=new JsSelectItem(_select,node);
		}
	}
}

/****************************************************************/

function JsSelectItem(parent,object)
{
    var _object     = this.object           =object;
    var _select     = parent.select;
        
    _object.className="selectText";
    _object.onclick=function()
        {
            this.className="selectText";
            var txt=_object.getAttribute("SelectText");
            var val=_object.getAttribute("SelectValue");
            if(typeof(_select.container.setLabel) == 'function')
            {
                _select.container.setLabel(val,txt);
            }
        };
    _object.onmouseover=function()
        {
            this.className="selectTextOver";
        };
    _object.onmouseout=function()
        {
            this.className="selectText";
        };
}

/****************************************************************/

function JsCalendar(container)
{
    this.container=container;
    this.calendar=null;
    this.currentDate=[];
    this.today=[];
    
    var theDate=[];
    
    this.setDefaultDate();
    
    var _currentDate=this._currentDate=container.valueField.value;
    if(_currentDate)
    {
        if(!this.checkDate(theDate)) return null;
    }
    else
    {
		this.currentDate[0]=this.today[0];
	    this.currentDate[1]=this.today[1];
	    this.currentDate[2]=this.today[2];
	
    }
    



    this.document=container._Panel.Document;
    this.body = container._ItemsHolderEl.appendChild( this.document.createElement( 'TABLE' )) ;
    this.body.className="calendarBody";
    
    this.initCalendar();
}

JsCalendar.prototype.clear=function()
{
    this.container.setLabel("","");
}

JsCalendar.prototype.setDefaultDate=function()
{
	var dftDate=new Date();
	this.today[0]=dftDate.getYear();
	this.today[1]=dftDate.getMonth()+1;
	this.today[2]=dftDate.getDate();
}

JsCalendar.prototype.checkDate=function(theDate)
{
	var reg=/^\d{4}-\d{1,2}-\d{1,2}$/
	if(!this._currentDate.match(reg))
	{
		//this._currentDate=new Date();
		var dftDate=new Date();
		this._currentDate=dftDate.getYear()+"-"+(dftDate.getMonth()+1)+"-"+dftDate.getDate();;
	
		// alert("默认日期的格式不正确\n\n默认日期可接受格式为:'yyyy-mm-dd'");
		 //return false;
	}
	theDate=this._currentDate.split("-");
	
	var y = this.currentDate[0] = parseInt(theDate[0]-0);
	var m = this.currentDate[1] = parseInt(theDate[1]-0);
	var d = this.currentDate[2] = parseInt(theDate[2]-0);
	
	switch(m)
	{
	    case 1:
	    case 3:
	    case 5:
	    case 7:
	    case 8:
	    case 10:
	    case 12:
		    if(d>31 || d <1)
		    {
			    alert("输入的日期不正确\n\n"+y + "年" + m + "月没有第" + d + "天");
			    return false;
		    }
		    break;
	    case 2:
		    if(d<1 || ((y%400==0 || (y%4==0 && y%100!=0)) && d>29) || ((y%4!=0 || (y%100==0 && y%400!=0)) && d>28))
		    {
			    alert("输入的日期不正确\n\n"+ y + "年" + m + "月没有第" + d + "天");
			    return false;
		    }
		    break;
	    case 4:
	    case 6:
	    case 9:
	    case 11:
		    if(d>30 || d<1)
		    {
			    alert("输入的日期不正确\n\n"+ y + "年" + m + "月没有第" + d + "天");
			    return false;
		    }
		    break;
	    default:
		    alert("输入的日期不正确\n\n"+ y + "年没有第" + m + "月");
		    return false;
	}
	return true;
}

JsCalendar.prototype.initCalendar=function()
{
    var body=this.body;
    var calendar=this;
	var row=null;
	var cell=null;

	row = body.insertRow(0);
	
	cell=row.insertCell(0);
	cell.bgColor="#BDE1C6";
	cell.align="left";
	
	this.year = this.insertSelect(cell,0,"年",1979,2030);
	this.month = this.insertSelect(cell,1,"月",0,12);

	this.preYear = this.insertTbCell(row,1,"<<","center");
	this.preYear.title="上一年";
	this.preYear.onclick=function()
	{
		if(calendar.currentDate[0]>1980)
		{
			calendar.currentDate[0]--;
			calendar.year.selectedIndex++;
			calendar.setDay();
			calendar.showCalendar();
		}
	}

	this.preMonth = this.insertTbCell(row,2,"<","center");
	this.preMonth.title="上一月";
	this.preMonth.onclick=function()
	{
		if (calendar.currentDate[0]>1980 || (calendar.currentDate[0]==1980 && calendar.currentDate[1]>1))
		{
			calendar.currentDate[1]--;
			
			if(calendar.currentDate[1]==0 && calendar.currentDate[0]>1980)
			{
				calendar.year.selectedIndex++;
				calendar.month.selectedIndex = 0;
				calendar.currentDate[1]=12;
				calendar.currentDate[0]--;
			}
			else
			{
				calendar.month.selectedIndex++;
			}
		}
		calendar.setDay();
		calendar.showCalendar();
	}

	this.preMonth = this.insertTbCell(row,3,">","center");
	this.preMonth.title="下一月";
	this.preMonth.onclick=function()
	{
		if(calendar.currentDate[0]<2030 || (calendar.currentDate[0]==2030 && calendar.currentDate[1]<12))
		{
			calendar.currentDate[1]++;
			if(calendar.currentDate[1]==13)
			{
				calendar.year.selectedIndex--;
				calendar.month.selectedIndex = 11;
				calendar.currentDate[1]=1;
				calendar.currentDate[0]++;
			}
			else
			{
				calendar.month.selectedIndex--;
			}
		}
		calendar.setDay();
		calendar.showCalendar();
	}

	this.nextYear = this.insertTbCell(row,4,">>","center");
	this.nextYear.title="下一年";
	this.nextYear.onclick=function()
	{
		if(calendar.currentDate[0]<2030)
		{
			calendar.year.selectedIndex--;
			calendar.currentDate[0]++;
			calendar.setDay();
			calendar.showCalendar();
		}
	}

	this.calendarClear = this.insertTbCell(row,5,"Clear","center");
	this.calendarClear.title="空白";
	this.calendarClear.onclick=function(){calendar.clear();}

	row = body.insertRow(1);
	cell= row.insertCell(0);
	cell.colSpan=7;
	cell.height=138;

	cell.vAlign="top";
	
	var oCalendar=this.calendar=cell.appendChild(this.document.createElement("table"));
	oCalendar.className="calendarPanel";
	oCalendar.cellPadding=0;
	oCalendar.cellSpacing=1;
	oCalendar.height="100%";

	this.showCalendar();
	
	row = body.insertRow(2);
	var foot=row.insertCell(0);
	foot.colSpan=7;
	foot.height=20;

	var weekDays = new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
	var iWDays = new Date(calendar.today[0],calendar.today[1]-1,calendar.today[2]);
	var days = iWDays.getDay();

	foot.innerText = "今天：" + this.today[0] + "年" + this.today[1] + "月" + this.today[2] + "日 [" + weekDays[days] + "]";
}

JsCalendar.prototype.showCalendar=function()
{
	var firstDay;
	var monthLength;
	var pannel=this.calendar;
	var year=this.currentDate[0];
	var month=this.currentDate[1];

	firstDay=this.getFirstDay(year,month);
	monthLength=firstDay+this.getMonthLength(year,month);

	var rowsCount = Math.ceil((monthLength)/7);
	
	while (pannel.rows.length > 0) 
	{
		pannel.deleteRow(0)
	}

	var row = pannel.insertRow(0);
	var dateName = new Array("日","一","二","三","四","五","六");
	
	for (var i = 0; i < 7; i++) 
	{
		var cell=row.insertCell(i)
		cell.innerText=dateName[i];
		cell.align="center";
		cell.width=35;
		cell.height=16;
		cell.className="calendarTitle";
	}

	var n=0;day=0;
	for(i=0;i<rowsCount;i++)
	{
		var row=pannel.insertRow(i+1);
		for(j=0;j<7;j++)
		{
			n++;
			if(n>firstDay && n<=monthLength)
			{
				day=n-firstDay;
				this.insertBodyCell(row,j,day);
			}
			else
			{
				var cell=row.insertCell(j);
				cell.className="cellBlank";
			}
		}
	}
}

JsCalendar.prototype.insertBodyCell=function(row,j,day,targetObject)
{
	var bgColor;	
	var cell=row.insertCell(j);
    var calendar=this;
	cell.innerText=day;
	cell.align="center";
	cell.width=35;
	cell.height=16;

    var normalCss="calendarCell cellNormal_N";
	
	
	if(j==0)
	{
		normalCss="calendarCell cellNormal_S";
	}
	else
	{
		normalCss="calendarCell cellNormal_N";
	}
	if(day==calendar.today[2])
	{
		normalCss="calendarCell cellNormal_T";
	}
	if(day==calendar.currentDate[2])
	{
		normalCss="calendarCell cellNormal_C";
	}
	cell.className=normalCss;

	cell.onmouseout=function()
	{
		cell.className=normalCss;
	}
	cell.onmousedown=function()
	{ 
		cell.className="calendarCell cellNormalDown";
	}
	cell.onmouseover=function()
	{
		cell.className="calendarCell cellNormalOver";
	}
	
	cell.onclick=function()
	{
		calendar.currentDate[2]=cell.innerText;
		calendar.selectDate();
	}
}

JsCalendar.prototype.insertTbCell=function(row,index,text,align,colSpan)
{
	var cell=row.insertCell(index);
	if(colSpan!=null) cell.colSpan=colSpan;
	cell.align="center";
	cell.innerText=text;
	cell.className="calendarCell ButtonNormal";
	cell.onmouseover=function()
	{
		cell.className="calendarCell ButtonOver";
	}
	cell.onmouseout=function()
	{
		cell.className="calendarCell ButtonNormal";
	}
	cell.onmousedown=function()
	{
		cell.className="calendarCell ButtonNormal";
	}
	cell.onmouseup=function()
	{
		cell.className="calendarCell ButtonOver";
	}
	return cell;
}

JsCalendar.prototype.insertSelect=function(cell,index,name,from,to)
{
	var oOption;
	var calendar=this;

	var sel=cell.appendChild(this.document.createElement("select"));

    var current=parseInt(this.currentDate[index]);
	for(var i=to;i>current;i--)
	{
		oOption = this.document.createElement("OPTION");
		sel.options.add(oOption);
		oOption.innerText = i+name;
		oOption.value = i;
	}

	oOption = this.document.createElement("OPTION");
	sel.options.add(oOption);
	oOption.innerText = current+name;
	oOption.value = current;
	oOption.selected=true;

	for(var i=current-1;i>from;i--)
	{
		oOption = this.document.createElement("OPTION");
		sel.options.add(oOption);
		oOption.innerText = i+name;
		oOption.value = i;
	}

	sel.onchange=function(){
		calendar.currentDate[index]=this.value;
		calendar.setDay();
		calendar.showCalendar();
	}
	return sel;
}

JsCalendar.prototype.getFirstDay=function(year, month)
{
	var firstDate = new Date(year,month-1,1);
	return firstDate.getDay();
}

JsCalendar.prototype.getMonthLength=function(year, month) 
{
	month--;
	var oneDay = 1000 * 60 * 60 * 24;
	var thisMonth = new Date(year, month, 1);
	var nextMonth = new Date(year, month + 1, 1);
	var len = Math.ceil((nextMonth.getTime() - thisMonth.getTime())/oneDay);
	return len;
}

JsCalendar.prototype.setDay=function()
{
	var y=parseInt(this.currentDate[0]);
	var m=parseInt(this.currentDate[1]);
	var d=parseInt(this.currentDate[2]);
	switch(m){
	case 2:
		if((y%400==0 || (y%4==0 && y%100!=0)) && d>29) 
		{
			this.currentDate[2]=29;
			return;
		}
		if((y%4!=0 || (y%100==0 && y%400!=0)) && d>28)
		{
			this.currentDate[2]=28;
			return;
		}
		break;
	case 4:
	case 6:
	case 9:
	case 11:
		if(d>30)
		{
			this.currentDate[2]=30;
		}
		break;
	default:
		return;
	}
}

JsCalendar.prototype.selectDate = function()
{
    var date=this.currentDate[0]+"-"+this.currentDate[1]+"-"+this.currentDate[2];
    this.container.setLabel(date,date);
    this.showCalendar();
}
//*****************************

function JsMCalendar(container)
{
    this.container=container;
    this.calendar=null;
    this.currentDate=[];
    this.today=[];
    
    var theDate=[];
    
    this.setDefaultDate();
    
    var _currentDate=this._currentDate=container.valueField.value+"-1";
    if(_currentDate)
    {
        if(!this.checkDate(theDate)) return null;
    }
    else
    {
		this.currentDate[0]=this.today[0];
	    this.currentDate[1]=this.today[1];   
	    this.currentDate[2]=this.today[2];   
    }
    



    this.document=container._Panel.Document;
    this.body = container._ItemsHolderEl.appendChild( this.document.createElement( 'TABLE' )) ;
    this.body.className="calendarBody";
    this.body.bgColor="#eeeeee";
    this.initCalendar();
}

JsMCalendar.prototype.clear=function()
{
    this.container.setLabel("","");
}

JsMCalendar.prototype.setDefaultDate=function()
{
	var dftDate=new Date();
	this.today[0]=dftDate.getYear();
	this.today[1]=dftDate.getMonth()+1;	
	this.today[2]=dftDate.getDate();
}


JsMCalendar.prototype.initCalendar=function()
{
    var body=this.body;
    var calendar=this;
	var row=null;
	var cell=null;

	row = body.insertRow(0);
	
	cell=row.insertCell(0);
	//cell.bgColor="#BDE1C6";
	cell.align="center";
	
	//this.year = this.insertSelect(cell,0,"年",1979,2030);
	//this.month = this.insertSelect(cell,1,"月",0,12);

	this.preYear = this.insertTbCell(row,0,"<<","center");
	this.preYear.title="上一年";
	this.preYear.onclick=function()
	{
		if(calendar.currentDate[0]>1980)
		{
			calendar.currentDate[0]--;
			//calendar.year.selectedIndex++;
			 calendar.year.innerText=calendar.currentDate[0]+"年";
			calendar.setDay();
			calendar.showCalendar();
		}
	}
    this.year = row.insertCell(1);
   
   
    this.year.innerText=this.currentDate[0]+"年";
    this.year.align="center";
	this.nextYear = this.insertTbCell(row,2,">>","center");
	this.nextYear.title="下一年";
	this.nextYear.onclick=function()
	{
		if(calendar.currentDate[0]<2030)
		{
			
			calendar.currentDate[0]++;
			calendar.year.innerText=calendar.currentDate[0]+"年";
			calendar.setDay();
			calendar.showCalendar();
		}
	}
	this.calendarClear = this.insertTbCell(row,3,"Clear","center");
	this.calendarClear.title="空白";
	this.calendarClear.onclick=function(){calendar.clear();}

	row = body.insertRow(1);
	cell= row.insertCell(0);
	cell.colSpan=4;
	cell.height=95;

	cell.vAlign="top";
	cell.align="center";
	var oCalendar=this.calendar=cell.appendChild(this.document.createElement("table"));
	oCalendar.className="calendarPanel";
	oCalendar.cellPadding=0;
	oCalendar.cellSpacing=1;
	oCalendar.height="100%";

	this.showCalendar();
	
	row = body.insertRow(2);
	var foot=row.insertCell(0);
	foot.colSpan=7;
	foot.height=20;

	var weekDays = new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
	var iWDays = new Date(calendar.today[0],calendar.today[1]-1,calendar.today[2]);
	var days = iWDays.getDay();

	foot.innerText = "今天：" + this.today[0] + "年" + this.today[1] + "月" + this.today[2] + "日 [" + weekDays[days] + "]";
}



JsMCalendar.prototype.showCalendar=function()
{
	var pannel=this.calendar;
	var year=this.currentDate[0];
	var month=this.currentDate[1];
	while (pannel.rows.length > 0) 
	{
		pannel.deleteRow(0)
	}

	var mon=0;	
	for(i=0;i<3;i++)
	{
		var row=pannel.insertRow(i);
		for(j=0;j<4;j++)
		{	
		    mon++;
		   this.insertBodyCell(row,j,mon,month);			
		}
	}
}

JsMCalendar.prototype.insertBodyCell=function(row,j,day,targetObject)
{
	var bgColor;	
	var cell=row.insertCell(j);
    var calendar=this;
	cell.innerText=day;
	cell.align="center";


    var normalCss="calendarCell cellNormal_N";	
	
	var nowDay=new Date().getMonth()+1;
	if(day==nowDay)
	{
	    normalCss="calendarCell cellNormal_S";
	}
	if(day==calendar.currentDate[1])
	{
		normalCss="calendarCell cellNormal_C";
	}
	cell.className=normalCss;

	cell.onmouseout=function()
	{
		cell.className=normalCss;
	}
	cell.onmousedown=function()
	{ 
		cell.className="calendarCell cellNormalDown";
	}
	cell.onmouseover=function()
	{
		cell.className="calendarCell cellNormalOver";
	}
		cell.width=40;
	cell.height=15;
	cell.onclick=function()
	{
		calendar.currentDate[1]=cell.innerText;
		calendar.selectDate();
	}
}

JsMCalendar.prototype.insertTbCell=function(row,index,text,align,colSpan)
{
	var cell=row.insertCell(index);
	if(colSpan!=null) cell.colSpan=colSpan;
	cell.align="center";
	cell.innerText=text;
	cell.className="calendarCell ButtonNormal";
	cell.onmouseover=function()
	{
		cell.className="calendarCell ButtonOver";
	}
	cell.onmouseout=function()
	{
		cell.className="calendarCell ButtonNormal";
	}
	cell.onmousedown=function()
	{
		cell.className="calendarCell ButtonNormal";
	}
	cell.onmouseup=function()
	{
		cell.className="calendarCell ButtonOver";
	}
	return cell;
}




JsMCalendar.prototype.setDay=function()
{
	var y=parseInt(this.currentDate[0]);
	var m=parseInt(this.currentDate[1]);
	var d=parseInt(this.currentDate[2]);
	switch(m){
	case 2:
		if((y%400==0 || (y%4==0 && y%100!=0)) && d>29) 
		{
			this.currentDate[2]=29;
			return;
		}
		if((y%4!=0 || (y%100==0 && y%400!=0)) && d>28)
		{
			this.currentDate[2]=28;
			return;
		}
		break;
	case 4:
	case 6:
	case 9:
	case 11:
		if(d>30)
		{
			this.currentDate[2]=30;
		}
		break;
	default:
		return;
	}
}

JsMCalendar.prototype.selectDate = function()
{
    var date=this.currentDate[0]+"-"+this.currentDate[1];//+"-"+this.currentDate[2];
    this.container.setLabel(date,date);
    this.showCalendar();
}
JsMCalendar.prototype.checkDate=function(theDate)
{
	var reg=/^\d{4}-\d{1,2}-\d{1,2}$/
	if(!this._currentDate.match(reg))
	{
		//this._currentDate=new Date();
		var dftDate=new Date();
		this._currentDate=dftDate.getYear()+"-"+(dftDate.getMonth()+1)+"-"+dftDate.getDate();;
	
		// alert("默认日期的格式不正确\n\n默认日期可接受格式为:'yyyy-mm-dd'");
		 //return false;
	}
	theDate=this._currentDate.split("-");
	
	var y = this.currentDate[0] = parseInt(theDate[0]-0);
	var m = this.currentDate[1] = parseInt(theDate[1]-0);

	return true;
}