/*                                                                  
* Copyright © 1999-2008 TeaLeaf Technology, Inc.  
* All rights reserved.
*
* THIS SOFTWARE IS PROVIDED BY TEALEAF ``AS IS'' 
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, 
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE DISCLAIMED.  
* IN NO EVENT SHALL TEALEAF BE LIABLE FOR ANY DIRECT, INDIRECT, 
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
* THE POSSIBILITY OF SUCH DAMAGE.
*
* @fileoverview 
* Cookie injector.   
*
* @requires 
* TeaLeafCookieCfg.js
*
* @version 2008.09.29.1
*                                                                   
*/

if(TeaLeaf.Cookie && TeaLeaf.Cookie.Configuration){

    /**
    * Set cookie value. 
    * @param tlname cookie name. 
    * @param tlvalue cookie value. 
    * @param tlexpires cookie expiration date. 
    * @param tlpath cookie path. 
    * @param tldomain cookie domain, pages on a domain made up of more than one server can share cookie info. 
    * @param tlsecure cookie secure, the stored info can only be accessed from a secure env. 
    * @addon
    */
    TeaLeaf.Cookie.tlSetCookieValue = function(tlname, tlvalue, tlexpires, tlpath, tldomain, tlsecure){
        document.cookie= tlname + "=" + tlvalue +
        ((tlexpires) ? "; expires=" + tlexpires.toGMTString() : "") +
        ((tlpath) ? "; path=" + tlpath : "") +
        ((tldomain) ? "; domain=" + tldomain : "") +
        ((tlsecure) ? "; secure" : "");        
    }

    /**
    * Get cookie value if it exists.
    * @param tlname cookie name. 
    * @addon
    */
    TeaLeaf.Cookie.tlGetCookieValue = function(tlname) {
	var tldocCookie = document.cookie;
        var tlindexOfBegin= tldocCookie.indexOf(tlname+"=");
   
        if(tlindexOfBegin >= 0){
		tlindexOfBegin += tlname.length + 1;
                var tlindexOfEnd = tldocCookie.indexOf(";", tlindexOfBegin);
                if(tlindexOfBegin <= tlindexOfEnd)
                    return tldocCookie.substring(tlindexOfBegin, tlindexOfEnd);
		else if(tlindexOfEnd == -1)
			return tldocCookie.substring(tlindexOfBegin);
        }   

        return null;
    }
    
    /**
    * Build a GUID. 
    * @addon
    */
    TeaLeaf.Cookie.tlBuildGuid = function(){
        var tlguid="";
        var i;
    
        for(var j=0; j<32; j++){
            i = Math.floor(Math.random()*16).toString(16).toUpperCase();
            tlguid = tlguid + i;
        }
        return tlguid;
    }

    /**
    * Check if a cookie value if not there. 
    * @addon
    */
    TeaLeaf.Cookie.tlCheckCookieValue = function(){
        var tlCookie = TeaLeaf.Cookie.Configuration.tlCookie;        
        for(var i=0; i<tlCookie.length; i++){
            if(TeaLeaf.Cookie.tlGetCookieValue(tlCookie[i].tlcookiename)==false){            
                if(tlCookie[i].tlset == true){            
                    var tlCookieValue = "";
                    if(tlCookie[i].tlvalue == ""){
                        tlCookieValue = TeaLeaf.Cookie.tlBuildGuid();
                    }                                
                    TeaLeaf.Cookie.tlSetCookieValue(tlCookie[i].tlcookiename, tlCookieValue, tlCookie[i].tlexpires, tlCookie[i].tlpath, tlCookie[i].tldomain, tlCookie[i].tlsecure);
                }       
            }
        }
    }
    if(TeaLeaf.Cookie.Configuration.tlinit == false){
        TeaLeaf.Cookie.Configuration.tlinit = true;
        TeaLeaf.addOnLoad(TeaLeaf.Cookie.tlCheckCookieValue);
    }
}
    
