/* $Change: 2312989 $ $DateTime: 2008/04/15 11:07:24 $ $Revision: #19 $ */

/*******************************************************************
* _choose_page_by_locale.js                                        *
*                                                                  *
* Created By:  Liz Hunt on 24 August 2005
* Modified By Leon Fainbuch on 21 August 2006                          *
*                                                                  *
* Requires Page-Appropriate Language Dropdown URLs JavaScript File *
/******************************************************************/

aLang = new Array();
aLang[0] = "en";
aLang[1] = "fr";
aLang[2] = "de";
aLang[3] = "it";
aLang[4] = "es";
aLang[5] = "ja";
aLang[6] = "ko";
aLang[7] = "zhs";
aLang[8] = "zht";

aSelectYourCountry = new Array();
aSelectYourCountry[0] = "Select Your Country";
aSelectYourCountry[1] = "Choisissez votre pays";
aSelectYourCountry[2] = "Land ausw鄣len";
aSelectYourCountry[3] = "Seleziona il tuo Paese";
aSelectYourCountry[4] = "Seleccione su Pa疄";
aSelectYourCountry[5] = "&#12362;&#23458;&#27096;&#12398;&#22269;&#12434;&#36984;&#25246;";
aSelectYourCountry[6] = "&#44397;&#44032;&#47484; &#49440;&#53469;&#54616;&#49901;&#49884;&#50724;";
aSelectYourCountry[7] = "&#36873;&#25321;&#24744;&#25152;&#22312;&#30340;&#22269;&#23478;";
aSelectYourCountry[8] = "&#36984;&#25799;&#24744;&#25152;&#22312;&#30340;&#22283;&#23478;";

aPath = new Array();
aPath[0] = "";
aPath[1] = "/f/";
aPath[2] = "/d/";
aPath[3] = "/i/";
aPath[4] = "/esa/";
aPath[5] = "/ja/";
aPath[6] = "/ko/";
aPath[7] = "/zhs/";
aPath[8] = "/zht/";

function WriteGlobalGatewayLink(sReturnURL,sLocale,sLabel)
{	
	var sGlobalGatewayURL = "http://sine.ni.com/nigwy/cds/globalgateway/p/lang/";
	var sHTML = '<a href="' + sGlobalGatewayURL + sLocale + '?rtrn=' + sReturnURL + '"><strong>' + sLabel + '</strong></a>';
	// document.write(sHTML);
}

function getLangIndex(LangCode)
{
	var LangIndex = 999;
	for (ndx=0; ndx<aLang.length; ndx++)
	{
		if (aLang[ndx]==LangCode)
		{
			LangIndex = ndx;
			break;
		}
	}
	return LangIndex;
}

function getPathIndex(Path)
{
	var PathIndex = 0;
	for (ndx=1; ndx<aPath.length; ndx++)
	{
		if (location.href.indexOf(aPath[ndx]) != -1)
		{
			PathIndex = ndx;
			break;
		}
	}
	return PathIndex;
}

function getURLParam(strParamName)
{
	var strReturn = "";
	var strHref = window.location.href;
	if ( strHref.indexOf("?") > -1 )
	{
		var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
		var aQueryString = strQueryString.split("&");
		for ( var iParam = 0; iParam < aQueryString.length; iParam++ )
		{
			if (aQueryString[iParam].indexOf(strParamName + "=") > -1 )
			{
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break;
			}
		}
	}
	return strReturn;
}

function getCookie(index) {
	var allcookies = document.cookie;
	var pos = allcookies.indexOf(index + "=");
	if (pos == -1) return null;
	var start = pos + (index.length + 1);
	var end = allcookies.indexOf(";", start);
	if (end == -1) end = allcookies.length;
	var value = allcookies.substring(start, end);
	return value;
}

var reLocale = /^[A-Za-z]{2}-[A-Za-z]{2}$/;	/* Regular Expression for Locale Cookie (2 Letters, Dash, 2 Letters) */
function IsLocale(s)
{
	return reLocale.test(s);
}

var thosecrazymetrics = "";
var tabmetric = "";
var wholeURL = location.href;
var baseURL = wholeURL.split("?");
var tabPound = wholeURL.split("#");

if (tabPound[1] == undefined)
{
	
}
else
{
	tabmetric = "#" + tabPound[1];
}

function addMetric(name,value)
{
	if ( thosecrazymetrics.indexOf("?") > -1 )
	{
		thosecrazymetrics = thosecrazymetrics + "&" + name + "=" + value;
	}
	else
	{
		thosecrazymetrics = thosecrazymetrics + "?" + name + "=" + value;
	}
}

function choosePageByLocale()
{
	var ErrorChecking = false;
	var NILocale = "";
	var PathLang = "";
	var sLocaleDestURL = "";
	var metc = getURLParam('metc');
	var media = getURLParam('media');
	var activity = getURLParam('activity');
	var details = getURLParam('details');

	if (metc != "")
	{
		addMetric('metc',metc);
	}
	if (media != "")
	{
		addMetric('media',media);
	}
	if (activity != "")
	{
		addMetric('activity',activity);
	}
	if (details != "")
	{
		addMetric('details',details);
	}

	/* Determine Language from URL */
	var PathIndex = getPathIndex(location.href);
	if (aLang[PathIndex].substring(0,2)=="zh")	/* If Path Language Is Chinese, Correct for NI Global Gateway */
	{
		if (aLang[PathIndex] == "zh-CN") PathLang = "zhs";
		if (aLang[PathIndex] == "zh-TW" || aLang[PathIndex] == "zh-HK") PathLang = "zht";
	}
	else
	{
		PathLang = aLang[PathIndex];	/* Otherwise Path Language is Language Code */
	}
	if (ErrorChecking) alert("URL = "+location.href+"\n"+"Path Language = "+PathLang+"\n");
	
	/* Get Locale Cookie */
	var Locale = getCookie('locale');
	if (ErrorChecking) alert("Locale Cookie = "+Locale+"\n");
	
	if (Locale == null || Locale == "" || !IsLocale(Locale))
	{
		/* Locale Cookie Not Set, Locale Cookie Emptied But Not Deleted, or Locale Cookie Corrupted */
		sLocaleDestURL = location.protocol + "//" + location.hostname + aLangURLs[0];
		WriteGlobalGatewayLink(sLocaleDestURL,NILocale,aSelectYourCountry[PathIndex]);
		if (ErrorChecking) alert("Locale Cookie Not Set, Locale Cookie Emptied But Not Deleted, or Locale Cookie Corrupted\n");
		return true;
	}
	if (Locale.substring(0,2)=="zh")	/* If Locale Is Chinese, Correct for NI Global Gateway to Set NI Locale */
	{
		if (Locale == "zh-CN") NILocale = "zhs";
		if (Locale == "zh-TW" || Locale == "zh-HK") NILocale = "zht";
	}
	else
	{
		Locale = Locale.substring(0,2);	/* Otherwise Need Only Two-Letter Language Code From Locale to Set NILocale */
		NILocale = Locale;
	}
	if (ErrorChecking) alert("NI Locale = "+NILocale+"\n");
	
	/* Get Language Index */
	LangIndex = getLangIndex(NILocale);
	if (ErrorChecking) alert("Language Index = "+LangIndex+"\n");
	
	// Initialize locale url variable
	// Locale destination url is for Locale redirection used by Global Gateway server-side script
	sLocaleDestURL = location.protocol + "//" + location.hostname + aLangURLs[LangIndex];
	
	// If current location url is the same as locale destination url, user is already on correct page,
	// so no redirect is needed (case 4)
	if (baseURL[0] == sLocaleDestURL)
	{	
		WriteGlobalGatewayLink(sLocaleDestURL,NILocale,aSelectYourCountry[LangIndex]);
		if (ErrorChecking) alert("Current URL matches Locale URL, No Redirect\n");
		return true;
	}
	
	if (typeof(aLangURLs) == "undefined")
	{
		/* Page-Appropriate Language Dropdown URLs JavaScript File Does Not Exist */
		document.write('<span style="font-size: 9px; color: #660000;"><strong>Error:</strong>&nbsp;Page-Appropriate Language Dropdown&nbsp;<br>URLs JavaScript File Does Not Exist</span>');
		if (ErrorChecking) alert("Page-Appropriate Language Dropdown URLs JavaScript File Does Not Exist\n");
		return true;
	}
	
	if (LangIndex == 999)
	{
		/* For Future Compatibility... If Chosen Language Not One of Nine Supported Languages, Send To English (Case 1)*/
		sLocaleDestURL = location.protocol + "//" + location.hostname + aLangURLs[0];
		WriteGlobalGatewayLink(sLocaleDestURL,NILocale,aSelectYourCountry[0]);
		location.replace(aLangURLs[0]+"");
		if (ErrorChecking) alert("Case 1\n");
		return true;
	}
	
	if (aLangURLs[LangIndex] == "-")
	{
		/* If Page Does Not Exist In Chosen Language, Send To Whatever Page Was Entered (Case 2) */
		sLocaleDestURL = location.protocol + "//" + location.hostname + aLangURLs[0];
		WriteGlobalGatewayLink(sLocaleDestURL,NILocale,aSelectYourCountry[PathIndex]);
		if (ErrorChecking) alert("Case 2\n");
		return true;
	}
	
	/* Send To Page In Chosen Language (Case 3) */
	sLocaleDestURL = location.protocol + "//" + location.hostname + aLangURLs[LangIndex];
	WriteGlobalGatewayLink(sLocaleDestURL,NILocale,aSelectYourCountry[LangIndex]);
	location.replace(aLangURLs[LangIndex] + tabmetric + thosecrazymetrics);
	if (ErrorChecking) alert("Case 3\n");
}

