/* anciliary script library - maintained by Online Team */
/* initial load - David Green 25/11/2009 */

/* ---------------------- General functions ----------------------- */

function fetchStyle(path,media) {
	if (typeof media == "undefined") { media = "screen"; }
	/*document.writeln(unescape('%3Clink href="'+path+'" rel="stylesheet" type="text/css" media="'+media+'" /%3E'));*/
	var tempStyle = document.createElement("link");
	tempStyle.type = "text/css";
	tempStyle.rel = "stylesheet";
	tempStyle.href = path;
	tempStyle.media = media;
	document.getElementsByTagName("head")[0].appendChild(tempStyle);
}
function createStyle(styleString) {
	document.writeln(unescape('%3Cstyle%3E '+styleString+' %3C/style%3E'));
}
function fetchScript(path) {
	/*document.write(unescape('%3Cscript language="javascript" type="text/javascript" src="'+path+'"%3E%3C/script%3E'));*/
	var tempScript = document.createElement("script");
	tempScript.type="text/javascript";
	tempScript.src=path;
	document.getElementsByTagName("head")[0].appendChild(tempScript);
}

function findDOM(objectID,withStyle) { 
	if (withStyle == 1) { return (document.getElementById(objectID).style); }
	else { return (document.getElementById(objectID)); }
}

function candyStripe() { // Parameters : tableid [,oddClass,evenClass]
	if (arguments.length == 3) {
		var tid = arguments[0];
		var oddClass = arguments[1]; var evenClass = arguments[2];
	} else {
		var tid = arguments[0];
		var oddClass = "odd"; var evenClass = "evn";
	}
	var oddRow = true;
	var allrows=findDOM(tid,0).getElementsByTagName("tr");
	for (i=0; i<allrows.length; i++) { 	
		if (oddRow) { allrows[i].className += " " + oddClass; } else { allrows[i].className += " " + evenClass; }
		oddRow = !oddRow;
	}
}

function candySpotter(tid) { 
	var oddCell = true;
	var allcells=findDOM(tid,0).getElementsByTagName("td");
	for (i=0; i<allcells.length; i++) { 	
		if (oddCell) { allcells[i].style.backgroundColor = "#fafafa"; } else { allcells[i].style.backgroundColor = "#f3f3f3"; }
		oddCell = !oddCell;
	}
}

function addEvent(obj, evType, fn) { 
	if (obj.addEventListener) { 
		obj.addEventListener(evType, fn, false); 
 		return true; 
 	} else if (obj.attachEvent) { 
		var r = obj.attachEvent("on"+evType, fn); 
 		return r; 
 	} else { 
 		return false; 
 	} 
}

function getElementsByClass(searchClass,tag) {
	var classElements = new Array();
	if (tag == null) tag = '*';
	var els = document.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if (pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

/* -------------------- Track Flash buttons ---------------------- */

function trackFlash(url,label) {
	if (waGAOK || waWT9OK) { /* Web Analytics available */
		if (typeof label != "undefined") { waEventCallStr(label); }
	}
	window.location.replace(url);
}

/* ------------------ URL Search string array -------------------- */

var request = new Array();
var query = unescape(document.location.search);
query = query.substring(1,query.length);
query = query.replace(/\+/g," ");
var arrQ = query.split("&");
for(i=0;i<arrQ.length; i++) { arrQ2 = arrQ[i].split('='); request[arrQ2[0]] = arrQ2[1]; }

/* ------------------- Cookie Management ------------------------- */

function createCookie(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() : "") +
	((path) ? ";path=" + path : ";path=/") +
	((domain) ? ";domain=" + domain : "") +
	((secure) ? ";secure" : "");
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

/* ---------- Search Engine Marketing Keyword processing --------- */

if (typeof request["semcp"] != "undefined") {
	if (request["semcp"].substr(0,4).toUpperCase() == "MLC ") { request["semcp"] = request["semcp"].substr(4); } // strip off leading MLC
	eraseCookie("contactAdviserGoogleCamp"); createCookie("contactAdviserGoogleCamp","SEM "+request["semcp"],7);
	eraseCookie("contactAdviserGoogleAdword"); createCookie("contactAdviserGoogleAdword",request["semad"],7);
	eraseCookie("contactAdviserGoogleTerm"); createCookie("contactAdviserGoogleTerm",request["utm_term"],7);
	
}

function googleSEMget(x) {
	var GSGtemp = "";
	switch (x) {
		case "semcp" : GSGtemp = readCookie("contactAdviserGoogleCamp"); break;
		case "semad" : GSGtemp = readCookie("contactAdviserGoogleAdword"); break;
		case "utm_term" : GSGtemp = readCookie("contactAdviserGoogleTerm"); break;
	}
	if (GSGtemp == null) { GSGtemp = "MLC Website"; }
	return GSGtemp;
}

/* ------------------------ What we offer ------------------------- */
/* to be deprecated */

var offerSet = 5; // redefine this variable locally to overide

function offerToggle(el,state) {
	findDOM(el,1).display=(state)?"block":"none";	
	findDOM(el+"btn",1).backgroundColor=(state)?"#EA660D":"#FFFFFF";
	findDOM(el+"btn",1).color=(state)?"#FFFFFF":"#EA660D";
}

function offerHide() { 
	offerToggle('offer1',false);
	offerToggle('offer2',false);
	offerToggle('offer3',false);
	offerToggle('offer4',false);
	offerToggle('offer5',false);
}

function offerShow(x) {
	clearTimeout(offerTime);
	offerInit(x.rel);
}

function offerInit(x) {
	offerHide();
	offerToggle(x,true);
	offerNext("later");
}

function offerGetNext() {
	for (i=1;i<=offerSet;i++) { 	if (findDOM("offer"+i,1).display=="block") { co = i; break; } }
	co++; if (co>offerSet) { co = 1; }
	return "offer"+co;
}

function offerNext(when) {
	if (when=="later") { 	offerTime = setTimeout("offerInit('"+offerGetNext()+"')",10000); }
	else { clearTimeout(offerTime); offerInit(offerGetNext()); }
}

/* -------------------- What we offer version 2 --------------------- */

function offerShowSet(varName,elementId,elementFirst,elementLast,delay) {

	this.varName = varName;
	this.setName = elementId;
	this.offerFirst = (typeof elementFirst != "undefined")?elementFirst:1;
	this.offerLast = (typeof elementLast != "undefined")?elementLast:5;
	this.delay = (typeof delay != "undefined")?delay:10;
	this.buttonBgOn = "#EA660D";
	this.buttonBgOff = "#FFFFFF";
	this.buttonTxtOn = "#FFFFFF";
	this.buttonTxtOff = "#EA660D";
	this.offerTime = null;
	this.offerChange = true;
	
	if (this.delay > 0) { this.delay *= 1000; } else { this.offerChange = false; }
	
	offerShowSet.prototype.offerToggle = function(setName,state) {
		findDOM(setName,1).display=(state)?"block":"none";	
		findDOM(setName+"btn",1).backgroundColor=(state)?this.buttonBgOn:this.buttonBgOff;
		findDOM(setName+"btn",1).color=(state)?this.buttonTxtOn:this.buttonTxtOff;
	}
	
	offerShowSet.prototype.offerHide = function() {
		for (var i=this.offerFirst; i<=this.offerLast; i++) { this.offerToggle(this.setName+i,false); }
	}
	
	offerShowSet.prototype.offerGoTo = function(x) {
		if (this.offerChange) { clearTimeout(this.offerTime); }
		this.offerInit(x.rel);
	}
	
	offerShowSet.prototype.offerInit = function(n) {
		this.offerHide();
		this.offerToggle(n,true);
		if (this.offerChange) { this.offerNext("later"); }
	}
	
	offerShowSet.prototype.offerGetNext = function() {
		for (var i=this.offerFirst; i<=this.offerLast; i++) { 	if (findDOM(this.setName+i,1).display=="block") { co = i; break; } }
		co++; if (co > this.offerLast) { co = this.offerFirst; }
		return this.setName+co;
	}
	
	offerShowSet.prototype.offerNext = function(when) {
		if (when=="later") { 	this.offerTime = setTimeout(this.varName+".offerInit('"+this.offerGetNext()+"')",this.delay); }
		else { clearTimeout(this.offerTime); this.offerInit(this.offerGetNext()); }
	}

	for (var i=this.offerFirst; i<=this.offerLast; i++) { 
	  var dispId = i-this.offerFirst+1;
		document.write('<a onclick="'+this.varName+'.offerGoTo(this);" class="offerbtn" id="'+this.setName+i+'btn" rel="'+this.setName+i+'">'+dispId+'</a>');
	}
	document.write('<a onclick="'+this.varName+'.offerNext(\'now\');" style="margin-left: 3px; cursor:pointer;">next &raquo;</a>');
	
	this.offerInit(this.setName+this.offerFirst);
}

/* ------------------------ feature tabs ------------------------- */
/* for mini-sites and adhoc usage                                  */

function featureToggle(el,state) { document.getElementById(el).style.display=state;	}

function featureHide() {
	featureHideAll(); // on local page
	var alltabs = document.getElementById('featuretabs').childNodes;
	for (var i=0;i<alltabs.length;i++) {
		if (alltabs[i].nodeName == 'LI') {
			alltabs[i].firstChild.className = '';
		}
	}
}

function featureShow(which) {
	var tmp = which.rel;
	if (typeof waEventCallStr == "function") { waEventCallStr(tmp); } // Web Analytics
	featureHide();
	featureToggle(tmp,'block');
	which.className='on';
}

function featureInit(which) {
	featureHide();
	featureToggle(which,'block');
	var alltabs = document.getElementById('featuretabs').childNodes;
	for (var i=0;i<alltabs.length;i++) {
		if (alltabs[i].nodeName == 'LI' && alltabs[i].firstChild.rel == which) {
			alltabs[i].firstChild.className = 'on';
		}
	}
}

/* ------------------------ faq def lists ------------------------ */
/* for mini-sites and adhoc usage                                  */

function togglefaq(which) {
	var tmpdt = document.getElementById(which);
	var tmpdd = document.getElementById(which+'dd');
	if (tmpdd.style.display == 'none' || !tmpdd.style.display.length ) {
		tmpdd.style.display = 'block';
		tmpdt.style.backgroundImage = 'url(/includes/imagesglobal/faq_minus.gif)';
		waEventCallStr(which); // Web Analytics
	} else {
		tmpdd.style.display = 'none';
		tmpdt.style.backgroundImage = 'url(/includes/imagesglobal/faq_plus.gif)';
	}
}

/* --------------------------- corners --------------------------- */

function featurepanelcorners() {
	var tmpArr = getElementsByClass('feature','div');
	for (i=0;i<tmpArr.length;i++) {
		for(j=0;j<tmpArr[i].childNodes.length;j++) {
			if(tmpArr[i].childNodes[j].nodeName == 'H1' || tmpArr[i].childNodes[j].nodeName == 'H2') {
				tmpArr[i].childNodes[j].innerHTML = tmpArr[i].childNodes[j].innerHTML + '<span class="cnrleft"></span>';
			}
		}
	}
}
function tabgrpcorners() {
	var tmpArr = getElementsByClass('tabgrp','div');
	for (i=0;i<tmpArr.length;i++) {
		tmpArr[i].innerHTML = tmpArr[i].innerHTML + '<span class="cnrleft"></span>';
	}
}


function othrcorners(which) {
	var tmpArr = getElementsByClass('buttn',which);
	for (i=0;i<tmpArr.length;i++) {
		tmpArr[i].innerHTML = tmpArr[i].innerHTML + '<span class="cnrtopleft"></span><span class="cnrtopright"></span><span class="cnrbotleft"></span><span class="cnrbotright"></span>';
	}
}
function tabcorners(which) {
	if (document.getElementById(which)) {
		var tmpEl = document.getElementById(which);
		for (i=0;i<tmpEl.childNodes.length;i++) {
			if(tmpEl.childNodes[i].nodeName == 'LI') {
				tmpEl.childNodes[i].firstChild.innerHTML = tmpEl.childNodes[i].firstChild.innerHTML + '<span class="cnrleft"></span>';
			}
		}
	}
}
function crnrs() {
	tabcorners('segment')
	tabcorners('pri')
	tabcorners('featuretabs')
	tabcorners('mktab')
	othrcorners('button')
	othrcorners('a')
}

/* --------------------------- TinyBox --------------------------- */

var TINY={};

function T$(i){return document.getElementById(i)}

TINY.box=function(){
	var p,m,b,fn,ic,iu,iw,ih,ia,f=0;
	return{
		show:function(c,u,w,h,a,t){
			if(!f){
				p=document.createElement('div'); p.id='tinybox';
				m=document.createElement('div'); m.id='tinymask';
				b=document.createElement('div'); b.id='tinycontent';
				document.body.appendChild(m); document.body.appendChild(p); p.appendChild(b);
				m.onclick=TINY.box.hide; window.onresize=TINY.box.resize; f=1
			}
			if(!a&&!u){
				p.style.width=w?w+'px':'auto'; p.style.height=h?h+'px':'auto';
				p.style.backgroundImage='none'; b.innerHTML=c
			}else{
				b.style.display='none'; p.style.width=p.style.height='100px'
			}
			this.mask();
			ic=c; iu=u; iw=w; ih=h; ia=a; this.alpha(m,1,80,3);
			if(t){setTimeout(function(){TINY.box.hide()},1000*t)}
		},
		fill:function(c,u,w,h,a){
			if(u){
				p.style.backgroundImage='';
				var x=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');
				x.onreadystatechange=function(){
					if(x.readyState==4&&x.status==200){TINY.box.psh(x.responseText,w,h,a)}
				};
				x.open('GET',c,1); x.send(null)
			}else{
				this.psh(c,w,h,a)
			}
		},
		psh:function(c,w,h,a){
			if(a){
				if(!w||!h){
					var x=p.style.width, y=p.style.height; b.innerHTML=c;
					p.style.width=w?w+'px':''; p.style.height=h?h+'px':'';
					b.style.display='';
					w=parseInt(b.offsetWidth); h=parseInt(b.offsetHeight);
					b.style.display='none'; p.style.width=x; p.style.height=y;
				}else{
					b.innerHTML=c
				}
				this.size(p,w,h)
			}else{
				p.style.backgroundImage='none'
			}
		},
		hide:function(){
			TINY.box.alpha(p,-1,0,3)
		},
		resize:function(){
			TINY.box.pos(); TINY.box.mask()
		},
		mask:function(){
			m.style.height=TINY.page.total(1)+'px';
			m.style.width=''; m.style.width=TINY.page.total(0)+'px'
		},
		pos:function(){
			var t=(TINY.page.height()/2)-(p.offsetHeight/2); t=t<10?10:t;
			p.style.top=(t+TINY.page.top())+'px';
			p.style.left=(TINY.page.width()/2)-(p.offsetWidth/2)+'px'
		},
		alpha:function(e,d,a){
			clearInterval(e.ai);
			if(d==1){
				e.style.opacity=0; e.style.filter='alpha(opacity=0)';
				e.style.display='block'; this.pos()
			}
			e.ai=setInterval(function(){TINY.box.ta(e,a,d)},20)
		},
		ta:function(e,a,d){
			var o=Math.round(e.style.opacity*100);
			if(o==a){
				clearInterval(e.ai);
				if(d==-1){
					e.style.display='none';
					e==p?TINY.box.alpha(m,-1,0,2):b.innerHTML=p.style.backgroundImage=''
				}else{
					e==m?this.alpha(p,1,100):TINY.box.fill(ic,iu,iw,ih,ia)
				}
			}else{
				var n=Math.ceil((o+((a-o)*.5))); n=n==1?0:n;
				e.style.opacity=n/100; e.style.filter='alpha(opacity='+n+')'
			}
		},
		size:function(e,w,h){
			e=typeof e=='object'?e:T$(e); clearInterval(e.si);
			var ow=e.offsetWidth, oh=e.offsetHeight,
			wo=ow-parseInt(e.style.width), ho=oh-parseInt(e.style.height);
			var wd=ow-wo>w?0:1, hd=(oh-ho>h)?0:1;
			e.si=setInterval(function(){TINY.box.ts(e,w,wo,wd,h,ho,hd)},20)
		},
		ts:function(e,w,wo,wd,h,ho,hd){
			var ow=e.offsetWidth-wo, oh=e.offsetHeight-ho;
			if(ow==w&&oh==h){
				clearInterval(e.si); p.style.backgroundImage='none'; b.style.display='block'
			}else{
				if(ow!=w){var n=ow+((w-ow)*.5); e.style.width=wd?Math.ceil(n)+'px':Math.floor(n)+'px'}
				if(oh!=h){var n=oh+((h-oh)*.5); e.style.height=hd?Math.ceil(n)+'px':Math.floor(n)+'px'}
				this.pos()
			}
		}
	}
}();

TINY.page=function(){
	return{
		top:function(){return document.documentElement.scrollTop||document.body.scrollTop},
		width:function(){return self.innerWidth||document.documentElement.clientWidth||document.body.clientWidth},
		height:function(){return self.innerHeight||document.documentElement.clientHeight||document.body.clientHeight},
		total:function(d){
			var b=document.body, e=document.documentElement;
			return d?Math.max(Math.max(b.scrollHeight,e.scrollHeight),Math.max(b.clientHeight,e.clientHeight)):
			Math.max(Math.max(b.scrollWidth,e.scrollWidth),Math.max(b.clientWidth,e.clientWidth))
		}
	}
}();

/* ----------------- TinyBox load flash video overlay for Personal Homepage Flash Feature Videos ------------------ */

function vidOverlay() {
	TINY.box.show('<embed width=\'719\' height=\'400\' wmode=\'transparent\' quality=\'high\' bgcolor=\'#FFFFFF\' name=\'flashtest\' id=\'banner\'  src=\'/resources/MLC/Flash/mlc-video.swf\' type=\'application/x-shockwave-flash\'><br /><a href=\'javascript:TINY.box.hide();\'>Close</a>',0,719,405,0,0);
}

function vidOverlay_MLCBrand_01() {
	TINY.box.show('<embed width=\'719\' height=\'400\' wmode=\'transparent\' quality=\'high\' bgcolor=\'#FFFFFF\' name=\'flashtest\' id=\'banner\'  src=\'/resources/MLC/Flash/AdviceFeature/mlc0329_45.swf\' type=\'application/x-shockwave-flash\'><br /><a href=\'javascript:TINY.box.hide();\'>Close</a>',0,719,405,0,0);
}

function vidOverlay_MLCAdvice_02() {
	TINY.box.show('<embed width=\'719\' height=\'400\' wmode=\'transparent\' quality=\'high\' bgcolor=\'#FFFFFF\' name=\'flashtest\' id=\'banner\'  src=\'/resources/MLC/Flash/AdviceFeature/mlc0329_30a.swf\' type=\'application/x-shockwave-flash\'><br /><a href=\'javascript:TINY.box.hide();\'>Close</a>',0,719,405,0,0);
}

function vidOverlay_MLCInsurance_03() {
	TINY.box.show('<embed width=\'719\' height=\'400\' wmode=\'transparent\' quality=\'high\' bgcolor=\'#FFFFFF\' name=\'flashtest\' id=\'banner\'  src=\'/resources/MLC/Flash/AdviceFeature/mlc0343_30i.swf\' type=\'application/x-shockwave-flash\'><br /><a href=\'javascript:TINY.box.hide();\'>Close</a>',0,719,405,0,0);
}
/* ----------------------- My Client Leads Flash Overlay ------------------------ */
	function flashOverlay_MCL() {
		TINY.box.show('<embed width=\'1085\' height=\'550\' wmode=\'transparent\' quality=\'high\' bgcolor=\'#FFFFFF\' name=\'flashtest\' id=\'banner\'  src=\'/resources/MLC/Flash/My_Client_Leads.swf\' type=\'application/x-shockwave-flash\'><br /><a href=\'javascript:TINY.box.hide();\'>Close</a>',0,1085,550,0,0);
}
/* ----------------------- autoload stylesheet ------------------------ */

var cssLoadArray = new Array();
cssLoadArray[0] = ["/portal/site/mlc/template.PAGE/groupInsurance","/resources/MLC/css/gi_new.css"];
cssLoadArray[1] = ["/portal/site/mlc/groupInsurance","/resources/MLC/css/gi_new.css"];
for (var i=0;i<cssLoadArray.length;i++) {
	if (window.location.pathname.indexOf(cssLoadArray[i][0])!=-1) { fetchStyle(cssLoadArray[i][1]); break; }
}

/* --------------------------- load corners --------------------------- */

//addEvent(window, 'load', pricorners);
addEvent(window, 'load', crnrs);
addEvent(window, 'load', featurepanelcorners);
addEvent(window, 'load', tabgrpcorners);


