var open_id = '';
var total_summ = '';
function Accordion(id, next)
{
	//alert($('#price_id_'+id+'_content').css('display'));
	
   	if($('#price_id_'+id+'_content').html() == '')
   	{
   		$.post("/?mid=3&ajax=1&cat_id="+id, {},
   		function(data){
   				$('#price_id_'+id+'_content').html(data);
   		});
   		$('#catalog_description').hide('fast');
        if(!next)
        {
            ShowLoader(id);
            return;
        }
   	}
    
    
    $('#price_id_'+id+'_content').toggle('slow');
    
    if(!next)
    {
    	$('#catalog_description').toggle('fast');
    }
               
    if($('#price_id_'+id+'_content_load').html() != '')
    {
        $('#price_id_'+id+'_content_load').toggle('slow');
        $('#price_id_'+id+'_content_load').empty();
        $('#catalog_description').hide('fast');
    }
   	
   	if(open_id != id)
   	{
   		$('#price_id_'+open_id+'_content').hide();
   		$('#catalog_description').hide('fast');
   	}
   	open_id = id;
   	
   	if($('#price_id_'+id+'_content').css('display') == 'block')
    {
    	$('#content').stop().scrollTo('#to_ancor_'+id,  500);
    }
}

function ShowLoader(id)
{
    $('#price_id_'+id+'_content_load').html('<img src="/themes/default/images/ajax-loader.gif" />');
    $('#price_id_'+id+'_content_load').toggle('fast');
    setTimeout("Accordion("+id+", 'next')", 1000);
}

function AddTovar(id, kolvo_fixed)
{
	if(kolvo_fixed)
	{
		kolvo = kolvo_fixed;
		good_kolvo = 1;
	}
	else
	{
		kolvo = $("#kolvo_"+id).val();
		reg = /^[0-9]+$/;
		good_kolvo = reg.exec(kolvo);	
	}
		 
	if(good_kolvo < 1)
	{
		alert('Чтобы добавить товар, необходимо указать количество');
		return false;
	}
	// Делаем input неактивным
	if(!kolvo_fixed)
	{
		$("#kolvo_"+id).addClass('input_grey');	
	}
	
	
	$("#pre_order_header").show();
	// Получаем наименование
	tovar_name = $("#name_"+id).html();
	// Получаем цену
	tovar_cena = $("#cena_"+id).html();
	if(kolvo_fixed && !parseInt(tovar_cena))
	{
		tovar_cena = 0;
	}
	
	if(!kolvo_fixed)
	{
		new_tovar_cena = str_replace('&nbsp;', '', tovar_cena);
		// Opera
		new_tovar_cena=new_tovar_cena.replace(/\s/i,'');
	}
	else
	{
		new_tovar_cena=tovar_cena;
	}
	
	tovar_all_price = (new_tovar_cena*kolvo);
	total_summ = ((total_summ*1)+(tovar_all_price*1));
	
	insert_html = '<table width="100%" cellspacing="1" style="color: #ffffff;" id="basket_'+id+'"><tr bgcolor="#694e62" style="font-weight: bold;"><td width="35%">'+tovar_name+'</td><td width="20%" align="center">'+tovar_cena+'</td><td width="20%" align="center">'+kolvo+'</td><td align="center" width="20%" id="id_tovar_all_price_'+id+'">'+tovar_all_price+'</td><td align="center" width="5%"><a href="javascript:void(0);" onclick="DelBasketPos('+id+'); return false;"><img src="/themes/default/images/del.jpg" style="margin: 0px;" alt="Удалить из корзины" title="Удалить из корзины"/></a></td></table>';
	$("#pre_order").append(insert_html);
	$("#total_summ").html(total_summ);
}

function DelBasketPos(id)
{
	
	$("#kolvo_"+id).val('');
	//$("#kolvo_"+id).removeAttr("disabled");
	$("#kolvo_"+id).removeClass('input_grey');
	$("#kolvo_"+id).removeAttr('checked');
	pos_summ = $('#id_tovar_all_price_'+id).html();
	total_summ = ((total_summ*1)-(pos_summ*1));
	$("#total_summ").html(total_summ);
	$('#basket_'+id).remove();
}

function SendOrder()
{
	fio = $('#fio_id').val();
	contacts = $('#contacts_id').val();
		
	if(fio == '')
	{
		alert('Вы не заполнили поле Ваше имя');
		return false;
	}
	if(contacts == '')
	{
		alert('Вы не заполнили поле Контакты');
		return false;
	}
	
	document.forms["form_order"].submit();
}

function str_replace ( search, replace, subject ) {    // Replace all occurrences of the search string with the replacement string
    // 
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
 
    if(!(replace instanceof Array)){
        replace=new Array(replace);
        if(search instanceof Array){//If search    is an array and replace    is a string, then this replacement string is used for every value of search
            while(search.length>replace.length){
                replace[replace.length]=replace[0];
            }
        }
    }
 
    if(!(search instanceof Array))search=new Array(search);
    while(search.length>replace.length){//If replace    has fewer values than search , then an empty string is used for the rest of replacement values
        replace[replace.length]='';
    }
 
    if(subject instanceof Array){//If subject is an array, then the search and replace is performed with every entry of subject , and the return value is an array as well.
        for(k in subject){
            subject[k]=str_replace(search,replace,subject[k]);
        }
        return subject;
    }
 
    for(var k=0; k<search.length; k++){
        var i = subject.indexOf(search[k]);
        while(i>-1){
            subject = subject.replace(search[k], replace[k]);
            i = subject.indexOf(search[k],i);
        }
    }
 
    return subject;
}

