function textCounter (field, maxlimit)
{
    if (field.value.length > maxlimit)
    {
        field.value = field.value.substring (0, maxlimit);
        field.blur();
        field.focus();
        return false;
    }
    else
    {
        document.getElementById('left').innerHTML = maxlimit - field.value.length;
    }
}

function ChangeFieldCat ()
{
    var sel = document.getElementById('Cat');

    window.location.href='/Admin/Consultation/Fields/'+sel[sel.selectedIndex].value+'.html';
}

function GetFields ()
{
    var sel = document.getElementById('Cat');

    if (sel[sel.selectedIndex].value != 0)
    {
        var r = new Ajax2 ('/Admin/Consultation/GetFields/'+sel[sel.selectedIndex].value+'.html', '', '', UpdateFields);
        r.makeRequest ('', '');
    }
}

function UpdateFields (response, args)
{
    var container = document.getElementById('FieldContainer');
    var fields = response.getElementsByTagName('Field');

    if (container.childNodes.length > 0)
    {
        for (d = container.childNodes.length-1; d >= 0; d--)
        {
            container.removeChild(container.childNodes[d]);
        }
    }

    for (i = 0; i <= fields.length-1; i++)
    {
        id = fields[i].getAttribute('Id');
        name = fields[i].firstChild.data;

        l = document.createElement('label');
        l.setAttribute('for', 'field_'+id);
        l.innerHTML = name+':';
        l.setAttribute('style', 'display: block; width: 130px; float: left; margin-bottom: 5px; text-align: right; padding-right: 5px;');

        a = document.createElement('input');
        a.setAttribute('type', 'text');
        a.setAttribute('name', 'field['+id+']');
        a.setAttribute('id', 'field_'+id);
        a.setAttribute('style', 'display: block; width: 250px; float: left; margin-bottom: 5px;');

        b = document.createElement('br');
        b.setAttribute('style', 'clear: left;');

        container.appendChild(l);
        container.appendChild(a);
        container.appendChild(b);
    }
}

function InitFiles ()
{
    var mf = new MultiFile (document.getElementById('fileList'), 10);
    mf.add (document.getElementById('dummy'));
}

function MultiFile (target, max)
{
    this.target = target;
    this.count = 0;
    this.id = 0;

    if (max)
    {
        this.max = max;
    }
    else
    {
        this.max = -1;
    }

    this.add = function (element)
    {
        if (element.tagName == 'INPUT' && element.type == 'file')
        {
            element.multifile = this;

            if (this.max != -1 && this.count >= this.max)
            {
                element.disabled = 'disabled';
            }

            element.onchange = function ()
            {
                ext = element.value.substr(element.value.lastIndexOf('.')+1, (element.value.length-1)-element.value.lastIndexOf('.')+1);

                if (ext.toLowerCase() == 'jpg' || ext.toLowerCase() == 'gif' || ext.toLowerCase() == 'png')
                {
                    element.name = 'file_' + element.multifile.id++;

                    var new_element = document.createElement('input');
                    new_element.type = 'file';
                    new_element.setAttribute ('class', 'fileinput');
                    new_element.className = 'fileinput';

                    element.parentNode.insertBefore (new_element, element);
                    element.multifile.add(new_element);
                    element.multifile.updateList(element);
                    element.style.display = 'none';
                }
                else
                {
                    alert ('File is not an image!');
                    element.value = '';
                    return false;
                }
            }

            this.count++;
            this.current = element;
        }
        else
        {
            alert ('Element is not file type!');
        }
    }

    this.updateList = function (element)
    {
        var hr = document.createElement('hr');
        hr.className = 'separator';
        var new_row = document.createElement('div');
        var delB = document.createElement('input');
        delB.setAttribute('type', 'image');
        delB.setAttribute('src', '/Images/delete.gif');
        delB.setAttribute('alt', 'Trinti');
        delB.style.border = 0;
        delB.style.verticalAlign = 'bottom';
        delB.style.marginRight = '10px';
        new_row.element = element;

        delB.onclick = function ()
        {
            this.parentNode.parentNode.removeChild(this.parentNode.nextSibling);
            this.parentNode.element.parentNode.removeChild(this.parentNode.element);
            this.parentNode.parentNode.removeChild(this.parentNode);
            this.parentNode.element.multifile.count--;
            this.parentNode.element.multifile.current.disabled = false;
            return false;
        }

        if (element.value.indexOf('\\') != -1)
        {
            fname = element.value.substr(element.value.lastIndexOf('\\')+1, (element.value.length-1)-element.value.lastIndexOf('\\')+1);
        }
        else
        {
            fname = element.value.substr(element.value.lastIndexOf('/')+1, (element.value.length-1)-element.value.lastIndexOf('/')+1);
        }

        var file = document.createElement('span');
        var fileT = document.createTextNode(fname);
        file.appendChild(fileT);
        new_row.appendChild(delB);
        new_row.appendChild(file);
        this.target.appendChild(new_row);
        this.target.appendChild(hr);
    }
}

var PhotoArray = new Array;
var Total = 0;

function BuildPhotoArray ()
{
    var div = document.getElementById('upload');

    for (i = 0; i < div.childNodes.length; i++)
    {
        if (div.childNodes[i].className == 'photoFrame')
        {
            var id = div.childNodes[i].id;
            var r_id = id.substr (6, id.length-5);
            PhotoArray['p'+r_id] = r_id;
            Total++;
        }
    }
}

function RemovePhoto (id, itemID)
{
    if (confirm ('Tikrai ištrinti?'))
    {
        var r = new Ajax2 ('/Admin/Consultation/RemovePhoto.html', 'PID='+PhotoArray['p'+id]+'&Item='+itemID, '', RPReady);
        r.makeRequest (1, id);
    }
}

function RPReady (response, id)
{
    var pdiv = document.getElementById('photo_'+id);

    for (i = 1; i <= Total; i++)
    {
        if (i > id)
        {
            PhotoArray['p'+i] = PhotoArray['p'+i]-1;
        }
    }

    pdiv.parentNode.removeChild(pdiv);
}

var images = new Array(3);
var links = new Array(3);
var fitems = new Array(3);
var iindex = 2;

function ChangePhoto ()
{
    var holder = document.getElementById('Pic');
    var load = document.getElementById('Loading');
    holder.style.opacity = 1;

    new Effect.Opacity('Pic', {duration:1.0, from:1.0, to:0.0});

    var r = new Ajax2 ('/Consultation/GetRandomPhoto.html', '', '', DoFade);
    r.makeRequest ();
}

function DoFade (response)
{
    var holder = document.getElementById('Pic');
    var load = document.getElementById('Loading');
    var item = response.getElementsByTagName('Item');

    holder.style.opacity = 0;

    RemoveC();

    iindex = item.length-1;
    for (i = 0; i <= item.length-1; i++)
    {
        images[i] = new Image();
        images[i].src = '/Uploads/Consultation/item_'+item[i].getAttribute('id')+'T_'+item[i].getAttribute('index')+'.png';

        links[i] = document.createElement('a');
        links[i].href = '/Consultation/View/'+item[i].getAttribute('cat')+'.html';

        links[i].appendChild(images[i]);
    }

    holder.appendChild(links[iindex]);
    iindex = iindex-1;

    new Effect.Opacity('Pic', {duration:1.0, from:0.0, to:1.0});

    setTimeout('DoInt();', 4000);
}

function DoInt ()
{
    if (iindex >= 0)
    {
        var holder = document.getElementById('Pic');

        new Effect.Opacity('Pic', {duration:1.0, from:1.0, to:0.0});
        setTimeout('DoInt2()', 1000);
    }
    else
    {
        ChangePhoto();
    }
}

function DoInt2 ()
{
    var holder = document.getElementById('Pic');

    if (iindex >= 0)
    {
        RemoveC();
        holder.style.opacity = 0;
        holder.appendChild(links[iindex]);
        iindex = iindex-1;

        new Effect.Opacity('Pic', {duration:1.0, from:0.0, to:1.0});

        setTimeout('DoInt();', 4000);
    }
}

function RemoveC ()
{
    var holder = document.getElementById('Pic');

    if (holder.childNodes.length > 0)
    {
        for (d = holder.childNodes.length-1; d >= 0; d--)
        {
            holder.removeChild(holder.childNodes[d]);
        }
    }
}

function Photos (count, id)
{
    var thumb = document.getElementById('Thumb_'+id);
    var num = document.getElementById('current_'+id);

    this.current = 1;

    this.next = function ()
    {
        if (this.current == count)
        {
            next = 1;
        }
        else
        {
            next = this.current+1;
        }

        this.current = next;
        num.innerHTML = this.current;

        thumb.src = '/Uploads/Consultation/item_'+id+'T_'+next+'.png';
    }

    this.previous = function ()
    {
        if (this.current == 1)
        {
            previous = count;
        }
        else
        {
            previous = this.current-1;
        }

        this.current = previous;
        num.innerHTML = this.current;

        thumb.src = '/Uploads/Consultation/item_'+id+'T_'+previous+'.png';
    }

    this.preview = function ()
    {
        window.open('/Uploads/Consultation/item_'+id+'_'+this.current+'.png');
    }
}