// 2009/03/20 for BWS

str = location.href;

if (((str.indexOf("canadapost.ca")) >= 0) || ((str.indexOf("postescanada.ca")) >= 0) || ((str.indexOf("ssl.postescanada-canadapost.ca")) >= 0))
{
	BWSDomain = str;
}
else
{
	BWSDomain = "http://10.131.237.44:11211";	   
}

BWSDomain = ""


// Pre-Load Images
function preload(name)
{
  var t = new Image()
  t.src = name;
}

// Mouse Over Routine

function mover(name, dir, optname1, optname2, optname3, optname4)
{
  var imgnames = new Array();
  var cnt = 0;
  var idir = "";

  //crappy code... i don't know how to use the args stuff and don't have time to figure it out.
  //what it does is add functionality to have multiple images react to a mouse-over.
  if(document.images[name])
    imgnames[cnt++] = name;
  if(document.images[optname1])
    imgnames[cnt++] = optname1;
  if(document.images[optname2])
    imgnames[cnt++] = optname2;
  if(document.images[optname3])
    imgnames[cnt++] = optname3;
  if(document.images[optname4])
    imgnames[cnt++] = optname4;

  for(i = 0; i < cnt; i++)
  {
    idir = dir;
    
    if(document.images[imgnames[i]])
    {
      if(document.images[imgnames[i]].src.search(/jpg/) == -1)
        extension = ".gif";
      else
        extension = ".jpg";
 
      if( idir != "" ) idir = idir + "/";
    
      if ( idir == "/dec/images/rightnav/" )//added for dec right nav rollovers, sept 8, 2003 ks
        document.images[imgnames[i]].src = idir + "_" + imgnames[i] + extension;
      else if (idir == "/personal/spring/images/rightnav/" )//added for spring right nav rollovers, 2004 ks
        document.images[imgnames[i]].src = idir + "_" + imgnames[i] + extension;
			else if (idir == "/personal/petstamp/images/rightnav/" )//added for petstamp right nav rollovers, aug 17, 2004 ks
        document.images[imgnames[i]].src = idir + "_" + imgnames[i] + extension;
	    else if ( idir != "images/" )
	      document.images[imgnames[i]].src = "/library/images/" + idir + "_" + imgnames[i] + extension;
	    else
	      document.images[imgnames[i]].src = idir + "_" + imgnames[i] + extension;
    }
  }
}

// Mouse Out Routine
function mout(name, dir, optname1, optname2, optname3, optname4)
{
  var extension;
  var imgnames = new Array();
  var cnt = 0;

  //crappy code... i don't know how to use the args stuff and don't have time to figure it out.
  //what it does is add functionality to have multiple images react to a mouse-over.
  if(document.images[name])
    imgnames[cnt++] = name;
  if(document.images[optname1])
    imgnames[cnt++] = optname1;
  if(document.images[optname2])
    imgnames[cnt++] = optname2;
  if(document.images[optname3])
    imgnames[cnt++] = optname3;
  if(document.images[optname4])
    imgnames[cnt++] = optname4;

  for(i = 0; i < cnt; i++)
  {	
    idir = dir;
    if(document.images[imgnames[i]])
    {
      if ( document.images[name].src.search(/jpg/) == -1 )
        extension = ".gif";
      else
        extension = ".jpg";
	
      if ( idir != "" ) idir = idir + "/";
      
      if ( idir == "/dec/images/rightnav/" ) //added for dec right nav rollovers, sept 8, 2003 ks
	    document.images[imgnames[i]].src = idir + imgnames[i] + extension;
      else if (idir == "/personal/spring/images/rightnav/" )//added for spring right nav rollovers, 2004 ks
        document.images[imgnames[i]].src = idir + imgnames[i] + extension;
			else if (idir == "/personal/petstamp/images/rightnav/" )//added for petstamp right nav rollovers, aug 17, 2004 ks
        document.images[imgnames[i]].src = idir + imgnames[i] + extension;
      else if ( idir != "images/" )
        document.images[imgnames[i]].src = "/library/images/" + idir + imgnames[i] + extension;
      else
        document.images[imgnames[i]].src = idir + imgnames[i] + extension;
	}
  }
}

// Mouse Over Routine for dynamic buckets
function bucketmover(filename, dir, imgsrcname)
{
	var extension;

	if(document.images[imgsrcname])
	{
		if(document.images[imgsrcname].src.search(/jpg/) == -1)
			extension = ".gif";
		else
			extension = ".jpg";
 
		document.images[imgsrcname].src = dir + "_" + filename + extension;
	}
}

// Mouse Out Routine for dynamic buckets
function bucketmout(filename, dir, imgsrcname)
{
	var extension;

	if(document.images[imgsrcname])
	{
		if(document.images[imgsrcname].src.search(/jpg/) == -1)
			extension = ".gif";
		else
			extension = ".jpg";

		document.images[imgsrcname].src = dir + filename + extension;
	}
}

// File Download Function
function download(filename)
{
  //alert("File Would Be Downloaded");
  location = filename;
}

// Mouseover handler for left nav
function nav(segment,name,prefix,language)
{
  if(document.images[name])
  {
    document.images[name].src='/library/images/leftnav/'+segment+'/'+prefix+name+'-'+language+'.gif';
  }
}

// use: getCookie("name");
function getCookie(name) 
{
	var index = document.cookie.indexOf(name + "=");
	if (index == -1) return null;

	index = document.cookie.indexOf("=", index) + 1;

	var endstr = document.cookie.indexOf(";", index);
	if (endstr == -1) endstr = document.cookie.length;

	return unescape(document.cookie.substring(index, endstr));
}

//2008/09/12 - ks - function to detect/assign the root domain, called in setCookie
function detectDomain(valuetodetect){
  var thisdomain = "";
  if (valuetodetect != null && valuetodetect != "" ){
    //alert ('passed currentdomain to parse is ' + valuetodetect);
    var _regex = /^([a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]\.)*([a-zA-Z0-9][a-zA-Z0-9\-]+[a-zA-Z0-9]\.[a-zA-Z]{2,6})$/;
    if (valuetodetect.match(_regex)){
      //alert('regex matches');
      thisdomain = RegExp.$2;
    }
  }
  //alert ('in detectDomain : ' + thisdomain);
  return thisdomain;
}

//Toggle setCookie from Legacy js. This sets a persistent cookie
//update the /library/js/common.js version as well
//ks - 2008/05/15 - updated 2008/07/29
//updated 2008/09/02 
//updated 2008/09/12 - assign root domain
function setCookie(name, value)
{
	//alert ("in setCookie and cookie name to be set is :" + name + " and cookie value is : " + value);
	if ( value != null && value != "" ){
	  //assign root domain
    var currentdomain = document.domain;
	  //alert ('before pass to detectDomain - currentdomain is ' + currentdomain);
	  var detecteddomain = detectDomain(currentdomain);
	  //alert ('after detecteddomain : ' + detecteddomain);
	  if (detecteddomain != '' ){
	    assigndomain = detecteddomain;
	  }
	  else{
	    assigndomain = currentdomain;
	  }
	  //alert('assigndomain : ' + assigndomain);
	  
	  var today = new Date();
	  var expiry = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000); // plus 28 days
	  
	  document.cookie=name + "=" + escape(value) + "; expires=" + expiry.toGMTString() + "; path=/; domain=" + assigndomain + ";";
	  
	  //cookies = document.cookie;
    
    /* 2008/09/05temp removal of cookie check to resolve bug 801, re-adding confirmCookie for LANG to handle toggle for domains
	  //added 2008/09/02 - confirm persistent cookie set
	  var cookienameset = "";

	  cookienameset =  getCookie(name);
		//alert ("cookienameset = " + cookienameset);
		
	  if ( cookienameset != null && cookienameset != ''){
	    //alert ("persistent cookienameset is not null/undefined/empty, so persistent cookie was set");
	    //do nothing
	  }
	  else{
	    //alert ("persistent cookienameset is " +  cookienameset + " , so call setCookieNonPersistent(name,value) where name = " + name + " and value = " + value);
	    setCookieNonPersistent(name,value);
	  } */
	}	
	
}

//added 2008/09/02 - ks - to set a Non-Persistent Cookie
function setCookieNonPersistent(name, value)
{
	
	//alert ("in setCookieNonPersistent where name = " + name + " and value is " + value);
	if ( value != null && value != "" ){
	  //assign root domain
    var currentdomain = document.domain;
	  //alert ('currentdomain is ' + currentdomain);
	  var detecteddomain = detectDomain(currentdomain);
	  //alert ('detecteddomain : ' + detecteddomain);
	  if (detecteddomain != '' ){
	    assigndomain = detecteddomain;
	  }
	  else{
	    assigndomain = currentdomain;
	  }
	 
	  //document.cookie=name + "=" + escape(value)  + "; path=/;";
	  document.cookie=name + "=" + escape(value)  + "; path=/; domain=" + assigndomain + ";";

	  //added 2008/09/02 - confirm non-persistent cookie set 
	  var cookienameset = "";
	  cookienameset =  getCookie(name);

	  if ( cookienameset != null && cookienameset != ''){
	    //alert ("non persistent cookienameset is not null/undefined/empty, so nonpersistent cookie was set");
	    //do nothing
	    }
	  else{
	    //popup('800x600','/Personal/Help/WebTechnology.aspx#Cookies');

	    //window.open ("http://www.canadapost.ca/Personal/Help/WebTechnology.aspx#Cookies","cpc","location=1,status=1,scrollbars=1,resizable=yes,left=100,top=150,width=800,height=600");
	    //setTimeOut(window.open ("http://www.canadapost.ca/Personal/Help/WebTechnology.aspx#Cookies","cpc","location=1,status=1,scrollbars=1,resizable=yes,left=100,top=150,width=800,height=600"), 3000);	  
	    self.setTimeout('winOpen()', 2000);
	  }
	}
	
}

function winOpen() {
     window.open ("http://www.canadapost.ca/Personal/Help/WebTechnology.aspx#Cookies","cpc","location=1,status=1,scrollbars=1,resizable=yes,left=100,top=150,width=800,height=600");
}


//confirm cookies on page load
function confirmCookie()
{
	var cookieValue = "";

	//check for style
	//first, check for querystring value
	qsParm['style'] = null;
	qsParm['lang'] = null;
	qsParm['language'] = null;
	checkQS();
	
	//style
	if (qsParm['style'] != null && qsParm['style'] != ''){
		//alert ("qsParm['style'] is not null: " + qsParm['style']);
		setCookie('style',qsParm['style']);		
	}
	else{
		cookieValue =  getCookie("style");
		if ( cookieValue != null && cookieValue != ''){
			//alert ("cookieValue is not null: " + cookieValue);
			setCookie('style',cookieValue);
		}
		else{
			//alert ("default standard");
			setCookie('style','0');			
		}
	}

//updated 2008/09/05 to handle toggle to new domain
	//LANG
	var currentURL = document.URL;
	
	var newLang = "e";

	//first, determine to Querystring LANG=value
	//alert("1 qsParm['lang'] : " + qsParm['lang']);
	//alert("2 qsParm['language'] : " + qsParm['language']);
	if (qsParm['lang'] != null && qsParm['lang'] != '')
	{
		//alert ("qsParm['lang'] is not null: " + qsParm['lang']);
		switch (qsParm['lang'])
		{
			case "fr":
				newLang = 'f';
				break;
			case "french":
				newLang = 'f';
				break;
			case "en":
				newLang = 'e';
				break;
			case "english":
				newLang = 'e';
				break;
			default :
				newLang = 'e';
				break;			
		}	//end switch

		setCookie('LANG',newLang);

	}
	else if (qsParm['language']!= null && qsParm['language'] != '')
	{
		//alert ("qsParm['lang'] is not null: " + qsParm['lang']);
		switch (qsParm['language'])
		{
			case "fr":
				newLang = 'f';
				break;
			case "french":
				newLang = 'f';
				break;
			case "en":
				newLang = 'e';
				break;
			case "english":
				newLang = 'e';
				break;
			default :
				newLang = 'e';
				break;			
		}	//end switch
	
		setCookie('LANG',newLang);		
	}
	//else, detect if a LANG cookie is set, and then compare to current page language before setCookie
	else{
		cookieValue =  getCookie("LANG");
		//alert("cookieValue : " + cookieValue);
		if ( cookieValue != null && cookieValue != ''){
			//alert ("cookieValue is not null: " + cookieValue);
			//compare with current url language
			//currentURL.value.search(/^[\w]+[\-e]{1}+[a-zA-Z]+$/) != -1 ;
			//if (currentURL.substring(currentURL.IndexOf("-e.")) != -1)
			if (document.URL.match('-e.'))
			{
				pageLang = "e";
				//alert ("in -e : " + document.URL.match('-e.'));
				if (cookieValue != pageLang)
				{
					setCookie('LANG',pageLang);
				}
			}
			else if (document.URL.match('-f.'))
			{
				pageLang = "f";
				//alert ("in -f : " + document.URL.match('-f.'));
				if (cookieValue != pageLang)
				{
					setCookie('LANG',pageLang);
				}
			}
		}/*
		else{
			alert ("final else" );
			//default English
			//setCookie('LANG','e');
			//do nothing, browser language is set by default, no LANG cookie is set			
		}*/
	}	
}

//check Querystring
var qsParm = new Array();
function checkQS() {
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			var key = parms[i].substring(0,pos).toLowerCase();;
			var val = parms[i].substring(pos+1).toLowerCase();;
			qsParm[key] = val;
			//alert ("in checkQS - key : " + key + " val : " + val);
		}
	}
} 
//Debugging Function
function inspect( obj )
{
  if ( ! obj )
  {
    ret = prompt ("Enter object", "document");
    obj = eval(ret);
  }

  var temp = "";
  for (x in obj)
  {
    temp += x + ": " + obj[x] + "\n";
    if ( temp.length > 700 )
    {
      alert(temp);temp='';
    }
  }
  alert (temp);
}

/*  ---------- Flash Detect Usage------------

if ( IsFlashDetect() == true )
{
  document.write("FLASH CODE");  
}
else
{
  document.write("IMG CODE");
}

--------------------------------------------- */

// *************
// everything below this point is internal until after the body tag
// do not modify! 
// *************

// system globals
var requiredVersion = 4;
var flash2Installed = false;		// boolean. true if flash 2 is installed
var flash3Installed = false;		// boolean. true if flash 3 is installed
var flash4Installed = false;		// boolean. true if flash 4 is installed
var flash5Installed = false;		// boolean. true if flash 5 is installed
var flash6Installed = false;		// boolean. true if flash 6 is installed
var maxVersion = 6;					// highest version we can actually detect
var actualVersion = 0;				// version the user really has
var hasRightVersion = false;		// boolean. true if it's safe to embed the flash movie in the page
var jsVersion = 1.0;				// the version of javascript supported

// check the browser...we're looking for ie/win
var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;		// true if we're on ie
var isWin = (navigator.appVersion.indexOf("Windows") != -1) ? true : false; // true if we're on windows

// this is a js1.1 code block, so make note that js1.1 is supported.
jsVersion = 1.1;

// write vbscript detection if we're not on mac.
if(isIE && isWin){ // don't write vbscript tags on anything but ie win
	document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n');
	document.write('on error resume next \n');
	document.write('flash2Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.2"))) \n');
	document.write('flash3Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.3"))) \n');
	document.write('flash4Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.4"))) \n');
	document.write('flash5Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.5"))) \n');	
	document.write('flash6Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.6"))) \n');	
	document.write('</SCR' + 'IPT\> \n'); // break up end tag so it doesn't end our script
}

// check the browser...we're looking for ie/win
var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;		// true if we're on ie
var isWin = (navigator.appVersion.indexOf("Windows") != -1) ? true : false; // true if we're on windows

// this is a js1.1 code block, so make note that js1.1 is supported.
jsVersion = 1.1;

/* removed Nov 24, 2003, and placed into /library/js/shockwave_detect.js - ks
// write vbscript detection if we're not on mac.
if(isIE && isWin){ // don't write vbscript tags on anything but ie win
	document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n');
	document.write('on error resume next \n');
	document.write('flash2Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.2"))) \n');
	document.write('flash3Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.3"))) \n');
	document.write('flash4Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.4"))) \n');
	document.write('flash5Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.5"))) \n');	
	document.write('flash6Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.6"))) \n');	
	document.write('</SCR' + 'IPT\> \n'); // break up end tag so it doesn't end our script
}
*/

// Checks the version of flash the user has, and returns it.  Returns 0 if
// there is no flash installed.
function FlashDetect(){	

	if (navigator.plugins){								// does navigator.plugins exist?
		if (navigator.plugins["Shockwave Flash 2.0"] 	// yes>> then is Flash 2 
		|| navigator.plugins["Shockwave Flash"]){		// or flash 3+ installed?

			// set convenient references to flash 2 and the plugin description
			var isVersion2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
			var flashDescription = navigator.plugins["Shockwave Flash" + isVersion2].description;
			// a flash plugin-description looks like this: Shockwave Flash 4.0 r5
			// so we can get the major version by grabbing the character before the period
			// note that we don't bother with minor version detection. do that in your movie with $version
			var flashVersion = parseInt(flashDescription.charAt(flashDescription.indexOf(".") - 1));

			// we know the version, now set appropriate version flags
			flash2Installed = flashVersion == 2;		
			flash3Installed = flashVersion == 3;
			flash4Installed = flashVersion == 4;
			flash5Installed = flashVersion == 5;
			flash6Installed = flashVersion == 6;
		}
	}
	
	// loop through all versions we're checking, and set actualVersion to highest detected version
	for (var i = 2; i <= maxVersion; i++) {	
		if (eval("flash" + i + "Installed") == true) actualVersion = i;
	}
	// if we're on webtv, the version supported is 2 (pre-summer2000, or 3, post-summer2000)
	// note that we don't bother sniffing varieties of webtv. you could if you were sadistic...
	if(navigator.userAgent.indexOf("WebTV") != -1) actualVersion = 2;	
	
	// uncomment next line to display flash version during testing
	// alert("version detected: " + actualVersion);


	// we're finished getting the version. time to take the appropriate action

	return actualVersion;
}

// Checks to ensure the user has a flash version greater than the required version.
function IsFlashDetect()
{
	if (FlashDetect() >= requiredVersion) return true;
  return false;
}
