
/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}
function logout()
{
	deleteCookie("globalFirstName");

	this.location.href = "/global/includes/logout.html";
}


function GlobalIsString(sFormField,sFormName)
{
	if ((sFormField=="") || (sFormField==sFormName))
	{
		alert("Please enter clues in the Search field.");
		return false;
	}
}

function writeGlobalHeaderWelcomeMessage(firstname)
{
	if (firstname == null)
	{
		if ((typeof(telstra_global_tabId) != "undefined") && (telstra_global_tabId == 0))
		{	
			// Homepage is Tab 0.
			document.write('<h1 style="display:inline; margin-top:0px;"><img src="/global/images/txt_welcome.gif" alt="Welcome to Telstra"></h1>');
		}
	}
	else
	{
		document.write('Welcome');
		if (firstname != "#ANONYMOUS#")
		{
			document.write(' <a href="https://telstra.com/tcoma/registration.asp">' + firstname + '</a>');
		}
		if ((typeof(telstra_global_header_displaytabs) == "undefined") || (telstra_global_header_displaytabs == true)) 
		{
			// KAZ Tom Connolly - support for SSO
			// Need to modify the logout so that it is not displayed during 
			// the checkout screens but still displayed in the shop online screen.
			// Also allow a 'custom' logout routine that logs the user out of
			// Siteminder, as in the current telstra phase 0 static site.
			// And logs the user out of WCS application.
			var default_fn = "logout()";
			if (typeof(telstra_global_header_fn_logout) != "undefined") 
			{
				default_fn = telstra_global_header_fn_logout;
			}
			
			// display the logout link if the tabs are displayed (unable to logout when tabs not displayed)
			//document.write(' <span class="bluebar">|</span> <a id="logout" href="JavaScript:logout();">Logout</a>');
			document.write(' <span class="bluebar">|</span> <a id="logout" href="JavaScript:'+default_fn+';">Logout</a>');
		}
	}
}

// BEGIN HEADER WELCOME MESSAGE LOGIC
var globalFirstName = getCookie("globalFirstName");
if ((typeof(telstra_global_loginState) != "undefined") && (telstra_global_loginState == 1))
{
	// Check for page defined firstname
	if (typeof(telstra_global_header_firstname) != "undefined")
	{
		setCookie("globalFirstName", telstra_global_header_firstname);
		writeGlobalHeaderWelcomeMessage(telstra_global_header_firstname);
	}
	else 
	{
		if (globalFirstName == null)
		{
			setCookie("globalFirstName", "#ANONYMOUS#");
			globalFirstName = "#ANONYMOUS#";
		}
		writeGlobalHeaderWelcomeMessage(globalFirstName);
	}
}
else if ((typeof(telstra_global_loginState) != "undefined") && (telstra_global_loginState == -1))
{
	// Remove cookie if it exists
	if (globalFirstName != null)
	{
		deleteCookie("globalFirstName");
	}
	writeGlobalHeaderWelcomeMessage(null);
}
else
{
	// Check if cookie exists
	if (globalFirstName == null)
	{
		var telstra_global_loginState = -1;
	}
	else
	{
		var telstra_global_loginState = 1;
	}
	writeGlobalHeaderWelcomeMessage(globalFirstName);
}
if (typeof(telstra_global_header_displaycontactus) != "undefined" && (telstra_global_header_displaycontactus == false) ) {
	if (document.getElementById) {
		document.getElementById('headercontact').style.display = "none";
	}
}


startList = function() {
	var selectedID = "tabId";
	
	if (typeof(telstra_global_tabId) != "undefined")
	{
		selectedID += telstra_global_tabId;
	}

	if (document.getElementById) {
		navRoot = document.getElementById("topnav");

		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			
			if (node.nodeName=="LI") {
				if(node.id == selectedID){
					node.className = "selected";
					}
				else {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
				}
			}
		}
		
	}
}


if (typeof(telstra_global_tabId) != "undefined"){
	startList();}

if (typeof(telstra_global_header_maxWidth) != "undefined"  )
{
    document.getElementById("header").style.width  = telstra_global_header_maxWidth; 
}
else
{
	document.getElementById("header").style.width = "770px";
}	
