function gocalc()
    {
    var cityto  = $('cityto').getValue();
    var beanbag = $('beanbag').getValue();
    var count   = $('calc_count').getValue();
    
    cityto = cityto.split('-');
    for(i=0;i<cityto.length;i++)
	{
	cityto[i] = cityto[i].split(' ');
	for(j=0;j<cityto[i].length;j++)
	    {
	    a = cityto[i][j][0].toUpperCase();
	    b = cityto[i][j].substring(1, cityto[i][j].length).toLowerCase();
	    cityto[i][j] = a + b;
	    }
	cityto[i] = cityto[i].join(' ');
	}
    cityto = cityto.join('-');
    if ($('cityto')) $('cityto').value = cityto;
    
    $('results').innerHTML = '';
    $('loader').setStyle({display: 'block'});
    
    var ajax = new Ajax.Updater(
        'results',
        '/calc/calc.php',
        {method: 'post',
        parameters: {cityto:cityto, beanbag:beanbag, count:count},
	onSuccess: function(){
                        $('loader').setStyle({display: 'none'});
                        }
	}
        );
    }

function gocalc_basket()
    {
    if ($('user_city'))
	{
	var cityto = $('user_city').getValue();
	$('user_city').disabled = true;
	var step = 3;
	}
    if ($('cityto'))
	{
	var cityto = $('cityto').getValue();
	$('cityto').disabled = true;
	var step = 1;
	}
    
    if (cityto.length > 0) {
    var inputs = $$('div.countnum input');
    for(i=0;i<inputs.length;i++) inputs[i].disabled = true;
    
    cityto = cityto.split('-');
    for(i=0;i<cityto.length;i++)
	{
	cityto[i] = cityto[i].split(' ');
	for(j=0;j<cityto[i].length;j++)
	    {
	    if (cityto[i][j][0]) {
		a = cityto[i][j][0].toUpperCase();
		b = cityto[i][j].substring(1, cityto[i][j].length).toLowerCase();
		cityto[i][j] = a + b;
	    }
	    }
	cityto[i] = cityto[i].join(' ');
	}
    cityto = cityto.join('-');
    
    if ($('user_city')) $('user_city').value = cityto;
    if ($('cityto')) $('cityto').value = cityto;
	
    if ($('delive_cost'))
        {
        var all_cost = parseInt($('all_cost').innerHTML);
        var delive_cost = parseInt($('delive_cost').innerHTML);
        $('all_cost').innerHTML = (all_cost - delive_cost);
        $('all_cost_label').innerHTML = (all_cost - delive_cost);
        }

    $('results').innerHTML = '';
    $('loader').setStyle({display: 'block'});
    
    var ajax = new Ajax.Updater(
        'results',
        '/calc/calc.php',
        {method: 'post',
        parameters: {cityto:cityto,step:step},
	onSuccess: function(){
	    var inputs = $$('div.countnum input');
	    for(i=0;i<inputs.length;i++) inputs[i].disabled = false;
	    if ($('user_city')) $('user_city').disabled = false;
	    if ($('cityto')) $('cityto').disabled = false;
	    $('loader').setStyle({display: 'none'});
	    },
	onComplete: function(){
	    if ($('inputdelive').getValue() && $('inputdelive').getValue() == '1')
		{
                if ($('all_cost'))
                    {
                    var all_cost = parseInt($('all_cost').innerHTML);
                    var delive_cost = parseInt($('delive_cost').innerHTML);
		    $('all_cost').innerHTML = (delive_cost + all_cost);
		    $('all_cost_label').innerHTML = (delive_cost + all_cost);
                    }
		}
            $('inputdelive').focus();
	    var inputs = $$('div.countnum input');
	    for(i=0;i<inputs.length;i++) inputs[i].disabled = false;
	    if ($('user_city')) $('user_city').disabled = false;
	    if ($('cityto')) $('cityto').disabled = false;
	    $('loader').setStyle({display: 'none'});
	    },
	onFailure: function(){
            var inputs = $$('div.countnum input');
	    for(i=0;i<inputs.length;i++) inputs[i].disabled = false;
	    if ($('user_city')) $('user_city').disabled = false;
	    if ($('cityto')) $('cityto').disabled = false;
	    $('loader').setStyle({display: 'none'});
	    }
	}
        );
    }
    else {
	 var inputs = $$('div.countnum input');
	    for(i=0;i<inputs.length;i++) inputs[i].disabled = false;
	    if ($('user_city')) $('user_city').disabled = false;
	    if ($('cityto')) $('cityto').disabled = false;
	    $('loader').setStyle({display: 'none'});
    }
    }
    
function calc_count_up()
    {
    var count = $('calc_count').getValue();
    count++;
    $('calc_count').value = count;
    }

function calc_count_down()
    {
    var count = $('calc_count').getValue() - 1;
    if (count > 0) $('calc_count').value = count;
    }

function calc_input()
    {
    if ($('inputdelive').getValue() && $('inputdelive').getValue() == '1') var inputdelive = '1';
    else var inputdelive = '0';
    
    var ajax = new Ajax.Updater(
        'all_cost_label',
        '/calc/calc_input.php',
        {method: 'post',
        parameters: {inputdelive:inputdelive},
	onComplete: function(){
		$('all_cost').innerHTML = $('all_cost_label').innerHTML;
		}
	}
        );
    }
