function showLoading() {
	$('list_container').innerHTML = $('loader').innerHTML;
};

function hideLoading() {
}

var searchTimer;
function searchKeyPressed(e) {
	var code = (window.event) ? event.keyCode : e.keyCode;
	if (code == 13)
	{
		searchCall2();
		if (searchTimer)
		{
			clearTimeout(searchTimer);
		}
		return;
	}
	if (code <= 45 && code != 8)
	{
		return;
	}
	if (searchTimer)
	{
		clearTimeout(searchTimer);
	}
	searchTimer = setTimeout(searchCall2, 700);
	return false;
}

function blockEnter(evt) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode :
          ((evt.which) ? evt.which : evt.keyCode);
    if (charCode == 13) {
        return false;
    } else {
        return true;
    }
}

function searchCall2()
{
	searchTimer = null;
	searchCall();
}

function updateFlash(id) {   var x = "<price>" +
        "<item amount=\""+0+"\" price=\""+base_price+"\" previous=\""+prev_base_price+"\" />" +
        "<item amount=\""+amount1+"\" price=\""+base_price+"\" previous=\""+prev_base_price+"\" />" +
        "<item amount=\""+amount2+"\" price=\""+price1+"\" previous=\""+prev_price1+"\" />" +   
        "<item amount=\""+amount3+"\" price=\""+price2+"\" previous=\""+prev_price2+"\" />" +
        "<item amount=\""+amount4+"\" price=\""+price3+"\" previous=\""+prev_price3+"\" />" +
        "<item amount=\""+amount4*2+"\" price=\""+price4+"\" previous=\""+prev_price4+"\" />" +
        "</price>";
        PriceChart = document.getElementById(id);
        if (PriceChart)
        {
                PriceChart.updatePrice(x);
        }
}

function updateSyndication()
{   
	    var sel = $('allow_syndication').checked;
        $('syndication_approval').disabled = sel ? '' : 'disabled';        $('price_syndication_share').disabled = sel ? '' : 'disabled';
        $('syndication_branding').disabled = sel ? '' : 'disabled';
}

function calculateDiscounts() {
        mode = $('discountMode').value;
        disc_1= calc_discount(prev_base_price, mode, prev_price1);
        disc_2= calc_discount(prev_base_price, mode, prev_price2);
        disc_3= calc_discount(prev_base_price, mode, prev_price3);
        disc_4= calc_discount(prev_base_price, mode, prev_price4);
        $('discount1').value = disc_1;
        $('discount2').value = Math.max(disc_1, disc_2);
        $('discount3').value = Math.max(disc_2, disc_3);
        $('discount4').value = Math.max(disc_3, disc_4);
};
function calc_discount(base, mode, price) {
        if (mode == 'abs') {
                return Math.max(precisionAfterPoint((base-price),2),0);
        }
        else if(mode == 'rel') {
                return Math.max(precisionAfterPoint(((base-price)/base*100),0),0);
        }
        return 0;
};
function normalizeDiscounts() {
        disc_1 = $('discount1').value;
        disc_2 = $('discount2').value;
        disc_3 = $('discount3').value;
        disc_4 = $('discount4').value;

        disc_2 = Math.max(disc_1, disc_2);
        disc_3 = Math.max(disc_2, disc_3);
        disc_4 = Math.max(disc_3, disc_4);

        $('discount2').value = disc_2;
        $('discount3').value = disc_3;
        $('discount4').value = disc_4;
};
function calculatePrices() {
	mode = $('discountMode').value;
	base_price = parseFloat($('base_price_field').value);
	price1 = calc_price(base_price, mode, parseFloat($('discount1').value));
	price2 = calc_price(base_price, mode, parseFloat($('discount2').value));
	price3 = calc_price(base_price, mode, parseFloat($('discount3').value));
	price4 = calc_price(base_price, mode, parseFloat($('discount4').value));
	
	$('price_price1').value = price1;
	$('price_price2').value = price2;
	$('price_price3').value = price3;
	$('price_price4').value = price4;
};
function calc_price(base, mode, discount) {
	if (mode == 'abs') {
		return precisionAfterPoint((base-discount),2);		
	}
	else if (mode == 'rel') {
		return precisionAfterPoint(((1-discount/100)*base),2);
	}
	return 0;
};
function precisionAfterPoint(x, precision) {
	u = Math.pow(10,precision);
	return Math.round(u*x)/u;
};
function priceUpdating()
{       
		calculateAmounts();
		calculatePrices();		
        $('test_amount').value = '';
        $('total_price').value = '';
        updateFlash('PriceChartFlash');
}
function loadPreviousValues() {
        prev_base_price = base_price;
        prev_price1 = price1;
        prev_price2 = price2;
        prev_price3 = price3;
        prev_price4 = price4;

        calculateAmounts();
        prev_amount1 = amount1;
        prev_amount2 = amount2;
        prev_amount3 = amount3;
        prev_amount4 = amount4;
}
function precisionAfterPoint(x, precision) {
        u = Math.pow(10,precision);
        return Math.round(u*x)/u;
}
function calculateAmounts() {
        amount1 = parseFloat($('amount1').value);
        amount2 = parseFloat($('amount2').value);
        amount3 = parseFloat($('amount3').value);
        amount4 = parseFloat($('amount4').value);
}
function calculateTotal(amount_id, total_id, fee_id) {
        if ($(amount_id).value.length == 0)
        {
                return;
        }
        var formVar =  $('price_form') != null ? $('price_form') : $('ajax_form');
				if (isNaN($(amount_id).value)) {
					$(amount_id).value = "";
					$(total_id).value = "";
				} else {
	        new Ajax.Request('/author_backend_content/total_price/' + document_id, {
	                method: 'post', 
	                parameters: formVar.serialize(true),
	                onSuccess: function(transport) {
	                },
	                onFailure: function() { alert('Communication to the server failed')}
	        });
				}
}
/* syndication_publisher/details */
function widgetSelectedCheckbox()
{
        var radios = $$('input[type=radio]');
        for (var i = 0; i < radios.length; i++)
        {
                if (radios[i].selected)
                {
                    widgetSelected(radios[i]);
                }
        }

}
function widgetSelected(radio)
{
	var radios = $$('input[type=radio]');
	for (var i = 0; i < radios.length; i++)
	{
		radios[i].selected = (radio == radios[i]);
	}
        var det = "";
        if (!$('link_to_details').checked)
        {
            det = "_details"
        }
	$('html').innerHTML = $(radio.value + "_html" + det).innerHTML;
}
function updateTotal(rate, credits){
    $('total').innerHTML = '$' + parseInt($('order_amount').value) * rate * credits + ' / ' + parseInt($('order_amount').value) * credits + " credits";
}

function reColorSortable(sortable, itemClass, oddClass, evenClass) {
    var firstColor = true;
    $(sortable).getElementsBySelector('li.sortableItem').each(function (item) {
        item.down('.' + itemClass).className = firstColor ? (oddClass + ' ' + itemClass) : (evenClass + ' ' + itemClass);
        firstColor = !firstColor;
    })
}
