/**
 Title:pbar_hm.js
 * Description:js file to generate pbar on freescale.com homepage
 * Copyright:    Copyright (c) 2009
 * Company: LTI
 * @version 1.0
**/
/*Method to load menu */
<!--//--><![CDATA[//><!--
sfPBarHover = function() {
	var sfEls = document.getElementById("pbarNav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfpbarhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfpbarhover\\b"), "");
		}
	}
}
function getCookieLoginStatus() {
		var name='SessID';
		var start = document.cookie.indexOf(name+"=");
		var temp = document.cookie.substring(start);
		var indext = temp.indexOf("SessID=");
		if (indext == -1) {
			return false;
		}
		var val=temp.substring(indext+1);
		if (val.length > 0) {
			return true;
		}
		return false;
	}
if (window.attachEvent) window.attachEvent("onload",sfPBarHover);
//--><!]]>
function getCookie(name) { 
    var bikky = document.cookie;
    var index = bikky.indexOf(name + "=");
    if (index == -1) return null;
    index = bikky.indexOf("=", index) + 1; // first character
    var endstr = bikky.indexOf(";", index);
    if (endstr == -1) endstr = bikky.length; // last character
    return unescape(bikky.substring(index, endstr));
}
function generatePBar()
{
	var name =  getCookie('remember_me');
	
	if (name != null)
	{
        var decryptedName = decrypt(name);
        getBarForIdentifiedUser(decryptedName);
    } 
    else{
			getBarForAnonymousUser();
	}
}
function getBarForAnonymousUser()
{	
	var loginLink = null;
	if ( !getCookieLoginStatus() ) {
		loginLink = '<a class="login" href="https://www.freescale.com/ruhp/myFreescale.html"  onclick="call_pbar_metrics(\'pb_login\');">Login</a>';
	} else {
		loginLink = '<a  class="login" href="https://www.freescale.com/security/public/login.LogoutController.sp" onclick="call_pbar_metrics(\'pb_logout\');">Logout</a>';
	}
	document.write('<div id="pbar">\n');
	document.write('<ul id="pbarNav">\n');
	document.write('<li class="first">');
	document.write('<a class=\"wc\">Welcome Guest</a>');
	document.write('</li>\n');
	document.write('<li><a class=\"login\" href="https://www.freescale.com/webapp/crcl.ccr_register.framework"  onclick="call_pbar_metrics(\'pb_register\');">Register</a>');
	if(loginLink != null)
	{
		document.write('<a href="#" class="nostyle">&nbsp;or&nbsp;</a>');
		document.write(loginLink);
	}
	document.write('</li>\n');
	document.write('<li>');
	document.write('<a href="#" onclick="displayBlockForHistory();call_pbar_metrics(\'pb_browse_history\');" >');
	document.write(String("Browse History"));
	document.write('</a></li>\n');
	document.write('<li class="last"><a href="#" onclick="displayWhyReg(\'WHYREGISTER\');call_pbar_metrics(\'pb_whyregister\');">Why Should I Register?</a></li>\n');
	document.write('</ul>\n');
	document.write('</div>');
}

function getBarForIdentifiedUser(userName)
{
	var loginLink = null;
	if ( !getCookieLoginStatus() ) {
		loginLink = '<a class="login" href="https://www.freescale.com/security/public/login.LoginActionController.sp"  onclick="call_pbar_metrics(\'pb_login\');">Login</a>';
	} else {
		loginLink = '<a class="login" href="https://www.freescale.com/security/public/login.LogoutController.sp" onclick="call_pbar_metrics(\'pb_logout\');">Logout</a>';
	}
	document.write("<div id=\"pbar\">\n");
	document.write("<ul id=\"pbarNav\">\n");
	document.write("<li class=\"first\">");
	document.write("<a class=\"firstcolor\" href=\"#\">");
	document.write(userName+"'s Freescale");
	document.write("&nbsp;<img src=\"/shared/images/pbar_arrow1.gif\"/></a>");
	document.write("<ul class=\"persMenu\">\n");
	document.write("<li><a href=\"https://www.freescale.com/webapp/crcl.ccr_interest_in.framework?regFlag=fromProvideUpdateInterests&#step4\"  onclick=\"call_pbar_metrics(\'pb_my_profile\');\">My Profile</a></li>\n");
	document.write("<li><a href=\"https://www.freescale.com/ruhp/myFreescale.html\" onclick=\"call_pbar_metrics(\'pb_my_freescale\');\">My Freescale</a></li>\n");
	document.write("<li><a href=\"#\"  onclick=\"displayBlockForDownload();call_pbar_metrics('pb_recent_downloads');\">Recent Downloads</a></li>\n");
	document.write("<li><a href=\"#\" onclick=\"displayBlockForTraining();call_pbar_metrics('pb_my_training');\">My Training</a></li>\n");
	document.write("</ul>\n");
	document.write("</li>\n");
	if(loginLink != null)
	{	
		document.write("<li>\n");
		document.write(loginLink);
		document.write("</li>\n");
	}
	document.write("<li class=\"last\">");
	document.write("<a href=\"#\" onclick=\"displayBlockForHistory();call_pbar_metrics(\'pb_browse_history\');\">Browse History</a></li>\n");
	document.write("</ul>\n");
	document.write("</div>");
}

function decrypt(source) 
{
	 var letterForOutput = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ @#$^&*()-_=+.:";
	 var letterToInput = "zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA +=_-)(*&^$#@:.";
   	 var charofInputString; 
  	 var posOfSrcInInput; 
  	 var charFrmOutPut; 
  	 var output = ""; 
  	 var sourceLength = source.length; 
  	 var start = 0; 
  	 var end = 1; 
  	 while (start != sourceLength) { 
	    charofInputString = source.substring(start,end);
	    posOfSrcInInput = letterToInput.indexOf(charofInputString);
	    if(posOfSrcInInput == -1)
        {
         charFrmOutPut = charofInputString;
        }
        else
        {
            charFrmOutPut = letterForOutput.charAt(posOfSrcInInput);
	    }
        start++;
	    end++;
	    output += charFrmOutPut;
	 }
    return output;
}
function displayWhyReg(code){ 
		var url = "/webapp/shared/components/inc_collateral.jsp?code="+code;
		var window_title = "Why Should I Register?"
		var width = "700";
		var height = "350";
		ajaxwin=dhtmlwindow.open('ajaxbox',url,window_title, 'width='+width+'px,height='+height+'px,left=100px,top=100px,resize=0,scrolling=1');
		return ajaxwin ;
}
function call_pbar_metrics(action)
{
if (window.personalization_bar_metrics && action!= 'undefined' ){
		personalization_bar_metrics(action);
	}
}