﻿var isCookie;
checkForCookie();

function MyLinks(linkCount, labelName){
	this.linkCount=linkCount;
	this.myLinksArr=new Array();
	this.pos=0
	this.labelName=labelName;
	this.link=new Array();

	for(var x=0;x<this.linkCount;x++){
		this.myLinksArr[x]=new Array();
		this.myLinksArr[x][0]='empty';
		this.myLinksArr[x][1]='empty';
	}
	if(isCookie){
		this.get();
	}
}


MyLinks.prototype.get = function(){
	var cookie
	var x;
	cookie=getCookieData(this.labelName);
	if(cookie){
		this.myLinksArr=cookie.split(',');
		for(x=0;x<this.myLinksArr.length;x++){
			this.myLinksArr[x]=this.myLinksArr[x].split(';');		
		}
		x=0;
		while( x<this.linkCount && this.myLinksArr[x][0]!='empty'){
			x++;
		}
		this.pos=x;
	}else{
		this.pos=0;
		this.save()
	}
}


MyLinks.prototype.save=function(){
	var data=this.myLinksArr;
	for(var x=0;x<data.length;x++){
		data[x]=data[x].join(';');		
	}			
	data=data.join(',');
	saveCookieData(this.labelName, data);	
}

MyLinks.prototype.put=function(){
	if(isCookie){
		if(this.pos<this.linkCount){
			this.putPos(this.pos);
			this.pos++;
		}else{
			window.open('/Templates/MyLinks/MyLinksReplace.htm','msgBox','width=300,height=150');
		}
	}else{
			window.open('/Templates/MyLinks/MyLinksNoCookie.htm','msgBox','width=300,height=150');
	}
}

MyLinks.prototype.putPos=function(pos){
	var data;
	this.myLinksArr[pos]=new Array();
	this.myLinksArr[pos][0]=this.link[0];
	this.myLinksArr[pos][1]=this.link[1];
	this.save();
	setTimeout('document.location.reload(true)',200);		
}

MyLinks.prototype.write=function(){
	var emptyFlag=true;
	for(var x=0;x<this.myLinksArr.length;x++) {
	if( this.myLinksArr[x][0] != 'empty' && this.myLinksArr[x][0] != 'e' && typeof(this.myLinksArr[x][0])!='undefined'){
			document.write(' - <a href="'+ this.myLinksArr[x][1] + '" target="_top">' + this.myLinksArr[x][0] + '</a> - <br />');
			emptyFlag=false;
	}	    
	}
	if(emptyFlag){
		document.write('<div class="Normal">Spara dina tre favoritsidor som snabblänkar</div>');	
	}	  
}

function MyLinksHelp(){
	window.open('/Templates/MyLinks/MyLinksHelp.htm','msgBox','width=430,height=290');
}


function checkForCookie(){
	document.cookie = "cookies=true";
	isCookie = (document.cookie!='');
}

function saveCookieData(labelName, data){
	var exp = new Date();
	exp.setTime(exp.getTime()+31536000000) //One year
	document.cookie=labelName + '=' + escape(data) + "; expires=" + exp.toGMTString();
}


function getCookieData(labelName){
	var labelLen = labelName.length;
	var cookieData = document.cookie;
	var cLen = cookieData.length;
	var x=0, cEnd;
	while (x < cLen) {
		var y = x + labelLen;
		if(cookieData.substring(x,y) == labelName){
			cEnd = cookieData.indexOf(";", y);
			if(cEnd == -1){
				cEnd = cookieData.length;
			}
			return unescape(cookieData.substring(y+1, cEnd))
		}
		x++;
	}
	return false
}

function delCookieData(labelName){
	var exp = new Date();
	exp.setTime(exp.getTime()-10)
	document.cookie=labelName + '=' + '' + "; expires=" + exp.toGMTString();
	setTimeout('document.location.reload(true)',1);
}

function printPage(){
if(window.print) window.print();
	else{
		if(agt.indexOf("mac") != -1)alert("To print this page press Command-P.")
		else alert("To print this page press Control-P.")
	}
}

var ml = new MyLinks( 3, "geMyLinks" );

	
function doSearch( url, searchTextId, searchRootId )
{
	dxDoSearch( url + "&DXSearchRoot=" + document.getElementById( searchRootId ).value, searchTextId );
}

function doSearchKeyDown( url, searchTextId, searchRootId )
{
	return dxDoSearchKeyDown( url + "&DXSearchRoot=" + document.getElementById( searchRootId ).value, searchTextId );
}