
var acall = null;

window.addEvent('domready', function()
{
	if ($('entries'))
	{
		$('entries').addEvent('mouseup', function(){
       setImpressionsAndPrice();
		});

		$('entries').addEvent('keyup', function(){
       setImpressionsAndPrice();
		});
	}
});

function toggleCat(id)
{
	$('root_entries').empty();

	childs = $('CS_'+id).getChildren();
	childs.forEach(function(i) {
		var opt = new Element('option');
			opt.value= i.value;
			opt.setHTML(i.getProperty('class'));
			opt.id	 = i.id;

		opt.injectInside($('root_entries'));
	});
}

function checkValues()
{
	var c = 0;

	$('entries').getChildren().forEach(function(item, index){
    if (item.selected == true)
      c++;
	});

	if (c < 1) {
		alert('Bitte nehmen Sie mindestens eine Auswahl vor.');
		return false;
	}

  return true;
}

function setImpressionsAndPrice()
{
    TargetingRemoteCall(zoneId);
}

function TargetingRemoteCall(zid)
{
    var startDate = new Date();
    var startDate = startDate.getTime()/1000;
    var endDate = startDate + 86400;

    var filter = new Array();
    var items = $('entries').getChildren();

    items.forEach(
        function(item, index)
        {
            if (item.selected == true)
              filter.push(item.value);
        }
    );
        
	var callUrl = '/httpRequest/?remote_call=getTargetingData&zid='+zid+'&tid=2&filter='+filter.toString()+'&start='+parseInt(startDate)+'&end='+parseInt(endDate);

  if (acall != null)
    acall.cancel();

	acall = new Ajax(callUrl, {method: 'get', onComplete: showResponseNew, onRequest: showThrobberNew, autoCancel: true });
	acall.request();
}

function showResponseNew(response)
{
  $('indicator').setStyle('display', 'none');

  res = Json.evaluate(response);
   
  $('imp_result_sum_org').value = res.impressions;
  
  calculateTargetingPrice();
}

function showThrobberNew()
{
  $('indicator').setStyle('display', 'block');
}

function toggleGeoTargeting()
{
	if ($('regio_checkbox').checked==false)
	{
		$('geo_targeting').setStyle('display', 'none');
	}
	else
	{
		$('geo_targeting').setStyle('display', 'inline');
	}
}

function toggleBoxes(name, icon)
{
  var el = $(name);

  if (el.getStyle('display') == 'none')
  {
    el.setStyle('display', 'block');
    icon.src = '/img/collapse.gif';
    icon.title = 'schließen';
  }
  else
  {
    el.setStyle('display', 'none');
    icon.src = '/img/expand.gif';
    icon.title = 'anzeigen';
  }
}

function calculateTargetingPrice()
{
	if ($('imp_result_sum_org') && $('imp_result_sum_org').value > 0)
  {
		if ($('channel_fixed_price').disabled == false) {
			duration = $('channel_fixed_price').value;
		} else {
			duration = 1;
		}

    channel_price = channel_price.replace(',', '.');
		imp = $('imp_result_sum_org').value;

		price_single = (imp / 1000) * channel_price;
		price = (imp / 1000) * duration * channel_price;

		$$('.dummy_radio_button').forEach(function(item,index) {
			id = item.id;
			id = id.replace(/product_radio_/, '');
			p  = price_single * $('product_radio_'+id).value;

  		$('product_price_result_'+id).innerHTML = numberFormat(runde(p, 2))+' €';

			$('product_volume_result_'+id).innerHTML = numberFormat(imp*$('product_radio_'+id).value)+' PIs';
		});
	}
}
