function FormatDate(sDate)
{
  if(sDate.length == 2 || sDate.length == 5)
  return sDate + '.';

  return sDate;
}

function ValidateDate(dayInp, monthInp, yearInp)
{
  var ctrlDay = document.getElementById(dayInp);
  var ctrlMonth = document.getElementById(monthInp);
  var ctrlYear = document.getElementById(yearInp);

  var monthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  if(ctrlDay && ctrlMonth && ctrlYear)
  {
    if(ctrlDay.value.length > 0 || ctrlMonth.value.length > 0 || ctrlYear.value.length > 0)
    {
      if(!ValidateDay(dayInp, true) || !ValidateMonth(monthInp, true) || !ValidateYear(yearInp, true))
        return false;

      var month = parseInt(ctrlMonth.value, 10);
      var maxDays = monthDays[month - 1];
      if(month == 2)
      {
        var year = parseInt(ctrlYear.value, 10);
        if((year % 4) == 0)
          maxDays = maxDays + 1;
      }

      var day = parseInt(ctrlDay.value, 10);
      if(day > maxDays)
      {
        HighlightControl(ctrlDay, true, true);
        return false;
      }
    }

    HighlightControl(ctrlDay, false, false);
    HighlightControl(ctrlMonth, false, false);
    HighlightControl(ctrlYear, false, false);
    return true;
  }
}

function ValidateDay(dayInp, checkEmpty)
{
  var ctrlDay = document.getElementById(dayInp); 
  if(ctrlDay.value.length > 0 || checkEmpty)
  {
    var day = parseInt(ctrlDay.value, 10);
    if(isNaN(day) || day < 1 || day > 31)
    {
      HighlightControl(ctrlDay, true, true);
      return false;
    }
  }
  HighlightControl(ctrlDay, false, false);
  return true;
}

function ValidateMonth(monthInp, checkEmpty)
{
  var ctrlMonth = document.getElementById(monthInp);
  if(ctrlMonth.value.length > 0 || checkEmpty)
  {
    var month = parseInt(ctrlMonth.value, 10);
    if(isNaN(month) || month < 1 || month > 12)
    {
      HighlightControl(ctrlMonth, true, true);
      return false;
    }
}

HighlightControl(ctrlMonth, false, false);
return true;
}

function ValidateYear(yearInp, checkEmpty)
{
  var ctrlYear = document.getElementById(yearInp);
  if(ctrlYear.value.length > 0 || checkEmpty)
  {
    var year = parseInt(ctrlYear.value, 10);
    if(isNaN(year) || year < 1900)
    {
      HighlightControl(ctrlYear, true, true);
      return false;
    }
}

HighlightControl(ctrlYear, false, false);
return true;
}

function HighlightControl(ctrl, bHighlight, focus)
{
  if(bHighlight)
  {
    ctrl.className='errorInput';
  }
  else
  {
    ctrl.className='';
  }

  if(focus)
  {
    ctrl.focus();
    ctrl.select();
  }
}

function IsNumericKeyCode(evt)
{
  var charCode = (evt.which) ? evt.which : evt.keyCode;
  if ((charCode >= 48 && charCode <= 57) || (charCode >= 96 && charCode <= 105))
    return true;

  return false;
}

function IsNumericCharCode(evt)
{
  var charCode = (evt.which) ? evt.which : evt.keyCode;
  var res = charCode <= 31 || charCode == 46 || charCode == 37 || charCode == 39 || (charCode >= 48 && charCode <= 57);
  return res;
}

function TextBoxContainOnlyDigits(event)
{
	if (window.event != null)
	{ 
		var k = window.event.keyCode; 
		if ((k < 48 || k > 57) && k != 8) 
			window.event.returnValue = false; 
	}
	else
	{
		var k = event.charCode;
		if ((k < 48 || k > 57) && k != 8) 
			return false; 
	}
}

function setCalendarBlurTimeout(dayInp, monthInp, yearInp, currentInp, focusInp, evt)
{
  var expr = '';
  document.getElementById(focusInp).value = '';
  if(dayInp == currentInp)
  {
  expr = 'if(document.getElementById(\'' + focusInp + '\').value != \'' + monthInp + '\' && document.getElementById(\'' + focusInp + '\').value != \'' + yearInp + '\') ValidateDate(\'' + dayInp + '\', \'' + monthInp + '\', \'' + yearInp + '\'); else if(document.getElementById(\'' + dayInp + '\').value.length != 2 || ' + (!IsNumericKeyCode(evt)? 'true': 'false') + ') ValidateDay(\'' + dayInp + '\', false);';
  }
  else if(monthInp == currentInp)
  {
  expr = 'if(document.getElementById(\'' + focusInp + '\').value != \'' + dayInp + '\' && document.getElementById(\'' + focusInp + '\').value != \'' + yearInp + '\') ValidateDate(\'' + dayInp + '\', \'' + monthInp + '\', \'' + yearInp + '\'); else if(document.getElementById(\'' + monthInp + '\').value.length != 2 || ' + (!IsNumericKeyCode(evt)? 'true': 'false') + ') ValidateMonth(\'' + monthInp + '\', false);';
  }
  else if(yearInp == currentInp)
  {
  expr = 'if(document.getElementById(\'' + focusInp + '\').value != \'' + dayInp + '\' && document.getElementById(\'' + focusInp + '\').value != \'' + monthInp + '\') ValidateDate(\'' + dayInp + '\', \'' + monthInp + '\', \'' + yearInp + '\'); else ValidateYear(\'' + yearInp + '\', false);';
  }
  window.setTimeout(expr, 50);
}

function Trace(traceId, source, txt)
{
    var ctrl = document.getElementById(traceId);
    if(!ctrl)
    {
        ctrl = document.createElement("DIV");
        ctrl.style.display='block';
        ctrl.id = traceId;
        document.body.appendChild(ctrl);
    }
    
    ctrl.innerHTML += source + ': ' + txt + '<br/>';    
}

function getClientWidth()
{
    return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function getClientHeight()
{
    return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}
function getBodyScrollTop()
{
	return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

function getBodyScrollLeft()
{
	return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
}

function showPopup(popupName)
{
    $('#' + popupName).css('top', parseInt(getClientHeight()/2)+getBodyScrollTop()-160); 
    $('#' + popupName).css('left',parseInt(getClientWidth()/2)+getBodyScrollLeft()-320); 
    $('#' + popupName).show(1000); 
}

function OpenInSameWindow(str)
{
    ret=open( str, 'test', 'toolbar=yes, directories=no, menubar=yes, resizable=yes, scrollbars=yes, width=800, height=500');
};