// -*- mode: javascript -*-
//
// Author: Memetrics Holding Pty. Ltd. 2005.
// All Rights Reserved.
//
// License: You may use and extend this library in accordance
// with the terms of your XOS(TM) license.
//
// Version: $Id: xos-toolkit.js,v 1.11.2.9 2007/02/21 03:49:47 svn Exp $
// Name: $Name: xos-4-0-ui-rewrite-branch $
//
//
////////////////////////////////////////////////////////////////////////////////
//
// If you don't have access to your web server config and can't get a
// P3P header automatically included then a compact version of the
// policy may be include in each html page by using
// tags. For example;
//
//
//
// Note DO NOT just copy and paste this into your pages, create a
// valid P3P policy for your website.
////////////////////////////////////////////////////////////////////////////////
//
// Cookie Handling from: http://www.dustindiaz.com/top-ten-javascript
//
var getCookie = function( name ) {
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ((!start) && (name != document.cookie.substring( 0, name.length ))) {
return null;
}
if (start == -1) {
return null;
}
var end = document.cookie.indexOf( ';', len );
if (end == -1) {
end = document.cookie.length;
}
return unescape( document.cookie.substring( len, end ) );
};
var setCookie = function( name, value, expires, path, domain, secure ) {
var today = new Date();
today.setTime( today.getTime() );
if (expires) {
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );
document.cookie = name + '=' + escape( value ) +
( ( expires ) ? ';expires=' + expires_date.toGMTString() : '' ) + //expires.toGMTString()
( ( path ) ? ';path=' + path : '' ) +
( ( domain ) ? ';domain=' + domain : '' ) +
( ( secure ) ? ';secure' : '' );
};
var deleteCookie = function( name, path, domain ) {
if (getCookie( name )) {
document.cookie = name + '=' +
( ( path ) ? ';path=' + path : '' ) +
( ( domain ) ? ';domain=' + domain : '' ) + ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}
};
////////////////////////////////////////////////////////////////////////////////
//
// JS/DOM Utilities
//
var _XOS_addEvent = function(o,e,f) {
if (o.addEventListener){ o.addEventListener(e,f,true); return true; }
else if (o.attachEvent){ return o.attachEvent("on"+e,f); }
else { return false; }
};
var _XOS_getElementsByTagAndClassName = function( tagName, classRegExp, parent ) {
tagName = tagName || "*";
parent = parent || document;
classRegExp = classRegExp || null;
var all = parent.getElementsByTagName( tagName );
var element = null;
var elements = [];
for (var idx = 0; idx < all.length; idx++) {
element = all[idx];
// ignore anything that isn't an element.
if (element.nodeType != 1) { continue; }
// If the want the additional test for a class then do it
if (classRegExp !== null) {
if (element.className && (-1 < element.className.search( classRegExp ))) {
elements.push( element );
}
}
//..otherwise it matched the on the tag so include it
else {
elements.push( element );
}
}
return elements;
};
var _XOS_isNullOrUndefined = function( thing ) {
return (thing === null) || (thing === undefined);
};
var _XOS_hasProperties = function( obj ) {
for (var key in obj) { return true; }
return false;
};
////////////////////////////////////////////////////////////////////////////////
//
// Functional Helpers
//
var _XOS_identity = function( thing ) {
return thing;
};
var _XOS_removeIf = function( predicate, list ) {
var results = [];
for (var idx = 0; idx < list.length; idx++) {
if (!predicate( list[idx] )) {
results.push( list[idx] );
}
}
return results;
};
var _XOS_removeIfNot = function( predicate, list ) {
var results = [];
for (var idx = 0; idx < list.length; idx++) {
if (predicate( list[idx] )) {
results.push( list[idx] );
}
}
return results;
};
// An extremely simple map, treats each entry in the list as a
// arguments to the function.
//
// Example Usage: map( function( p ){ return p + 2; }, [1, 2, 3] );
var _XOS_mapApply = function( fn, list ) {
var result = [];
var args = null;
for (var idx = 0; idx < list.length; idx++) {
args = list[idx];
// If the args are already a list, assume that the caller knows
// what they are doing and wants these passed as arguments to the
// function. However as a convenience if we only have a single
// argument for each call just wrap it in a list to stop JS from
// complaining. We do this instead of just calling fn( args ) as
// we want to explicitly make this == null to avoid any crazy JS
// this wrangling and hence unexpected behaviour.
if (typeof(args.length) != 'number') {
args = [args];
}
result.push( fn.apply( null, args ) );
}
return result;
};
////////////////////////////////////////////////////////////////////////////////
//
// Misc Helpers
//
var _XOS_items = function (obj) {
var result = [];
var e;
for (var prop in obj) {
var v;
try {
v = obj[prop];
} catch (e) {
continue;
}
result.push([prop, v]);
}
return result;
};
function _XOS_makeGUID() {
return Math.floor(Math.random() * 1000000000000);
}
// Joins the arguments together inserting the prefix between each.
// null/undefined arguments are ignored
var _XOS_Join = function() {
if (arguments.length < 2) { return arguments[1] || ""; }
var prefix = arguments[0];
var delimiter = "";
var result = "";
for (var i = 1; i < arguments.length; i++) {
if (!_XOS_isNullOrUndefined(arguments[i])) {
result = result + delimiter + arguments[i];
delimiter = prefix;
}
}
return result;
};
////////////////////////////////////////////////////////////////////////////////
//
// XOS Treatment Handler
//
var SCRIPT_PATH = null;
var TREATMENT_SCRIPT = null;
var PREVIEW_SCRIPT = null;
var XOS_TEST_COOKIE = "XOS Test Cookie";
var XOS_VISITOR_KEY = "xosVisitorKey";
// The JS execution model means that each script is executed before
// the next one (if any) is loaded, this means that at the time the
// following bit of JS runs this script (ie, xos-toolkit.js) will be
// the last