function onDateSelected(event,dates,test){
    fieldId = new String(this.id);
    fieldId = fieldId.substring(11);
    var date = this.getSelectedDates()[0];
    var format = new String(this.NxcDateFormat['format']);
    format = format.replace(/Y/,date.getFullYear());
    format = format.replace(/y/,date.getYear());
    var month = date.getMonth()+1;
    format = format.replace(/m/,(month < 10 ? "0" + month : month));
    format = format.replace(/n/,month);
    format = format.replace(/d/,(date.getDate() < 10 ? "0" + date.getDate() : date.getDate()));
    format = format.replace(/j/,date.getDate());

    document.getElementById(fieldId).value = format;
    this.destroy();
    var command = 'window.' + this.id + ' = null;';
    eval(command);
}
function showCalendar(id,calObj,format,button) {
    if(calObj != null) {
        /* Hide the calendar */
        calObj.destroy();
        calObj = null;
    }
    else
    {
        /* Create Yahoo calendar */
        calObj = new YAHOO.widget.Calendar(
            "yuiCalendar"+id,
            "yui-cal-"+id
        );

        /* Set the calendar according to the format set in the Visma settings in eZ publish */
        calObj.cfg.setProperty("DATE_FIELD_DELIMITER", format['delimiter']);
        calObj.cfg.setProperty("MDY_DAY_POSITION", format['MDY_DAY_POSITION']);
        calObj.cfg.setProperty("MDY_MONTH_POSITION", format['MDY_MONTH_POSITION']);
        calObj.cfg.setProperty("MDY_YEAR_POSITION", format['MDY_YEAR_POSITION']);
        calObj.cfg.setProperty("MD_DAY_POSITION", format['MD_DAY_POSITION']);
        calObj.cfg.setProperty("MD_MONTH_POSITION", format['MD_MONTH_POSITION']);
        calObj.cfg.setProperty("MY_MONTH_POSITION", format['MY_MONTH_POSITION']);
        calObj.cfg.setProperty("MY_YEAR_POSITION", format['MY_YEAR_POSITION']);

        calObj.cfg.setProperty("MONTHS_SHORT",   format['MONTHS_SHORT']);
        calObj.cfg.setProperty("MONTHS_LONG",    format['MONTHS_LONG']);
        calObj.cfg.setProperty("WEEKDAYS_1CHAR", format['WEEKDAYS_1CHAR']);
        calObj.cfg.setProperty("WEEKDAYS_SHORT", format['WEEKDAYS_SHORT']);
        calObj.cfg.setProperty("WEEKDAYS_MEDIUM",format['WEEKDAYS_MEDIUM']);
        calObj.cfg.setProperty("WEEKDAYS_LONG",  format['WEEKDAYS_LONG']);

        /*
            TODO:
            Find a way to localise the following info:
            YAHOO.widget.Calendar.DEFAULT_CONFIG={strings:{month:"Month",year:"Year",submit:"Okay",cancel:"Cancel",invalidYear:"Year needs to be a number"},monthFormat:YAHOO.widget.Calendar.LONG,initialFocus:"year"};
        */

        /* Store format (used when setting the selected date back into the text input) */
        calObj.NxcDateFormat = format;

        /* Get value from text field */
        var value = new String(document.getElementById(id).value);

        /* Enable navigator to move more quickly in the calendar */
        /* The navigator i accessible when clicking on the month and year in the top of the calendar. */
        if(format['navigator'] == true) {
            calObj.cfg.setProperty("navigator", true);
        }

        /* Set the date range parameter to * if the format is using - as the delimiter */
        if(format['delimiter'] == "-") {
            calObj.cfg.setProperty("DATE_RANGE_DELIMITER", '*');
        }
        /* Set the date delimiter parameter to = if the format is using , as the delimiter */
        if(format['delimiter'] == ",") {
            calObj.cfg.setProperty("DATE_DELIMITER", '=');
        }

        /* Set default selected date (current selcted date from text field) */
        if(value.length > 0) {
            calObj.cfg.setProperty("selected", value.toString());
        }

        /* Calculate correct month to display */
        var page = false;
        if(value.length > 0) {
            var parts = value.split(format['delimiter']);
            var year = parts[format['MDY_YEAR_POSITION']-1];
            var month = parts[format['MDY_MONTH_POSITION']-1];
            if(format['MY_MONTH_POSITION'] > format['MY_YEAR_POSITION']) {
                page = year + format['delimiter'] + month;
            } else {
                page = month + format['delimiter'] + year;
            }
        } else {
            var today = new Date();
            if(format['MY_MONTH_POSITION'] > format['MY_YEAR_POSITION']) {
                page = today.getFullYear() + format['delimiter'] + (today.getMonth()+1);
            } else {
                page = (today.getMonth()+1) + format['delimiter'] + today.getFullYear();
            }
        }
        if(page) {
            calObj.cfg.setProperty("pagedate", page);
        }

        /* Set handler for handling the user selecting a new date */
        calObj.selectEvent.subscribe(onDateSelected,calObj,true);

        /* Render (show) the calendar! */
    	calObj.render();
    }
    /* Return the Calendar object (should be stored by the calling button and return later to be able to close it) */
    return calObj;
}

/**
 * Masks out chars that is not legal in floats
 **/
function OnKeyPressFloat(e) {
    var evt = (e) ? e : (window.event) ? window.event : null;

    if(evt) {
        var key = (evt.charCode) ? evt.charCode : ( (evt.keyCode) ? evt.keyCode : ( (evt.which) ? evt.which : 0 ) );
        var char = String.fromCharCode(key);

        switch(char) {
            case '.':
            case ',':
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                return true;
            case 'x':
            case 'v':
            case 'v':
                if(evt.ctrlKey) {
                    return true;
                }
                return false;
            default:
                switch(key) {
                    case 8:
                    case 9:
                    case 37:
                    case 38:
                    case 39:
                    case 40:
                    case 46:
                        return true;
                    default:
                        return false;
                }
        }
    }
}

/**
 * Masks out chars that is not legal in integers
 **/
function OnKeyPressInteger(e) {
    var evt = (e) ? e : (window.event) ? window.event : null;

    if(evt) {
        var key = (evt.charCode) ? evt.charCode : ( (evt.keyCode) ? evt.keyCode : ( (evt.which) ? evt.which : 0 ) );
        var char = String.fromCharCode(key);

        switch(char) {
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                return true;
            case 'x':
            case 'v':
            case 'v':
                if(evt.ctrlKey) {
                    return true;
                }
                return false;
            default:
                switch(key) {
                    case 8:
                    case 9:
                    case 37:
                    case 38:
                    case 39:
                    case 40:
                    case 46:
                        return true;
                    default:
                        return false;
                }
        }
    }
}
/**
 * Insert 0 in numeric fields if users removes the value
 **/
function OnNumericChanged(element)
{
    if(element.value == '') {
        element.value = '0';
    }
}
