//
// cookies.js
//
// Tools to set and get cookies.
//

    // determines the domain root of the current user
    var tmp, tmp2, tmp3, currentDomain;
    var currentLocation = '';
    currentLocation += window.location.host;
    //currentLocation = '192.168.1.23:3714'; // useful for debugging
    // localhost? Netscape < 6.2 do not support sharing cookies across domains
    if ( currentLocation.indexOf( 'localhost' ) != -1 || document.layers )
        currentDomain = '';
    else
    {

    	domain = '';

	tmpColon = currentLocation.split( ':' );

	if(tmpColon.length > 1){
		domain = tmpColon[0];
	}
	tmpDomain = domain.split('.');

		// ip address in url?
        if (tmpDomain.length == 4 && tmpDomain[ 3] == parseInt( tmpDomain[ 3]) )
        {
            currentDomain = ';Domain=' + domain;
	}else {


	    start = domain.indexOf('.');
	    if(start>-1){
	    	currentDomain = ';Domain=' ;
	    	start = start+1;
	    	currentDomain += domain.substring(start,domain.length);
	    } else {
	    	currentDomain = '';
	    }


        }

    }

    // returns the value of a session or permanent cookie
    function getCookie( name )
        {

	  var nvl = document.cookie.split( ';' );

          for (var n=0; n < nvl.length; n++)
          {

            var nv = nvl[ n ];
            if (nv.charAt( 0 ) == ' ')
              nv = nv.substring( 1 );
            var nvp = nv.split( '=' );
            if (nvp[0] == name)
            {
              if ( typeof( nvp[1] ) == 'undefined' )
                return '';
              else
                return nvp[1];
            }
          }
          return '';
        }

    // sets a session cookie
    function setCookie( name, value, durationSeconds )
        {
          value += '; Path=/' + currentDomain + ';';
          if (typeof(durationSeconds) != 'undefined')
            value += '; Max-age=' + durationSeconds;
            document.cookie = name + '=' + value;
        }

    // sets a permanent cookie which lasts 1 month
    function setOneMonthCookie( name, value )
        {
          var monthArray = new Array ( 'Jan' , 'Feb' , 'Mar' , 'Apr' , 'May' , 'Jun' , 'Jul' , 'Aug' , 'Sep' , 'Oct' , 'Nov' , 'Dec' );
          var today = new Date().toGMTString();
          //var today="Sat, 18 Dec 2003 01:35:26 UTC"; // useful for debugging
          var oneMonth, nextMonth, tmp;

          for ( var thisMonth = 0; thisMonth < monthArray.length; thisMonth++ )
          {
              if ( today.indexOf( monthArray[ thisMonth ] ) != -1 )
              {
                // are we in Dec?
                if ( thisMonth == monthArray.length-1)
                {
                    // wrap month back to Jan
                    nextMonth = 0;
                    // increment year
                    tmp = today.slice( 0 , 15 ) + Number( Number( today.charAt(15) ) +1 ) + today.slice( 16 , today.length );
                    today = tmp;
                }
                else
                {
                    // increment month
                    nextMonth = thisMonth + 1;
                }
                oneMonth = today.replace( monthArray[ thisMonth ] , monthArray[ nextMonth ] );
                // workaround for number of days in month
                var day = Number( oneMonth.charAt(5) + oneMonth.charAt(6)  );
                if ( day > 28 )
                    day = 28;
                tmp = oneMonth.slice( 0 , 5 ) + day + oneMonth.slice( 7 , today.length );
                oneMonth = tmp;
              }
          }
          value += '; Path=/' + currentDomain + '; expires=' + oneMonth;
          document.cookie = name + '=' + value;
        }

        // deletes a permanent cookie
        function deleteCookie( name )
            {
                if ( getCookie( name ) != '' )
                    document.cookie = name + "=; Path=/" + currentDomain + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
            }

	function openCookieWindow(loc,title,properties)
	{
		var sessionID = getCookie("JSESSIONID");
		// split the loc and insert the jsessionid cookie value
		var splitLoc = loc.split("?");
		loc = splitLoc[0] + ";jsessionid=" + sessionID;
		if (typeof(splitLoc[1]) != 'undefined')
			loc = loc + "?" + splitLoc[1];
		return window.open( loc ,title,properties);
	}
