// JavaScript Document / XMLHttpRequest handling
// apVersion = 1.2.100306
// supports Safari, Mozilla, Opera, IE7+
// 2010 Bernhard Barkow, creativeeyes.at

// XHR handling:
function getHTTPObj() {
	var req = false;
	if (window.XMLHttpRequest && !(window.ActiveXObject)) {
		try { req = new XMLHttpRequest(); }
		catch(e) { req = false; }
	} else if(window.ActiveXObject) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
					req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
					req = false;
			}
		}
	}
	return req;
}

function makeRequest(u) {
	if(httpReq) {
		//if(reqActive) 
		httpReq.abort();
		httpReq.onreadystatechange = processReqChange;
		httpReq.open("GET", u, true);
		httpReq.send(null);
		reqActive=true;
	}
}

function processReqChange() {
	if (httpReq.readyState == 4) { // "loaded"
		reqActive = false;
		if (httpReq.status == 200) {	// "OK"
			showCart(httpReq.responseText);
		}
	}
}

// entity encoding (necessary fix for strings with quotes)
function html_entity_decode(s) {	// simple replacement
	var hash_map = { "&lt;":"<", "&gt;":">", "&nbsp;":" ", "&amp;":"&", "&quot;":'"'}, s2=s, entity, char;
	for (var entity in hash_map) {
			char = hash_map[entity];
			s2 = s2.split(entity).join(char);
	}
	return s2;
}
// show cart confirmation message dialog:
function showCart(s) {
	var o = getElem("dimBG"), p = getElem("cartMsg");
	if (o!=null) {
		addClass(o, "active");
		o.onclick = hideCart;
	}
	if (p!=null) {
		addClass(p, "active");
		p.onclick = hideCart;
		if (document.useJSON) {	// not all browsers support native JSON!
			var json = JSON.parse(s);
			json.txt = html_entity_decode(json.txt);	// ugly fix necessary for quoted strings with php
			p.innerHTML = json.txt;
			updateCartItemsCounter(json.iic, true);
		} else {	// try a simple workaround
			p.innerHTML = s;
			updateCartItemsCounter(document.apc_cartN, false);
		}
	}
}
function hideCart() {
	var o = getElem("dimBG"), p = getElem("cartMsg");
	if (o!=null) removeClass(o, "active");
	if (p!=null) removeClass(p, "active");
}
function updateCartItemsCounter(n, bAbs) {	// bAbs: absolute / relative item count
	var iicNode = getElem("itemsInCart");
	if (iicNode!=null) {
		if (bAbs) iicNode.innerHTML = n;
		else {
			var i = parseInt(iicNode.innerHTML);
			if (isNaN(i) || i<1) i=0;
			iicNode.innerHTML = (i + parseInt(n)).toString();
		}
	}
}

function getEvtTarget(e) {
	var t=null;
	if (e.target) t = e.target;
	else if (e.srcElement) t = e.srcElement;
	return t;
}
// add item to cart
function addToCart(e) {
	var evt = e ? e : window.event;
	var t = getEvtTarget(evt);
	if (t!=null) {
		if (t.tagName != "A") t = t.parentNode;//IE-fix
		var j = t.href.lastIndexOf("art_id=");
		var aid = t.href.substring(j+7, t.href.length);
		var url = cartURL+aid;
		if (document.useJSON) url += "&J=1";
		else document.apc_cartN = 1;
		makeRequest(url);
	}
	return false;
}
// the same, but using form fields
function addToCartFrm(e) {
	var evt = e ? e : window.event;
	var t = getEvtTarget(evt);
	if (t!=null) {
		var f = t.form;
		if (f!=null) {
			var i=(typeof(f.anz)=="undefined" ? "1" : f.anz.value);
			var url = cartURLf+f.artNr.value+"&anz="+i;
			if (document.useJSON) url += "&J=1";
			else document.apc_cartN = i;
			makeRequest(url);
		}
	}
	return false;
}

// global variables:
var httpReq = getHTTPObj(), reqActive=false;
var cartURL = "/s_addx/art_id=", cartURLf = "/s_addx/artNr=";
document.useJSON = (typeof(JSON) != "undefined");
document.apc_cartN = 0;

// initialization code:
function initLinks() {
	var a = getElementsByClassName("intoCart"), n = a.length, e, tn;
	for (var i=0; i<n; i++) {
		e = a[i];
		tn = e.tagName.toLowerCase();
		if (tn == "a") e.onclick = addToCart;
		else if (tn == "input") e.onclick = addToCartFrm;
	}
}

tempFuncAj = window.onload;
window.onload = function(){
	if (typeof (tempFuncAj) == "function"){
		try{
			tempFuncAj();
		} catch(e){}
	}
	initLinks();
}
