function LinkBuilderForLink(pageName, storeId, catalogId, commerceViewName, options, directMethod)
{
// This redirects the page to a new url.  
	SiteAreaView=commerceViewName;
	var domainName = document.domain;
	var currentURL = document.URL;
	var flatten = "yes";
	

	if (commerceViewName == '') { 
		// Set SiteAreaView based upon the Site Area of page being linked to.  This will determine which JSP should be used to display the WCM content.
		// Note: The pageName must be in the format eg. /site_area1/page name... 
		SiteAreaView = 'GenContent';
		if (pageName == '/SearsOpticalUS/StoreFinder/StoreFinder') {SiteAreaView = 'SOLocatorFormView';flatten="no"};	//exception for FaceShape selector	
	}

	var wcmpageName = pageName;
		
	if (domainName == '10.250.0.61')  // WWCM server. Used when previewing WCM content
		{outStr='/wps/wcm/myconnect' + pageName}
	else if (flatten == "yes")
			{outStr =  'http://www.searsopticalenespanol.com/searsoptical/enes/24/_www_searsopticalcontacts_com/webapp/wcs/stores/servlet/' + SiteAreaView + '|-1|' + storeId + '|' + catalogId + '|' + wcmpageName}
		else
			{outStr =  'http://www.searsopticalenespanol.com/searsoptical/enes/24/_www_searsopticalcontacts_com/webapp/wcs/stores/servlet/' + SiteAreaView + '?langId=-1&storeId=' + storeId + '&catalogId=' + catalogId + '&pageName=' + wcmpageName }
	
	if (commerceViewName=='UserRegistrationForm')
		{outStr =  'http://www.searsopticalenespanol.com/searsoptical/enes/24/_www_searsopticalcontacts_com/webapp/wcs/stores/servlet/UserRegistrationForm?langId=-1&storeId=' + storeId + '&catalogId=' + catalogId + '&new=Y'};
		
	
	if (pageName == "#") {outStr = '#'};  // Used for tempory links when the page name is not know

	if (directMethod == "redirect")
		{ window.location=outStr }
	else
		{ document.write('<a href="' + outStr + '" ' + options + '>') };
}

function WCM_link(pageName)
// Used to in an onclick in a href. e.g <a href="javascrip:WCM_link('/Product/SearsOptical_Products_Kids_Frames')">
{
       // Strip off the WCM generated path, e.g. /wps/wcm/connect	
	var WCMPathPos = pageName.indexOf('wps/wcm');
	var WCMPath=pageName;
	if (WCMPathPos > 0)
		{ WCMPath = pageName.substring(WCMPathPos + 36, pageName.length) };

	LinkBuilderForLink(WCMPath, getstoreId(), getcatalogId(), '', '', 'redirect');
}


function customJspLink (commerceViewName) 
// Used to in an onclick in a href to Custom JSP Pages. e.g <a href="javascript:customJspLink('UserRegistrationForm')">
{
	LinkBuilderForLink('', getstoreId(), getcatalogId(), commerceViewName, '','redirect');	
}


function getstoreId(){
	var url = document.URL;
	var pageNameArr;
	var storeId="";
	if (url.indexOf('|') > 0) {pageNameArr=url.split("|");storeId=pageNameArr[2]};  // get for flatten URLs with pipe
	if (url.indexOf('%7C') > 0) {pageNameArr=url.split("%7C");storeId=pageNameArr[2]};  //get for flatthen URLs with encoded pipe
	
	var storeIdPos=url.indexOf('storeId=');    //get for unflatten URLs
	if (storeIdPos > 0) {storeId=url.substring(storeIdPos+8, storeIdPos+13)	};
	return storeId;
}

function getcatalogId(){
	var url = document.URL;
	var pageNameArr;
	var categoryId="";
	if (url.indexOf('|') > 0) {pageNameArr=url.split("|");categoryId=pageNameArr[3];};  // get for flatten URLs with pipe
	if (url.indexOf('%7C') > 0) {pageNameArr=url.split("%7C");categoryId=pageNameArr[3];};  //get for flatthen URLs with encoded pipe
	
	var categoryIdPos=url.indexOf('catalogId=');  // get for unflatten URLs
	if (categoryIdPos > 0) {categoryId=url.substring(categoryIdPos+10, categoryIdPos+15)};
	return categoryId;
}


function printPage()
{
	var url = document.URL;
	if (url.indexOf('|') > 0 || url.indexOf('%7C') > 0) 
		{url += "|Print=On"}
	else
		{if (url.indexOf('?')>0) {url += "&Print=On"} else {url += "?&Print=On"}};
	printWindow=window.open(url);
	if (window.focus) {printWindow.focus()}
	return false;
}