/**
 * Copyright (c) 2003-2005, Jannis Hermanns (on behalf the Serendipity Developer Team)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *     * Neither the name of the serendipity developer group nor the names of its contributors
 *       may be used to endorse or promote products derived from this software without
 *       specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/**
 *  Thanx a lot, my favolite blog system 's9y' developers.
 *  Every blogger really must use 's9y'. 
 *  Let's go to http://s9y.org/
 *
 *  Modified by MS-K 2008-05-15
 *  http://lab.till-daylight.org/
 */


function getfilename(value) {
	var re = /^.+[\/\\]+?(.+)$/;
	var al = '/[^0-9A-Za-z\._]/';
    var str = value.replace(re, "$1");

    if(str.search(/[^0-9A-Za-z\._]/) == -1){
        return str;
    } else {
        var d = new Date();
        var str = d.setTime(d) +'.jpg';
        return str;
    }
    // return value.replace(re, "$1");
}

isFileUpload = true;

/* 初期表示数 */
var fieldcount = 1;

/* 表示上限 */
var add_max = 5;

function addField() {
    if(fieldcount <= add_max) {
    
    fieldcount++;

    fields = document.getElementById('addforms').cloneNode(true);
    fields.id = 'upload_form_' + fieldcount;
    fields.style.display = 'block';

    // Get the DOM outline be uncommenting this:
    // document.getElementById('debug').innerHTML = showNodes(fields);

    // garvin: This gets a bit weird. Opera, Mozilla and IE all have their own numbering.
    // We cannot operate on "ID" basis, since a unique ID is not yet set before instancing.
    if (fields.childNodes[0].nodeValue == null) {
        // I.E, it does not have a linebreak as first element.
        userfile       = fields.childNodes[0].childNodes[0].childNodes[0].childNodes[1].childNodes[0];
        targetfilename = fields.childNodes[0].childNodes[0].childNodes[1].childNodes[1].childNodes[0];
        columncount    = fields.childNodes[1].childNodes[0];
    } else {
        // We have a browser which has \n's as their own nodes. Don't ask me.
        // Now let's check if it's Opera or Mozilla.
        if (fields.childNodes[1].childNodes[0].nodeValue == null) {
            // Opera.
            userfile       = fields.childNodes[1].childNodes[0].childNodes[0].childNodes[1].childNodes[0];
            targetfilename = fields.childNodes[1].childNodes[0].childNodes[1].childNodes[1].childNodes[0];
            columncount    = fields.childNodes[3].childNodes[1];
        } else if (fields.childNodes[1].childNodes[1].childNodes[0].childNodes[3] == null) {
        	// Safari.
            userfile       = fields.childNodes[1].childNodes[1].childNodes[0].childNodes[1].childNodes[0];
            targetfilename = fields.childNodes[1].childNodes[1].childNodes[1].childNodes[1].childNodes[0];
            columncount    = fields.childNodes[3].childNodes[1];
        } else {
            // Mozilla.
            userfile       = fields.childNodes[1].childNodes[1].childNodes[0].childNodes[3].childNodes[0];
            targetfilename = fields.childNodes[1].childNodes[1].childNodes[2].childNodes[3].childNodes[0];
            columncount    = fields.childNodes[3].childNodes[1];
        }
    }

    userfile.id   = 'userfile_' + fieldcount;
    // userfile.name = 'userfile[' + fieldcount + ']';
    userfile.name = 'userfile[]';

    targetfilename.id   = 'target_filename_' + fieldcount;
    // targetfilename.name = 'target_filename[' + fieldcount + ']';
    targetfilename.name = 'target_filename[]';

    columncount.id   = 'column_count_' + fieldcount;
    columncount.name = 'column_count[' + fieldcount + ']';

    iNode = document.getElementById('upload_form');
    iNode.parentNode.insertBefore(fields, iNode);
    }
}

var i;
var inputStorage = new Array();
function checkInputs() {
    for (i = 1; i <= fieldcount; i++) {
        if (!inputStorage[i]) {
            fillInput(i, i);
        } else if (inputStorage[i] == document.getElementById('target_filename_' + i).value) {
            fillInput(i, i);
        }
    }

}


function debugFields() {
    for (i = 1; i <= fieldcount; i++) {
        debugField('target_filename_' + i);
        debugField('userfile_' + i);
    }
}

function debugField(id) {
    alert(id + ': ' + document.getElementById(id).value);
}


function fillInput(source, target) {
    var useDuplicate = false;
    var sourceval;

    // First field is a special value for foreign URLs instead of uploaded files
    sourceval = getfilename(document.getElementById('userfile_' + source).value);

    if (sourceval.length > 0) {
        document.getElementById('target_filename_' + target).value = sourceval;
        inputStorage[target] = sourceval;
    }

    // Display filename in duplicate form as well!
    if (useDuplicate) {
        tkey = target + 1;

        if (!inputStorage[tkey] || inputStorage[tkey] == document.getElementById('target_filename_' + tkey).value) {
            document.getElementById('target_filename_' + (target+1)).value = sourceval;
            inputStorage[target + 1] = '~~~';
        }
    }
}
