var RE_NUM = /^\-?\d+$/;
var NUM_CENTYEAR = 30;

function clickDay(_field, _date, _time)
{
  if(!prs_date (_date)) return false;
  if(!prs_time (_time)) return false;
  _field.value = _date;
  _field.value += ' ';
  _field.value += _time;
  hideCalendar();
}
function prs_date (str_date) 
{
	var arr_date = str_date.split('-');

	if (arr_date.length != 3) return cal_error ("Nieprawidłowy format daty: '" + str_date + "'.\nAkceptowany dd-mm-yyyy.");
	if (!arr_date[0]) return cal_error ("Nieprawidłowy format daty: '" + str_date + "'.\nNie podano roku.");
	if (!RE_NUM.exec(arr_date[0])) return cal_error ("Nieprawidłowy rok: '" + arr_date[0] + "'.\nDozwolone tylko liczby całkowite.");
	if (!arr_date[1]) return cal_error ("Nieprawidłowy format daty: '" + str_date + "'.\nNie podano miesiąca.");
	if (!RE_NUM.exec(arr_date[1])) return cal_error ("Nieprawidłowy miesiąc: '" + arr_date[1] + "'.\nDozwolone tylko liczby całkowite.");
	if (!arr_date[2]) return cal_error ("Nieprawidłowy format daty: '" + str_date + "'.\nNie podano dnia miesiąca.");
	if (!RE_NUM.exec(arr_date[2])) return cal_error ("Nieprawidłowy dzień miesiąca: '" + arr_date[1] + "'.\nDozwolone tylko liczby całkowite.");

	var dt_date = new Date();
	dt_date.setDate(1);

	if (arr_date[1] < 1 || arr_date[1] > 12) return cal_error ("Nieprawidłowy miesiąc: '" + arr_date[1] + "'.\nDozwolony zakres 01-12.");
	dt_date.setMonth(arr_date[1]-1);
	 
	if (arr_date[0] < 100) arr_date[0] = Number(arr_date[0]) + (arr_date[0] < NUM_CENTYEAR ? 2000 : 1900);
	dt_date.setFullYear(arr_date[0]);

	var dt_numdays = new Date(arr_date[0], arr_date[1], 0);
	dt_date.setDate(arr_date[2]);
	if (dt_date.getMonth() != (arr_date[1]-1)) return cal_error ("Nieprawidłowy dzień miesiąca: '" + arr_date[2] + "'.\nDozwolony zakres 01-"+dt_numdays.getDate()+".");

	return true;
}
function prs_time (str_time) 
{
  var dt_date = new Date;
	var arr_time = String(str_time ? str_time : '').split(':');

  if (RE_NUM.exec(arr_time[0]))
		if (arr_time[0] < 24) dt_date.setHours(arr_time[0]);
		else return cal_error ("Nieprawidłowa godzina: '" + arr_time[0] + "'.\nDozwolony zakres 00-23.");
	else return cal_error ("Nieprawidłowa godzina: '" + arr_time[0] + "'.\nDozwolony zakres 00-23.");
	
	if (RE_NUM.exec(arr_time[1]))
		if (arr_time[1] < 60) dt_date.setMinutes(arr_time[1]);
		else return cal_error ("Nieprawidłowa minuta: '" + arr_time[1] + "'.\nDozwolony zakres 00-59.");
	else return cal_error ("Nieprawidłowa minuta: '" + arr_time[1] + "'.\nDozwolony zakres 00-59.");

	return true;
}
function cal_error (str_message) 
{
	alert (str_message);
	return false;
}
function hideCalendar()
{
  calDiv = fetch_obj('calendar');
  calDiv.style.display = 'none';
}
function get_calendar(field_name, get_file)
{
  field = fetch_obj(field_name);
  field_pos = getAnchorPosition(field_name)
  calDiv = fetch_obj('calendar');
  calDiv.style.top = field_pos.y + field.offsetHeight + 'px';
  calDiv.style.left = field_pos.x + 'px';
  calDiv.style.display = "block";
  advAJAX.get({
    url: get_file,
    onInitialization : function() {
        //someDiv.innerHTML = '';
    },
    onSuccess : function(obj) {
        calDiv.innerHTML = obj.responseText;
    },
    onError : function(obj) {
        alert("Error: " + obj.status);
    },
    onFinalization : function() {
        /* Ponowne pokazanie warstwy po zako�czeniu wszystkich operacji */
    }
  });
}    
function getAnchorPosition (anchorname) 
{
    var useWindow = false;
    var coordinates = new Object ();
    var x = 0, y = 0;
    var use_gebi = false, use_css = false, use_layers = false;
    if ( document.getElementById ) {
        use_gebi = true;
    } else if ( document.all ) {
        use_css = true;
    } else if ( document.layers ) {
        use_layers = true;
    }
    if ( use_gebi && document.all ) {
	x = AnchorPosition_getPageOffsetLeft ( document.all [ anchorname ] );
	y = AnchorPosition_getPageOffsetTop ( document.all [ anchorname ] );
    } else if ( use_gebi ) {
	var o = document.getElementById ( anchorname );
	x = AnchorPosition_getPageOffsetLeft ( o );
	y = AnchorPosition_getPageOffsetTop ( o );
    } else if ( use_css ) {
	x = AnchorPosition_getPageOffsetLeft ( document.all [ anchorname ] );
	y = AnchorPosition_getPageOffsetTop ( document.all [ anchorname ] );
    } else if ( use_layers ) {
	var found = 0;
	for ( var i = 0; i < document.anchors.length; i ++ ) {
  	    if ( document.anchors[i].name == anchorname ) {
                found = 1;
                break;
            }
	}
	if ( found == 0 ) {
	    coordinates.x = 0;
            coordinates.y = 0;
            return coordinates;
	}
	x = document.anchors[i].x;
	y=document.anchors[i].y;
    } else {
	coordinates.x=0; coordinates.y=0; return coordinates;
    }
    coordinates.x=x;
    coordinates.y=y;
    return coordinates;
}
function getAnchorWindowPosition (anchorname) 
{
    var coordinates = getAnchorPosition ( anchorname );
    var x = 0;
    var y = 0;
    if ( document.getElementById ) {
	if ( isNaN ( window.screenX ) ) {
	    x = coordinates.x - document.body.scrollLeft + window.screenLeft;
	    y = coordinates.y - document.body.scrollTop + window.screenTop;
	} else {
	    x = coordinates.x + window.screenX + ( window.outerWidth - window.innerWidth ) - window.pageXOffset;
	    y = coordinates.y + window.screenY + ( window.outerHeight - 24 - window.innerHeight ) - window.pageYOffset;
	}
    } else if ( document.all ) {
	x = coordinates.x - document.body.scrollLeft + window.screenLeft;
	y = coordinates.y - document.body.scrollTop + window.screenTop;
    } else if ( document.layers ) {
	x = coordinates.x + window.screenX + ( window.outerWidth - window.innerWidth ) - window.pageXOffset;
	y = coordinates.y + window.screenY + ( window.outerHeight - 24 - window.innerHeight ) - window.pageYOffset;
    }
    coordinates.x = x;
    coordinates.y = y;
    return coordinates;
}
function AnchorPosition_getPageOffsetLeft ( el ) 
{
    var ol = el.offsetLeft;
    while ( ( el = el.offsetParent ) != null ) {
        ol += el.offsetLeft;
    }
    return ol;
}
function AnchorPosition_getWindowOffsetLeft ( el ) 
{
    return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
}	
function AnchorPosition_getPageOffsetTop ( el ) 
{
    var ot = el.offsetTop;
    while ( ( el = el.offsetParent ) != null ) {
        ot += el.offsetTop;
    }
    return ot;
}
function AnchorPosition_getWindowOffsetTop ( el ) 
{
	return AnchorPosition_getPageOffsetTop ( el ) - document.body.scrollTop;
}

