﻿
var default_value;
/*Function to remove the spaces where added in the beginning or end  */
function trim(str)
	   {
		return str.replace(/^\s*|\s*$/g,"");
	   }
function assign()
{
	document.getElementById("favorite_text").value=window.opener.document.getElementById('favorite_value').value;

}
function createCookie(name,value,days) 
{
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) 
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function add_favorite()
 {
   var url=opener.location;
   var pagename=document.getElementById('favorite_text').value;
   var x=readCookie('url');
   if(!x)
   {
     window.opener.document.form.favourites.options[0].text='Mina favoritsidor';
     window.opener.document.form.favourites.options[0].value='Mina favoritsidor';
     var len=window.opener.document.form.favourites.options.length++;
     window.opener.document.form.favourites.options[len].value=url;
     window.opener.document.form.favourites.options[len].text=trim(pagename);
	 url=url+"$"+pagename+"||";
	 createCookie('url',url,365);      
    }  
    else
    {
	  var res=x.indexOf(url);
      if(res==-1)
      {
       var newurl=x+url+"$"+pagename+"||"; 
       createCookie('url',newurl,365); 
       var y=readCookie("url");
       var arr=y.split("||");   
       window.opener.document.form.favourites.options[0].value=new Option('Mina favoritsidor','Mina favoritsidor');
       var len=window.opener.document.form.favourites.options.length++;
       window.opener.document.form.favourites.options[len].value=pagename;
       window.opener.document.form.favourites.options[len].text=trim(pagename);
      }
      else
      {
       var x=readCookie("url");
       var arr=x.split("||");   
      }

    }  
	window.close();   
 } 
 function addUrl()
 {
	var a=display_text();
	document.getElementById('favorite_value').value=a;
	/*window.open('http://212.73.28.12/gleerups/se/kontrol/popup_favorites/','Favorites','menubar=0,resizable=0,location=0,status=0,scrollbars=0,left=350,top=200,width=250,height=150');*/
window.open('/gleerups/se/kontrol/popup_favorites/','Favorites','menubar=0,resizable=0,location=0,status=0,scrollbars=0,left=350,top=200,width=250,height=150');
	
 }
 function display_text()
	{
		var inputcode=document.getElementById('breadcrumbpage').innerHTML;
		var strInputCode=removescript(inputcode);	/***Remove script tag ***/
		var breadcrum=removetag(strInputCode);		/****Remove other html tags***/
		breadcrum=breadcrum.replace(/&gt;/g,'>');
		breadcrum=trim(breadcrum);
		var blength=breadcrum.length;
		if(blength!=0)						
			{
				var lastpos=breadcrum.lastIndexOf('>');
				var breadcrumname=breadcrum.substring(lastpos+1,breadcrum.length);
				var breadcrumname1=breadcrum.split('>');
				var breadlen=breadcrumname1.length;
				    																	
				if(breadlen>3)		/***Display favorites from breadcrum***/
					{
						if(document.getElementById('favorites_title'))
    					{    			 
    						if(navigator.userAgent.indexOf("Firefox") != -1)
								default_value=document.getElementById('favorites_title').textContent;		/***For firefox***/
							else
								default_value=document.getElementById('favorites_title').innerText;			/***For other browsers  ***/
	   					}
	   					else if(document.getElementById('customeracc_head'))
    					{    			 
    						if(navigator.userAgent.indexOf("Firefox") != -1)
								default_value=document.getElementById('customeracc_head').textContent;		/***For firefox***/
							else
								default_value=document.getElementById('customeracc_head').innerText;			/***For other browsers  ***/
	   					}
    					else
    					{
							var temp=breadcrumname1[0].toString();
							var temp1=breadcrumname1[1].toString();
							var temp2=temp+temp1;
							var len1=temp2.length+3;
							breadcrumname=breadcrum.substring(len1,breadcrum.length);
    						breadcrumname=breadcrumname.replace(/>/g,"/");
    						default_value=breadcrumname;
    					}
			    			
					}
				else
					{
						if(document.getElementById('favorites_title'))
						{
							if(navigator.userAgent.indexOf("Firefox") != -1)
								default_value=document.getElementById('favorites_title').textContent;
							else
								default_value=document.getElementById('favorites_title').innerText;
						}
						else if(document.getElementById('customeracc_head'))
						{
							if(navigator.userAgent.indexOf("Firefox") != -1)
								default_value=document.getElementById('customeracc_head').textContent;
							else
								default_value=document.getElementById('customeracc_head').innerText;
						}
						else			
						default_value=breadcrumname;								
					}
			}
		else			/***Display favorites from URL(home page)***/
		{
			var homeurl=document.URL;				
			var hlastpos=homeurl.lastIndexOf('/');         
			var hName=homeurl.substring(0,hlastpos);
			var hlastpos1=hName.lastIndexOf('/')+1;
			var homename=hName.substring(hlastpos1,hName.length);
			default_value=homename;
		}	
		return(default_value);
	}	
 function removescript(content_value)
	{
		var strscript=/SCRIPT/g;
		content_value=content_value.replace(strscript,'script');
		var startIndex=content_value.indexOf('<script');
			 
				if(startIndex==-1)
						{
							resultstr=content_value;
						}
				else
						{
							var endIndex=content_value.indexOf('<\/script>');
							var middleString=content_value.substring(startIndex,endIndex+9);
							var newString=content_value.replace(middleString,'');
							removescript(newString);
						}
								    
				return(resultstr);

	}
function removetag(HTMLWord)
	{
			a = HTMLWord.indexOf("<");
			b = HTMLWord.indexOf(">");
			HTMLlen = HTMLWord.length;
			c = HTMLWord.substring(0, a);
			if(b == -1)
				b = a;
			d = HTMLWord.substring((b + 1), HTMLlen);
			Word = c + d;
			tmp = Word.indexOf("<");
			if(tmp != -1)
				Word = removetag(Word);
			return Word;
	}	
function trim(str)
		{
		return str.replace(/^\s*|\s*$/g,"");
		}
function createOptions()
{
   var x=readCookie("url");
    if (!x)
    {
     document.form.favourites.options.length=0;
     document.form.favourites.options[0]=new Option('Mina favoritsidor','Mina favoritsidor');
    }
    else
    {
       var arr=x.split("||");
       var count=arr.length;  
       count--;
       document.form.favourites.options.length=count;
       document.form.favourites.options[0]=new Option('Mina favoritsidor','Mina favoritsidor');
       for(i=1;i<=count;i++)
       {
         var j=i-1; 
         var lastpos=arr[j].lastIndexOf('$');         
         var Name=arr[j];
         var pagename=Name.substring(lastpos+1,Name.length);
         document.form.favourites.options[i]=new Option(trim(pagename),arr[j]);
        }
    }
    /*Function to find whether content is present in left*/
}
function toUrl(input)
{
  var url=input.options[input.selectedIndex].value;
  var lastpos=url.lastIndexOf('$');
  var pageurl=url.substring(0,lastpos);
  document.location.href=pageurl;
}


