function urlencode (str) {
    str = (str + '').toString();

    // Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current
    // PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following.
    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
    replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}


function ticket_change_type()
{
    var type = $('#ticket-type').val();

    $.fancybox.showActivity();
    $.ajax(
    {
        type: "POST",
        cache: false,
        url: "soap/ticket/",
        data: 'op=change_type',
        success: function(html)
        {
            $.fancybox.hideActivity();
            ticket_update_content(html);
        }
    });
}

function ticket_summary_show()
{
    $("div.top_login").animate(
    {
        top: "12px",
    }, 'slow', function()
    {
        $('.top_top_login').show('slow');
    });
}

function ticket_summary_hide()
{
    $('.top_top_login').hide('slow', function()
    {
        $("div.top_login").animate(
        {
            top: "0px",
        }, 'slow');
    });
}

function ticket_remove_item(index, id_event)
{
    $.fancybox.showActivity();
    $.ajax(
    {
        type: "POST",
        cache: false,
        url: "soap/ticket/",
        data: 'index=' + index + '&op=remove',
        success: function(html)
        {
            $.fancybox.hideActivity();

            $('td[title=' + id_event + ']').removeClass('hover-persistent');

            ticket_update_content(html);        
        }
    });
}

function ticket_update()
{
    amount = $('#ticket-button-miza').val();

    $.fancybox.showActivity();
    $.ajax(
    {
        type: "POST",
        cache: false,
        url: "soap/ticket/",
        data: 'amount=' + amount,
        success: function(html)
        {
            $.fancybox.hideActivity();

            ticket_update_content(html);
        }
    });
}

function ticket_update_content(html)
{
    if (html != '')
        $('.top_top_login').html(html);
    return false;
}

function ticket_update_ajax()
{
    $.fancybox.showActivity();
    $.ajax(
    {
        type: "POST",
        cache: false,
        url: "soap/ticket/",
        data: '',
        success: function(html)
        {
            ticket_update_content(html);
            $.fancybox.hideActivity();
        }
    });
}

function ticket_print(serial)
{
    window.open(base_url + '/soap/ticket/print', 'bilet', 'width=850,height=700,toolbar=yes, location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes, resizable=yes');

    return false;
}

function ticket_save()
{
	if (is_user == 0) //redirect user to login form, do not add to ticket
    {
        $('a.top_log').click();
        return false;
    }
	
    $.fancybox.showActivity();
    $.ajax(
    {
        type: "POST",
        cache: false,
        url: "soap/ticket/",
        data: 'op=save',
        success: function(html)
        {
            $.fancybox.hideActivity();

            ticket_update_content(html);        
        }
    });    
}

function ticket_delete()
{
    $.fancybox.showActivity();
    $.ajax(
    {
        type: "POST",
        cache: false,
        url: "soap/ticket/",
        data: 'op=delete',
        success: function(html)
        {
            $.fancybox.hideActivity();

            $('td.hover-persistent').removeClass('hover-persistent');
            ticket_update_content(html);
            ticket_hide();
        }
    });
}

function ticket_show(html)
{
    if (ticket_is_open())
        return false;

    $("div.top_top_top_login").fadeOut('slow', function()
    {
        $("div.top_login").animate(
        {
            top: "290px",
        }, 'slow', function()
        {            
            $('.top_top_login').fadeIn('slow');
            $('.top_top_login').addClass('is_visible');
            $('.top_bilet').toggleClass('hover');
            //$('.top_bilet').toggleClass('up');
        });
    });
}

function ticket_hide()
{
    $('.top_top_login').fadeOut('slow', function()
    {
        $("div.top_login").animate(
        {
            top: "15px",
        }, 'slow', function()
        {
            $("div.top_top_top_login").fadeIn('slow');
            $('.top_top_login').removeClass('is_visible');
            $('.top_bilet').toggleClass('hover');
            ticket_update_summary();
        });
    });
}

function ticket_is_open()
{
    return $('.top_top_login').hasClass('is_visible');
}

function toggle_combined()
{
    if ($('#ticket-combined:checked').val() !== undefined)
        is_combined = 1;
    else
        is_combined = 0;

    //alert(is_combined);

    $.fancybox.showActivity();
    $.ajax(
    {
        type: "POST",
        cache: false,
        url: "soap/ticket/",
        data: 'op=change_type&is_combined=' + is_combined,
        success: function(html)
        {
            $.fancybox.hideActivity();
            //alert('ceva');
            ticket_update_content(html);
        }
    });
}

function toggle_banker(index)
{
    $.fancybox.showActivity();
    $.ajax(
    {
        type: "POST",
        cache: false,
        url: "soap/ticket/",
        data: 'op=toggle_banker&index=' + index,
        success: function(html)
        {
            $.fancybox.hideActivity();
            ticket_update_content(html);
        }
    });
}

function toggle_system(index)
{
    $.fancybox.showActivity();
    $.ajax(
    {
        type: "POST",
        cache: false,
        url: "soap/ticket/",
        data: 'op=toggle_system&index=' + index,
        success: function(html)
        {
            $.fancybox.hideActivity();
            ticket_update_content(html);
        }
    });
}

function ticket_display_toggle()
{
	if (!ticket_is_open())
    {
        if ($('#top_top_bilet').html() == null)
            ticket_update_ajax();

        ticket_show();
    }
    else
        ticket_hide();
    return false;
}

function ticket_check_input()
{
	return false;
}

/*----------------------------------------------------------------------*/
/*--- VIRTUAL TICKET ---*/
$(document).ready(function()
{
    $('table.offer_table > tbody > tr > td').click(function()
    {
        var el = $(this);
        var el_parent = $(this).parent();

        //MINIMAL VALIDATION
        if(el_parent.hasClass('tr_offer')) return false;
        
        if (is_user == 0) //redirect user to login form, do not add to ticket
        {
            //$('a.top_log').click();
            //return false;
        }
        
        var cota = $(this).html();

        if (cota.indexOf('-') != -1)
            return false;
            
        if (cota.indexOf('|') != -1)
            return false;            

        id_long = $(this).attr('headers');
        id_arr = id_long.substr(4, id_long.length);
        table_ids = id_arr.split('_');

        if ($(this).parent().hasClass('tr_offer'))
            return false;

        //var Rang = $('th[headers=ht_' + table_ids[0] + '_' + table_ids[1] + '_' + table_ids[2] + '] span').html().replace(/<\/?[^>]+>/gi, '');
        var Rang = $('th[headers=ht_' + table_ids[0] + '_' + table_ids[1] + '_' + table_ids[2] + '] span').text();
        Rang = urlencode(Rang);
        //alert(Rang);
        /*
        var EventID = $('td[id=ht_' + table_ids[0] + '_' + table_ids[1] + ']').attr('headers');
        */
        var EventID = $(this).attr('title');

        //$(this).addClass('hover-persistent');

        $.fancybox.showActivity();
        $.ajax(
        {
            type: "POST",
            cache: false,
            url: "soap/ticket/",
            data: 'EventID=' + EventID + '&Rang=' + Rang + '&op=add',
            success: function(html)
            {
                $.fancybox.hideActivity();

                // add or remove hover persisten classes
                el_parent.find('td').removeClass('hover-persistent');
                el.addClass('hover-persistent');

                ticket_update_content(html);
                ticket_show(html);                
                //if(ticket_is_empty)
                	//ticket_show(html);
                //else
                //ticket_update_summary();
                
            //                $('div.top_top_login center').blink(
            //                {
            //                    maxBlinks: 3
            //                });

            }
        });
    });

    $('.top_bilet').click(function()
    {
        if (!ticket_is_open())
        {
            //alert($('#top_top_bilet').html());
            if ($('#top_top_bilet').html() == null)
                ticket_update_ajax();

            ticket_show();
        }
        else
            ticket_hide();
        return false;
    });
    
    $('div#top-ticket-summary2').click(function()
    {
    	ticket_display_toggle();
    });
});
