//domain name
globaldomain = ".drikkeviser.dk";

// how many favorits to save
var maxFavorits = 50;

function Set_Cookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}

function Get_Cookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
	    if ((!start) && (name != document.cookie.substring(0,name.length))){
			 return null;
		}
    	if (start == -1){
			 return null;
    	}
	var end = document.cookie.indexOf(";",len);
    	if (end == -1){
			end = document.cookie.length;
		}
   return unescape(document.cookie.substring(len,end));
}

function Delete_Cookie(name,path,domain) {
    if (Get_Cookie(name)){
	 	document.cookie = name + "=" +
        ( (path) ? ";path=" + path : "") +
        ( (domain) ? ";domain=" + domain : "") +
        ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
}

var expdate = new Date ();
expdate.setTime (expdate.getTime() + (365 * 24 * 60 * 60 * 1000)); // a year from now
var sFavoritString;
var sOut;
var aFavoritArray = new Array();
var sNewName ='';
var sAddThisPage ='';
var pageLocation = window.location.href


	
function buildFavoritTable(){
	deleteCount = 0;
	sOut = "<table width='100%' border='0' cellspacing='0' cellpadding='0'>\n";
	sArray = "<b>Sangbog</b>";
	if(Get_Cookie('difFavorits') != null && Get_Cookie('difFavorits')!= ''){
		sFavoritString = Get_Cookie('difFavorits');
		//####
		var reg = /"|\[|\]/g;
		sFavoritString = sFavoritString.replace(reg,'')
		//####
		aFavoritArray = sFavoritString.split('$');
		for(i=0;i<aFavoritArray.length;i++){
				aFavoritArray[i] = aFavoritArray[i].split(',');
			}
		if(sFavoritString != null && sFavoritString != ''){
				sArray = "<a href='show_favs.asp?favID=";
				for(i=0; i < aFavoritArray.length; i++){
					sOut +=	'<tr>\n';
					sOut +=	"<td width='10' height='14'>&nbsp;</td>\n";
					sOut +=	"<td width='8'>::</td>\n";
					sOut +=	"<td width='111'><a href='";
					sOut +=	 aFavoritArray[i][1] + "'title='Klik her for at se din sangbog " + aFavoritArray[i][0] + "'>" + aFavoritArray[i][0] + "</a></td>\n";
					sOut += "<td width='30' align='right'><a href='javascript:difDeleteFavorit(" + deleteCount + ")' title='Klik her for at slette denne drikkevise'>Slet</a></td>\n";
					sOut +=	"<td width='10'>&nbsp;</td>\n";
					sOut +=	"</tr>\n";
					deleteCount++;
					startPos = aFavoritArray[i][1].indexOf('=');
					sArray += '$' + aFavoritArray[i][1].substring(startPos+1,startPos+5) + '$'
					}					
					sArray += "' title='Klik her for at se din sangbog klar til udskrift.'>Se din sangbog</a>"
		}
	}
	else{
			sOut +=	'<tr>\n';
			sOut +=	"<td width='10' height='14'>&nbsp;</td>\n";
			sOut +=	'<td  colspan="3">Saml alle dine yndlingssange i en sangbog!<br>Hvis du vil tilføje denne side til din sangbog, så klik på linket herunder</td>';
			sOut += '<td width="10" >&nbsp;</td>\n';
			sOut += '</tr>\n';
	}	
	if(Get_Cookie('difFavorits')!= null  && Get_Cookie('difFavorits')!= ''){ 
			if( aFavoritArray.length >= maxFavorits){
				sAddThisPage = '';
				}
			else{
			sAddThisPage += '<tr>\n';
			sAddThisPage += '<td width="10" height="20">\&nbsp;</td>\n';
			sAddThisPage += '<td colspan="3" align="left" valign="bottom"><br><a href="javascript:getName()">Tilføj denne side til din sangbog...</a></td>\n';
			sAddThisPage += '<td width="10" >&nbsp;</td>\n';
			sAddThisPage += '</tr>\n';
			}
			}
		else{
			sAddThisPage += '<tr>\n';
			sAddThisPage += '<td width="10" height="20">\&nbsp;</td>\n';
			sAddThisPage += '<td colspan="3" align="left" valign="bottom"><a href="javascript:getName()">Tilføj denne side...</a></td>\n';
			sAddThisPage += '<td width="10" >&nbsp;</td>\n';
			sAddThisPage += '</tr>\n';
		}
	sOut2 = sOut + '</table>';
	sOut += sAddThisPage;
	sOut +='</table>'
}

function difDeleteFavorit(index){
	var tempArray = new Array();		
	var tempCount = 0;
	//########
	if(aFavoritArray.length == 1){
		Delete_Cookie('difFavorits','/',globaldomain)
	}
	//########
	for(i=0;i<aFavoritArray.length;i++){
		if(i == index){
			continue;
		}
		else{
			tempArray[tempCount] = aFavoritArray[i];
			tempCount++;
		}
	}
		aFavoritArray = tempArray;
		Set_Cookie('difFavorits',aFavoritArray.join('$'),expdate,'/',globaldomain)
		window.history.go(0)
	}
	
function difAddFavorit(){
	tempName = sNewName;
		if(aFavoritArray != null  && Get_Cookie('difFavorits')!= null){
			tempStr = aFavoritArray.join('$')
			if(tempStr.length != 0 ){
				tempStr += '$' +  tempName + ',' + pageLocation;
			}else{
				tempStr +=  tempName + ',' + pageLocation;
			}
		}
		else{
			tempStr = tempName + ',' + pageLocation;
		}
	Set_Cookie('difFavorits',tempStr,expdate,'/',globaldomain)
	buildFavoritTable()
	window.history.go(0);
}
function getName(){
		x = window.innerWidth/2;
		y = window.innerHeight/2;
		if(document.all){
			xIE = document.body.offsetWidth/2;
			yIE = document.body.offsetHeight/2;
		}
		if(!document.all && document.getElementById){
			xIE = window.innerWidth/2 - 100;
			yIE = window.innerHeight/2 + 60;
		}
		if(document.layers){
			xIE = 0;
			yIE = 0;
		}
		eval("thePrompt = window.open('favorit/namePrompt.htm','namePrompt','height=125,width=200,screenX=" + x + ",screenY=" + y + ",left=" + xIE + ",top=" + yIE + ",dependent=yes')");
		
}

buildFavoritTable();
