// array to store SiteURL

var arsitename=new Array();
arsitename[0]="sea";
arsitename[1]="sa";
arsitename[2]="au";
arsitename[3]="nz";
arsitename[4]="hk";
arsitename[5]="tw";
arsitename[6]="cn";
arsitename[7]="kr";
arsitename[8]="jp";

var arsiteurl=new Array();
arsiteurl[0]="http://www.visa-asia.com/ap/sea/index.shtml";
arsiteurl[1]="http://www.visa-asia.com/ap/sa/index.shtml";
//arsiteurl[2]="/ap/au/index.shtml";
arsiteurl[2]="http://www.visa.com.au";
//arsiteurl[3]="/ap/nz/index.shtml";
arsiteurl[3]="http://www.visa.co.nz";
//arsiteurl[4]="/ap/hk/zh_hk/index.shtml";
arsiteurl[4]="http://www.visa.com.hk";
//arsiteurl[5]="/ap/tw/index.shtml";
arsiteurl[5]="http://www.visa.com.tw";
//arsiteurl[6]="/ap/cn/index.shtml";
arsiteurl[6]="http://www.visa.com.cn";
arsiteurl[7]="http://www.visa-asia.com/ap/kr/index.shtml";
//arsiteurl[8]="/ap/jp/index.shtml";
arsiteurl[8]="http://www.visa.co.jp";

var totalsite = arsiteurl.length;

//--Here you try to read the cookie and see whether there is value in it and assign the value to a variable.

var sitename = GetCookie('visaasia-redirect');

if (sitename != null) {
	var siteurl = GetURL(sitename);
	document.location.href = siteurl;
}

function GetURL(sitename) {
	var siteurl = "";
	for (i=0; i < totalsite; i++){
		if (sitename == arsitename[i]){
			siteurl = arsiteurl[i];
		}
	}
	if (siteurl == ""){
		siteurl = domain + "/ap/sea/index.shtml";
	}
	return siteurl;
}


function goredirect( sitename, checked ) {
	if (checked){
		// save cookie
		pathname = location.pathname;
		myDomain = pathname.substring(0,pathname.lastIndexOf('/')) +'/';
		var largeExpDate = new Date ();
		largeExpDate.setTime(largeExpDate.getTime() + (30 * 24 * 3600 * 1000));
		SetCookie('visaasia-redirect',sitename,largeExpDate,myDomain);
	}
	// redirect user
	var siteurl = GetURL(sitename);
	document.location.href = siteurl;
}

//---- This are all the functions for manipulate the cookies.

function getCookieVal (offset) {
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == -1)
      endstr = document.cookie.length;
   return unescape(document.cookie.substring(offset, endstr));
}

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 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" : "");
}

