// globallinks.js
// This js contains the functions for creating html links
// author Rajib Das < rajirdas@in.ibm.com >
//
// Note: Pls take extreme care while modyfying 
//
	//create the links in the header 
	//get the host
	var TempHost=location.host;

	// variable to hold the langId either from Url or from webSiteLang cookie
	var JPLang = "";
	// boolean to check if the langId is present in the Url
	var isLangIdInUrl = false;

	//var hasLoginCookie = document.cookie.indexOf("UserID=Yes");
	//var hasknownCookie = document.cookie.indexOf("AnonymousClientId=N");
	var hasLoginCookie = -1 ;
	var hasknownCookie = -1 ;
	
	var hasLoginCookieValue;
	var hasknownCookieValue;
	//WebLang~-1
	var websiteLang;
	

	var hasClientIDCookie = document.cookie.indexOf("ClientId=");
	var hasNameCookie = document.cookie.indexOf("WelcomeName=");
	
	var sialSiteDefIndex = document.cookie.indexOf("SialSiteDef=");
	//alert(sialSiteDefIndex);
	if(sialSiteDefIndex != -1){
		sialSiteDefIndex = sialSiteDefIndex + 12;
		var sialSiteDefEndIndex = document.cookie.indexOf(";",sialSiteDefIndex);
		if(sialSiteDefEndIndex == -1){
			sialSiteDefEndIndex = document.cookie.length;
		}
		var sialSiteDefValue = document.cookie.substring(sialSiteDefIndex, sialSiteDefEndIndex);
		
		var arr = sialSiteDefValue.split("|");
		for(var i=0 ; i< arr.length ; i++){
			var tempV = arr[i];
			if(tempV.indexOf("AnonymousClientId") > -1){
				var acltIDarr = tempV.split("~");
				if("N" == acltIDarr[1]){
					hasknownCookie = 10 ;
					hasknownCookieValue = tempV.substring(18);
				}
			}
			else if(tempV.indexOf("UserID") > -1){
				var aUserIdArr = tempV.split("~");
				if("Yes" == aUserIdArr[1]){
					hasLoginCookie = 10;
					hasLoginCookieValue = tempV.substring(7);
				}
			}
			else if(tempV.indexOf("WebLang") > -1){
				var aWebLangIdArr = tempV.split("~");
				websiteLang = aWebLangIdArr[1];
			}
		}
	}

	//get the window URL
	var winURL = window.location.href;
	//check if the LangId is present in the URL
	if(winURL.indexOf("langId=") > -1){
		isLangIdInUrl = true;
	}

	//langId from URL gets the preference over webLangId cookie
	if(isLangIdInUrl){
		if(winURL.indexOf("langId=-10") > -1){
			JPLang = "-10" ;
		}
	}
	else{
		if(websiteLang == -10){
			JPLang = "-10" ;
		}
	}

	//CD# 9001071
	// For Mozilla this IF block will be executed.
	if (hasNameCookie != -1){
		var start = hasNameCookie + 12;		             // Start of cookie value
		var end = document.cookie.indexOf(";", start);            // End of cookie value
		if (end == -1) end = document.cookie.length;
		var WelcomeName = document.cookie.substring(start, end);  //Extract the value.
		if(WelcomeName == ""){
			hasNameCookie = -1;
		}
	}

	
	
	var dividerHTML = "<td width=\"1\" valign=\"bottom\"><img src=\"/images/g_divider_topnav.gif\" width=\"1\" height=\"10\" alt=\"\"></td>";
	
	if (hasNameCookie != -1)
	{
		var start = hasNameCookie + 12;		             // Start of cookie value
		var end = document.cookie.indexOf(";", start);   // End of cookie value
		if (end == -1) end = document.cookie.length;
		var WelcomeName = document.cookie.substring(start, end);  //Extract the value.
		WelcomeName = decodeURI(WelcomeName);
		WelcomeName = replaceString(WelcomeName, "+").toUpperCase();
		//change the WelcomeName disaply pattern when langId = Japanese. For Japan langId = -10
		//CD 9001199
		if(JPLang == "-10"){
			var tempWelComeNameArr = WelcomeName.split(" ");
			if(tempWelComeNameArr.length == 2){
				var fName = tempWelComeNameArr[0];
				var lName = tempWelComeNameArr[1];
				WelcomeName = lName + " " + fName ;
			}
			else{
				var fName ;
				var lName = "" ;
				var flag = true ;
				for(var i=0 ; i< tempWelComeNameArr.length ;i++){
					if(i == 0){
						fName = tempWelComeNameArr[i];
					}
					else{
						if(flag){
							lName = lName + tempWelComeNameArr[i];
							flag = false;
						}
						else{
							lName = lName + " " + tempWelComeNameArr[i];
						}
					}
				}
				WelcomeName = lName + " " + fName ;
			}
		}

		if (hasLoginCookie == -1)
		{
			//Not Logged in - Known Users (cookie detected)
			document.write("<td>Welcome <b>"+ WelcomeName + "</b></td>");
			document.write(dividerHTML);
			document.write("<td><a href=\"https://"+TempHost + buildWcsUrl("LiteLogoff", "11001")+ "\"  title=\"Not You?\">Not You?</a></td>");
			document.write(dividerHTML);
			document.write("<td><a href=\"https://"+TempHost + buildWcsUrl("LogonForm","11001" )+  "\"  title=\"Login\">Login</a></td>");
			document.write(dividerHTML);
			document.write("<td><a href=\"https://"+TempHost+ buildWcsUrl("UserProfileNavigation","11001")+ "\"  title=\"My Profile\">My Profile</a></td>");
			document.write(dividerHTML);
			document.write("<td><a href=\"https://"+TempHost+ buildWcsUrl("FavoriteListDisplayView" ,"11001" ) + "\"  title=\"Favorite Products\">Favorite Products</a></td>");
		}
		else
		{
			//Logged in - Authenticated Users
			document.write("<td>Welcome <b>"+ WelcomeName + "</b></td>");
			document.write(dividerHTML);
			document.write("<td><a href=\"https://"+TempHost+ buildWcsUrl("Logoff", "11001")+ "\" title=\"Logout\">Logout</a></td>");
			document.write(dividerHTML);
			document.write("<td><a href=\"https://"+TempHost+ buildWcsUrl("UserProfileNavigation", "11001")+ "\"  title=\"My Profile\">My Profile</a></td>");
			document.write(dividerHTML);
			document.write("<td><a href=\"https://"+TempHost+ buildWcsUrl("FavoriteListDisplayView" ,"11001" ) + "\"  title=\"Favorite Products\">Favorite Products</a></td>");
		}
	}
	else if (hasLoginCookie != -1)
	{
		//Logged in - Authenticated Users but there in no nameCookie
		document.write("<td>Welcome</td>");
		document.write(dividerHTML);
		document.write("<td><a href=\"https://"+TempHost + buildWcsUrl("Logoff", "11001")+ "\"  title=\"Logout\">Logout</a></td>");
		document.write(dividerHTML);
		document.write("<td><a href=\"https://"+TempHost + buildWcsUrl("UserProfileNavigation", "11001")+ "\"  title=\"My Profile\">My Profile</a></td>");
		document.write(dividerHTML);
		document.write("<td><a href=\"https://"+TempHost + buildWcsUrl("FavoriteListDisplayView" ,"11001" ) + "\"  title=\"Favorite Products\">Favorite Products</a></td>");
	}
	else
	{
		//Not Logged in - Unknown Users
		document.write("<td><a href=\"https://"+TempHost+ buildWcsUrl("LogonForm" , "11001" ) + "\" title=\"Login\">Login</a></td>");
		document.write(dividerHTML);
		document.write("<td><a href=\"https://"+TempHost + buildWcsUrl("UserRegNavigation" , "11001" )+ "\" title=\"Register\">Register</a></td>");
		document.write(dividerHTML);
		document.write("<td><a href=\"https://"+TempHost + buildWcsUrl("UserProfileNavigation", "11001")+ "\" title=\"My Profile\">My Profile</a></td>");
		if (hasknownCookie != -1)
		{
			document.write(dividerHTML);
			document.write("<td><a href=\"https://"+TempHost + buildWcsUrl("FavoriteListDisplayView" ,"11001" ) + "\" title=\"Favorite Products\">Favorite Products</a></td>");
		}
	}
