//<?
//  header("Content-Type: application/x-javascript");
//?>

function checkDate(fieldName) {

var errorCode=0;
dateField = fieldName.value;
if (dateField.length != 10) errorCode=1;
day = dateField.substring(0, 2);
point1 = dateField.substring(2, 3);
month = dateField.substring(3, 5);
point2 = dateField.substring(5, 6);
year = dateField.substring(6, 10);
if (isNaN(day)) errorCode = 1;
if (isNaN(month)) errorCode = 1;
if (isNaN(year)) errorCode = 1;
if ((day < 1) || (day > 31)) errorCode = 2;
if (point1 != '.') errorCode = 3;
if ((month < 1) || (month > 12)) errorCode = 4;
if (point2 != '.') errorCode = 5;
if ((month == 4) || (month == 6) || (month == 9) || (month == 11)){
if (day == 31) errorCode = 7
}
if (month == 2){
var g=parseInt(year/4);
if (isNaN(g)) errorCode=8;
if (day > 29) errorCode = 9;
if ((day == 29) && ((year/4) != parseInt(year/4))) errorCode=10;
}
if (errorCode > 0) {
alert('Грешна дата или грешен формат!\n Моля въведете датата във формат: ДД.ММ.ГГГГ или ползвайте календарчето!');
fieldName.focus();
fieldName.select();
return false;
}
else return true;
}




// Author: Robert W. Husted
// Localized:  CNSys PLC. Bulgaria

calDateFormat    = "DD.MM.yyyy";
topBackground    = "#CCCCFF";
bottomBackground = "#6633CC";
tableBGColor     = "black";
cellColor        = "#dddddd";
headingCellColor = "#ffffff";
headingTextColor = "black";
dateColor        = "black";
focusColor       = "#ff0000";
hoverColor       = "#6633CC";
fontStyle        = "9pt Verdana, Tahoma, Arial, helvetica";
headingFontStyle = "bold 9pt Verdana, Tahoma, Arial, helvetica";

bottomBorder  = false;
tableBorder   = 0;

var isNav = false;
var isIE  = false;


if (navigator.appName == "Netscape") {
    isNav = true;
}
else {
    isIE = true;
}

selectedLanguage = "BG";

buildCalParts();


function setDateField(dateField) {

    calDateField = dateField;

    inDate = dateField.value;

    setInitialDate();

    calDocTop    = buildTopCalFrame();
    calDocBottom = buildBottomCalFrame();
}


function setInitialDate() {
   
    calDate = new Date(inDate);

    if (isNaN(calDate)) {

        calDate = new Date();
    }

    calDay  = calDate.getDate();

    calDate.setDate(1);
}


function showCalendar(dateField) {

    setDateField(dateField);

    calDocFrameset = 
        "<HTML><HEAD><META HTTP-EQUIV='Content-Type' CONTENT='text/html; CHARSET=Windows-1251'><TITLE>JavaScript Calendar</TITLE></HEAD>\n" +
        "<FRAMESET ROWS='70,*' FRAMEBORDER='0'>\n" +
        "  <FRAME NAME='topCalFrame' SRC='javascript:parent.opener.calDocTop' SCROLLING='no'>\n" +
        "  <FRAME NAME='bottomCalFrame' SRC='javascript:parent.opener.calDocBottom' SCROLLING='no'>\n" +
        "</FRAMESET>\n";

    top.newWin = window.open("javascript:parent.opener.calDocFrameset", "calWin", winPrefs);
    top.newWin.focus();
}


function buildTopCalFrame() {

    var calDoc =
        "<HTML>" +
        "<HEAD><META HTTP-EQUIV='Content-Type' CONTENT='text/html; CHARSET=Windows-1251'>" +
        "</HEAD>" +
        "<BODY BGCOLOR='" + topBackground + "'>" +
        "<FORM NAME='calControl' onSubmit='return false;'>" +
        "<CENTER>" +
        "<TABLE CELLPADDING=0 CELLSPACING=1 BORDER=0>" +
        "<TR><TD COLSPAN=7>" +
        "<CENTER>" +
        getMonthSelect() +
        "<INPUT style='font-family: Verdana; font-size:10pt' NAME='year' VALUE='" + calDate.getFullYear() + "'TYPE=TEXT SIZE=4 MAXLENGTH=4 onChange='parent.opener.setYear()' onFocus='this.blur()'>" +
        "</CENTER>" +
        "</TD>" +
        "</TR>" +
        "<TR>" +
        "<TD COLSPAN=7>" +
        "<INPUT style='font-family:Verdana; font-size:10pt' " +
        "TYPE=BUTTON NAME='previousYear' VALUE='<<&#1075;.'    onClick='parent.opener.setPreviousYear()'><INPUT style='font-family:Arial; font-size:10pt' " +
        "TYPE=BUTTON NAME='previousMonth' VALUE='<&#1084;.'   onClick='parent.opener.setPreviousMonth()'><INPUT  style='font-family:Arial; font-size:10pt' " +
        "TYPE=BUTTON NAME='today' VALUE='&#1044;&#1085;&#1077;&#1089;' onClick='parent.opener.setToday()'><INPUT  style='font-family:Arial; font-size:10pt' " +
        "TYPE=BUTTON NAME='nextMonth' VALUE='&#1084;.>'   onClick='parent.opener.setNextMonth()'><INPUT  style='font-family:Arial; font-size:10pt' " +
        "TYPE=BUTTON NAME='nextYear' VALUE='&#1075;.>>'    onClick='parent.opener.setNextYear()'>" +
        "</TD>" +
        "</TR>" +
        "</TABLE>" +
        "</CENTER>" +
        "</FORM>" +
        "</BODY>" +
        "</HTML>";

    return calDoc;
}


function buildBottomCalFrame() {       

    var calDoc = calendarBegin;

    month   = calDate.getMonth();
    year    = calDate.getFullYear();

    day     = calDay;

    var i   = 0;

    var days = getDaysInMonth();

    if (day > days) {
        day = days;
    }

    var firstOfMonth = new Date (year, month, 1);

    var startingPos  = firstOfMonth.getDay();
    days += startingPos;

    var columnCount = 0;

    for (i = 0; i < startingPos; i++) {

        calDoc += blankCell;
        columnCount++;
    }

    var currentDay = 0;
    var dayType    = "weekday";

    for (i = startingPos; i < days; i++) {

	var paddingChar = "&nbsp;";

        if (i-startingPos+1 < 10) {
            padding = "&nbsp;&nbsp;";
        }
        else {
            padding = "&nbsp;";
        }

        currentDay = i-startingPos+1;

        if (currentDay == day) {
            dayType = "focusDay";
        }
        else {
            dayType = "weekDay";
        }

        calDoc += "<TD bgcolor='" + cellColor + "'>" +
                  "<a style='text-decoration: none' class='" + dayType + "' href='javascript:parent.opener.returnDate(" + 
                  currentDay + ")'>" + padding + currentDay + paddingChar + "</a></TD>";

        columnCount++;

        if (columnCount % 7 == 0) {
            calDoc += "</TR><TR>";
        }
    }

    for (i=days; i<42; i++)  {

        calDoc += blankCell;
	columnCount++;

        if (columnCount % 7 == 0) {
            calDoc += "</TR>";
            if (i<41) {
                calDoc += "<TR>";
            }
        }
    }

    calDoc += calendarEnd;

    return calDoc;
}


function writeCalendar() {

    calDocBottom = buildBottomCalFrame();

    top.newWin.frames['bottomCalFrame'].document.open();
    top.newWin.frames['bottomCalFrame'].document.write(calDocBottom);
    top.newWin.frames['bottomCalFrame'].document.close();
}


function setToday() {

    calDate = new Date();

    var month = calDate.getMonth();
    var year  = calDate.getFullYear();

    top.newWin.frames['topCalFrame'].document.calControl.month.selectedIndex = month;

    top.newWin.frames['topCalFrame'].document.calControl.year.value = year;

    writeCalendar();
}


function setYear() {

    var year  = top.newWin.frames['topCalFrame'].document.calControl.year.value;

    if (isFourDigitYear(year)) {
        calDate.setFullYear(year);
        writeCalendar();
    }
    else {
        top.newWin.frames['topCalFrame'].document.calControl.year.focus();
        top.newWin.frames['topCalFrame'].document.calControl.year.select();
    }
}


function setCurrentMonth() {

    var month = top.newWin.frames['topCalFrame'].document.calControl.month.selectedIndex;

    calDate.setMonth(month);
    writeCalendar();
}


function setPreviousYear() {

    var year  = top.newWin.frames['topCalFrame'].document.calControl.year.value;

    if (isFourDigitYear(year) && year > 1000) {
        year--;
        calDate.setFullYear(year);
        top.newWin.frames['topCalFrame'].document.calControl.year.value = year;
        writeCalendar();
    }
}

function setPreviousMonth() {

    var year  = top.newWin.frames['topCalFrame'].document.calControl.year.value;
    if (isFourDigitYear(year)) {
        var month = top.newWin.frames['topCalFrame'].document.calControl.month.selectedIndex;

        if (month == 0) {
            month = 11;
            if (year > 1000) {
                year--;
                calDate.setFullYear(year);
                top.newWin.frames['topCalFrame'].document.calControl.year.value = year;
            }
        }
        else {
            month--;
        }
        calDate.setMonth(month);
        top.newWin.frames['topCalFrame'].document.calControl.month.selectedIndex = month;
        writeCalendar();
    }
}


function setNextMonth() {

    var year = top.newWin.frames['topCalFrame'].document.calControl.year.value;

    if (isFourDigitYear(year)) {
        var month = top.newWin.frames['topCalFrame'].document.calControl.month.selectedIndex;

        if (month == 11) {
            month = 0;
            year++;
            calDate.setFullYear(year);
            top.newWin.frames['topCalFrame'].document.calControl.year.value = year;
        }
        else {
            month++;
        }
        calDate.setMonth(month);
        top.newWin.frames['topCalFrame'].document.calControl.month.selectedIndex = month;
        writeCalendar();
    }
}


function setNextYear() {

    var year  = top.newWin.frames['topCalFrame'].document.calControl.year.value;
    if (isFourDigitYear(year)) {
        year++;
        calDate.setFullYear(year);
        top.newWin.frames['topCalFrame'].document.calControl.year.value = year;
        writeCalendar();
    }
}


function getDaysInMonth()  {

    var days;
    var month = calDate.getMonth()+1;
    var year  = calDate.getFullYear();

    if (month==1 || month==3 || month==5 || month==7 || month==8 ||
        month==10 || month==12)  {
        days=31;
    }
    else if (month==4 || month==6 || month==9 || month==11) {
        days=30;
    }
    else if (month==2)  {
        if (isLeapYear(year)) {
            days=29;
        }
        else {
            days=28;
        }
    }
    return (days);
}

function isLeapYear (Year) {

    if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) {
        return (true);
    }
    else {
        return (false);
    }
}


function isFourDigitYear(year) {

    if (year.length != 4) {
        top.newWin.frames['topCalFrame'].document.calControl.year.value = calDate.getFullYear();
        top.newWin.frames['topCalFrame'].document.calControl.year.select();
        top.newWin.frames['topCalFrame'].document.calControl.year.focus();
    }
    else {
        return true;
    }
}

function getMonthSelect() {
   if (selectedLanguage == "BG") {
        monthArray = new Array('&#1071;&#1085;&#1091;&#1072;&#1088;&#1080;', '&#1060;&#1077;&#1074;&#1088;&#1091;&#1072;&#1088;&#1080;', '&#1052;&#1072;&#1088;&#1090;', '&#1040;&#1087;&#1088;&#1080;&#1083;', '&#1052;&#1072;&#1081;', '&#1070;&#1085;&#1080;',
                               '&#1070;&#1083;&#1080;', '&#1040;&#1074;&#1075;&#1091;&#1089;&#1090;', '&#1057;&#1077;&#1087;&#1090;&#1077;&#1084;&#1074;&#1088;&#1080;', '&#1054;&#1082;&#1090;&#1086;&#1084;&#1074;&#1088;&#1080;', '&#1053;&#1086;&#1077;&#1084;&#1074;&#1088;&#1080;', '&#1044;&#1077;&#1082;&#1077;&#1084;&#1074;&#1088;&#1080;');
    }
    
    var activeMonth = calDate.getMonth();

    monthSelect = "<font face='Verdana, Arial' size='2'><SELECT style='font-family:Verdana' NAME='month' onChange='parent.opener.setCurrentMonth()'>";

    for (i in monthArray) {
        
        if (i == activeMonth) {
            monthSelect += "<OPTION SELECTED>" + monthArray[i] + "\n";
        }
        else {
            monthSelect += "<OPTION>" + monthArray[i] + "\n";
        }
    }
    monthSelect += "</SELECT></font>";

    return monthSelect;
}

function buildCalParts() {

    blankCell = "<TD align=center bgcolor='" + cellColor + "'>&nbsp;&nbsp;&nbsp;</TD>";

    calendarBegin =
        "<HTML>" +
        "<HEAD>" +
        "<STYLE type='text/css' media = 'screen'>" +
        "<!--" +
        "TD.heading { text-decoration: none; color:" + headingTextColor + "; font: " + headingFontStyle + "; }" +
        "TD{text-decoration: none; color:" + dateColor + "; font: " + fontStyle + "; }" +
        "A.focusDay:link { text-decoration: none; color: #ff0000; font: " + fontStyle + "; }" +
        "A.focusDay:hover { text-decoration: none; color: #ff0000; font: " + fontStyle + "; font-weight: bold;}" +
        "A.weekday:link { text-decoration: none; color: #000000 ;font: " + fontStyle + "; }" +
        "A.weekday:hover { text-decoration: none; color: " + hoverColor + "; font: " + fontStyle + "; font-weight: bold;}" +
		"-->" +
        "</STYLE>" +
        "</HEAD>" +
        "<BODY BGCOLOR='" + bottomBackground + "'" +
        "<CENTER>";

        if (isNav) {
            calendarBegin += 
                "<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=" + tableBorder + " ALIGN=CENTER BGCOLOR='" + tableBGColor + "'><TR><TD>";
        }

        calendarBegin +=
            "<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=" + tableBorder + " ALIGN=CENTER BGCOLOR='" + tableBGColor + "'>" +
             "<TR BGCOLOR='" + headingCellColor + "'>" + "<TD class='heading' align='center'><img src = 'img/nd.gif' width = '26' height = '16'></TD>" +
			 "<TD class='heading' align='center'><img src = 'img/pn.gif' width = '26' height = '16'></TD>" +
			"<TD class='heading' align='center'><img src = 'img/vt.gif' width = '26' height = '16'></TD>" +
			"<TD class='heading' align='center'><img src = 'img/sr.gif' width = '26' height = '16'></TD>" +
			"<TD class='heading' align='center'><img src = 'img/cht.gif' width = '26' height = '16'></TD>" +
			"<TD class='heading' align='center'><img src = 'img/pt.gif' width = '26' height = '16'></TD>" +
			"<TD class='heading' align='center'><img src = 'img/sb.gif' width = '26' height = '16'></TD>" +
			"</TR>" +
            "<TR>";

    calendarEnd = "";

        if (bottomBorder) {
            calendarEnd += "<TR></TR>";
        }

        if (isNav) {
            calendarEnd += "</TD></TR></TABLE>";
        }

        calendarEnd +=
            "</TABLE>" +
            "</CENTER>" +
            "</BODY>" +
            "</HTML>";
}

function jsReplace(inString, find, replace) {

    var outString = "";

    if (!inString) {
        return "";
    }

    if (inString.indexOf(find) != -1) {
        t = inString.split(find);

        return (t.join(replace));
    }
    else {
        return inString;
    }
}

function doNothing() {
}


function makeTwoDigit(inValue) {

    var numVal = parseInt(inValue, 10);

    if (numVal < 10) {

        return("0" + numVal);
    }
    else {
        return numVal;
    }
}

function returnDate(inDay)
{

    calDate.setDate(inDay);

    var day           = calDate.getDate();
    var month         = calDate.getMonth()+1;
    var year          = calDate.getFullYear();
    var monthString   = monthArray[calDate.getMonth()];
    var monthAbbrev   = monthString.substring(0,3);

	outDate = calDateFormat;

    if (calDateFormat.indexOf("DD") != -1) {
        day = makeTwoDigit(day);
        outDate = jsReplace(outDate, "DD", day);
    }

	else if (calDateFormat.indexOf("dd") != -1) {
        outDate = jsReplace(outDate, "dd", day);
    }

    if (calDateFormat.indexOf("MM") != -1) {
        month = makeTwoDigit(month);
        outDate = jsReplace(outDate, "MM", month);
    }

    else if (calDateFormat.indexOf("mm") != -1) {
        outDate = jsReplace(outDate, "mm", month);
    }

    if (calDateFormat.indexOf("yyyy") != -1) {
        outDate = jsReplace(outDate, "yyyy", year);
    }

    else if (calDateFormat.indexOf("yy") != -1) {
        var yearString = "" + year;
        var yearString = yearString.substring(2,4);
        outDate = jsReplace(outDate, "yy", yearString);
    }
    else if (calDateFormat.indexOf("YY") != -1) {
        outDate = jsReplace(outDate, "YY", year);
    }

    if (calDateFormat.indexOf("Month") != -1) {
        outDate = jsReplace(outDate, "Month", monthString);
    }

    else if (calDateFormat.indexOf("month") != -1) {
        outDate = jsReplace(outDate, "month", monthString.toLowerCase());
    }

    else if (calDateFormat.indexOf("MONTH") != -1) {
        outDate = jsReplace(outDate, "MONTH", monthString.toUpperCase());
    }

    if (calDateFormat.indexOf("Mon") != -1) {
        outDate = jsReplace(outDate, "Mon", monthAbbrev);
    }

	else if (calDateFormat.indexOf("mon") != -1) {
        outDate = jsReplace(outDate, "mon", monthAbbrev.toLowerCase());
    }

    else if (calDateFormat.indexOf("MON") != -1) {
        outDate = jsReplace(outDate, "MON", monthAbbrev.toUpperCase());
    }

    if (calDateFormat.indexOf("Weekday") != -1) {
        outDate = jsReplace(outDate, "Weekday", weekday);
    }

    else if (calDateFormat.indexOf("weekday") != -1) {
        outDate = jsReplace(outDate, "weekday", weekday.toLowerCase());
    }

    else if (calDateFormat.indexOf("WEEKDAY") != -1) {
        outDate = jsReplace(outDate, "WEEKDAY", weekday.toUpperCase());
    }

    if (calDateFormat.indexOf("Wkdy") != -1) {
        outDate = jsReplace(outDate, "Wkdy", weekdayAbbrev);
    }

    else if (calDateFormat.indexOf("wkdy") != -1) {
        outDate = jsReplace(outDate, "wkdy", weekdayAbbrev.toLowerCase());
    }

    else if (calDateFormat.indexOf("WKDY") != -1) {
        outDate = jsReplace(outDate, "WKDY", weekdayAbbrev.toUpperCase());
    }

    calDateField.value = outDate;

    calDateField.focus();
    calDateField.blur();
    top.newWin.close();

}

