/**
 * CookieCollection.js
 *
 * Version 2.1 2002-08-04
 *
 * Author Dmitry Belakhov
 *
 * Defines a 'CookieCollection' object that represents a collection of 'Cookie' objects
 */


function CookieCollection()
{
    this.cookies = new Array();
    this.cookieCount = 0;

    this.getCookie = cookieGetCookie;
    this.getCookies = arrGetCookies;
    this.setCookie = voidSetCookie;

    this.getCookieNames = arrGetCookieNames;




    // Get all cookies
    var strAllCookies = document.cookie;

    // Reset cookie count to zero
    //this.cookieCount = 0;

    // Separate individual cookies
    var arrAllCookiesData = strAllCookies.split(";");

    // Create array holding individual cookie objects
    for (var i = 0; i < arrAllCookiesData.length; i++)
    {
	// Trim whitespace
	var strTrimmedCookieData = strTrimWhitespace(arrAllCookiesData[i]);

	var arrACookieData = strTrimmedCookieData.split("=");
	this.cookies[i] = new Cookie(arrACookieData[0], arrACookieData[1]);
	this.cookieCount++;
    }

}


/**
 * Method to return all cookies as objects of type 'Cookie'
 */

function arrGetCookies()
{
    return this.cookies;
}

/**
 * Method to return a single cookie with a specified name as an object of type 'Cookie'
 */

function cookieGetCookie(strCookieName)
{
    for (var i = 0; i < this.cookieCount; i++)
    {
	if (this.cookies[i].getName() == strCookieName)
	    return this.cookies[i];
    }
}


/**
 * Method to set a cookie as a specified object of type 'Cookie'
 */

function voidSetCookie(aCookie)
{
    strCookieData = "";

    if (aCookie.getValue() != undefined)
	strCookieData = aCookie.getName() + "=" + escape(aCookie.getValue());
    if (aCookie.getDomain() != undefined)
	strCookieData += ";domain=" + aCookie.getDomain();
    if (aCookie.getPath() != undefined)
	strCookieData += ";path=" + aCookie.getPath();
    if (aCookie.getMaxAge() != undefined)
	strCookieData += ";expires=" + aCookie.getMaxAge();
    if (aCookie.getSecure() != undefined)
	strCookieData += ";" + aCookie.getSecure();

    document.cookie = strCookieData;
}


/**
 * Method to set a cookie as a specified object of type 'Cookie'
 */

 function arrGetCookieNames()
 {
    var arrCookieNames = new Array();

    for (var i = 0; i < this.cookieCount; i++)
	arrCookieNames[i] = this.cookies[i].getName();

    return arrCookieNames;
 }



/**
 * Utility function to remove whitespace from ends of a string
 */

function strTrimWhitespace(strAString)
{
    var strATrimmedString = strAString;

    var intFirstCharacter = 0;
    var intLastCharacter = strAString.length;

    var blnFirstCharacterFound = false;
    var blnLastCharacterFound = false;

    while (!blnFirstCharacterFound)
    {
	if (strAString.charAt(intFirstCharacter) == " ")
	    intFirstCharacter++
	else
	    blnFirstCharacterFound = true;
    }

    while (!blnLastCharacterFound)
    {
	if (strAString.charAt(intLastCharacter - 1) == " ")
	    intLastCharacter--
	else
	    blnLastCharacterFound = true;
    }

    strATrimmedString = strAString.substring(intFirstCharacter, intLastCharacter);
    return strATrimmedString;
}