// Copyright Levexis Ltd	Fri Sep  3 11:18:49 2010 GMT	1	
if(typeof LEVU=="undefined"){LEVU=function(){function f(a){var b=document.getElementsByTagName("body");if(b.length===0)b=document.getElementsByTagName("head");b[0].appendChild(a)}return p={SQ:function(a){var b=this.FS(a);if(b){var c=b.src.indexOf("/"+a+"?");if(c>-1)return b.src.substr(c+a.length+2)}return""},ES:function(a){if((a=this.FS(a))&&a.innerHTML)try{eval(a.innerHTML)}catch(b){LEVU.LE("error with es of [ "+a.innerHTML+" ]","Error")}},FS:function(a){for(var b=document.getElementsByTagName("script"),
c=b.length-1;c>=0;c-=1)if(b[c].src.indexOf("/"+a)>-1)return b[c];return""},GP:function(a,b){if(b&&a)return LEVU.RX(a,"[?|&]?"+b+"=([^&]*)")},SP:function(a,b,c){var d=a.indexOf(b);if(d>0){d=RegExp("[&|?]"+b+"=[^&]*");return a.replace(d,b+"="+c)}else if(d<0)return a+"&"+b+"="+c;else{d=RegExp("^"+b+"=[^&]*");return a.replace(d,b+"="+c)}},AIF:function(a,b){var c=document.createElement("iframe");if(b){c.width="18px";c.height="18px"}else{c.width="0px";c.height="0px"}c.style.position="absolute";c.style.top=
"0px";c.style.left="-999em";f(c);c.src=a},WIF:function(a,b){var c=LEVU.GetCB(5),d='<iframe id="tmif'+c+'" src="" style="position: absolute; top: 0px; left: -999em " ';d+=b?'width = "18px" height="18px"':'width = "0px" height="0px"';document.write(d+"></iframe>");document.getElementById("tmif"+c).src=a},AJS:function(a){var b=document.createElement("script");b.type="text/javascript";f(b);b.src=a},CIm:function(a){(new Image).src=a},DS:function(a){if(!a)return 0;return a>=Math.random()*100?1:0},LE:function(a,
b){b||(b="Error");typeof console!="undefined"&&LEVU.GC("tmDebug")==1&&console.log("TagMan "+b+": "+a)},GetCB:function(a){return(Math.random()+"").substr(2,a)},GC:function(a){var b=document.cookie,c=b.indexOf(a+"=");if(c>-1){epos=b.indexOf(";",c);return epos>c?unescape(b.substr(c+a.length+1,epos-c-a.length-1)):unescape(b.substr(c+a.length+1))}else return""},SC:function(a,b,c,d){a=a+"="+escape(b)+";";if(d==1)a+=" expires=Tue, 2 Jun 2015 00:00:00 UTC;";c||(c="/");a+=" path="+c+";";document.cookie=a},
OTQ:function(a){var b=[];for(var c in a)b.push(escape(c)+"="+escape(a[c]));return b.join("&")},QTO:function(a){a=a.replace(/^[?&]/,"").split("&");for(var b={},c=0;c<a.length;c++){var d=a[c].split("=");b[unescape(d[0])]=unescape(d[1])}return b?b:""},IIF:function(){return window.parent.frames.length===0?0:1},RX:function(a,b){if(a&&b){var c=RegExp(b).exec(a);if(c){c.splice(0,1);return c.join("")}}return""},SS:function(a,b,c){if(a)return c?a.toString().substr(b,c):a.toString().substr(b);return""},FD:function(a,
b){if(a&&b)return a.indexOf(b)==-1?0:1;return 0},GS:function(a,b){if(a&&b){var c="";b=","+b+",";for(var d=a.split("/"),g=d.length*-1,e=0;e<d.length;e+=1){if(b.indexOf(","+e+",")>-1||b.indexOf(","+g+",")>-1){if(c)c+="/";c+=d[e]}g+=1}return c}},WJS:function(a,b){var c='<script type="text/javascript"';c+=b?' src="'+a+'">':">"+a;document.write(c+"<\/script>")},ADW:function(a){if(typeof LEVU.djs=="undefined")LEVU.djs="";LEVU.djs+=" "+a},IDW:function(){if(typeof LEVU.djs!="undefined"&&LEVU.djs){var a="https://"==
LEVU.GPR()?"https://sec":"http://res";LEVU.WJS(a+".levexis.com/js/LEVU.WDW.xjs",1)}},WDW:function(){if(typeof LEVU.djs!="undefined"&&LEVU.djs){LEVU.WJS(LEVU.djs,0);LEVU.djs=""}},GPR:function(){return location.protocol=="https:"?"https://":"http://"},PR:function(a){var b="";for(var c in a)b+="\t"+c+"=>"+a[c]+"\n";return b}}}();LEVU.ES("lu.js");var q=LEVU.SQ("lu.js");if(q){var u=LEVU.GPR()+q;LEVU.WJS(u,1)}};
// if the staging cookie is set it will now document.write the staging version of the tmap out instead
// it's done here so we can use the LEVU.GC function
if ( LEVU.GC('tmStaging') ) {
    // avoid infinite loop if someone puts live template on staging or is messing around with their dns
    if (typeof(tmIncludedOnce)=='undefined') {
        var stagingMapURL = LEVU.FS('octopustravel.js');
         if (stagingMapURL) {            
            var stagingMapURL = stagingMapURL.src.replace( /(.*?)\/\/(.*?)\.(.*?)\// , '$1//tagstaging.$3/' );
            LEVU.WJS(stagingMapURL,1);
        } else {
            LEVU.LE('Unable to find [octopustravel.js]','FATAL');
        }
        var tmIncludedOnce = 1;
    }
} else {
// not in staging mode proceed as normal
    tmParam['host']=location.hostname;

    
    // reverse compatibility
    if (typeof( tmPageId ) == 'undefined' && typeof( tmContId ) != 'undefined') var tmPageId = tmContId;
    
    if (typeof(tmPageId)!='undefined') LEVU.LE('base tmPageId='+tmPageId,'Debug');
    if (typeof(tmOPV)!='undefined') LEVU.LE('base tmOPV='+tmOPV,'Debug');
    
    // Initialise globals
    var searchRecords = [ LEVU.GS(document.location.toString(), 2) // Stem 2
,document.location.toString() // Total URL Rule
 ];
    
    if (!searchRecords) {
            LEVU.LE('No mapping rules found','Warning');
    } else {
            LEVU.LE("Mapping Rule Values\n" + LEVU.PR(searchRecords),'Debug');	
    }
    
// These are the libs used by this code the includes have to be at the start of the line


//
// If we have not already, define the TMAN namespace first
//
if (typeof(TMAN) == 'undefined'){ var TMAN = {} }

TMAN.MAP = function() { this.maps = {}; };
TMAN.MAP.prototype = {
	//
	// Adds map to TMAN.MAP hash object
	//
	AddMap: function(t) {
		h = new TMAN.MAP.HashMap(t);
		this.maps[h.head.ParentCId()] = h;
	},
	//
	// Adds map to TagMah hash object only if container ID matches
	//
	AddMapIf: function(c,t) {
		var HM = new TMAN.MAP.HashMap(t);
		var d = HM.head.ParentCId();
		if (d == c)
			this.maps[c] = HM;
	},
	//
	// Searches maps for text - hashing first
	//
	Search: function(t) {
		var h = TMAN.MAP.B92SHA1(t);
		for (var i in this.maps) {
			var m = this.maps[i];
			if (m)
				return m.Search(h);
		}
	}
};

TMAN.MAP.Head = function(m) {
	this.ok = true;	this.error = '';
	if (!m || (m.length == 0))
		return;
	this.v = TMAN.B92.desmall(m.substr(0,2));
	if (this.v !=  2) {
		this.ok = false; this.error = 'Version '+this.v+' is unsupported';
		return;
	}
	this.hlen = TMAN.B92.desmall(m.substr(2,1));
	this.clen = TMAN.B92.desmall(m.substr(3,1));
	this.cid  = TMAN.B92.desmall(m.substr(4,2));
	this.olen = TMAN.B92.desmall(m.substr(6,1));
	this.oto  = TMAN.B92.desmall(m.substr(7,2));
	a  = TMAN.B92.desmall(m.substr(9,1));
	this.schema = m.substr(10,a);
	this.maps = m.substr(10+a);
};
TMAN.MAP.Head.prototype = {
	Version:	function() { return this.v; },
	HashLen:	function() { return this.hlen; },
	CIdLen:		function() { return this.clen; },
	ParentCId:	function() { return this.cid; },
	OtherLen:	function() { return this.olen; },
	Maps:		function() { return this.maps; },
	OPVTO:		function() { return this.oto; },
	Schema:		function() { return this.schema; }
};

//
// Returns the sha1 hash of a text message - as a packed array of ints
//
TMAN.MAP.SHA1 = function(msg) {
	// This rotates 32 bit int s bits to left
	function ROL(n,s) { return ( n<<s ) | (n>>>(32-s)); }

	// Remove unichar chars
	function Utf8Encode(s) {
		string = s.replace(/\r\n/g,"\n");
		var u = "";
		for (var n = 0; n < s.length; n++) {
			var c = s.charCodeAt(n);
			if (c < 128)
				u += String.fromCharCode(c);
			else if((c > 127) && (c < 2048)) {
				u += String.fromCharCode((c >> 6) | 192);
				u += String.fromCharCode((c & 63) | 128);
			}
			else {
				u += String.fromCharCode((c >> 12) | 224);
				u += String.fromCharCode(((c >> 6) & 63) | 128);
				u += String.fromCharCode((c & 63) | 128);
			}
 
		}
		return u;
	}
	var blockstart;
	var i, j,k;
	var W = new Array(80);
	var H0 = 0x67452301; var H1 = 0xEFCDAB89; var H2 = 0x98BADCFE;
	var H3 = 0x10325476; var H4 = 0xC3D2E1F0;
	var A, B, C, D, E;
	var temp;
	//msg = Utf8Encode(msg);
	var msg_len = msg.length;
	var word_array = new Array();
	for( i=0; i<msg_len-3; i+=4 ) {
		j = msg.charCodeAt(i)<<24 | msg.charCodeAt(i+1)<<16 |
		msg.charCodeAt(i+2)<<8 | msg.charCodeAt(i+3);
		word_array.push( j );
	}
	switch( msg_len % 4 ) {
		case 0:
			i = 0x080000000;
		break;
		case 1:
			i = msg.charCodeAt(msg_len-1)<<24 | 0x0800000;
		break;
 
		case 2:
			i = msg.charCodeAt(msg_len-2)<<24 | msg.charCodeAt(msg_len-1)<<16 | 0x08000;
		break;
 
		case 3:
			i = msg.charCodeAt(msg_len-3)<<24 | msg.charCodeAt(msg_len-2)<<16 | msg.charCodeAt(msg_len-1)<<8	| 0x80;
		break;
	}
	word_array.push( i );
	while( (word_array.length % 16)!= 14) word_array.push(0);
	word_array.push( msg_len>>>29 );
	word_array.push( (msg_len<<3)&0x0ffffffff );
	for ( blockstart=0; blockstart<word_array.length; blockstart+=16 ) {
		for( i=0; i<16; i++ ) W[i] = word_array[blockstart+i];
		for( i=16; i<80; i++ ) W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);
                A = H0; B = H1; C = H2; D = H3; E = H4;
		for (i=0;i<80;i++) {
			k = W[i];
			if (i<20)
				temp = (ROL(A,5) + ((B&C) | (~B&D)) + E + k + 0x5A827999) & 0x0ffffffff;
			else	if (i<40)
					temp = (ROL(A,5) + (B ^ C ^ D) + E + k + 0x6ED9EBA1) & 0x0ffffffff;
				else	if (i<60)
						temp = (ROL(A,5) + ((B&C) | (B&D) | (C&D)) + E + k + 0x8F1BBCDC) & 0x0ffffffff;
					else
						temp = (ROL(A,5) + (B ^ C ^ D) + E + k + 0xCA62C1D6) & 0x0ffffffff;
                        E = D; D = C; C = ROL(B,30); B = A; A = temp;
                }
		H0 = (H0 + A) & 0xffffffff; H1 = (H1 + B) & 0xffffffff; H2 = (H2 + C) & 0xffffffff; H3 = (H3 + D) & 0xffffffff; H4 = (H4 + E) & 0xffffffff;
	}
	return [ H0, H1 , H2, H3, H4 ];
};
//
// Given a string returns the b92 encoded sha hash as a string of 25 chars.
//
TMAN.MAP.B92SHA1 = function(text) { return TMAN.B92.encode(TMAN.MAP.SHA1(text),25); };

//
// A specific Map now...
//
TMAN.MAP.Map = function(m,h) {
	if (!m || !h)
		return;
	this.head = h;
	this.hash = m.substr(0,h.HashLen());
	this.Cid   = TMAN.B92.desmall(m.substr(h.HashLen(),h.CIdLen()));
	this.Other = TMAN.B92.desmall(m.substr(h.HashLen()+h.CIdLen(),h.OtherLen()));
};
TMAN.MAP.Map.prototype = {
	Hash:		function() { return this.hash; },
	ContainerId:	function() { return this.Cid; },
	Header:		function() { return this.head; },
	OncePerVisit:	function() { return ((this.Other & 1)==1)?true:false; }
};

TMAN.MAP.HashMap = function(t) {
	this.text = t;
	this.head = new TMAN.MAP.Head(this.text);
};
TMAN.MAP.HashMap.prototype.Search = function(m) {
	var h = this.head;
	//
	// Extract hashlen and entire map len
	//
	var hl = h.HashLen();
	var ml = hl + h.CIdLen() + h.OtherLen();
	//
	// Get the least most hashlen bytes
	//
	m = m.substr(m.length-hl);
	var t = h.Maps(); var i = 0; var j=0;
	while (i != -1) {
	    j = t.indexOf(m,i);
	    //
	    // If a match found, but not on a boundary then try to find next one...
	    //
	    if ((j % ml) == 0)
	        return new TMAN.MAP.Map(t.substr(j,ml),h);
	    if (j==i) // e.i. we havn't moved on!
	        i = j + 1;
	    else
	        i = j;
	}  
	return;
};
if(typeof(TMAN)=="undefined"){var TMAN={}}TMAN.B92={};TMAN.B92.div=function(f){var a=f.length;var h=new Array(a*2);var g=0;var d=0;var e=0;for(e=0;e<a*2;e++){g<<=16;if((e%2)==0){h[e]=((f[d]>>16)&65535)|g}else{h[e]=(f[d++]&65535)|g}g=h[e]%92;h[e]=h[e]/92|0}var b=new Array(a);for(e=0;e<a;e++){b[e]=(h[e*2]<<16)|(h[e*2+1])}TMAN.B92.rem=g;return b};TMAN.B92.encode=function(e,a){var f=e.length;var g=new Array(f);var b=new Array(a);var d=0;TMAN.B92.rem=0;for(d=0;d<f;d++){g[d]=e[d]}for(d=0;d<a;d++){g=TMAN.B92.div(g);b[d]=TMAN.B92.rem}var c="";while(a>0){d=b[--a];if(d>58){c+=String.fromCharCode(d+34)}else{if(d>1){c+=String.fromCharCode(d+33)}else{c+=String.fromCharCode(d+32)}}}return c};TMAN.B92.decode=function(l,f){var h=l.length>>1;if((f*2)>h){h=f*2}var a=new Array(h);var b=0;var d=0;for(b=0;b<h;b++){a[b]=0}for(d=0;d<l.length;d++){for(b=0;b<h;b++){a[b]*=92;if(b==0){var g=l.charCodeAt(d);if(g>92){a[0]+=g-34}else{if(g>34){a[0]+=g-33}else{a[0]+=g-32}}}}for(b=0;b<h-1;b++){var e=a[b]>>16;if(e){a[b+1]+=e;a[b]&=65535}}}var k=new Array(f);for(d=0;d<f;d++){b=f-(d+1);k[b]=a[(d*2)+1]<<16;k[b]+=a[d*2]}return k};TMAN.B92.desmall=function(c){var b=TMAN.B92.decode(c,1);return b[0]};
    
    // These are the maps...
    TMAN.MapStrings = [ " #!! #!  .octopustravelV) t+  , h. z/ H0 >- " ];
    
    if (!TMAN.MapStrings) {
            LEVU.LE('No mappings for this container','Warning');
    } else {
            LEVU.LE("Mapping string\n" + LEVU.PR(TMAN.MapStrings),'Debug');	
    }
    
    var TH = new TMAN.MAP;
    var i = 0;
    // only search the map for the base container that matches the pageId
    for (var i=0;i<TMAN.MapStrings.length;i++) TH.AddMapIf(tmPageId,TMAN.MapStrings[i]);
    // Now look for the container on the extract rules, one at a time
    var container;
    i = searchRecords.length;
    while (i > 0 && !container) {
            i--;
            // watch out for errors if expression not found
            if(typeof(searchRecords[i])!='undefined') container = TH.Search(i+searchRecords[i].toString());
    }
    // update pageId and ContainerId if matched
    if (container) {
            var sourcePageId =  tmPageId;
      if (container.OncePerVisit()) {
            tmOPV=container.Header().OPVTO();
      } else {
            tmOPV=0;
      }
      tmPageId = container.ContainerId();
    }
}