// ---------------------------------------------------------------------------
//  HTML 依存部（カレンダー側）
// ---------------------------------------------------------------------------
function calScriptOnCalSelect()
{
	var text = '';
	text += 'self.document.mainform.CALEVENT.value = 1;';
	text += 'self.document.mainform.CALMONTH.value = month;';
	text += 'self.document.mainform.CALDATE.value  = date;';
	return text;
}
/*ローカルデバッグ用------------------------*/
/*var servertime = 1247019998000;//---------/
/*----------------------------------------*/

// ---------------------------------------------------------------------------
//  HTML 依存部（メイン側）
// ---------------------------------------------------------------------------
function calCalenderInit(){
	calWritePage(1);
	var tabText={"tab1":"出発日","tab2":"返却日"}
	
	$("#searchCalendarTab1").append("<span>"+tabText.tab1+"</span>");
	$("#searchCalendarTab1 a").hide();
	$("#searchCalendarTab2").append("<span>"+tabText.tab2+"</span>");
	$("#searchCalendarTab2 span").hide();
}




function calCalendar(sel){
	calOnInterval();
	document.mainform.CALSELECT.value = sel;
	calOpenCalendar();
}
function calChange(month, date){
	if (document.mainform.CALSELECT.value == 1) {
		calSelectDate(document.mainform.fromMonth, document.mainform.fromDay, month, date);
		calSelectDate(document.mainform.toMonth, document.mainform.toDay, month, date);
	} else if (document.mainform.CALSELECT.value == 2) {
		calSelectDate(document.mainform.toMonth, document.mainform.toDay, month, date);
	}
}
function calOnInterval(){
	if (document.mainform.CALEVENT.value != 0) {
		document.mainform.CALEVENT.value = 0;
		calChange(document.mainform.CALMONTH.value,
		          document.mainform.CALDATE.value);
	}
}

function zeroFill(val){
	var str = ""+val;
	if(str.length < 2){
		str = ""+"0"+str;
	}
	return str;	
}

function calTopInitialize(){
	calTodayDate = new Date(servertime);
	calStartDate = new Date(servertime + (      1 * 60 * 60000));//1時間後
	calEndDate   = new Date(servertime + (90 * 24 * 60 * 60000));//90日後
//	calInitDate  = new Date(servertime + ( 1 * 24 * 60 * 60000));//翌日

	//30または00分への差を得る
	startDayMinutesBase = calStartDate.getMinutes();
	if(startDayMinutesBase == 00 || startDayMinutesBase == 30){
		var svn = 0;
	}else if(startDayMinutesBase > 00 && startDayMinutesBase < 30){
		var svn = 30 - startDayMinutesBase;
	}else{
		var svn = 60 - startDayMinutesBase;
	}
	
	calStartDateSet = new Date(servertime + ( 1 * (60+svn) * 60000));//1時間+svn時間後
	startDayHours = calStartDateSet.getHours();
	startDayMinutes = calStartDateSet.getMinutes();
	
	//0時0分ならば1分繰り上げ
	if(startDayHours==00 && startDayMinutes==00){
		startDayMinutes = ""+01;
	}
	//時間のゼロ埋め処理
	startDayHours		= zeroFill(startDayHours,2);
	startDayMinutes	= zeroFill(startDayMinutes,2);
	
	calBrowserCheck();
	//月日の初期値をセット
	calSelectInitialDate(document.mainform.fromMonth,
	                     document.mainform.fromDay, calStartDateSet);
	calSelectInitialDate(document.mainform.toMonth,
	                     document.mainform.toDay, calStartDateSet);
	//時間の初期値をセット
	document.mainform.fromTime.selectedIndex	= time2index(startDayHours+""+startDayMinutes);
	document.mainform.toTime.selectedIndex		= time2index("1800");
	
	calCalendar(1);
	setInterval(calOnInterval, 100);
}

/* Convert Time 2 selectIndexNumber.
--------------------------------------------*/
timeList = new Object();
timeList ={
"0001":1, "0030":2, "0100":3, "0130":4, "0200":5, 
"0230":6, "0300":7, "0330":8, "0400":9, "0430":10, 
"0500":11, "0530":12, "0600":13, "0630":14, "0700":15, 
"0730":16, "0800":17, "0830":18, "0900":19, "0930":20, 
"1000":21, "1030":22, "1100":23, "1130":24, "1200":25, 
"1230":26, "1300":27, "1330":28, "1400":29, "1430":30, 
"1500":31, "1530":32, "1600":33, "1630":34, "1700":35, 
"1730":36, "1800":37, "1830":38, "1900":39, "1930":40, 
"2000":41, "2030":42, "2100":43, "2130":44, "2200":45, 
"2230":46, "2300":47, "2330":48, "2359":49
};
function time2index(time){
	return timeList[time];
}
/*--------------------------------------------*/

function calCheckMemberID(members){
	var str = document.mainform.number.value;
	var newstr = "";
	var i, code;
	for (i = 0; i < str.length; i++) {
		code = str.charCodeAt(i);
		if (48 <= code && code <= 57) {
			newstr = newstr + str.charAt(i);
		}
	}
	document.mainform.number.value = newstr;

	if (!members && (document.mainform.number.value != "")) {
		alert("「会員ではない方」の場合、会員番号は入力しないでください。");
		return false;
	}
	if (members && (document.mainform.number.value  == "")) {
		alert("「会員の方」の場合、会員番号を入力してください。\n会員番号は、「スペース」や「-」を除き、半角数字のみ、ご入力ください。");
		return false;
	}

	var str = document.mainform.number.value;
	var i, code;
	for (i = 0; i <str.length; i++) {
		code = str.charCodeAt(i);
		if (!(48 <= code && code <= 57)) {
			alert("会員番号は半角数字で入力してください。\n「スペース」や「-」は除いてご入力ください。"); 
			return false;
		}
	}

	return true;
}
function calCheckInput(members, copy){
	calOnInterval();
	if (!calCheckMemberID(members)) {
		return false;
	}
	if (!calCheckInputSub(document.mainform.fromYear,
			      document.mainform.fromMonth,
			      document.mainform.fromDay,
			      document.mainform.fromTime,
			      document.mainform.toYear,
			      document.mainform.toMonth,
			      document.mainform.toDay,
			      document.mainform.toTime)) {
		return false;
	}
	if (copy == 2) {
		calCopyDateToText(document.mainform2.fromYear,
				  document.mainform2.fromMonth,
				  document.mainform2.fromDay,
				  document.mainform2.fromTime,
				  document.mainform.fromYear,
				  document.mainform.fromMonth,
				  document.mainform.fromDay,
				  document.mainform.fromTime);
		calCopyDateToText(document.mainform2.toYear,
				  document.mainform2.toMonth,
				  document.mainform2.toDay,
				  document.mainform2.toTime,
				  document.mainform.toYear,
				  document.mainform.toMonth,
				  document.mainform.toDay,
				  document.mainform.toTime);
		var i;
		for (i = 0; i < document.mainform.reserveType.length; i ++){ 
			if (document.mainform.reserveType[i].checked){ 
				document.mainform2.reserveType.value
				= document.mainform.reserveType[i].value;
			}
		}
	}

	return true;
}
function checkBiz(){
	var date = new Date();
	if ((date.getDay() == 0) ||
	    ((date.getDay()   >= 1 && date.getDay()   <= 5) &&
	     (date.getHours()  < 9 || date.getHours() >= 20)) ||
	    ((date.getDay()   == 6) &&
	     (date.getHours()  < 9 || date.getHours() >= 16))) {
		alert("当コーナーのサービス時間は下記の通りと" +
		      "させていただいております。\n" +
		      "  ・平日：9：00〜20：00\n"+
		      "  ・土曜日：9：00〜16：00\n" + 
		      "  ・日曜・祝日：サービス停止\n" +
		      "予めご了承ください。");
		return false;
	}
	return true;
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
}

$(function(){
	calCalenderInit();
	
	$("#searchCalendarTab1 a").click(function(){
		calCalendar(1);
		$("#searchCalendarTab1 *,#searchCalendarTab2 *").toggle();
		return false;
	});
	$("#searchCalendarTab2 a").click(function(){
		calCalendar(2);
		$("#searchCalendarTab1 *,#searchCalendarTab2 *").toggle();
		return false;
	});

});

function noMemberButton(){
	var ret = calCheckInput(0, 2);
	if(ret==true){
		window.document.mainform2.submit();
	}
	return false;
	}