function ShowCalendar (fld,strFieldValue,fldGranularity,Imprecise,OnSelectDateEvent) {
//  fld : name of the HTML field in which the date is returned.
//  fldGranularity : name of the HTML field in which the date format is given and returned ("D", "M", "Y" or "C").
//  Imprecise : boolean which indicates whether the date format selection appears.

    var strGranularity="";
    var intGranularity;
    var strRequest;
    var strDefaultDate=strFieldValue;

    if (fldGranularity!=null && fldGranularity.length>0)  //  In some places of Tytti, "Showcalendar()" is called with only one argument
        strGranularity=String(eval("document.DForm."+fldGranularity+".value")).toUpperCase();
    if (strGranularity=="" || strGranularity==" ")    //  Precisions are char(1) in the database. That's why we use " ".
        strGranularity="D";

    if (fldGranularity==null)
        fldGranularity="";

    intGranularity=GetIntGranularity(strGranularity);
    strDefaultDate=ImpreciseDateToRegular(strDefaultDate,strGranularity);
    strRequest="CalendarCOM.asp?SOURCE_FIELD=" + fld+"&DATE_FORMAT_FIELD="+fldGranularity+
                    "&GRANULARITY="+intGranularity+"&DEFAULT_DATE="+strDefaultDate;

    if (OnSelectDateEvent!="undefined" && OnSelectDateEvent!=null)
        strRequest=strRequest+"&SELECT_EVENT="+OnSelectDateEvent;

    if (Imprecise==true) {
        strRequest=strRequest+"&IMPRECISE=1";
        cale = window.open(strRequest,'cale',
        'toolbar=no,scrollbar=no,dependent=yes,status=no,height=300,width=270,titlebar=no,alwaysRaised=yes,resizable=no');
    }
    else
        cale = window.open(strRequest,'cale',
        'toolbar=no,scrollbar=no,dependent=yes,status=no,height=300,width=270,titlebar=no,alwaysRaised=yes,resizable=no');
	cale.focus();
}

function ImpreciseDateToRegular(strImpreciseDate,strGranularity) {
//  We transform a date from a imprecise date string to a regular date string, according to the given format(granularity)
    var i;
    var strRes;

    strRes=strImpreciseDate;
    if (strGranularity=='M') {
        i=strImpreciseDate.indexOf("/");
        if (i!=-1)
            strRes="01."+strImpreciseDate.substring(0,i)+"."+strImpreciseDate.substring(i+1,strImpreciseDate.length);
    }
    else
    if (strGranularity=='Y' || strGranularity=='C')
        strRes="01.01."+strImpreciseDate;

    return strRes;
}

function RegularDateToImprecise(strRegularDate,strGranularity) {
//  We transform a date from a regular date string to a imprecise date string, according to the given format(granularity)
    var i,j;
    var strRes;
    var year;

    strRes=strRegularDate;

    if (strGranularity!='D') {
        i=strRegularDate.indexOf(".");
        if (i!=-1) {
            j=strRegularDate.indexOf(".",i+1);
            if (j!=-1) {
                year=strRegularDate.substring(j+1,strRegularDate.length);
                if (strGranularity=='M')
                    strRes=strRegularDate.substring(i+1,j)+"/"+year;
                else                
                if (strGranularity=='C')  //  Decade : we round the year to the lower ten boundary
                    strRes=year-(year % 10);
                else
                    strRes=year;
            }
        }
    }

    return strRes;
}

function DateCompare(strFirstDate,strSecondDate) {
//  Return -1 if FirstDate<SecondDate, 0 if FirstDate=SecondDate, and +1 if FirstDate>SecondDate
//  Dates have to be regular (dd.mm.yyyy)
    var i,j;
    var intDay1,intDay2,intMonth1,intMonth2,intYear1,intYear2;
    var int1,int2;

    intDay1=GetDay(strFirstDate);
    intMonth1=GetMonth(strFirstDate);
    intYear1=GetYear(strFirstDate);
    intDay2=GetDay(strSecondDate);
    intMonth2=GetMonth(strSecondDate);
    intYear2=GetYear(strSecondDate);

    int1=intYear1*10000+intMonth1*100+intDay1;
    int2=intYear2*10000+intMonth2*100+intDay2;

    if (int1>int2)
        return 1;
    else
    if (int1<int2)
        return -1;
    else
        return 0;
}

function GetDay(strRegularDate) {
//  Returns the day part of the regular date (dd.mm.yyyy)
    var i,j;
    var intRes=0;

    i=strRegularDate.indexOf(".");
    if (i!=-1) {
        j=strRegularDate.indexOf(".",i+1);
        if (j!=-1)
            intRes=strRegularDate.substring(0,i);
    }
    return intRes;
}

function GetMonth(strRegularDate) {
//  Returns the month part of the regular date (dd.mm.yyyy)
    var i,j;
    var intRes=0;

    i=strRegularDate.indexOf(".");
    if (i!=-1) {
        j=strRegularDate.indexOf(".",i+1);
        if (j!=-1)
            intRes=strRegularDate.substring(i+1,j);
    }
    return intRes;
}

function GetYear(strRegularDate) {
//  Returns the year part of the regular date (dd.mm.yyyy)
    var i,j;
    var intRes=0;

    i=strRegularDate.indexOf(".");
    if (i!=-1) {
        j=strRegularDate.indexOf(".",i+1);
        if (j!=-1)
            intRes=strRegularDate.substring(j+1,strRegularDate.length);
    }
    return intRes;
}

function isValidDate(strDate,strGranularity) {
//  Check the validity of "strDate" according to the "Granularity" date format.
//  Granularity == "D","M","Y" or "C"

    if (strDate==null)
        return false;
    var intDay=1,intMonth=1,intYear,datePattern,matchArray;

    if (strGranularity==null) 
        strGranularity="D";
    switch(strGranularity) {
        case "D" : datePattern = /^(\d{2})(\.)(\d{2})\2(\d{4})$/;
                    break;
        case "M" : datePattern = /^(\d{2})(\/)(\d{4})$/;
                    break;
        case "Y" : datePattern = /^(\d{4})$/;
                    break;
        case "C" : datePattern = /^(\d{4})$/;
                    break;
        default : return false;
    }
    matchArray = strDate.match(datePattern); // is the format ok?

    if (matchArray != null) {
        if (strGranularity=="D") {
            intDay = matchArray[1];
            intMonth = matchArray[3];
            intYear = matchArray[4];
        }
        else
        if (strGranularity=="M") {
            intMonth = matchArray[1];
            intYear = matchArray[3];
        }
        else
            intYear = matchArray[1];

        if (intMonth > 0 && intMonth < 13 && intDay > 0 && intDay < 32 
        && ((intMonth!=4 && intMonth!=6 && intMonth!=9 && intMonth!=11) || intDay!=31)) {
            if (intMonth == 2) { // check for february 29th
                var isleap = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0));
                if (intDay>=29 && !isleap)
                    return false;
            }
        }
        else
            return false;
    }
    else
        return false;

    return true;
}

function GetCharGranularity(intGranularity) {
    switch(intGranularity) {
        case "0" : return "D";
        case "1" : return "M";
        case "2" : return "Y";
        case "3" : return "C";
    }
}

function GetIntGranularity(strGranularity) {
    switch(strGranularity) {
        case "D" : return 0;
        case "M" : return 1;
        case "Y" : return 2;
        case "C" : return 3;
    }
}

//  ** Methods which were previously in Calendar(/Org)Com.asp **************
function fadeAway ()
{
	wx = 180;
	wy = 220;
	for (i=0; i < 20; i++) {
		this.resizeTo (wx, wy);
		wx -= 5;
		wy -= 5;
		this.moveBy (2, 2)
	}
}

function Cancel ()
{
	fadeAway();
	window.close();
}

function DateEntered(DateField,ButtonName) {
//  KeyUp event fired from the date text box

    if (event.keyCode==13 && window.DForm(DateField).value!="") {
        event.returnValue=false;
        window.DForm(ButtonName).click();
    }
    else
        window.DForm(ButtonName).disabled=(window.DForm(DateField).value=="");
}

function GranularityClick(Radio) {
    window.DForm.GRANULARITY.value=Radio.value;
    window.DForm.submit();
}

//  ****************************************************************************
