/* -------------------------------------------------------------------------
 *
 * JavaScript for NNI, version 1.1.4
 *   (C) 2009 Nikkei Inc. All rights reserved.
 *
 * ------------------------------------------------------------------------- */

var NNI = {
	"version":				"1.1.4",
	"shortCookieName":		"NNI_ID",
	"longCookieName":		"NNI_ID2",
	"longCookie2Name":		"NNI_ID3",
	"freeUrl":				"/e/fr/freetop.aspx",
	"regUrl":				"/e/ac/regtop.aspx",
	"redirectUrl":			"/e/ac/re.cgi",
	"MES01":				"This browser cannot access Nikkei.com. Nikkei.com is designed to work with the latest versions of Internet Explorer and Firefox.",
	"cookieDomain":			"e.nikkei.com",
	"cookiePath":			"/"
};

function getCookie(key){

	var cookies;
	var value = "";
	
	cookies = document.cookie.split("; ");
	for (var i = 0; i < cookies.length; i++) {
		var str = cookies[i].split("=");
		if (str[0] == key) {
			value = unescape(str[1]);
			break;
		}
	}

	return(value);

}

function getCookie_ex(key){

	var cookies;
	var value = "";

	cookies = document.cookie.split("; ");
	for (var i = 0; i < cookies.length; i++) {
		var str = cookies[i].split("=");
		if(key == NNI.longCookie2Name){
			if (str[0].indexOf(key, 0) != -1) {
				value = unescape(str[1]);
				break;
			}
		}
		else {
			if (str[0] == key) {
				value = unescape(str[1]);
				break;
			}
		}
	}
	
	return(value);

}

function getCookie_ex_nodec(key){

	var cookies;
	var value = "";

	cookies = document.cookie.split("; ");
	for (var i = 0; i < cookies.length; i++) {
		var str = cookies[i].split("=");
		if(key == NNI.longCookie2Name){
			if (str[0].indexOf(key, 0) != -1) {
				value = str[1];
				break;
			}
		}
		else {
			if (str[0] == key) {
				value = str[1];
				break;
			}
		}
	}
	
	return(value);

}

function AutoLogin(url){
	location.href = url;
}

function CookieCheck(auto_login){

var Name;
var Name2;
var Name3;
var buf;
var Cookie;

	//2002.11.15:init
	
	Name = getCookie_ex(NNI.shortCookieName);
	Name2 = getCookie_ex(NNI.longCookieName);
	Name3 = getCookie_ex(NNI.longCookie2Name);

	if (Name != "") {
		AutoLogin(NNI.regUrl);
	}
	else {

		AutoLogin(NNI.freeUrl);
	}
}

function formLoad() {
  var tabView = new YAHOO.widget.TabView('tabviewMyportfolio');

$(document).ready(function(){
	$("#myArticleToggle").click(function () {
		if ($("#myArticle").is(":hidden")) {
			$("#myArticle").slideDown("slow");
			$("#myArticleToggleBtn").attr("src","/e/common/images/btn_close.gif");
		} else {
			$("#myArticle").slideUp("slow");
			$("#myArticleToggleBtn").attr("src","/e/common/images/btn_open.gif");
		}
	});
	$("#myPortfolioToggle").click(function () {
		if ($("#myPortfolio").is(":hidden")) {
			$("#myPortfolio").slideDown("slow");
			$("#myPortfolioToggleBtn").attr("src","/e/common/images/btn_close.gif");
		} else {
			$("#myPortfolio").slideUp("slow");
			$("#myPortfolioToggleBtn").attr("src","/e/common/images/btn_open.gif");
		}
	});
});

  loadMyPortfolio("SPORT");
  loadMyArticle("SNEWS");
}

function loadMyPortfolio(key) {
  loadMyData(key + "2", "myPortfolio");
}
function loadMyArticle(key) {
  loadMyData(key + "1", "myArticle");
}

function loadMyData(key, divID){
    var xmlhttp=false;
    if(typeof ActiveXObject!="undefined"){ /* IE5, IE6 */
        try {
            xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); /* MSXML3 */
        }
        catch(e){
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); /* MSXML2 */
        }
    }
    if(!xmlhttp && typeof XMLHttpRequest!="undefined"){
        xmlhttp=new XMLHttpRequest(); /* Firefox, Safari, IE7 */
    }
    if(!xmlhttp){
        alert(NNI.MES01);
        return false;
    }

	xmlhttp.onreadystatechange=function(){
        if(xmlhttp.readyState==4 && xmlhttp.status==200){
            document.getElementById(divID).innerHTML=xmlhttp.responseText;
            if (trim(xmlhttp.responseText) == "") {
            	if (divID == "myArticle") {
            		document.getElementById("myArticleArea").innerHTML="";
            	} else if (divID == "myPortfolio") {
            		document.getElementById("myPortfolioArea").innerHTML="";
            	}
            }
        }
    }

	var dummy = "&dummy=" + (new Date()).getTime();
    xmlhttp.open("GET","/e/servlet/Transform?p=" + key + dummy,true);
    xmlhttp.send(null);
}

function trim(stringValue) {
return stringValue.replace(/(^\s*|\s*$)/, "");
}

function clearNniidCookie() {
    deleteCookie(NNI.longCookieName);
    deleteCookieLike(NNI.longCookie2Name);
}

function getNniid() {
	var sep = "~~~";
	var shortCookie = getCookieValue(NNI.shortCookieName);

	if (!shortCookie) {
		return("");
	}

	var pos = shortCookie.indexOf(sep, 0);
	if (pos != -1) {
		shortCookie = shortCookie.substring(0, pos);
	}
	var nniid = "";
	
	for (var i=0; i<shortCookie.length; i++) {
		nniid = nniid + "%" + shortCookie.substring(i,i+2);
		i++;
	}
	return unescape(nniid);
}

function getMachineid() {
	var sep = "~~~";
	var shortCookie = getCookieValue(NNI.shortCookieName);

	if (!shortCookie) {
		return("");
	}

	pos = shortCookie.indexOf(sep, 0);
	if (pos < 0) {
		return("");
	}
	
	pos = shortCookie.indexOf(sep, pos + 1);
	if (pos < 0) {
		return("");
	}

	pos = shortCookie.indexOf(sep, pos + 1);
	if (pos < 0) {
		return("");
	}

	endpos = shortCookie.indexOf(sep, pos + 1);
	if (endpos < 0) {
		return("");
	}
	
	if (endpos - pos <= sep.length) {
		return("");
	}

	return shortCookie.substring(pos + sep.length, endpos);
}

function getCookieValue (cookieName) {
  var exp = new RegExp (escape(cookieName) + "=([^;]+)");
  if (exp.test (document.cookie + ";")) {
    exp.exec (document.cookie + ";");
    return unescape(RegExp.$1);
  }
  else return false;
}

function deleteCookie (cookieName) {

	var expireDate = new Date ();
	expireDate.setYear(expireDate.getYear() - 1);
	document.cookie = escape(cookieName) + "=" + "; domain=" + NNI.cookieDomain + "; path=" + NNI.cookiePath + "; expires=" + expireDate.toGMTString();
	return true;
}

function deleteCookieLike (cookieName) {
  var exp = new RegExp ("(" + escape(cookieName) + "[^=;]+)=([^;]+)", "gi");
  var buffer = document.cookie + ";";

  do {
      var match = exp.exec(buffer);
      if (RegExp.$1) {
          deleteCookie(RegExp.$1);
      }
  } while (exp.lastIndex != 0);

  return true;     
}

function getLoginInfo() {
	var name;
	var cookie;
	var buf;
		
	name = getCookie(NNI.shortCookieName);
	cookie = name.substring(0,4);
	buf = CharCodeToString(cookie);

	return buf.substring(0,2);
}

function getLoginInfoHTML() {
	var info = getLoginInfo();
	if(info=="NE"){
		getUserInfo_NE();
	}else if(info=="NF" || info=="NS" || info=="NM"){
		getUserInfo_NF();
	}else{
		getUserInfo_Logout();
	}
}

function getUserInfo_NE(){
	document.write('<div id="userInfomation">');
	document.write('<img src="/e/common/images/acc_top.gif" width="300" height="10" alt=""/>');
	document.write('<div class="userInfoBox">');
	document.write('<p><strong>Welcome to Nikkei.com</strong><br />Visit Account Details to update your contact info, reset your password, get a receipt, change your subscription options & more!</p>');
	document.write('</div>');
	document.write('<ul class="linkList">');
	document.write('<li><a href="/e/?SUBMIT=LOGOUT"><img src="/e/common/images/btn_acc_logout.gif" width="65" height="26" alt="Logout"/></a></li>');
	document.write('<li><a href="https://reg.e.nikkei.com/e/member.pl"><img src="/e/common/images/btn_acc_ad.gif" width="104" height="26" alt="Account details" /></a></li>');
	document.write('</ul>');
	document.write('</div>');
}

function getUserInfo_NF(){
	document.write('<div id="userInfomation">');
	document.write('<img src="/e/common/images/acc_top.gif" width="300" height="10" alt=""/>');
	document.write('<div class="userInfoBox">');
	document.write('<p><strong>Welcome to Nikkei.com</strong><br />');
	document.write('Visit Account Details to update your user profile or reset your password.</p>');
	document.write('</div>');
	document.write('<ul class="linkList">');
	document.write('<li><a href="/e/?SUBMIT=LOGOUT"><img src="/e/common/images/btn_acc_logout.gif" width="65" height="26" alt="Logout"/></a></li>');
	document.write('<li><a href="https://reg.e.nikkei.com/e/member.pl"><img src="/e/common/images/btn_acc_ad.gif" width="104" height="26" alt="Account details" /></a></li>');
	document.write('</ul>');
	document.write('</div>');
}

function getUserInfo_Logout(){
	document.write('<div id="userInfomation">');
	document.write('<img src="/e/common/images/acc_top.gif" width="300" height="10" alt=""/>');
	document.write('<div class="userInfoBox">');
	document.write('<p><strong>Thank you for visiting Nikkei.com. </strong><br />');
	document.write('Register now and with free access till ');
	subscdate();
	document.write('!<br />');
	document.write('<a href="/e/fr/info/subs/visitors.aspx">Learn more about our site</a> & <a href="/e/fr/info/subs/info.aspx">subscriptions.</a></p>');
	document.write('</div>');
	document.write('<ul class="linkList">');
	document.write('<li><a href="/e/ac/re.cgi"><img src="http://www.nikkei.co.jp/NNI/e/common/images/btn_acc_login.gif" width="122" height="36" alt="Login" /></a></li>');
	document.write('<li><a href="https://reg.e.nikkei.com/e/j1.pl"><img src="http://www.nikkei.co.jp/NNI/e/common/images/btn_acc_regi.gif" width="122" height="36" alt="Register Now!" /></a></li>');
	document.write('</ul>');
	document.write('</div>');
}

function CharCodeToString(strText){
	var sbuf;
	var i;
	var c;

	sbuf='';
	for(i=0; i<strText.length; i=i+2){
		c = ("0x"+ strText.substring(i,i+2));
		sbuf = sbuf + String.fromCharCode(c);
	}
	return(sbuf);
}

function addOnloadEvent(func) {
	if (window.addEventListener) { //for W3C DOM
		window.addEventListener("load", func, false);
	} else if (window.attachEvent) { //for IE
		window.attachEvent("onload", func);
	} else  {
		window.onload = func;
	}
}

// Subscription date JavaScript Start
function subscdate() {
	var myDate = new Date() ;
	var yy = myDate.getUTCFullYear() ;
	var mm = myDate.getUTCMonth()  + 1;
	var dd = myDate.getUTCDate() ;
	var utchh = myDate.getUTCHours() ;
	var endDate = new Array(31,28,31,30,31,30,31,31,30,31,30,31)
	var newYY, newMM, newDD, jsthh;



	newYY = yy
	newMM = mm
	newDD = endDate[newMM - 1]
	jsthh = utchh + 9
	if (jsthh >= 24 ) {
	    dd = dd + 1
	}

	if (dd >= 15 ) {
	    newMM = newMM + 1
		if (newMM > 12 ) {
	    	newMM = 1
	    	newYY = newYY + 1
	    }
        newDD = endDate[newMM - 1]
	}

	if( (newMM == 2) && (newYY%4 == 0) ) {
	     newDD = newDD + 1
	}
	if(newMM < 10 ) { newMM =  "0" + newMM; }

	document.write("<b>" + newDD + "/" + newMM + "/" + newYY + "</b>");
}
// Subscription date JavaScript End

function getCookieNames() {

	var cookies = document.cookie.split("; ");
	var cookieNames = new Array(cookies.length);

	for(var i=0; i < cookies.length; i++) {
		var str = cookies[i].split("=");
		cookieNames[i] = str[0];
	}
	
	return cookieNames;
}
