﻿// JavaScript Document

var access = GetCookie("ACCESS");
var date   = GetCookie("DATE");
var dayflg = GetCookie("DAYFLG");

if(access == ""){access = 0;}
if(dayflg == ""){dayflg = 0;}

var ndate = new Date();
var year  = ndate.getYear();
var month = ndate.getMonth() + 1;
var day   = ndate.getDate();
var hour  = ndate.getHours();
var min   = ndate.getMinutes();
var sec   = ndate.getSeconds();
if(year < 1900){year += 1900;}
if(month < 10){month = "0" + month;}
if(day < 10){day = "0" + day;}
if(hour < 10){hour = "0" + hour;}
if(min < 10){min = "0" + min;}
if(sec < 10){sec = "0" + sec;}

var cdate = "" + year + "" + month + "" + day;
SetCookie("DATE", cdate);

var byear = date.substring(0,4);
var bmon = date.substring(4,6);
var bday = date.substring(6,8);

apday = new Date(byear,bmon-1,bday);
dayms = 24 * 60 * 60 * 1000;
gap = Math.floor((ndate.getTime()-apday.getTime())/dayms);

if(bmon != month){access = 0; dayflg = 0;}

if(access == 0){
	var fdate = new Date();
	var fyear  = fdate.getYear();
	var fmonth = fdate.getMonth() + 1;
	var fday   = fdate.getDate();
	if(fday == 1){
	fdate.setMonth(fmonth-1);
	var fmonth = fdate.getMonth();
	}
	fdate.setDate(fday-1);
	var fday   = fdate.getDate();
	var fhour  = fdate.getHours();
	fdate.setHours(fhour-2);
	var fhour  = fdate.getHours();
	var fmin   = fdate.getMinutes();
	var fsec   = fdate.getSeconds();
	if(fyear < 1900){fyear += 1900;}
	if(fmonth < 10){fmonth = "0" + fmonth;}
	if(fday < 10){fday = "0" + fday;}
	if(fhour < 10){fhour = "0" + fhour;}
	var firsttime = fyear + "年" + fmonth + "月" + fday + "日 " + fhour + "時";
	document.write('<span class="mspg14px-150">' , firsttime , '現在、</span><span class="mspg12pt-175red"><strong>残り');
	document.write('１６個</strong></span><span class="mspg14px-150">となっております。</span>');
	
	SetCookie("ACCESS", ++access);
	SetCookie("DAYFLG", 0);
}else if(access == 1){
	if(date == cdate){
		if(dayflg == 0){
			var secondtime = year + "年" + month + "月" + day + "日 " + hour + "時";
			document.write('<span class="mspg14px-150">' , secondtime , '現在、</span><span class="mspg12pt-175red"><strong>残り');
			document.write('１４個</strong></span><span class="mspg14px-150">となっております。</span>');
		
			SetCookie("DAYFLG", 1);
		}else if(dayflg == 1){
			var secondtime = year + "年" + month + "月" + day + "日 " + hour + "時";
			document.write('<span class="mspg14px-150">' , secondtime , '現在、</span><span class="mspg12pt-175red"><strong>残り');
			document.write('１１個</strong></span><span class="mspg14px-150">となっております。</span>');
		}
	}else if((date != cdate) && (gap == 1)){
		var secondtime = year + "年" + month + "月" + day + "日 " + hour + "時";
		document.write('<span class="mspg14px-150">' , secondtime , '現在、</span><span class="mspg12pt-175red"><strong>残り');
		document.write('９個</strong></span><span class="mspg14px-150">となっております。</span>');

		SetCookie("ACCESS", ++access);
		SetCookie("DAYFLG", 0);
	}else if((date != cdate) && (gap == 2)){
		var secondtime = year + "年" + month + "月" + day + "日 " + hour + "時";
		document.write('<span class="mspg14px-150">' , secondtime , '現在、</span><span class="mspg12pt-175red"><strong>残り');
		document.write('３個</strong></span><span class="mspg14px-150">となっております。</span>');

		SetCookie("ACCESS", 3);
		SetCookie("DAYFLG", 0);
	}else if((date != cdate) && (gap >= 3)){
		var secondtime = year + "年" + month + "月" + day + "日 " + hour + "時";
		document.write('<span class="mspg14px-150">' , secondtime , '現在、</span><span class="mspg12pt-175red"><strong>残り');
		document.write('わずか</strong></span><span class="mspg14px-150">となっております。</br>');
		document.write('終了次第販売を停止いたします。</span>');

		SetCookie("ACCESS", 3);
		SetCookie("DAYFLG", 0);
	}
}else if(access == 2){
	if(date == cdate){
		if(dayflg == 0){
			var secondtime = year + "年" + month + "月" + day + "日 " + hour + "時";
			document.write('<span class="mspg14px-150">' , secondtime , '現在、</span><span class="mspg12pt-175red"><strong>残り');
			document.write('７個</strong></span><span class="mspg14px-150">となっております。</span>');
			
			SetCookie("DAYFLG", 1);
		}else if(dayflg == 1){
			var secondtime = year + "年" + month + "月" + day + "日 " + hour + "時";
			document.write('<span class="mspg14px-150">' , secondtime , '現在、</span><span class="mspg12pt-175red"><strong>残り');
			document.write('４個</strong></span><span class="mspg14px-150">となっております。</span>');
		}
	}else if((date != cdate) && (gap == 1)){
		var secondtime = year + "年" + month + "月" + day + "日 " + hour + "時";
		document.write('<span class="mspg14px-150">' , secondtime , '現在、</span><span class="mspg12pt-175red"><strong>残り');
		document.write('３個</strong></span><span class="mspg14px-150">となっております。</span>');

		SetCookie("ACCESS", 3);
		SetCookie("DAYFLG", 0);
	}else if((date != cdate) && (gap >= 2)){
		var thirdtime = year + "年" + month + "月" + day + "日 " + hour + "時";
		document.write('<span class="mspg14px-150">' , thirdtime , '現在、</span><span class="mspg12pt-175red"><strong>残り');
		document.write('わずか</strong></span><span class="mspg14px-150">となっております。</br>');
		document.write('終了次第販売を停止いたします。</span>');

		SetCookie("ACCESS", 3);
		SetCookie("DAYFLG", 0);
	}
}else{
	var thirdtime = year + "年" + month + "月" + day + "日 " + hour + "時";
	document.write('<span class="mspg14px-150">' , thirdtime , '現在、</span><span class="mspg12pt-175red"><strong>残り');
	document.write('わずか</strong></span><span class="mspg14px-150">となっております。</br>');
	document.write('終了次第販売を停止いたします。</span>');
}


function GetCookie(key){
	var tmp = document.cookie + ";";
	var index1 = tmp.indexOf(key, 0);
	if(index1 != -1){
		tmp = tmp.substring(index1,tmp.length);
		var index2 = tmp.indexOf("=",0) + 1;
		var index3 = tmp.indexOf(";",index2);
		return(unescape(tmp.substring(index2,index3)));
	}
	return("");
}

function SetCookie(key, val){
	document.cookie = key + "=" + escape(val) + ";expires=Fri, 31-Dec-2030 23:59:59;";
}
