// ShopReturnUrl bliver sat i Ajour hos elementet "Indkøbskurv"
var objCookieMdl = new CookieModule();
var ShopReturnUrl = objCookieMdl.getValueByCookieAndKey('User','ShopReturnUrl');
var cookieName = 'dyberglarsenshopbasket';
var myString = getBasketCookie();
var activeDivId = null;

if (myString != null) {
	myString = getBasketCookie();
}
else {
	myString = 'id€0|€|amount€0|€|price€0|€|name€0|€|type€0|€|itemno€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|€|amount€0|€|price€0|€|name€0|€|type€0|€|itemno€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|€|amount€0|€|price€0|€|name€0|€|type€0|€|itemno€0';
	setBasketCookie(myString);
}

function addbasket(id,amount,price,name,type,itemno) {

	myString = getBasketCookie();

	var myArray
	myArray = myString.split(";");

	var i, myItem, myId, myItemNo, myAmount, myPrice, myName, myType, ItemExist, newamount

	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("€")[1];
		myAmount = myItem[1].split("€")[1];
		myPrice = myItem[2].split("€")[1];
		myName = myItem[3].split("€")[1];
		myType = myItem[4].split("€")[1];
		myItemNo = myItem[5].split("€")[1];

		if (id == myId && myType==type) { //Hvis produktet findes i indkøbskurven læg antal til

			newamount = parseInt(amount) + parseInt(myAmount)

			myString = myString.replace('id€'+myId+'|€|amount€'+myAmount+'|€|price€'+myPrice+'|€|name€'+myName+'|€|type€'+myType+'|€|itemno€'+myItemNo, 'id€'+id+'|€|amount€'+newamount+'|€|price€'+price+'|€|name€'+name+'|€|type€'+type+'|€|itemno€'+itemno);
			
			setBasketCookie(myString);

			ItemExist = true;
			break;
		}
	}

	if (!ItemExist) {
		myString = myString +';id€'+id+'|€|amount€'+amount+'|€|price€'+price+'|€|name€'+name+'|€|type€'+type+'|€|itemno€'+itemno;
		setBasketCookie(myString);
	}

	showbasket();
	
//	alert(myType + ' er tilføjet til indkøbskurven.');

}

function updatebasket(id,amount) {

	myString = getBasketCookie();

	var myArray
		myArray = myString.split(";");

	var i, myItem, myId, myAmount, myName, ItemExist, myItemNo

	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];
		myType = myItem[4].split("€")[1];
		myItemNo = myItem[5].split("€")[1];

		if (id == myId && myType==type) {

			myNewAmount = amount;
			
				if (myNewAmount == 0) {
					myString = myString.replace(';id€'+myId+'|€|amount€'+myAmount+'|€|price€'+ myPrice +'|€|name€'+myName +'|€|type€'+myType+'|€|itemno€'+myItemNo, '');
					setBasketCookie(myString);
				}
				else {
					myString = myString.replace('id€'+myId+'|€|amount€'+myAmount, 'id€'+myId+'|€|amount€'+myNewAmount);
					setBasketCookie(myString);
				}

			break;
		}
	}
renderhiddenfields()
}

function removefrombasket(id) {

	myString = getBasketCookie();

	var myArray
		myArray = myString.split(";");

	var i, myItem, myId, myAmount, myName, ItemExist, myItemNo

	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];
		myType = myItem[4].split("€")[1];
		myItemNo = myItem[5].split("€")[1];

		if (id == myId) {
			myString = myString.replace(';id€'+myId+'|€|amount€'+myAmount+'|€|price€'+ myPrice +'|€|name€'+myName +'|€|type€'+myType +'|€|itemno€'+myItemNo, '');
			setBasketCookie(myString);
			break;
		}
	}
	showfullbasket(true);
}

function showbasket() {

	myString = getBasketCookie();

	myArray = myString.split(";");

	var totalAmount, totalPrice
	totalAmount = 0;
	totalPrice = 0;
	myPrice = 0;
	items = '';
	
	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];
		myItemNo = myItem[5].split("€")[1];
		
//		myPrice = javascriptNumber(myPrice);
		
		totalPrice = totalPrice + (parseInt(myAmount) * parseFloat(myPrice));
		
		items += '<table width="100%" cellpadding="0" cellspacing="0"><tr><td>' + myAmount + ' stk. ' + myName + '</td><td align="right"><a href="javascript: removefrombasket('+ myId +')">Fjern</a></td></tr></table>';
	}

	if (totalPrice==0){
		document.getElementById('Basket').className='Kurv';
	} else {
		document.getElementById('Basket').className='KurvOn';
		document.getElementById('Basket').innerHTML = '<div style="padding: 55px 0 0 66px"><div style="font-size: 13px; color: #669900">Beløb ' + formatNumber(totalPrice) + '<div><div style="margin-top: 5px"><a href="/kurv.asp"><img src="/files/grafik/system/til_kassen.gif" width="67" height="14" alt="Til kassen"></a></div></div>';
	}

}

function showfullbasket(edit) {

	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 + '<div class="clear">';
    strOut = strOut + '  <table width="555" border="0" cellspacing="0" cellpadding="0" class="basketBorder">';
    strOut = strOut + '    <tr class="basketGreen">';
    strOut = strOut + '        <td width="35">STK</td>';
    
    if (edit) { strOut = strOut + '        <td>&nbsp;</td>'; }
    
    strOut = strOut + '        <td width="70">Varenr.</td>';
    strOut = strOut + '        <td width="250">Varenavn</td>';
    strOut = strOut + '        <td width="110" align="right">Enhedspris</td>';
    strOut = strOut + '        <td width="80" align="right">Pris</td>';
    strOut = strOut + '    </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];
		myType = myItem[4].split("€")[1];
		myItemNo = myItem[5].split("€")[1];
		
		if (myType != ''){ size = ', ' + myType } else { size = '' }
		
		id = myId;
		name =  myName;
		pricetype = myType;
		
		itemprice = parseFloat(myPrice)
//		itemprice = parseFloat( javascriptNumber(myPrice) );
		
		itemamount = parseInt(myAmount);
		totalitemprice = itemamount * itemprice;
		totalAmount = totalAmount + itemamount

		totalPrice = totalPrice + totalitemprice
		
		moms = totalPrice * 0.2;

		strOut = strOut + '    <tr>';
		strOut = strOut + '        <td class="textCenter checkStk">' + itemamount + '</td>';
		
		if (edit) { strOut = strOut + '        <td><img style="cursor: pointer" onclick="removefrombasket(' + myId + ')" src="/AjrGfx/shop/bin.gif"/></td>'; }
		
		if (size == ', 0'){ size='' }
		
		strOut = strOut + '        <td>' + myItemNo + '&nbsp;</td>';
		strOut = strOut + '        <td>' + name + size + '&nbsp;</td>';
		strOut = strOut + '        <td class="textRight" nowrap>' + formatNumber(itemprice) + ' kr.</td>';
		strOut = strOut + '        <td class="textRight" nowrap>' + formatNumber(totalitemprice) + ' kr.</td>';
		strOut = strOut + '    </tr>';

	}

    strOut = strOut + '    <tr>';
    strOut = strOut + '       <td>&nbsp;</td>';
    
    if (edit) {	strOut = strOut + '       <td>&nbsp;</td>';	}
	
    strOut = strOut + '        <td>&nbsp;</td>';
    strOut = strOut + '        <td>&nbsp;</td>';
    strOut = strOut + '        <td>';
    strOut = strOut + '          <div style="margin-bottom: 3px">Total ex. moms</div>';
    strOut = strOut + '          <div style="margin-bottom: 3px">Moms</div>';
    strOut = strOut + '          <div style="margin-bottom: 3px"><strong>Total inkl. moms</strong></div>';
    strOut = strOut + '        </td>';
    strOut = strOut + '        <td class="textRight" nowrap>';
    strOut = strOut + '          <div style="margin-bottom: 3px">' + formatNumber(totalPrice-moms) + ' kr.</div>';
    strOut = strOut + '          <div style="margin-bottom: 3px">' + formatNumber(moms) + ' kr.</div>';
    strOut = strOut + '          <div style="margin-bottom: 3px"><strong>' + formatNumber(totalPrice) + ' kr.</strong></div>';
    strOut = strOut + '        </td>';
    strOut = strOut + '    </tr>';
    strOut = strOut + '  </table>';
	strOut = strOut + '</div>';
	
	if (edit) {	
		strOut = strOut + '<table style="padding-top: 15px;" width="100%" border="0" cellpadding="0" cellspacing="0">';		
		strOut = strOut + '<tr><td><input onMouseOver="this.style.cursor=\'pointer\';" onfocus="this.blur()" onclick="document.location=ShopReturnUrl; return false;" type="image" src="/files/grafik/system/btn_tilbage.gif"></td><td align="right"><input onfocus="this.blur()" onMouseOver="this.style.cursor=\'pointer\';" onclick="renderhiddenfields();this.form.submit()" type="image" src="/files/grafik/system/btn_til_betaling.gif"></td></tr>';
		strOut = strOut + '</table>';
	}

	if (totalAmount==0) {
		
		document.location = ShopReturnUrl;
		
	} else {	

		document.getElementById('FullBasket').innerHTML = 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];
		myItemNo = myItem[5].split("€")[1];
		
		if(myAmount>0){
			strOut = strOut + '<input type="text" name="products" value="'+myId+'€'+myAmount+'€'+myPrice.toString()+'€'+myType+'€'+myItemNo+'"/>';
		}

	}

	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('DKK ');
	num.setCurrency(true);
	num.setCurrencyPosition(num.LEFT_OUTSIDE);
	num.setNegativeFormat(num.LEFT_DASH);
	num.setNegativeRed(false);
	num.setSeparators(true, '.', ',');
	
	return num.toFormatted()
}

function javascriptNumber(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);
}

function flipPriceGroup(form, me, showid, id){

	for ( var i=0;i<me.length;i++ )
	{

		if(me[i].value > 0){
			
			var str = 'PriceGrp_' + showid + '_' + me[i].value;

			document.getElementById('select'+showid).style.display = 'none';
			document.getElementById(str).style.display = 'none';
			resetAmount(form, me[i].value); //hvis man skifter til en anden prisgruppe, så nulstilles alle valgt antal for den forestilling
		
		}
			
	}
	
	if(id==0){
		document.getElementById('select'+showid).style.display = 'block';
		
	} else {
		document.getElementById(id).style.display = 'block';
	}
	
}

function resetAmount(me, id){

	for ( var i=0;i<me.elements.length;i++ )
	{
		if( me.elements[i].type=='text' )
		{
			
			if (me.elements[i].name.indexOf(id) >= 0) {
				document.getElementById(me.elements[i].name.replace(/PriceGrp/,'subprice')).innerHTML = formatNumber(0);
				me.elements[i].value = 0;
			}
			
		}
	}

}

function resetAllFields(me){

	for ( var i=0;i<me.elements.length;i++ )
	{
		if( me.elements[i].type=='text' )
		{
			
			me.elements[i].value = 0;
			
		}
	}

}

function isNumber(form, me, number) {

//	var isNumber = !isNaN(number);
	
	if(isNaN(number)){
	
		alert('Angiv venligst et tal'); me.focus(); return false;
		
	}
	
	checkMaxAmount(form, me);
	
	calculateTotalPrice(form);

}

function checkMaxAmount(form, me){

	var id = me.name.split('_')[2]
	
	var totalseats = 0

	for ( var i=0;i<form.elements.length;i++ )
	{
		if( form.elements[i].type=='text' )
		{
			
			if (form.elements[i].name.indexOf(id) >= 0) {

				totalseats += parseInt(form.elements[i].value);
			}
			
		}
	}
	
	if ( totalseats > 16 ) {
	
		alert('Du kan bestille op til 16 billetter på én gang til samme forestilling. \nØnsker du at bestille til en større gruppe, kontakt venligst Billetservice på tlf. 70213021.');
	
		me.value = 0;

		var subprice = parseInt( document.getElementById( me.name.replace(/PriceGrp/,'subprice')).innerHTML );

		document.getElementById('totalprice').innerHTML = formatNumber(totalprice-subprice);

		return false;
	}

}

function calculateTotalPrice(me){

	var totalprice;
	totalprice = 0;
	
	for ( var i=0;i<me.elements.length;i++ )
	{
		if( me.elements[i].type=='text' )
		{
			
			priceid = me.elements[i].name.replace(/PriceGrp/,'Price');
			subpriceid = me.elements[i].name.replace(/PriceGrp/,'subprice');

			document.getElementById(subpriceid).innerHTML = formatNumber( parseInt(document.getElementById(priceid).innerHTML) * parseInt(me.elements[i].value) );
			totalprice += parseInt(document.getElementById(priceid).innerHTML) * parseInt(me.elements[i].value);
			
		}
	}
 
	
	var currentprice = 0;
	var reservedticketprices = me.ticketprice;
	
	if (reservedticketprices) {
	//alert ('reservedticketprices: ' + reservedticketprices)

		for ( var i=0;i<reservedticketprices.length;i++ )
		{
		  currentprice += parseInt(reservedticketprices[i].value);
		  //alert('price: '+parseInt(reservedticketprices[i].value))
		}
 	}
 
	document.getElementById('totalprice').innerHTML = formatNumber(totalprice+currentprice);

}

