var updatePrice = function()
{
	// gif banner
	var gif_300x250 = $('gif_300x250').checked;
	var gif_250x250 = $('gif_250x250').checked;
	var gif_180x150 = $('gif_180x150').checked;
	var gif_400x400 = $('gif_400x400').checked;
	var gif_728x90 = $('gif_728x90').checked;
	var gif_468x60 = $('gif_468x60').checked;
	var gif_234x60 = $('gif_234x60').checked;
	var gif_125x125 = $('gif_125x125').checked;
	var gif_120x600 = $('gif_120x600').checked;
	var gif_160x600 = $('gif_160x600').checked;
	
	var gif_banner_count = 0;
	if (gif_300x250) gif_banner_count++;
	if (gif_250x250) gif_banner_count++;
	if (gif_180x150) gif_banner_count++;
	if (gif_400x400) gif_banner_count++;
	if (gif_728x90) gif_banner_count++;
	if (gif_468x60) gif_banner_count++;
	if (gif_234x60) gif_banner_count++;
	if (gif_125x125) gif_banner_count++;
	if (gif_120x600) gif_banner_count++;
	if (gif_160x600) gif_banner_count++;
	
	// flash banner
	var flash_300x250 = $('flash_300x250').checked;
	var flash_250x250 = $('flash_250x250').checked;
	var flash_180x150 = $('flash_180x150').checked;
	var flash_400x400 = $('flash_400x400').checked;
	var flash_728x90 = $('flash_728x90').checked;
	var flash_468x60 = $('flash_468x60').checked;
	var flash_234x60 = $('flash_234x60').checked;
	var flash_125x125 = $('flash_125x125').checked;
	var flash_120x600 = $('flash_120x600').checked;
	var flash_160x600 = $('flash_160x600').checked;
	
	var flash_banner_count = 0;
	if (flash_300x250) flash_banner_count++;
	if (flash_250x250) flash_banner_count++;
	if (flash_180x150) flash_banner_count++;
	if (flash_400x400) flash_banner_count++;
	if (flash_728x90) flash_banner_count++;
	if (flash_468x60) flash_banner_count++;
	if (flash_234x60) flash_banner_count++;
	if (flash_125x125) flash_banner_count++;
	if (flash_120x600) flash_banner_count++;
	if (flash_160x600) flash_banner_count++;
	
	var banner_price = 0;
	var mutation_price = 0;
	var discount_price = 0;
	var concept_price = 0;
	var total_price = 0;
	var voucher_price = 0;
	
	banner_price += gif_banner_count * 160;
	banner_price += flash_banner_count * 200;
		
	total_price += banner_price;
	
	// discount
	if (gif_banner_count >= 5)
		discount_price -= gif_banner_count * 80;
	
	if (flash_banner_count >= 5)
		discount_price -= flash_banner_count * 80;
	
	total_price += discount_price;
	
	// mutli language
	var multi_lang = $('multi_lang').checked;
	
	if (multi_lang)
	{
		var languages = $('languages').value;
		if (languages == 'mehr')
		{
			languages = 10;
		}
		else
		{
			languages = parseInt(languages) - 1;
		}
		mutation_price += (gif_banner_count + flash_banner_count) * languages * 40;
	}
	total_price += mutation_price;
	
	// concept
	var order_concept = $('order_konzept').checked;
	
	if (order_concept)
	{
		concept_price += 390;
	}
	total_price += concept_price;
	
	// voucher
	var voucher_value = $('voucher_value').value;
	var voucher_type = $('voucher_type').value;
	
	if (voucher_type == 'absolute')
	{
		voucher_price = voucher_value;
	}
	else if (voucher_type == 'percentage')
	{
		voucher_price = total_price * (voucher_value / 100);
	}
	total_price -= voucher_price;
	
	$('price_banner').innerHTML = number_format(banner_price, 2, ',', '.');
	$('price_mutation').innerHTML = number_format(mutation_price, 2, ',', '.');
	$('price_rabatt').innerHTML = number_format(discount_price, 2, ',', '.');
	$('price_konzept').innerHTML = number_format(concept_price, 2, ',', '.');
	$('price_voucher').innerHTML = number_format(voucher_price, 2, ',', '.');
	$('price_sum').innerHTML = number_format(total_price, 2, ',', '.');
}

function checkOrderForm()
{
	// gif banner
	var gif_300x250 = $('gif_300x250').checked;
	var gif_250x250 = $('gif_250x250').checked;
	var gif_180x150 = $('gif_180x150').checked;
	var gif_400x400 = $('gif_400x400').checked;
	var gif_728x90 = $('gif_728x90').checked;
	var gif_468x60 = $('gif_468x60').checked;
	var gif_234x60 = $('gif_234x60').checked;
	var gif_125x125 = $('gif_125x125').checked;
	var gif_120x600 = $('gif_120x600').checked;
	var gif_160x600 = $('gif_160x600').checked;
	
	var gif_banner_count = 0;
	if (gif_300x250) gif_banner_count++;
	if (gif_250x250) gif_banner_count++;
	if (gif_180x150) gif_banner_count++;
	if (gif_400x400) gif_banner_count++;
	if (gif_728x90) gif_banner_count++;
	if (gif_468x60) gif_banner_count++;
	if (gif_234x60) gif_banner_count++;
	if (gif_125x125) gif_banner_count++;
	if (gif_120x600) gif_banner_count++;
	if (gif_160x600) gif_banner_count++;
	
	// flash banner
	var flash_300x250 = $('flash_300x250').checked;
	var flash_250x250 = $('flash_250x250').checked;
	var flash_180x150 = $('flash_180x150').checked;
	var flash_400x400 = $('flash_400x400').checked;
	var flash_728x90 = $('flash_728x90').checked;
	var flash_468x60 = $('flash_468x60').checked;
	var flash_234x60 = $('flash_234x60').checked;
	var flash_125x125 = $('flash_125x125').checked;
	var flash_120x600 = $('flash_120x600').checked;
	var flash_160x600 = $('flash_160x600').checked;
	
	var flash_banner_count = 0;
	if (flash_300x250) flash_banner_count++;
	if (flash_250x250) flash_banner_count++;
	if (flash_180x150) flash_banner_count++;
	if (flash_400x400) flash_banner_count++;
	if (flash_728x90) flash_banner_count++;
	if (flash_468x60) flash_banner_count++;
	if (flash_234x60) flash_banner_count++;
	if (flash_125x125) flash_banner_count++;
	if (flash_120x600) flash_banner_count++;
	if (flash_160x600) flash_banner_count++;
	
	// concept
	var order_concept = $('order_konzept').checked;
	var concept = $('konzept').value;
	
	// data
	var company = $('company').value;
	var contact_person = $('contact_person').value;
	var street = $('street').value;
	var city = $('city').value;
	var phone = $('phone').value;
	var fax = $('fax').value;
	var email = $('email').value;
	var website = $('website').value;
	
	// terms of agreement
	var accept_tog = $('accept_tog').checked;
	
	// error found?
	var error = false;
	var err_data = false;
	
	// check banners
	if ((gif_banner_count + flash_banner_count) == 0 && !order_concept)
	{
		$('err_banners').style.display = 'block';
		error = true;
	}
	else
	{
		$('err_banners').style.display = 'none';
	}
	
	// concept
	if (!order_concept && (concept.length < 10 || concept == "Bitte geben Sie hier Ihr Bannerkonzept ein"))
	{
		$('err_concept').style.display = 'block';
		error = true;
	}
	else
	{
		$('err_concept').style.display = 'none';
	}
	
	/*** CLIENT DATA ***/	
	// company
	if (company == '')
	{
		$('company').className = 'error';
		error = true;
		err_data = true;
	}
	else
	{
		$('company').className = 'text';
	}
	
	// contact_person
	if (contact_person == '')
	{
		$('contact_person').className = 'error';
		error = true;
		err_data = true;
	}
	else
	{
		$('contact_person').className = 'text';
	}
	
	// street
	if (street == '')
	{
		$('street').className = 'error';
		error = true;
		err_data = true;
	}
	else
	{
		$('street').className = 'text';
	}
	
	// city
	if (city == '')
	{
		$('city').className = 'error';
		error = true;
		err_data = true;
	}
	else
	{
		$('city').className = 'text';
	}
	
	// phone
	if (phone == '')
	{
		$('phone').className = 'error';
		error = true;
		err_data = true;
	}
	else
	{
		$('phone').className = 'text';
	}
	
	// email
	if (email == '' || email.indexOf('@') < 0 || email.indexOf('.') < 0)
	{
		$('email').className = 'error';
		error = true;
		err_data = true;
	}
	else
	{
		$('email').className = 'text';
	}
	
	// when find error, give response to visitor	
	if (err_data)
	{
		$('err_data').style.display = 'block';
		error = true;
	}
	else
	{
		$('err_data').style.display = 'none';
	}
	
	// terms of agreement
	if (!accept_tog)
	{
		$('err_tog').style.display = 'block';
		error = true;
	}
	else
	{
		$('err_tog').style.display = 'none';
	}
	
	// when find error, give response to visitor	
	if (error)
	{
		$('err_found').style.display = 'block';
	}
	else
	{
		$('err_found').style.display = 'none';
	}
	
	// return opposite of error found (no error = good!)
	return !error;
}

function checkContactForm()
{
	var contact_person = $('contact_person').value;
	var contact = $('contact').value;
	
	// contact_person
	if (contact_person == '')
	{
		$('contact_person').className = 'error';
		error = true;
	}
	else
	{
		$('contact_person').className = 'text';
		error = false;
	}
	
	// contact
	if (contact == '')
	{
		$('contact').className = 'error';
		error = true;
	}
	else
	{
		$('contact').className = 'text';
		error = false;
	}
	
	// when find error, give response to visitor	
	if (error)
	{
		$('err_found').style.display = 'block';
	}
	else
	{
		$('err_found').style.display = 'none';
	}
	
	// return opposite of error found (no error = good!)
	return !error;
}

var hideCallback = function(hideAnyway)
{	
	if (hideAnyway == null) hideAnyway = false;

	if (($('callback_contact').value == '' || $('callback_contact').value == 'Ihr Telefon/Email') || 
		hideAnyway)
		{
			$('callback_form').fade();
		}
}

var submitCallback = function()
{
	if ($('callback_contact').value.length > 3 &&
		$('callback_contact').value != 'Ihr Telefon/Email')
	{
		var contact = $('callback_contact').value;

		var opt = {
		    method: 'post',
		    postBody: 'contact='+contact,
			evalJS: true,
		    onSuccess: function(t) {
	    		$('callback_form').innerHTML = t.responseText;
		    },
		    on404: function(t) {
		        alert('Error 404: location "' + t.statusText + '" was not found.');
		    },
		    onFailure: function(t) {
		        $('callback_form').innerHTML = 'Beim Senden der Nachricht ist ein Fehler aufgetreten. Bitte versuchen Sie es erneut.';
		    },
			onComplete: function(t) {
				setTimeout('$(\'callback_form\').fade()', 5000);
			}
		};
		new Ajax.Request('callback.php', opt);
		/* $('callback_form').innerHTML = '<p>Vielen Dank!<br />Wir werden uns in Kürze mit Ihnen in Verbindung setzen!</p>'; */
	}
	else
	{
		alert('Bitte geben Sie eine gültige Kontaktmgölichkeit an!');
		$('callback_form').style.display='block';
		$('callback_contact').focus();
	}
}

function number_format( number, decimals, dec_point, thousands_sep ) {
    // Formats a number with grouped thousands
    //
    // version: 906.1806
    // discuss at: http://phpjs.org/functions/number_format
    var n = number, prec = decimals;

    var toFixedFix = function (n,prec) {
        var k = Math.pow(10,prec);
        return (Math.round(n*k)/k).toString();
    };

    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
    var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;

    var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;

    var abs = toFixedFix(Math.abs(n), prec);
    var _, i;

    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;

        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
    }

    var decPos = s.indexOf(dec);
    if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
        s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
    }
    else if (prec >= 1 && decPos === -1) {
        s += dec+new Array(prec).join(0)+'0';
    }
    return s;
}

function info(url, target)
{
	if (target.length = 0) target = "bannergarten_info";
	var urlParts = url.split("#", 2);
	url = urlParts[0] + "?info=1";
	if (urlParts.length == 2) url = url + "#" + urlParts[1];
	window.open(url, target, "menubar=no,width=880,height=300,toolbar=no,scrollbars=yes,resizeable=yes");
	return false;
}

Event.observe(window, 'load', function()
{
	var infoLinks = $$('a.info_link');
	infoLinks.each(function(infoLink)
	{
		infoLink.observe('click', function(e)
		{
			info(this.href, this.target);
			Event.stop(e);
		});
	});
});


function checkVoucher()
{
	var voucher = $('voucher').value;
	var total = $('price_sum').innerHTML;
	
	new Ajax.Request(
		'./content/ajax/voucher.php',
		{
			'method': 'post',
			'parameters': {
				'voucher': voucher,
				'total': total
			},
			'onSuccess': getVoucherResponse
		}		
	);
}

function getVoucherResponse(transport, json)
{
	var data = transport.responseText.evalJSON(true);
		
	$('voucher').value = data['code'];
	$('voucher_text').innerHTML = data['msg'];
	$('voucher_value').value = data['value'];
	$('voucher_type').value = data['type'];
	
	updatePrice();
}

