function search_parameters(language)
{
    ids = [];
    x = 0;
    
    if (language == 'english')
    {
        language = 'english';
    }
    else
    {
        language = 'finnish';
    }
    
    for(var i = 0; i < $j("#search_groups")[0].options.length; i++)
    {
        if ($j("#search_groups")[0].options[i].selected == true)
        {
            ids[x] = $j("#search_groups")[0].options[i].value;
            x++;
        }
    }
    
    if (ids.length == 0)
    {
        ids[0] = "0";
    }
    
    $j.get("ajax_layer.php?language=" + language, { 'search_parameters[]': ids },
    function(data)
    {
        $j("#parameters").html(data);
    });
}

function toggle_parameters(id)
{
    id = "#" + id;
    $j(id).toggle();
}

function add_compare(id, language)
{
    id = parseInt(id);
    
    if (language == 'english')
    {
        language = 'english';
    }
    else
    {
        language = 'finnish';
    }
    
    $j.get("ajax_layer.php?language=" + language, { add_compare: id },
    function(data)
    {
        $j(data).find('product').each(function(){
            if ($j(this).find('error').text() == 1)
            {
                return;
            }
            
            model = $j(this).find('product_model').text()
            products_count = $j(this).find('product_count').text()
            lang = $j(this).find('lang').text()
            title = $j(this).find('title').text()
            
            $j("#compare_empty_tr").hide();
            $j("#compare_count").html(products_count);
            
            $j("#compare_sortable").append('<li id="compare_tr_' + id + '"><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td align="left"><img src="gfx/filler.gif" width="20" height="20" alt="" border="0" style="cursor: move;" /></td><td width="100%">' + model + '</td><td align="right" valign="top"><a href="javascript: remove_compare(' + id + ', 1, \'' + lang + '\');"><img src="gfx/bt_remove_comparebox.gif" border="0" /></a></td></tr></table></li>');
            
            button_src = lang == 'finnish' ? 'gfx/bt_poista.gif' : 'gfx/bt_remove.gif';
            $j("#compare_a_" + id).attr("href","javascript: remove_compare('" + id + "', 1, '" + lang + "')");
            $j("#compare_i_" + id).attr("src", button_src);
            $j("#compare_i_" + id).attr("title", title);
            
            $j("#compare_empty_li").remove();
            
            $j("#compare_sortable li").each(function(i, n)
            {
                if (i < 3)
                {
                    $j(this).addClass("compare_in_compare");
                }
                else
                {
                    $j(this).removeClass("compare_in_compare");
                }
            });
        });
    });
}

function remove_compare(id, no_table_update, language)
{
    id = parseInt(id);
    no_table_update = parseInt(no_table_update);
    
    if (language == 'english')
    {
        language = 'english';
    }
    else
    {
        language = 'finnish';
    }
    
    $j.get("ajax_layer.php?language=" + language, { remove_compare: id, no_table_update: no_table_update },
    function(data)
    {
        $j(data).find('product').each(function(){
            if ($j(this).find('error').text() == 1)
            {
                return;
            }
            
            products_count = $j(this).find('product_count').text()
            lang = $j(this).find('lang').text()
            title = $j(this).find('title').text()
            
            if (products_count == 0)
            {
                $j("#compare_empty_tr").show();
            }
            $j("#compare_count").html(escape(products_count));
            $j("#compare_tr_" + id).remove();
            
            button_src = lang == 'finnish' ? 'gfx/bt_lisaa.gif' : 'gfx/bt_add.gif';
            
            $j("#compare_a_" + id).attr("href","javascript: add_compare('" + id + "', '" + lang + "')");
            $j("#compare_i_" + id).attr("src", button_src);
            $j("#compare_i_" + id).attr("title", title);
            
            text = $j(this).find('text').text();
            if (text != '0')
            {
                $j("#compare_div").html(text);
            }
            
            $j("#compare_sortable li").each(function(i, n)
            {
                if (i < 3)
                {
                    $j(this).addClass("compare_in_compare");
                }
                else
                {
                    $j(this).removeClass("compare_in_compare");
                }
            });
        });
    });
}

function slot_selections(product_id, area, product_in_slot_id, language)
{
    productid = parseInt(product_id);
    product_in_slot_id = parseInt(product_in_slot_id);
    
    if (language == 'english')
    {
        language = 'english';
    }
    else
    {
        language = 'finnish';
    }
    
    if (area != 'b' && area != 'c')
    {
        return;
    }
    
    if (area == 'b')
    {
        another_value = $j("#slot_c").val();
        //$j("#slot_c_box").show();
        //$j("#slot_c_box_lower").show();
    }
    else
    {
        another_value = $j("#slot_b").val();
        $j("#slot_b_box").show();
        $j("#slot_b_box_lower").show();
    }
    another_value = parseInt(another_value);
    
    $j.get("ajax_layer.php?language=" + language, { product_id: product_id, slot: area, product_in_slot_id: product_in_slot_id, another_value: another_value },
    function(data)
    {
        if (data.length == 0)
        {
            //return;
            if (area == 'b')
            {
                $j("#slot_c_box").hide();
                $j("#slot_c_box_lower").hide();
            }
            else
            {
                $j("#slot_b_box").hide();
                $j("#slot_b_box_lower").hide();
            }
        }
        else
        {
            if (area == 'b')
            {
                $j("#slot_c_box").show();
                $j("#slot_c_box_lower").show();
            }
            else
            {
                $j("#slot_b_box").show();
                $j("#slot_b_box_lower").show();
            }
        }
        
        if (area == 'b')
        {
            $j("#slot_c").html(data);
        }
        else
        {
            $j("#slot_b").html(data);
        }
    });
}

function confirm_cancel_order(text)
{
    var info = text;
    if (confirm(info))
    {
        return true;
    }
    else
    {
        return false;
    }
}

function update_shipping(country)
{
    if (country == 'FIN')
    {
        $j('img#shipping').attr("src","gfx/itella.png");
    }
    else
    {
        $j('img#shipping').attr("src","gfx/tnt.png");
    }
}

function is_vat_required(company, country)
{
    if (company.length > 0)
    {
        $j.get("ajax_layer.php", { vat_country: country },
        function(data)
        {
            if (data.length > 0)
            {
                if (data == 'true')
                {
                    $j("#vat_req").css("display", "inline");
                }
                else
                {
                    $j("#vat_req").css("display", "none");
                    $j("#vat_style").attr("class", "orderer_details");
                }
            }
        });
    }
    else
    {
        //company empty, vat is not required
        $j("#vat_req").css("display", "none");
        $j("#vat_style").attr("class", "orderer_details");
    }
}

function toggle_months(month)
{
    month = parseInt(month);
    $j("#month_" + month).toggle();
}
