/**
 * @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);
	}
}


/*QUICKORDERFORM FUNCTIONS*/
function checkArticle(field, value)
{
	if(isSet(value))
	{
		loadFile('POST', 'includes/articleFunc.php', 'function=checkReference&value='+value, 'json',function (data) {postCheckArticle(field, data);});
	}
	else
	{
		clearQuickOrderLine(field);
	}
}



function clearQuickOrderLine(field)
{
	if(window.location.href.indexOf('ordertype')!=-1)
	{
		document.getElementById('addButton'+field).style.display="none";
	}

	if(document.getElementById('quickDesc'+field).tagName.toLowerCase()=='input')
	{
		document.getElementById('quickDesc'+field).value='';
	}
	else
	{
		var inputBox=document.createElement('input');
		inputBox.setAttribute('id','quickDesc'+field);
		inputBox.setAttribute('type','text');
		inputBox.setAttribute('disabled','disabled');
		inputBox.setAttribute('value','');
		inputBox.setAttribute('size','45');
		document.getElementById('quickDesc'+field).parentNode.replaceChild(inputBox,document.getElementById('quickDesc'+field));
	}
	document.getElementById('quickMess'+field).innerHTML='';
	document.getElementById('quickStat'+field).src='/images/general/layout/transparent.gif';
	document.getElementById('quickCat'+field).value='';
	document.getElementById('quickFinalRef'+field).value='';
	document.getElementById('quickStock'+field).value='';
	document.getElementById('quickMinQty'+field).value='';
	document.getElementById('quickSelf'+field).value='';
}



function postCheckArticle(fieldNumber, data)
{
	clearQuickOrderLine(fieldNumber);
	document.getElementById('quickStat'+fieldNumber).src='/images/general/layout/badInput.png';
	if(data.colorCode!='red')
	{
		if(data.type=='product')
		{
			var selectionBox=document.createElement('select');
			selectionBox.setAttribute('id','quickDesc'+fieldNumber);
			selectionBox.setAttribute('onchange','selectArticle('+fieldNumber+', this.options[this.selectedIndex].value);');
			selectionBox.onchange=function (){selectArticle(fieldNumber, this.options[this.selectedIndex].value);};
			var option=document.createElement('option');
			optionValue=document.createTextNode(quickOrderXml.getElementsByTagName("selectColor")[0].childNodes[0].nodeValue);
			option.appendChild(optionValue);
			selectionBox.appendChild(option);

			for(var article in data.articles)
			{
				var option=document.createElement('option');
				option.setAttribute('value',article);
				optionValue=document.createTextNode(data.articles[article].title);
				option.appendChild(optionValue);
				selectionBox.appendChild(option);
			}
			document.getElementById('quickDesc'+fieldNumber).parentNode.replaceChild(selectionBox,document.getElementById('quickDesc'+fieldNumber));
			document.getElementById('quickStock'+fieldNumber).value=capitalize(data.articles[article].colorCode);
			document.getElementById('quickCat'+fieldNumber).value=data.articles[article].catalog.catalogue;
			document.getElementById('quickMinQty'+fieldNumber).value=data.articles[article].catalog.amount;
			document.getElementById('quickSelf'+fieldNumber).value=data.articles[article].self;
		}
		else if(data.type=='article')
		{
			var stockColor=capitalize(data.colorCode);
			document.getElementById('quickDesc'+fieldNumber).value=data.title;
			document.getElementById('quickStock'+fieldNumber).value=stockColor;
			document.getElementById('quickCat'+fieldNumber).value=data.catalog.catalogue;
			document.getElementById('quickFinalRef'+fieldNumber).value=data.artRef;
			document.getElementById('quickMinQty'+fieldNumber).value=data.catalog.amount;
			document.getElementById('quickSelf'+fieldNumber).value=data.self;
		}
		if(isSet(document.getElementById('quickQty'+fieldNumber).value))
		{
			checkQuantity(fieldNumber, document.getElementById('quickQty'+fieldNumber).value);
		}
	}
	else
	{
		document.getElementById('quickMess'+fieldNumber).innerHTML=data.errorMessage;
	}
}



function checkQuantity(fieldNumber, value)
{
	if(window.location.href.indexOf('ordertype')!=-1)
	{
		document.getElementById('addButton'+fieldNumber).style.display="none";
	}
	var statusImage='/images/general/layout/badInput.png';
	var strippedValue=value.replace(/[^0-9]+/gi,'');
	if(strippedValue != value)
	{
		document.getElementById('quickQty'+fieldNumber).value=strippedValue;
	}
	if(isSet(strippedValue))
	{
		if((isSet(document.getElementById('quickFinalRef'+fieldNumber).value)))
		{
			var minQty=document.getElementById('quickMinQty'+fieldNumber).value;
			if(strippedValue%minQty==0)
			{
				statusImage='/images/general/layout/stockSphere'+document.getElementById('quickStock'+fieldNumber).value+'.png';
				document.getElementById('quickMess'+fieldNumber).innerHTML='';
				if(window.location.href.indexOf('ordertype')!=-1)
				{
					document.getElementById('addButton'+fieldNumber).style.display="inline";
				}
			}
			else
			{
				document.getElementById('quickMess'+fieldNumber).innerHTML=quickOrderXml.getElementsByTagName("errorQty")[0].childNodes[0].nodeValue +' '+minQty;
			}
		}
		else
		{
			if(document.getElementById('quickDesc'+fieldNumber).tagName=='INPUT')
			{
				document.getElementById('quickMess'+fieldNumber).innerHTML=quickOrderXml.getElementsByTagName("errorNoValidRef")[0].childNodes[0].nodeValue;
			}
			else if(document.getElementById('quickDesc'+fieldNumber).tagName=='SELECT')
			{
				document.getElementById('quickMess'+fieldNumber).innerHTML=quickOrderXml.getElementsByTagName("errorForgotChoice")[0].childNodes[0].nodeValue;
			}
		}
	}
	else
	{
		if(isSet(document.getElementById('quickRef'+fieldNumber).value))
		{
			document.getElementById('quickMess'+fieldNumber).innerHTML=quickOrderXml.getElementsByTagName("errorForgotQty")[0].childNodes[0].nodeValue;
		}
		else
		{
			statusImage='/images/general/layout/transparent.gif';
		}
	}
	document.getElementById('quickStat'+fieldNumber).src=statusImage;
}



function selectArticle(fieldNumber, art)
{
	if(parseFloat(art)==art)
	{
		document.getElementById('quickFinalRef'+fieldNumber).value=art;
		if(isSet(document.getElementById('quickQty'+fieldNumber).value))
		{
			checkQuantity(fieldNumber, document.getElementById('quickQty'+fieldNumber).value);
		}
	}
	else
	{
		document.getElementById('quickMess'+fieldNumber).innerHTML=quickOrderXml.getElementsByTagName("errorForgotChoice")[0].childNodes[0].nodeValue;
	}
}



function multiAddCart()
{
	if(isSet(document.getElementById('lineCounter')))
	{
		var lineCounter=document.getElementById('lineCounter').value;
		var badRefs=new Array();
		var badRefCounter=0;
		var refRegex=/^([BbNn]\w{2}){0,1}\s*[0-9]{0,3}\-*[0-9]{2}$/;
		var qtyRegex=/[0-9]+/;
		for(var i=1;i<lineCounter;i++)
		{
			if((isSet(document.getElementById('quickRef'+i).value) && document.getElementById('quickRef'+i).value.match(refRegex)) && (isSet(document.getElementById('quickQty'+i).value) && document.getElementById('quickQty'+i).value.match(qtyRegex)))
			{
				if(!isSet(document.getElementById('quickMess'+i).innerHTML))
				{
					var cat=document.getElementById('quickCat'+i).value;
					var ref=document.getElementById('quickFinalRef'+i).value;
					var qty=document.getElementById('quickQty'+i).value;
					var self=document.getElementById('quickSelf'+i).value;
					addToCart(cat, ref, qty, self, 'O02');
					clearQuickOrderLine(i);
				}
				else if(window.location.href.indexOf('quickdeliveryform')==-1)
				{
					badRefs[badRefCounter]= {};
					badRefs[badRefCounter]['quickStat']=document.getElementById('quickStat'+i).src;
					badRefs[badRefCounter]['quickRef']=document.getElementById('quickRef'+i).value;
					badRefs[badRefCounter]['quickQty']=document.getElementById('quickQty'+i).value;
					if(document.getElementById('quickDesc'+i).tagName.toLowerCase()=='input')
					{
						badRefs[badRefCounter]['quickDesc']=document.getElementById('quickDesc'+i).value;
					}
					else
					{
						badRefs[badRefCounter]['quickDesc']=document.getElementById('quickDesc'+i).innerHTML;
					}
					badRefs[badRefCounter]['quickMess']=document.getElementById('quickMess'+i).innerHTML;
					badRefs[badRefCounter]['quickStock']=document.getElementById('quickStock'+i).value;
					badRefs[badRefCounter]['quickCat']=document.getElementById('quickCat'+i).value;
					badRefs[badRefCounter]['quickFinalRef']=document.getElementById('quickFinalRef'+i).value;
					badRefs[badRefCounter]['quickMinQty']=document.getElementById('quickMinQty'+i).value;
					badRefs[badRefCounter]['quickSelf']=document.getElementById('quickSelf'+i).value;
					badRefCounter++;
				}
			}
		}

		if(badRefCounter > 0)
		{
			document.getElementById('badRefs').value=JSON.stringify(badRefs);
			document.forms["badRefsForm"].submit();
		}
		else
		{
			goToCart();
		}
	}
}



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



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);
	}
*/
	if(isSet(document.getElementById('quickCat'+number).value) && isSet(document.getElementById('quickFinalRef'+number).value) && isSet(document.getElementById('quickQty'+number).value) && document.getElementById('quickSelf'+number))
	{
		loadFile('POST', '/includes/ordertypelines.php', 'numCli='+numCli+'&cmdType='+cmdType+'&currentRow='+number+'&action=insertLine&catArt='+document.getElementById('quickCat'+number).value+'&refArt='+document.getElementById('quickFinalRef'+number).value+'&qteArt='+document.getElementById('quickQty'+number).value+'&selfArt='+document.getElementById('quickSelf'+number).value, '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);});
}
*/