var url = location.href;
var arrRedirectFrom;
var redirectTo;
var isProblem=false;

/* site 1 Start */
if (!isProblem)
{
	arrRedirectFrom = new Array ("http://presidentschoice.ca","http://presidents-choice.ca");
	redirectTo = "http://www.presidentschoice.ca";
	isHostNameOnly = false;
	isProblem = domainRedirect (url, arrRedirectFrom, redirectTo, isHostNameOnly);
}
/* site 1 End */

/* site 1 with SSL Start 
if (!isProblem)
{
	arrRedirectFrom = new Array ("https://test1",
								 "https://test2");
	redirectTo = "https://localhost";
	isHostNameOnly = false;
	isProblem = domainRedirect (url, arrRedirectFrom, redirectTo, isHostNameOnly);
}
 site 1 with SSL End */

function domainRedirect(url, arrRedirectFrom, redirectTo, isHostNameOnly){
	redirectToHostName = getHostName(redirectTo);
	for (var c=0;c<arrRedirectFrom.length;c++)
	{
		redirectFromHostName = getHostName(arrRedirectFrom[c]);
		// check whether redirectFrom host name is the same as redirectTo value
		if (redirectFromHostName == redirectToHostName)
		{
			alert ("domainRedirect problem: No value in arrRedirectFrom is the same as redirect To value" + "\n" + "redirectTo value: " + redirectTo);
			return true;
		}
		else if (getHostName(url) == redirectFromHostName)
		{
			rExp = new RegExp (arrRedirectFrom[c], "i");
			url = url.replace (rExp, redirectTo);
	
			if (isHostNameOnly) url = redirectTo;
			location.replace (url);
		}
	}
}

function getHostName(url)
{
	var hostname = location.hostname;
	var part1Length = 0;
	part1pos=url.indexOf("http://");
	if (part1pos == 0) part1Length = 7;
	part1pos=url.indexOf("https://")
	if (part1pos == 0) part1Length = 8;
	part2pos=url.indexOf("/", part1Length);
	if (part2pos<0) part2pos = url.length;
	hostLength=part2pos-part1Length;
	return url.substr(part1Length, hostLength);
}

function popup_window_PC(url, name, width, height)
{
 var wnd = window.open(ROOT + url + LANGUAGE + "/", name, 'width=' + width + ',height=' + height + ',location=no,menubar=no,status=no,toolbar=no,resizable=yes,scrollbars=yes');
 wnd.focus();
}

function popup_questions()
{
	var wnd = window.open(ROOT + '/AboutPC/Questions/' + LANGUAGE + '/', 'pcQuestions', 'width=740,height=580,location=no,menubar=no,status=no,toolbar=no,resizable=no,scrollbars=no');
	wnd.focus();
}

function popup(url)
{
	var wnd = window.open(ROOT + url, 'pcPopup', 'width=570,height=400,location=no,menubar=no,status=no,toolbar=no,resizable=no,scrollbars=yes')
	wnd.focus();
}

function popup_join(url)
{
	var wnd = window.open('http://www.banking.pcfinancial.ca/en_ca/redirect/preschoice/why_join.html', 'joinPopup', 'width=420,height=570,location=no,menubar=no,status=no,toolbar=no,resizable=no,scrollbars=yes')
	wnd.focus();
}

function popup_links()
{
	var wnd = window.open(ROOT + '/AboutPC/OtherLinks/' + LANGUAGE + '/', 'pcLinks', 'width=660,height=440,location=no,menubar=no,status=no,toolbar=no,resizable=no,scrollbars=no');
	wnd.focus();
}

function popup_letusknow()
{
	var wnd = window.open(ROOT + '/AboutPC/Questions/' + LANGUAGE + '/letusknow.aspx', 'pcQuestions', 'width=740,height=580,location=no,menubar=no,status=no,toolbar=no,resizable=no,scrollbars=no');
	wnd.focus();
}

function popup_addrecipe(id)
{
	if(!AUTHENTICATED)
	{
		window.location = ROOT + '/Insider/RecipeBox/Login.aspx';
		return;
	}
	
	var wnd = window.open(ROOT + '/Insider/RecipeBox/Add.aspx/id/' + id, 'pcRecipeAddPopup', 'width=380,height=550,location=no,menubar=no,status=no,toolbar=no,resizable=no,scrollbars=yes')
	wnd.focus();
}

function popup_addrecipelist(id)
{
	popup_addlist('RecipeId', id);
}

function popup_addproductlist(id)
{
	popup_addlist('ProductId', id);
}

function popup_addlist(type, id)
{
	if(!AUTHENTICATED)
	{
		window.location = ROOT + '/Insider/ShoppingList/Login.aspx';
		return;
	}
	
	var wnd = window.open(ROOT + '/Insider/ShoppingList/Add.aspx/' + type + '/' + id, 'pcListAddPopup', 'width=380,height=550,location=no,menubar=no,status=no,toolbar=no,resizable=no,scrollbars=yes')
	wnd.focus();
}

function popup_emailrecipe(id)
{
	popup('/FoodAndRecipes/Recipes/Send.aspx/id/' + id);
}

function popup_emailproduct(id, catid)
{
	popup('/FoodAndRecipes/GreatFood/Send.aspx/id/' + id + '/catid/' + catid);
}

function popup_minichefs()
{
	var wnd = window.open('http://www.presidentschoice.ca/' + LANGUAGE + '/minichefs/', 'pcMiniChefs', 'width=790,height=510,location=no,menubar=no,status=no,toolbar=no,resizable=yes,scrollbars=yes');
	wnd.focus();
}
function popup_window_PC(url, name, width, height)
{
	var wnd = window.open(ROOT + url, name, 'width=' + width + ',height=' + height + ',location=no,menubar=no,status=no,toolbar=no,resizable=yes,scrollbars=yes');
	wnd.focus();
}

function popupBM()
{
	if (LANGUAGE == "en")
		popup_window_PC("/bm/blue_menu.html", "bluemenu", 520, 400)
	else
		popup_window_PC("/bm/blue_menu_fr.html", "bluemenu", 520, 400)
}

function CheckPCMobileFrench(){
	//if (LANGUAGE == 'fr'){
	//	location.href = ROOT + "/PCTelecom/PCMobile/ComingSoon.aspx";
	//}
}

//sets the  center column div to be visible
//NOTE: Center Column div is an element in al pages that have overflow = hidden,
//		Some pages have problems with this overflow when load by browsers different to IE.
function SetCenterColumnOverflowVisible(){
	//over write the center column
	centerColumn = document.getElementById('CenterColumnDiv');
	if (centerColumn != null){
		centerColumn.style.overflow = 'visible';
	}
}


function popupGM(url)
{
	var wnd = window.open(ROOT + url, 'pcPopup', 'width=700,height=550,location=no,menubar=no,status=no,toolbar=no,resizable=no,scrollbars=yes');
	wnd.focus();
}


function popup_emailproductGM(id, catid)
{
	popup('/HouseAndHome/Send.aspx/id/' + id + '/catid/' + catid);
}


function popup_ImageView(id,Type)
{
	popupGM('/HouseAndHome/ProductImageView.aspx/id/' + id + '/Type/'+ Type);
}
function popup_RecipeImageView(id, image)
{
	popupGM('/FoodAndRecipes/Recipes/RecipeImageView.aspx/id/' + id + '/image/' + image);
}
function popup_ProductImageView(id, image)
{
	popupGM('/FoodAndRecipes/GreatFood/ProductImageView.aspx/id/' + id + '/image/' + image);
}
function popup_StoreLocator (language)
{
	if (window.screen) {
        var clientW = Math.floor(screen.availWidth);
        var clientH = Math.floor(screen.availHeight);
       }

	var w=590;
	var h=450;
    Window=window.open("http://storelocator.presidentschoice.ca/GMStoreSearchOptionsBanner.aspx?site=GM&lang="+language.toUpperCase(),'Window','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width='+w+',height='+h+',top='+((clientH-h)/2)+',left='+((clientW-w)/2));
	Window.focus();
}

function linkredirect(relativePath)
	{
		var Redirect = 'http://' + location.host + relativePath;
		window.location = Redirect;
		return;
	}
	
function IncludeFile(filelocation)
	{
		//var Path = 'http://localhost/pc.web.user' + filelocation;
		var Path = 'http://' + location.host + filelocation;
		document.write("<script src='" + Path + "'></script>");
	}

function popupGeneral(url)
{
	var wnd = window.open(url, 'pcPopup', 'width=570,height=400,location=no,menubar=no,status=no,toolbar=no,resizable=no,scrollbars=yes')
	wnd.focus();
}

function Enlarge(obj)
{

	var index = obj.src.lastIndexOf("/");								
	var smallImg = obj.src.slice(index + 1);
	var largeImg = smallImg.replace(".", "_large.");
	var urlbase = obj.src.substr(0,index + 1);
	var largeUrl = urlbase + largeImg;		
	
	popupGeneral(largeUrl);
	
}
