// INIT SETTINGS -----------------------------------------

var name = 'loss.huskeliste';
var myString = getHuskelisteCookie();

function addtohuskeliste(productid) {

	var myArray = myString.split("<split>");
	var ItemExist = false;

	for (i = 0; i <= myArray.length-1; i++) {

		myItem = myArray[i].split("|€|");
		myId = myItem[0].split("€")[1];

		if (productid == myId)
		{
			ItemExist = true;
			alert('Denne forestilling er alleredet tilføjet til din huskeliste.')
			break;
		}
	}

	if (!ItemExist) {
		myString += '<split>id€'+productid;
		setHuskelisteCookie(myString);
	}
	
	showhuskeliste();
}

function removefromhuskeliste(code) {

	var myArray = myString.split("<split>");

	for (i = 1; i < myArray.length; i++)
	{
		myItem = myArray[i].split("|€|");
		myId = myItem[0].split("€")[1];

		if (code == myId) {

			myString = myString.replace('<split>'+myArray[i],'');
			setHuskelisteCookie(myString);
			showedithuskeliste('edithuskelistecontainer');
			showhuskeliste();
			break;
		}
	}
	
}

function showhuskeliste()
{

	var productcodes = '';

	myArray = myString.split("<split>");
	
	for (i = 1; i < myArray.length; i++)
	{
		myItem = myArray[i].split("|€|");
		myId = myItem[0].split("€")[1];
		
		productcodes += myId + ',';
		
	}

	if (document.getElementById('huskelistecontainer')) {
		makeRequest('/ajax/huskeliste.asp?productcodes='+unescape(productcodes),'huskelistecontainer');
	}	
	
}

function showedithuskeliste(containerid)
{

	var productcodes = '';

	myArray = myString.split("<split>");
	
	for (i = 1; i < myArray.length; i++)
	{
		myItem = myArray[i].split("|€|");
		myId = myItem[0].split("€")[1];
		
		productcodes += myId + ',';
		
	}

	if (document.getElementById(containerid)) {
		makeRequest('/ajax/edit_huskeliste.asp?productcodes='+unescape(productcodes),containerid);
	}	
	
}

function setHuskelisteCookie(value)
{
    document.cookie = name + "=" + escape(value) +";EXPIRES="+getexpirydate(30); // Set # of days before expire
}

function getHuskelisteCookie()
{

    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf(prefix);
    var initstring = 'id€0';

    if (begin == -1)
    {
		setHuskelisteCookie(initstring);
    }

    var end = dc.indexOf(';', begin);

    if (end > 0) {
        initstring = unescape(dc.substring(begin + prefix.length, end));
    } else {
		initstring = unescape(dc.substring(begin + prefix.length, dc.length));
    }
    
    return initstring;
 
}

function deleteHuskelisteCookie(){
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);  // This cookie is history
	var cval = getHuskelisteCookie (name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

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 getexpirydate(nodays){
	var UTCstring;
	Today = new Date();
	nomilli=Date.parse(Today);
	Today.setTime(nomilli+nodays*24*60*60*1000);
	UTCstring = Today.toUTCString();
	return UTCstring;
}
