function getCookieValue(index)
{
  var allcookies = document.cookie;
  var pos = allcookies.indexOf(index + "=");
  if (pos == -1) return null;
  var start = pos + (index.length + 1);
  var end = allcookies.indexOf(";", start);
  if (end == -1) end = allcookies.length;
  var value = allcookies.substring(start, end);
  return value;
}

// Get the value of the origin tag based on the page.
function GetOriginValue(currentUrl)
{

    // Regular Expressions for checking Shop and Secure Urls. 
    var shopPattern = new RegExp("/*shop[0-9]*[a-z]*[.dev]*[.]+nordstrom.com/S/", "i");
    var securePattern = new RegExp("/*secure[0-9]*[a-z]*[.dev]*[.]+nordstrom.com", "i");

    if (0 <= currentUrl.toLowerCase().indexOf("/livehelp/default.asp"))
        return "DLP";
    else if (shopPattern.test(currentUrl))
        return "PP"; 
    else if (securePattern.test(currentUrl))
        return "CP"; 
}

/* For IE the address bar has to be shown while for other browser the 
 * address bar has to be hidden.
*/
function CheckBrowserType()
{
    if (navigator.appName.match("Microsoft Internet Explorer"))
	    return "yes";
    else
        return "no";
}

/* This function is used to open a pop-up window with the mentioned parameters
 * and return the opened window as an object to the calling function. 
 */
function OpenChatPopupWindow(url, name)
{
    var w = 500;
    var h = 320;
    var l = (screen.width-w)/2;
    var t=(screen.height-224)/2; 
    
    var features="left="+l+",top="+t+",width="+w+",height="+h+",toolbar=0,status=1,location=" + CheckBrowserType();
    
    var popupWindow = window.open(url, name, features);
    return popupWindow;
}

/* This function gets called on click of the 'Live Help' link in the 
 * footer across Shop and Secure pages. This function call has been 
 * written in the GlobalFooterContainer.htm container.
 */
function DecideDestinationUrl(aboutUrl)
{
    
    /* If the method call is from Shop or About pages then redirect the customer 
     * to the Live Help Default Landing page else call the Click() method to 
     * initiate the pop-up based chat client.
     */
    if(0 <= document.location.href.indexOf("secure.dev.nordstrom.com") || 
                0 <= document.location.href.indexOf("secure.nordstrom.com"))
    {
        Click();
    }
    else
    {
        document.location.href = aboutUrl + "help/livehelp/default.asp";
    }
}

var defaultChat;
function Click()
{   
	var currentURL;
	var nextURL;
	var origin;
	
	// Check if the pop-up window is undefined or closed, then display the pop-up window. 
	if (null == defaultChat || defaultChat.closed)	
	{		
		nextURL = ChatURL + "livehelpstart.asp";
		currentURL = document.location.href;
        origin = GetOriginValue(currentURL);	
        
        /* If customer clicks on 'Live Chat' link in asp pages under Secure website,
         * then change the origin tag's value.
         */
        if (0 <= currentURL.toLowerCase().indexOf("/services/"))
		{
		    origin = "FS";
		}	
		nextURL = nextURL + "?uri=" + escape(currentURL) + "&origin=" + origin;
		
		/* If the method has been invoked from the chat pop-up window then
		 * the pop-up page should be redirected to the LiveHelpStart.asp.
		 */
		if (0 <= currentURL.indexOf("/livehelp/livehelpstart.asp"))
		{
		    document.location.replace(nextURL);
		}
		else
		{
		
			/* Open the pop-up based chat window with the mentioned parameters.
		     * The window name should be different in order to open the 
		     * same pop-up on every call to this function.
		     */
		    defaultChat = OpenChatPopupWindow(nextURL, "defaultChat");
		    defaultChat.focus(); 
		}
	}
	
	// If the pop-window is already opened then focus it.
	else
	{
		defaultChat.focus(); 
	}
}

var beautyChat;
function BeautyClick()
{
	var currentURL;
	var nextURL;
	var origin;
	if (null == beautyChat || beautyChat.closed)	
	{		
		nextURL = ChatURL + "livehelpstart.asp?contact=beauty";
		currentURL = document.location.href;
		origin = GetOriginValue(currentURL);
		
        /* If customer clicks on 'Live Chat' link in beautyhotline page under Secure website,
         * then change the origin tag's value.
         */
		if (0 <= currentURL.toLowerCase().indexOf("/services/beautyhotline.asp"))
		{
		    origin = "BHL";
		}				
		nextURL = nextURL + "&uri=" + escape(currentURL) + "&origin=" + origin;
		beautyChat = OpenChatPopupWindow(nextURL, "beautyChat");				
		beautyChat.focus();
	}
	else
	{
	    beautyChat.focus();
	}
}

/* This method created to display the chat pop-up window when the user clicks
 * on Chat button/links.
 */
var designerChat;
function DesignerChatPopUp(strNextURL,strConcatinationOperator,strcurrentURL)
{
	var currentURL;
	var nextURL;
	var ConcatinationOperator;
	var BoutiqueID ;
	var currentURL ;
	var origin;
	
	currentURL = strcurrentURL;
	ConcatinationOperator = strConcatinationOperator ;
	
	if (null == designerChat || designerChat.closed)
	{		
		nextURL = strNextURL;
		origin = GetOriginValue(currentURL);
		
		/* If customer clicks on 'Live Chat' link in designer_contact page under Secure website,
         * then change the origin tag's value.
         */
		if (0 <= currentURL.toLowerCase().indexOf("/services/designer_contact.asp"))
		{
		    origin = "Designer";
		}
		nextURL = nextURL + ConcatinationOperator + "uri=" + escape(currentURL) + "&origin=" + origin;		
		designerChat = OpenChatPopupWindow(nextURL, "designerChat");		
		designerChat.focus();
	}
	else
	{
	    designerChat.focus();
	}
}

// This method modified to only redirect to the designer_contact.asp page.
function DesignerRedirect(strNextURL,strConcatinationOperator,strcurrentURL)
{
	var currentURL;
	var nextURL;
	var ConcatinationOperator;
	var BoutiqueID ;
	var currentURL ;
	
	currentURL = strcurrentURL;
	ConcatinationOperator = strConcatinationOperator ;
	
	document.cookie = "LCS=on;path=/;domain=.nordstrom.com";
	nextURL = strNextURL;
	nextURL = nextURL + ConcatinationOperator + "uri=" + escape(currentURL);			
	document.location.href=nextURL;	
}

function DesignerClick(strFlashID)
{
	var nextURL;
	var concatinationOperator;
	var FlashID ;
	
	FlashID = strFlashID;
	
	nextURL = ChatURL + "livehelpstart.asp?contact=designer";
	concatinationOperator = "&"

	if(FlashID!=null)
	{
		currentURL = document.location.href ;
		if (currentURL.indexOf("FlashID")== -1)
		{
			if (currentURL.indexOf("?")== -1)	
			{
				currentURL = currentURL + '?' + FlashID;								
			}
			else
			{
				currentURL = currentURL + '&' + FlashID;			
			}
		}
		else
		{
			currentURL = currentURL.substring(0, currentURL.indexOf("?"))
			currentURL = currentURL + '?' + FlashID;			
		}
	}
	else
	{		
		currentURL = document.location.href;		
	}

    // Call this method to display the chat pop-up window.
	DesignerChatPopUp(nextURL,concatinationOperator,currentURL);
}

function DesignerEmailClick()
{
	var nextURL;
	var concatinationOperator;
	var  currentURL;
	
	nextURL = getSecureURL() + "services/designer_contact.asp";
	concatinationOperator = "?"
	currentURL = document.location.href;
	DesignerRedirect(nextURL,concatinationOperator,currentURL);
}

function doResize() {
   if(navigator.appName.indexOf("Netscape")!=-1 && window && window.location && window.location.href) {
     window.location.href=window.location.href;
   }
 }

function clearLastLocation() {
document.cookie = "lastLocation=;path=/;domain=.nordstrom.com";
}


function LiveHelpCheck()
{
	if (window.name=='main_frame') {
		return true;
	}
	else {
		return false;
	}
}

function open_popup (strURL) {
window.name = "popup";
var feature = window.open(strURL,'feature','toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=461,height=430');
}


function validateForm() {
  var nameField, newStr;
  nameField = document.loginguest.elements[0].value;
  newStr = "";
  for (var i = 0; i < nameField.length; i++) {
    var c = nameField.charAt(i);
    if (c == ' ') {
      newStr += "_";
    } else {
      newStr += nameField.charAt(i);
    }
  }

  document.loginguest.elements[0].value = newStr;

  if (isValidUsername(document.loginguest.elements[0], "Name")) {
    if (!vchat()) return false;
    document.loginguest.elements[5].value += "&question=" + escape(document.loginguest.question.value);
    document.loginguest.elements[0].value = stripunderscore(document.loginguest.elements[0].value);
    return true;
  } else return false;
}		

function stripunderscore(value) {
	var nameField, newStr;
	var temp;
	nameField = value;
	newStr = "";
	for (var i = 0; i < nameField.length; i++) {
		var c = nameField.charAt(i);
		if (c == '_') {
			newStr += " ";
		} else {
			newStr += nameField.charAt(i);
		}
	}
	return newStr;
}

function isValidUsername(field, desc) 
    {
        if (isProvided(field, desc)) 
        {
            var s = field.value;
            if (s.length < 2) 
            {
                alert('The value in "' + desc + '" must be at least 2 characters long.');
                return false;
            }
            for (var i = 0; i < s.length; i++) 
            {
                var c = s.charAt(i);
                if ((c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && (c < '0' || c > '9') && c != '-' && c != '.' && c != '_' && c != '\'') 
                {
                    alert('The value in "' + desc + '" may only contain alphabetic and numeric characters and \'-\'.');
                    return false;
                }
            }
            var firstOne = s.charAt(0);
            if (firstOne >= '0' && firstOne <= '9')
            {
            	alert("We're sorry, all names must begin with a letter of the alphabet. Please try again.");
            	return false;
            }

            return true;
        }
        else return false;
    }

    function isProvided(field, desc) 
    {
        var s = field.value;        
        if (s && s.length) return true;
        if (desc == 'Question')
						alert ('Please enter a question and click SEND to communicate with one of our customer service agents.');
				else
						alert('The value in "' + desc + '" may not be empty.');
        return false;
    }

function encodeQuestion(strQuestion)
{
    // Encode question string and ensure final result is <= 255
    if (strQuestion.length>255)    
    {
        strQuestion    = strQuestion.substring(0,255);
        alert("Question is too long.  The question is truncated to 250 characters.");
    }

    var    quest =    unescape(strQuestion); // in case the question has already been encoded
    var    string = "";

    for (var i = 0; i < quest.length && string.length < 250; i++) // don't want encoded string to exceed 255
    {
	 if ((quest.charAt(i) == "\n") && (quest.charAt(i+1) == "\r" )) { // cr/lf (PCs)
	    string += " ";
	    i += 1;
        }
	else if (quest.charAt(i) == "\n") // cr (Macs)
	    string += " ";
	else if (quest.charAt(i) == "\r") // lf (Unix)
	    string += " ";
	else if (quest.charAt(i) == "%")  // hoses customer client @rec-990518 (remove for 2.4!!!)
	    string += " ";
        else 
            string += quest.charAt(i);
    } // endfor
    string = escape(string);
    if (string.length>240)
    {
      string = string.substring(0,240);
    }
    return string;
}

function vchat() 
{
	var strQuestion;
	strQuestion = document.loginguest.elements[1].value;
	if (strQuestion.length > 240)
	{
        alert("Question is too long.  The question should be truncated to 250 characters or less.");
		return false;
    }
    document.loginguest.elements[2].value = encodeQuestion(document.loginguest.elements[1].value);
    return isProvided(document.loginguest.elements[1], 'Question');
}

function endSession(returnUrl) {
  var LastLoc
			document.cookie="cc=0;path=/;domain=.nordstrom.com";
			document.cookie = "LCS=0; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/;domain=.nordstrom.com";
			LastLoc = unescape(returnUrl);
			if  (LastLoc=="" || LastLoc=="null" ||LastLoc==0)
			{
			LastLoc = "http://www.nordstrom.com";
			}

			document.cookie = "LCSLastLoc=0; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/;domain=.nordstrom.com";		
			top.location.href=LastLoc; 
    }