function show(ob){
	document.getElementById(ob).style.display = "block";
}
function hide(ob){
	self.document.getElementById(ob).style.display = "none";
}

function searchIMDB()
{
    movieName = document.getElementById('imdb').value;
    window.open('http://www.imdb.com/find?s=all&q='+encodeURI(movieName));
}

showFilenameIn = new Array(4, 6, 8, 9, 12, 13, 20, 21, 22, 23, 26, 27, 30, 31, 32);
function checkInArray(searchFor, searchIn)
{
    for (i=0; i<searchIn.length; i++)
    {
        if (searchIn[i] == searchFor) return 1;
    }
    return 0;
}

uploadFormOK = 0;
function checkUploadForm()
{
    return uploadFormOK;
}

var numAdded = 0;
function category_change()
{
    var sel = document.getElementById("category");
    var table = document.getElementById('utable');

    uploadFormOK = 0;
    catId = sel.options[sel.selectedIndex].value;

    //hide fields if any
    if (numAdded)
    {
        for (i=0; i<numAdded; i++)
        {
            table.deleteRow(4);
        }
    }

    numAdded = 0;
    //end hiding


    if (catId > 0)
    {
        if (checkInArray(catId, showFilenameIn))
        {
            show('filename_title');
            show('filename_field');
        }
        else
        {
            hide('filename_title');
            hide('filename_field');
        }

        document.getElementById('loader').innerHTML = '<img src="/images/loadinfo.net.gif" border="0" />';
        agent.call('/ajax.php','category_change','callback_category_change', catId);
    }
    else
    {
        hide('desc_title');
        hide('desc_field');
        hide('filename_title');
        hide('filename_field');
    }
}

function callback_category_change(fields)
{
    table = document.getElementById('utable');

    for (i=0; i<fields.length; i++)
    {
        cf = fields[i];
        if (cf.field == 'general')
        {
            document.getElementById('desc_title').innerHTML = cf.name;
            show('desc_title');
            show('desc_field');
        }
        else
        {
            tr1 = table.insertRow(4);
            htd = tr1.insertCell(-1);
            if (!htd) alert('bad');
            htd.className = 'header';
            htd.innerHTML = cf.name;
            otd = tr1.insertCell(-1);
            if (!otd) alert('bad2');
            otd.className = 'lista';
            otd.innerHTML = cf.field;

            numAdded++;
        }
    }

    uploadFormOK = 1;
    document.getElementById('loader').innerHTML = '';
}

currentOpenPreview = '';
previews = new Array();
function showPreview(hash)
{
    if (currentOpenPreview == hash)
    {
        hide(hash);
        currentOpenPreview = '';
        return;
    }

    if (currentOpenPreview != '')
    {
        hide(currentOpenPreview);
        currentOpenPreview = '';
    }

    if (previews[hash])
    {
        show(hash);
        currentOpenPreview = hash;
        return;
    }

    agent.call('/ajax.php','showPreview','callback_showPreview', hash);
}

function callback_showPreview(previewObj)
{
    hash = previewObj['hash'];
    content = previewObj['content'];

    show(hash);
    document.getElementById(hash).innerHTML = content;

    currentOpenPreview = hash;
    previews[hash] = 1;
}