/**
 * Initializes the request Object
 *
 * @return request The request Object
 */
function init()
{
	var req;  // The variable that makes Ajax possible!

	try
	{
		// Opera 8.0+, Firefox, Safari
		req = new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer Browsers
		try
		{
			req = new ActiveXObject('Msxml2.XMLHTTP');
		}
		catch (e)
		{
			try
			{
				req = new ActiveXObject('Microsoft.XMLHTTP');
			}
			catch (e)
			{
				// Something went wrong
				alert('Your browser broke!');
				return false;
			}
		}
	}
	return req;
}

function changeProductCount(prodId, e)
{
	var req = init();
       	// Create a function that will receive data sent from the server
	req.onreadystatechange = function()
	{
		if (req.readyState == 4)
		{

			var id = 'input_'+prodId;
			var param = req.responseText.split(';');
			var newProdSum = param[0];
			var cnt = param[1];
			var totalOffers = param[param.length - 2];
			var totalProducts = param[param.length - 1];

			var offers = new Array();

			for(i = 0,j=2; j < param.length; i++,j +=2)
			{
				if ( i == param.length - 2)
					break;

				offers[i] = new Object();
				offers[i]['offer'] = param[j];
				offers[i]['remaining'] = param[j + 1];
			}

			if (newProdSum > 0)
			{
				var product = 'sum_products_'+prodId;
				var oldProdSum = document.getElementById(product).innerHTML;
				var oldTotalSum = document.getElementById('sum_total').innerHTML;
				var oldTotalSumCoupons = document.getElementById('sum_total_coupons').innerHTML;

				oldProdSum = standardPriceFormat_Intern(oldProdSum);
				oldTotalSum = standardPriceFormat_Intern(oldTotalSum);
				oldTotalSumCoupons = standardPriceFormat_Intern(oldTotalSumCoupons);

				var newTotal = oldTotalSum - oldProdSum + Number(newProdSum);
				var newTotalCoupons = oldTotalSumCoupons - oldProdSum + Number(newProdSum);

				var newMwst = newTotal * ( 1 - 100/119);
				var newNetto = newTotal - newMwst;
				var prodPrice = (Number(newProdSum)).toFixed(2).toString();

				document.getElementById(product).innerHTML = standardPriceFormat_Show(prodPrice);
				document.getElementById('sum_mwst').innerHTML = standardPriceFormat_Show(newMwst);
				document.getElementById('sum_netto').innerHTML = standardPriceFormat_Show(newNetto);
				document.getElementById('sum_total').innerHTML = standardPriceFormat_Show(newTotal);
				document.getElementById('sum_total_coupons').innerHTML = standardPriceFormat_Show(newTotalCoupons);
			}

			if (Number(document.getElementById(id).value) > Number(cnt))
				document.getElementById('max_number_exceeded_'+prodId).style.display = 'block';
			else
				document.getElementById('max_number_exceeded_'+prodId).style.display = 'none';

			document.getElementById('input_'+prodId).value = cnt;

			document.getElementById('shopping_cart_cnt').innerHTML = totalProducts ;
			document.getElementById('shopping_cart_cnt_1').innerHTML = totalProducts ;
			document.getElementById('shopping_cart_sum').innerHTML = standardPriceFormat_Show(newTotal);
			swapText(offers, document.getElementById(id).value, totalOffers, prodId);
		}
	}

	value = parseInt(e.value);

	if ( isNaN(value) || value < 1)
	  {
		var id = 'input_'+prodId;
		document.getElementById(id).value = 0;
		return;
	}
	var url = 'http://www.gimahhot.de/';
	var queryString = '../m329-' + prodId + '-6-' + value + '/shopping_cart.html';

	req.open('GET', queryString, true);
	req.send(null);
}

function swapText(offers, cnt, totalOffers, prodId)
{
	cnt = parseInt(cnt);

	if (cnt > offers[0]['remaining'])
	{
		var html = '<p class="darkorange"><b>Unterschiedliche Preise:</b> <br />';
		var temp = cnt;
		var total = 0;
		var rows = 0;

		// only some product are available for the lowest price
		for( i = 0;i < offers.length; ++i)
		{
			++rows;
			temp -= offers[i]['remaining'];
			id = "id='price_diff_ajax_" + parseInt(i) + '_' + prodId + "'";

			if ( temp <= 0)
			{
				var temp1 = Number(temp) + Number(offers[i]['remaining']);

				html += '<p '+ id +'>Der Artikel ist ' +temp1 +
					' mal für <strong>' +offers[i]['offer'] +
					' &euro;</strong> verfügbar.<br /></p>';
				break;

			}

			html += '<p ' + id + '>Der Artikel ist nur ' +
				offers[i]['remaining'] +' mal für <strong>'+
				offers[i]['offer']+' &euro;</strong> verfügbar,'+
				' der nächst günstigere kostet <strong>' +
				offers[i+1]['offer']+' &euro;</strong>.<br />' +
				'</p>';
		}

		document.getElementById('price_diff_ajax_' + prodId).style.display = 'block';
		document.getElementById('price_diff_ajax_' + prodId).innerHTML = html;
	}
	else
	{
		document.getElementById('price_diff_ajax_' + prodId).style.display = 'none';

		for (i = 0; i < offers.length; i++)
		if (document.getElementById('price_diff_ajax_'+i +'_'+ prodId))
			document.getElementById('price_diff_ajax_'+ i +'_'+ prodId).style.display = 'none';
		else
			break;
	}

	for (i = 0; i < totalOffers; i++)
			if (document.getElementById('price_diff_'+i +'_'+ prodId))
				document.getElementById('price_diff_'+i +'_'+ prodId).style.display = 'none';
			else
				break;
}

function hideBanner(hostDir)
{
	var req = init();
	req.open('GET', hostDir+'/hide_banner.php', true);
	req.send(null);
}

function showCharityPage(page, hostdir, graphical)
{
	var req = init();
	// Create a function that will receive data sent from the server
	req.onreadystatechange = function()
	{
		if (req.readyState == 4)
		{
			var xml = req.responseXML;
			if (xml == undefined)
				return;

			var users = xml.getElementsByTagName('user');
			var page = xml.documentElement.getAttribute('page');
			var totalPages = xml.documentElement.getAttribute('totalPages');

			if (users.length == 0)
				return;

			html = '';
			for (var i = 0; i < users.length; i++)
			{
				var name = getNodeValue(users[i],'name');
				var url = getNodeValue(users[i],'url');
				var link = '<a href="'+url+'">'+url+'</a>';
				html += name + '<br />' + link + '<br /><br />';
			}

			document.getElementById('users').innerHTML = html;

			var pagi = document.getElementById('pagination');

			var urlFnc = function(page, hostdir)
			{
				return '#" onclick="javascript:showCharityPage('+page+', \''+hostdir+'\', '+graphical+')';
			}

			if (pagi)
				pagi.innerHTML = paginate(page, totalPages, graphical ? 2 : 4, urlFnc, hostdir, graphical);
		}
	}

	page = parseInt(page);

	req.open('GET', hostdir + '/charity_subscribers.php?page='+page, true);
	req.send(null);
}

function getNodeValue(obj, tag)
{
	return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
}

function showTagPage(url, params, elementId)
{
	var req = init();
	req.onreadystatechange = function()
	{
		if (req.readyState == 4)
		{
			var page = document.getElementById(elementId);

			if (page)
				page.innerHTML = req.responseText;
		}
	}

    req.open('POST', url, true);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	req.send(params);
}

 // Trading stuff
function getOtherShopsData(prodId, hostdir)
{
	var req = init();
	// Create a function that will receive data sent from the server
	req.onreadystatechange = function()
	{
		if (req.readyState == 4)
		{
			if (req.status != 200 && req.status != 0)
			{
				alert("OtherShopData not found!");
				return false;
			}
			
			var data = req.responseText;
			if (data != '')
				document.getElementById('othershopsdata').innerHTML = data;
		}
	}
	
	req.open("GET", hostdir + "/trade_other_shops_data.php?prod_id=" + prodId, true);
	req.send(null);
}

