﻿//<script id="jsCookie" language="javascript">
/*Requirements:
    1. uses String functions in Utility.js
*/
var PCookie = new constructorCookie()

function constructorCookie() {
    if (document.getElementById('PCookie') == undefined) { alert('Dear Programmer please set the "id" attribute of Utility.js to "PCookie".'
                + '\n\nExample: <script id="PCookie" language="javascript" src="../uiCommon/Cookie.js" debug="true"></script>') }
    
    //Properties:
    this.Cookie = document.cookie
    this.Debug = ( P.Get(document.getElementById('PCookie').debug, 'false') == 'false' ) ? false : true
    this.Path = null     //defaults to page specific for domain specific path = '\'.
    
    var dtExpiration = new Date()
    dtExpiration.setTime(dtExpiration.getTime() + 10*365*24*60*60*1000)
    this.ExpirationDate = dtExpiration.toGMTString()
    
    //Methods:
    this.Get = fn_Get_PCookie
    this.Set = fn_Set_PCookie
    this.IsKey = fn_IsKey_PCookie
}

function fn_Get_PCookie(Key) {  //Arguments: 1 = Cookie.
    var s = P.RemoveBlanks( P.Get(arguments[1], document.cookie) )
    var pairs = s.split(';')
 	
    for(var i=0; i < pairs.length; ++i) {
	    var pairSplit = pairs[i].split('=')
	    
	    if(pairSplit[0] == Key) {
	        if (PCookie.Debug) { alert('fn_Get_PCookie(' + Key + ') = ' + pairSplit[1]) }
	        return pairSplit[1]
	    }
    }
    
    return null
}

function fn_Set_PCookie(Key, Value) {       //Arguments: 2 = Path, 3 = ExpirationDate.
    var jsPath = '; path=' + P.Get(arguments[2], PCookie.Path)
    var jsExpiration = P.Get(arguments[3], PCookie.ExpirationDate)
    //var jsNamespace = P.Get(arguments[4])
    if (PCookie.Debug) { alert('fn_Set_PCookie(): ' + Key + '=' + Value + '; expires=' + jsExpiration + jsPath) }
    document.cookie = Key + '=' + Value + '; expires=' + jsExpiration + jsPath
}

function fn_IsKey_PCookie(Key) {
    var s = P.RemoveBlanks(PCookie.Cookie)
    var pairs = s.split(';')
    
    for(var i=0; i < pairs.length; ++i) {
	    var pairSplit = pairs[i].split('=')
	    if(pairSplit[0] == Key) return true
    }
    
    return false
}
