// INIT SETTINGS -----------------------------------------

var name = 'tackit.cookie'
var myString = 'id=0|#|amount=0|#|price=0|#|name=0';

if(getCookie() != null) {

	myString = getCookie();
}

function setCookie(value)
{
    document.cookie= name + "=" + escape(value);
}

function getCookie()
{

    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie() {
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);  // This cookie is history
	var cval = getCookie (name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
	showbasket();
}

function addtobasket(me){

	if(me.Dato.value==''){ alert('Please select a date'); return false; }

	var id = me.DocumentId.value  + '_' + me.Dato.value;
	var amount = 1;
	var price = me.Price.value;
	var name = me.DocumentName.value + ' ' + me.Dato.value;

	addbasket(id,amount,price,name);

}

function addbasket(id,amount,price,name) {

	var myArray
		myArray = myString.split(";");

	var i,myItem,myId, myAmount, myPrice, myName, ItemExist

	ItemExist = false;

	for (i = 0; i <= myArray.length-1; i++) {
		myItem = myArray[i].split("|#|");
		myId = myItem[0].split("=")[1];
		myAmount = myItem[1].split("=")[1];
		myPrice = myItem[2].split("=")[1];
		myName = myItem[3].split("=")[1];

		if (id == myId) { //Hvis produktet findes i kurven læg antal til
			var myNewAmount = 0
//			myNewAmount = parseInt(myAmount) + parseInt(amount);

			myString = myString.replace('id='+myId+'|#|amount='+myAmount, 'id='+myId+'|#|amount='+amount);
			
			setCookie(myString);

			ItemExist = true;
			break;
		}
	}

	if (!ItemExist) {
		myString = myString +';id='+id+'|#|amount='+amount+'|#|price='+price+'|#|name='+name;
		setCookie(myString);
	}

	myString = getCookie();
	
	calculatebasket(myString);
	
	alert(name+ ' has been added to your booking list');
}

function updatebasket(id,amount) {

	var myArray
		myArray = myString.split(";");

	var i,myItem,myId, myAmount, myName, ItemExist

	ItemExist = false;	

	for (i = 0; i <= myArray.length-1; i++) {
		myItem = myArray[i].split("|#|");
		myId = myItem[0].split("=")[1];
		myAmount = myItem[1].split("=")[1];
		myPrice = myItem[2].split("=")[1];
		myName = myItem[3].split("=")[1];

		if (id == myId) {

			myNewAmount = amount;
			
				if (myNewAmount == 0) {
					myString = myString.replace(';id='+myId+'|#|amount='+myAmount+'|#|price='+ myPrice +'|#|name='+myName, '');
					setCookie(myString);
				}
				else {
					myString = myString.replace('id='+myId+'|#|amount='+myAmount, 'id='+myId+'|#|amount='+myNewAmount);
					setCookie(myString);
				}

			break;
		}
	}
renderhiddenfields()
}

function calculatebasket(myString) {

	myArray = myString.split(";");

	var totalAmount, totalPrice, Product
	totalAmount = 0;
	totalPrice = 0.00;
	myPrice = 0;
	Product = '';
	
	
	for (i = 1; i <= myArray.length-1; i++) {
		myItem = myArray[i].split("|#|");
		myId = myItem[0].split("=")[1];
		myAmount = myItem[1].split("=")[1];
		myPrice = myItem[2].split("=")[1];
		myName = myItem[3].split("=")[1];
		
		totalAmount = totalAmount + parseInt(myAmount);
		totalPrice = totalPrice + (parseInt(myAmount) * parseFloat(myPrice));
		
		Product = Product + '<tr><td style="border-bottom: 1px solid #ffffff; padding-left: 10px">'+myName+'</td><td align="center" style="border-bottom: 1px solid #ffffff; border-left: 1px solid #ffffff; border-right: 1px solid #ffffff">'+myAmount+' stk.</td><td align="right" style="border-bottom: 1px solid #ffffff; padding-right: 10px">DKK '+formatNumber(myPrice)+' pr.stk</td></tr>' 

	}

	if (Product=='') {
		Product = '<div class="BreadMediumStdColor" style="border-bottom: 1px solid #ffffff;width:250px;padding-left:10px;padding-top:5px;padding-bottom:5px;background-color:#D8E2E5;">Du har ingen varer i kurven</div>' 
	}
	else {
		Product = '<div class="BreadMediumStdColor" style="background-color:#D8E2E5;"><table class="BreadMediumStdColor" width="250" cellpadding="4" cellspacing="0">'+Product+'</table></div>'
	}

	if (document.getElementById('Products')) {
		document.getElementById('Products').innerHTML = Product;
	}		

	if (document.getElementById('basketAmount')) {
		document.getElementById('basketAmount').innerHTML = totalAmount;
	}
	if (document.getElementById('basketPrice')) {
		document.getElementById('basketPrice').innerHTML = 'kr. ' + formatNumber(totalPrice);
	}
}


function showbasket() {

	calculatebasket(myString);

}

function isbasketempty() {

	var totalAmount, price, totalPrice
	price = 0;
	moms = 0;
	totalAmount = 0;
	totalPrice = 0.00;

	myArray = myString.split(";");

	for (i = 1; i <= myArray.length-1; i++) {
		myItem = myArray[i].split("|#|");
		myId = myItem[0].split("=")[1];
		myAmount = myItem[1].split("=")[1];
		myPrice = myItem[2].split("=")[1];
		
		price = parseInt(myAmount) * parseFloat(myPrice);
		totalAmount = totalAmount + parseInt(myAmount);
		totalPrice = totalPrice + (parseInt(myAmount) * parseFloat(myPrice));
		
	}

	if (totalAmount == 0) {
		sOut = '<input type="hidden" name="basketisempty" value="true">';
	}
	else {	
		sOut = '<input type="hidden" name="basketisempty" value="false">';
	}

	document.write (sOut);

}

function showfullbasket() {

	var BasketHeaderColor = '#ffffff';
	var ItemColor = BasketHeaderColor;
	var subTotalColor = '#ffffff';
	var TotalColor = subTotalColor;
	var BorderColor = '#cccccc';
	var ItemNameWidth = '31%';
	var UnitPriceWidth = '23%';
	var AmountWidth = '23%';
	var TotalPriceWidth = '23%';
	
	myArray = myString.split(";");

	var totalAmount, price, totalPrice, moms, strOut
	price = 0;
	moms = 0;
	totalAmount = 0;
	totalPrice = 0.00;
	strOut = '';
	
	var TableWidth = 330
		
	strOut = strOut + '<script type="text/javascript">'
	strOut = strOut + 'function checkamount(id,value){'
	strOut = strOut + 'if(isNaN(value)){alert("Ugyldigt tal"); document.location=document.location; return false;}'
	strOut = strOut + '  if(value.length>1 && value.charAt(0)==0){alert("Ugyldigt tal"); document.location=document.location; return false;}'
	strOut = strOut + '	 else{updatebasket(id,value); document.location=document.location}'
	strOut = strOut + '}'
	strOut = strOut + '</script>'
	
	strOut = strOut + '<table class="BreadMediumStdColor" width="'+TableWidth+'" bgcolor="'+BorderColor+'" border="0" cellpadding="5" cellspacing="1">';
//	strOut = strOut + '<tr bgcolor="'+BasketHeaderColor+'"><td width="'+ItemNameWidth+'">VARENAVN</td><td width="'+UnitPriceWidth+'" align="right">STYKPRIS</td><td width="'+AmountWidth+'" align="right">ANTAL</td><td width="'+TotalPriceWidth+'" align="right">SAMLET</td></tr>';
	
	for (i = 1; i <= myArray.length-1; i++) {
		myItem = myArray[i].split("|#|");
		myId = myItem[0].split("=")[1];
		myAmount = myItem[1].split("=")[1];
		myPrice = myItem[2].split("=")[1];
		myName = myItem[3].split("=")[1];
		
		price = parseInt(myAmount) * parseFloat(myPrice);
		totalAmount = totalAmount + parseInt(myAmount);
		totalPrice = totalPrice + (parseInt(myAmount) * parseFloat(myPrice));
		moms = totalPrice * 0.25;
		
		strOut = strOut + '<tr bgcolor="'+ItemColor+'"><td>' + myName + '</td><td><img style="cursor: pointer" onclick="updatebasket(\'' + myId + '\',0); document.location=document.location" src="AjrGfx/javascript/bin.gif" width="12" height="14" alt="Fjern" title="Fjern"></td><td align="right">' + formatNumber(myPrice) + '</td></tr>';
	}
	
	strOut = strOut + '</table>';
	
	var strGebyrTxt
//	strGebyrTxt = 'Ekspeditionsgebyr (kr. 10,-) og forsendelsesgebyr (kr. 15,-) vil blive lagt til din ordre.'
	
	strOut = strOut + '<table class="BreadMediumStdColor" width="'+TableWidth+'" border="0" cellpadding="5" cellspacing="0">';
	strOut = strOut + '<tr bgcolor="'+subTotalColor+'"><td style="font-weight: bold; border-left: 1px solid '+BorderColor+'; border-bottom: 1px solid '+BorderColor+'">TOTAL:</td><td style="font-weight: bold; border-right: 1px solid '+BorderColor+'; border-bottom: 1px solid '+BorderColor+'" align="right">DKK ' + formatNumber(totalPrice) + '</td></tr>';
	strOut = strOut + '<tr bgcolor="'+subTotalColor+'"><td style="font-weight: bold; border-left: 1px solid '+BorderColor+'; border-bottom: 1px solid '+BorderColor+'">VAT:</td><td style="font-weight: bold; border-right: 1px solid '+BorderColor+'; border-bottom: 1px solid '+BorderColor+'" align="right">DKK ' + formatNumber(moms) + '</td></tr>';
	strOut = strOut + '<tr bgcolor='+TotalColor+'><td style="font-weight: bold; border-left: 1px solid '+BorderColor+'; border-bottom: 1px solid '+BorderColor+'">AMOUNT INCL. VAT:</td><td style="font-weight: bold; border-right: 1px solid '+BorderColor+'; border-bottom: 1px solid '+BorderColor+'" align="right">DKK ' + formatNumber(totalPrice+moms) + '</td></tr>';
//	strOut = strOut + '<tr><td colspan="2" style="border-left: 1px solid '+BorderColor+'; border-right: 1px solid '+BorderColor+'; border-bottom: 1px solid '+BorderColor+'">' + strGebyrTxt + '</td></tr>';
	strOut = strOut + '</table>';

//	strOut = strOut +'<table style="padding-top: 15px;" width="'+TableWidth+'" border="0" cellpadding="0" cellspacing="0">';		
//	strOut = strOut + '<tr><td align="right"><input class="ButtonInputsMedium" onfocus="this.blur()" onMouseOver="this.style.cursor=\'pointer\';" onclick="renderhiddenfields(); ProcessAjrForm112();" type="button" value="Submit booking"></td></tr>';
//	strOut = strOut + '<tr><td align="right"><input class="ButtonInputsMedium" onMouseOver="this.style.cursor=\'pointer\';" onfocus="this.blur()" onclick="history.back()" type="button" value="Tilbage">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input class="ButtonInputsMedium" onMouseOver="this.style.cursor=\'pointer\';" onfocus="this.blur()" type="button" value="Opdatér" onclick=" document.location=document.location"></td></tr>';
//	strOut = strOut + '</table>';

	
	if (totalAmount == 0) {

		strOut = '<table class="BreadMediumStdColor" width="'+TableWidth+'" border="0" cellpadding="4" cellspacing="0">';		
		strOut = strOut + '<tr><td style="border: 1px solid #cccccc;">No course selected!</td></tr>';
		strOut = strOut + '</table>';

		strOut = strOut +'<table style="padding-top: 15px;" width="'+TableWidth+'" border="0" cellpadding="0" cellspacing="0">';		
		strOut = strOut + '<tr><td align="right"><input class="ButtonInputsMedium" onMouseOver="this.style.cursor=\'pointer\';" onfocus="this.blur()" onclick="history.back()" type="button" value="Back"></td></tr>';
		strOut = strOut + '</table>';

		document.write (strOut);
	}
	else {	
		document.write (strOut);
	}
	
}

function renderhiddenfields() {

	myArray = myString.split(";");

	var totalAmount, totalPrice
	totalAmount = 0;
	totalPrice = 0.00;
	myPrice = 0;
	strOut = '';
	var Out = '';

	for (i = 1; i <= myArray.length-1; i++) {
		myItem = myArray[i].split("|#|");
		myId = myItem[0].split("=")[1];
		myAmount = myItem[1].split("=")[1];
		myPrice = myItem[2].split("=")[1];
		myName = myItem[3].split("=")[1];
		
//	strOut = strOut + '<input type="hidden" name="doc_'+myId+'" value="'+myAmount+'">';
//	strOut = strOut + '<input type="hidden" name="price_'+myId+'" value="'+formatNumber(myPrice)+'">';
//	strOut = strOut + '<input type="hidden" name="fDocuments" value="'+myId+'">';

		Out = Out + myName+'<br>';

	}

	strOut = '<input type="hidden" name="fKurser" value="'+Out+'">';

	if (document.getElementById('HiddenFields')) { document.getElementById('HiddenFields').innerHTML = strOut; }
}

function formatNumber(Number)
{

//	Uses NumberFormat.js file
//	For more options visit http://www.mredkj.com/javascript/numberFormatPage2.html (STT)

	var num = new NumberFormat();
	num.setInputDecimal('.');
	num.setNumber(Number);
	num.setPlaces('0', true);
	num.setCurrencyValue('');
	num.setCurrency(true);
	num.setCurrencyPosition(num.LEFT_OUTSIDE);
	num.setNegativeFormat(num.LEFT_DASH);
	num.setNegativeRed(false);
	num.setSeparators(true, ',', ',');
	
	return num.toFormatted()
}
