/*
CVS Version : $Id: myaccount.js,v 1.13 2003/06/11 15:29:40 aankja Exp $

*/

function showlogoff(storeid,langid,logoffurl,linkname,confirmmsg){
if (storeid!="" && langid!="" && logoffurl!="" && linkname!="" && confirmmsg!="" && document.cookie.length > 0){
search="WCS_SESSION_ID";
offset = document.cookie.indexOf(search)
if (offset != -1) {
offset += search.length
end = document.cookie.indexOf(";", offset)
if (end == -1)
end = document.cookie.length
thevalue=unescape(document.cookie.substring(offset, end));
lastchar=thevalue.substring(thevalue.length-5,thevalue.length);
if (lastchar.indexOf(',,')==-1 && lastchar.indexOf('null')==-1 && thevalue.indexOf(storeid)!=-1 ){
document.writeln("<td>");
document.writeln("<div class=pipe>&nbsp;|&nbsp;</div>");
document.writeln("</td>");
document.writeln("<td>");
document.writeln("<script><a href=\"javascript:if(confirm('"+escape(confirmmsg)+"')){document.location='/webapp/wcs/stores/servlet/Logoff?langId="+langid+"&storeId="+storeid+"&URL="+logoffurl +"'};\" class=logout>"+linkname+"</a></" + "script>");
document.writeln("</td>");
}//write
} //offset
} //cookie
}

/**
	Enhanced version of the showlogoff function that doesn't ouput formatting
	@author KLSK
	@author BM26 (fixed bug between sites)
	@author KNBU (migrated to use WC5.6 cookie. Existing 5.4 is in a separate method OLD54haslogoffURL
	@author MTJO 2006-11-21  - (fixed problem with logout in firefox)
*/
function haslogoffURL(storeid){
	//document.write('<td>' + unescape(document.cookie) + '</td>');

	if (document.cookie == null) {
		// no cookie at all
		return false;
	}
	
	if (document.cookie.famloggedin) {
		return true;
	}
	
  // Split cookie
  var cookieArray = unescape(document.cookie).split(';');
  var wcauthstring = 'WC_USERSESSION_';
  var wcauthinfo = '';
  var wcstorestring = 'WC_ACTIVESTOREDATA';
  var wcstoreinfo = '';
  for (var i=0; i<cookieArray.length; i++) {
  	if (cookieArray[i].indexOf(wcauthstring) > -1) {
  		// Get authentication info
			wcauthinfo = cookieArray[i].substring(cookieArray[i].indexOf('=') + 1);
		}
		if (cookieArray[i].indexOf(wcstorestring) > -1) {
			// Get store info
			wcstoreinfo = cookieArray[i].substring(cookieArray[i].indexOf('=') + 1);
	  }
  }
	if (wcauthinfo == null || wcauthinfo == '') {
		return false;
	}

	var wcauthinfo_array = wcauthinfo.split(",");
	var wcstoreinfo_array = wcstoreinfo.split(",");

  var userId = wcauthinfo_array[0];

  var detailedInfo = wcauthinfo_array[12].substring(wcauthinfo_array[12].indexOf('[') + 1);
  var detailedInfo_array = detailedInfo.split("|");
  
var cookieStoreId = detailedInfo_array[0];


  // If 12th element is 'null' the user is not logged in
  if (wcauthinfo_array[11] == 'null') {
  	// Not logged in
  	return false;
  }

	// If the user changes store, kill the cookie
	
	if (storeid != cookieStoreId) {
 		var sDomain = document.domain;
 		
 		while (sDomain.indexOf('.') > -1) {
		  document.cookie = "WC_USERSESSION_" + userId + "=;domain=" + sDomain + ";path=/;expires=Wednesday, 09-Nov-99 23:12:40 GMT";
		  document.cookie = "WC_AUTHENTICATION_" + userId + "=;domain=" + sDomain + ";path=/;expires=Wednesday, 09-Nov-99 23:12:40 GMT";
		  sDomain = sDomain.substring(sDomain.indexOf('.') + 1);
		}
		//document.cookie = "WCS_ACTIVESTOREDATA=;path=/;expires=Wednesday, 09-Nov-99 23:12:40 GMT";
		//document.cookie = "WCS_UNIQUE_ID=;path=/;expires=Wednesday, 09-Nov-99 23:12:40 GMT";
		//document.cookie = "WCS_SSLCHECKCOOKIE=;path=/;expires=Wednesday, 09-Nov-99 23:12:40 GMT";
		//document.cookie = "WCS_PARORG=;path=/;expires=Wednesday, 09-Nov-99 23:12:40 GMT";
		//document.cookie = "WCS_CRNTCNTRCTS=;path=/;expires=Wednesday, 09-Nov-99 23:12:40 GMT";
		//document.cookie = "WCS_ELGBCNTRCTS=;path=/;expires=Wednesday, 09-Nov-99 23:12:40 GMT";
		//document.cookie = "WCS_SESSCNTRCTS=;path=/;expires=Wednesday, 09-Nov-99 23:12:40 GMT";
		//document.cookie = "JSESSIONID=;path=/;expires=Wednesday, 09-Nov-99 23:12:40 GMT";
		return false;
	}

	// User is logged in
	return true;

}


function getCurrentURL(){
	var returnurl = document.location.pathname + document.location.search;
	// This is fixed in setupyouarehere.jsp..
	// var returnurl = document.location.pathname;
	return returnurl;
}

function logonLink() {
	var argForm;
	//netscape 6 && ie 5>
	if (document.getElementById){
	        argForm = document.getElementById("myAccount");
	}
	else if (document.all){ //Explorer 4
	        argForm = document.all['myAccount'];
	}
	else {
	        alert (js_fn_NOT_VALID_BROWSER);
	        return false;
	}

	// Check if Form webSiteProfile is accessible. If so user is already in editprofile page. Use returnURL parameter
	var profileFormName = "webSiteProfile";
	var profileForm;

	if (document.getElementById) {
  		if (document.getElementById(profileFormName)) {
  			profileForm = document.getElementById(profileFormName);
  		}
	}
	else if (document.all) {
		if (document.all[profileFormName]) {
			profileForm = document.all[profileFormName];
		}
 	}
	else {
 		alert (js_fn_NOT_VALID_BROWSER);
	}

	if (profileForm != null) {
		argForm.returnURL.value = profileForm.returnURL.value;
	}
	else {
  	// argForm.returnURL.value = getCurrentURL();
  	// global variable in WCS pages (youarehere.jsp)
  	// DEBUG
  	// alert("Using maReturnUrl varaible: " + maReturnUrl);
  	// DEBUG
	result = typeof maReturnUrl;
//	if (result=='undefined'){
		currenturl = getCurrentURL();
		argForm.returnURL.value = currenturl;
//	}
//	else{
//  		argForm.returnURL.value = maReturnUrl;
//	}
  }

  argForm.submit();
}


function OLD54haslogoffURL(storeid){
	var wcssessionidcookie = getCookie("WCS_SESSION_ID");
	if (wcssessionidcookie == null) {
		//no cookie at all.
		return false;
	}

	//split variables in cookie.
	var wcssessionidcookie_array = wcssessionidcookie.split(",");

	//last String in array is the userid and if there is a user id, then user is logged in.

	var userId = wcssessionidcookie_array[wcssessionidcookie_array.length - 1];
	var loggedIn = userId != null && userId != "" && userId != "null";

	if (wcssessionidcookie_array[2] == storeid) {

		if (loggedIn) {
			//logged in customer, show logout.
			return true;
		}
		else {
			//not logged in but cookie for right country.
			return false;
		}
	}
	else {
		//User from wrong country, kill cookie.
		document.cookie = "WCS_SESSION_ID=;path=/;expires=Wednesday, 09-Nov-99 23:12:40 GMT";
		document.cookie = "WCS_SSLCHECKCOOKIE=;path=/;expires=Wednesday, 09-Nov-99 23:12:40 GMT";
		document.cookie = "WCS_AUTHENTICATION_ID=;path=/;expires=Wednesday, 09-Nov-99 23:12:40 GMT";
		document.cookie = "WCS_PARORG=;path=/;expires=Wednesday, 09-Nov-99 23:12:40 GMT";
		document.cookie = "WCS_CRNTCNTRCTS=;path=/;expires=Wednesday, 09-Nov-99 23:12:40 GMT";
		document.cookie = "WCS_ELGBCNTRCTS=;path=/;expires=Wednesday, 09-Nov-99 23:12:40 GMT";
		document.cookie = "WCS_SESSCNTRCTS=;path=/;expires=Wednesday, 09-Nov-99 23:12:40 GMT";
		document.cookie = "JSESSIONID=;path=/;expires=Wednesday, 09-Nov-99 23:12:40 GMT";
		return false;
	}
}
