/*
 *
 * AOL HELP Hat/Header Module
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// _is_qa_ is for QA use, change the value to 1 means, it is for QA SNS.
var _is_qa_ = 0;

/*
 * Configurable JS variables for DEV
 * 
 * JS variables:
 * 
 * 1. SP_GLOBAL_ACTIVE
 * 2. SP_FEEDBACK_BRAND
 * 3. SP_ALSO_LIKE_FIRST_URL
 * 4. SP_ALSO_LIKE_FIRST_TEXT
 * 5. sitedomain
 * 6. siteState
 * 
 * 
 * Explain:
 * 
 * 1. SP_GLOBAL_ACTIVE
    specify the active global nav items. Possible values:
	main   		(Help Main) 				
	az     		(Help A - Z)
	myaccount 	(Account & Settings)
	contact     (Contact Us)
	none		(none of itme got activated)
	
	Example:
	var SP_GLOBAL_ACTIVE = "myaccount";
	it means, the active item is "account & settings"
		Default is: main
	
 *
 *	
 *  2. SP_FEEDBACK_BRAND
 *  if you don't set this value, then it defaults to help site. The value is "aolhelp".
 * 
 *
 * 
 * 3. SP_ALSO_LIKE_FIRST_URL
 * 4. SP_ALSO_LIKE_FIRST_TEXT
 * 
 * the first "also like" link in HAT, default is "My Account" and link is https://bill.aol.com
 * 
 * 
 * 
 * 
 * 
 * 5. sitedomain
 * 6. siteState
 * 
 * in help application, these two values are already in all existing jsp files.
 * all jsp files must have these two values set.
 * 
 * 
 * 
 */


/* *******************************************************
 * 
 * 
 * below is JS code section
 * 
 * 
 *********************************************************/

if(window.location.href.toLowerCase().indexOf("https://")>=0)
    _sns_use_ssl_=1;
else
    _sns_use_ssl_=0;

var _screenname_host_name_ = "my.screenname.aol.com";
if(_is_qa_ != 0)
	_screenname_host_name_ = "my.screenname.qa.aol.com";

var _snshosturl="https://" + _screenname_host_name_;  //my.screenname.aol.com"; 

var _stdloginurl=_snshosturl+"/_cqr/login/login.psp";
var _logouturl=_snshosturl+"/_cqr/logout/mcLogout.psp"
var _anchorimgurl=_snshosturl+"/images/dot.gif";

if(typeof sitedomain=='undefined')
    var sitedomain="help.aol.com"; 

var _signinlinkURL_, _signoutlinkURL_; 

if(typeof siteState=='undefined')
    var siteState="OrigUrl%3dhttp%3a%2f%2fhelp%2eaol%2ecom"; 

_signinlinkURL_ = _stdloginurl + '?sitedomain='+sitedomain+'&siteState='+siteState+'&lang=en&seamless=y&authLev=1&offerId=aol-helpv1-en-us';
_signoutlinkURL_ = _logouturl+'?uitype=mini&sitedomain='+sitedomain+'&authLev=1&seamless=y&lang=en&locale=us&siteState='+siteState+'&_sns_fg_color_=333333&_sns_bg_color_=F6F6F6';

	
var _currDate_=new Date;  
var _aaCookie_ = getCk("SNS_AA"); 
var _sns_sst_ = -1, aanmvalpos=0; 
if(_aaCookie_) {
	var _asrc_ = getValue(_aaCookie_, "asrc"); 
    if( _asrc_ == "1" || _asrc_ == "2" ) { 
        _sns_sst_ = _currDate_.getTime()/1000;
    } else { 
    	_sns_sst_=getValue(_aaCookie_, "sst"); 
    } 
} 

var _SIGN_URL_ = ""; //"https://my.screenname.qa.aol.com/_cqr/logout/mcLogout.psp?uitype=mini&sitedomain=bill.aol.com&authLev=1&seamless=y&lang=en&locale=us&siteState=OrigUrl%3dbill.aol.com&_sns_fg_color_=333333&_sns_bg_color_=F6F6F6";
var _SIGN_TEXT_ = ""; //"Sign Out";
var _SIGN_TITLE_ = "";
if (_sns_sst_ != -1 && (_currDate_.getTime()/1000) - _sns_sst_ < 86400 )  {
	_SIGN_URL_ = _signoutlinkURL_;
	_SIGN_TEXT_ = "Sign Out"; 
	 var loginId=""; 
	 var rsp_cookie=getCk("RSP_COOKIE"); 
	 if(rsp_cookie)
	     loginId=base64Decode(getValue(rsp_cookie, "name")); 
	 if(loginId != "")
		 _SIGN_TITLE_ ='Sign Out '+loginId;
} else {
	_SIGN_URL_ = _signinlinkURL_;
	_SIGN_TEXT_ = "Sign In"; 
}

var _CDN_SERVER_ = "http://o.aolcdn.com/bill.aol.com";
if(_is_qa_ )
	_CDN_SERVER_ = "http://o.aolcdn.com/billqa.aol.com";
if(_sns_use_ssl_ == 1) {
	_CDN_SERVER_ = "https://s.aolcdn.com/bill.aol.com";
	if(_is_qa_ )
		_CDN_SERVER_ = "https://s.aolcdn.com/billqa.aol.com";
}

//temp value
//_CDN_SERVER_ = "..";


 var _ALSO_LIKE_FIRST_TEXT_ = "My Account";
 var _ALSO_LIKE_FIRST_URL_ = "https://bill.aol.com/";
 var _FEEDBACK_URL_ = "http://feedback.aol.com/rs/rs.php?sid=aolhelp";
 var _FEEDBACK_BRAND_ = "aolhelp";
 if(typeof SP_ALSO_LIKE_FIRST_TEXT != 'undefined')
	 _ALSO_LIKE_FIRST_TEXT_ =  SP_ALSO_LIKE_FIRST_TEXT;
 if(typeof SP_ALSO_LIKE_FIRST_URL != 'undefined')
	 _ALSO_LIKE_FIRST_URL_ = SP_ALSO_LIKE_FIRST_URL;
 if(typeof SP_FEEDBACK_BRAND != 'undefined') {
	 _FEEDBACK_URL_ = "http://feedback.aol.com/rs/rs.php?sid="+SP_FEEDBACK_BRAND;
	 _FEEDBACK_BRAND_ = SP_FEEDBACK_BRAND;
 }
 
 
 var _HELP_HOST_URL_ = "http://help.aol.com";
 var _CONTACT_US_SIGN_URL_ = _stdloginurl + '?sitedomain=bill.aol.com&authLev=2&lang=en&locale=us&offerId=aol-helpv2-en-us&siteState=OrigUrl%3Dhttps%253A%252F%252Fbill.aol.com%252FSPortal%252Fjsp%252Fcontact_us.jsp';
 if(_is_qa_) {
	 _HELP_HOST_URL_ = "http://help.qat.aol.com"; 
	 _CONTACT_US_SIGN_URL_ = _stdloginurl + '?sitedomain=bill.qa.aol.com&authLev=2&lang=en&locale=us&offerId=aol-helpv2-en-us&siteState=OrigUrl%3Dhttps%253A%252F%252Fbill.qa.aol.com%252FSPortal%252Fjsp%252Fcontact_us.jsp';
 }
 
 /** 
	Set Active State for the main navigation:

	Possible SP_GLOBAL_ACTIVE values
	main   		(Help Main) 				
	az     		(Help A - Z)
	myaccount 	(Account & Settings)
	contact     (Contact Us)
	
	Example:
	var SP_GLOBAL_ACTIVE = "myaccount";
	it means, the active item is "account & settings"
		
*/
 
var _HELP_MAIN_ = "";
var _HELP_AZ_ = "";
var _HELP_ACCOUNT_ = "";
var _HELP_CONTACT_ = "";
if(typeof SP_GLOBAL_ACTIVE == 'undefined')
	_HELP_MAIN_ = "active";
else {
	if(SP_GLOBAL_ACTIVE.toLowerCase() == "az")
		_HELP_AZ_ = "active";
	else if(SP_GLOBAL_ACTIVE.toLowerCase() == "myaccount")
		_HELP_ACCOUNT_ = "active";
	else if(SP_GLOBAL_ACTIVE.toLowerCase() == "contact")
		_HELP_CONTACT_ = "active";
	else if(SP_GLOBAL_ACTIVE.toLowerCase() == "none")
		;
	else
		_HELP_MAIN_ = "active";
}

var _HELP_AOL_CLIENT_START_ = "";
var _HELP_AOL_CLIENT_END_ = "";
if(navigator.userAgent.toLowerCase().indexOf("aol")>=0) {
	_HELP_AOL_CLIENT_START_ = '<!--';
	_HELP_AOL_CLIENT_END_ = '-->';
}
 // below html code is from MATT
 // HTML for the hat/header module
 var header_html=""; 
 if(typeof SP_FROMWHERE_VALUE == 'undefined' || SP_FROMWHERE_VALUE.toLowerCase()!="pc__") {
	 header_html = '	<!-- Hat/Header HTML --> '
		+'	<div id="hat">'
		+'		<ul class="links">'
		+'			<li class="first"><a href="http://www.aol.com" class="brand">Aol.</a></li>'
		+'			<li><a href="http://webmail.aol.com/?_AOLLOCAL=mail" class="">MAIL</a></li>'
		+'			<li>'
		+'				<ul class="sub_links">'
		+'					<li class="first">You might also like:&nbsp;</li>'
		+'					<li><a href="'+_ALSO_LIKE_FIRST_URL_+'" class="">'+_ALSO_LIKE_FIRST_TEXT_+'</a>,&nbsp;</li>'
		+'					<li><a href="http://daol.aol.com" class="">Discover AOL</a>,&nbsp;</li>'
		+'					<li class="more"> and <a href="#" onmouseover="Hat_display_more()">More<span class="no_show"> Has attached menu</span></a>'
		+'						<div id="more_links" style="" onmouseout="Hat_hide_more()" onmouseover="Hat_display_more()">'
		+'							<h4>More Sites You Might Like</h4>'
		+'							<ul>'
		+'								<li><a href="http://www.aim.com/">AIM</a></li>'
		+'								<li><a href="http://www.dailyfinance.com/">DailyFinance</a></li>'
		+'								<li><a href="http://www.engadget.com/">Engadget</a></li>'
		+'							</ul>'
		+'							<ul>'
		+'								<li><a href="http://www.huffingtonpost.com/">HuffingtonPost</a></li>'
		+'								<li><a href="http://lifestore.aol.com/">Lifestore</a></li>'
		+'								<li><a href="http://www.mapquest.com/">MapQuest</a></li>'
		+'							</ul>'
		+'							<ul>'
		+'								<li><a href="http://www.moviefone.com/">Moviefone</a></li>'
		+'								<li><a href="http://music.aol.com/">Music</a></li>'
		+'								<li><a href="http://www.patch.com/">Patch</a></li>'
		+'							</ul>'
		+'							<ul>'
		+'								<li><a href="http://aol.sportingnews.com/">SportingNews</a></li>'
		+'								<li><a href="http://techguru.aol.com/">TechGuru</a></li>'
		+'								<li><a href="http://www.winamp.com/">WinAmp</a></li>'
		+'							</ul>'
		+'						</div>'
		+'					</li>'
		+'				</ul>'
		+'			</li>'					
		+'		</ul>'
		+'	</div>'
		
		+'	<div id="header">'
		+'      <!-- Specfic styles for the header module -->'	
		+'		<div id="branding">'
		+'			<h1 class="help_branding">'
		+'				<a href="' + _HELP_HOST_URL_ + '" title="AOL HELP.">'
		+'					<img src="'+_CDN_SERVER_+'/images/2010_logo_aol_help.gif" title="AOL HELP" alt="AOL HELP." border="0"/>'
		+'				</a>'
		+'			</h1>'
		+'			<ul class="member_links">'
		+'				<li class="first">' + _HELP_AOL_CLIENT_START_ + '<a href="'+_SIGN_URL_+'" class="login" title="'+_SIGN_TITLE_+'">'+_SIGN_TEXT_+'</a></li>'
		+'				<li>' + _HELP_AOL_CLIENT_END_ + '<a onclick = "return fBo(\''+_FEEDBACK_BRAND_+'\');" href="'+_FEEDBACK_URL_+'" class="feedback">Feedback</a>'
		+'			</ul>'
		+'		</div>'
		+''
		+'		<div id="navigation">'
		+'			<ul>'
		+'				<li class="first"><a href="' + _HELP_HOST_URL_ + '" class="nav_Help_Main '+_HELP_MAIN_+'">HELP MAIN</a></li>'
		+'				<li><a href="' + _HELP_HOST_URL_ + '/help/a-z/" class="nav_Help_AZ '+_HELP_AZ_+'">HELP A-Z</a></li>'
		+'				<li><a href="' + _HELP_HOST_URL_ + '/help/accountsettings/" class="nav_Help_Accounts '+_HELP_ACCOUNT_+'">ACCOUNTS &amp; SETTINGS</a></li>'
		+'				<li><a href="'+_CONTACT_US_SIGN_URL_+'" class="nav_Help_Contact '+_HELP_CONTACT_+'">CONTACT AOL</a></li>'
		+'			</ul>'
		+'		</div> <!-- #navigation -->	'			
		+'	</div><!-- #header -->';
 } else {
	 var __PC__SEARCH_CATEGORY;//this variable is not in use anymore, keep it here, in case later on , pc got changed!
	 var __PC__HOMEPAGE_URL;
	 var __PC__CDN_PATH_;
	 if(typeof SP_IS_FROM_UK != 'undefined' && SP_IS_FROM_UK==true) {
		 __PC__SEARCH_CATEGORY = '<input type="hidden" name="SEARCH_CATEGORY_ID" value="AOLUKSearch"/>';
		 __PC__HOMEPAGE_URL = "https://lifestore.aol.co.uk/";
	 }  else {
		 __PC__SEARCH_CATEGORY = '<input type="hidden" name="SEARCH_CATEGORY_ID" value="USSearch"/>';
	 	 __PC__HOMEPAGE_URL = "https://lifestore.aol.com/";
	 }
	 
	 if(_is_qa_ == 1) {	
		 __PC__CDN_PATH_ = "https://s.aolcdn.com/os/GPS/QH/"; //http://o.aolcdn.com/os/GPS/QH/images/add_target.gif
	 } else {		 
		 __PC__CDN_PATH_ = "https://s.aolcdn.com/os/GPS/";  //http://o.aolcdn.com/os/GPS/images/add_target.gif
	 }
	 header_html = '	<!-- Hat/Header HTML --> '
		+'	<div id="header" >'
		+'		<div id="branding_pc">'	
		+'			<div class="login_area">'
		+'				<ul class="member_links">'
		+'					<li class="first">' + _HELP_AOL_CLIENT_START_ + '<a href="' + _SIGN_URL_ + '" title="' + _SIGN_TITLE_ + '">' + _SIGN_TEXT_ + '</a></li>'
		+'					<li>' + _HELP_AOL_CLIENT_END_ + '<a href="' + _HELP_HOST_URL_ + '">Help</a></li>'
		+'				</ul>'
		+'			</div>'
		+'			<h1>'
		+'				<a href="' + __PC__HOMEPAGE_URL + '"><img border="0" src="' + __PC__CDN_PATH_ + 'images/gps_product_central_logo.png"></a>'
		+'			</h1>'
		+'			<div class="search_area">'
		+'				<form method="get" action="' + __PC__HOMEPAGE_URL + 'search">'
		+'			     	<input type="hidden" name="VIEW_SIZE" value="101" />'
		+'			     	<input type="hidden" name="SEARCH_OPERATOR" value="OR" />'                    	
		+'					<input type="hidden" name="SEARCH_CATEGORY_ID" value="Search"/>'				             
		+'					<input class="textbox" value="Search Lifestore" type="text" name="SEARCH_STRING" onfocus="if(this.value==\'Search Lifestore\') this.value=\'\';" onblur="if(this.value==\'\') this.value=\'Search Lifestore\';"/>'
		+'			        <input class="submit" value="SEARCH" type="submit" />'
		+'			    </form>'
		+'			</div>'
		+'		</div>'
		+'	</div>';
 }
 
	// Injecting the HTML for the header module
	document.write(header_html);
	 	
	function Hat_display_more(){
		document.getElementById("more_links").style.display="block";
	}
	function Hat_hide_more(){
		document.getElementById("more_links").style.display="none";
	}
	
	// simplify bread crumbs in jsp pages by calling js function.
	// 1st argument is name, 2nd is URL; if URL is null, then no link.
	// for example:
	//      WriteBreadcrumbs("Help Main", "http://help.aol.com", "HELP A-Z", null)
	//           then in page, it is displayed as: Help Main -> HELP A-Z
	//      "Help Main" is a link, but HELP A-Z is not a link.
	function WriteBreadcrumbs(){
		var a=arguments,l=a.length,s="",st="",i;
		for(i=0;i<l;i+=2){
			st=a[i+1]?"<a href='"+a[i+1]+"'>"+a[i]+"</a>":a[i];
			if(i==0)s=st;else s+=" &gt; "+st;
		}
		document.write("<div id='breadcrumbs'>"+s+"</div>");
	}
	function BuildBreadcrumbs(){
		var a=arguments,l=a.length,s="",st="",i;
		for(i=0;i<l;i+=2){
			st=a[i+1]?"<a href='"+a[i+1]+"'>"+a[i]+"</a>":a[i];
			if(i==0)s=st;else s+=" &gt; "+st;
		}
		return "<div id='breadcrumbs'>"+s+"</div>";
	}
	
	
	
	
	//below functions copied from jswriter.js, they are used for retrieve signed SN
	//there is also a way to identify if member is signed or not, for more info, please
	// see jswrite.js file.
	function getCk(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)); 
	} 

	function getValue(nmvalstr,name) {
	    var cookie_array=nmvalstr.split("&");nmvalpos=0;
	    while(nmvalpos<cookie_array.length) {
		if(cookie_array[nmvalpos].indexOf(name+"=")>=0) {
	            var sns_name_pos=cookie_array[nmvalpos].indexOf("=")+1;
	            var nmval=cookie_array[nmvalpos].substring(sns_name_pos,cookie_array[nmvalpos].length);
		    return nmval; 
	        }  
	        nmvalpos++;
	    } 
	    return null; 
	} 

	function base64Decode(str){
	    if(!str) return "";

	    var result=new Array();
	    var i=0,j=0,x,shiftreg=0,charCount=-1,asciiNum=0;
	    var _hexChars=['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'];
	    var c;

	    for(i=0;i<str.length;i++) {
	        c=str.charAt(i);
	        if('A'<=c&&c<='Z')
		    x=str.charCodeAt(i)-65;   
		else if('a'<=c&&c<='z')
		    x=str.charCodeAt(i)-97+26;
	        else if('0'<=c&&c<='9')
		    x=str.charCodeAt(i)-48+52;
	        else if(c=='+')
		    x=62;
		else if(c=='/')
		    x=63;
	        else 
		    continue;   

		charCount++;   
		switch(charCount%4) {
		    case 0: shiftreg=x; continue;
		    case 1: asciiNum=(shiftreg<<2)|(x>>4); shiftreg=x&0x0F; break;
		    case 2: asciiNum=(shiftreg<<4)|(x>>2); shiftreg=x&0x03; break;
		    case 3: asciiNum=(shiftreg<<6)|(x>>0); shiftreg=x&0x00; break;
		}

		var num=asciiNum;   
		if((num<32||num>126)&&(num!=0x0d)&&(num!=0x0a)) {
		    result[j++]="<";
		    result[j++]=_hexChars[((num/16)&0x0F)];
		    result[j++]=_hexChars[((num/1)&0x0F)];
		    result[j++]=">";
		} else 
		    result[j++]=String.fromCharCode(num);  
	    }  
	    return result.join('');
	}



