//Javascript from Manulife

function languageToggle() {
var theA=document.getElementById("lang-select");
if(theA){
var theurl=window.location.href;
if(theurl.indexOf("/french")>0){
if(theA.href!="http://www.coverme.com/")theA.href=theurl.replace("/french","");
}else{
pos=theurl.indexOf("//");
pos=theurl.indexOf("/",pos+2);
theA.href=theurl.substring(0,pos)+'/french'+theurl.substring(pos);
}
}
}

function appendMkt(theref,themkt){
if(themkt=="" || theref.substring(0,1)=="#" || theref.toLowerCase().substring(0,10)=="javascript"){return theref;}
else{
if(theref.indexOf("covermelife")>0||theref.indexOf("assurance-viepourmeproteger")>0||theref.indexOf("mlilacc01")>0){themkt="Open&"+themkt;}
if(theref.indexOf("www.manulife.ca")>0) return theref;
if(theref.toLowerCase().indexOf("mkt=")>=0) {
mktposHref=theref.toLowerCase().indexOf("mkt=");
mktHref=theref.indexOf("&",mktposHref)>0?theref.substring(mktposHref+4,theref.indexOf("&",mktposHref)):theref.substring(mktposHref+4);
return theref.replace("MKT="+mktHref,themkt).replace("mkt="+mktHref,themkt);
}
else if(theref.indexOf("?")>=0) return theref+"&"+themkt;
else return theref+"?"+themkt;
}
}

function updateEnviron(thelink,theanchor,theMkt){
var theurl=window.location.href.toLowerCase();
var thehref=thelink;	if(thehref==null) return;
var found=0;
if(!theMkt)theMkt="";
if (theurl.indexOf("localhost")>0||theurl.indexOf("testux")>0){thehost="localhost";thehost2="testux";found=1;} 
else if(theurl.indexOf("devsecure.lhplans.com")>0) {thehost="www.dev.memberhealthplan.com";thehost2="devsecure.lhplans.com";found=1;}
else if(theurl.indexOf("www.dev.memberhealthplan.com")>0) {thehost="www.dev.memberhealthplan.com";thehost2="devsecure.lhplans.com";found=1;}
if(found){
if (thehref.indexOf("secure.lhplans.com")>0&&thehref.indexOf("devsecure.lhplans.com")<0){
thehref=thehref.replace("secure.lhplans.com",thehost2);
}else if(thehref.indexOf("www.coverme.com")>0){
thehref=thehref.replace("www.coverme.com",thehost);
}else if(thehref.indexOf("www.pourmeproteger.com")>0){
thehref=thehref.replace("www.pourmeproteger.com",thehost);
}else if(thehref.indexOf("www.covermelife.com")>0){			//life
thehref=thehref.replace("www.covermelife.com","affinityuat.manulife.com");
}else if(thehref.indexOf("www.covermetravel.com")>0){			//travel
thehref=thehref.replace("www.covermetravel.com","affinityuat.manulife.com");
}else if(thehref.indexOf("www.assurance-viepourmeproteger.com")>0){			//life fr
thehref=thehref.replace("www.assurance-viepourmeproteger.com","affinityuat.manulife.com");
}
thehref=appendMkt(thehref,theMkt);
if(theanchor)theanchor.setAttribute("href",thehref);
}else if(theMkt!=""){
thehref=appendMkt(thehref,theMkt);
if(theanchor)theanchor.setAttribute("href",thehref);
}
return thehref;
}

function environCheck() {
var anchors = document.getElementsByTagName("a"),thehref,thehost;
var referrer=document.referrer;
var href=window.location.href;
var query=href.indexOf("?")>=0?href.substring(href.indexOf("?")):"";
var mktpos=query.toLowerCase().indexOf("mkt="),mkt="";
if(mktpos>=0){mkt=query.indexOf("&",mktpos)>0?query.substring(mktpos+4,query.indexOf("&",mktpos)):query.substring(mktpos+4)}
for (var i = 0; i < anchors.length; i++) {
//Environment+MKT
thehref=anchors[i].getAttribute("href");

//Add for Organic Search Engine activity
if(mkt=="" && referrer!= '') {
   referrer=referrer.toLowerCase();
   if (referrer.indexOf("google")>=0 && referrer.indexOf("google")<=30) {
   	mkt="W9G";
   }
   if (referrer.indexOf("yahoo")>=0 && referrer.indexOf("yahoo")<=30) {
   	mkt="W9Y";
   }

}

if(mkt=="") updateEnviron(thehref,anchors[i],"");
else updateEnviron(thehref,anchors[i],"MKT="+mkt);
}

//Google search
thehref=document.googleForm.action;
if (href.toLowerCase().indexOf("localhost")>0||href.toLowerCase().indexOf("testux")>0){document.googleForm.action=thehref.replace("www.coverme.com","localhost"); } 
else if(href.toLowerCase().indexOf("devsecure.lhplans.com")>0||href.toLowerCase().indexOf("www.dev.memberhealthplan.com")>0) {document.googleForm.action=thehref.replace("www.coverme.com","www.dev.memberhealthplan.com"); }
}

addLoadEvent(languageToggle);
addLoadEvent(environCheck);

