/******************************************
|   Flashupload Functions for Stylish template
|   ========================================
|   by Stephen Yabziz
|   (c) 2005-2006 YABSoft Services
|   http://www.yabsoft.com
|   ========================================
******************************************/
var debug      = 0;
//upload start time
var upStart    = 0;
//total bytes uploaded
var TotalBytes = 0;
//avoid repeat email sending
var FlashDone  = 0;
var ErrorDetected = 0;
var storeData = new Array();
var hasFlashUploads = false;
var flashItemId = null;
function clearFileQueue(id) {
    var tbody = document.getElementById(id);
    while (tbody.childNodes.length > 0) {
       tbody.removeChild(tbody.firstChild);
    }
}
function displayFileQueue(id,data)
{
    clearFileQueue(id);
    storeData = data;
    number = data.length;
    hasFlashUploads = number>0;
    for (i=0; i<number; i=i+1) {
       var theTableBody = document.getElementById(id);
       var newRow = theTableBody.insertRow(0);
       if (i%2!=0) { newRow.className="altrow"; }
       var name = data[i]['name'];
       var size = convertsize(data[i]['size']);

       var newCell;
       newCell = newRow.insertCell(0);
       newCell.innerHTML = '<img src="'+skinWeb+'/images/file.jpg" border=0/>';
       newCell = newRow.insertCell(1);
       newCell.innerHTML = name;
       newCell = newRow.insertCell(2);
       newCell.innerHTML = size;
       newCell = newRow.insertCell(3);
       newCell.innerHTML = '<a href="#" onclick="'+"flashObj('flashUpload').removeFileAt(" + i + ');return false;"><img src="'+skinWeb+'/images/delete.gif" alt="Delete" border="0"/></a><a href="#up" onclick="if(show_options==\'block\') {toggleFlash('+ i +');return false;} else{flashItemId ='+ i +'; return showPopupWindow(this, \'flash_options\');}" title="Options"><img src="'+skinWeb+'/images/options.gif" alt="Options" border="0"/></a>';
       //toggleFlash(i);
    }
}
function JSRecCmd(str,vars)
{
    //alert(str+vars[1]);
    if(str=='error')
    {
        if(vars[0].indexOf("http response code:302")!=-1) return; 
        ErrorDetected = 1;
        //if all files fail to upload,reset upload interface
        if(vars[1]==1)
        {
            document.getElementById('uploaderror').style.display='';
            document.getElementById('uperror').innerHTML=vars[0];
            resetUploadInterface();
        }
        //show errors
        alert(vars[0]);
        return '';
    }
    if((str=='complete'||str=='done')&&FlashDone==0)
    {
        FlashDone = 1;

        calProgress(vars[0],vars[1],upStart);
        flashObj("flashUpload").removeAllFiles();
        submitEmailForm();

        return '';
    }
    if(str=='start')
    {
        var myDate=new Date()
        upStart = parseInt(myDate.getTime()/1000)
        calProgress(vars[0],0,upStart);
        return '';
    }
    if(str=='up')
    {
        calProgress(vars[0],vars[1],upStart);
        return '';
    }
    if(str=='add'||str=='del'||str=='rm')
    {
        document.getElementById('uploadlist').style.display = 'block';
        displayFileQueue('uploadlistbody',vars);
        return '';
    }
    if(!debug) return;
    for(var key in vars)
    {
        if(typeof(vars[key])=='object')
        {
            for(var subkey in vars[key])
            {
                if(debug) alert(subkey+"="+vars[key][subkey]);
            }
        }
        else
        {
            if(debug) alert(key+"="+vars[key]);
        }
    }
}
function buildFlashUpload(flashPath)
{
    //if(typeof flashObj('flashUpload') != 'undefined') return '';

    var so = new SWFObject(flashPath, "flashUpload", "79", "30", "8", "#FFF",0); 
    so.addParam('allowScriptAccess','always');
    if(!so.write("flashuploadobj"))
    {
        $('#flashversion').show();
    }
}
function toggleFlash(id)
{
    if(id==flashItemId)
    {
        $('#flash_options').toggle();
    }
    else
    {
        $('#flash_options').show();
    }
    flashItemId = id;

    $('#flash_name').html(storeData[id]['name']);

    var fs = new Array();
    fs = custom_fields.split(',');
    for(var i=0;i<fs.length;i++)
    {
        if(is_object('flash_'+fs[i]))
        $('#flash_'+fs[i]).val( storeData[flashItemId][fs[i]]==null ? '' : storeData[flashItemId][fs[i]] );
    }

}
function save2Flash()
{
    var fs = new Array();
    fs = custom_fields.split(',');
    for(var i=0;i<fs.length;i++)
    {
        if(is_object('flash_'+fs[i]))
        storeData[flashItemId][fs[i]] = $('#flash_'+fs[i]).val();
    }

    flashObj('flashUpload').saveData(storeData);

    $('#flash_options').hide();
}
function isDefined(property) {
   return (typeof property != 'undefined');
}
