// /js/new/login_oneciti.js
// ---------------------------------------------------------------------------
// Java Script for Login functionality
// ---------------------------------------------------------------------------

var userIsLogin = false;
var cbolWin;
// ----------------------------------------------------------------
// Print appropriate HTML on the ACCESS top nav bar
// 'Login' or 'Log Off'
// ----------------------------------------------------------------
function checklogin() {
	
	var LoginStatusnon = "ssoSESSIONNONS" + "=";
	var LoginStatus = "ssoSESSION" + "=";
	var CookieValuenon = getcookie(LoginStatusnon);
	var CookieValue = getcookie(LoginStatus);

	var isNav = (navigator.appName == 'Netscape')
	var is_getElementById   = (document.getElementById) ? true : false;
	var isNav6 = isNav && is_getElementById;
	var bV=parseInt(navigator.appVersion);
	var NS4=(document.layers) ? true : false;
	var IE4=((document.all)&&(bV>=4))?true:false;

		//DZ 7/25/2007: DZ wrote if statement for fa.locator redirect
		if ( location.hostname == "fa.smithbarney.com" )
		{
			var Login_on = '<a href="https://www.smithbarney.com/cgi-bin/login/checklogout.cgi" target="_top"><span class="amtplus">Log On</span></a>';
		}
		else
		{
			//DZ 6/21/2007  added "cursor:pointer" to the css/new/standard.css as this is reading the .amtplus from there and not base.css
			var Login_on = '<a href="/cgi-bin/login/login.cgi" target="_top"><span class="amtplus">Log On</span></a>';
		}
//		var Login_off = '<a href="/cgi-bin/login/logout.cgi" target="_top"><span class="amtminus">Log Off</span></a>';
//      GVL - 5/15/2007 target from _top to _self
		var Login_off = '<a href="javascript:SBlogoff();" target="_self"><span class="amtminus">Log Off</span></a>';


	// ----------------------------------------------------------------------
	// 8/15/00, we are going to put 'logon' button on TeleScan,
	// and nothing on CBOE, anything else is as usual
	// ----------------------------------------------------------------------
	var specialDomain1 = "www.cboe.com";
	var specialDomain2 = "prosearch.mycititrade.telescan.com";
	var specialDomain3 = "www.mastercard.com";

	var currentLocation = window.location.toString().toLowerCase();
	if (currentLocation.indexOf(specialDomain1.toLowerCase())!= -1) {
		// ------------------------------------------------
		// CBOE
		// ------------------------------------------------
		//alert("cboe");
	} else if (currentLocation.indexOf(specialDomain3.toLowerCase())!= -1) {
		// ------------------------------------------------
		// Mastercard
		// ------------------------------------------------
	   	// mastercard
	} else if (currentLocation.indexOf(specialDomain2.toLowerCase())!=-1) {
		// ------------------------------------------------
		// telescan
		// ------------------------------------------------
		//alert("telescan");
		if (NS4) {
			document.layers["headlinksDiv"].document.write(Login_off);
		} else {
			document.write(Login_off);
		}
		userIsLogin = true;

	} else {

		if ((CookieValue.toLowerCase() == "logout") || (CookieValuenon.toLowerCase() == "logout")) {

			if (NS4) {
				document.layers["headlinksDiv"].document.write(Login_on);
			} else {
				document.write( Login_on );
			}
			userIsLogin = false;

		} else if ((CookieValue == "empty") && (CookieValuenon == "empty")) {

			if (NS4) {
				document.layers["headlinksDiv"].document.write(Login_on);
			} else {
				document.write( Login_on );
			}
			userIsLogin = false;

		} else {

			if (NS4) {
				document.layers["headlinksDiv"].document.write(Login_off);
			} else {
				document.write(Login_off);
			}
			userIsLogin = true;

		}
	}
}


// ----------------------------------------------------------------
// Get a cookie
// ----------------------------------------------------------------
function getcookie(in_cookiename) {
	var objCookies = document.cookie;
	var pos = objCookies.indexOf(in_cookiename);
	var begin = pos + in_cookiename.length;
	var end = objCookies.indexOf(";", begin);
	if (end  == -1) end= objCookies.length;
	var tempvalue = unescape(objCookies.substring(begin,end));
	if (pos == -1)  tempvalue = 'empty';
	return tempvalue;
}

// ----------------------------------------------------------------
// Begin user type (CBOL, FLS, CIS, etc.) detection code block.
//
// This block of code should be duplicated in the following files
// since various includes pull in the .js files out of order:
//	/js/01_login.js
//	/js/new/login.js
//	/js/new/CoreCookies.js
// ----------------------------------------------------------------

if ( typeof core_user == "undefined" )
{
	var core_user = false;
	
	if( getcookie('CORE_CTX'))
	{
		var core_ctx = getcookie("CORE_CTX");

		if ( core_ctx != null && core_ctx != "" && core_ctx != "empty" )
		{ core_user = true; }
		else
		{
			if (getcookie('BRAND_CTX')) 
			{ 
				if(getcookie('BRAND_CTX').indexOf('MSSB')!= -1) 
				{ core_user = true; } 
			}
		}
	}
	else
	{
		if (getcookie('BRAND_CTX')) 
		{ 
			if(getcookie('BRAND_CTX').indexOf('MSSB')!= -1) 
			{ core_user = true; } 
		}
	}
}

if ( typeof cbol_user == "undefined" )
{
	var cbol_user = false;
	var cis_ctx = getcookie("CIS_CTX");
	var cbol_bc = getcookie("CBOLBreadcrumbs");
	var cis_exists = false;
	var cbolbc_exists = false;

	if ( cis_ctx != null && cis_ctx != "" && cis_ctx != "empty" )
	{
		if ( cbol_bc != null && cbol_bc != "" && cbol_bc != "empty" )
		{
			cis_exists = true;
			cbolbc_exists = true;
			cbol_user = true;
		}
		else
		{
			cis_exists = true;
			cbolbc_exists = false;
		}
	}
}

if ( typeof fls_user == "undefined" )
{
	var fls_user = false;
	
	if( getcookie('FLS_CTX'))
	{
		var fls_ctx = getcookie('FLS_CTX');

		if ( fls_ctx != null && fls_ctx != "" && fls_ctx != "empty" )
		{ 
			if (getcookie('BRAND_CTX')) 
			{ 
				if(getcookie('BRAND_CTX').indexOf('MyFi')!= -1 || getcookie('BRAND_CTX').indexOf('Citi')!= -1 ) 
				{ fls_user = true; } 
			}
		}
		else
		{
			if (getcookie('BRAND_CTX')) 
			{ 
				if(getcookie('BRAND_CTX').indexOf('MyFi')!= -1) 
				{ fls_user = true; } 
			}
		}
	}
	else
	{
		if (getcookie('BRAND_CTX')) 
		{ 
			if(getcookie('BRAND_CTX').indexOf('MyFi')!= -1) 
			{ fls_user = true; } 
		}
	}
}

//DZ: The new non-JV cookie for Citi retained business
if ( typeof non_jv_user == "undefined" )
{
	var non_jv_user = false;
	if (getcookie("NON_JV_CTX"))
	{
		var non_jv_ctx = getcookie("NON_JV_CTX");
		if ( non_jv_ctx != null && non_jv_ctx != "" && non_jv_ctx != "empty" )
		{ non_jv_user = true; }
		else
		{
			if (getcookie('BRAND_CTX')) 
			{ 
				if(getcookie('BRAND_CTX').indexOf('Citi')!= -1) 
				{ non_jv_user = true; } 
			}
		}
	}
	else
	{
		if (getcookie('BRAND_CTX')) 
		{ 
			if(getcookie('BRAND_CTX').indexOf('Citi')!= -1) 
			{ non_jv_user = true; } 
		}
	}
}

if ( typeof pcr_user == "undefined" )
{
	var pcr_user = false;
	var pcr_ctx = getcookie("PCR_CTX");

	if ( pcr_ctx != null && pcr_ctx != "" && pcr_ctx != "empty" )
	{
		pcr_user = true;
	}
}
// ----------------------------------------------------------------
// End user type code block.
// ----------------------------------------------------------------

if ( typeof cbb_user == "undefined" )
{
	var cbb_user = false;
}

if ( cbol_user )
{
	var cbb_ctx = getcookie("CBB_CTX");

	if ( cbb_ctx != null )
	{
		cbb_user = true;
	}
}

//set the PZN tool user
if ( typeof pzn_user == "undefined" )
{
	var pzn_user = false;
	var pzn_ctx = getcookie("PZN_CTX");

	if ( pzn_ctx != null && pzn_ctx != "" && pzn_ctx != "empty" )
	{ 
		if (getcookie('PZN_CTX').indexOf('ID')!= -1)	
		{ pzn_user = true; } 
	}
}

//set the EW8 user
if ( typeof ew8_user == "undefined" )
{
	var ew8_user = false;
	var ew8_ctx = getcookie("EW8Session");

	if ( ew8_ctx != null && ew8_ctx != "" && ew8_ctx != "empty" )
	{ ew8_user = true; }
}

// ----------------------------------------------------------------
// Set Login Image Src
// ----------------------------------------------------------------
function checkLoginImage() {
	if (document.images["login"]){
		document.images["login"].src =
			(userIsLogin) ? "images/0_btn_logoff.gif" : "images/0_btn_logon.gif";
	}
}

var ns4 = (document.layers)? true:false
var ie4 = (document.all)? true:false
var isNav = (navigator.appName.indexOf("Netscape") != -1 );
var isIE = (navigator.appName.indexOf("Microsoft") != -1 );

function handleEnterSubmission(field, evt)
{
	var keyCode = evt.which ? evt.which : evt.keyCode;

	if ( keyCode == 13 )
	{
		if(document.getElementById("btnLogin")) { document.getElementById("btnLogin").click(); return false;}
		else { document.getElementById("submitbtn").click(); return false; }
	}
	else
	{
		return true;
	}
}

function handleEnterSubmission2(evt)
{
	var keyCode = 0;
	if ( isIE )
	{
		keyCode = event.keyCode;
		var nKey = 0;
	}
	if ( isNav )
	{
		keyCode = evt.which;
		var ieKey = 0;
	}
	if ( keyCode == 13 )
	{
		if ( isNav || isIE )
		{
			document.forms["logon"].submit();
		}
		return false;
	}
	else
	{
		return true;
	}
}

if ( isNav && ns4 )
{
	document.onkeydown = handleEnterSubmission
	document.captureEvents(Event.KEYDOWN)
}

function form_funcs()
{
	if ( document.forms["logon"].username.value == "" )
	{
		document.forms["logon"].username.focus();
		document.forms["logon"].username.select();
	}
}
var FORM_NAME='logon';

//DZ added this to see if the fields are empty, return them to their original state.
function checkCopy(f)
{ 
	if (f.value=="") 
	{   

		if (f.id == 'marketSearchTxt') { document.getElementById(f.id).value = "Enter Symbol"; }

		if (f.id == 'searchTxt') { document.getElementById(f.id).value = "Search Our Site"; }
		
		if (f.id == 'username')
		{ f.value="Enter User Name" }
		else if (f.id == 'password')
		{
			//check if the password as an id. then the below happens
			var browser=navigator.appName;
  
  			if (browser=="Microsoft Internet Explorer")
  			{
			//alert("I'm in IE6");
    			var input = document.getElementById(f.id);
    			//I'm using escapes below
    			var input2 = document.createElement('<input name=\'password\' type=\'text\' id=\'password\' value=\'Enter Password\' onblur=\'checkCopy(this)\' onfocus=\'swapfield(this,\"password\")\' onkeyup=\'return handleEnterSubmission(this.form,event)\'  maxlength=\'16\' autocomplete=\'off\' />');
    			input.parentNode.appendChild(input2);
    			input.parentNode.removeChild(input);
			}
    		else
 			{	//For everything except IE6
				document.getElementById(f.id).type = "text";
	  			document.getElementById(f.id).value = "Enter Password";
 			}  
		}	
	}
}

//DZ added this for the OneCiti password field. It can be used to swap out any input field type.
function swapfield(f, lcType)
{
  var browser=navigator.appName;
  
  if (browser=="Microsoft Internet Explorer")
  {
	document.getElementById(f.id).value = "";
    var input = document.getElementById(f.id);
    //I'm using escapes below
    var input2 = document.createElement('<input name=\'password\' type=\'password\' id=\'password\' onblur=\'checkCopy(this)\' onkeyup=\'return handleEnterSubmission(this.form,event)\'  maxlength=\'16\' autocomplete=\'off\' />');
    input.parentNode.appendChild(input2);
    input.parentNode.removeChild(input);
    input2.focus();
    //input.parentNode.replaceChild(input,input2);

  }
  else
  {	//alert("I'm in the function");
  	//I have to grab all the text in the field.
  	document.getElementById(f.id).value = "";
  	document.getElementById(f.id).type = lcType;
  }
}

// DZ rewrote this for OneCiti
function getCookieNames_oneciti() { 
	if (typeof(window.nameList)=="undefined" || window.nameList.length==0)
	{
		// I have to do a onfocus function to clear out this field
		document.write('<input type="text" id="username" name="username" value="Enter User Name" onblur="checkCopy(this)" onfocus=this.value="" maxlength="35" autocomplete="off">');
		//DZ added this 10/16/2007 to give username focus when no user. Disabled it for OneCiti Rebrand
		//document.forms["logon"].username.focus();
		return;
	}

        var l=[];
        if (typeof(window.nameList)!="undefined")
        {
			l = window.nameList.split("\&");
			//alert(l); //This brings up the username
        }
        l=l.join('|').split('|');
		//alert(l); This brings up the user name into an array and then writes it out.
        var elSelect='';
        elSelect+='<select name="username" id="username" onChange="onSelectUser(this);">';
        //elSelect+='<select name="username" id="username" class="unpw" onChange="onSelectUser(this);">';
        elSelect+='<option>------ Sign on as: -------</option>';
        for( var i = 0; i < l.length; i++ )
        {
					elSelect+='<option value="'+l[i]+'"'+(i==l.length-1?' selected':'')+'>'+l[i]+'</option>';
					//elSelect+='<option value="'+l[i]+'"'+(i==0?' selected':'')+'>'+l[i];
        }
        elSelect+='<option>----- Other Options -----';
        elSelect+='<option value="inputUserName">A different user</option>';
        elSelect+='<option value="removeUserName">Remove a User ID (cookie)</option>';
        elSelect+='</select>';

	 //DZ this gives another input box, but I don't know why it's there. Is it a hidden variable?
        //elSelect+='<input style="display:none; padding-left:3px; margin-top:-1px; margin-bottom:-1px;" type="text" name="username2" id="username2" value="" />';
        //elSelect+='<input style="display:none; padding-left:3px; margin-top:-1px; margin-bottom:-1px;" class="unpw" type="text" name="username2" id="username2" value="" />';

        document.write(elSelect);
}


function getCookieNames() {
	if (typeof(window.nameList)=="undefined" || window.nameList.length==0)
	{
		document.write('<input type="text" id="username" name="username" class="tbox3 unpw"  maxlength="35" autocomplete="off">');
		//document.write('<input type="text" id="username" name="username" class="unpw"  maxlength="16" autocomplete="off">');
		
		//DZ added this 10/16/2007 to give username focus when no user
		document.forms["logon"].username.focus();
		return;
	}

        var l=[];
        if (typeof(window.nameList)!="undefined")
        {
			l = window.nameList.split("\&");
			//alert(l); //This brings up the username
        }
        l=l.join('|').split('|');
		//alert(l); This brings up the user name into an array and then writes it out.
        var elSelect='';
        elSelect+='<select name="username" id="username" class="tbox3 unpw" onChange="onSelectUser(this);">';
        //elSelect+='<select name="username" id="username" class="unpw" onChange="onSelectUser(this);">';
        elSelect+='<option>------ Sign on as: -------';
        for( var i = 0; i < l.length; i++ )
        {
					elSelect+='<option value="'+l[i]+'"'+(i==l.length-1?' selected':'')+'>'+l[i];
					//elSelect+='<option value="'+l[i]+'"'+(i==0?' selected':'')+'>'+l[i];
        }
        elSelect+='<option>----- Other Options -----';
        elSelect+='<option value="inputUserName">A different user';
        elSelect+='<option value="removeUserName">Remove a User ID (cookie)';
        elSelect+='</select>';

        elSelect+='<input style="display:none; padding-left:3px; margin-top:-1px; margin-bottom:-1px;" class="tbox3 unpw" type="text" name="username2" id="username2" value="" />';
        //elSelect+='<input style="display:none; padding-left:3px; margin-top:-1px; margin-bottom:-1px;" class="unpw" type="text" name="username2" id="username2" value="" />';

        document.write(elSelect);
}

// This is for OneCiti rebrand
function getCheckBox4RememberID_new() {
	if (typeof(window.nameList)=="undefined" || window.nameList.length==0) {
		document.write('<input type="checkbox" id="rememberId"  name="rememberId" />');
		document.write('<label for="rememberId">Remember User Name</label>&nbsp;');
                return;
	}
	//DZ 10/15/2007 - as per Max that if there's a username present, the password field will get focus. Took out for one citi
	/*else	{ document.forms["logon"].password.focus(); }*/
}

function getCheckBox4RememberID() {
        //if (window.inputUserName) {
	if (typeof(window.nameList)=="undefined" || window.nameList.length==0) {
                //document.write(' <tr>' );
                //document.write(' <td class="rgt" ><input type="checkbox" name="rememberId" ></td> ');
                //document.write(' <td >Remember user name</td> ');
                //document.write(' </tr>' );
		document.write('<input type="checkbox" id="rememberId"  name="rememberId" />');
		document.write('<label for="rememberId" style="font-weight:normal; color:black;">Remember User Name</label>&nbsp;');
                return;
	}
	//DZ 10/15/2007 - as per Max that if there's a username present, the password field will get focus.
	else
	{
		document.forms["logon"].password.focus();
	}
}

function onSelectUser(e)
{
        var n=e[e.selectedIndex].value;
        if(n=="") {
				//alert("If n==''");
                e.selectedIndex=1;
                return;
        }
        if(n=='inputUserName')  {
				
				//DZ added this 9/11/2007
				var passwordValue = document.forms[FORM_NAME].password.value;
				if(passwordValue !="")
				{
					document.forms[FORM_NAME].password.value = ""
				}
				//End DZ addition
                document.forms[FORM_NAME].submit();
                return;
        }
        if(n=='removeUserName') {
                document.forms[FORM_NAME].submit();
                return;
        }
}


function SBlogoff()
{
	if ( typeof core_user != "undefined" )
	{

		if ( ( core_user ) && ( getcookie("CORE_CTX").indexOf("ON") != -1 ) )
		{
			if ( confirm("Would you like to log off of Citibank Online as well?") )
			{
				CBOLLogOffWin = window.open("", "CBOLLogOff");

				//FOR STAGING: DZ had to put this in for Citibank to test. 10/1/2007
				//CBOLLogOffWin.location.href = "https://web-uat1.citibankonlineqa.com/cgi-bin/citifi/portal/sb/checkSessionStatus.do?SignOff=SIGNOFF&BV_UseBVCookie=Yes&pageType=SIGNOFF&eACE-TOKEN=null";
				
				//FOR PRODUCTION: DZ changed URL as per Glenn - 6/14/2007
				CBOLLogOffWin.location.href = "https://web.da-us.citibank.com/cgi-bin/citifi/portal/sb/checkSessionStatus.do?SignOff=SIGNOFF&BV_UseBVCookie=Yes&pageType=SIGNOFF&eACE-TOKEN=null";
			}
		}
		
		//DZ added this to the code 7/26/2007
		if ( location.hostname == "fa.smithbarney.com" )
		{
			document.location.href = "https://www.smithbarney.com/cgi-bin/login/logout.cgi";
		}
		else
		{
			document.location.href = "/cgi-bin/login/logout.cgi"; //Original here
		}
	}
	//DZ added this 7/27/2007 to redirect from the "fa.locator.com"
	if ( location.hostname == "fa.smithbarney.com" )
	{
		document.location.href = "https://www.smithbarney.com/cgi-bin/login/logout.cgi";

		// GL - 13JUN2007 - This code is used to close any open child window with the name of 'PCR' on user logout.
		var MSG = "https://www.smithbarney.com/kill_window.html"
		window.open( MSG , 'PCR' , 'top=10000,left=-1000,height=1,width=1,alwaysLowered=yes' );
	}
	else
	{
		// GL - 13JUN2007 - This code is used to close any open child window with the name of 'PCR' on user logout.
		var MSG = window.location.protocol + "//" + window.location.hostname + "/kill_window.html"
		window.open( MSG , 'PCR' , 'top=10000,left=-1000,height=1,width=1,alwaysLowered=yes' );
	}
}

function relaunch_pcr_window( URL )
{
	//Kill the old PCR window	
	var MSG = window.location.protocol + "//" + window.location.hostname + "/kill_window.html"
	window.open( MSG , 'PCR' , 'top=10000,left=-1000,height=1,width=1,alwaysLowered=yes' );
	
	//Launch new PCR widnow with new URL
	MSG = window.location.protocol + "//" + window.location.hostname + URL
	window.open( URL , 'PCR' , 'top=100,left=100,height=600,width=800,status=1,menubar=1,location=1,toolbar=1,scrollbars=1,resizable=1' ); 
}


