var oForm;

function InitializeForm()
{
    if (g_bGetForm)
        oForm = GetElement("oHistoryForm");
    else
        oForm = oHistoryForm;
    oForm.oDay.focus();
}

function ValidateForm()
{
    switch (oForm.oHistoryType.value)
    {
    case "oneday":
        {
            var oReg = new RegExp("^([0-9]{1,2})\\.([0-9]{1,2})\\.([0-9]{4})$", "");
            var strError = langDateFormat;
            if (oReg.exec(oForm.oDay.value) != null)
            {
                var nDay = parseInt(RegExp.$1, 10), nMonth = parseInt(RegExp.$2, 10), nYear = parseInt(RegExp.$3, 10);
                var date = new Date(nYear, nMonth - 1, nDay);
                strError = langIncorrectDate;
                if ((date.getDate() == nDay) &&
                    (date.getMonth() == nMonth - 1) &&
                    (date.getFullYear() == nYear))
                {
                    oForm.oDayHidden.value = 
                        nYear.toString() +
                        ((nMonth < 10) ? ("0" + nMonth) : nMonth) +
                        ((nDay < 10) ? ("0" + nDay) : nDay);
                    return true;
                }
            }
            
            oForm.oDay.focus();
            alert(strError);
            return false;
        }
        break;
        
    case "onemonth":
        {
            var strYearMonthInput = oForm.oYear.value + oForm.oMonth.value;
            var dateNow = new Date();
            var strYearMonthNow = 
                dateNow.getFullYear().toString() + 
                (dateNow.getMonth() + 1 < 10 ? "0" + (dateNow.getMonth() + 1) : dateNow.getMonth() + 1);
            if (strYearMonthInput > strYearMonthNow)
            {
                var strErr = langMonthNG;
                strErr = strErr.replace("REPLACE_MONTH", aMonths[dateNow.getMonth()] + " " + dateNow.getFullYear());
                alert(strErr);
                return false;
            }
            return true;
        }
        break;

    case "interval":
        {
            var oReg = new RegExp("^([0-9]{1,2})\\.([0-9]{1,2})\\.([0-9]{4})$", "");
            var strError, nDay, nMonth, nYear, oDate;
            try
            {
                if (oReg.exec(oForm.oStartDate.value) == null)
                {
                    oForm.oStartDate.focus();
                    throw langDateFormat;
                }
                nDay = parseInt(RegExp.$1, 10);
                nMonth = parseInt(RegExp.$2, 10);
                nYear = parseInt(RegExp.$3, 10);
                oDate = new Date(nYear, nMonth - 1, nDay);
                if ((oDate.getDate() != nDay) || (oDate.getMonth() != nMonth - 1) || (oDate.getFullYear() != nYear))
                {
                    oForm.oStartDate.focus();
                    throw langIncorrectDate;
                }
                oForm.oStartDateHidden.value = nYear.toString() + ((nMonth < 10) ? ("0" + nMonth) : nMonth) + ((nDay < 10) ? ("0" + nDay) : nDay);

                if (oReg.exec(oForm.oEndDate.value) == null)
                {
                    oForm.oEndDate.focus();
                    throw langDateFormat;
                }
                nDay = parseInt(RegExp.$1, 10);
                nMonth = parseInt(RegExp.$2, 10);
                nYear = parseInt(RegExp.$3, 10);
                oDate = new Date(nYear, nMonth - 1, nDay);
                if ((oDate.getDate() != nDay) || (oDate.getMonth() != nMonth - 1) || (oDate.getFullYear() != nYear))
                {
                    oForm.oEndDate.focus();
                    throw langIncorrectDate;
                }
                oForm.oEndDateHidden.value = nYear.toString() + ((nMonth < 10) ? ("0" + nMonth) : nMonth) + ((nDay < 10) ? ("0" + nDay) : nDay);
                
                if (oForm.oStartDateHidden.value > oForm.oEndDateHidden.value)
                {
                    oForm.oEndDate.focus();
                    throw langIncorrectInterval;
                }
            }
            catch(e)
            {
                alert(e);
                return false;
            }
            return true;
        }
        break;
    }        
    return false;
}

function SetHistoryType(strHistoryType)
{
    oForm.oHistoryType.value = strHistoryType;    
}
