//



function deleteArticle() {
	var shopCartSel = document.getElementById('cart-select');
	var pos = shopCartSel.options[shopCartSel.selectedIndex].value;
	DWRUtil.useLoadingMessage();
	DWREngine._execute(_ajaxConfig._cfscriptLocation,null,'cartDeleteArticle',{"arrPos":pos},deleteArticle_return);
}

function deleteArticle_return(retVar) {
	var shopCart = document.getElementById('shopCart');
	var shopCartSel = document.getElementById('cart-select');
	
	shopCartSel.length = 0;
	
	if(retVar.LEN == 0) {
		shopCart.style.display = 'none';
	} else {
		for(i=0; i < retVar.ARR.length; i++) {
			addOption(shopCartSel,retVar.ARR[i][0] +' ('+ retVar.ARR[i][1] +')',i+1);
		}
	}
}

function emptyCart(location) {
	var c = confirm('Do you really want to empty the entire cart?');
	
	if(typeof location != 'undefined') {
		var action = location;
	} else {
		var action = 'none';
	}
	
	if(c) {
		DWRUtil.useLoadingMessage();
		DWREngine._execute(_ajaxConfig._cfscriptLocation,null,'cartEmpty',{'doneAction':action},emptyCart_return);
	}
}

function emptyCart_return(retVar) {
	switch(retVar){
	case 'reload':
		window.location.reload();
		break;
	default:
		var shopCart = document.getElementById('shopCart');
		var shopCartSel = document.getElementById('cart-select');
		
		shopCartSel.length = 0;
		shopCart.style.display = 'none';
	}
}

function ShowNumAlert()
{
	alert('Ange ett heltal');
}

function IsInteger(input)
{
	 if (input.match(/^\d+$|^\d+\.\d{2}$/ ) )
		 return true;
	 else
		 return false;
}
function addArticle(artid, quant, NoReload) {
	if (quant.length == 0)
	{
		alert('Ange ett antal');
		return false;
	}
	if (!IsInteger(quant))
		{
		ShowNumAlert();
		return false;
		}
	
	DWREngine._execute(_ajaxConfig._cfscriptLocation,null,'cartAddArticle',{"artID":artid,"qty":quant},addArticle_return);
	//return true;
}

function addArticle(artid, quant, NoReload) {
	if (quant.length == 0)
	{
		alert('Ange ett antal');
		return false;
	}
	if (!IsInteger(quant))
		{
		ShowNumAlert();
		return false;
		}
	
	var noCache = new Date().getTime();
	DWREngine._execute(_ajaxConfig._cfscriptLocation +'?noCache='+ noCache,null,'cartAddArticle',{"artID":artid,"qty":quant},addArticle_return);
	//return true;
}

function addArticleList(artList,amountList) {
	var noCache = new Date().getTime();
	
	$.post('/core/components/module/webshop2.cfc', {
		method: 'cartAddArticleList',
		artList: artList,
		amountList: amountList,
		noCache: noCache
	}, function(data) {
		addArticleList_return(data);
	},'json')
}

function addArticleList_return(data,reload) {
	var shopCart = $('#shopCart'); 
	shopCart.css('display','block');
	
	var shopCartSel =  $('#cart-select');
	shopCartSel.empty();
	
	if(data.ARR.DATA.length == 0) {
		shopCart.style.display = 'none';
	} else {
		$(data.ARR.DATA).each(function(i) {
			shopCartSel.append('<option value="'+ i +'">'+ data.ARR.DATA[i][5] +' ('+ data.ARR.DATA[i][1] +')</option>');
		});
		
		if(reload) {
			this.location.reload();
		}
	}
}

function addArticle_withoutCheck(artid, quant) {
	DWREngine._execute(_ajaxConfig._cfscriptLocation,null,'cartAddArticle',{"artID":artid,"qty":quant},addArticle_return);
}

function UpdateCartQuantity()
{
	DWRUtil.useLoadingMessage();
	var MyObj = new Object();
	for (this_item in cartItems)
	{
		var quant = document.getElementById('qty'+this_item).value;
		if (!IsInteger(quant))
			{
			ShowNumAlert();
			return;
			}
		MyObj[this_item] =quant ; 
	}
	DWREngine._execute(_ajaxConfig._cfscriptLocation,null,'cartUpdateArticleCount',{"Articles":MyObj},UpdateCartQuantity_return);
}



function UpdateCartQuantity_return(retVar)
{
	addArticle_return(retVar,true);
	window.location = window.location;
}

function addArticle_return(retVar,reload) {
	var shopCart = document.getElementById('shopCart');
	shopCart.style.display = 'block';
	
	var shopCartSel = document.getElementById('cart-select');
	//console.log(retVar.ARR.data['strproductname'][0]);
	shopCartSel.length = 0;
	
	if(retVar.LEN == 0) {
		shopCart.style.display = 'none';
	} else {
		for(i=0; i < retVar.LEN; i++) {
		//	addOption(shopCartSel,retVar.ARR[i][0] +' ('+ retVar.ARR[i][1] +')',i+1);
			addOption(shopCartSel, retVar.ARR['data']['strproductname'][i] + ' ('+retVar.ARR['data']['intorderarticlequantity'][i]+')', i+1);
		}
		
		if(reload) {
			this.location.reload();
		}
	}
}

function addOption(selectbox,text,value) {
	var optn = document.createElement("option");
	optn.text = text;
	optn.value = value;
	
	selectbox.options.add(optn);
}

function downloadFile(downloadid) {
	location.href='/downloads/download.cfm?downloadID='+downloadid;
}


