
bbcjs.lib.string = {};
bbcjs.lib.string.obj2string = function (o, pd, vd)
{
//Have name1=val1&name2=val2 as defaults
if (typeof(pd)=="undefined") pd = "&";
if (typeof(vd)=="undefined") vd = "=";
var str = "", i, t="", cnt = 0;
if (typeof(o)=="object")
{
for (i in o)
{
cnt++;
if (typeof(o[i])=="object" && ((o[i].constructor==Object) || (o[i].constructor==Array) ))
{
t += i + vd + escape(bbcjs.lib.string.obj2string(o[i], pd, vd)) + pd;
}
else if (typeof(o[i])!="object")
{
t += i + vd + o[i] + pd;
}
}
str = t.substring(0, t.length-1);
}
else bbcjs.trace("obj2string expected typeof object, not "+typeof(o),1);
if (cnt==0) str = "_{}";
return str;
}
bbcjs.lib.string.string2obj = function (str, pd, vd)
{
//Have name1=val1&name2=val2 as defaults
if (typeof(pd)=="undefined") pd = "&";
if (typeof(vd)=="undefined") vd = "=";
var pairs = str.split(pd);
var i, j, o={}, pair=[], t;
for (i=0; i<pairs.length; i++)
{
pair = pairs[i].split(vd);
if (i==0 && (parseInt(pair[0])==0)) o = [];
if (pair.length==2)
{
t = unescape(pair[1]);
if (t.indexOf(vd)!=-1)
{
o[pair[0]] = bbcjs.lib.string.string2obj(t, pd, vd);
}
else
{
if (!isNaN(parseInt(pair[0]))) pair[0] = parseInt(pair[0]);
if (unescape(pair[1])!="_{}") o[pair[0]] = pair[1];
else o[pair[0]] = {};
}
}
else bbcjs.trace("WARNING: string2obj:: '"+pairs[i]+"' is not a proper name-val pair (using '"+pd+"' and '"+vd+"')",5);
}
return o;
}
bbcjs.lib.string.WordLimit = function (limit)
{
this.limit = limit;
this.type = 'WordLimit';
}
bbcjs.lib.string.countWords = function (str)
{
var matches = new Array();
matches = str.match(/\w+/g);
if (matches == null) return 0;
else return matches.length;
}
bbcjs.lib.string.WordLimit.prototype.validate = function (str)
{
return (bbcjs.lib.string.countWords(str) <= this.limit);
}
function sprintf()
{
if (!arguments || arguments.length < 1 || !RegExp)
{
return;
}
var str = arguments[0];
var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
var a = b = [], numSubstitutions = 0, numMatches = 0;
while (a = re.exec(str))
{
var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
var pPrecision = a[5], pType = a[6], rightPart = a[7];
numMatches++;
if (pType == '%')
{
subst = '%';
}
else
{
numSubstitutions++;
if (numSubstitutions >= arguments.length)
{
bbcjs.trace('sprintf : Not enough function arguments (' + (arguments.length - 1)
+ ', excluding the string)\n'
+ 'for the number of substitution parameters in string ('
+ numSubstitutions + ' so far).',1);
}
var param = arguments[numSubstitutions];
var pad = '';
if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
else if (pPad) pad = pPad;
var justifyRight = true;
if (pJustify && pJustify === "-") justifyRight = false;
var minLength = -1;
if (pMinLength) minLength = parseInt(pMinLength);
var precision = -1;
if (pPrecision && pType == 'f')
precision = parseInt(pPrecision.substring(1));
var subst = param;
switch (pType)
{
case 'b':
subst = parseInt(param).toString(2);
break;
case 'c':
subst = String.fromCharCode(parseInt(param));
break;
case 'd':
subst = parseInt(param) ? parseInt(param) : 0;
break;
case 'u':
subst = Math.abs(param);
break;
case 'f':
subst = (precision > -1)
? Math.round(parseFloat(param) * Math.pow(10, precision))
/ Math.pow(10, precision)
: parseFloat(param);
break;
case 'o':
subst = parseInt(param).toString(8);
break;
case 's':
subst = param;
break;
case 'x':
subst = ('' + parseInt(param).toString(16)).toLowerCase();
break;
case 'X':
subst = ('' + parseInt(param).toString(16)).toUpperCase();
break;
}
var padLeft = minLength - subst.toString().length;
if (padLeft > 0)
{
var arrTmp = new Array(padLeft+1);
var padding = arrTmp.join(pad?pad:" ");
}
else
{
var padding = "";
}
}
str = leftpart + padding + subst + rightPart;
}
return str;
}
var printf = sprintf;
bbcjs.lib.date = new Object();
bbcjs.lib.date.prototype = new bbcjs.Module("date", 0.1, "$Revision: 1.4 $", "$Date: 2005/05/19 10:37:56 $");
bbcjs.lib.date.dateFromNow = function (s)
{
bbcjs.trace("dateFromNow: pattern: '"+s+"'", 5);
var today = new Date();
var expdate = new Date();
var multi = 1;
var md = s.match(/([\+\-]{1})(\d+)([smhdwMy]{1})/i);
if (md[1] == "-") multi = -1;
if ( (md.length) && (md.length==4))
{
diff = multi*(parseInt(md[2]));
//Ammend the expdate to reflect the information given...
if (md[3]!="M") md[3] = md[3].toLowerCase();
switch (md[3])
{
case "s" : expdate.setSeconds(today.getSeconds() + diff ); break;
case "m" : expdate.setMinutes(today.getMinutes() + diff ); break;
case "h" : expdate.setHours(today.getHours() + diff ); break;
case "d" : expdate.setDate(today.getDate() + diff ); break;
case "w" : expdate.setDate(today.getDate() + (diff*7) ); break;
case "M" : expdate.setMonth(today.getMonth() + diff ); break;
case "y" : expdate.setFullYear(today.getFullYear() + diff ); break;
default : expdate = today; break;
}
bbcjs.trace("dateFromNow: new date is: "+expdate, 5);
}
else bbcjs.trace("bbcjs.lib.date.dateFromNow:: ERROR - pattern does not match required.",1);
return expdate;
}
bbcjs.cookies = new Object();
bbcjs.cookies.prototype = new bbcjs.Module("cookies", 0.1, "$Revision: 1.10 $", "$Date: 2005/05/06 14:52:43 $");
bbcjs.cookies.cookieRaw = {};
bbcjs.cookies.cookieData = {};
bbcjs.cookies.loadOnSet = true;
bbcjs.cookies.loadCookies = function()
{
bbcjs.trace("<b>loadCookies called...</b>",3);
// Empty existing cookie objects
this.cookieData = {};
this.cookieRaw = {};
var tmpCookname, tmpArray, i, c, value;
//Split cookie contents on the ; seperator, store in allCookies
var allCookies = document.cookie.split("; ");
//Loop through all of our cookies found...
for (i=0; i<allCookies.length; i++)
{
//Split on the '=' seperator, which splits "cookieName=content"
tmpCookname = unescape(allCookies[i].split("=")[0]);
this.cookieRaw[tmpCookname] = allCookies[i].split("=")[1];
value = this.cookieRaw[tmpCookname];
bbcjs.trace("&nbsp;&nbsp;&nbsp;Found cookie: <b>"+tmpCookname+"</b>, calling string2obj...",4);
//Ignore leading space if we had a space after the last ';'
if (tmpCookname.charAt(0)==" ") tmpCookname = tmpCookname.substring(1,tmpCookname.length);
this.cookieData[tmpCookname] = bbcjs.lib.string.string2obj(unescape(value));
}
bbcjs.trace("<b>loadCookies found "+i+" cookie(s)...</b>",3);
return true;
}
bbcjs.cookies.setCookie = function(name, value, expiry, path, domain)
{
var t = "";
//if we pass the cookiedata type info, munge name=vals into a string.
if (typeof(value)=="object")
{
value = bbcjs.lib.string.obj2string(value);
}
//If we have a string, do our parsing with dateFromNow
if (typeof(expiry)=="string") expiry = bbcjs.lib.date.dateFromNow(expiry);
if ((typeof(expiry)=='undefined')||(expiry=='')) expiry = "";
else expiry = ("; expires="+expiry.toGMTString());
bbcjs.trace("setCookie: setting '"+name+"' to:<br />&nbsp;&nbsp;"+value, 3);
bbcjs.cookies.checkCookieLimits(name+"="+ escape(value));
path  = ((typeof(path)   =='undefined')||(path==''))   ? "; path=/":("; path="+path);
domain  = ((typeof(domain) =='undefined')|| (domain==''))? "; domain=bbc.co.uk":("; domain="+domain);
document.cookie = name+"="+ escape(value) + expiry + path + domain;
if (bbcjs.cookies.loadOnSet) bbcjs.cookies.loadCookies();
return true;
}
bbcjs.cookies.removeCookies = function()
{
bbcjs.trace("removeCookies called: deleting ALL cookies...",3);
var str = "", arr = [];
//loop through all cookies loaded from loadCookies, and set to expire in past
if (document.cookie != "") {
for (var i in bbcjs.cookies.cookieRaw)
{
bbcjs.cookies.deleteCookie(i);
arr[arr.length] = i;
}
}
return arr;
}
bbcjs.cookies.deleteCookie = function (name)
{
bbcjs.trace("deleteCookie: deleting '"+name+"'", 4);
return bbcjs.cookies.setCookie(name, "delete", "-1y");
}
bbcjs.cookies.checkCookieLimits = function (str)
{
var kB = (str.length/1024);
if (kB>=4) bbcjs.trace("WARNING: the cookie length may be too long for the browser ("+kB+"KB)",1);
else bbcjs.trace("checkCookieLimits: cookie length OK: ("+kB+"KB)",4);
return false;
}
//Load cookies straight away...
bbcjs.cookies.loadCookies();
bbcjs.trace('<b><font color="green">jst_cookies.js</font> was included.</b>',2);

