

var sBaseURL = "http://www.tesco.com";
var sSecureURL = "https://secure.tesco.com";

// urlData is used when checkUrl is not used from a <a/> tag. urlData acts as a fake anchor.
function urlData(doc, href, target) {
	if(!doc) {
		this.href = href;
	} else {
		this.href = doc.location.protocol + "//" + doc.location.host;
		if(href.charAt(0) != "/") {
			this.href += "/";
		}
		this.href += href;
	}
	this.target = target;
}

function checkUrl(o, secure, showButtons, win) {
	if((showButtons == "undefined") || (showButtons ==null)) {
		showButtons = true;
	}

	if(secure && (document.location.protocol != "https:")) {
		return _makeURLSecure(o, true, showButtons, win);
	} else if(!secure && (document.location.protocol == "https:")) {
		return _makeURLSecure(o, false, showButtons, win);
	} else {
		var oDoc;
		if(!win) {
			oDoc = document;
		} else {
			oDoc = win.document;
		}

		if(showButtons && !oDoc.appFrames.topNav().document.showButtons) {
			return _makeURLSecure(o, (document.location.protocol == "https:"), true, win, true);
		} else if(!showButtons && oDoc.appFrames.topNav().document.showButtons) {
			return _makeURLSecure(o, (document.location.protocol == "https:"), false, win, true);
		}
		
		if (o.target == "mainContent") {
			// Are we in full basket mode? In that case, go back to normal mode
			if (oDoc.appFrames.pBasket() && oDoc.appFrames.pBasket().location.href.indexOf("mode=full") > -1) {
		 		oDoc.appFrames.pBasket().location.href = "/superstore/frames/basket.asp";
			}
			if(!oDoc.appFrames.mainContent()) {
				oDoc.appFrames.main().location.href = "/superstore/frames/main.asp?url=" + escape(o.href);
				return false;
			}
		} else if((o.target == "mainContentRight") && (!oDoc.appFrames.mainContentRight())) {
			if(o.id == "searchForm") {
				var o = oDoc.getElementById("search");
				if(o) {
					oDoc.appFrames.main().location.href = "/superstore/frames/main.asp?url=" + escape("/superstore/frames/mainContent.asp?main=" + escape("/superstore/product/search.aspx?search=" + escape(escape(o.value.URLEncode()))));
				}
				return false;
			} else if(o.href) {
				oDoc.appFrames.main().location.href = "/superstore/frames/main.asp?url=" + escape("/superstore/frames/mainContent.asp?main=" + o.href);
				return false;
			}
		}
	}
	
	return true;
}

function _makeURLSecure(o, secure, showButtons, win, forceTopFrame) {
	var sDomain = sBaseURL;
	var sOrigDomain = sSecureURL;
	if(secure) {
		sDomain = sSecureURL;
		sOrigDomain = sBaseURL;
	}
	if(forceTopFrame) {
		sOrigDomain = sDomain;
	}
	var oDoc;
	if(!win) {
		oDoc = window.top;
	} else {
		oDoc = win.top;
	}

	var sShowButtons = "";	
	if(!showButtons) {
		sShowButtons = "buttons=0&";
	}

	if (o.target == "mainContent") {
		oDoc.location.href = sDomain + "/superstore/default.asp?" + sShowButtons + "url=" + escape("/superstore/frames/main.asp?url=" + escape(o.href.substring(sOrigDomain.length)));
	} else if(o.target == "mainContentRight") {
		if(o.id == "searchForm") {
			var sSearch = "";
			var oSearch = document.getElementById("search");
			if(oSearch) {
				sSearch = escape(oSearch.value);
			}
			oDoc.location.href = sDomain + "/superstore/default.asp?" + sShowButtons + "url=" + escape("/superstore/frames/main.asp?url=/superstore/frames/mainContent.asp?main=" + escape("/superstore/product/search.aspx?search=" + escape(escape(escape(sSearch)))));
		} else if(o.href) {
			oDoc.location.href = sDomain + "/superstore/default.asp?" + sShowButtons + "url=" + escape("/superstore/frames/main.asp?url=/superstore/frames/mainContent.asp?main=" + o.href);
		}
	} else {
		oDoc.location.href = sDomain + "/superstore/default.asp?" + sShowButtons + "url=" + escape(o.href.substring(sOrigDomain.length));
	}
	
	return false;
}


