  /*
  * Pop-up/cookie scripts for fass.se polls 
  * Jonas Ladenfors 2005-04-12
  *
  * To set timeout date for when the pop-up and cookie should "be nomore" change the value in "expDate"
  * 
  * You can use two functions, each function has it´s own separate cookie.
  * 1. enkatPopUpBodyCreator - Should be placed in the onLoad statement of the index page and produces a popup window or a focus on the search form
  * 2. showPollLink - Shows a small img link for people with popup blockers.
  *
  */

  // Declaring valid date character, minimum year and maximum year
var dtCh= "-";

    
  function getCookieVal (offset) {  
      var endstr = document.cookie.indexOf (";", offset);  
      if (endstr == -1)    
        endstr = document.cookie.length;  
      return unescape(document.cookie.substring(offset, endstr));
  }
  
  function GetCookie (name) {  
    var arg = name + "=";  
    var alen = arg.length;  
    var clen = document.cookie.length;  
    var i = 0;  
    while (i < clen) {    
      var j = i + alen;    
      if (document.cookie.substring(i, j) == arg)      
        return getCookieVal (j);    
        i = document.cookie.indexOf(" ", i) + 1;    
        if (i == 0) break;   
      }  
    return null;
  }

  function SetCookie (name, value) {   
    
    var argv = SetCookie.arguments;  
    var argc = SetCookie.arguments.length;    
    
    var expires = (argc > 2) ? argv[2] : null;  
    var path = (argc > 3) ? argv[3] : null;  
    var domain = (argc > 4) ? argv[4] : null;  
    var secure = (argc > 5) ? argv[5] : false;  
      
    document.cookie = name + "=" + escape (value) + 
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
    ((path == null) ? "" : ("; path=" + path)) +  
    ((domain == null) ? "" : ("; domain=" + domain)) +    
    ((secure == true) ? "; secure" : "");
  }

 function popUp(URL) {
    day = new Date();
    id = day.getTime();
    if(eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=800,height=800,left = 50,top = 50');")){
	return "ok";
	}else{
	return null;
	}
 }
 
 function enkatPopUpBodyCreator(datum, urlen, name){
    
  var popUpUrl = urlen; 	
  var focusIt = null;
  var favorite = GetCookie(name);

	var yearArray =new Array();
	var temp = datum;
	var i;
	for (i=0;i<3;i++)
	{
	   yearArray[i] = temp.substring(0,temp.indexOf(dtCh));
	   temp = temp.substring(temp.indexOf(dtCh)+1);
	}
	
	var year = yearArray[0];
    var month = yearArray[1];
    var day = temp;

    var myDate=new Date()
   	myDate.setFullYear(year,month-1,day);
	
  if (favorite == null && (Date.parse(new Date()) < Date.parse(myDate)))
  {      
  	
    if(popUp(popUpUrl)!=null){
    	SetCookie(name, 'PollCookie[FASS.SE]', myDate);
    }
  }else{  
         //focusIt = document.getElementById(['search1']).focus();
  }
    return focusIt;
 }
 
 function showPollLink(){
 	var favorite = null;
 	favorite = GetCookie('PollCokie');
 	if (favorite == null && (Date.parse(new Date()) < Date.parse(expDate))) {      
 		document.write("<img class='poll_icon' alt='poll ikon' src='../images/poll.gif' border=0 alt='Utför en kortare enkät för att hjälp LIF förbättra Fass.se' onclick=\"SetCookie('PollCokie', 'PollCookie[FASS.SE]', new Date(expDate));\">");
 		//SetCookie('PollCokie', 'PollCookie[FASS.SE]', new Date(expDate));
 	}
 }
 
