var arVersion = navigator.appVersion.split("MSIE");
var msie_version = parseFloat(arVersion[1]);
var _fixPng = null;
function fixPNG(img) {
	if (! img)
		return;
	if (_fixPng === null)
		_fixPng = ((msie_version >= 5.5) && (msie_version < 7) && (document.body.filters)) ? true : false;

	if (! _fixPng)
		return;		

	var blank = 'clear.gif';

	if (typeof(img) == 'string')
        // alert('str: '+ img);
        img = document.getElementById(img);
	if (! img)
		return;
	if (! img.src)
		return;
		
	if (img.src.substr(img.src.length - blank.length) == blank)
		return;

	// alert('img: '+ img.src);
	var src = img.src;
	img.src = 'clear.gif';
	img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+ src +"', sizingMethod='scale')";
}

function showProdDetail(id, flag) {
	function el(id) {
		return document.getElementById(id);
	}
	el('prod_detail_'+id).style.display = flag ? 'block' : 'none';
	el('prod_detail_expand_'+id).style.display = flag ? 'none' : '';
	el('prod_detail_collapse_'+id).style.display = flag ? '' : 'none';	
}

function getAbsoluteElementPosition(el) {
	var x = 0;
	var y = 0;
	while (el) {
		x += parseInt(el.offsetLeft);
		y += parseInt(el.offsetTop);
		el = el.offsetParent;
	}
	return {x:x, y:y};
}

var check_in_progress = false;
function indicateCartCheck(link) {
	if (link && check_in_progress)
		return false;
	check_in_progress = link ? true : false;

	var throbber = document.getElementById('shop_throbber');
	if (! throbber) {
		var throbber = document.createElement('img');
		throbber.id = 'shop_throbber';
		throbber.src = 'fileadmin/codeon/i/throbber-2.gif';
		throbber.style.display = 'none';
		throbber.style.position = 'absolute';
		throbber.style.width = '15px';
		throbber.style.height = '15px';
		throbber.title = 'Bitte warten...';
		throbber.alt = 'Bitte warten...';
		document.body.appendChild(throbber);
	}
	if (link) {
		var pos = getAbsoluteElementPosition(link);
		throbber.style.left = pos.x + 'px';
		throbber.style.top = pos.y + 'px';
		throbber.style.display = 'block';
	} else {
		throbber.style.display = 'none';
	}
	return true;
}

function checkCart(product_id, link) {
	if (! indicateCartCheck(link))
		return false;
	var req = new Request({
		url: 'fileadmin/codeon/scripts/check_cart.php?id='+product_id+'&rnd='+Math.random(), 
		method: 'get',
		encoding: 'iso-8859-1',
		onSuccess: function(responseText, responseXML){
			indicateCartCheck(false);
			processCheckCartResponse(responseText, product_id, link);
		},
		onFailure: function() {
			indicateCartCheck(false);
			alert('Ein Fehler ist aufgetreten, bitte erneut versuchen.');
		}
	}).send();
	return false;
}

function checkCartDelete(product_id, link) {
	if (! indicateCartCheck(link))
		return false;
	var req = new Request({
		url: 'fileadmin/codeon/scripts/check_cart_delete.php?id='+product_id+'&rnd='+Math.random(), 
		method: 'get',
		encoding: 'iso-8859-1',
		onSuccess: function(responseText, responseXML){
			indicateCartCheck(false);
			processCheckCartResponse(responseText, product_id, link);
		},
		onFailure: function() {
			indicateCartCheck(false);
			alert('Ein Fehler ist aufgetreten, bitte erneut versuchen.');
		}
	}).send();
	return false;	
}

var last_product_id = null;
var last_product_link = null;
function processCheckCartResponse(response, product_id, link) {
	if (response == 'OK') {
		document.location.href = link.href;
	} else {
		last_product_id = product_id;
		last_product_link = link;
		$('alert_window_content').innerHTML = response;
		showAlertWindow();
	}
}

function showAlertWindow() {
	var aw = $('alert_window');
	if (aw) {
		aw.style.display = 'block';
		aw.style.top = (document.body.scrollTop + 150)+'px';
		var left = (document.body.offsetWidth - 548) / 2;
		aw.style.left = left+'px';
	}
}

function closeAlertWindow() {
	$('alert_window').style.display = 'none';
}

function alert_addProduct() {
	document.location.href = last_product_link.href;
}
function alert_addMandatory(multi) {
	var url = url = last_product_link.href;
	if (multi) {
		var id = null;
		var inputs = document.getElementsByTagName('input');
		for (var i = 0; i < inputs.length; i++) {
			var input = inputs[i];
			if (input.id && input.id.substr(0, 5) == 'mand_') {
				if (input.checked)
					id = input.id.substr(5);
			}
		}
		if (! id) {
			alert('Sie mussen eines der Produkte auswaehlen');
			return;
		} else {
			if (url.indexOf('fwd') > 0)
				url += '&fwd[with]='+id;
			else
				url += '&with='+id;
		}
	}
	document.location.href = url;
}

function alert_deleteProduct() {
	document.location.href = last_product_link.href;
}

function alert_showCompat() {
	var a = $('compat_prod_'+last_product_id);
	if (a)
		document.location.href = a.href;
}

_smartFocuses = {};

function smartFocus(el, id, switchToPassword) {
	if (_smartFocuses[id])
		return;
	el.value = '';
	el.style.color = '#000';

	if (switchToPassword)
		$(el).set('type', 'password');

	_smartFocuses[id] = true;
}
function onFirstPasswordFocus() {
	var html = '<input type="password" id="pwd_input" name="password" class="input-pass" style="color:black" value="" />';
	$('pwd_container').innerHTML = html;
	setTimeout("$('pwd_input').focus()", 1);
}

function expandProduct(id, flag) {
	document.getElementById('prod_'+id+'_col').style.display = flag ? 'block' : 'none';
	document.getElementById('prod_'+id+'_exp').style.display = flag ? 'none' : 'block';
	document.getElementById('prod_'+id+'_desc').style.display = flag ? 'block' : 'none';
	return false;
}

function blurLink(el) {
	el.blur();
}



