// ShopReturnUrl bliver sat i Ajour hos elementet "Indkøbskurv"
var objCookieMdl = new CookieModule();
var ShopReturnUrl = objCookieMdl.getValueByCookieAndKey('User','ShopReturnUrl');

var cookieName = 'famlabhrshopbasket';
var myString = getBasketCookie();

if (myString != null) {
	myString = getBasketCookie();
}
else {
	myString = 'id€0|€|showid|€|amount€0|€|price€0|€|name€0|€|type€0';
	setBasketCookie(myString);
}

function setBasketCookie(value)
{
    document.cookie= cookieName + "=" + escape(value);
}

function getBasketCookie()
{
    var dc = document.cookie;
    var prefix = cookieName + "=";
    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 removeBasketCookie() {
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);  // This cookie is history
	var cval = getBasketCookie (cookieName);
	document.cookie = cookieName + "=" + cval + "; expires=" + exp.toGMTString();

	myString = 'id€0|€|showid|€|amount€0|€|price€0|€|name€0|€|type€0';
	setBasketCookie(myString);
}

function deleteBasketCookie() {
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);  // This cookie is history
	var cval = getBasketCookie (cookieName);
	document.cookie = cookieName + "=" + cval + "; expires=" + exp.toGMTString();

	myString = 'id€0|€|showid|€|amount€0|€|price€0|€|name€0|€|type€0';
	setBasketCookie(myString);
	
	showbasket();
}

function addbasket(id,amount,price,name,type) {

	myString = getBasketCookie();

	var myArray
	myArray = myString.split(";");

	var i, myItem, myId, myAmount, myPrice, myName, myType, ItemExist

	ItemExist = false;

	if (myArray.length == 11) {
		alert('Det er kun muligt at købe 10 forskellige arrangementer/produkter'); return false;
	}

	for (i = 0; i <= myArray.length-1; i++) {
		myItem = myArray[i].split("|€|");
		myId = myItem[0].split("€");
		myAmount = myItem[1].split("€");
		myPrice = myItem[2].split("€");
		myName = myItem[3].split("€");
		myType = myItem[4].split("€");

		if (id == myId[1]) { //Hvis produktet findes i indkøbskurven læg antal til

			myString = myString.replace('id€'+myId[1]+'|€|amount€'+myAmount[1]+'|€|price€'+myPrice[1]+'|€|name€'+myName[1]+'|€|type€'+myType[1], 'id€'+id+'|€|amount€'+amount+'|€|price€'+price+'|€|name€'+name+'|€|type€'+type);
			
			setBasketCookie(myString);

			ItemExist = true;
			break;
		}
	}

	if (!ItemExist) {
		myString = myString +';id€'+id+'|€|amount€'+amount+'|€|price€'+price+'|€|name€'+name+'|€|type€'+type;
		setBasketCookie(myString);
	}

	showbasket();
	
	alert(name + ' dodan je u koaricu.');

}

function updatebasket(id,amount) {

	myString = getBasketCookie();

	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("€");
		myAmount = myItem[1].split("€");
		myPrice = myItem[2].split("€");
		myName = myItem[3].split("€");
		myType = myItem[4].split("€");

		if (id == myId[1]) {

			myNewAmount = amount;
			
				if (myNewAmount == 0) {
					myString = myString.replace(';id€'+myId[1]+'|€|amount€'+myAmount[1]+'|€|price€'+ myPrice[1] +'|€|name€'+myName[1] +'|€|type€'+myType[1], '');
					setBasketCookie(myString);
				}
				else {
					myString = myString.replace('id€'+myId[1]+'|€|amount€'+myAmount[1], 'id€'+myId[1]+'|€|amount€'+myNewAmount);
					setBasketCookie(myString);
				}

			break;
		}
	}
renderhiddenfields()
}

function showbasket() {

	myString = getBasketCookie();

	myArray = myString.split(";");

	var totalAmount, totalPrice
	totalAmount = 0;
	totalPrice = 0.00;
	myPrice = 0;
	items = '';
	
	for (i = 1; i <= myArray.length-1; i++) {
		myItem = myArray[i].split("|€|");
		myId = myItem[0].split("€");
		myAmount = myItem[1].split("€");
		myPrice = myItem[2].split("€");
		myName = myItem[3].split("€");
		myType = myItem[4].split("€");

		//totalAmount = totalAmount + parseInt(myAmount[1]);
		//totalPrice = totalPrice + (parseInt(myAmount[1]) * parseFloat(myPrice[1]));
		totalPrice = totalPrice + parseFloat(myPrice[1]);
//		items += Left(myName[1],39) + '<br>';
		items += myName[1] + '<br>';
	}
	
//	if (document.getElementById('basketAmount')) {
//		document.getElementById('basketAmount').innerHTML = totalAmount;
//	}
	if (document.getElementById('basketitems')) {
		document.getElementById('basketitems').innerHTML = items;
	}
	if (document.getElementById('basketPrice')) {
		document.getElementById('basketPrice').innerHTML = formatNumber(totalPrice);
	}
	if (document.getElementById('fPrice')) {
		document.getElementById('fPrice').value = formatNumber(totalPrice);
	}	
}

function showfullbasket() {

	myString = getBasketCookie();

	myArray = myString.split(";");

	var totalAmount, price, totalPrice, moms, strOut
	price = 0;
	moms = 0;
	totalAmount = 0;
	totalPrice = 0.00;
	strOut = '';
		
	strOut = strOut + '<script>'
	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="100%" bgcolor="#cccccc" border="0" cellpadding="3" cellspacing="1">';
	strOut = strOut + '<tr bgcolor="#F2F2F2" style="text-transform: uppercase"><td>Seminar</td><td align="right">Vrsta ulaznice</td><td width="53" align="right">Broj</td><td width="88" align="right">Ukupno</td></tr>';
	
	for (i = 1; i <= myArray.length-1; i++) {
		myItem = myArray[i].split("|€|");
		myId = myItem[0].split("€");
		myAmount = myItem[1].split("€");
		myPrice = myItem[2].split("€");
		myName = myItem[3].split("€");
		myType = myItem[4].split("€");
		
		id = myId[1]
		name =  myName[1];
		pricetype = myType[1];
		itemprice = parseFloat(myPrice[1]);
		itemamount = parseInt(myAmount[1]);
		totalitemprice = itemamount * itemprice;
		totalAmount = totalAmount + itemamount

		totalPrice = totalPrice + totalitemprice
		
		moms = totalPrice * (1-(100/122));
		
		strOut = strOut + '<tr bgcolor="#ffffff"><td>' + name + '</td><td align="right">' + pricetype + '</td><td align="right"><input maxlength="3" style="font-size: 10px; text-align: center; width: 18px; border: 1px solid #D0D0D0" onblur="checkamount('+ id +',this.value);" type="text" value="' + itemamount + '"></td><td nowrap align="right">' + formatNumber(totalitemprice) + '</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="100%" border="0" cellpadding="4" cellspacing="0">';
	strOut = strOut + '<tr bgcolor="#F2F2F2"><td style="font-weight: bold; border-left: 1px solid #cccccc; border-bottom: 1px solid #cccccc">Total:</td><td style="font-weight: bold; border-right: 1px solid #cccccc; border-bottom: 1px solid #cccccc" align="right">KN ' + formatNumber(totalPrice) + '</td></tr>';
	strOut = strOut + '<tr><td style="font-weight: bold; border-left: 1px solid #cccccc; border-bottom: 1px solid #cccccc">PDV:</td><td style="font-weight: bold; border-right: 1px solid #cccccc; border-bottom: 1px solid #cccccc" align="right">KN ' + formatNumber(moms) + '</td></tr>';
//	strOut = strOut + '<tr><td colspan="2" style="border-left: 1px solid #cccccc; border-right: 1px solid #cccccc; border-bottom: 1px solid #cccccc">' + strGebyrTxt + '</td></tr>';
	strOut = strOut + '</table>';

	strOut = strOut + '<table style="padding-top: 15px;" width="100%" border="0" cellpadding="0" cellspacing="0">';		
	strOut = strOut + '<tr><td align="right"><input class="WebshopButton" onMouseOver="this.style.cursor=\'pointer\';" onfocus="this.blur()" onclick="document.location=ShopReturnUrl" type="button" value="Natrag">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input class="WebshopButton" onfocus="this.blur()" onMouseOver="this.style.cursor=\'pointer\';" onclick="renderhiddenfields();fmOrderForm.submit()" type="button" value="Sljedeci"></td></tr>';
	strOut = strOut + '</table>';

	if (totalAmount==0) {

		strOut = '<table class="BreadMediumStdColor" width="100%" border="0" cellpadding="4" cellspacing="0">';		
		strOut = strOut + '<tr><td style="border: 1px solid #cccccc;">Isprazni koaricu!</td></tr>';
		strOut = strOut + '</table>';

		strOut = strOut + '<table style="padding-top: 15px;" width="100%" border="0" cellpadding="0" cellspacing="0">';		
		strOut = strOut + '<tr><td align="right"><input class="WebshopButton" onMouseOver="this.style.cursor=\'pointer\';" onfocus="this.blur()" onclick="history.back()" type="button" value="Natrag"></td></tr>';
		strOut = strOut + '</table>';

		document.write (strOut);
	}
	else {	
		document.write (strOut);
	}
	
}

function renderhiddenfields() {

	myString = getBasketCookie();

	myArray = myString.split(";");

	var totalAmount, totalPrice
	totalAmount = 0;
	totalPrice = 0.00;
	myPrice = 0;
	strOut = '';	

	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];
		myType = myItem[4].split("€")[1];
		
		strOut = strOut + '<input type="hidden" name="name_'+myId +'" value="'+myName+'">';
		strOut = strOut + '<input type="hidden" name="amount_'+myId +'" value="'+myAmount+'">';
		strOut = strOut + '<input type="hidden" name="price_'+myId +'" value="'+formatNumber(myPrice)+'">';
		strOut = strOut + '<input type="hidden" name="type_'+myId +'" value="'+myType+'">';
		strOut = strOut + '<input type="hidden" name="fDocuments" value="'+myId+'">';

	}

	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('2', false);
	num.setCurrencyValue('');
	num.setCurrency(true);
	num.setCurrencyPosition(num.LEFT_OUTSIDE);
	num.setNegativeFormat(num.LEFT_DASH);
	num.setNegativeRed(false);
	num.setSeparators(true, '.', ',');
	
	return num.toFormatted()
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

