/**
 * Current JS IS ONLY FOR 3.0 Biz 
 * This method is used to check if the user is cookied and what type of user he
 * is based on which he is redirected.
 * 
 * The method should be called right after the opening <body> tag. It will then
 * make body invisible via display: none. Then the cookie is figured out and the
 * browser is either redirected or the body content is shown. This trick helps
 * prevent the page content from flashing on quick redirects.
 */
var $ = jQuery.noConflict();
function segmentUser() {
	
	var queryStr = new Querystring();
	var homePage_content = document.body;
	homePage_content.style.display = 'none';
	// Rule: QueryString passed to force user to specific page
	var stayOnPage = queryStr.get("state","false");
    
	if (stayOnPage == "true") {

		// show the page content if no redirect happened
		homePage_content.style.display = '';		
		return;
	}

	
	// Hide the home page while reading cookies	to prevent blink on redirect
	
	// Rule: QueryString passed to force user to Nextel page
	var nextelPage = queryStr.get("brand","sprint");

	if (nextelPage == "Nextel") {
		window.location.href = '/index_n.html' + location.search.substring(0,location.search.length);
		return;
	}

	var homePage_cookies = document.cookie;
	var homePage_loginMode = 'SMSESSION=';
	var homePage_rememberMe = 'rememberUser=';
	
	/*
	 * Logic to redirect to the logged in index page served from application
	 * server in case the user is already signed in
	 */
	
	if (homePage_cookies.indexOf(homePage_loginMode) != -1) {

		var startpos = homePage_cookies.indexOf(homePage_loginMode)
				+ homePage_loginMode.length;

		var endpos = homePage_cookies.indexOf(";", startpos);

		if (endpos == -2)
			endpos = homePage_cookies.length;

		var cookie_value = unescape(homePage_cookies
				.substring(startpos, endpos));
		if (cookie_value != "LOGGEDOFF") {
			window.location = 'https://mysprint.sprint.com/mysprint/pages/sl/global/index.jsp'+ location.search.substring(0,location.search.length);
			
			return;
		}
	}
	
	/*
	 * Logic to redirect to the remember me page served from application server
	 * in case the user had checked in remember me during sign in
	 */
	 
	if (homePage_cookies.indexOf(homePage_rememberMe) != -1) {

		var startpos = homePage_cookies.indexOf(homePage_rememberMe)
				+ homePage_rememberMe.length + 1;

		var endpos = homePage_cookies.indexOf(";", startpos);

		if (endpos == -2)
			endpos = homePage_cookies.length;

		var cookie_value = unescape(homePage_cookies
				.substring(startpos, endpos));

		if (cookie_value) {
			window.location = 'https://mysprint.sprint.com/mysprint/pages/sl/global/index.jsp'+ location.search.substring(0,location.search.length);
			return;
		}
	}

	

	// Logic to extract userName and userType from cookie

	var homePage_cookieName = 'user=';
	var homePage_userId = "";
	var homePage_userType = "";
    var login_cookie_set = false;

	if (homePage_cookies.indexOf(homePage_cookieName) != -1) {
		var startpos = homePage_cookies.indexOf(homePage_cookieName) + homePage_cookieName.length + 1;
		var endpos = homePage_cookies.indexOf(";", startpos);
		if (endpos == -2){ endpos = homePage_cookies.length; }
		if (endpos == -1){ endpos = homePage_cookies.length; }
		var cookie_value = unescape(homePage_cookies.substring(startpos, endpos));
		var cookieVals = cookie_value.split("|");
		homePage_userId = cookieVals[0];
		homePage_userType = cookieVals[1];
		login_cookie_set = true;
	}


	// Logic to extract segmented user from cookie

	var segPage_cookieName = 'segment_user=';
	var segPage_userType = "";

	if (homePage_cookies.indexOf(segPage_cookieName) != -1) {
		var startpos = homePage_cookies.indexOf(segPage_cookieName) + segPage_cookieName.length; // + 1;
		var endpos = homePage_cookies.indexOf(";", startpos);
		if (endpos == -2){ endpos = homePage_cookies.length; }
		if (endpos == -1){ endpos = homePage_cookies.length; }
		var segment_userType = unescape(homePage_cookies.substring(startpos, endpos));
	}


	/*** segment cookie values
	 Consumer Prospect: CP
	 Consumer Customer: CC
	 Business Prospect: BP
	 Business Customer: BC
	 ****/
	
	// Redirect segemented client
	if (segment_userType == 'CC'){window.location.href = '/index_c.html' + location.search.substring(0,location.search.length);return; }
	else if (segment_userType == 'BC'){window.location.href = '/index_b.html' + location.search.substring(0,location.search.length);return; }
	else if (segment_userType == 'CP'){window.location.href = '/index_p.html' + location.search.substring(0,location.search.length); return;}
	else if (segment_userType == 'BP'){window.location.href = '/index_b.html' + location.search.substring(0,location.search.length);return; }
	else if (homePage_userType == 'consumer'){window.location.href = '/index_c.html' + location.search.substring(0,location.search.length);return;}
	//Is Prospect senario in user cookie required, Prabir?
	else if (homePage_userType == 'prospect'){ window.location.href = '/index_p.html' + location.search.substring(0,location.search.length);return; }
	else if (homePage_userType == 'business'){ window.location.href = '/index_b.html' + location.search.substring(0,location.search.length);return; }
	else{
		if (login_cookie_set == true) {
			window.location.href = '/index_c.html' + location.search.substring(0,location.search.length);return;
		}
	}
	
	//Following code defaults to index_p.html instead of presenting the customer with segment (index.html) page.
	//window.location.href = '/index_p.html' + location.search.substring(0,location.search.length);
	//return;
	
		// show the page content if no redirect happened
		homePage_content.style.display = '';

}

/**
 * This method is used to check if the user is cookied and whether his session
 * is valid or if he is a remembered user
 * 
 * The method should be called right after the opening <body> tag. It will then
 * make body invisible via display: none. Ten the cookie is figured out and the
 * browser is either redirected or the body content is shown. This trick help
 * prevent the page content from flashing on quick redirects.
 */
function checkCookie() {

   	// Rule: QueryString passed to force user to specific page
	var queryStr = new Querystring();
	var stayOnPage = queryStr.get("state","false");
	var homePage_content = document.body;

	if (stayOnPage == "true") {

		// show the page content if no redirect happened
		homePage_content.style.display = '';		

		return;
	}

	
	//Hide the page and retrive cookie values to see if any redirection required
	
	homePage_content.style.display = 'none';
	
	var homePage_cookies = document.cookie;
	var homePage_loginMode = 'SMSESSION=';
	var isUserLoggedIn = false;
	var homePage_currentSegmentCookie = 'segment_user=';
	//var homePage_rememberMe = 'rememberUser=';

	
	/*
	 * Logic to redirect to the logged in index page served from application
	 * server in case the user is already signed in
	 */
	if (homePage_cookies.indexOf(homePage_loginMode) != -1) {

		var startpos = homePage_cookies.indexOf(homePage_loginMode)
				+ homePage_loginMode.length;

		var endpos = homePage_cookies.indexOf(";", startpos);

		if (endpos == -2)
			endpos = homePage_cookies.length;

		var cookie_value = unescape(homePage_cookies
				.substring(startpos, endpos));
		if (cookie_value != "LOGGEDOFF") {
			isUserLoggedIn = true;
		}
		
		
		
		if (isUserLoggedIn) {
			window.location = 'https://mysprint.sprint.com/mysprint/pages/sl/global/index.jsp'+ location.search.substring(0,location.search.length);
			return;
		}
	}
	
	//Redirection when unauth users in JSP page click consumer tab
	if(!isUserLoggedIn) {
		var curSegUserCookieValue = Sprint.fn.readCookie("segment_user");
		if(curSegUserCookieValue) {
			var currentHomePageType = $("#homePageType").val();
			switch(currentHomePageType) {
				case 'Customer':
					if (curSegUserCookieValue == "CP" || curSegUserCookieValue == "BP" || curSegUserCookieValue == "BC") {
						window.location.href = '/index_p.html?context=CP';
					} 
					
					break;
			}
		}
	}

	// show the page content if no redirect happened
	homePage_content.style.display = '';
	return;
}

/**
 * This generic method is used to set the cookie.
 * Cookie name, value and number of days to expiry are passed as parameters to this method.
 */

function setCookie(c_name,value,expiredays) {
	var exdate = new Date();

	var path = "/";

	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie = c_name + "=" + escape(value) +
	((expiredays==null) ? "" : ";expires=" + exdate.toUTCString()) +
	( ( path ) ? ";path=" + path : "" ) + (('.sprint.com') ? "; domain=" + '.sprint.com' : "");
	

}

/**
 * This method is used to set a cookie for a business user and redirect to the
 * business home page
 */
function setBusinessCookie() {
	
	setCookie('segment_user','business',180);
	setCookie('user','xyz|business',30);
	window.location = '/index_b.html'; 
}

/**
 * This method is used to set a cookie for a prospect user and redirect to the
 * prospect home page
 */
function setProspectCookie() {
	setCookie('segment_user','CP',180);
	setCookie('user','xyz|prospect',30);
	window.location = '/index_p.html';
}

/**
 * This method is used to set a cookie for a existing customer and redirect to
 * the existing customer home page
 */
function setConsumerCookie() {
	setCookie('segment_user','CC',180);
	setCookie('user','xyz|consumer',30);
	window.location = '/index_c.html';
}

/**
 * This method is used to set a cookie for existing Conumer customer
 */
function setConsumerCustomerCookie() {
	Sprint.fn.createCookie("segment_user", "CC", "180", "/", ".sprint.com");
	Sprint.fn.createCookie("user", "xyz|consumer", "30", "/", ".sprint.com");
}

/**
 * This method is used to set a cookie for existing Business customer
 */
function setBusinessCustomerCookie() {
	Sprint.fn.createCookie("segment_user", "BC", "180", "/", ".sprint.com");
	Sprint.fn.createCookie("user", "xyz|business", "30", "/", ".sprint.com");
}

/**
 * This method is used to set a cookie for a prospect Consumer Customer  
 */
function setConsumerProspectCookie() {
	Sprint.fn.createCookie("segment_user", "CP", "180", "/", ".sprint.com");
	Sprint.fn.createCookie("user", "xyz|prospect", "30", "/", ".sprint.com");
}

/**
 * This method is used to set a cookie for a prospect business customer
 */
function setBusinessProspectCookie() {
	Sprint.fn.createCookie("segment_user", "BP", "180", "/", ".sprint.com");
	Sprint.fn.createCookie("user", "xyz|prospect", "30", "/", ".sprint.com");
}


/** This method clears the user cookie when the user clicks on "I am not" link */
function clearUser() {
	value = "";
	var date = new Date();
	date.setTime(date.getTime() + (-1 * 24 * 60 * 60 * 1000));
	var expires = "; expires=" + date.toGMTString();
	document.cookie = "user=" + value + expires + "; path=/; domain=sprint.com";
}

/**
 * This method is invoked onclick of "I'm a Sprint customer!" link on the
 * Prospect page
 */
function showCustomerPage() {
	window.location = '/index_c.html';
}

/**
 * This method is invoked onclick of "Not a Sprint customer?" link on the
 * Customer page
 */
function showProspectPage() {
	window.location = '/index_p.html';
}

/**
 * This function is called by function setChatTealeafSessionId 
 */

function trim(s) {
	 var l=0; var r=s.length -1;
	 while(l < s.length && s[l] == ' ') {    
		 l++; 
	 }
	 while(r > l && s[r] == ' ') {     
		  r-=1;     
	 }
	 return s.substring(l, r+1);
}         

/**
 * This function is invoked to set the chat tealeaf session id 
 * onclick of the Chat button
 */ 
function setChatTealeafSessionId() {
	var chattealeafSessionId = "";
	var c=document.cookie.split(';');
	for(var x=0; x<c.length; x++)
	{
		var ck=c[x].split('=');
		var name=trim(ck[0]);
		if(name=="TLTSID")
		{
			chattealeafSessionId = ck[1];
		}
	}

	return chattealeafSessionId;
}


function Querystring(qs) {
	
  this.params = new Object();
  this.get=getQuerystring;
	
  if (qs == null){
    qs=location.search.substring(1,location.search.length);
  }

  if (qs.length == 0) return;

  // Turn <plus> back to space
  qs = qs.replace(/\+/g, ' ');
  var args = qs.split('&'); 

  // split out each name=value pair
  for (var i=0;i<args.length;i++) {
    var value;
    var pair = args[i].split('=');
    var name = unescape(pair[0]);

    if (pair.length == 2){
      value = unescape(pair[1]);
    }else{
      value = name;
    }
    this.params[name] = value;
  }
}


function getQuerystring(key, default_) {
  // This line changes UNDEFINED to NULL
  if (default_ == null) default_ = null;
  var value=this.params[key];
  if (value==null) value=default_;
  return value;
}

