////////////////////////////////////////////////////////////////////////////////
// CALENDAR CONTROL
////////////////////////////////////////////////////////////////////////////////


// test for IE or DOM
isIE = (document.all ? true : false);
isDOM = (document.getElementById ? true : false);

// get the true offset of anything on NS4, IE4/5 & NS6
function getAbsX(elt) 
{ return (elt.x) ? elt.x : getAbsPos(elt,"Left"); }

function getAbsY(elt) 
{ return (elt.y) ? elt.y : getAbsPos(elt,"Top"); }

function getAbsPos(elt, which) {
 iPos = 0;
 while (elt != null) {
  iPos += elt["offset" + which];
  elt = elt.offsetParent;
 }
 return iPos;
}

var editDay;
var editMonth;
var editYear;
var editControl;
var tableBorder;
var tableCellpadding;
var tableCellspacing;

var isFocusIn = false;

//the width & height of the calendar popup
var layerWidth;
var layerHeight;

//remember the selects that were hidden
var hiddenSelects = new Array();


//***********************
//render the calendar
//***********************
function createCalendar( border, cellpadding, cellspacing, resourcePath )
{
	tableBorder = border;
	tableCellpadding = cellpadding;
	tableCellspacing = cellspacing;
	
	//first write 
	
	document.write( "<div id='pupCalendar' class='calendar_layer' onFocus='isFocusIn=true;' onBlur='isFocusIn=false;'>\n" );
	document.write( "&nbsp;<a href='javascript:downYearValue();' onFocus='isFocusIn=true;' onBlur='isFocusIn=false;'><img src='" + resourcePath + "Prev.gif' alt='&lt;' border='0'/></a>\n" );
	document.write( "<select name='months' id='months'  onchange='setMonthDays();' onFocus='isFocusIn=true;' onBlur='isFocusIn=false;' class='calendar_dropmonths'>\n" );
	var monthCount = calendarMonths.length;
	for( i = 0; i < monthCount; i++ )
	{
		document.write( "<option>" + calendarMonths[i] + "</option>\n" );
	}
	document.write( "</select>" );
	document.write( "<input type='text' name='year' id='year' value='2004' size='4' maxlength='4' onkeydown='surpressEnter()' onKeyUp='if(year.value.length==4)updateYear();' onChange='updateYear()' class='calendar_manualyear' onFocus='isFocusIn=true;'>\n" );
	document.write( "<a href='javascript:upYearValue();' onFocus='isFocusIn=true;' onBlur='isFocusIn=false;'><img src='" + resourcePath + "Next.gif' alt='&gt;' border='0'/></a>\n" );
	document.write( "<a href='javascript:forceHidePupCalendar();' onFocus='isFocusIn=true;' onBlur='isFocusIn=false;'><img src='" + resourcePath + "Close.gif' alt='X' border='0'/></a>&nbsp;\n" );
	document.write( "<br />\n" );
	document.write( "<div id='monthDays'></div>\n" );

	document.write( "</div>\n" );
	
	//point to the calendar element
	var cal;	
	
	if(isIE)
		cal = document.all["pupCalendar"];
	else
		cal = document.getElementById("pupCalendar");
	
	//set layer width for future use
	layerWidth = cal.clientWidth;
	
	//NS bugfix
	if(layerWidth == 0)
		layerWidth = 180;
}


//***********************
//render calendar's month/day table
//***********************
function setMonthDays()
{
	//point to the calendar element
	var cal;	
	
	if(isIE)
	{
		cal = document.all["pupCalendar"];
		months = document.all["months"];
		year = document.all["year"];
	}
	else
	{
		cal = document.getElementById("pupCalendar");
		months = document.getElementById("months");
		year = document.getElementById("year");
		monthDays = document.getElementById("monthDays");
	}

	var tempDate = new Date();
	month = months.selectedIndex;
	monthDate = 1;
	currentYear = year.value;
	firstMonthDate = new Date( eval( currentYear ), month, monthDate );
	firstDay = firstMonthDate.getDay();
	if( firstDay == 0 )
	{
		firstDay = 7;
	}

	var borderDefs = " ";
	if( tableBorder != -1 )
	{
		borderDefs = "border=" + tableBorder + " cellpadding=" + tableCellpadding + " cellspacing=" + tableCellspacing;
	}
	
	monthTable = "<table ";
	monthTable += borderDefs;
	monthTable += " width='" + layerWidth + "' style='cursor: default;' class='calendar_table' onFocus='isFocusIn=true;' onBlur='isFocusIn=false;'>";
	monthTable += "<tr class='calendar_firstrow'><td onFocus='isFocusIn=true;' onBlur='isFocusIn=false;'>" + calendarDays[0] + "</td><td onFocus='isFocusIn=true;' onBlur='isFocusIn=false;'>" + calendarDays[1];
	monthTable += "</td><td onFocus='isFocusIn=true;' onBlur='isFocusIn=false;'>" + calendarDays[2] + "</td><td onFocus='isFocusIn=true;' onBlur='isFocusIn=false;'>" + calendarDays[3] + "</td><td onFocus='isFocusIn=true;' onBlur='isFocusIn=false;'>" + calendarDays[4] + "</td>";
	monthTable += "<td class='calendar_weekendfirstrow' onFocus='isFocusIn=true;' onBlur='isFocusIn=false;'>" + calendarDays[5];
	monthTable += "</td><td class='calendar_weekendfirstrow' onFocus='isFocusIn=true;' onBlur='isFocusIn=false;'>" + calendarDays[6] + "</td></tr><tr>";

	if( firstDay > 1 )
	{
		monthTable += "<td colspan='" + (firstDay - 1) + "' onFocus='isFocusIn=true;' onBlur='isFocusIn=false;'></td>";
	}
	/*
	for( i = 0; i < firstDay - 1; i++ )
	{
		monthTable += "<td ></td>";
	}
	*/
	do {
		if( firstDay > 7 )
		{
			monthTable += "</tr><tr>";
			firstDay = 1;
		}
		firstDay++;
		
		var tdStyle = "calendar_item";
		
		// weekdays
		if( firstDay == 7 || firstDay == 8 )
		{
			tdStyle = "calendar_weekend";
		}
		
		// current date
		if( monthDate == tempDate.getDate() && months.selectedIndex == tempDate.getMonth() && year.value == tempDate.getFullYear() )
		{
			tdStyle = "calendar_currentdate";
		}
		
		// user selected date
		if( editDay != null )
		{
			if( monthDate == editDay && months.selectedIndex == (editMonth - 1) && year.value == editYear )
			{
				tdStyle = "calendar_userdate";
			}
		}
		
		monthTable += "<td align='right' class='" + tdStyle + "' ";
		monthTable += "onClick='updateEditControl(" + monthDate + ", " + (months.selectedIndex + 1) + ", " + year.value + "); isFocusIn=false;' ";
		monthTable += "onmouseover=\"className='" + tdStyle + "selected'\" ";
		monthTable += "onmouseout=\"className='" + tdStyle + "'\"  onFocus='isFocusIn=true;' onBlur='isFocusIn=false;'>";
		monthTable += monthDate + "</td>\n";
		monthDate++;
	} while( month == new Date( eval( year.value ), month, monthDate ).getMonth() );

	monthTable += "</tr><tr><td colspan='4' align='center' class='calendar_todaycancel' ";
	monthTable += "onClick='updateEditControl(" + tempDate.getDate() + ", " + (tempDate.getMonth() + 1) + ", " + tempDate.getFullYear() + "); isFocusIn=false;' ";
	monthTable += "onmouseover=\"className='calendar_todaycancelselected'\" ";
	monthTable += "onmouseout=\"className='calendar_todaycancel'\" onFocus='isFocusIn=true;' onBlur='isFocusIn=false;' >" + calendarToday + "</td>";

	monthTable += "<td colspan='3' align='center' class='calendar_todaycancel' onClick=\"ClearDate('" + editControl.id + "');\"";
	monthTable += "onClick='isFocusIn=false;' ";
	monthTable += "onmouseover=\"className='calendar_todaycancelselected'\" ";
	monthTable += "onmouseout=\"className='calendar_todaycancel'\" onFocus='isFocusIn=true;' onBlur='isFocusIn=false;'>" + calendarCancel + "</td></tr></table>";
	
	monthDays.innerHTML = monthTable;
	
	//set layerHeight for future use
	layerHeight = cal.clientHeight;
	
	//NS bugfix
	if(layerHeight == 0)
		layerHeight = 160;
}


//*******************
//add the layer containing the calendar to the page
//*******************
function createCalendarLayer()
{
	createCalendar( -1, 0, 0 );
}

//*******************
//hide pup calendar if focus is lost
//*******************
function hideWithTimeout()
{
	if( isIE )
		setInterval( hidePupCalendar, 400 );
}


//************* 
// position calendar to given x, y coordinates
//************* 
function showCalendarAbsolute( x, y, editCtrl )
{

	isFocusIn = false;
	hideWithTimeout();
	
	//point to the calendar element
	var cal;	
		
	if(isIE)
	{
		cal = document.all["pupCalendar"];
		year = document.all["year"];
		months = document.all["months"];
	}
	else
	{
		cal = document.getElementById("pupCalendar");
		year = document.getElementById("year");
		months = document.getElementById("months");
	}

	editControl = editCtrl;
	updateUserSelectedDate();
	
	var curDate;
	if( editDay != null )
	{
		curDate = "" + editDay + "/" + editMonth + "/" + editYear;
	}
	else
	{	
		curDate = new Date();
		curDate = "" + curDate.getDate() + "/" + (curDate.getMonth() + 1) + "/" + curDate.getFullYear();
	}
	
	cal.style.left = x;
	cal.style.top = y;
	curDate = curDate.split('/');
	year.value = curDate[2];
	months.selectedIndex = eval( curDate[1] ) - 1;
	setMonthDays();
	cal.style.visibility = "visible";
	
	//hide all select boxes
	hideSelectsBelowLayer(x, y);
 
}


//************* 
// display the calendar
//************* 
function showPupCalendar( editCtrl )
{
	if( editCtrl )
		editCtrl.focus();
	
	scrollH = document.body.scrollHeight;
	calX = getAbsX(editCtrl);
	calY = getAbsY(editCtrl) + editCtrl.offsetHeight;
	resHeight = 180;
	
	//find out whether there is enough space to display the calendar below the textbox
	//if( (scrollH > calY + resHeight) )
		showCalendarAbsolute( calX, calY, editCtrl);
	//else
		//showCalendarAbsolute( calX, calY - editCtrl.offsetHeight - resHeight, editCtrl);
		
	//showCalendarAbsolute( document.body.scrollLeft + evt.x, document.body.scrollTop + evt.y, editCtrl );
}


//************* 
// get the actual date
//************* 
function getToday()
{
	today = new Date();
	return today.getDate() + '/' + (today.getMonth() + 1) + '/' + today.getFullYear();
}


//************* 
// hide calendar when focus is lost
//************* 
function hidePupCalendar()
{
		
	//point to the calendar element
	var cal;	
	
	if(isIE)
		cal = document.all["pupCalendar"];
	else
		cal = document.getElementById("pupCalendar");	
		
	if( isFocusIn )
		return;
	
	if( isIE )
		if( document.activeElement == editControl )
			return;
	
	if( cal )
		cal.style.visibility = "hidden";

	editControl = null;
	
	//show all select boxes
	showHiddenSelects();
	
}

//************* 
// hide calendar (force!)
//************* 
function forceHidePupCalendar()
{
	//point to the calendar element
	var cal;
	
	if(isIE)
		cal = document.all["pupCalendar"];
	else
		cal = document.getElementById("pupCalendar");

	if( cal )
		cal.style.visibility = "hidden";
		
	isFocusIn = false;
	editControl = null;
	
	//show all select boxes
	showHiddenSelects();

}


//************* 
// preset the textbox
//************* 
function updateEditControl( day, month, year )
{
	if( editControl == null )
		return;
				
	day = "" + day;
	month = "" + month;
	year = "" + year;
	
	var dIndex = 0;
	var mIndex = 0;
	var yIndex = 0;
	var result = "";
	var index = 0;
	
	// count date template day letters "d"
	var count = baseDateTemplate.length;
	for( i = 0; i < count; i++ )
	{
		switch( baseDateTemplate.charAt( i ) )
		{
		case "D":
			dIndex++;
			break;
		case "M":
			mIndex++;
			break;
		case "Y":
			yIndex++;
			break;
		}
	}
	
	while( day.length < dIndex )
		day = "0" + day;
	while( month.length < mIndex )
		month = "0" + month;
	while( year.length < yIndex )
		year = "0" + year;
		
	while( day.length > dIndex )
		day = day.substring( 1, day.length );
	while( month.length > mIndex )
		month = month.substring( 1, month.length );
	while( year.length > yIndex )
		year = year.substring( 1, year.length );
	
	dIndex = 0;
	mIndex = 0;
	yIndex = 0;	
	for( i = 0; i < count; i++ )
	{
		switch( baseDateTemplate.charAt( i ) )
		{
		case "D":
			result += day.charAt( dIndex++ );
			break;
		case "M":
			result += month.charAt( mIndex++ );
			break;
		case "Y":
			result += year.charAt( yIndex++ );
			break;
		default:
			result += baseDateTemplate.charAt( i );
		}
	}
	
	editControl.value = result;
	
	//07.07.2004_Boyan
	if(editControl.onchange)
		editControl.onchange();

	forceHidePupCalendar();
}


//************* 
// set users selection to the textbox
//************* 
function updateUserSelectedDate()
{
	if( editControl == null )
	{
		editDay = null;
		editMonth = null;
		editYear = null;
		return;
	}
	if( editControl.value.length != baseDateTemplate.length )
	{
		editDay = null;
		editMonth = null;
		editYear = null;
		return;
	}
	
	editDay = "";
	editMonth = "";
	editYear = "";
	
	var templateLen = baseDateTemplate.length;
	for( i = 0; i < templateLen; i++ )
	{
		var letter = baseDateTemplate.charAt( i );
		var editLetter = editControl.value.charAt( i );		
		switch( letter )
		{
		case "D":
			editDay += editLetter;
			break;
		case "M":
			editMonth += editLetter;
			break;
		case "Y":
			editYear += editLetter;
			break;
		}
	}
	
	while( editDay.charAt( 0 ) == "0" )
		editDay = editDay.substring( 1, editDay.length );
	while( editMonth.charAt( 0 ) == "0" )
		editMonth = editMonth.substring( 1, editMonth.length );
	while( editYear.charAt( 0 ) == "0" )
		editYear = editYear.substring( 1, editYear.length );
	
	if( editYear.length < 4 )
	{
		editYear = 2000 + parseInt( editYear );
	}
	
	//document.write( "" + editDay + "/" + editMonth + "/" + editYear );	
}


//************* 
// get absolute left of some control
//************* 
function getAbsoluteLeft( someControl )
{
	var result = someControl.offsetLeft;
	var parent = someControl.offsetParent;
	while( parent != document.body )
	{
		result += parent.offsetLeft;
		if( parent.tagName == "TABLE" )
			result++;
		parent = parent.offsetParent;
	}
	return result;
}


//************* 
// get absolute top of some control
//************* 
function getAbsoluteTop( someControl )
{
	var result = someControl.offsetTop;
	var parent = someControl.offsetParent;
	while( parent != document.body )
	{
		result += parent.offsetTop;
		if( parent.tagName == "TABLE" )
			result++;
		parent = parent.offsetParent;
	}
	return result;
}


//************* 
// update the year when changed by the user
//************* 
function updateYear()
{
	if( year.value.length == 4 || isNaN( year.value ) )
	{
		checkYearValue();
	}
	else
	{
		var intYear = parseInt( year.value );
		intYear += 2000;
		year.value = intYear;
		checkYearValue();
	}
}


//************* 
// get the current year
//************* 
function getCurrentYear()
{
	return (new Date().getFullYear());
}


//************* 
// move one year up (next-Button)
//************* 
function upYearValue()
{
	newYear = eval( year.value );
	if(newYear < 9999)
	{
		year.value = ++newYear;
		setMonthDays();
	}
}


//************* 
// move one year down (prev-Button)
//************* 
function downYearValue()
{
	newYear = eval( year.value );
	if( newYear > 1000 )
	{
		year.value = --newYear;
		setMonthDays();
	}
}


//************* 
// check the year value and set months
//************* 
function checkYearValue()
{
	if( isNaN( year.value ) )
	{
		year.value = getCurrentYear();
	}
	
	newYear = eval( year.value );
	if( (newYear < 1000) || (newYear > 9999) )
	{
		year.value = getCurrentYear();		
	}
	setMonthDays();
}

//************* 
// hide selects below the layer - otherwise they would show through in IE
//************* 
function hideSelectsBelowLayer(calX, calY) {
		
		if(isIE)
		{
			//first clear the hidden selects array to have a clean one
			hiddenSelects.length = 0;
			
			for(i=0; i<document.forms[0].elements.length; i++)
			{
				if(
					(document.forms[0].elements[i].type == "select-one" || 
					document.forms[0].elements[i].type == "select-multiple") &&
					document.forms[0].elements[i].id != "months")
				{
					
					//find out if this select is positioned below the calendar
					ddlX = getAbsX(document.forms[0].elements[i]);
					ddlY = getAbsY(document.forms[0].elements[i]);
					ddlH = document.forms[0].elements[i].offsetHeight;
					ddlW = document.forms[0].elements[i].offsetWidth;
			
					/*
					test = 	"X-left: " + (ddlX + 200) + " >= " + calX + " ? " + ((ddlX + 200 >= calX) ? "true" : "false") + "\n";
					test +=	"X-right: " + ddlX + " < " + (calX + layerWidth) + " ? " + ((ddlX < calX + layerWidth) ? "true" : "false") + "\n";
					test +=	"Y-top: " + (ddlY) + " >= " + (calY - 20) + " ? " + ((ddlY >= calY - 20) ? "true" : "false") + "\n";
					test += "Y-bottom: " + ddlY + " < " + (calY + layerHeight) + " ? " + ((ddlY < calY + layerHeight) ? "true" : "false");
					alert("" + test);
					*/
					
				if(
					//the ddl's x is 200 px left of or in the cal but not more right than cals x + layerWidth
					//the ddl's y is 20 px over or in the cal but no more then cals y + layerHeight 
					((ddlX + ddlW >= calX) && (ddlX < calX + layerWidth)) && 
					((ddlY >= calY - ddlH) && (ddlY < calY + layerHeight))
					)
					{
						document.forms[0].elements[i].style.visibility = "hidden";
						
						//remember the hiddenFieldsName
						hiddenSelects.push(document.forms[0].elements[i].id);
					}
			}
		}
	}
}

//************* 
// shows selects that were previously hidden because they were beneath the layer
//************* 
function showHiddenSelects()
{
	for(i = 0; i < hiddenSelects.length; i++)
		var elt = document.forms[0].elements[hiddenSelects[i]].style.visibility = "visible";
	
	//clear hiddenSelects array
	hiddenSelects.length = 0;
}




////////////////////////////////////////////////////////////////////////////////
// DATE VALIDATOR CONTROL
////////////////////////////////////////////////////////////////////////////////

//var baseDateTemplate = typeof( dateFormat ) != "undefined" ? new String( dateFormat ) : new String( "TT.MM.JJJJ" );
//var baseErrorMessage = typeof( dateErrorMessage ) != "undefined" ? dateErrorMessage + ": " : "Ungültiges Datum: ";
var baseDateTemplate = dateFormat;
var baseErrorMessage = dateErrorMessage + ": ";
var templateLetter = "-"; //TODO
var templateString = createTemplateString();

//**********************
//prepare the template vor validation
//**********************
function createTemplateString()
{
	var result = "";
	var len = baseDateTemplate.length;
	var i;
	
	for( i = 0; i < len; i++ )
	{
		switch( baseDateTemplate.charAt( i ) )
		{
		case "D":
		case "M":
		case "Y":
			result += templateLetter;
			break;
		default:
			result += baseDateTemplate.charAt( i );
		}
	}
	return result;
}



//**********************
//validate the entered Date
//**********************
function validateDate( editDate )
{
	// special patch:
	// if full year (JJJJ) and it is at the end of the string:
	// if user has entered only two digits -> add 2000
	if( (baseDateTemplate.indexOf("YYYY") != -1) && (baseDateTemplate.charAt( baseDateTemplate.length - 1 ) == "Y") )
	{
		if( baseDateTemplate.length - editDate.value.length == 2 )
		{
			var yearChars = editDate.value.substring( editDate.value.length - 2, editDate.value.length );
			var curChars = "" + getCurrentYear();
			var cur = (curChars * 1) - 1994;

			//could it be one of the next 6 years?
			if( (yearChars * 1) > cur )
				yearChars = (1900 + (yearChars * 1));
			else
				yearChars = (2000 + (yearChars * 1));
				
			editDate.value = editDate.value.substring( 0, editDate.value.length - 2 ) + yearChars;
		}
	}
	
	var monthLengths = new Array(12)
 		monthLengths[0] = 31;
 		monthLengths[1] = 29;
 		monthLengths[2] = 31;
 		monthLengths[3] = 30;
 		monthLengths[4] = 31;
 		monthLengths[5] = 30;
 		monthLengths[6] = 31;
 		monthLengths[7] = 31;
 		monthLengths[8] = 30;
 		monthLengths[9] = 31;
 		monthLengths[10] = 30;
 		monthLengths[11] = 31;
	

	// wrap the value of the edit box in a String
	var value = new String( editDate.value );
	var valueLen = value.length;
	if( valueLen == 0 )
		return;
	
	// check if value consists only of spaces
	// if so, value = ""; return
	var spacesOnly = true;
	for( i = 0; i < valueLen; i++ )
	{
		if( value.charAt( i ) != " " )
		{
			spacesOnly = false;
			break;
		}
	}
	if( spacesOnly )
	{
		editDate.value = "";
		return;
	}
	
	// Trim value string first (Example: "  10.12.2002    " must become "10.12.2002")
	var beginIndex = 0;
	var endIndex = valueLen - 1;
	while( value.charAt( beginIndex ) == " " )
		beginIndex++;
	while( value.charAt( endIndex ) == " " )
		endIndex--;
	value = value.substring( beginIndex, endIndex + 1 );
	valueLen = value.length;

	editDate.value = value;
	
	if( valueLen != baseDateTemplate.length )
	{
		dateErrorAlert( errorExactFormat );
		onErrorReturn( editDate );
		return;
	}
	
	// day, month, year
	var day = 0;
	var month = 0;
	var year = 0;
	
	// begin parse
	var current = 0;
	while( current < valueLen )
	{
		var templateLetter = baseDateTemplate.charAt( current );
		// alert( "Template letter = " + templateLetter + " Value letter = " + value.charAt( current ) );
		if( templateLetter == "D" )			// day
		{
			var digit = parseInt( value.charAt( current ), 10 );
			if( isNaN( digit ) )
			{
				dateErrorAlert( errorIncorrectDay );
				onErrorReturn( editDate );
				return;
			}
			day *= 10;
			day += digit;
		}
		else if( templateLetter == "M" )	// month
		{
			var digit = parseInt( value.charAt( current ), 10 );
			if( isNaN(digit) )
			{
				dateErrorAlert( errorIncorrectMonth );
				onErrorReturn( editDate );
				return;
			}
			month *= 10;
			month += digit;
		}
		else if( templateLetter == "Y" )	// year
		{
			var digit = parseInt( value.charAt( current ), 10 );
			if( isNaN(digit) )
			{
				dateErrorAlert( errorIncorrectYear );
				onErrorReturn( editDate );
				return;
			}
			year *= 10;
			year += digit;
		}
		else
		{
			if( templateLetter != value.charAt( current ) )
			{
				dateErrorAlert( errorIncorrectDelimiter );
				onErrorReturn( editDate );
				return;
			}
		}
		current++;
	}
	
	// parse is successful
	// check if values have correct ranges
	// year is not checked (it can't be)
	if( month < 1 || month > 12 )
	{
		dateErrorAlert( errorMonthValue );
		onErrorReturn( editDate );
		return;
	}
	
	if( day < 1 || day > monthLengths[month - 1] )
	{
		dateErrorAlert( errorDayValue + monthLengths[month - 1]);
		onErrorReturn( editDate );
		return;
	}
	
	// If month = February and day = 29 check for leap year
	if( month == 2 && day == 29 )
	{
		if( year < 100 ) year += 2000;
		// check century year
		if( (year % 100) == 0 )
		{
			if( (year % 400) != 0 )
			{
				dateErrorAlert( errorLeapYear );
				onErrorReturn( editDate );
				return;
			}
		}
		else if( (year % 4) != 0 )
		{
			dateErrorAlert( errorLeapYear );
			onErrorReturn( editDate );
			return;
		}
	}
	
	// everything is OK - return
	//alert( "Correct" );
	
}

//**********************
//put out error message
//**********************
function dateErrorAlert( msg )
{
	alert( baseErrorMessage + msg + errorFormat + baseDateTemplate );
}

//**********************
//init control again if error occured
//**********************
function onErrorReturn( editDate )
{
	editDate.value = "";
	editDate.focus();
	return;
}



//**********************
//clear control
//**********************
function ClearDate( editDate )
{
	if(isIE)
		editCtrl = document.all[editDate];
	else
		editCtrl = document.getElementById(editDate);
	
	editCtrl.value = "";
	forceHidePupCalendar();
	return;
}



//**********************
//validate the date format while typing
//**********************
function validateCurrent( editDate )
{
	var keyCode = getEventKeyCode();
	//alert(keyCode);
	
	if( keyCode == 8 || keyCode == 37 ) // BACKSPACE
	{
		return;
	}

	var value = new String( editDate.value );
	var valueLen = value.length;
	var templateLen = templateString.length;
	
	//more characters typed than allowed?
	if( valueLen > templateLen )
	{
		editDate.value = value.substring( 0, templateLen );
		return;
	}
	if( valueLen == templateLen )
	{
		return;
	}
	
	var templateChar = templateString.charAt( valueLen - 1 );
	if( templateChar != templateLetter )
	{
		editDate.value = editDate.value.substring( 0, valueLen - 1 ) + templateChar;
		return;
	}
	
	templateChar = templateString.charAt( valueLen );
	if( templateChar != templateLetter )
	{
		editDate.value = value + templateChar;
		return;
	}
}


//**********************
//check if the char typed is allowed
//**********************
function checkSpecialKey( editDate )
{	
	//special patch for pressing of "."
	if(getEventKeyCode() == 190) //.
	{
		var val = new String( editDate.value );
		var valLen = val.length;
		
		//when just 1 digit for day entered
		if(valLen == 1 && (val * 1) < 10)
		{
			val = "0" + val;
			editDate.value = val;
		}
		
		//when just 1 digit for month entered
		if(valLen == 4)
		{	
			valMon = val.substring(3, 4) * 1;
			
			if(valMon < 10)
			{
				newval = val.substring(0,3) + "0" + valMon;
				editDate.value = newval;
			}
		}
		
	}

	if(getEventKeyCode() == 37 )
	{
		//event.returnValue = false;
		setEventKeyCode(8);
		//event.keyCode = 8;
		return;
	}
	
	//on enter - hide the calendar
	if(getEventKeyCode() == 13)
	{
		if (isIE)
		{
			event.cancelBubble = true;
			event.returnValue = false;
		}
		validateDate(editDate);	
		forceHidePupCalendar();	
	}
	
	var ekk = getEventKeyCode();
	if( ekk == 39 || ekk == 9 || ekk == 13 || ekk == 8) // right arrow, tab, enter, backspace
	{
		return;
	}
	
	//numeric block of keyboard
	if( ekk >= 96 && ekk <= 105 ) // "0" thru "9" on numeric block
	{
		return;
	}
	
	//everything else but the regular numbers
	if( ekk < 48 || ekk > 57 ) // "0" thru "9"
	{
		if (isIE)
			event.returnValue = false;
		return;
	}

	if( editDate.value.length >= templateString.length && ekk >= 32 )
	{
		if (isIE)
			event.returnValue = false;
	}	
}


//**********************
//clear the textbox
//**********************
function unselect( editDate )
{
	editDate.value = editDate.value;
	if (isIE)
		event.returnValue = false;
}

function surpressEnter()
{
	//on enter in the year box - do nothing
	if (isIE)
		if(event.keyCode == 13)
		{
			event.cancelBubble = true;
			event.returnValue = false;	
		}
}