// Set user country pref
// Author: Xiaowen, Corporate Webmaster
// Updated by: Marie Shafi
// $Id: pwpref.js,v 1.5 2005/08/16 20:26:15 scale Exp $

var lenovoPrefsCount = 0;
var lenovoPrefsImage = new Image();
var lenovoPrefsTarget;



function showPref(form)
{

	//if (isCookieEnabled())
	//{
		
		document.write(getSetPrefCheckbox());
	//}
}

function showPage(link)
{

document.hpbyc.Country.value = link.href;
document.hpbyc.action="index.aspx"
document.hpbyc.submit();

}

function setPrefFromTextLink(link)
{
	if (!link) return link;
	
var form = getForm();
	if (!form) return;
	var	url = link.href;
	var	x = form.x;
	setPref(url, x)
}

function setPrefFromDropdown(form)
{
	form = getForm(form);
	if (!form) return;

	var	url = form.url[form.url.selectedIndex].value;
	var	x = form.x;
	setPref(url, x)
}

function setPref(url, x)
{
	//if (!url || !x) return; //removed tracker 2200-SCA

	if (!url) return;
	lenovoPrefsTarget = url;

	var	len = url.length;
	//if (len>=7 && url.charAt(len-1)=='/' && url.charAt(len-4)=='/' && url.charAt(len-7)=='/')
	//{
		//var	x = (typeof(x)=="object" && typeof(x.checked)=="boolean") ? x.checked : false;
		//if (x)
		//{
			//var	cc = url.substring(len-6, len-4);
			//var	lc = url.substring(len-3, len-1);
			//var	ac = "all";
			//setCookie("cc="+cc+";lc="+lc+";ac="+ac);

            //lenovoPrefsImage.src = "//www.ibm.com/fscripts/lenovoinfo/prefs?cc=" + cc + "&lc=" + lc;    // Convert to fastcgi later
			//lenovoPrefsImage.src = "//www.ibm.com/scripts/lenovoinfo/prefs.pl?cc=" + cc + "&lc=" + lc;

			//var lenovoPrefsWatchdog = window.setTimeout("lenovoPrefsCheck();", 50); 

			//measurement(cc, lc, ac);
		//}
		//else {
			document.location.href = lenovoPrefsTarget;
		//}
	//}
}

// Check if the image setting the ibm.com cookie has loaded
// or the maximum wait time has been reached
function lenovoPrefsCheck()
{
	var maxTries = 500;
	if (lenovoPrefsImage.complete || ++lenovoPrefsCount >= maxTries) {
		document.location.href = lenovoPrefsTarget;
	} else {
		lenovoPrefsWatchdog = window.setTimeout("lenovoPrefsCheck();", 50); 
	}
}

function setCookie(value)
{
	var	expires = new Date;
	expires.setTime(expires.getTime()+5*365*24*3600*1000);

	var	ckstr = "ipcInfo="+escape(value)+
				"; path=/; domain=.lenovo.com; expires="+expires.toGMTString();
	document.cookie = ckstr;

}

function deleteCookie()
{
	var	expires = new Date;
	expires.setTime(expires.getTime()-5*265*24*3600);

	document.cookie = "ipcInfo=; path=/; domain=.lenovo.com; expires="+expires.toGMTString();
}

function getCookie()
{
	var	name = "ConsumerSite=";
	var	cookieString = document.cookie;
	var	n = cookieString.length, m = name.length;
	var	i, j;

	for (i = 0; i<n-m && name!=cookieString.substring(i, i+m); ++i);

	if (i>=n-m) 
		return "";
	
	i += m;
	j = cookieString.indexOf(";", i);

	return unescape((j<i)?cookieString.substring(i):cookieString.substring(i, j));
	document.write(unescape((j<i)?cookieString.substring(i):cookieString.substring(i, j)));
}

function isCookieEnabled()
{
	var	ipcInfo = getCookie();


	setCookie("cc=;lc=;ac=");
	var	rc = (getCookie()=="cc=;lc=;ac=");

	if (rc)
	{
		if (ipcInfo=="")
			deleteCookie();
		else
			setCookie(ipcInfo);
	}


	return rc;	
}

function measurement(cc, lc, ac)
{
	var	rand = Math.round(Math.random()*900000+100000);
	var	cgif = new Image;
	var	imgstr = "/i/uc/c.gif?cc="+cc+"&lc="+lc+"&ac="+ac+
				"&page=planetwide&prefs=set&random="+rand;
	cgif.src = imgstr;
}

function preselect(form)
{
	var	ipcInfo = getCookie();
	if (ipcInfo=="")
		return;

	var	len = ipcInfo.length, idx;

	idx = ipcInfo.indexOf("cc=");
	if (idx<0 || idx+5>=len || ipcInfo.charAt(idx+5)!=';')
		return;

	var	cc = ipcInfo.substring(idx+3, idx+5);

	idx = ipcInfo.indexOf("lc=");
	if (idx<0 || idx+5>=len || ipcInfo.charAt(idx+5)!=';')
		return;

	var	lc = ipcInfo.substring(idx+3, idx+5);

	var	url = "/"+cc+"/"+lc+"/";

	form = getForm(form);
	if (!form)
	{
		return;
	}


	var	list = form.url;
	var	i;
	for (i = list.length-1; i>=0; --i)
	{
		idx = list[i].value.indexOf(url);
		if (idx>0 && list[i].value.substring(idx)==url)
		{
			list.selectedIndex = i;
			break;
		}
	}
}

function getSetPrefCheckbox()
{
	var	LenovoSite = getCookie();
	if (LenovoSite=="")
		return "<input name='x' type='checkbox' checked='checked'/><b>Remember this choice</b><br />";
	else
		return "<input name='x' type='checkbox'/><b>Remember this choice</b><br />";
}

function getForm(form)
{
	return (form ? form : document.hpbyc);
}
