﻿function getExpDate(days, hours, minutes) {
  var dt = new Date();
  dt.setDate(dt.getDate() + days);
  dt.setHours(dt.getHours() + hours);
  dt.setMinutes(dt.getMinutes() + minutes);
  return dt.toGMTString();
}


function getCookieData(offset) {
  var dataend = document.cookie.indexOf (";", offset);
  if (dataend == -1) {
    dataend = document.cookie.length;
  }
  return document.cookie.substring(offset, dataend); //TODO do i need decode here
}

//get cookie by name
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 unescape(getCookieData(j));
    }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}

//store cookie value
function setCookie(name, value, expires) {
  
    var path = '/'; 
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime( today.getTime() );

    expires = expires * 1000 * 60 * 60 * 24;
    var expires_date = new Date( today.getTime() + (expires) );
        
    document.cookie = name + "=" + escape(value)  +
        ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
        ("; domain=" + document.domain.replace(/www/, "") ) +        
        ( ( path ) ? ";path=" + path : "" ); 
}

//remove the cookie by setting old expiration date
function deleteCookie(name) {
    if (getCookie(name)) 
    {
        var expires="Thu, 01-Jan-70 00:00:01 GMT";
        setCookie(name,'',expires);
    }
}

function deleteCookieValue(name,value,expire) 
{
    actualValue = getCookie(name);

    if(actualValue.length == value.length)
    {
        deleteCookie(name);
        return;
    }
   
    if(actualValue.indexOf(value) == 0)
        newValue = actualValue.replace(value+',','');
    else
        newValue = actualValue.replace(','+value,'');

    setCookie(name,newValue,expire);
}


//recSep = '|'; 
//fieldSep = '*';

var baseCookie = Class.create();

//implements
baseCookie.prototype = {
        
       initialize: function(cookieName,cookieValue,expireDay,recSep,fieldSep) {
           if(cookieValue)
            {
                arrItems = new Array();
                arrItems = cookieValue.split(recSep); 
                for(i=0;i<arrItems.length;i++)
                {
                    this.buildItems(arrItems[i]);
                }
            }  
            this.cookieName = cookieName;
            this.expireDay = expireDay;
            this.recSep = recSep;
            this.fieldSep = fieldSep;
       },
       
       del:function(id){
           selectedIndex = -1;
           /*this.items.each(function(item,index){
                if(id==item.id)
                {
                    selectedIndex = index;
                    throw $break;
                }
            }); */
            if (this.items.length > 0)
            {
                for (i = 0; i < this.items.length; i++)
                {
                    if(id == this.items[i].id)
                    {
                        selectedIndex = i;
                        break;
                    }
                }
            }
            this.items.splice(selectedIndex,1);
            this.save();
        },
        
        delAll:function(){
           this.items.splice(0,this.items.length);
           //this.save();
           deleteCookie(this.cookieName);
        },
        
        getItemById:function(id) {
            obj = null;
            if (this.items.length > 0)
            {
                for (i = 0; i < this.items.length; i++)
                {
                    if(id == this.items[i].id)
                    {
                        obj = this.items[i];
                        break;
                    }
                }
            }            
            return obj;
        }
};

/*------JOB COOKIE --------*/

//declare a job class
var jobItem = Class.create();

//implements
jobItem.prototype = {
        
   initialize: function(id,note,rate,index) {
	    this.id= id;
	    this.note= note;
	    this.rate= rate;
	    this.index= index;
   }
};
            
//declare jobs collection class
var jobs = Class.create();
    
//implements
jobs.prototype = {

    items:[], 

    buildItems:function(item){
      this.items[this.items.length] =  (new jobItem((item.split(fieldSep))[0],(item.split(fieldSep))[1],(item.split(fieldSep))[2],(item.split(fieldSep))[3]));
    },
    
    add:function(id){
        var newIndex = 1;
        this.items.each(function(item){
            if(item.index >= newIndex)
                newIndex = eval(item.index) + 1;
        });
        newjobItem = new jobItem(id,'',0,newIndex);
        this.items[this.items.length] = newjobItem;
        this.save();
        
        return newjobItem;
    },
    //build cookie from collection and then save it in cookie
    save:function(){
        
        cookieValue = '';
        count = this.items.length;
        
        if (this.items.length > 0)
        {
            for (i = 0; i < this.items.length; i++)
            {
                cookieValue += this.items[i].id; 
                if(this.items[i].note)
                    cookieValue += fieldSep + this.items[i].note ;   
                else
                    cookieValue += fieldSep + '';
                
                if(this.items[i].rate)
                    cookieValue += fieldSep + this.items[i].rate;    
                else
                    cookieValue += fieldSep + '0'
                
                cookieValue += fieldSep + this.items[i].index;    
                
                //close
                if(i < count-1)
                    cookieValue += recSep;
            }
        }     
                   
        setCookie(this.cookieName,cookieValue,this.expireDay)
    },
    
    updateNote:function(id,note){
        (this.getItemById(id)).note = note;
        this.save();
    },
    
    updateRate:function(id,rate){
       (this.getItemById(id)).rate = rate;
       this.save();
    }
};
/*------/JOB COOKIE--------*/

/*------SEARCH COOKIE--------*/

//declare a search class
var searchItem = Class.create();

//implements
searchItem.prototype = {
        
   initialize: function(id,url) {
	    this.id= id;
	    this.url= url;
   }
};
            
//declare searches collection class
var searches = Class.create();
    
//implements
searches.prototype = {
 
    items:[],
    
    buildItems:function(item){
       this.items[this.items.length] = (new searchItem((item.split(fieldSep))[0],(item.split(fieldSep))[1]));
    },

    add:function(id,url){
        this.items[this.items.length] = new searchItem(id,url);
        this.save();
    },
    //build cookievalue from collection and then save it in cookie
    save:function(){
        
        cookieValue = '';
        count = this.items.length;
        
        this.items.each(function(item,index){
            cookieValue += item.id; 
            cookieValue += fieldSep + item.url ; 
            
            
            //close
            if(index<count-1)
                cookieValue += recSep;
        });
        setCookie(this.cookieName,cookieValue,this.expireDay)
    }
};
/*------/SEARCH COOKIE--------*/

Object.extend(jobs.prototype,baseCookie.prototype);
Object.extend(searches.prototype,baseCookie.prototype);
