/*==============================================================
StatusReadWrite.js
readCookie()      //クッキーを読み込んで配列に格納
                  //クッキーがない場合はfalse が返る。
saveStatus()      //配列に格納されたデータをクッキーに保存
makeString()      //配列からカンマ区切りデータを作成取得
deleteCookie()    //クッキー削除

readStatus()      //読み込んだデータを配列に格納

クッキー項目
----------------
1: MD5文字 → ログイン字につくられるor別クッキーに入っている
2: 登録日時 YYYY/MM/dd-hh:mm:ss
3: 更新日時 YYYY/MM/dd-hh:mm:ss
4: Medium化しているBOX番号
5: box0 ピンポイント 地点名称１
6: box0 ピンポイント 地点番号１
7: box0 ピンポイント 県番号1
8: box0 ピンポイント 地点名称２
9: box0 ピンポイント 地点番号２
10: box0 ピンポイント 県番号２
11: box0 ピンポイント 地点名称３
12: box0 ピンポイント 地点番号３
13: box0 ピンポイント 県番号３
14: box1 アプリケーション名称    --+
15: box1 small URL                 |
16: box1 medium URL                |
17: box1 full URL                --+
18: box2 アプリケーション名称
     :
22: box3 アプリケーション名称
     :
25: box4
     :
30: box5
     :
34: box6 アプリケーション名称
35: box6 small URL
36: box6 medium URL
37: box6 full URL
38: box0 ピンポイント エリア名称（北海道のみ）
39: box1 ピンポイント エリア名称（北海道のみ）
40: box2 ピンポイント エリア名称（北海道のみ）

---------------

   箱番号    (rotateとは違うので注意)
  +-----------+
  |     0     |
  +-----------+
  +---+---+---+
  | 1 | 2 | 3 |
  +---+---+---+
  | 4 | 5 | 6 |
  +---+---+---+
==============================================================*/
// 各項目の開始位置
//

function StatusReadWrite(loginsw)
{
   this.pinpointAryPos=4
   this.boxAryPos=13
   this.pinpointAreaNamePos=37
   this.cookieKey="WNIMyWeatherScreen"
//   this.cookieKey="WNIMyScreen"
   this.isFirstAccess=false
   this.saveItem = new Array()            // この配列に格納される。
   this.primaryBoxAry=new Array()
   this.contentBoxAry=new Array()
   this.mideumBoxNo=-1

   //=====================================================================
   //read & write
   //=====================================================================
   this.setMD5key=function(mdkey){this.saveItem[0]=mdkey}
   this.getMD5key=function(){return this.saveItem[0]}

   //以下二つは自動生成
   this.getCreateTime=function(){return this.saveItem[1]}
   this.getUpdateTime=function(){return this.saveItem[2]}

   //第４項目
   this.setMideumSizeBox=function(no)
   {
      //消す場合は-1
      this.saveItem[3]=no
   }
   this.getMideumSizeBox=function(){
      return this.saveItem[3]
   }

   //pinpoint  第5〜13項目
   this.setPinpointInfo=function(ptName1,ptNo1,ptPref1,ptAreaName1,
                                 ptName2,ptNo2,ptPref2,ptAreaName2,
                                 ptName3,ptNo3,ptPref3,ptAreaName3)
   {
      this.saveItem[4]=ptName1
      this.saveItem[5]=ptNo1
      this.saveItem[6]=ptPref1
      this.saveItem[7]=ptName2
      this.saveItem[8]=ptNo2
      this.saveItem[9]=ptPref2
      this.saveItem[10]=ptName3
      this.saveItem[11]=ptNo3
      this.saveItem[12]=ptPref3

      this.saveItem[37]=ptAreaName1
      this.saveItem[38]=ptAreaName2
      this.saveItem[39]=ptAreaName3
   }

   this.getPinpointInfo=function()
   {
      //return [[ptName1,ptNo1,ptPref1,ptAreaName1],[ptName2,,],[,,,ptPref3]]
      var refAryCnt=this.pinpointAryPos
      var retary = new Array
      for (i=0;i<3;i++){
         var tmpAry = new Array(4)
         for(m=0;m<3;m++){
            tmpAry[m]=this.saveItem[refAryCnt]
            refAryCnt++
         }
         tmpAry[3]=this.saveItem[this.pinpointAreaNamePos+i] //後から追加
         retary.push(tmpAry)
      }
      return retary
   }
   // boxno 1〜6   pinpoint  第14〜37項目  (4*6box)
   this.setContentBoxInfo=function(boxno,appName,url1,url2,url3)
   {
      if (boxno<1 || boxno==null){
         return false
      }
      var setpos=this.boxAryPos+(boxno-1)*4
      this.saveItem[setpos]=appName
      this.saveItem[setpos+1]=url1
      this.saveItem[setpos+2]=url2
      this.saveItem[setpos+3]=url3
      return true
   }
   this.getContentBoxInfo=function(boxno)
   {
      if (boxno<=0 || boxno>6) return null
      var pos=this.boxAryPos + (boxno-1)*4     //4は1box内のアイテム
      ansary = new Array()
      ansary[0]=this.saveItem[pos]
      ansary[1]=this.saveItem[pos+1]
      ansary[2]=this.saveItem[pos+2]
      ansary[3]=this.saveItem[pos+3]
      return ansary
   }

   this.clearContentBoxInfo=function(boxno)
   {
      if (boxno<1 || boxno==null){
         return false
      }
      var setpos=this.boxAryPos+(boxno-1)*4
      this.saveItem[setpos]=""
      this.saveItem[setpos+1]=""
      this.saveItem[setpos+2]=""
      this.saveItem[setpos+3]=""
      return true
   }

   //=====================================================================
   //read & write
   //=====================================================================
   this.makeString=function(){
        return this.saveItem.join(",")
   }

    this.whatdomain =function()
    {
       var arg = location.href.split("?");
       var mysite = arg.shift();

       if (mysite.indexOf("wni.co.jp") > 0) {
           mysite = ".wni.co.jp";
       } else if (mysite.indexOf("weathernews.jp") > 0) {
           mysite = ".weathernews.jp";
       } else {
           mysite = "";
       }
       return mysite;
    }
    this.saveStatus=function(dbstr)
    {
      /*md5*/
      if (this.saveItem[0]==null){
          this.saveItem[0]="dummy"
      }
      tm= new Date();
      year = tm.getYear();    if (year < 1900) year += 1900;
      mon  = tm.getMonth()+1; if (mon < 10) mon = "0" + mon;
      day = tm.getDate();    if (day< 10) day = "0" + day;
      hour = tm.getHours();   if (hour < 10) hour = "0" + hour;
      min = tm.getMinutes();   if (min < 10) min = "0" + min;
      sec = tm.getSeconds();   if (sec < 10) sec = "0" + sec;
      nowstr=year+"/"+mon+"/"+day+"-"+hour+":"+min+":"+sec;

      /*touroku time*/
      if (this.isFirstAccess ||this.saveItem[1]==null){
         this.saveItem[1]=nowstr
      }

      /*kousin time*/
      this.saveItem[2]=nowstr

      if (dbstr==null || dbstr.length <10){
          dbstr = escape(this.makeString())
      }
      document.cookie = this.cookieKey + "=" + dbstr  +
       ";expires=Thu, 1-Jan-2030 00:00:00 GMT" +
       ";domain=" + this.whatdomain() +
       ";path=/;";

      this.isFirstAccess=false
    }

   this.readStatus=function(str)
   {
       this.saveItem=str.split(",")
       return this.saveItem
   }
   this.readLocalCookie = function()
   {
      var tmp=unescape(document.cookie)+";"
      var pos1=0,pos2=0
      var lastpos=0
      var keystr, valstr
      termary=tmp.split(";")

      for (i=0;i<termary.length;i++){
         term=termary[i].split("=")
         if (term!=null && term.length>=2){
            if (term[0].indexOf("WNIMyScreen") >= 0) { // oldkey
		document.cookie = "WNIMyScreen=" + term[1] +
		";expires=Mon, 1-Jan-1990 00:00:00 GMT";
                this.readStatus(term[1])
                this.isFirstAccess=false
                return(true);
            }
            if (term[0].indexOf(this.cookieKey) >= 0){
                this.readStatus(term[1])
                this.isFirstAccess=false
                return(true);
            }
         }
      }
      this.isFirstAccess=true
      return (false)
   }
   this.readCookie = function(loginSW,dbstr)
   {
       if (loginSW==null){
           loginSW=0
           dbstr=""
       }
       if (loginSW==1 && dbstr.length > 10){
           this.saveStatus(dbstr)
       }

       return this.readLocalCookie()
   }

   this.deleteCookie=function()
   {
      var tmp=this.cookieKey+"=;"
      tmp+="expires=Mon, 1-Jan-1990 00:00:00 GMT"
      document.cookie = tmp
      this.isFirstAccess=true
   }
}

