/**
 * @author	Johan
 * @at			2009/03/04
 * --------------------
**/
function createNewCart()
{
	stopAlert(10);
	loadFile("post", "/includes/cart.php", "function=createNewCart", false, function() {window.location.reload();});
}



function selectCart(cartToDelete, newCart)
{
	stopAlert(10);
	loadFile("post", "/includes/cart.php", "function=selectCart&cartToDelete="+cartToDelete+"&newCart="+newCart, false, function() {window.location.reload();});
}



function addToCart(catalog, artRef, qty, self, origin)
{
	if(isSet(artRef))
	{
		loadFile("post", "/includes/cart.php", "function=addToCart&catalog="+catalog+"&artRef="+artRef+"&qty="+qty+"&self="+self+"&origin="+origin, "text", postAdd);
	}
	else
	{
		startAlert(22);
	}
}



function postAdd(data)
{
	if(window.location.href.indexOf("quickdelivery")==-1)
	{
		window.scrollTo(0,0);
		cartOut(1, 0, data);
	}
}



function multiAddCart(number, origin)
{
	var badArts=new Array();
	var message="";
	var start=1;

	if(origin=="O01")
	{
		start=number;
	}

	for(var i=start;i<=number;i++)
	{
		if(document.getElementById("added"+i).value==0)
		{
			if(document.getElementById("checked"+i).value==1)
			{
				var badArt=false;
				if(document.getElementById('tete'+i).value == 1)
				{
					var reference=document.getElementById('ref'+i).options[document.getElementById('ref'+i).selectedIndex].value;
				}
				else
				{
					var reference=document.getElementById("ref"+i).value;
				}

				var availability;
				if (isSet(document.getElementById("disp"+i)))
				{
					availability=document.getElementById("disp"+i).value;
				}
				var qty=document.getElementById("qte"+i).value;
				var minQty=document.getElementById("minQty"+i).value;
				var self=document.getElementById("self"+i).value;

				if(reference!="select")
				{
					if(isSet(availability) && availability==1)
					{
						if(isSet(qty) && qty>0)
						{
							if(isSet(minQty) && minQty!=0 && (qty%minQty == 0))
							{
								var prefPattern = new RegExp("^[BN][A-Z1-9]{2}");
								var catalog=reference.match(prefPattern)[0];
								var artRef=reference.substring(4);

								if(origin=="O01")
								{
									return {'pref':catalog, 'ref':artRef, 'qty':qty, 'self':self};
								}
								else
								{
									document.getElementById("added"+i).value=1;
									addToCart(catalog, artRef, qty, self, origin);
								}
							}
							else
							{
								badArt=true;
								message+=reference+": "+xmlDoc.getElementsByTagName('errorQty')[0].childNodes[0].nodeValue+minQty+"<br />";
							}
						}
						else
						{
							badArt=true;
							message+=reference+": "+xmlDoc.getElementsByTagName('forgotQty')[0].childNodes[0].nodeValue+"<br />";
						}
					}
					else
					{
						badArt=true;
						message+=reference+": "+xmlDoc.getElementsByTagName('errorAvail')[0].childNodes[0].nodeValue+"<br />";
					}
				}
				else
				{
					badArt=true;
					message+=xmlDoc.getElementsByTagName('selectRef')[0].childNodes[0].nodeValue+"<br />";
				}
			}
		}
	}
	if(badArt==true)
	{
		if(origin!="O01")
		{
		 var container="message";
		}
		else
		{
			var container="message_"+number;
		}
		document.getElementById(container).innerHTML = message;
	}
	else if(origin!="O01")
	{
		goToCart();
	}
}



function goToCart()
{
	if(activeRequest==false)
	{
		window.location.href="orderForm.php";
	}
	else
	{
		window.setTimeout(function () { goToCart(); }, 77);
	}
}



function addToDefaultCart(number,numCli,cmdType)
{
	var article=multiAddCart(number, "O01");
	if(isSet(article))
	{
		loadFile('POST', '/includes/ordertypelines.php', 'numCli='+numCli+'&cmdType='+cmdType+'&currentRow='+number+'&action=insertLine&catArt='+article['pref']+'&refArt='+article['ref']+'&qteArt='+article['qty']+'&selfArt='+article['self'], 'text', 'lines_'+number);
	}
}



function addAwardToCart(artRef, qty, oldQty)
{
	if(!isSet(oldQty))
	{
		$oldQty=0;
	}
	loadFile("post", "/includes/cart.php", "function=addAwardToCart&artRef="+artRef+"&qty="+qty+"&oldQty="+oldQty, "json", postAddAwardToCart);
}



function postAddAwardToCart(data)
{
	if (isSet(data.errorCode))
	{
		if(window.location.href.indexOf("orderForm.php")!=-1 && isSet(data.oldQty) && isSet(data.artRef))
		{
			document.getElementById('awardQty'+data.artRef).value=data.oldQty;
		}
		startAlert(data.errorCode);
	}
	else
	{
		if(window.location.href.indexOf("awards.php")!=-1)
		{
//			document.getElementById('saldo').innerHTML=data.headerBox;
			document.getElementById('pointsAmount').innerHTML=data.newPoints;
			document.getElementById('pointsExp').innerHTML=data.expPoints;
			postAdd(data.headerBox);
//			startAlert(13);
		}
		else
		{
			window.location.reload();
		}
	}
}



function isNumeric(value)
{
  if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/))
	{
		return false;
	}
	else
	{
  	return true;
	}
}



function removeArtFromCart(catalog, artRef, qty, self, origin)
{
	loadFile("post", "/includes/cart.php", "function=removeArtFromCart&catalog="+catalog+"&artRef="+artRef+"&qty="+qty+"&self="+self+"&origin="+origin, "", window.location.href);
}



function removeAllArts()
{
	loadFile("post", "/includes/cart.php", "function=removeAllArts", "", window.location.href);
}



function returnArtToCart(catalog, artRef, qty, self, origin)
{
	loadFile("post", "/includes/cart.php", "function=returnArtToCart&catalog="+catalog+"&artRef="+artRef+"&qty="+qty+"&self="+self+"&origin="+origin, "", window.location.href);
}



function disablePlusminus(artNr)
{
	var buttons=document.getElementById("plusminus"+artNr).getElementsByTagName("a");
	var images=document.getElementById("plusminus"+artNr).getElementsByTagName("img");
	if(images.length<3)
	{
		for (var i=0;i<buttons.length;i++)
		{
			buttons[i].style.display="none";
		}
		var img=document.createElement("img");
		img.setAttribute("src", "/images/general/buttons/btn_updcmdtype.jpg");
		img.setAttribute("width", "20");
		img.setAttribute("width", "20");
		img.setAttribute("class", "recalculate");
		img.setAttribute("height", "20");
		img.setAttribute("alt", orderFormXML.getElementsByTagName('recalculate')[0].childNodes[0].nodeValue);
		document.getElementById("plusminus"+artNr).appendChild(img);
	}
}



function enablePlusminus(artRef)
{
	var renew=document.getElementById("plusminus"+artRef).getElementsByTagName("img")[2];
	renew.parentNode.removeChild(renew);

	var buttons=document.getElementById("plusminus"+artRef).getElementsByTagName("a");
	for (var i=0;i<buttons.length;i++)
	{
		buttons[i].style.display="inline";
	}
}



function changeQty(catalog, artRef, minQty, oldQty, newQty, self, origin, change)
{
	var calculate=false;
	if(oldQty!=newQty)
	{
		if(newQty>0)
		{
			if((newQty%minQty)==0)
			{
				var qty=newQty-oldQty;
				calculate=loadFile("post", "/includes/cart.php", "function=addToCart&catalog="+catalog+"&artRef="+artRef+"&qty="+qty+"&self="+self+"&origin="+origin, "", window.location.href);
			}
			else
			{
				var times=Math.floor(newQty/minQty);
				newQty=times*minQty;

				if(newQty==0)
				{
					if(oldQty!=minQty)
					{
						var qty=minQty;
						calculate=loadFile("post", "/includes/cart.php", "function=addToCart&catalog="+catalog+"&artRef="+artRef+"&qty="+qty+"&self="+self+"&origin="+origin, "", window.location.href);
					}
				}
				else
				{
					document.getElementById("qty"+artRef).value=newQty;
					var qty=newQty-oldQty;
					calculate=loadFile("post", "/includes/cart.php", "function=addToCart&catalog="+catalog+"&artRef="+artRef+"&qty="+qty+"&self="+self+"&origin="+origin, "", window.location.href);
				}
				startAlert(7, { "minAmount": minQty, "newAmount":  newQty });
			}
		}
		else if(newQty==0)
		{
			newQty=minQty;
			removeArtFromCart(catalog, artRef, newQty, self, origin);
		}
		else
		{//INCORRECT VALUE
			document.getElementById("qty"+artRef).value=oldQty;
		}
	}
	if(isSet(change) && !isSet(calculate))
	{
		enablePlusminus(artRef);
	}
}



function changeFurQty(cartItemId, artQty, oldQty, newQty, change)
{
	var calculate=false;

	if(newQty>artQty)
	{
		newQty=artQty;
	}

	document.getElementById("ass"+cartItemId).value=newQty;

	if(oldQty!=newQty)
	{
		if(newQty>=0)
		{
			calculate=loadFile("post", "/includes/cart.php", "function=changeFurQty&cartItemId="+cartItemId+"&qty="+newQty, "", window.location.href);
		}
	}


	if(isSet(change) && !isSet(calculate))
	{
		enablePlusminus(cartItemId);
	}
}



function removeAwardFromCart(artRef, qty)
{
	loadFile("post", "/includes/cart.php", "function=removeAwardFromCart&artRef="+artRef+"&qty="+qty, "", window.location.href);
}



function returnAwardToCart(artRef, qty)
{
	loadFile("post", "/includes/cart.php", "function=returnAwardToCart&artRef="+artRef+"&qty="+qty, "", window.location.href);
}



function changeAwardQty(artRef, oldQty, newQty)
{
	if(oldQty!=newQty)
	{
		if(newQty>0)
		{
			document.getElementById("awardQty"+artRef).value=newQty;
			var qty=newQty-oldQty;
			addAwardToCart(artRef, qty, oldQty);
		}
		else if(newQty==0)
		{
			removeAwardFromCart(artRef, oldQty);
		}
		else
		{
			//incorrect value alert
		}
	}
}



function changeAwardGiftQty(artRef, oldQty, newQty)
{
	if(newQty>0)
	{
		qty=1;
	}
	else
	{
		qty=0;
	}

	if(oldQty!=qty)
	{
		document.getElementById("awardQty"+artRef).value=qty;
		loadFile("post", "/includes/cart.php", "function=changeAwardGiftQty&giftRef="+artRef+"&qty="+qty, "", window.location.href);
	}
}



function removeCodeFromCart()
{
	loadFile("post", "/includes/cart.php", "function=removeCodeFromCart", "", window.location.href);
}



function removeAllAwards()
{
	loadFile("post", "/includes/cart.php", "function=removeAllAwards", "", window.location.href);
}



function setWarranty(value)
{
	loadFile("post", "/includes/cart.php", "function=setWarranty&value="+value, "json", postSetWarranty);
}



function postSetWarranty(data)
{
	document.getElementById("warranty").innerHTML=data.warranty;
	document.getElementById("subtotal").innerHTML=data.subtotal;
	document.getElementById("highTax").innerHTML=data.highTax;
	document.getElementById("total").innerHTML=data.total;
}


function cartIn(bottom, padding, data)
{
	var cartButton=document.getElementById('saldo').getElementsByTagName('div')[0];
	bottom+=3;
	padding--;
	cartButton.style.bottom=bottom+"px";
	cartButton.style.paddingTop=padding+"px";

	if(bottom<1)
	{
		window.setTimeout(function () { cartIn(bottom, padding, data); }, 77);
	}
	else
	{
		document.getElementById('saldo').innerHTML=data;
	}
}



function cartOut(bottom, padding, data)
{
	var cartButton=document.getElementById('saldo').getElementsByTagName('div')[0];
	bottom-=3;
	padding++;
	cartButton.style.bottom=bottom+"px";
	cartButton.style.paddingTop=padding+"px";

	if(bottom>-21)
	{
		window.setTimeout(function () { cartOut(bottom, padding, data); }, 77);
	}
	else
	{
		window.setTimeout(function () { cartIn(bottom, padding, data); }, 77);
	}
}


/*jbeg zegt: vervangen door form
function changeFurSpecialChoice(ref)
{
	loadFile("post", "/includes/cart.php", "function=changeFurSpecialChoice&ref="+ref, 0, function () {setTimeout(window.location.reload(), 5000);});
}
*/