// Copyright (c) 2003 by IBM Corporation

var debug=false;

var CCLCgeo=[
"cnzh4",
"frfr3",
"dede3",
"itit3",
"jpjp4",
"uken3",
"gben3",
"usen1",
"dzen3",
"alen3",
"asen4",
"ades3",
"aopt3",
"aien1",
"aqen1",
"agen2",
"ares2",
"amen3",
"awen2",
"auen4",
"atde3",
"azen4",
"bsen1",
"bhen3",
"bden4",
"bben2",
"byen3",
"benl3",
"been3",
"befr3",
"bzen2",
"bjfr3",
"bmen2",
"bten4",
"boes2",
"baen3",
"bwen3",
"bven3",
"brpt2",
"ioen4",
"bnen4",
"bgbg3",
"bffr3",
"bifr3",
"khen4",
"cmfr3",
"caen1",
"cafr1",
"cvpt3",
"kyen1",
"cffr3",
"tdfr3",
"cles2",
"cxen4",
"ccen4",
"coes2",
"kmfr3",
"cgfr3",
"cken4",
"cres2",
"hren3",
"cyen3",
"czen3",
"dken3",
"djfr3",
"dmen2",
"does2",
"tpen4",
"eces2",
"egen3",
"sves2",
"gqes3",
"eren3",
"eeen3",
"eten3",
"fken2",
"foen3",
"fjen4",
"fien3",
"gffr2",
"pffr4",
"tffr3",
"gafr3",
"gmen3",
"geen3",
"ghen3",
"gien3",
"gren3",
"glen3",
"gden2",
"gpfr2",
"guen4",
"gtes2",
"gnfr3",
"gwpt3",
"gyen2",
"htfr2",
"vait3",
"hnes2",
"hken4",
"huen3",
"isen3",
"inen4",
"iden4",
"ieen3",
"ilen3",
"cifr3",
"jmen2",
"joen3",
"kzen3",
"keen3",
"kien4",
"krkr4",
"kwen3",
"kgen3",
"laen4",
"lven3",
"lben3",
"lsen3",
"lren3",
"lide3",
"lten3",
"lufr3",
"moen4",
"mken3",
"mgfr3",
"mwen3",
"myen4",
"mven4",
"mlfr3",
"mten3",
"mhen4",
"mqfr2",
"mren3",
"muen3",
"ytfr3",
"mxes2",
"fmen4",
"mden3",
"mcfr3",
"mnen4",
"msen2",
"mafr3",
"mzpt3",
"mmen4",
"naen3",
"nren4",
"npen4",
"nlen3",
"anen2",
"ncfr3",
"nzen4",
"nies2",
"nefr3",
"ngen3",
"nuen4",
"nfen4",
"mpen4",
"noen3",
"omen3",
"pken3",
"pwen4",
"psen3",
"paes2",
"pgen4",
"pyes2",
"pees2",
"phen4",
"pnen4",
"plen3",
"ptpt3",
"pres2",
"qaen3",
"refr3",
"roen3",
"ruen3",
"rwen3",
"shen3",
"knen2",
"lcen2",
"wsen4",
"smit3",
"stpt3",
"saen3",
"snfr3",
"scen3",
"slen3",
"sgen4",
"sken3",
"sien3",
"sben4",
"soen3",
"zaen3",
"eses3",
"lken4",
"vcen1",
"sren3",
"szen3",
"seen3",
"chde3",
"chen3",
"chfr3",
"chit3",
"syen3",
"twzh4",
"tjen3",
"tzen3",
"then4",
"tgfr3",
"tken4",
"toen3",
"tten2",
"tnen3",
"tren3",
"tmen3",
"tcen1",
"tven4",
"ugen3",
"uaen3",
"aeen3",
"uyes2",
"uzen3",
"vuen4",
"vees2",
"vnen4",
"vgen2",
"vien2",
"wffr4",
"ehen3",
"yeen3",
"yuen3",
"zmen3",
"zwen3",
"00en0"];

function popUpProperties(inobj){
	op=window.open();
	op.document.open('text/plain');
	for(objprop in inobj){
		op.document.write(objprop+' => '+inobj[objprop]+'\n');
	}
	op.document.close();
}

function setCookie(value){
	expires=new Date;
	expires.setTime(expires.getTime()+5*365*24*3600*1000);
	ckstr="ipcInfo="+escape(value)+"; path=/;domain=.ibm.com;expires="+expires.toGMTString();
	document.cookie=ckstr;
}

function setSessionCookie(value){
	ckstr="ipcInfo="+escape(value)+"; path=/;domain=.ibm.com";
	document.cookie=ckstr;
}

function deleteCookieOrig(){
	expires=new Date;
	expires.setTime(expires.getTime()-5*365*24*3600);
	document.cookie="ipcInfo=; path=/;domain=.ibm.com;expires="+expires.toGMTString();
}

function deleteCookie(){
	document.cookie="ipcInfo=; path=/;domain=.ibm.com;";
}

function getCookie(){
	nm="ipcInfo=";
	cookieString=document.cookie;
	n=cookieString.length;
	m=nm.length;
	for (i=0; i<n-m && nm!=cookieString.substring(i, i+m); ++i);
	if (i>=(n-m)){
		return "";
	}
	i+=m;
	j=cookieString.indexOf(";", i);
	c=unescape((j<i)?cookieString.substring(i):cookieString.substring(i, j));
	return c;
}

function getCookieCCLC(){
	ipcInfo=getCookie();
	if (ipcInfo=="") return "";
	len=ipcInfo.length;
	// look first for cc=, lc= ; if not found, look for pwc=, pwl=
	i=ipcInfo.indexOf("cc=");
	if (i>=0) {
		if (i+5>=len||ipcInfo.charAt(i+5)!=';')return "";
		cc=ipcInfo.substring(i+3, i+5);
		i=ipcInfo.indexOf("lc=");
		if (i<0||i+5>=len||ipcInfo.charAt(i+5)!=';')return "";
		lc=ipcInfo.substring(i+3, i+5);
	} else {
		j=ipcInfo.indexOf("pwc=");
		if (j<0||j+6>=len||ipcInfo.charAt(j+6)!=';')return "";
		cc=ipcInfo.substring(j+4, j+6);
		j=ipcInfo.indexOf("pwl=");
		if (j<0||j+6>=len||ipcInfo.charAt(j+6)!=';')return "";
		lc=ipcInfo.substring(j+4, j+6);
	}
	return cc+lc;
}

function getBrowserCCLC(){
	cc=lc="";
	l=(navigator.userLanguage?navigator.userLanguage:navigator.language).toLowerCase();
	len=l.length;
	if(len<2)return "00en";
	lc=l.substring(0,2);
	if(len<5){
		if(lc=="ja")cc="ja";
		if(lc=="en")cc="us";
		if(lc=="jp")cc="jp";
		if(lc=="zh")cc="cn";
		if(lc=="fr")cc="fr";
		if(lc=="de")cc="de";
		if(lc=="ko")cc="ko";
		if(lc=="pt")cc="br";
		if(lc=="es")cc="mx";
	}else{
		cc=l.substring(3,5);
	}
	if(cc=="")cc="00";
	if(lc=="")lc="en";
	return cc+lc;
}

// if there's no cookie but the browser has CC and LC, then we set the cookie
function getCCLC(){
	cclc=getCookieCCLC();
	if(cclc==""){
		cclc=getBrowserCCLC();
		n="";
		if(cclc=="jpjp")n=cclc;
		if(cclc=="koko")n=cclc;
		if(cclc=="cnzh")n=cclc;
		if(cclc=="twzh")n=cclc;
		cclc=n;
		if(cclc!="")setCookie("cc="+cc+";lc="+lc+";ac=all");
	}
	return cclc;
}

// returns 0, 1, 2, 3, or 4
function getGeo(cclc){
	for(i=0;i<CCLCgeo.length; i++){
		if(cclc==CCLCgeo[i].substring(0,4)) return CCLCgeo[i].substring(4,6);
	}
	return 0;
}

// returns the filename component of a path; ignores everything beginning w. '?'
// e.g. if ref = "http://www.ibm.com/xxy/aab/mypage.html", it returns "mypage.html"
function getLocFilename(ref) {
  var rslt;
  var q = ref.indexOf("?");
  if (q == -1) {
    rslt = ref;
  } else {
    rslt = ref.substr(0, q);
  }
  var s = rslt.lastIndexOf("/");
  if (s == -1) {
    rslt = ref;
  } else {
    rslt = ref.substr(s+1);
  }
  return rslt;
}

function getRegionalPageName(cclc){
	var g=getGeo(cclc);
	var cc=cclc.substring(0,2);
	var lc=cclc.substring(2,4);
	// check for country/lang. match
	for(i=0;i<Pages.length; i++){
		if(cclc==Pages[i].substring(0,4)) return Pages[i].substr(4);
	}
	// check for geo/lang. match: e.g. "-3fr" for French in EMEA, or "-2es" for Spanish in Latin America
	for(i=0;i<Pages.length; i++){
		if(("-"==Pages[i].substring(0,1)) && (g==Pages[i].substring(1,2)) &&
		   (lc==Pages[i].substring(2,4))) return Pages[i].substr(4);
	}
	// check for language match, without country: e.g. "--fr" or "--es"
	for(i=0;i<Pages.length; i++){
		if(("--"==Pages[i].substring(0,2)) &&
		   (lc==Pages[i].substring(2,4))) return Pages[i].substr(4);
	}
	// check for country match
	for(i=0;i<Pages.length; i++){
		if(cc==Pages[i].substring(0,2)) return Pages[i].substr(4);
	}
	// check for geo match
	for(i=0;i<Pages.length; i++){
		if(g==Pages[i].substring(0,1)) return Pages[i].substr(1);
	}
	return "";
}

// adjusts the selected option in the country pulldown
function preselectDefaultCountry(){
	c=getCookie();
	if (c.indexOf("cc=")>=0) document.ccform.remember.checked=1;
	cclc=getCCLC();
	if(cclc=="")return;
	l=document.ccform.country;
	for(i=0;i<l.length;i++){
		if (l[i].value==cclc){
			l.selectedIndex=i;
			break;
		}
	}
}

// opens a new window iff the server doesn't contain "ibm.com"
function goToOrPopupPage(url, repl) {
	var external_page = false;
	var hname;
	if (url.substring(0,7)=="http://") {
		next_slash = url.indexOf("/", 7);
		if (next_slash==-1) {
			hname = url.substring(7);
		} else {
			hname = url.substring(7,next_slash);
		}
		if (hname.indexOf("ibm.com") == -1) external_page = true;
	} else if (url.substring(0,8)=="https://") {
		next_slash = url.indexOf("/", 8);
		if (next_slash==-1) {
			hname = url.substring(8);
		} else {
			hname = url.substring(8,next_slash);
		}
		if (hname.indexOf("ibm.com") == -1) external_page = true;
	}
	if (!external_page) {
		if (debug) alert("repl is "+repl);
		if (repl) {
			window.location.replace(url);
		} else {
			window.location=url;
		}
	} else {
		window.open(url, "extwin");
	}
}

function redirectToRegionalPage(){
	if (debug) alert("redirectToRegionalPage(): my URL="+document.URL);
	p=getRegionalPageName(getCCLC());
	if(p!="" && p!=location.href && p!=getLocFilename(location.href)) goToOrPopupPage(p,true);
}

// returns true or false
function cookieHasGoParam() {
	ipcInfo=getCookie();
	if (ipcInfo=="") return false;
	i=ipcInfo.indexOf("go=");
	if (i>=0) return true;
	else return false;
}

// returns true or false
function stopParameterSet() {
	if (document.URL.indexOf("?OpenDocument&s") > 0) {
		return true;
	} else {
		return false;
	}
}

// redirects iff the cookie is set, AND:
// either the 's' parameter is not set, or it's set and the 'go' param is also set
function maybeRedirectToRegionalPage(){
	if (debug) alert("maybeRedirectToRegionalPage(): my URL="+document.URL);
	if (stopParameterSet()) {
		if (debug) alert("stop parameter is set");
		// stop UNLESS the cookie says 'go'
		if (cookieHasGoParam()) {
			if (debug) alert("cookie has 'go' param");
			var cclc=getCCLC();
			var cc=cclc.substring(0,2);
			var lc=cclc.substring(2,4);
			// set the cookie again, but omit the 'go' part:
			// determine if the cookie is 'remembered' or not
			var ipcInfo=getCookie();
			if (ipcInfo.indexOf("pwc=")<0) {
				setCookie("cc="+cc+";lc="+lc+";ac=all");
			} else {
				setSessionCookie("pwc="+cc+";pwl="+lc+";ac=all");
			}
			p=getRegionalPageName(cclc);
			if (debug) alert("cclc="+cclc+"; page="+p);
			// parameter repl==false because we want this page to stay in the history:
			if(p!="" && p!=location.href && p!=getLocFilename(location.href)) goToOrPopupPage(p,false);
		}
		// else do nothing (no redirect)
	} else {
		p=getRegionalPageName(getCCLC());
		if (debug) alert("maybe-redirect URL: "+p);
		if(p!="" && p!=location.href && p!=getLocFilename(location.href)) goToOrPopupPage(p,false);
	}
}

// called when the 'Go' button is pushed by the user
// should only be used on pages that do an initial maybeRedirectToRegionalPage()!!!
function goToRegionalPage_backFriendly(){
	s=document.ccform.country;
	i=s.selectedIndex;
	v=s.options[i].value;
	if(i==0)return;
	cc=v.substring(0,2);
	lc=v.substring(2,4);
	if(cc=="00"){
		deleteCookie();
		document.ccform.remember.checked=0;
		p=getRegionalPageName(cc+lc);
		if(p!="" && p!=location.href && p!=getLocFilename(location.href)) goToOrPopupPage(p,false);
	} else {
		if (stopParameterSet()) {
			if(document.ccform.remember.checked){
				setCookie("cc="+cc+";lc="+lc+";ac=all");
			} else {
				setSessionCookie("pwc="+cc+";pwl="+lc+";ac=all");
			}
			p=getRegionalPageName(cc+lc);
			if(p!="" && p!=location.href && p!=getLocFilename(location.href)) goToOrPopupPage(p,false);
		} else {
			// add a field 'go' to the cookie string
			if(document.ccform.remember.checked){
				setCookie("cc="+cc+";lc="+lc+";ac=all;go=1");
			} else {
				setSessionCookie("pwc="+cc+";pwl="+lc+";ac=all;go=1");
			}
			// reload self w. "?s" appended -- this will then redirect
			if (debug) alert("redirecting::location="+document.URL+"?OpenDocument&s");
			window.location.replace(document.URL + "?OpenDocument&s");
			//window.location=document.URL+"?OpenDocument&s";
		}
	}
}

function goToRegionalPage(){
	s=document.ccform.country;
	i=s.selectedIndex;
	v=s.options[i].value;
	if(i==0)return;
	cc=v.substring(0,2);
	lc=v.substring(2,4);
	if(cc=="00"){
		deleteCookie();
		document.ccform.remember.checked=0;
	}else{
		if(document.ccform.remember.checked){
			setCookie("cc="+cc+";lc="+lc+";ac=all");
		}else{
			setSessionCookie("pwc="+cc+";pwl="+lc+";ac=all");
		}
	}
	p=getRegionalPageName(cc+lc);
	if(p!="" && p!=location.href && p!=getLocFilename(location.href)) goToOrPopupPage(p,false);
}

function goToIBMhome(){
	s=document.ccform.country;
	i=s.selectedIndex;v=s.options[i].value;
	if(i==0)return;
	cc=v.substring(0,2);
	lc=v.substring(2,4);
	if(cc=="00"){
		deleteCookie();
		document.ccform.remember.checked=0;
	}else{
		if(document.ccform.remember.checked){
			setCookie("cc="+cc+";lc="+lc+";ac=all");
		}else{
			setSessionCookie("pwc="+cc+";pwl="+lc+";ac=all");
		}
	}
	window.location="http://www.ibm.com/";
}
