// 10 Jul 2003: RC. Added new function GetCountryFromUrl(). Modified MM_jumpMenu() to call GetCountryFromUrl().
// 24 Jun 2003: RC. Synchronized with old version of this file that inadvertently crept onto site on 18 June 2003 7:20PM (Web Site time)
//  2 Jun 2003: RC. Synchronized with old version of this file that inadvertently crept into site. Added functions like SpecificationHistoryBarIT().
// 10 Sep 2002: RC. Added PutAdminSetupLink().
// 23 Aug 2002: RC. Modified PopulateSelectCountry() to set country if current country is not in users list of countries.
// 11 Aug 2002: RC. Added "BuyNowActive" cookie read to control buy now image display.
//  2 Aug 2002: RC. Added function PopulateSelectCountry().
//  5 Jul 2002: RC. Dynamic virtual path control.
//  4 Jul 2002: RC. Added function SetBuyNowStatus()
// 28 Jun 2002: RC. Added Buy Now control
var sconCanopus = "canopus";
var sconCountry = "country";
var sconBuyNowActive = "BuyNowActive";
var sVirtDir = sconCanopus;
var sMyCountry = null;
var s = StrTokIdx(document.location.pathname,"/",1);
if(s==null || s.toLowerCase() != sVirtDir)
	sVirtDir = "";
	
function GetCountryFromUrl(sURL) {
	var s = sURL;
	var sCtry = null;
	s = s.toLowerCase();
	// Find domain component - look for "// then "/"
	var i;
	if((i = s.indexOf("//")) >= 0 || (i = s.indexOf("www.")) == 0)
		{
		var iStart = i + 2;
		// Now look for "/" - if not found we go to end of string
		i = s.indexOf("/", iStart);
		if(i >= iStart)
			s = s.substr(iStart, i - iStart);
		else
			s = s.substr(iStart);
		if(s.indexOf(".au") > 0 || s.indexOf("-au") > 0)
			sCtry = "AU";
		else if(s.indexOf(".de") > 0 || s.indexOf("-de") > 0)
			sCtry = "DE";
		else if(s.indexOf(".it") > 0 || s.indexOf("-it") > 0)
			sCtry = "IT";
		else if(s.indexOf(".uk") > 0 || s.indexOf("-uk") > 0)
			sCtry = "UK";
		else if(s.indexOf(".us") > 0 || s.indexOf("-us") > 0)
			sCtry = "US";
		else if(s.indexOf(".jp") > 0 || s.indexOf("-jp") > 0)
			sCtry = "JP";
		else if(s.indexOf(".cn") > 0 || s.indexOf("-cn") > 0)
			sCtry = "CN";
		}
	if(sCtry == null)
		{
		s = sURL;
		var n = s.indexOf("_");
		if(n >= 0)
			{
			var m = s.indexOf(".", n);
			if(m == (n+3))
				sCtry = s.substring(n + 1, m).toUpperCase();
			}
		}
	return sCtry;	
	}

function GetCountry() {
	var sCtry = (sMyCountry!=null) ? sMyCountry : GetCookie (sconCanopus, sconCountry);
	if(sCtry == null)
		sCtry = "US";
	sMyCountry = sCtry.toUpperCase();
	return sMyCountry;
}

function SetCountry(sCtry) {
	if(sCtry == null)
		sCtry = "US";
	SetCookie(sconCanopus, sconCountry + "=" + sCtry);
}

function HomePageCtry(sCtry) {
	var sPage;
	if(sCtry == null)
		sCtry = GetCountry();
	if(sCtry == "US")
		sPage = "index.asp";
	else
		sPage = "index_" + sCtry + ".asp";
	return sPage.toLowerCase();
}

function PrefixVirtDir(s) {
	var s2;
	if(sVirtDir.length>0)
		{
		var c=s.substr(0,1);
		if(c==".")
			s2=s;
		else
			{
			s2="/"+sVirtDir;
			if(c!="/")
				s2+="/";
			s2+=s;
			}
		}
	else
		s2=s;
	return s2;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0, Calls PrefixVirtDir()
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=PrefixVirtDir(a[i]);}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0, Calls PrefixVirtDir()
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=PrefixVirtDir(a[i+2]);}
}

function MM_SelectMenu(selObj){ //v3.0
	var sPage = HomePageCtry(), sValue;
	for(var i = 0; i < selObj.length; ++i)
		{
		sValue = selObj.options[i].value;
		sValue = sValue.toLowerCase();
		if(sValue == sPage)
			selObj.selectedIndex = i;
		}
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
	//var sCtry = "US";
	var sVal = selObj.options[selObj.selectedIndex].value;
	var sCtry = GetCountryFromUrl(sVal);
/*	var n = sVal.indexOf("_");
	if(n >= 0)
		{
		var m = sVal.indexOf(".");
		if(m > n)
			{
			sCtry = sVal.substring(n + 1, m).toUpperCase();
			}
		}*/
	SetCountry(sCtry);
	eval(targ+".location='"+sVal+"'");
	if (restore) selObj.selectedIndex=0;
}

function AddToHistoryBar(sText, sURL) {
	var oTable = getObj("histbar");
	if(oTable)
		{
		var bInnerText = (getBrowser()==1);
		var oCollR = oTable.rows;
		for (var i=0; i < oCollR.length; i++)
			{
			var oCollC = oCollR[i].cells;
			for (var j=0; j < oCollC.length; j++)
				{
				var oCell = oCollC[j];
				if(( bInnerText && oCell.innerText.length > 0) ||
					(!bInnerText && oCell.innerHTML.indexOf("|") > 0))
					{
					var sNewHTML;
					sNewHTML =  "<span class='nav_strip_arrow_red'> &gt; </span>";
					sNewHTML += "<span class='linkblue'>";
					if(sURL)
						sNewHTML += ("<a id='clientAdded' href='" + sURL + "' class='linkblue'>");
					else
						sNewHTML += "<span class='text_general_white'>";
					sNewHTML += sText;
					if(sURL)
						sNewHTML += "</a>";
					sNewHTML += "</span>";
					oCell.innerHTML = oCell.innerHTML + sNewHTML;
					}
				}
			}
		}
}

function ProductHistoryBar() {
	AddToHistoryBar("Products","../index/product_index.asp"); // Product index page
	AddToHistoryBar(document.title); 							// This page title - no link
}

function ProductHistoryBarIT() {
	AddToHistoryBar("Prodotti","../index/product_index.asp"); // Product index page
	AddToHistoryBar(document.title); 							// This page title - no link
}

function ProductHistoryBarDE() {
	AddToHistoryBar("Produkte","../index/product_index.asp"); // Product index page
	AddToHistoryBar(document.title); 							// This page title - no link
}

function SpecificationHistoryBar(sParentUrl) {
	var sTitle = document.title;
	var sProduct =  Trim(StrTokIdx(StrTokIdx(sTitle, ":", 0),"(",0));
	var sThisPage = StrTokIdx(sTitle, ":", 1);
	if(sThisPage)
		sThisPage = Trim(sThisPage);
	else
		{
		sThisPage = Getfname(document.location.pathname);
		var i = sThisPage.indexOf(".");
		if(i >= 0)
			sThisPage = sThisPage.substring(0,i);
		}
	if(sParentUrl!=null)
		sPathProduct = sParentUrl;
	else
		sPathProduct = "pm_" + sProduct + ".asp";
	AddToHistoryBar("Products","../index/product_index.asp"); // Product index page
	AddToHistoryBar(sProduct,sPathProduct); // Product link
	AddToHistoryBar(sThisPage); // This page title
	//alert(document.location);
}

function SpecificationHistoryBarIT(sParentUrl) {
	var sTitle = document.title;
	var sProduct =  Trim(StrTokIdx(StrTokIdx(sTitle, ":", 0),"(",0));
	var sThisPage = StrTokIdx(sTitle, ":", 1);
	if(sThisPage)
		sThisPage = Trim(sThisPage);
	else
		{
		sThisPage = Getfname(document.location.pathname);
		var i = sThisPage.indexOf(".");
		if(i >= 0)
			sThisPage = sThisPage.substring(0,i);
		}
	if(sParentUrl!=null)
		sPathProduct = sParentUrl;
	else
		sPathProduct = "pm_" + sProduct + ".asp";
	AddToHistoryBar("Prodotti","../index/product_index.asp"); // Product index page
	AddToHistoryBar(sProduct,sPathProduct); // Product link
	AddToHistoryBar(sThisPage); // This page title
	//alert(document.location);
}

function SpecificationHistoryBarDE(sParentUrl) {
	var sTitle = document.title;
	var sProduct =  Trim(StrTokIdx(StrTokIdx(sTitle, ":", 0),"(",0));
	var sThisPage = StrTokIdx(sTitle, ":", 1);
	if(sThisPage)
		sThisPage = Trim(sThisPage);
	else
		{
		sThisPage = Getfname(document.location.pathname);
		var i = sThisPage.indexOf(".");
		if(i >= 0)
			sThisPage = sThisPage.substring(0,i);
		}
	if(sParentUrl!=null)
		sPathProduct = sParentUrl;
	else
		sPathProduct = "pm_" + sProduct + ".asp";
	AddToHistoryBar("Produkte","../index/product_index.asp"); // Product index page
	AddToHistoryBar(sProduct,sPathProduct); // Product link
	AddToHistoryBar(sThisPage); // This page title
	//alert(document.location);
}

function SupportHistoryBar() {
	AddToHistoryBar("Support","support_index.asp"); // Support index page
	AddToHistoryBar(document.title); 					// This page title - no link
}

function TechsupportHistoryBar(sParentUrl) {
	var sTitle = document.title;
	var sSupport =  Trim(StrTokIdx(StrTokIdx(sTitle, ":", 0),"(",0));
	var sThisPage = StrTokIdx(sTitle, ":", 1);
	if(sParentUrl!=null)
		sPathSupport = sParentUrl;
	else
		sPathSupport = "pm_" + sSupport + ".asp";
	AddToHistoryBar("Support","../support_index.asp"); // Support index page
	
	AddToHistoryBar(document.title); // This page title - no link
	//alert(document.location);
}

function SalesHistoryBar() {
	AddToHistoryBar("Sales","sales_index.asp"); // Product index page
	AddToHistoryBar(document.title); 							// This page title - no link
}

function SalesHistoryBarIT() {
	AddToHistoryBar("Vendite","sales_index.asp"); // Product index page
	AddToHistoryBar(document.title); 							// This page title - no link
}

function SalesHistoryBarDE() {
	AddToHistoryBar("Kaufen","sales_index.asp"); // Product index page
	AddToHistoryBar(document.title); 							// This page title - no link
}

function CanopusHistoryBar() {
	AddToHistoryBar("Canopus","canopus_index.asp"); // Product index page
	//AddToHistoryBar("Press Releases","canopus_pressreleases.asp"); // Product index page
	AddToHistoryBar(document.title); 							// This page title - no link
}

function FixVirtualURLs() { //v3.0
	var sProt = document.location.protocol;
	var sHost = document.location.host;
	var i;
	var nImgSrc=0, nLinkUrl=0;
	var sCtry = GetCountry();
	if(Getfname(document.location.pathname.toLowerCase()) != HomePageCtry(sCtry))
		{
		// Preload toolbar rollover images - NOT home page
		var sPath = "/" + sCtry + "/images/common/"
		MM_preloadImages(sPath + "tb_home_on.gif", sPath + "tb_products_on.gif", sPath + "tb_sales_on.gif", sPath + "tb_support_on.gif", sPath + "tb_forum_on.gif", sPath + "tb_canopus_on.gif")
		}
	// Iterate through images collection
	for(i = 0; i < document.images.length; ++i)
		{
		var img = document.images[i];
		if(sVirtDir.length > 0 && ! img.complete)
			{
			var sImg = img.src;
			var n = sImg.indexOf(sHost);
			if(n >= 0)
				{
				var s1 = sImg.substr(n + sHost.length);
				var s2 = s1.substr(1,sVirtDir.length);
				if(s2.toLowerCase() != sVirtDir)
					{
					var sNewSrc = "/" + sVirtDir + sImg.substr(n + sHost.length);
					//alert(sNewSrc);
					document.images[i].src = sNewSrc;
					nImgSrc++;
					}
				}
			}
		if(IsImgBuyNow(img))
			SetBuyNow(img,sCtry);
		}
		
	SetBuyNow(document.images.buynow,sCtry);
	SetBuyNow(document.images.buynow1,sCtry);
	SetBuyNow(document.images.image48,sCtry);

	if(sVirtDir.length > 0)
		{
		// Iterate through links collection
		for(i = 0; i < document.links.length; ++i)
			{
			if(sHost == document.links[i].hostname)
				{
				var sPathname = document.links[i].pathname;
				var sSubPath = sPathname.substr(0,sVirtDir.length);
				if(sSubPath.toLowerCase() != sVirtDir)
					{
					document.links[i].pathname = sVirtDir + "/" + document.links[i].pathname;
					nLinkUrl++;
					}
				}
			}
		}
	//alert("URLs modified for virtual site:\nImages=" + nImgSrc + "\nLinks=" + nLinkUrl);
}

function IsImgBuyNow(oImg) { //v3.0
	var sImgName = oImg.name.toLowerCase();
	var sImgBuyNow = sImgName.substr(0,6);
	return (sImgBuyNow == "buynow" || sImgName == "image48");
}

function SetBuyNow(oImg, sCtry) { //v3.0
	if(oImg!=null)
		{
//		if(sCtry == null)
//			sCtry = GetCountry();
//		if(sCtry != "US")
		if(GetCookie(sconCanopus,sconBuyNowActive) == 0)
			RemoveImage(oImg);
		}
}

function SetBuyNowStatus(oImg, sUrl) { //v3.0
	if(oImg!=null)
		{
//		if(GetCountry() != "US")
		if(GetCookie(sconCanopus,sconBuyNowActive) == 0)
			RemoveImage(oImg);
		else if(sUrl != null)
			{
			sUrl = sUrl.toString();
			if(sUrl.length > 0)
				oImg.parentNode.href = sUrl;
			}
		}
}

// Function to populate country select object.
// Parameter 1: oSelect - SELECT object.
// Parameter 2: nMode   - 0 Anonomous user, 1 Authenticated user
// Returns: Number of items in select list.
function PopulateSelectCountry(oSel, nMode) {
	var aCtryCodes = new Array("US", "AU", "DE", "IT", "UK");
	var aCtryNames = new Array("USA", "AUSTRALIA", "DEUTSCHLAND", "ITALIA", "UNITED KINGDOM");

	var bHasCurCtry = false;
	var sCtryList="";
	var sUserGroups = (nMode == 0) ? "<all>" : GetCookie(svnLogon, svKeyUserGroups);
	var sCountry = GetCountry();
	var s1,s2,s3,i = 0;
	while((s1 = StrTokIdx(sUserGroups,",",i++)) != null)
		{
		if(s1.toLowerCase() == "<all>" )
			{
			for(var n = 0; n < aCtryCodes.length && n < aCtryNames.length; ++n)
				{
				if(sCtryList.length > 0)
					sCtryList += ",";
				if(sCountry == aCtryCodes[n])
					{
					sCtryList += "*";
					bHasCurCtry = true;
					}
				sCtryList += (aCtryNames[n] + "?" + aCtryCodes[n]);
				}
			}
		}

	if(sCtryList.length == 0)
		{
		for(var n = 0; n < aCtryCodes.length && n < aCtryNames.length; ++n)
			{
			i = 0;
			while((s1 = StrTokIdx(sUserGroups,",",i++)) != null)
				{
				s1 = s1.toUpperCase()
				s2 = ce_StrTokIdx(s1, "_", 0);
				s3 = ce_StrTokIdx(s1, "_", 1);
				if(s2 != null && s2 == "DB" && s3 != null && s3 == aCtryCodes[n])
					{
					if(sCtryList.length > 0)
						sCtryList += ",";
					if(sCountry == aCtryCodes[n])
						{
						sCtryList += "*";
						bHasCurCtry = true;
						}
					sCtryList += (aCtryNames[n] + "?" + aCtryCodes[n]);
					}
				}
			}
		}
	if(! bHasCurCtry)
		{
		// Get first country code in list
		var sCtry = StrTokIdx(StrTokIdx(sCtryList,",",0),"?",1);
		if(sCtry!=null)
			SetCountry(sCtry);
		}	
	return PopulateSelect(oSel,sCtryList);
}

function PutAdminSetupLink() {
	var sUserName = GetCookie("ceLogon","UserName");
	if(sUserName!=null && sUserName.length > 0)
		document.write(" | <a href='/admin/setupmenu.asp'>Admin Setup</a>");
}

