
var expDays = 60;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

//check if user accepts cookies
var WM_acceptsCookies = false;
if(document.cookie == '') {
    document.cookie = 'WM_acceptsCookies=yes'; // Try to set a cookie.
    if(document.cookie.indexOf('WM_acceptsCookies=yes') != -1) {
	WM_acceptsCookies = true;
    }// If it succeeds, set variable
} else { // there was already a cookie
  WM_acceptsCookies = true;
}

if(document.location  == "http://kensington.com" || document.location == "http://kensington.com/"){
	document.location = "http://www.kensington.com";
}


function setCookie (name, value) {
	var argv = setCookie.arguments;
	var argc = setCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape (value) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}

function WM_killCookie (name) {
	document.location = "http://www.kensington.com/rmvCookie.html";
}

function rmvCookie (name) {
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);
	var cval = GetCookie (name);

	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
	document.location = "http://www.kensington.com";
}

function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}









function WM_readCookie(name) {
    if(document.cookie == '') { // there's no cookie, so go no further
	return false;
    } else { // there is a cookie
	var firstChar, lastChar;
	var theBigCookie = document.cookie;
	firstChar = theBigCookie.indexOf(name);	// find the start of 'name'
	var NN2Hack = firstChar + name.length;
	if((firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=')) { // if you found the cookie
	    firstChar += name.length + 1; // skip 'name' and '='
	    lastChar = theBigCookie.indexOf(';', firstChar); // Find the end of the value string (i.e. the next ';').
	    if(lastChar == -1) lastChar = theBigCookie.length;
	    return unescape(theBigCookie.substring(firstChar, lastChar));
	} else { // If there was no cookie of that name, return false.
	    return false;
	}
    }
} // WM_readCookie




//after visitor selects country, create cookie, and send visitor to respective site
function getSite(list)
{
if (list.options[list.selectedIndex].value!="#") {
	var countryId = list.options[list.selectedIndex].value;
	var usr = navigator.userAgent.toLowerCase();
	if(usr.indexOf("googlebot") != -1 || usr.indexOf("slurp") != -1 || 
	   usr.indexOf("msnbot") != -1 || usr.indexOf("architext spider") != -1 || 
	   usr.indexOf("yahoo slurp") != -1 || usr.indexOf("ask jeeves") != -1 || 
	   usr.indexOf("ia_archiver") != -1 || usr.indexOf("scooter") != -1 || 
	   usr.indexOf("mercator") != -1 || usr.indexOf("crawler@fast") != -1 || 
	   usr.indexOf("lycos_spider_(t-rex)") != -1 || usr.indexOf("webcrawler") != -1) {
	   
	   //spider detected; dont set cookie
	   } else {
	   		setCookie("kcpg", countryId, exp);
	   }

	switch (countryId) {
	case "ar":
		top.location.href="http://www.kensingtonargentina.com";
		break;
	case "cl":
		top.location.href="http://www.kensington.cl";
		break;
	case "hk":
		top.location.href="http://www.kensingtonhk.com";
		break;
	case "jp":
		top.location.href="http://www.nanayojapan.co.jp";
		break;
	case "cn":
		top.location.href="http://www.kensingtonchina.com";
		break;
	case "korea":
		top.location.href="http://www.kensingtonkorea.com";
		break;
	case "india":
		top.location.href="http://www.in.kensington.com";
		break;
	case "indo":
		top.location.href="http://www.kensindoprima.com";
		break;
	case "tn":
		top.location.href="http://www.kensington.com.tw";
		break;
	default:
		top.location.href="http://" + countryId + ".kensington.com";
		break;
	}

}

}


function redirByCookie(){
	//check if visitor was already here. if so, send visitor to respective site
	if (WM_readCookie("kcpg")) {
	var cookieValue = WM_readCookie("kcpg");

		if (cookieValue == "cl") {
			//top.location.href="http://www.kensington.cl";
		} else if (cookieValue == "cn") {
			//top.location.href="http://www.kensingtonchina.com";
		} else if (cookieValue == "jp") {
			//top.location.href="http://www.nanayojapan.co.jp";
		} else if (cookieValue == "hk") {
      		top.location.href="http://www.kensingtonhk.com";
		} else if (cookieValue == "india") {
			//top.location.href="http://www.kensingtonindia.com";
		} else if (cookieValue == "indo") {
			top.location.href="http://www.kensindoprima.com";
		} else if (cookieValue == "tn") {
			top.location.href="http://www.kensington.com.tw";
		} else if (cookieValue == "korea") {
			//top.location.href="http://www.kensingtonkorea.com";
		} else if (cookieValue == "ar") {
			top.location.href="http://www.kensingtonargentina.com";
		} else {//all other sites
			top.location.href="http://" + cookieValue + ".kensington.com";
		}
	}
}

function createDropDownList(){
	//creates drop-down list for landing page/change country page
	//weeds out non-javascript browsers
	document.writeln('<div style="padding:0px 0px 11px 215px">');
	document.writeln('<select class="selectCountry" onchange="getSite(this)">');
	document.writeln('<option value="#">Select Your Country</option>');
	document.writeln('<option value="us" class="header">United States</option>');
	document.writeln('<option value="ar">Argentina</option>');
	document.writeln('<option value="au">Australia &amp; New Zealand</option>');
	document.writeln('<option value="be.nl">Belgium - Dutch</option>');
	document.writeln('<option value="be.fr">Belgium - French</option>');
	document.writeln('<option value="ca">Canada - English</option>');
	//document.writeln('<option value="ca.fr" style="color:#999">Canada - French</option>');
	document.writeln('<option value="cl">Chile</option>');
	document.writeln('<option value="cn">China</option>');
	document.writeln('<option value="cz">Czech Republic</option>');
	document.writeln('<option value="dk">Denmark</option>');
	document.writeln('<option value="dk">Estonia</option>');
	document.writeln('<option value="uk">Finland</option>');
	document.writeln('<option value="fr">France</option>');
	document.writeln('<option value="de">Germany</option>');
	document.writeln('<option value="hk">Hong Kong</option>');
	document.writeln('<option value="hu">Hungary</option>');
	document.writeln('<option value="dk">Iceland</option>');
	document.writeln('<option value="india">India</option>');
	document.writeln('<option value="indo">Indonesia</option>');
	document.writeln('<option value="ie">Ireland</option>');
	document.writeln('<option value="it">Italy</option>');
	document.writeln('<option value="jp">Japan</option>');
	document.writeln('<option value="korea">Korea</option>');
	document.writeln('<option value="nl">Netherlands</option>');
	document.writeln('<option value="no">Norway</option>');
	document.writeln('<option value="pl">Poland</option>');
	document.writeln('<option value="ru">Russia</option>');
	document.writeln('<option value="es">Spain</option>');
	document.writeln('<option value="se">Sweden</option>');
	document.writeln('<option value="tn">Taiwan</option>');
	document.writeln('<option value="uk">United Kingdom</option>');
	document.writeln('</select></div>');
}
