// warn user before allowing buttons of class="danger" to go ahead
//
function DangerLinkClick() {
    return confirm(this.title);
}

// Fill in filename, mimetype on file upload
//
function AttachFieldChange() {
    var path= this.value;
    var parts= path.split(new RegExp('[\\\\/]'));
    path= parts[parts.length-1];
    document.getElementById('f-title').value= path;
    parts= path.split('.');
    var mediatype= parts[parts.length-1].toLowerCase();
    if (mediatypes[mediatype])
        mediatype= mediatypes[mediatype]
    else
        mediatype= 'application/octet-stream';
    document.getElementById('f-mediatype').value= mediatype;
}
var mediatypes= {
    'txt': 'text/plain',
    'html': 'text/html',
    'css': 'text/css',
    'js': 'text/javascript',
    'xml': 'text/xml',
    'xhtml': 'application/xhtml+xml',
    'rtf': 'text/rtf',
    'ps': 'application/postscript',
    'eps': 'application/postscript',
    'doc': 'application/msword',
    'xls': 'application/vnd.ms-excel',
    'ppt': 'application/vnd.ms-powerpoint',
    'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    'xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    'pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
    'odt': 'application/vnd.oasis.opendocument.text',
    'odg': 'application/vnd.oasis.opendocument.graphics',
    'odp': 'application/vnd.oasis.opendocument.presentation',
    'ods': 'application/vnd.oasis.opendocument.spreadsheet',
    'svg': 'image/svg+xml',
    'gif': 'image/gif',
    'jpeg': 'image/jpeg',
    'jpg': 'image/jpeg',
    'png': 'image/png',
    'tiff': 'image/tiff',
    'tif': 'image/tiff',
    'bmp': 'image/bmp',
    'pdf': 'application/pdf',
    'zip': 'application/zip',
    'jar': 'application/java-archive',
    'tar': 'application/x-tar',
    'gz': 'application/x-gzip',
    'tgz': 'application/x-gzip',
    'm3u': 'audio/x-mpegurl',
    'mpeg': 'video/mpeg',
    'mpg': 'video/mpeg',
    'mp3': 'audio/mpeg',
    'flac': 'audio/flac',
    'wav': 'audio/x-wav',
    'ra': 'audio/x-realaudio',
    'ogg': 'audio/ogg',
    'ogv': 'video/ogg',
    'ogm': 'video/ogg',
    'flv': 'video/x-flv',
    'wma': 'audio/x-ms-wma',
    'wmv': 'audio/x-ms-wmv',
    'avi': 'video/x-msvideo'
}

// stop buttons with class="nodouble" from taking a double-click as two separate submissions
//
var NoDoubleClicker= Object.subclass();
NoDoubleClicker.prototype._init= function(element) {
    this.lastclick= 0;
    element.onclick= this.onclick.bind(this);
};
NoDoubleClicker.prototype.onclick= function(element) {
    var now= new Date().getTime();
    if (now<this.lastclick+1000)
        return false;
    this.lastclick= now;
    return true;
};


// Bind to appropriate elements
//
Array_fromSequence(document.getElementsByClassName('nodouble', 'input')).map(function(input) {
    new NoDoubleClicker(input);
});
Array_fromSequence(document.getElementsByClassName('danger', 'input')).map(function(input) {
    input.onclick= DangerLinkClick;
});
if (document.getElementById('f-attach'))
    document.getElementById('f-attach').onchange= AttachFieldChange;
