var cookieControll=function(){
  this._period=7;
  this._name="pageHistory_2010091";
  this._reg=/pageHistory_2010091\[\d+?\]='(.+?)'/gm;
  this._history=new Array;
  this._stack=new Array;
  this._currentFlag={flag:false,num:null};
  this._domain={
    "nissan-cms2.jazz.netgrid.jp" : "netgrid.jp",
    "nissan-cms.jazz.netgrid.jp" : "netgrid.jp",
    "nissan-cms.netgrid.co.jp" : "netgrid.co.jp",
    "wctcms.nissan.co.jp" : "nissan.co.jp",
    "wwwpro.nissan.co.jp" : "nissan.co.jp",
    "www.nissan.co.jp" : "nissan.co.jp",
    "www2.nissan.co.jp" : "nissan.co.jp"
  }
}
cookieControll.prototype={
  setCookie:function(){
    var nowtime = new Date().getTime(),
    clear_time = new Date(nowtime + (60 * 60 * 24 * 1000 * this._period)),
    expires = clear_time.toGMTString(),
    history
    
    var pagaTitleText = $("title").html();
    var carNameText = pagaTitleText.split("：")[1].split("[")[0];
    var pageNameText = pagaTitleText.split("] ")[1];
    var search = location.search;
    
    pageNameText = pageNameText.split(" ｜ ")[ pageNameText.split(" ｜ ").length - 1 ];
    if(pageNameText.indexOf("Webカタログ トップ")>0) pageNameText = "Webカタログ トップ";
    
    search = ( search.length>0 )? search : "";
    
    (this._history.length>0)?
      this._history.unshift(escape("'"+( carNameText + "：" + pageNameText ) +"$"+location.pathname.replace("index.html","")+ search +"'")):
      this._history.push(escape("'"+( carNameText + "：" + pageNameText )+"$"+location.pathname.replace("index.html","") + search +"'"))
    if(this._history.length>3){
      this._history.pop();
      history=this._history;
    }else{
      history=this._history;
    }
    for(var i=0,len=history.length;i<len;i++){
      document.cookie=this._name+"["+i+"]="+escape(unescape(history[i]).replace(/(pageHistory_2010091\[\d+?\]=)|;/,""))+";"+" expires="+expires+";path=/;domain="+this._domain[location.hostname]+";";
    }
    
  },
  deleteOldData : function(){
    var rold = /pageHistory\[\d+?\]=/,
        ck = unescape( document.cookie ).match( rold ),
        nowtime = new Date().getTime(),
        clear_time = new Date(nowtime + (60 * 60 * 24 * 1000 * -100 )),
        expires = clear_time.toGMTString()
    if( ck && ck.length > 0 ){
      for( var i =0;i<3;++i ){
        document.cookie = "pageHistory["+i+"]=;path=/;domain="+this._domain[location.hostname]+";expires="+expires
        document.cookie = "pageHistory["+i+"]=;path=/;expires="+expires
      }
    }
  },
  foreach : function(arr,f){
    for(var i=0,len=arr.length;i<len;i++){
      f.call(arr[i],i)
    }
  },
  getCookie:function(){
    var history=unescape(document.cookie).match(this._reg),
    pageHistory_2010091=[];
    if(history && history.length>0){
      this._history=history;
      eval(history.join(";"));
    }
    this.setPageHistory(pageHistory_2010091,$("dl.past_navi"),this._stack)
  },
  setPageHistory:function(arr,nav,stack){
    var that=this;
    if(arr.length<1){
      nav.append("<dd>まだありません</dd>");
    }else{
      this.foreach(arr,function(iter){
        if(this.split("$")[1] ===location.pathname.replace("index.html","") + location.search ){
          that._currentFlag.flag=true;
          that._currentFlag.num=iter;
        }
      })
      if(this._currentFlag.flag==true){
        var temp=arr[this._currentFlag.num];
        this._history.splice(this._currentFlag.num,1);
      }
      for(var i=0;i<3;i++){
        if(arr[i]){
        (function(m){
          try{
            if(!/layouter/.test(location.href)){
              var prefix="<a href="+"\""+location.protocol+ "//" + location.hostname  + arr[i].split("$")[1]+"\""+">" + arr[i].split("$")[0]
              nav.append("<dd>" + prefix+"</a>" + "</dd>")
              stack.push(arr[i].split("$")[1])
            }
          }catch(e){
            var nowtime = new Date().getTime(),
            clear_time = new Date(nowtime + (60 * 60 * 24 * 1000 * -1)),
            expires = clear_time.toGMTString();
            document.cookie="pageHistory_2010091["+i+"]="+"null;expires"+expires+";path=/;"
          }
        })(this._currentFlag)
        }
      }
      }
  }
}
var cookieControll=new cookieControll();
cookieControll.deleteOldData();
$(function(){
  cookieControll.getCookie();
  $("input.zipsup1").attr("maxlength","3")
  $("input.zipsup2").attr("maxlength","4")
});
window.onunload=function(){
  cookieControll.setCookie();
}
function SetLayZip(p,test,modelName){
  modelName = modelName || modelNameRoma;
  function byClass(n,Class){
    for(var i=0,collection=(n)?n.getElementsByTagName("input"):document.getElementsByTagName("input"),len=collection.length,reg=new RegExp("\\b"+Class+"\\b"),stack=new Array;i<len;i++)
      if(reg.test(collection[i].className))stack.push(collection[i])
        return stack;
  }
  function getVal(args,sep){
    for(var i=0,arr=new Array,len=args.length;i<len;i++)arr.push(args[i].value);
      return (sep)?arr.join(sep):arr.join("");
  }
  // input check
  var obj = p;
  var fv = getVal(byClass(p,"zipsup1"));
  var fw = getVal(byClass(p,"zipsup2"));
  var url =
    (test)?
      "http://map2.nissan.co.jp/c/f/TESTDRIVE/"+modelName+"/":
      "http://map2.nissan.co.jp/c/f/DEALER/"+modelName+"/";
  if ((fv == '')|/\s/.test(fv)|/\s/.test(fw)){
    if((fv.length < 3)|(fv.length > 3)){
      window.open(url);
      return false;
    }else if(/[^0-9]+/.test(fv)){
      window.open(url);
      return false;
    }else if(/[^0-9]+/.test(fw)){
      window.open(url);
      return false;
    }
  }
  obj.submit();
} 
