function WCalendar76(Prefix, Value, Mask)
{
    this.Prefix = Prefix;
	this.selectedDate = typeof(Value) == 'object' ? Value : null;
	this.undoValue = Value;
	this.Text = typeof(Value) == 'string' ? Value : '';
	this.canBeEmpty = false;
	
	this.dispYear = this.selectedDate != null ? this.selectedDate.getFullYear() : new Date().getFullYear();
	this.dispMonth = this.selectedDate != null ? this.selectedDate.getMonth() : new Date().getMonth()
	
    this.divExpression = document.getElementById(Prefix + 'divExpression');
    this.edtIsExpression = document.getElementById(Prefix + 'edtIsExpression'); 
    this.edtExpression = document.getElementById(Prefix + 'edtExpression');
	
	this.divMain = document.getElementById(Prefix + 'divMain');
	this.frmMain = document.getElementById(Prefix + 'frmMain');
	(this.edtMain = document.getElementById(this.Prefix + 'edtMain')).WCalendar76 = this;
	this.tdDown  = document.getElementById(Prefix + 'tdDown');
	this.tblPopup = document.getElementById(Prefix + 'tblPopup');
	this.tblMain = document.getElementById(Prefix + 'tblMain');
	this.tblNavigator = document.getElementById(Prefix + 'tblNavigator');
	this.tdToday = document.getElementById(Prefix + 'tdToday');
	this.popupDirection = 'RightDownwards';
	
	this.tdHeader = document.getElementById(Prefix + 'tdHeader');
	this.name = Prefix.substring(0, Prefix.length - 1).replace('/','_');
	this.Mask = Mask;
	this.curPos = 0;
	this.lastSection  = this.Mask.substr(0, 1);
	this.type = 'WCalendar76';
	this.autoPostBack = false;
	this.uniqueId = null;
	this.container = null;

	
	// consts
	this.weekDays = new Array('Sun', 'Mon', 'Tues', 'Wed', 'Thu', 'Fri', 'Sat'); 
	//this.Months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	this.Months = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec');
	this.Separators = new Array('.', ':', '/', ' ', '-');
	
	/*events*/
	this.onblur = null;
    this.onkeydown = null;
    this.onfocus = null;
    

	/* -- Methods for work with Sections of Mask -- */
	
	this.getCurSection = function()
	{
		return this.Mask.substr(this.curPos, 1);
	}
	
	this.getSectionByPattern = function(Pattern)
	{
		return Pattern.substr(0, 1); //this.Mask.substr(this.Mask.indexOf(Pattern), 1);
	}
	
	this.getSectionLength = function(Section)
	{
		startIndex = this.Mask.indexOf(Section);
		endIndex = 0;
		while (this.Mask.substr(startIndex + endIndex , 1) == Section) endIndex++;
		return endIndex;
	}
		
	this.getSectionValue = function(Section)
	{
		return this.edtMain.value.substr(this.Mask.indexOf(Section), this.getSectionLength(Section));
	}
	
	
	this.getFirstSection = function()
	{
		return this.Mask.substr(0, 1);
	}
	
	this.getLastSection = function()
	{
		return this.Mask.substr(this.Mask.length - 1, 1);
	}
	
	this.getBuildSectionValue = function(Section)
	{
		curValue = this.getSectionValue(Section);
		if (this.ConvertToInt(curValue) == 0 || this.ConvertToInt(curValue) == null) curValue = this.getMinValueSection(Section);
		if (curValue || curValue==0) curValue = curValue.toString().trim().LPad('0', this.getSectionLength(Section));
		return curValue;
	}
	
	this.getMinValueSection = function(Section)
	{
		switch (Section)
		{
			case 'y': 
				{
					if (this.YearPattern() == 'yyyy') return 1900;
					else return 1;
				}
			case 'M': return 1;
			case 'd': return 1;
			case 'H': return 0;
			case 'm': return 0;
			case 's': return 0;
			default: 1;
		}
	}
	
	this.getMaxValueSection = function(Section)
	{
		switch (Section)
		{
			case 'y': return 9999;
			case 'M': 
			{
				return 12;
			}
			case 'd': 
			{
				curYear = this.ConvertToInt(this.getSectionValue('y'));
				curMonth = this.ConvertToInt(this.getSectionValue('M')); 
				if (curYear != null && curMonth != null) return this.ConvertToInt(this.lastDayOfMonth(new Date(curYear, curMonth - 1, 1)).getDate());
				else 
				return 31;
			}
			
			case 'H': return 23;
			case 'm': return 59;
			case 's': return 59;
			default: 12;
		}
	}
	
	this.getNextSection = function(Direct, CurSection)
	{
		//Direct = 1 - move right;D irect = -1 - move left;
		CurIndex = this.Mask.indexOf(CurSection);
		while (this.Mask.substr(CurIndex,1) == CurSection || this.IsSeparator(this.Mask.substr(CurIndex,1)))
		{
			CurIndex = CurIndex + Direct;
			if (CurIndex < 0) break;
			if (CurIndex >= this.Mask.length - 1) break;
		}
		return this.Mask.substr(CurIndex,1);
	}
	
	this.getPositionInSection = function(CurPos, CurSection) 
	{
	    return CurPos - this.Mask.indexOf(CurSection); // 0 - first position
	}
	
	this.setSection = function(newSection)
	{
		CurIndex = this.Mask.indexOf(newSection);
		if (CurIndex == -1) return;
		this.curPos = CurIndex;
		this.SetCursor(this.edtMain, this.curPos);
		if (this.lastSection != newSection)
		{
			this.onChangeSection();
		}
		
	}
	
	this.setSectionValue = function(Section, newValue)
	{
		startIndex = this.Mask.indexOf(Section);
		lenSection = this.getSectionLength(Section);
		this.edtMain.value = this.edtMain.value.substr(0, startIndex) + newValue + this.edtMain.value.substr(startIndex + lenSection, this.edtMain.value.length);
	}

	this.isValidSectionValue = function(Section)
	{
		if (this.Mask.indexOf(Section) == -1) return false; //if not found section;
		CurSectionValue = this.ConvertToInt(this.getSectionValue(Section));
		if (CurSectionValue == null) return  false;
		CurMonth = this.ConvertToInt(this.getSectionValue('M'));
		CurYear = this.ConvertToInt(this.getSectionValue('y'));
	
		if (Section == 'd' && CurMonth != null && CurYear != null)
			return  CurSectionValue >= this.getMinValueSection(Section) && CurSectionValue <= this.lastDayOfMonth(new Date(CurYear, CurMonth - 1, 1)).getDate();
		else 
			return  CurSectionValue >= this.getMinValueSection(Section) && CurSectionValue <= this.getMaxValueSection(Section);
	}
	
	this.existsSection = function(Section)
	{
		return this.Mask.indexOf(Section) != -1;
	}
	
	/* -- Methods for work with Mask -- */
	this.YearPattern = function()
    {
    	if (this.Mask.indexOf('yyyy') != -1) return 'yyyy';
		else if (this.Mask.indexOf('yy') != -1) return 'yy';
		else return '';
	}
	
	this.MonthPattern = function()
	{
		return 'MM';
	}
		
	this.DayPattern = function()
	{
		return 'dd';
	}

	this.HourPattern = function()
	{
		return 'HH';
	}

	this.MinutePattern = function()
	{
		return 'mm';
	}
	
	this.SecondPattern = function()
	{
		return 'ss';
	}
			
	this.MillisecondPattern = function()
	{
		return 'fff';
	}
	
	this.GetCurMaskSymbol = function()
	{
		return this.Mask.substr(this.curPos, 1);
	}
	
	this.GetIntPartByPattern = function(Pattern, Value)
	{
		if (this.Mask.indexOf(Pattern) != -1)
		{
			Result = this.ConvertToInt(Value.substr(this.Mask.indexOf(Pattern), Pattern.length));
			return Result != null ? Result : -1;
		}
		else return this.getMinValueSection(this.getSectionByPattern(Pattern));
	}
	
	this.IsSeparator = function(Char)
	{
		for (idxIsSep = 0; idxIsSep  < this.Separators.length; idxIsSep++)
			if (Char == this.Separators[idxIsSep]) return true;
		return false; 
	}
	
	this.onlyTime = function() 
	{
	     return (!this.existsSection("y") && !this.existsSection("M") && !this.existsSection("d"));
	}
	
	/* -- Methods for work with Cursor -- */

	this.InitCursor = function()
	{
		this.curPos	= this.GetCursor(this.edtMain);
		if (this.IsSeparator(this.GetCurMaskSymbol())) this.curPos++;
		if (this.curPos >= this.edtMain.value.length) this.curPos = this.edtMain.value.length - 1;
		if (this.lastSection != this.getCurSection()) this.onChangeSection();
		this.SelectCurSymbol(this.curPos);
	}
	
	this.GetCursor = function(input)
	{
	    if (document.selection)
	    {
		    var sel = document.selection.createRange();
		    sel.moveStart('textedit', -1);
		    return sel.text.length;
        }
        else if (input.selectionEnd)
            //return input.selectionStart;
            return input.selectionEnd;
        else
            return 0;
	}

	this.GetCurSymbol = function()
	{
		return this.edtMain.value.substr(this.curPos, 1);
	}
	
	this.setCursorBeginPosition = function()
	{
		this.curPos = 0;
		this.SetCursor(this.edtMain, this.curPos);
		this.SelectCurSymbol();
	}
	
	this.SetCursor = function(input, pos)
	{
	    if (input)
	        if (input.createTextRange)
	            try
	            {
		            var sel = input.createTextRange();
		            sel.collapse(true);
		            sel.moveEnd('character', pos);
		            sel.collapse(false);
		            sel.select();
	            }
		        catch (ex) { }
            else if (input.setSelectionRange)		
		        input.setSelectionRange(pos, pos)
	}
	
	this.SetCurSymbol = function(Symbol)
	{
	    if (document.selection)
	    {
		    var sel = document.selection.createRange();
		    sel.collapse(true);
		    sel.moveEnd('character', 1);
		    sel.select();
		    if (sel.text != '') 
		    {
			    sel.text = Symbol;
			    sel.collapse(false);
			    sel.select();
		    }
		}
		else if (this.edtMain.setSelectionRange)
		{
		    this.edtMain.value = this.edtMain.value.substr(0, this.curPos) + Symbol + this.edtMain.value.substr(this.curPos + 1);
		    this.edtMain.setSelectionRange(this.curPos, this.curPos + 1);
		}
	}
	
	this.SelectCurSymbol = function()
	{
		this.SetCursor(this.edtMain, this.curPos);
		if (document.selection)
		{
		    sel = document.selection.createRange();
		    sel.collapse(true);
		    sel.moveEnd('character', 1);
		    sel.select();
        }
        else if (this.edtMain.setSelectionRange)
            this.edtMain.setSelectionRange(this.curPos, this.curPos + 1);
	}

	/* -- Methods for work with DateTime value -- */
	
	this.equalDates = function(d1, d2)
	{
		return d1.getFullYear() == d2.getFullYear()  &&  d1.getMonth() == d2.getMonth()  &&  d1.getDate() == d2.getDate();
	}	
	
	this.GetCentury = function(curYear)
	{
		/*if (curYear > new Date().getFullYear() - 1950) return 1900
		else return 2000;*/
		
		var curCentury = Math.floor(  (new Date().getFullYear()) / 100 );
        return curYear < 30 ? curCentury : curCentury - 1;
	}
	
	this.lastDayOfMonth = function(date)
	{
		var res = new Date(date.getYear(), date.getMonth() + 1, 1);
		res.setDate(res.getDate() - 1);
		return res;
	}
		
	this.getDateStringWithMask = function()
	{
		var result = this.Mask;
		if (this.selectedDate == null) this.selectedDate = new Date();
		result = result.replace('yyyy', this.dispYear.toString().LPad('0', 4));
		result = result.replace('yy',  this.dispYear.toString().Right(2).LPad('0', 2));
		result = result.replace('MM',   (this.dispMonth + 1).toString().LPad('0', 2));
		result = result.replace('dd',   this.selectedDate.getDate().toString().LPad('0', 2));
		result = result.replace('HH',   this.selectedDate.getHours().toString().LPad('0', 2));
		result = result.replace('mm',   this.selectedDate.getMinutes().toString().LPad('0', 2));
		result = result.replace('ss',   this.selectedDate.getSeconds().toString().LPad('0', 2));
		
		return result;
	}
	
	this.DateTimeToStr = function(Date, Mask)	
	{	
		var result = Mask;
		result = result.replace('yyyy', Date.getFullYear().toString().LPad('0', 4));
		result = result.replace('yy',   Date.getFullYear().toString().Right(2).LPad('0', 2));
		result = result.replace('MM',   (Date.getMonth() + 1).toString().LPad('0', 2));
		result = result.replace('dd',   Date.getDate().toString().LPad('0', 2));
		result = result.replace('HH',   Date.getHours().toString().LPad('0', 2));
		result = result.replace('mm',   Date.getMinutes().toString().LPad('0', 2));
		result = result.replace('ss',   Date.getSeconds().toString().LPad('0', 2));

		return result;
	}
	
	this.StrToDateTimeByMask = function(Value)
	{
		var Year = this.GetIntPartByPattern(this.YearPattern(), Value);
		if (this.YearPattern() == 'yy' && Year != -1) 
		    Year = this.GetCentury(Year) * 100 + Year;
		
		var Month = this.GetIntPartByPattern(this.MonthPattern(), Value);
		
		var Day = this.GetIntPartByPattern(this.DayPattern(), Value);
		       
		var Hour = this.GetIntPartByPattern(this.HourPattern(), Value);
		var Minute = this.GetIntPartByPattern(this.MinutePattern(), Value);
		var Second = this.GetIntPartByPattern(this.SecondPattern(), Value);

        var Result = null;
		if (!(Year == -1 || Month == -1 || Day == -1 || Hour == -1 || Minute == -1 || Second == -1))
		{
			Result = new Date(Year, Month - 1, Day, Hour, Minute, Second);
			if (!(Result.getDate() == Day && Result.getMonth() + 1 == Month  &&  Result.getFullYear() == Year))  
			    Result = null;
		}
		
		return Result;
	}
	
	
	/* Methods for work with design Calendar control */	
	
	this.pointIsBelogn = function(x, y)
	{
		var ActiveObjectID = document.elementFromPoint(x, y).id;
		return ActiveObjectID == this.tblMain.id || ActiveObjectID == this.edtMain.id || ActiveObjectID == this.tdDown.id ||
			   x >= this.tblPopup.offsetLeft  &&  x <= this.tblPopup.offsetLeft + this.tblPopup.offsetWidth  &&
			   y >= this.tblPopup.offsetTop  &&  y <= this.tblPopup.offsetTop + this.tblPopup.offsetHeight;
	}
	
    this.getAbsolutePosition = function()
    {
        var ap = WebForm_GetElementPosition(this.tblMain);
        return {left: ap.x, top: ap.y, scrollLeft: 0, scrollTop: 0};
    }
	
	this.setPopupPosition = function()
	{
        var edtPos = position.getXY(this.tblMain);
        
        if (this.popupDirection.indexOf('Downwards') != -1)
            this.tblPopup.style.top = edtPos[1] + this.tblMain.offsetHeight + 'px';
        else 
        {
            var bodyHeight = document.body.clientHeight;
            this.tblPopup.style.bottom = bodyHeight - edtPos[1] + 'px';
        }
            
        if (this.popupDirection.indexOf('Right') != -1)
            this.tblPopup.style.left = edtPos[0] + 'px';
        else
        {
            var bodyWidth = document.body.clientWidth;
            var divMargin = parseInt(position.getStyle(this.tblPopup, 'marginRight'), 10) || 0;
            this.tblPopup.style.right = bodyWidth - this.tblMain.offsetWidth - edtPos[0] - divMargin + 'px';
        } 
	}
	
	this.ShowPopup = function()
	{
	    if (this.tblPopup.parentNode.tagName != 'BODY')
	    {
	        document.body.insertBefore(this.tblPopup, document.body.firstChild);
	        document.body.insertBefore(this.frmMain, document.body.firstChild);
        }
	        
		this.tblPopup.style.display = '';
		this.frmMain.style.display = '';
		
        this.setPopupPosition();
        this.frmMain.style.width = this.tblPopup.offsetWidth;
		this.frmMain.style.height = this.tblPopup.offsetHeight;
		this.frmMain.style.top = this.tblPopup.offsetTop;
		this.frmMain.style.left = this.tblPopup.offsetLeft;
		this.tblPopup.zIndex = this.frmMain.zIndex - 1000000001;			    
		
	}

	this.HidePopup = function()
	{
		this.tblPopup.style.display = 'none';
		this.frmMain.style.display = 'none';
	}
	
	if (window.attachEvent)
        window.attachEvent('onresize', Function.createDelegate(this, this.HidePopup));
    else
	    window.addEventListener('resize', Function.createDelegate(this, this.HidePopup), false);
	

	this.edtMainRefresh = function()
	{
		if (this.selectedDate != null) 
		{
			this.edtMain.value = this.getDateStringWithMask();
			this.SetCursor(this.edtMain, this.curPos);
			this.SelectCurSymbol();
		}
	}

	this.hover = function(object)
	{
		var hoverIndex = object.className.indexOf('hover');
		if (object.className.indexOf('selected') < 0)
			if (hoverIndex < 0)	object.className += 'hover';
			else  object.className = object.className.substring(0, hoverIndex);
	}
	
	this.turnYear = function(Step)
	{
		this.dispYear += Step;
		this.PopupRefresh(); 
	}
	
	this.turnMonth = function(Step)
	{
		var temp = new Date(this.dispYear, this.dispMonth, 1);
		temp.setMonth(temp.getMonth() + Step);
		this.dispYear = temp.getFullYear();
		this.dispMonth = temp.getMonth();
		this.PopupRefresh(); 
	}
	
	this.turnToDay = function()
	{
		
		this.selectedDate = new Date();
		this.dispYear = new Date().getFullYear();
		this.dispMonth = new Date().getMonth();
		
		this.edtMainRefresh();
		this.HidePopup();
		this.setSection(this.getFirstSection());
		this.setCursorBeginPosition();
		if (this.oldValue != this.edtMain.value)
			    this.valueChanged()
	}
	
	this.PopupRefresh = function()
	{
		var CurDate =  this.selectedDate;
		if (CurDate == null) 
		{
			CurDate = new Date();
			this.dispYear = CurDate.getFullYear();
			this.dispMonth = CurDate.getMonth();
		}
			
		this.tdHeader.innerHTML = this.Months[this.dispMonth]  + ' ' + this.dispYear;
		
		var text = '<table cellspacing="0"  onclick="' + this.name + '.CalTableonClick(event)" id="' + Prefix + '">';
		text += '<tr><th>' + this.weekDays.join('</th><th>') + '</tr>';
		
		text += '<tr>';
		var begOfMonth = new Date(this.dispYear, this.dispMonth, 1);
		for (i = 0; i++ < begOfMonth.getDay(); text += '<td id="' + Prefix + '"></td>');
		
		for (var dateIndex = begOfMonth; this.dispMonth == dateIndex.getMonth(); dateIndex.setDate(dateIndex.getDate() + 1))
		{
			if (dateIndex.getDay() == 0) text += '</tr><tr>';
			text += '<td ';
			text += 'id="' + Prefix + '"';
			text += 'class=' + (this.equalDates(dateIndex, CurDate) ?  '"w76_calendar_selected_day"' : '"w76_calendar_day"');
			text += 'onMouseOut="' + this.name + '.hover(this)"';
			text += 'onMouseOver="' + this.name + '.hover(this)"';
			text += '>';
			text += dateIndex.getDate() + '</td>';
		}	
		this.divMain.innerHTML = text;
		this.tdToday.innerHTML = 'Today: ' + this.DateTimeToStr(new Date(), 'MM/dd/yyyy');
	}
	
	
	/* -- Other methods -- */
	this.buildFullValue = function()
	{
		CurPosition = this.curPos;
		CurDay = this.ConvertToInt(this.getSectionValue('d'));
		MaxDay = this.getMaxValueSection('d');
		if (CurDay > MaxDay) this.setSectionValue('d', MaxDay);
				
		this.curPos = CurPosition;
		this.SetCursor(this.edtMain, this.curPos);
		this.SelectCurSymbol();
	}
		
	this.isSelectAll = function()
	{
	    var curSelection = '';
	    if (document.selection)
		    curSelection = document.selection.createRange().text;
        else		    
		    curSelection = this.edtMain.value.substr(this.edtMain.selectionStart, this.edtMain.selectionEnd - this.edtMain.selectionStart);
		    
		return curSelection.length == this.edtMain.value.length;
	}
		
	this.ConvertToInt = function(Value)
	{
		if (Value.toString().trim() == '') return null;
		else 
		{
			if (isNaN(Value * 1) || Value * 1 != Value) return null;
			else return Value * 1;
		}
	}

	this.isValid = function()
	{
		return (this.StrToDateTimeByMask(this.edtMain.value) != null) ? true: false; 
	}	
	
	this.processNumberKeyDown = function(event)
	{
	    var keyCode = event.keyCode;
	    var selectText = '';
	    if (document.selection)
		    selectText = document.selection.createRange().text;
        else
            selectText = this.edtMain.value.substr(this.edtMain.selectionStart, this.edtMain.selectionEnd - this.edtMain.selectionStart);

		if ((keyCode >= 48 && keyCode <=57) || (keyCode >= 96 && keyCode <= 105))
		{
			if (keyCode >= 96 && keyCode <=105) keyCode = keyCode - 48;
			
			if ( ( ( !this.isValidSectionValue('d') && !this.isValidSectionValue('M') && !this.isValidSectionValue('y') ) &&
			  	   (  this.existsSection('d') && this.existsSection('M') && this.existsSection('y') ) )  ||
			     ( ( !this.isValidSectionValue('H') && !this.isValidSectionValue('m')) &&
			  	   (  this.existsSection('H') && this.existsSection('m') ) )   || 
			  	      this.edtMain.value == '' || selectText.length > 1 
			  	)
			{
				this.selectedDate = new Date();
				this.edtMain.value = this.getDateStringWithMask();
			
				this.lastSection = this.getFirstSection();
				this.setSection(this.getFirstSection());
				this.setSectionValue(this.getFirstSection(), ''.LPad(' ', this.getSectionLength(this.getFirstSection())));
				
				this.setCursorBeginPosition();
				
			}
			
			CurSymbol = this.GetCurSymbol();
			CurSectionValue = this.getSectionValue(this.lastSection).trim();
			this.SetCurSymbol(String.fromCharCode(keyCode));
			
			if (this.getPositionInSection(this.curPos, this.lastSection) == 0 && !this.isValidSectionValue(this.lastSection) && this.getSectionLength(this.lastSection) <= 2)
			{
			      this.setSectionValue(this.lastSection, String.fromCharCode(keyCode).RPad(' ', this.getSectionLength(this.lastSection)));
			      this.curPos++;
			      this.SetCursor(this.edtMain, this.curPos);
    		}
			else
			{
			    if ( CurSectionValue != '' && !this.isValidSectionValue(this.lastSection))
			    {
				    this.SetCursor(this.edtMain, this.curPos);
				    this.SetCurSymbol(CurSymbol);
			    } 
			    else this.curPos = this.GetCursor(this.edtMain);
			   
			 }

			if (this.IsSeparator(this.GetCurMaskSymbol()))
			{
			      this.curPos++;
			      this.onChangeSection();
			}
			if (this.curPos >= this.edtMain.value.length) this.curPos = this.edtMain.value.length - 1;
			
			this.SetCursor(this.edtMain, this.curPos);
			this.SelectCurSymbol();
			if (event.preventDefault) 
			    event.preventDefault();
            else 
                event.returnValue = false;			    
		}
	}
	
	this.processServiceKeyDown = function(event)
	{
		switch (event.keyCode)
		{
			case 9: /*Tab*/ case 32: /*Space*/
			{
				if (!event.shiftKey && this.lastSection == this.getLastSection()  ||  event.shiftKey  &&  this.lastSection == this.getFirstSection()
				|| this.isSelectAll()) 
					if (event.keyCode == 9) return true; else;
				else
					this.setSection(this.getNextSection(!event.shiftKey ? 1 : -1, this.lastSection));
			} break;
			case 8: //Backspace
			{
				if (this.isSelectAll() && this.canBeEmpty) this.edtMain.value = '';
				else
				{
					this.SetCurSymbol(' ');
					this.curPos = this.GetCursor(this.edtMain) - 2;
					if ( this.IsSeparator(this.GetCurMaskSymbol()))  
					{
						this.curPos--;
						this.onChangeSection();
					}
					if (this.curPos < 0) this.curPos = 0;
				}
			} break;

			case 46: //Delete
			{
				if (this.isSelectAll() && this.canBeEmpty) this.edtMain.value = '';
				else
				{
					this.SetCurSymbol(' ');
					this.curPos = this.GetCursor(this.edtMain)  - 1 ;
				}
			} break;

			case 37: //Left
			{
				if (this.isSelectAll())this.setCursorBeginPosition();
				else
				{
					this.curPos--;
					if ( this.IsSeparator(this.GetCurMaskSymbol()) ) 
					{
						this.curPos--;
						this.onChangeSection();
					}
					if (this.curPos < 0) this.curPos = 0;
				}
			} break;
			
			case 39: //Right
			{
				
				if (this.isSelectAll())this.setCursorBeginPosition();
				else
				{
					this.curPos++;
					if ( this.IsSeparator(this.GetCurMaskSymbol()) )  
					{
						this.curPos++;
						this.onChangeSection();
					}
					if (this.curPos >= this.edtMain.value.length) this.curPos = this.edtMain.value.length - 1;
				}
			} break;
			
			case 38: //Up
			{
				if (event.altKey) this.HidePopup();
				else 
				{
				    if (this.isSelectAll()) this.setCursorBeginPosition();
				    else
				    {
					    MaxSectionValue = this.getMaxValueSection(this.lastSection);
					    CurSectionValue = this.ConvertToInt(this.getSectionValue(this.lastSection));
					    CurSectionValue++;
				        if (CurSectionValue >= MaxSectionValue) CurSectionValue = MaxSectionValue;
				        this.setSectionValue(this.lastSection, CurSectionValue.toString().LPad('0', this.getSectionLength(this.lastSection)));
			        }
				}
			} break;
			
			case 40: //Down
			{
				if (event.altKey && this.tblPopup.style.display == 'none') this.tdDownClick();
				else 
				{
				    if (this.isSelectAll())this.setCursorBeginPosition();
				    else
				    {
					    MinSectionValue = this.getMinValueSection(this.lastSection);
					    CurSectionValue = this.ConvertToInt(this.getSectionValue(this.lastSection));
					    CurSectionValue--;
					    if (CurSectionValue <= MinSectionValue) CurSectionValue = MinSectionValue;
					    this.setSectionValue(this.lastSection, CurSectionValue.toString().LPad('0', this.getSectionLength(this.lastSection)));
				    }
				}
			} break;
			case 16: /*Shift*/ case 18: /*alt*/return; 
		}
		this.SetCursor(this.edtMain, this.curPos);
		this.SelectCurSymbol();
		if (event.preventDefault)
		    event.preventDefault();
        else		    
		    event.returnValue = false;
	}
	
	/* Events of Calendar control */
	
	this.btnChangeType_onClick = function()
    {
    1;
       this.setIsExpression(this.getIsExpression() == 'False');
    }
	
	this.edtMainKeyDown = function(event)
	{
		1;
		if (event.keyCode == 27) /*Esc*/
		{
		    this.setUndoValue();
		    event.returnValue = false;
		}
		    
		if ((event.keyCode >= 48 && event.keyCode <=57) || (event.keyCode >= 96 && event.keyCode <=105))
			this.processNumberKeyDown(event);
		else  
		{
		    if (this.edtMain.value != '' || event.altKey)
		    {
			    if (event.altKey || this.isValidSectionValue('d') || this.isValidSectionValue('y') || this.isValidSectionValue('H') || this.isValidSectionValue('m') ) 
			    this.processServiceKeyDown(event);
		    }
		    else return true;
		}
		
		if (this.onkeydown) this.onkeydown(event);
	}
	
	this.valueChanged = function(event)
	{
	    if (this.autoPostBack)
	    {
	        this.doPostBack('ValueChanged');
	        this.oldValue = this.edtMain.value;
	    }
	}
	
	this.onChangeSection = function()
	{
		newValue = this.getBuildSectionValue(this.lastSection);
		this.setSectionValue(this.lastSection, newValue);
		this.lastSection = this.getCurSection();
		this.buildFullValue();
	}

	this.edtMainClick = function(event)
	{
		if (this.isValidSectionValue('d') || this.isValidSectionValue('M') || this.isValidSectionValue('y') || 
			this.isValidSectionValue('H') || this.isValidSectionValue('m') || this.isValidSectionValue('s'))
		{
			
			this.InitCursor();
		}
	}
	
	this.doPostBack = function(eventArgument) 
	{
		if (!this.container)
		{
		    var theform = document.forms[0];
		    theform.__EVENTTARGET.value = this.uniqueId;
		    theform.__EVENTARGUMENT.value = eventArgument;
		    theform.submit();
		}
		else
		    __doWCPostBack(this.container, this.uniqueId, eventArgument)
	}
	
	this.elementBelongThis = function(element)
	{
	    if (element && element.nodeType == 3 && element.parentNode)
	        element = element.parentNode;
        
        var elementId = element && element.id ? element.id : '';
	    return elementId.indexOf(this.Prefix) != -1;
	}
	
	this.edtMainOnBlur = function(event)
	{
		//var activeElement = document.activeElement ?  document.activeElement : event.explicitOriginalTarget;
		var activeElement =  event && event.explicitOriginalTarget ?  event.explicitOriginalTarget : document.activeElement;
		if (!this.elementBelongThis(activeElement) || activeElement  &&  activeElement.id == this.edtMain.id)
		{
			this.HidePopup();
				
			CurSectionValue = this.ConvertToInt(this.getSectionValue(this.lastSection));
			if (this.edtMain.value != '' && (this.isValidSectionValue('d') || this.isValidSectionValue('M') || this.isValidSectionValue('y') || 
				 this.isValidSectionValue('H') || this.isValidSectionValue('m') || this.isValidSectionValue('s')))
				this.setSectionValue(this.lastSection, this.getBuildSectionValue(this.lastSection));
			
			CurDay = this.ConvertToInt(this.getSectionValue('d'));
			MaxDay = this.getMaxValueSection('d');
			if (CurDay != null & CurDay > MaxDay) this.setSectionValue('d', MaxDay);
			this.lastSection = this.getFirstSection();
			
			if (this.onblur) this.onblur(event);
			
			if (this.oldValue != this.edtMain.value)
			    this.valueChanged()
		}
		else if (this.tblPopup.style.display != 'none') 
		   window.setTimeout(Function.createDelegate(this, 
		        function()
		        {
    		        this.edtMain.focus();
                }), 0);
		this.undoValue = this.edtMain.value;
	}
	
	this.edtMainOnFocus = function(event)
	{		
	    if (this.onfocus) this.onfocus(event);
	    this.edtMain.select();
		return true;	
	}
	
	this.tdDownClick = function(event)
	{
		if (this.tblPopup.style.display == 'none' && !this.onlyTime())  
		{
			this.buildFullValue();
			
			this.selectedDate = this.isValid() ? this.StrToDateTimeByMask(this.edtMain.value) : new Date();
			this.dispYear = this.isValid() ? this.selectedDate.getFullYear() : new Date().getFullYear();
			this.dispMonth = this.isValid() ? this.selectedDate.getMonth() : new Date().getMonth();
			
			this.PopupRefresh();	
			this.ShowPopup();
		}
		else this.HidePopup();
		
		this.setSection(this.getFirstSection());
		this.setCursorBeginPosition();
		this.edtMain.focus(); 
	}
	
	this.CalTableonClick = function(event)
	{
		var td = event.srcElement ? event.srcElement : 
		    (event.target && event.target.nodeType == 3 && event.target.parentNode ? event.target.parentNode : event.target);
		var day = 0;
		if (td && td.firstChild && td.firstChild.nodeValue != '') 
		    day = 1 * td.firstChild.nodeValue;
		
		if (day) 
		{
			if (this.selectedDate != null) this.selectedDate = new Date(this.dispYear, this.dispMonth, day, this.selectedDate.getHours(), this.selectedDate.getMinutes(), this.selectedDate.getSeconds());
			else this.selectedDate = new Date(this.dispYear, this.dispMonth, day);
									
			this.edtMainRefresh();
			this.HidePopup();
			this.setSection(this.getFirstSection());
			this.setCursorBeginPosition();
			
			if (this.oldValue != this.edtMain.value)
			    this.valueChanged()
		}
	}
	
    this.focus = function()
    {
        if (this.edtMain.style.display != 'none')
            this.edtMain.focus();
        else
            this.edtExpression.focus();            
    }		
    
    this.setUndoValue = function()
    {
        if (typeof(this.undoValue) == 'string')
        {
          this.edtMain.value = this.undoValue; 	
		  this.selectedDate = new Date();  
		  
        }
        else 
        {
            this.selectedDate = this.undoValue;
            this.edtMainRefresh();
		    this.setSection(this.getFirstSection());

		}
	    this.setCursorBeginPosition();
		this.HidePopup();
    }
    
    this.setVisibility = function(value)
	{
	    this.tblPopup.style.display = 'none';
	    this.tblMain.style.display = value ? '' : 'none';
	}
	
	this.setDisplay = function(value)
	{
	    this.setVisibility(value != 'none');
	}
	
	this.getDisplay = function()
	{
	    return this.tblMain.style.display;
	}
	
	this.setLeft = function(value)
	{
	    this.tblMain.style.left = value;
	}
	
	this.setTop = function(value)
	{
	    this.tblMain.style.top = value;
	}
	
	this.setWidth = function(value)
	{
	    var tdDownWidth = this.tdDown.offsetWidth;
	    this.edtMain.style.width = (this.tblMain.style.width = value) - tdDownWidth;
	}
	
	this.setSelectedDate = function(value)
	{
	    1;
	    this.selectedDate = typeof(value) == 'string' ? this.StrToDateTimeByMask(value) : value;
	    this.dispYear = this.selectedDate != null ? this.selectedDate.getFullYear() : new Date().getFullYear();
	    this.dispMonth = this.selectedDate != null ? this.selectedDate.getMonth() : new Date().getMonth()
	    
	    if (this.selectedDate != null)  this.edtMain.value = this.getDateStringWithMask();
		else this.edtMain.value = '';
	    
	}
	
	this.getSelectedText = function()
	{
	    CurSectionValue = this.ConvertToInt(this.getSectionValue(this.lastSection));
		if (this.edtMain.value != '' && (this.isValidSectionValue('d') || this.isValidSectionValue('M') || this.isValidSectionValue('y') || 
		    this.isValidSectionValue('H') || this.isValidSectionValue('m') || this.isValidSectionValue('s')))
			this.setSectionValue(this.lastSection, this.getBuildSectionValue(this.lastSection));
			
		CurDay = this.ConvertToInt(this.getSectionValue('d'));
		MaxDay = this.getMaxValueSection('d');
		if (CurDay!= null & CurDay > MaxDay) this.setSectionValue('d', MaxDay);
		
		this.lastSection = this.getFirstSection();
		return this.edtMain.value;
    }			
    
    this.getText = function()
    {
      1;
        return this.getIsExpression() == 'True' ? this.edtExpression.value  : this.getSelectedText();
    }
    
    this.setText = function(value)
    {
    1;
        if (this.getIsExpression() == 'True')
            this.edtExpression.value = value;
        else
            this.Text = this.edtMain.value = value;
        
        this.undoValue = value;  //01/02/2007
    }
    
    this.getValue  = function()
    {
        var dt = this.selectedDate;
        if (dt)
            return dt.getFullYear() + '/' + (dt.getMonth()+1) + '/' + dt.getDate() + ' ' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds();
        else
            return '';            
    }
    
    this.setValue = function(value)
    {
        var dt = new Date(value);
        this.selectedDate = dt;
        this.dispYear = this.selectedDate != null ? this.selectedDate.getFullYear() : new Date().getFullYear();
	    this.dispMonth = this.selectedDate != null ? this.selectedDate.getMonth() : new Date().getMonth();
	    
	    this.undoValue = value;
    }
    
    this.getMask = function()
    {
        return this.Mask;
    }
    
    this.setMask = function(value)
    {
        this.Mask = value;
        this.edtMainRefresh();
    }
    
    this.getIsExpression = function()
    {
    1;
        return this.edtIsExpression.value == '1' ? 'True' : 'False';
    }
    
    this.setIsExpression = function(value)
    {
        value = value === true || value === 'True'; 
    
        this.HidePopup();
        this.tdDown.style.display = this.edtMain.style.display = value ? 'none' : '';
        this.edtExpression.style.display = value ? '' : 'none';
       
        this.edtIsExpression.value = value ? '1' : '0';
    }

	if (this.Text != null && this.Text != '') 
	{
		this.edtMain.value = this.Text; 	
		this.selectedDate = new Date();
	}
	else 
	    if (this.selectedDate != null)  this.edtMain.value = this.getDateStringWithMask();
		else this.edtMain.value = '';
		
    this.oldValue = this.edtMain.value;		
}


function Controls76_WCalendar76_Builder(name, obj_id)
{
    this.name = name;
    try { this.obj = eval(obj_id); }
    catch (ex) { this.obj = null; }
    
    this.properties = new Array('IsExpression', 'Display', 'Value', 'Mask', 'Text');
    
    this.get_prop = function(prop_name)
    {
        1;
        if (this.obj != null)
            switch(prop_name)
            {
                default: return eval('if (this.obj.get' + prop_name + ') this.obj.get' + prop_name + '()');
            }
            
        return "";
    }
    
    this.set_prop = function(prop_name, prop_value)
    {
        1;
        if (this.obj != null)
            switch(prop_name)
            {
                default: eval('if (this.obj.set' + prop_name + ') this.obj.set' + prop_name + '(prop_value)');
            }
    }
}
