// 子ウィンドウが処理中か否か
var isChildWindowProcessing = "FALSE";
function setChildWindowProcessing(setValue){
	isChildWindowProcessing = setValue;
}

var processingCheckInterval = 1000;//1sec
var processingChildWindow = null;
var openerSelfUnlockUrl = "";

// 子ウインドウが処理状態を監視する
// 処理中だが子ウィンドウが存在しない場合、自身のロックを解除する
function openerSelfUnlock(){
	
	try {

		// if not processing, return
		if ( isChildWindowProcessing == "FALSE" ) {
			activateOpenerButton();//call opener function
			processingChildWindow = null;
			return;
		}

		// if child window is active, set next timer
		if ( processingChildWindow && !processingChildWindow.closed ) {
			setTimeout("openerSelfUnlock()",processingCheckInterval);
		} else {
			isChildWindowProcessing = "FALSE";
			activateOpenerButton();//call opener function
			processingChildWindow = null;
			//if ( openerSelfUnlockUrl == "" ) {
			//	window.document.location.reload();
			//} else {
			//	window.document.location.replace(openerSelfUnlockUrl);
			//}
		}

		return;
	} catch(e) {
		return;
	}
}


/* do disable Button */
var disableButton;

/* PDFダウンロード処理 */
function pdfWindowOpen(form) {
	var date = new Date();
	var nowmills = date.getTime();
	var q = document.createElement('input');
	q.setAttribute("type","hidden");
	q.setAttribute("name","createFileName");
	q.setAttribute("value",nowmills);
	form.appendChild(q);
	win  = window.open("pdfFlushAction.do?search=" + nowmills + "&first=true","_blank","width=800,height=600,fullscreen=no,toolbar=no,menubar=no,directories=no,personalbar=no,scrollbars=no,resizable=yes,status=no");
	win.focus();
}

/* 非同期用ＰＤＦダウンロードポップアップ処理 */
function pdfAsyncWindowOpen(form,url) {
	// Start up child window
	processingChildWindow  = window.open(url,"_blank","width=800,height=600,fullscreen=no,toolbar=no,menubar=no,directories=no,personalbar=no,scrollbars=no,resizable=yes,status=yes");
	processingChildWindow.focus();

	// 親ウィンドウが閉じられた場合に子ウィンドウを閉じてもらうために子ウィンドウを登録
   	subWindow.push(processingChildWindow);

	// Disable caller window
	disableOpenerButton();//call opener function

	// set to the processing
	isChildWindowProcessing = "TRUE";
	setTimeout("openerSelfUnlock()",processingCheckInterval);
}

// 標準地域コード印刷画面のポップアップ画面を表示する
// エラーチェックはサーバー側で実施する
function openPrintStdAreaCode(form,documentType) {
	
	// 実行中の子ウィンドウがなければ、標準地域コード印刷画面のポップアップ画面を表示
	if (isChildWindowProcessing=="FALSE") {
		form.elements["documentType"].value = documentType;
		pdfAsyncWindowOpen(form,"printStdAreaCode.do?action=doShow");
	}
	
}

/* パラメータ設定処理 */
function setRequestParam(submit,form) {

	var q = document.createElement('input');
	q.setAttribute("type","hidden");
	q.setAttribute("name","action");
	q.setAttribute("value",submit.value);
	form.appendChild(q)
	disableButton = submit.value;

}

/* ボタンの非活性化処理（２度押し防止） */
function disableSubmit(form, fieldName ) {
	

	
	if( typeof( fieldName ) != "undefined" ) {
		form.elements[ fieldName ].value = false;
	}
	
	for(i = 0; i < form.elements.length; i++ ) {
    	if(form.elements[i].type == 'submit') {
	    	form.elements[i].disabled = true;

	    }
    }	
}

/* 都道府県一覧ボタンの入力チェックと実行  */
function checkDo1(form) {

	// 開始ページの入力チェック
	var result = validateInputValue(form.startPageNumber);

	// チェックＯＫであれば、PDFダウンロード処理を実行
    if( result ) {
		pdfWindowOpen(form);
    }

}


/* 市区町村一覧ボタン、支庁内郡表示ありボタンの入力チェックと実行  */
function checkDo2(form) {
	var year = form.year.value;
	var month = form.month.value;
	var day = form.day.value;
	
	var checkFlg;

	// 出力日付の妥当性チェック
	if(year != "" || month != "" || day!=""){
		// ２月の場合
		if( month == "2") {
			checkFlg = dayOfLeapYearCheck(year, day);
		}
		// ２月以外の場合
		else {
			checkFlg = dayOfMonthCheck(month, day);
		}
	}

	// 開始ページの入力チェック
  	var result = validateInputValue(form.startPageNumber);

	var findFlg = false;

	// 都道府県で全てが選択されている場合
	var isChecked = form.selectionMethod[0].checked;
	if( isChecked == true) {
		findFlg = true;

	// 都道府県で全てが選択されていない場合
	}else{
		// 都道府県グループの定義
		var prefGroupArray = new Array( "groupA", "groupB", "groupC", "groupD", "groupE", "groupF", "groupG", "groupH");

		// 都道府県グループでループ
		for( i = 0; i < prefGroupArray.length; i++) {

			var elements = form.elements[ prefGroupArray[i] ];
			// 都道府県がチェックされているか判定
			for( k = 0; k < elements.length; k++ ) {
				if( elements[k].checked ) {
					findFlg = true;
					break;
				}
			}
			if( findFlg ) {
				break;
			}
		}
	}
	
	
	if( year == 1970 && month < 4 ) {
		
		return;
	}
	

	// チェックＯＫであれば、PDFダウンロード処理を実行
    if( findFlg && result && checkFlg) {
		pdfWindowOpen(form);
    }
    
}

// ２月の日付の妥当性チェック
function dayOfLeapYearCheck(inYear, inDay) {
	var year = Number(inYear);
	if (((year%4)==0 && (year%100)!=0) || (year%400)==0) {
		if( (inDay == "30") || (inDay == "31")) {
			return false;
		}
	}
	else {
		if((inDay == "29") || (inDay == "30") || (inDay == "31")) {
			return false;
		}
	}
	return true;
}

// ２月以外の月の日付の妥当性チェック
function dayOfMonthCheck(month, day) {
	if((month == "4") || (month == "6") || (month == "9") || (month == "11")) {
		if(day == "31") {
			return false;
		}
	}
	return true;
}


/* 半角数値チェック */
function validateInputValue(textfield) {

	// 戻り値を初期化
    var allHalfSize = true;

	// 空文字の場合は、ＯＫ
	if(textfield.value == "" ) {
		return allHalfSize;
	}

	// 入力文字が半角数値であるかをチェック
    allHalfSize = textfield.value.match( /[0-9]+/ );
    
//	var han = "0123456789";
//	for (i = 0; i < inputVal.length; i++ ) {
//		var c = inputVal.charAt(i);
//		// Safari2.0では対応していないため、置き換え.
//		var n = -1;
//		for( k = 0; k < han.length; k++) {
//		
//			if( han[k] == c ) {
//				n = k;
//				break;
//			}
//		}
//		//var n = han.indexOf(c,0);
		
//		if( n == -1 ) {
//			allHalfSize = false;
//			break;
//		}
//	}

    return (allHalfSize == textfield.value);
}



function setHiddenParamter(form, button) {
	var q = document.createElement('input');
	q.type = 'hidden';
	q.name = 'action';
	q.value = button;
	form.appendChild(q);
}


