// Following function is used when checking if a submit button was already clicked once, in order to avoid
// double form submission.
var submitButtonClicked = false;
function doCheckSubmission() {
    if (submitButtonClicked == false) {
        submitButtonClicked = true;
        return true;
    } else { //true - button was already was clicked; disable any other submition
        return false;
    }
}


//dates dropdowns related code
function HZICalendar(day, month, year) {
    var maxDay = 31;
    var minDay = 1;
    var maxMonth = 12;
    var minMonth = 1;
    var maxYear = 9999;
    var minYear = 1000;

    if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) {
        maxDay = 30;
    } else if (month == 2) {
        if ((year % 4 == 0) && ((year % 100 != 0)) || (year % 400 == 0)) {
            maxDay = 29;
        } else {
            maxDay = 28;
        }
    }

    if (day < minDay) {
        this.day = minDay;
    } else if (day > maxDay) {
        this.day = maxDay;
    } else {
        this.day = day;
    }

    if (month < minMonth) {
        this.month = minMonth;
    } else if (month > maxMonth) {
        this.month = maxMonth;
    } else {
        this.month = month;
    }

    if (year < minYear) {
        this.year = minYear;
    } else if (year > maxYear) {
        this.year = maxYear;
    } else {
        this.year = year;
    }
}

function daysBetween(date1, date2) {
    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24
    // Convert both dates to milliseconds
    var date1_ms = date1.getTime()
    var date2_ms = date2.getTime()
    // Calculate the difference in milliseconds
    var difference_ms = date2_ms - date1_ms
    // Convert back to days and return
    return Math.round(difference_ms / ONE_DAY)
}

/*
 * This method adjustes the indate select fields to the nearest corect date and the outdate to be next to indate.
 */
function populateDateFields(form, inDateElement, outDateElement, userDateFormat) {
    populateDateFieldsWithVariableNumberOfDays(form, inDateElement, outDateElement, userDateFormat, 1);
}

/*
 * This method adjustes the indate select fields to the nearest corect date and the outdate to be next to indate.
 */
function populateDateFieldsWithAdjustment(form, inDateElement, outDateElement, userDateFormat) {
    populateDateFieldsWithVariableNumberOfDays(form, inDateElement, outDateElement, userDateFormat, numberOfDays);
}

/*
 * This method is used to construct the correct inDate and outDate for the date fields input.
 */
function populateDateFieldsWithVariableNumberOfDays(form, inDateElement, outDateElement, userDateFormat, nrOfDays) {
    var inDateFromForm = form.elements[inDateElement].value;
    var inDateElementAsString, outDateElementAsString;

    var dayStart, dayEnd, monthStart, monthEnd, yearStart, yearEnd;
    var numberOfDateFormat = getNumberOfDateFormat(userDateFormat);

    dayStart = userDateFormat.indexOf("d");
    dayEnd = userDateFormat.lastIndexOf("d") + 1;
    monthStart = userDateFormat.indexOf("M");
    monthEnd = userDateFormat.lastIndexOf("M") + 1;
    yearStart = userDateFormat.indexOf("y");
    yearEnd = userDateFormat.lastIndexOf("y") + 1;
    var inDateMonthAsNumber = parseInt(inDateFromForm.substring(monthStart, monthEnd), 10) - 1;

    var inDate = new Date(1970, 0, 1);
    inDate.setYear(inDateFromForm.substring(yearStart, yearEnd));
    inDate.setMonth(inDateMonthAsNumber.toString());
    inDate.setDate(inDateFromForm.substring(dayStart, dayEnd));

    var outDate = new Date(1970, 0, 1);
    outDate.setYear(inDate.getYear());
    outDate.setMonth(inDate.getMonth());
    outDate.setDate(inDate.getDate() + nrOfDays);

    var inDateYear, inDateMonth, inDateDay, outDateYear, outDateMonth, outDateDay;
    inDateYear = "" + inDate.getYear();
    if (inDateYear.length == 1) {
        inDateYear = "0" + inDateYear;
    }

    inDateMonth = "" + (inDate.getMonth() + 1);
    if (inDateMonth.length == 1) {
        inDateMonth = "0" + inDateMonth;
    }

    inDateDay = "" + inDate.getDate();
    if (inDateDay.length == 1) {
        inDateDay = "0" + inDateDay;
    }

    outDateYear = "" + outDate.getYear();
    if (outDateYear.length == 1) {
        outDateYear = "0" + outDateYear;
    }

    outDateMonth = "" + (outDate.getMonth() + 1);
    if (outDateMonth.length == 1) {
        outDateMonth = "0" + outDateMonth;
    }

    outDateDay = "" + outDate.getDate();
    if (outDateDay.length == 1) {
        outDateDay = "0" + outDateDay;
    }

    switch (numberOfDateFormat) {
        case 0: // ddMMyy format
            inDateElementAsString = inDateDay + inDateMonth + inDateYear;
            outDateElementAsString = outDateDay + outDateMonth + outDateYear;
            break;
        case 1: // MMddyy format
            inDateElementAsString = inDateMonth + inDateDay + inDateYear;
            outDateElementAsString = outDateMonth + outDateDay + outDateYear;
            break;
        case 2: // yyMMdd format
            inDateElementAsString = inDateYear + inDateMonth + inDateDay;
            outDateElementAsString = outDateYear + outDateMonth + outDateDay;
            break;
    }

    form.elements[inDateElement].value = inDateElementAsString;
    form.elements[outDateElement].value = outDateElementAsString;
}

var numberOfDays = 1;
//end of dates dropdowns related code

//calendar
function showCalendar(obj, dateFormat) {
    if (self.gfPop) {
        var numberOfDateFormat = getNumberOfDateFormat(dateFormat);
        gfPop.showPopCalendar(dateFormat, numberOfDateFormat, obj);
    }
    return false;
}

function showCalendarForDropdowns(day, month, year, dateFormat) {
    var range = [[parseInt(year.options[0].value), 1, 1], [parseInt(year.options[year.options.length - 1].value), 12, 31]];
    var numberOfDateFormat = getNumberOfDateFormat(dateFormat);
    
    if (self.gfPop) {
        gfPop.showPopCalendarForDropdowns(dateFormat, numberOfDateFormat, day, month, year, range);
    }
    return false;
}

/* This method gets the right parameter for the user selected date format for showing the calendar
 * based on the selected date format by the user.
 *
 */
function getNumberOfDateFormat(dateFormat) {
    var numberOfDateFormat = 0;
    var dayStart, monthStart, yearStart;
    //determine the positions of "dd", "MM" and "YY" strings
    //maybe some error checking and message should be used; get *End also and if dayEnd != dayStart + 1
    dayStart = dateFormat.indexOf("d");
    monthStart = dateFormat.indexOf("M");
    yearStart = dateFormat.indexOf("y");

    //check to order in which "dd", "MM" and "yy" appear
    if (dayStart < monthStart && monthStart < yearStart) {
        numberOfDateFormat = 0; // ddMMyy format
    } else if (monthStart < dayStart && dayStart < yearStart) {
        numberOfDateFormat = 1; // MMddyy format
    } else if (yearStart < monthStart && monthStart < dayStart) {
        numberOfDateFormat = 2; // yyMMdd format
    } // else use the default value of 0 = ddMMyy format (see variable declaration)

    return numberOfDateFormat;
}

function isLeapYear(year)
{
    if (((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0)) {
        return(true);
    }
    else {
        return(false);
    }
}

function getDaysInMonth(month, year)
{
    var days;
    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 syncInDaysWithMonth(form)
{
    var realDays = getDaysInMonth(form.inMonth[form.inMonth.selectedIndex].value, form.inYear[form.inYear.selectedIndex].value);
    if (realDays < form.inDay.length) {
        form.inDay.length = realDays;
    }
    else if (form.inDay.length < realDays) {
        form.inDay.length = realDays;
        for (inx = 29; inx <= realDays; inx++) {
            form.inDay[inx - 1].value = "" + inx;
            form.inDay[inx - 1].text = "" + inx;
        }
    }
}

function syncOutDaysWithMonth(form)
{
    var realDays = getDaysInMonth(form.outMonth[form.outMonth.selectedIndex].value, form.outYear[form.outYear.selectedIndex].value);

    if (realDays < form.outDay.length) {
        form.outDay.length = realDays;
    }
    else if (form.outDay.length < realDays) {
        form.outDay.length = realDays;
        for (inx = 29; inx <= realDays; inx++) {
            form.outDay[inx - 1].value = "" + inx;
            form.outDay[inx - 1].text = "" + inx;
        }
    }
}

/* This method retreives a form base on it's name. First is searchses by id xhtml fashion then html.
 * @author George Cojocariu
 */
function getForm(formIdentifier) {
    //xhtml
    var form = document.getElementById(formIdentifier);
    //try by name
    if (form == null) {
        //html
        form = document.forms.namedItem(formIdentifier);
    }
    return form;
}

/* This method submits the specified form. It is intended to be called from an event from a html tag. If the form is not
 * found it returns false thus submition will not occur.
 * @author George Cojocariu
 */
function submitForm(formIdentifier) {
    var result = true;
    var form = getForm(formIdentifier);

    if (form == null) {
        result = false;

    } else {
        form.submit();
    }

    return result;
}

/* This method submits the specified form with the specified action. It is intended to be called from an event from a
 * html tag. If the form is not found it returns false thus submition will not occur.
 * @author George Cojocariu
 */
function submitFormAction(formIdentifier, action) {
    var result = true;
    var form = getForm(formIdentifier);

    if (form == null) {
        result = false;

    } else {
        form.action = action;
        form.submit();
    }

    return result;
}

/* This method sets the attribute parameter with the value of the attributeValue and submits the specified form.
 * It is intended to be called from an event from a html tag. If the form is not found it returns false thus submition will not occur.
 * @author George Cojocariu
 */
function messageConfirmSubmitFormAction(message, formIdentifier, action) {
    if (confirm(message)){
        var form = getForm(formIdentifier);
        if (form != null) {
            form.action = action;
            form.submit();
        }
    }
}


