 /*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	return _isEmpty;
}

function isMatch(formElement, matchVal, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == matchVal) {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	return _isEmpty;
}

/*
	Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
	for (i=0; i < listElement.options.length; i++) {
		if (listElement.options[i].value == listValue)	{
			listElement.selectedIndex = i;
		}
	}	
}

function createCalendar()
{
var day_of_week = new Array('P','O','T','C','P','S','S');
var month_of_year = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

var Calendar = new Date();
var year = Calendar.getFullYear();
var month = Calendar.getMonth();
var today = Calendar.getDate();
var weekday = Calendar.getDay();
var DAYS_OF_WEEK = 7;
var DAYS_OF_MONTH = 31;
var cal;

Calendar.setDate(1);
Calendar.setMonth(month);

/* Day's code */
var TR_start = '<tr>';
var TR_end = '</tr>';
var highlight_start = '<b>';
var highlight_end   = '</b>';
var TD_start = '<td align="center">';
var TD_start_highlight = '<td align="center" class="highlight">';
var TD_start_weekend = '<td align="center" class="weekend">';
var TD_start_weekend_highlight = '<td align="center" class="weekend highlight">';
var TD_end = '</td>';

/* Calendar's code */
cal = '<table border="0" cellspacing="0" cellpadding="0" id="kalendars">';
//cal += '<td colspan="' + DAYS_OF_WEEK + '" align="center"><b>';
//cal += month_of_year[month] + '   ' + year + '</b>' + TD_end + TR_end;
cal += TR_start;

for(index=0; index < DAYS_OF_WEEK; index++)
{
	if(weekday == index+1)
	{
		if(index == 5 || index == 6)
		{
			cal += TD_start_weekend + '<b>' + day_of_week[index] + '</b>' + TD_end;
		}
		else
		{
			cal += TD_start + '<b>' + day_of_week[index] + '</b>' + TD_end;
		}
	}
	else
	{
		if(index == 5 || index == 6)
		{
			cal += TD_start_weekend + day_of_week[index] + TD_end;
		}
		else
		{
			cal += TD_start + day_of_week[index] + TD_end;
		}
	}
}
cal += TD_end + TR_end;
cal += TR_start;

firstMonthDay = Calendar.getDay();
if(firstMonthDay==0){firstMonthDay=7;}
for(index=1; index < firstMonthDay; index++)

cal += TD_start + ' ' + TD_end;

for(index=0; index < DAYS_OF_MONTH; index++)
{
	if( Calendar.getDate() > index )
	{
  		week_day =Calendar.getDay();
  		
  		if(week_day==0){week_day=7}
  
  		if(week_day == 1) cal += TR_start;

  		if(week_day != DAYS_OF_WEEK+1)
  		{
  			var day  = Calendar.getDate();
 			if( today==Calendar.getDate() ) 
			{
				if(week_day == 6 || week_day == 7)
				{
					cal += TD_start_weekend_highlight + '<b>' + day + '</b>' + TD_end;
				}
				else
				{
					cal += TD_start_highlight + '<b>' + day + '</b>' + TD_end;
				}
			}
			else
			{
				if(week_day == 6 || week_day == 7)
				{
					cal += TD_start_weekend + day + TD_end;
				}
				else
				{
					cal += TD_start + day + TD_end;
				}
			}
  		}
  		if(week_day == DAYS_OF_WEEK) cal += TR_end;
	}
  	Calendar.setDate(Calendar.getDate()+1);
}
cal += '</table>';
document.getElementById("kalendars_area").innerHTML=cal;	
}
