//erstellen des requests
var req = false;
	
function doBasket(command, index) {

	if (!index) index = "";
	
	if (command == "addToBasket") {
		command = command + "&articleID=" + document.getElementById("articleID").value;
	}

	if (command == "changeNum") {
		articleField = "articleID[" + index + "]";
		numberField = "number[" + index + "]";
		if (document.getElementById(numberField).value == 0) {
			command = "delete&articleID=" + document.getElementById(articleField).value;
		} else {
			command = command + "&articleID=" + document.getElementById(articleField).value + "&number=" + document.getElementById(numberField).value;
		}
		number = document.getElementById(numberField).value;
	}

	if (command == "delete") {
		articleField = "articleID[" + index + "]";
		command = command + "&articleID=" + document.getElementById(articleField).value;
	}

	selectables = document.getElementsByName("selectable[]").length

	for (i=0; i<selectables; i++) {

		selectfieldname = document.getElementsByName("selectable[]")[i].value;
		command = command + "&" + selectfieldname + "=" + document.getElementsByName(selectfieldname)[0].value; 

	}
    
	
	
	try{
		req = new XMLHttpRequest();
	}
	catch (ms){
       try{
           req = new ActiveXObject("Msxml2.XMLHTTP");
       } 
       catch (nonms){
           try{
               req = new ActiveXObject("Microsoft.XMLHTTP");
           } 
           catch (failed){
               req = null;
           }
       }  
   }

   if (req == null) alert("Error creating request object!");

   var numArticles = 0;
   
   if (document.getElementById("numArticles")) {
	   numArticles = document.getElementById("numArticles").firstChild.nodeValue;
   }

     
   //anfrage erstellen (GET, url ist localhost,
   //request ist NICHT asynchron!! (false) Hat sonst nicht funktioniert!!!      
   req.open("GET", '../kappes/doBasket.php?command='+command, false);
	

   req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   req.send();
   
   //Beim abschliessen des request wird diese Funktion ausgeführt
   req.onreadystatechange = interpretRequest;
}


function interpretRequest() {            
	switch(req.readyState) {
		case 4:
            if(req.status!=200) {
                alert("Fehler:"+req.status); 
            } else { 
                if (req.responseText) {
                    check = command.indexOf("changeNum") + 1;
                    if (check) {
                        document.getElementById(numberField).value = number;
                    }
	                   document.getElementById("numArticles").firstChild.nodeValue = Number(req.responseText);
	                   }
            }
            break;
    
        default:
            return false;
            break;     
	}
}

/*
 * loads and shows product image from preview
 */
function loadImage(imagefile) {
	// set src of product picture to new picture
	document.images.productpicture.src = imagefile;
	// create path to Big Picture
	picturePath = document.images.productpicture.src;
	// get position of last /
	slashpos = picturePath.lastIndexOf("/") + 1;
	// set new big picture path
	newPicturePath = picturePath.substring(0, slashpos) + "big_" + picturePath.substring(slashpos);
	// set path in lightbox link to big picture
	document.getElementById("pictureLink").href = newPicturePath;
}

/*
 * Show dialog window by fading in
 */
function showDialog() {
	document.getElementById("dialogouterwrapper").style.display = "block";
	var millisec = 200;
	var speed = Math.round(millisec / 100);
	var timer = 0;	
	
	for (i=0; i<=100; i++) {
		setTimeout("setOpacity("+i+")", (timer * speed));
		timer++;
	}
}
// opacity function for showDialog 
function setOpacity(value) {
	document.getElementById("dialogouterwrapper").style.opacity = (value/100);
}

/*
 * Closes dialog window
 */
function closeactionwindow() {
	document.getElementById("dialogouterwrapper").style.display = "none";
}

