function LinkBuilderForLink(PageName, StoreId, CatalogId, CommerceViewName, Options, DirectMethod)
{
// This redirects the page to a new url.  It is needed because the same content is used in 4 different environments, which are
// US Domain, CA Domain, WWCM Preview, and Local host (used for development).
	SiteAreaView=CommerceViewName;
	var storeId=getStoreId();
	var DomainName = document.domain;
	var currentURL = document.URL;	
	var flatten = 'yes';
	var flattenServer = "lenscrafters";
var PageNameArr=PageName.split("/");
	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... 
		
		if (PageNameArr[1].toLowerCase() == 'footer') {SiteAreaView = '7'};
		if (PageNameArr[1].toLowerCase() == 'products') {SiteAreaView = '4'};
		if (PageNameArr[1].toLowerCase() == 'home') {SiteAreaView = 'designer-eyeglass-frames-sunglasses-and-contact-lenses'};
		if (PageNameArr[1].toLowerCase() == 'style') {SiteAreaView = '5'};
		if (PageNameArr[1].toLowerCase() == 'service') {SiteAreaView = '6'};
		if (PageName == '/Style/LensCrafters_Style_Face_Shape') {SiteAreaView = 'whats-your-face-shape-find-eyeglasses-to-flatter-your-face'; flatten = 'no'};	//exception for FaceShape selector	
	}
	else
	{ 	switch (CommerceViewName.toLowerCase())   // For the custom JSPs, determine if the URL needs to be flatten and if the server is eppointments
		{
			case 'logonform' : flatten = 'no'; flattenServer = 'lenscrafters'; break;
			case 'offersview': flatten = 'no'; flattenServer = 'lenscrafters'; break;
			case 'storelocatorscheduleexam': flatten ='no';flattenServer = 'lenscrafters';break;
			case 'storelocatorfullsearchview':flatten='no';flattenServer='lenscrafters';break;
			default: flatten = 'no'; flattenServer = 'eppointments'; break;
		}
	}
		
	if (flattenServer == 'eppointments')	
	{
		switch (DomainName.toLowerCase())
		{
			case 'www.lenscrafters.com' : DomainName = 'www.eyeappts.com'; break;         // Eppointments domain on production
			case 'stage.lenscrafters.com' : DomainName = 'stage.eyeappts.com'; break;   // Eppointments domain on staging			
			case 'int.lenscrafters.com': DomainName = 'int.eyeappts.com'; break;  // Eppointments domain on Integration
			case 'luxdev.lenscrafters.com': DomainName = 'luxdev.eyeappts.com'; break;  // Eppointments domain on Integration
		}
	}
	wcmDomain = 'Lens_US';
	if (currentURL.indexOf('Lens_CA') > 0 || currentURL.indexOf('.ca') > 0) {wcmDomain = 'Lens_CA'};
	var wcmPageName = PageName;
	if (DomainName =='10.250.0.16')  // WWCM server. Used when previewing WCM content
		{OutStr='/wps/wcm/myconnect/Luxottica/LensCrafters/' + wcmDomain + PageName}
	else if (flatten == 'no') {
		var urlOptions="";
		if (SiteAreaView.toLowerCase()=="storelocatorscheduleexam") {urlOptions="&navPath=eyeExam&navCategory=Logon";SiteAreaView="store-locator-find-lenscrafters-stores-near-you"} 
		if (PageName != "") {wcmPageName = '&pageName=/LensCrafters/' + wcmDomain + PageName};
		OutStr = 'http://' + DomainName + '/eyeglasses/' + SiteAreaView + '/'+ PageName + '/' +urlOptions;

		if (PageName == "" && urlOptions == "")
			OutStr = 'http://' + DomainName + '/eyeglasses/' + SiteAreaView;

		if (SiteAreaView.toLowerCase()=="storelocatorfullsearchview"){OutStr = 'http://' + DomainName + '/eyeglasses/store-locator-find-lenscrafters-stores-near-you'}
		else if(SiteAreaView.toLowerCase()=="offersview"){OutStr = 'http://' + DomainName + '/eyeglasses/coupons-discounts-and-offers'}
		else if(SiteAreaView.toLowerCase()=="logonform"){OutStr = 'http://' + DomainName + '/eyeglasses/my-lenscrafters-sign-in-register-to-a-lenscrafters-online-account'};}
	else  
		{
		if (PageName != "") 
		{
			PageName = PageNameArr[2].toLowerCase();
			if(PageNameArr.length > 3 )
			{
				PageName = PageName+'/'+PageNameArr[3].toLowerCase();				
			};
		};		 
		OutStr = 'http://' + DomainName + '/eyeglasses/' + SiteAreaView +'/'+ PageName;
		if(SiteAreaView == 'designer-eyeglass-frames-sunglasses-and-contact-lenses') 	//homepage
			OutStr = 'http://' + DomainName + '/eyeglasses/' + SiteAreaView
		
	};

	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/LensCrafters_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="javascrip:WCM_link('StoreLocatorView')">
{
	LinkBuilderForLink('', getStoreId(), getCatalogId(), CommerceViewName, '','redirect');	
}

function nav_url_builder(PageName, NavName)
{
// This function is used to build the Navigator links.  This function will write class="on" if the user is on the page that the 
// navigator item is part of.
// Nav_Category is set in the content page.
// PageName is the location of the page. e.g. Products/Kids_Frames
// NavName is the name of the navigator.  e.g. Kids Frames.

	if (Nav_Category == NavName) 
		{LinkBuilderForLink(PageName, getStoreId(), getCatalogId(), '', 'class="on"', 'DocWrite')} 
	else 
		{LinkBuilderForLink(PageName, getStoreId(), getCatalogId(), '', '', 'DocWrite')};
}

function WCM_url(PageName)
// Used to build a URL link with that does not have any attributes in the anchor
{

       // Strip off the WCM generated path, e.g. /wps/wcm/connect	
	var WCMPathPos = PageName.indexOf('wps/wcm');

	var OffsetPos = PageName.indexOf('wps/wcm/connect');
	if (OffsetPos > 0)
		{OffsetNum = 36}
	else
		{OffsetNum = 38};
		
	var WCMPath=PageName;
	if (WCMPathPos > 0)
		{ WCMPath = PageName.substring(WCMPathPos + OffsetNum, PageName.length) };

	LinkBuilderForLink(WCMPath, getStoreId(), getCatalogId(), '', '', 'DocWrite');
}



function customJspUrl (CommerceViewName) {
	LinkBuilderForLink('', '', getStoreId(), getCatalogId(), CommerceViewName, '', 'DocWrite');	
}

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)	
	}
	else if (location.hostname=='www.lenscrafters.com' || location.hostname=='stage.lenscrafters.com' || location.hostname=='origin.lenscrafters.com' || location.hostname=='int.lenscrafters.com' || location.hostname=='luxdev.lenscrafters.com'){
		storeId=11151;
	}
	else if (location.hostname=='www.lenscrafters.ca' || location.hostname=='stage.lenscrafters.ca' || location.hostname=='int.lenscrafters.ca' || location.hostname=='luxdev.lenscrafters.ca'){
		storeId=11152;
	}
	return storeId;
}

function getCatalogId(){
	var url = document.URL;
	var PageNameArr;
	var catalogId="";
	if (url.indexOf('|') > 0) {PageNameArr=url.split("|");catalogId=PageNameArr[3];};  // get for flatten URLs with pipe
	if (url.indexOf('%7C') > 0) {PageNameArr=url.split("%7C");catalogId=PageNameArr[3];};  //get for flatthen URLs with encoded pipe
	
	var catalogIdPos=url.indexOf('catalogId=');  // get for unflatten URLs
	if (catalogIdPos > 0) {
		catalogId=url.substring(catalogIdPos+10, catalogIdPos+15)
	}
	else if (location.hostname=='www.lenscrafters.com' || location.hostname=='stage.lenscrafters.com' || location.hostname=='origin.lenscrafters.com' || location.hostname=='int.lenscrafters.com' || location.hostname=='luxdev.lenscrafters.com'){
		catalogId=10051;
	}
	else if (location.hostname=='www.lenscrafters.ca' || location.hostname=='stage.lenscrafters.ca' || location.hostname=='int.lenscrafters.ca' || location.hostname=='luxdev.lenscrafters.ca'){
		catalogId=10052;
	}
	return catalogId;
}

function setCALogo()
// Changes the logo to the Canadian
{
	currentURL = document.URL;
	if (currentURL.indexOf("Lens_CA") > 0 || currentURL.indexOf(".ca") > 0 || currentURL.indexOf("11152") > 0)
	{
	   logoObj = document.getElementsByName('logo');
	   logoObj[0].id='logo_ca';
	}
}

function attributeSearch(searchAttrType, searchValue, gender, title, frameType, categoryId)
// Builds the attribute URL
// frameType - determines if Frames or Sunglasses should be searched.  sg - Sunglasses  fr - Frames
//searchAttrtype - material, color, build, etc.  Can also be 'gender' which will search for all frame types in that gender
{
	var domainName = document.domain;
	var storeId=getStoreId();
	var catalogId=getCatalogId();
	var url=document.URL;
	var country="US";
	var OutStr = "";
	if (domainName.indexOf("lenscrafters.ca") > 0 || url.indexOf("10602") > 0) {country="CA"};
	if (gender=="") {
		gender="F";
		if (url.indexOf("mens-frames") > 0) {gender="M"}
	}
	if (typeof(frameType)=="undefined") {frameType="fr"};
	if (frameType.toUpperCase()=="FR") { 	
		genderFull="womens-frames";
		if (gender.toUpperCase()=="M") {genderFull="mens-frames"}
	}
	else
	{ 	genderFull="womens-sunglasses";
		if (gender.toUpperCase()=="M") {genderFull="mens-sunglasses"}
	}
	if (searchAttrType == "gender")
		{OutStr = 'http://' + domainName + '/eyeglasses/ProductGroupingSearchCmd?langId=-1&storeId=' + storeId + '&catalogId=' + catalogId + '&searchType=' + searchAttrType + '&site=' + country + '&gender=' + gender.toUpperCase() + '&navCategory=' + genderFull + '&title=' + title + '&prodType=' + frameType.toUpperCase() + '&categoryId=' + categoryId;}
	else
		{OutStr = 'http://' + domainName + '/eyeglasses/ProductGroupingSearchCmd?langId=-1&storeId=' + storeId + '&catalogId=' + catalogId + '&searchType=attrNameValue&attrName=' + searchAttrType + '&attrValue=' + searchValue + '&site=' + country + '&gender=' + gender.toUpperCase() + '&navCategory=' + genderFull + '&title=' + title + '&prodType=' + frameType.toUpperCase()+ '&categoryId=' + categoryId;}
	window.location=OutStr;	
}

function priceSearch(criteria, priceAmount, gender, title, frameType, categoryId)
// Builds the price search URL
// frameType - determines if Frames or Sunglasses should be searched.  sg - Sunglasses  fr - Frames
//searchAttrtype - material, color, build, etc.  Can also be 'gender' which will search for all frame types in that gender
{
	var domainName = document.domain;
	var storeId=getStoreId();
	var catalogId=getCatalogId();
	var url=document.URL;
	var country="US";
	var OutStr = "";
	if (url.indexOf("11152") > 0 || url.indexOf("10602") > 0) {country="CA"};
	if (gender=="") {
		gender="F";
		if (url.indexOf("MensFrames") > 0) {gender="M"}
	}
	if (typeof(frameType)=="undefined") {frameType="fr"};
	if (frameType.toUpperCase()=="FR") { 	
		genderFull="womens-frames";
		if (gender.toUpperCase()=="M") {genderFull="mens-frames"}
	}
	else
	{ 	genderFull="womens-sunglasses";
		if (gender.toUpperCase()=="M") {genderFull="mens-sunglasses"}
	}
	OutStr = 'http://' + domainName + '/eyeglasses/ProductGroupingSearchCmd?langId=-1&storeId=' + storeId + '&catalogId=' + catalogId + '&searchType=price' + '&attrName='+ criteria + '&attrValue=' + priceAmount + '&site=' + country + '&gender=' + gender.toUpperCase() + '&navCategory=' + genderFull + '&title=' + escape(title) + '&prodType=' + frameType.toUpperCase() + '&categoryId=' + categoryId;
	window.location=OutStr;	
}


function collectionUrl(gender,          //either m or f
			UScollectionId,   // the US ID of the collection
			CAcollectionId,   // the Canadian ID of the collection
			collectionType,   //either brands or collections
			productId,
			frameType)   // either fr - Frames or sg - Sunglasses       
// example URL - http://stage.lenscrafters.com/webapp/wcs/stores/servlet/category2|-1|11151|10051|10515|image|WomensFrames|10535|brands||
{
	var domainName = document.domain;
	var storeId=getStoreId();
	var catalogId=getCatalogId();
	var url=document.URL;

	
	if (typeof(frameType)=="undefined") {frameType="fr"};
	if (frameType.toUpperCase()=="FR") { 	
		genderFull="womens-frames";
		if (gender.toUpperCase()=="M") {genderFull="mens-frames"}
	}
	else
	{ 	genderFull="womens-sunglasses";
		if (gender.toUpperCase()=="M") {genderFull="mens-sunglasses"}
	}

	if (url.indexOf("Lens_CA") > 0 || url.indexOf(".ca") > 0 || url.indexOf("11152") > 0)
	{
	   if (gender.toLowerCase()=='m') {navId="10511"} else {navId="10510"};   // CA
	   collectionId=CAcollectionId;
	}
	else
	{
	   if (gender.toLowerCase()=='m') {navId="10514"} else {navId="10515"};    // US
	   collectionId=UScollectionId;
	}
	
	if (typeof(productId)=="undefined" || productId=="")
	{	var OutStr = 'http://' + domainName + '/eyeglasses/category2|-1|' + storeId + '|' + catalogId +'|'+ navId + '|image|' + genderFull + '|' + collectionId + '|' + collectionType;  }
	else
	{       var OutStr = 'http://' + domainName + '/eyeglasses/Product|-1|' + storeId + '|' + catalogId +'|'+ collectionId +'|'+ navId +'|'+ collectionType +'|'+ genderFull + '|' + productId;  }

	if (typeof(collectionType)!="undefined")
	{window.location=OutStr}
}

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;
}