var url = "/opencms/system/modules/de.phase6.intern/shop2/";

function getHTTPObject() {
  var xmlhttp;
/*@cc_on
  @if (@_jscript_version >= 5)
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }
  @else
    xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

var http = getHTTPObject();
var isWorking = false;
var prevn = -1;
var content_prevn = -1;
var basket_empty = true;
var prevArticleID = 0;
var showBasketCallback = "";


function showBasket() {
  if (http.readyState == 4) {
    if (http.responseText.indexOf('invalid') == -1) {
			data = http.responseText.replace( /\r\n/g, "\n" );
			var splitData = data.split("\n");
			document.getElementById("CartList").innerHTML = splitData[0];
			document.getElementById("CartSum").innerHTML = splitData[1];
			basket_empty = splitData[0].length < 10;
	    isWorking = false;
//			if (showBasketCallback != "")
				eval(showBasketCallback);
    }
  }
}

function putBasket(ArticleID, cnt) {
	prevArticleID = ArticleID;
  if (!isWorking && (typeof http == 'object')) {
    http.open("GET", url + "ajax_ins_basket.jsp?ArticleID=" + escape(ArticleID) + "&Count=" + escape(cnt) +
			"&CountryID=" + CountryID + "&attr_website=" + attr_website + "&b_ShowBruttoPrice=" + b_ShowBruttoPrice + "&tmp=" + Math.random(), true);
    http.onreadystatechange = showBasket;
    isWorking = true;
    http.send(null);
  }
  return false;
}

function deleteArticle(ArticleID) {
  if (!isWorking && (typeof http == 'object')) {
		if (document.getElementById("p6-nr-" + ArticleID) != undefined) {
			// this is content
			document.getElementById("p6-nr-" + ArticleID).value = "0";
			content_prevn = -1;
		}
		else {
			// else this is product.
			// because can be only 1 product in cart, we just clear all products.
			for (var i = 0; i < 4; i++) {
				if (document.getElementById("p6-nr-" + i) != undefined)
					document.getElementById("p6-nr-" + i).value = "0";
				if (document.getElementById("win-version-" + i) != undefined)
					document.getElementById("win-version-" + i).className = "";
				if (document.getElementById("mac-version-" + i) != undefined)
					document.getElementById("mac-version-" + i).className = "";
			}
			prevn = -1;
		}
    http.open("GET", url + "ajax_ins_basket.jsp?ArticleID=" + escape(ArticleID) +
			"&Count=0&CountryID=" + CountryID + "&b_ShowBruttoPrice=" + b_ShowBruttoPrice + "&attr_website=" + attr_website + "&tmp=" + Math.random(), true);
    http.onreadystatechange = showBasket;
    isWorking = true;
    http.send(null);
  }
  return false;
}


function getCnt(n, isContent) {
	var cnt = Number(document.getElementById("p6-nr-" + n).value);
	if (isNaN(cnt)) {
		cnt = 1;
		document.getElementById("p6-nr-" + n).value = cnt;
	}
	if (n != prevn) {
		if (document.getElementById("cd-" + prevn) != undefined) {
			document.getElementById("cd-" + prevn).checked = false;
			showhideShippingInfo(prevn);
		}
		if (prevn != -1 && !isContent)
			document.getElementById("p6-nr-" + prevn).value = "0";
		if (cnt == 0)
			document.getElementById("p6-nr-" + n).value = ++cnt;
		prevn = n;
	}
	return cnt;
}

function showPrice(showID, hideID) {	
		document.getElementById(showID).style.display = "";
		document.getElementById(hideID).style.display = "none";
}

// DNS-309: allow to order ONE upgrade.
function setUpdate(n, DLID) {
	var cnt = getCnt(n, false);
	if (cnt > 1) {
		cnt = 1;
		document.getElementById("p6-nr-" + n).value = cnt;
	}
	setArticle(n, DLID, DLID);
}

function setArticle(n, DLID, CDID) {
	var cnt = getCnt(n, false);
	showhideShippingInfo(n);
	if (CDID == DLID)
		putBasket(CDID, cnt);
	else {
		if (document.getElementById("cd-" + n).checked) {
			putBasket(CDID, cnt);
		}
		else {
			putBasket(DLID, cnt);
		}
	}
}


function setContent(n, ArticleID) {
	var pn = prevn;
	prevn = content_prevn;
	content_prevn = n;
	var cnt = getCnt(n, true);
	putBasket(ArticleID, cnt);
	prevn = pn;
}


function setContentLimited(n, ArticleID) {
	var pn = prevn;
	prevn = content_prevn;
	content_prevn = n;
	var cnt = getCnt(n, true);
	prevn = pn;
	if (cnt > 10)
		document.getElementById("p6-nr-" + n).value = "10";
	setContent(n, ArticleID);
}


function changePanel(hidePanel, showPanel) {
	document.getElementById(hidePanel + '-products-panel').style.display = 'none';
	document.getElementById(hidePanel + '-panel-button').className = 'selection-panel';
	document.getElementById(showPanel + '-products-panel').style.display = '';
	document.getElementById(showPanel + '-panel-button').className = 'selection-panel selected-panel';
}


function clearSelectOptions(elSel) {
	while (elSel.options.length > 1)
		elSel.options[1] = null;
}

function addSelectOption(elSel, text, value) {
	var elOptNew = document.createElement('option');
	elOptNew.text = text;
	elOptNew.value = value;
	try {
		elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
	}
	catch(ex) {
		elSel.add(elOptNew); // IE only
	}
}

function showSubjects() {
  if (http.readyState == 4) {
    if (http.responseText.indexOf('invalid') == -1) {
			data = http.responseText.replace( /\r\n/g, "\n" );
			var splitData = data.split("\n");
			var sel = document.getElementById("subjects");
			clearSelectOptions(sel);
			for (i = 0; i < splitData.length - 1; i = i + 2)
				addSelectOption(sel, splitData[i+1], splitData[i]);
			document.getElementById("subject-filter").style.display = "";
	    isWorking = false;
    }
  }
}

function getSubjects() {
	document.getElementById("subject-filter").style.display = "none";
	document.getElementById("book-filter").style.display = "none";
	document.getElementById("ContentShopComment").style.display = "none";
	document.getElementById("ContentShop").style.display = "none";
	document.getElementById("MoreContentLater").style.display = "";
	if (document.getElementById("publishers").selectedIndex > 0) {
		publisherID = document.getElementById("publishers").options[document.getElementById("publishers").selectedIndex].value;
	  if (!isWorking && (typeof http == 'object')) {
	    http.open("GET", url + "ajax_get_content_subjects.jsp?PublisherID=" + escape(publisherID) + "&tmp=" + Math.random(), true);
	    http.onreadystatechange = showSubjects;
	    isWorking = true;
	    http.send(null);
	  }
	}
  return false;
}


function showBooks() {
  if (http.readyState == 4) {
    if (http.responseText.indexOf('invalid') == -1) {
			data = http.responseText.replace( /\r\n/g, "\n" );
			var splitData = data.split("\n");
			var sel = document.getElementById("books");
			clearSelectOptions(sel);
			for (i = 0; i < splitData.length - 1; i = i + 2)
				addSelectOption(sel, splitData[i+1], splitData[i]);
			document.getElementById("book-filter").style.display = "";
	    isWorking = false;
    }
  }
}

function getBooks() {
	document.getElementById("book-filter").style.display = "none";
	document.getElementById("ContentShopComment").style.display = "none";
	document.getElementById("ContentShop").style.display = "none";
	document.getElementById("MoreContentLater").style.display = "";
	if (document.getElementById("subjects").selectedIndex > 0) {
		subjectID = document.getElementById("subjects").options[document.getElementById("subjects").selectedIndex].value;
	  if (!isWorking && (typeof http == 'object')) {
	    http.open("GET", url + "ajax_get_content_books.jsp?SubjectID=" + escape(subjectID) + "&tmp=" + Math.random(), true);
	    http.onreadystatechange = showBooks;
	    isWorking = true;
	    http.send(null);
	  }
	}
  return false;
}


function showContents() {
  if (http.readyState == 4) {
    if (http.responseText.indexOf('invalid') == -1) {
			document.getElementById("ContentShop").innerHTML = http.responseText;
			document.getElementById("ContentShopComment").style.display = "";
			document.getElementById("ContentShop").style.display = "";
			document.getElementById("MoreContentLater").style.display = "none";
			isWorking = false;
    }
  }
}

function getContents() {
//	document.getElementById("ContentShopComment").style.display = "none";
//	document.getElementById("ContentShop").style.display = "none";
	if (document.getElementById("subjects").selectedIndex > 0) {
		bookID = document.getElementById("books").options[document.getElementById("books").selectedIndex].value;
	  if (!isWorking && (typeof http == 'object')) {
	    http.open("GET", url + "ajax_get_content_articles.jsp?BookID=" + escape(bookID) +
			"&CountryID=" + CountryID + "&attr_website=" + attr_website + "&tmp=" + Math.random(), true);
	    http.onreadystatechange = showContents;
	    isWorking = true;
	    http.send(null);
	  }
	}
  return false;
}

var setCouponSuccessCallback = null;

function setCouponCallback() {
  if (http.readyState == 4) {
    if (http.responseText.indexOf('invalid') == -1) {
			data = http.responseText.replace( /\r\n/g, "\n" );
			var splitData = data.split("\n");
			
			if (splitData[0] == 'error') {
				alert(splitData[1]);
			}
			else {
				document.getElementById("CartList").innerHTML = splitData[0];
				document.getElementById("CartSum").innerHTML = splitData[1];
				if (setCouponSuccessCallback != null)
					setTimeout(setCouponSuccessCallback, 100);
			}
			isWorking = false;
			setCouponSuccessCallback = null;
		}
	}
}

function setCoupon() {
  if (document.order.Coupon.value != '' && !isWorking && (typeof http == 'object')) {
    http.open("GET", url + "ajax_set_coupon.jsp?Code=" + escape(document.order.Coupon.value) +
			"&CountryID=" + CountryID + "&attr_website=" + attr_website + "&b_ShowBruttoPrice=" + b_ShowBruttoPrice + "&tmp=" + Math.random(), true);
    http.onreadystatechange = setCouponCallback;
    isWorking = true;
    http.send(null);
  }
  return false;
}



