
var xhr = false;
var xhr_minicart = false;

//   unescape  ä=%E4   Ä=%C4  ö=%F6  Ö=%D6  ü=%FC  Ü=%DC   ß=%DF 

function getXHR(){ 
        var lxhr = false; 
		if (window.XMLHttpRequest) { 
            lxhr = new XMLHttpRequest(); 
         } 
         else if (window.ActiveXObject) { 
            try 
            { 
               lxhr = new ActiveXObject("Msxml2.XMLHTTP"); 
            } 
            catch (e) 
            { 
               lxhr = new ActiveXObject("Microsoft.XMLHTTP"); 
            } 
         } 
         else 
         { 
         alert("Achtung, dieser Browser unterst%FChtzt kein XMLHTTPRequest!"); 
         lxhr = false; 
         } 
         
         return lxhr;
}

var scrollInMiniCartLeft = 0;
var scrollInMiniCartWidth = 0;
var scrollInMiniCartIndex = 0;
function scrollInMiniCart(){
   	if (scrollInMiniCartIndex < scrollInMiniCartWidth) {
   		scrollInMiniCartIndex += 10;
    	document.getElementById("zo_mini_cart_body").style.left = scrollInMiniCartLeft - scrollInMiniCartIndex;
    	document.getElementById("zo_mini_cart_body").style.width = scrollInMiniCartIndex;
    	setTimeout("scrollInMiniCart()", 1);
    } else { 	
    	document.getElementById("zo_mini_cart_body").style.left = scrollInMiniCartLeft - scrollInMiniCartWidth;
    	document.getElementById("zo_mini_cart_body").style.width = scrollInMiniCartWidth;
   	}
    return true;  
}

function ajx_retLoadMiniCart(){
	if (xhr_minicart.readyState == 4) {
    	if (xhr_minicart.status == 200) {
    	  	if (document.getElementById("zo_mini_cart").innerHTML == "") {
    	    	document.getElementById("zo_mini_cart").innerHTML = xhr_minicart.responseText;
    	    	if (document.getElementById("zo_mini_cart_body") != null) {
	    			scrollInMiniCartIndex = 0;
	    			scrollInMiniCartLeft = 853;
	    			scrollInMiniCartWidth = 204;
	    	    	document.getElementById("zo_mini_cart_body").style.width = scrollInMiniCartIndex;
	    	    	document.getElementById("zo_mini_cart_body").style.left = scrollInMiniCartLeft;
	    	    	setTimeout("scrollInMiniCart()", 1);
	    	    }	
    	  	} else {  
    	    	document.getElementById("zo_mini_cart").innerHTML = xhr_minicart.responseText;
    	  	};  
        } else {
            // tu nichts
        }
    }
    return true;  
}

function ajxLoadMiniCart(){
 
    xhr_minicart = getXHR();

    xhr_minicart.onreadystatechange = ajx_retLoadMiniCart;
   
    xhr_minicart.open('GET','../index2.php?option=com_zorder&task=loadMiniCart',true);
    xhr_minicart.send(null);
    
    return true;
}

function ajx_retAddToCart(){
	if (xhr.readyState == 4) {
    	if (xhr.status == 200) {
		   	ajxLoadMiniCart();
		   	alert(unescape("Der Artikel wurde zum Warenkorb hinzugef%FCgt."));
        } else {
            alert(unescape("Der Artikel konnte nicht zum Warenkorb hinzugef%FCgt werden!"));
        }
    }
    return true;  
}

function ajxAddToCart(productid, price){
 
    xhr = getXHR();

    xhr.onreadystatechange = ajx_retAddToCart;
  
    xhr.open("GET","../index2.php?option=com_zorder&task=addProductToCart&pro_id="+productid+"&pro_price="+price,true);
    xhr.send(null);
    
    return true;
}

function deleteFromCart(ordid,orpid){
	document.Cart.task.value="delteProductFromCart";
	document.Cart.ordid.value=ordid;
	document.Cart.orpid.value=orpid;
	if (typeof document.Cart.onsubmit == "function") {
		document.Cart.onsubmit();
	}
	document.Cart.submit();
 
//    xhr.open("GET","index2.php?option=com_zorder&task=delteProductFromCart&orp_id="+id,true);
}

function refreshCart(nextPage){
    if (nextPage == undefined) {
	  document.Cart.nextPage.value="cart";
	} else {  
	  document.Cart.nextPage.value=nextPage;
    } 
	document.Cart.task.value="refreshCart";
	document.Cart.order_kind.value="edit";
	if (typeof document.Cart.onsubmit == "function") {
		document.Cart.onsubmit();
	}
	document.Cart.submit();
}

function checkMandatoryFields(checkAGB){ 
	if (checkAGB && !document.getElementsByName("cbAGB")[0].checked) {
		alert(unescape("Achtung, Sie m%FCssen unserer AGB zustimmen, um eine Bestellung abschicken zu k%F6nnen!"));
		return false;
	};
	if (document.getElementsByName("ord_name")[0].value == "" || document.getElementsByName("ord_street")[0].value == "" || document.getElementsByName("ord_zip")[0].value == "" || document.getElementsByName("ord_city")[0].value == "" || document.getElementsByName("ord_email")[0].value == "") {
		alert(unescape("Achtung, Felder welche mit einem Stern(*) gekennzeichnet sind,\nm%FCssen eingegeben werden!"));
		return false;
	} else {
		return true;
	}	
}

function checkCart(){ 
    if (checkMandatoryFields(false)) {
		document.Cart.task.value="refreshCart";
		document.Cart.order_kind.value="check";
		if (typeof document.Cart.onsubmit == "function") {
			document.Cart.onsubmit();
		}
		document.Cart.submit();
	}
}

function sendCart(){
    if (checkMandatoryFields(true)) {
		document.Cart.task.value="sendCart";
		document.Cart.order_kind.value="send";
		if (typeof document.Cart.onsubmit == "function") {
			document.Cart.onsubmit();
		}
		document.Cart.submit();
	}
}

function calcCartPosSum(id, price, amount) {
  	lsum = document.getElementById("zo_cart_sum").innerHTML;
  	lsum = lsum - document.getElementById("orp_cart_pos_sum"+id).innerHTML;
  	document.getElementById("orp_cart_pos_sum"+id).innerHTML = formatPriceAndAmount((amount * price));
  	document.getElementById("zo_cart_sum").innerHTML = formatPriceAndAmount(lsum + (amount * price));
  	return true;
}

function formatPriceAndAmount(iValue) {
  	return number_format(iValue, 2, '.', '');
}

function number_format( /* in: float   */ number,
                        /* in: integer */ laenge,
                        /* in: String  */ sep,
                        /* in: String  */ th_sep ) {

  number = Math.round( number * Math.pow(10, laenge) ) / Math.pow(10, laenge);
  str_number = number+"";
  arr_int = str_number.split(".");
  if(!arr_int[0]) arr_int[0] = "0";
  if(!arr_int[1]) arr_int[1] = "";
  if(arr_int[1].length < laenge){
    nachkomma = arr_int[1];
    for(i=arr_int[1].length+1; i <= laenge; i++){  nachkomma += "0";  }
    arr_int[1] = nachkomma;
  }
  if(th_sep != "" && arr_int[0].length > 3){
    Begriff = arr_int[0];
    arr_int[0] = "";
    for(j = 3; j < Begriff.length ; j+=3){
      Extrakt = Begriff.slice(Begriff.length - j, Begriff.length - j + 3);
      arr_int[0] = th_sep + Extrakt +  arr_int[0] + "";
    }
    str_first = Begriff.substr(0, (Begriff.length % 3 == 0)?3:(Begriff.length % 3));
    arr_int[0] = str_first + arr_int[0];
  }
  return arr_int[0]+sep+arr_int[1];
}


