var cookieControll=function(){
		this._period=7;
		this._name="pageHistory";
		this._reg=/pageHistory\[\d+?\]='(.+?)'/gm;
		this._history=new Array;
		this._stack=new Array;
		this._currentFlag={flag:false,num:null};
	}
	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
			(this._history.length>0)?
				this._history.unshift(escape("'"+$("title").html()+"$"+location.href.replace("index.html","")+"'")):
				this._history.push(escape("'"+$("title").html()+"$"+location.href.replace("index.html","")+"'"))
			if(this._history.length>6){
				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\[\d+?\]=)|;/,""))+";"+" expires="+expires+";path=/;";
			}
		},
		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=[];
			if(history && history.length>0){
				this._history=history;
				eval(history.join(";"));
			}
			this.setPageHistory(pageHistory,$("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].replace("index.html","")==location.href.replace("index.html","")){
						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<6;i++){
					if(arr[i]){
					(function(m){
						try{
							if(!/layouter/.test(location.href)){
								var pagaTitleText = arr[i].split("$")[0];
								var carNameText = pagaTitleText.split("：")[1].split("[")[0];
								var pageNameText = pagaTitleText.split("] ")[1];
								pageNameText = pageNameText.split(" ｜ ")[ pageNameText.split(" ｜ ").length - 1 ];
								if(pageNameText.indexOf("Webカタログ トップ")>0) pageNameText = "Webカタログ トップ";
								
								var prefix="<a href="+"\""+arr[i].split("$")[1]+"\""+">" + carNameText + "：" + pageNameText
								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["+i+"]="+"null;expires"+expires+";path=/;"
						}
					})(this._currentFlag)
					}
				}
				}
		}
	}
	var cookieControll=new cookieControll();
	$(function(){
		cookieControll.getCookie();
		$("input.zipsup1").attr("maxlength","3")
		$("input.zipsup2").attr("maxlength","4")
	});
	window.onunload=function(){
		cookieControll.setCookie();
	}
	
////////////////////////////////////////////////////////	
	
	
	function SetLayZip(p,test){
	var model_name=(modelNameRoma==="RIO")?"CLIPPER/RIO" : 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("");
	}
	var obj = p;
	// 入力チェック
	var fv = getVal(byClass(p,"zipsup1")),
	fw = getVal(byClass(p,"zipsup2")),
	url=(test)?"http://map2.nissan.co.jp/c/f/TESTDRIVE/"+model_name+"/":"http://map2.nissan.co.jp/c/f/DEALER/"+model_name+"/";
	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();
} 
