var sendAction = '';
var LoadContent = '';
var Box = '';
var httpKandy = createKandyRequestObject();

function createKandyRequestObject() {
	
	var ro;
	var browser = navigator.appName;
	
	if(browser == "Microsoft Internet Explorer"){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		ro = new XMLHttpRequest();
	}

	return ro;
}

function sendKandyRequest(action, parameter, box) {

	if(action == 'showProductsList')
		httpKandy.open('get', kandyServerUrl+'kandy/files/kandy_ProductsComparison.php?action='+action+'&manufacturers_id='+parameter+'&box='+box);

	if(action == 'showContractsList')
		httpKandy.open('get', kandyServerUrl+'kandy/files/kandy_ContractsComparison.php?action='+action+'&kandy_contracts_networks_id='+parameter+'&box='+box);

	if(action == 'addKandyMixerProduct' || action == 'removeKandyMixerProduct')
		httpKandy.open('get', kandyServerUrl+'kandy/extensions/kandy_Mixer/files/kandy_Mixer.php?action='+action+'&products_id='+parameter);

	if(action == 'addKandyMixerContract' || action == 'removeKandyMixerContract')
		httpKandy.open('get', kandyServerUrl+'kandy/extensions/kandy_Mixer/files/kandy_Mixer.php?action='+action+'&kandy_contracts_id='+parameter);

	if(action == 'addKandyMixerExtra' || action == 'removeKandyMixerExtra')
		httpKandy.open('get', kandyServerUrl+'kandy/extensions/kandy_Mixer/files/kandy_Mixer.php?action='+action+'&products_id='+parameter);

	if(action == 'kandymixerprice')
		httpKandy.open('get', kandyServerUrl+'kandy/extensions/kandy_Mixer/files/kandy_Mixer.php?action='+action);

	httpKandy.onreadystatechange = handleKandyResponse;
	httpKandy.send(null);
}

function handleKandyResponse() {

	if(httpKandy.readyState == 4){

		var response = httpKandy.responseText;
		
		if(sendAction == 'showProductsList') {
			document.getElementById('product_'+Box).innerHTML = response;
		} else if(sendAction == 'showContractsList') {
			document.getElementById('contract_'+Box).innerHTML = response;
		} else if(sendAction == 'kandymixerprice') {
			document.getElementById('kandymixerprice').innerHTML = response;
		} else {
			document.getElementById(Box).innerHTML = response;
		}

		if(sendAction == 'addKandyMixerProduct' || sendAction == 'removeKandyMixerProduct' || sendAction == 'addKandyMixerContract' || sendAction == 'removeKandyMixerContract' || sendAction == 'addKandyMixerExtra' || sendAction == 'removeKandyMixerExtra') {
			Box = 'kandymixerprice';
			sendAction = 'kandymixerprice';
			sendKandyRequest('kandymixerprice');	
		}
	} else {
		if(sendAction == 'showProductsList') {
			document.getElementById('product_'+Box).innerHTML = '<div class="kandymixerloading"><img src="kandy/images/ajax-loader.gif" /></div>';
		} else if(sendAction == 'showContractsList') {
			document.getElementById('contract_'+Box).innerHTML = '<div class="kandymixerloading"><img src="kandy/images/ajax-loader.gif" /></div>';
		} else {
			document.getElementById(Box).innerHTML = '<div class="kandymixerloading"><img src="kandy/images/ajax-loader.gif" /></div>';
		}
	}
}

function showProductsList(manufacturers_id, box) {
	
	Box = box;
	sendAction = 'showProductsList';
	sendKandyRequest('showProductsList', manufacturers_id, box);
}

function showContractsList(kandy_contracts_networks_id, box) {
	
	Box = box;
	sendAction = 'showContractsList';
	sendKandyRequest('showContractsList', kandy_contracts_networks_id, box);
}

function addKandyMixerProduct(products_id, box) {
	
	Box = box;
	sendAction = 'addKandyMixerProduct';
	sendKandyRequest('addKandyMixerProduct', products_id, box);
}

function removeKandyMixerProduct(products_id, box) {
	
	Box = box;
	sendAction = 'removeKandyMixerProduct';
	sendKandyRequest('removeKandyMixerProduct', products_id, box);
}

function addKandyMixerContract(kandy_contracts_id, box) {
	
	Box = box;
	sendAction = 'addKandyMixerContract';
	sendKandyRequest('addKandyMixerContract', kandy_contracts_id, box);
}

function removeKandyMixerContract(kandy_contracts_id, box) {
	
	Box = box;
	sendAction = 'removeKandyMixerContract';
	sendKandyRequest('removeKandyMixerContract', kandy_contracts_id, box);
}

function addKandyMixerExtra(products_id, box) {
	
	Box = box;
	sendAction = 'addKandyMixerExtra';
	sendKandyRequest('addKandyMixerExtra', products_id, box);
}

function removeKandyMixerExtra(products_id, box) {
	
	Box = box;
	sendAction = 'removeKandyMixerExtra';
	sendKandyRequest('removeKandyMixerExtra', products_id, box);
}

function kandyShowElement(id) {
		
	if(document.getElementById(id)) {
	
		vis = (document.getElementById(id).style.visibility === 'hidden' || document.getElementById(id).style.visibility === '') ? 'visible' : 'hidden';
		document.getElementById(id).style.visibility = vis;

		dis = (document.getElementById(id).style.display === 'none') ? 'block' : 'none';
		document.getElementById(id).style.display = dis;
	}
		
}

function kandySwitchElement(id, fieldname, maxid) {
	
	for(var t = 1; t < maxid+1; ++t) {
		if(document.getElementById(fieldname+t)) {

			document.getElementById(fieldname+t).style.visibility = 'hidden';
			document.getElementById(fieldname+t).style.display = 'none';
		}
	}

	if(document.getElementById(id)) {
		document.getElementById(id).style.visibility = 'visible';
		document.getElementById(id).style.display = '';
	}
}

function kandyContractsInfo(ct, popupWidth, popupHeight) {
	urlString = kandyServerUrl+'kandy/files/kandy_ContractsInfo.php?kandy_contracts_id=' + ct;
	setup = 'width='+popupWidth+',height='+popupHeight+',left=0,top=0,directories=no,toolbar=no,locationbar=no,menubar=no,scrollbars=yes,status=no,resizable=yes,dependent=yes,name=yes';
	window.open(urlString, "Tarifinformationen" , setup);
}