var dayMaxStandard   = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var dayMaxLeapYear = new Array(31,29,31,30,31,30,31,31,30,31,30,31);

function isLeapYear(year) {
    if ((year/4)   != Math.floor(year/4))   return false;
    if ((year/100) != Math.floor(year/100)) return true;
    if ((year/400) != Math.floor(year/400)) return false;
    return true;
}

function pad(number,length) {
    var str = '' + number;
    while (str.length < length)
        str = '0' + str;
    return str;
}

function setIso8601FromSelect(theForm,uniqueId) {
    var hours = theForm.elements[uniqueId+'hours'].value;
    var minutes = theForm.elements[uniqueId+'minutes'].value;
    var seconds = theForm.elements[uniqueId+'seconds'].value;
    var day = theForm.elements[uniqueId+'day'].value;
    var month = theForm.elements[uniqueId+'month'].value;
    var year = theForm.elements[uniqueId+'year'].value;
    // check for correct days
    var dayMax = dayMaxStandard;
    if (isLeapYear(year)) {
        dayMax = dayMaxLeapYear;
    }
    if (day > dayMax[month-1]) {
        day = dayMax[month-1];
        theForm.elements[uniqueId+'day'].value = day;
    }
    theDate = year+'-'+pad(month,2)+'-'+pad(day,2)+'T'+pad(hours,2)+':'+pad(minutes,2)+':'+pad(seconds,2);
    theForm.elements[uniqueId.substr(uniqueId.indexOf('_')+1)].value = theDate;
}

function initEditForm(form) {
    resizeAll(form);
}
// element object or string id
function resizeTextarea(element) {
    var element = $(element);
    element.setStyle({
        width: '100%'
    });
}

function resizeAll(form) {
    var elements = $(form).getElements();
    elements.each(function(element) {
        if (element.type == 'textarea') resizeTextarea(element);
    });
}

function popReload() {
    window.self.location = window.self.location.href;
}

function toggleContainerBorder(id) {
    if ($(id).getStyle('border') == 'red 1px solid') {
        $(id).setStyle({ border: 'red 0px solid' });
    } else {
        $(id).setStyle({ border: 'red 1px solid' });
    }
}

function getInnerText (object) {
    return (object.innerText) ? object.innerText : (object.textContent) ? object.textContent : "";
}

var closeTimer;
function catchAndClose () {
    var pageContent = document.body.innerHTML;
    pageContent = pageContent.replace(/^\s*|\n|\s*$/g,"");
    pageContent = pageContent.replace(/<script.*script>/gi,"");
    if (pageContent.length > 0) {
        document.write('<hr/>');
        document.write('<p>Closing window in <big><span id="timerValue">20</span></big> seconds.</p>');
        document.write('<p><input type="button" name="close" value="close" onClick="window.opener = null; window.close(); return true"> or <input type="button" name="stop" value="stop" onClick="closeTimer.stop()">.</p>');
        closeTimer = new PeriodicalExecuter(function(pe) {
            var seconds = $('timerValue').innerHTML;
            seconds = seconds - 1;
            $('timerValue').update(seconds);
	        if (seconds == 0) {
                window.opener = null;
	            window.close();
	        }
        }, 1);
    } else {
        window.opener = null;
        window.close();
    }
}