/*
telus scripts
Last edit: 2009-06-26
*/

/* ########################################################### */

function GetCookie (name) {  
	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;  
	while (i < clen) {    
	var j = i + alen;    
	if (document.cookie.substring(i, j) == arg)      
		return getCookieVal (j);    
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i == 0) break;   
	}  
	return null;
}


function getCookieVal(offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function SetCookie (name, value) {
        var argv = SetCookie.arguments;
        var argc = SetCookie.arguments.length;
        var expires = (argc > 2) ? argv[2] : 365;  // always permanent
        var path = (argc > 3) ? argv[3] : '/';
//        var domain = (argc > 4) ? argv[4] : 'telus.com';
        
        var domain;
	if (argc > 4) {
	  domain = argv[4];
	}
	else {
          if (window.location.hostname.match(/telus.com/) ) {
            domain = 'telus.com';
          }  
          else {
            domain = window.location.hostname;
          }  
        }
        
        var secure = (argc > 5) ? argv[5] : false;

        // always permanent
        var today=new Date();
        var expTime=new Date();
        expTime.setTime(today.getTime()+3600000*24*expires*10)

        document.cookie = name + "=" + escape (value) +
        ((expires == null) ? "" : ("; expires=" + expTime.toGMTString())) +
        ((path == null) ? "" : ("; path=" + path)) +
        ((domain == null) ? "" : ("; domain=" + domain)) +
        ((secure == true) ? "; secure" : "");
}

function DeleteCookie (name) {  
	var exp = new Date();  
	exp.setTime (exp.getTime() - 1);  
 
	// This cookie is history  
	var cval = GetCookie (name);  
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function regionInit(){
// read existing cookie
var ck = GetCookie('telusPortalRegion') + '';
ck = ck.toUpperCase();
if(ck=='NULL'){ck='';}
var pg = bpDoc.region.toUpperCase();
// do values for page and cookie match?

if(ck == pg && !(ck=='' && pg=='')){
// ...match...
// do nothing
// ...no match....
} else {
    // is the page a 'national' page?
    if(pg == 'NATIONAL'){
        // is the cookie value blank? If yes, set default value
        if(ck==''){
            SetCookie('telusPortalRegion', 'NS');
            ck = 'NS';
        }
    } else {
        if(ck=='' && pg==''){
            SetCookie('telusPortalRegion', 'NS');
            ck = 'NS';
        } else if (ck=='AB' && pg=='BC'){
            // leave the AB cookie if this is a BC page
            ck = 'AB';
        } else {
        // otherwise make the cookie equal to whatever the page is
        ck = pg;
        SetCookie('telusPortalRegion', ck);
        }
    }    
}
//set hidden vars
if(document.getElementById('telusPortalRegion')){
    document.getElementById('telusPortalRegion').value = ck.toUpperCase();
}
// This should already be set
/*if(document.getElementById('telusPortalSegment')){
    document.getElementById('telusPortalSegment').value = bpDoc.segment;
}*/
// set the drop down nav to the value of 'ck'
if(document.getElementById('dropRegion')){
    var rd = document.getElementById('dropRegion');
    for (var i = 0; i < rd.options.length; i++){
        if(rd.options[i].innerHTML.toUpperCase() == ck.toUpperCase()){
            rd.options[i].selected = "selected";
        }
    }
}

SetCookie('CustomerType','business');

if (document.location.href.indexOf('fr_CA') > -1)
    SetCookie('lang','fr');
else
    SetCookie('lang','en');
    
}

function doRegionJump(frm,sel){
    // set region cookie
    var rgn = sel.options[sel.selectedIndex].innerHTML.toUpperCase();
    // Set region cookie
    SetCookie('telusPortalRegion', rgn);
    // Set segment cookie
    SetCookie('telusPortalSegment', bpDoc.segment);
	// jump to the selected URL
	//document.location = sel.options[sel.selectedIndex].value;
    frm.action = sel.options[sel.selectedIndex].value;
	frm.submit();
    //return false;
}

/* ########################################################### */
/* vars */
var isIe = false;
var isOp = false;
var isSaf = false;

/* variable to know if IE */
if ($.browser.msie) { isIe = true; }
/* variable to know if Opera */
if ($.browser.opera) { isOp = true; }
/* variable to know if Safari */
if ($.browser.safari) { isSaf = true; }

/* ########################################################### */
/* sousmenu */
var sousmenu = {
	load : function() { // add action on submenu links
		$("#header .item-sm").hover(sousmenu.over, sousmenu.out);
		$("#header .sm .ls-sm li").click(sousmenu.smLink);
	},
	over : function() { // add class on hover on submenu links
		$(this).addClass("open");
	},
	out : function() { // remove class on hover on submenu links
		$("#header .item-sm").removeClass("open");
	},
	smLink : function () {
		leLink = $(this).children("h4").children("a").get("0");
		window.location = $(leLink).attr("href");
	}
};

/* ########################################################### */
/* ready fonction */

$(document).ready(function () {
	sousmenu.load()
    regionInit();
});

