// JavaScript Document
var docData = [];
var docPastData = [];
var docFavoriteData = [];
var largeSummary = false;
var largeFavorite = false;
var largeContact = false;

var paraBar;
var paraDocBar;

var createData = [];
var gOrderNo = '';

var ssid;
var order_no;
var mizusakiCd;
var harborInExist;
var harborInTug;
var harborInTugNoList;
var harborInBarse;
var harborOutExist;
var harborOutTug;
var harborOutTugNoList;
var harborOutBarse;

var harborInTugSelect
var harborInBarseSelect
var harborOutTugSelect
var harborOutBarseSelect

var signalFlag = [];

$(document).ready(function(){
	window.name = "W240";
	
	ssid = $('#ssid').val();
	order_no  = $('#searchOrderNo').val();
	mizusakiCd = $('#searchMizusakiCd').val();
	paraBar = $('#hdnBar').val();
	paraDocBar = $('#hdnDocBar').val();
	$('.chbSignalFlag_koro[value="'+$('#hdnDefaultFlag1').val()+'"]').prop('checked',true);
	$('.chbSignalFlag_timei[value="'+$('#hdnDefaultFlag2').val()+'"]').prop('checked',true);
	
	$('.chbSignalFlag_koro').on('click',function(){
		var checkCnt = 0;
		$('.chbSignalFlag_koro').each(function(){
			if($(this).prop('checked')){
				checkCnt = checkCnt + 1;
			}
		});
		if(checkCnt > 1){
			alert('航路信号旗の選択は1種類までです。');
			$(this).prop('checked',false);
		}
	});
	$('.chbSignalFlag_timei').on('click',function(){
		var checkCnt = 0;
		$('.chbSignalFlag_timei').each(function(){
			if($(this).prop('checked')){
				checkCnt = checkCnt + 1;
			}
		});
		if(checkCnt > 1){
			alert('地名信号旗の選択は1種類までです。');
			$(this).prop('checked',false);
		}
	});
	
	$('.BarseCreate').on('click',function(){
		checkInput();
		var createType = $(this).attr('data-createType');
		var InOutKbn = $(this).attr('data-InOutKbn');
		var chimeiA = $(this).attr('data-chimeiA');
		var chimeiB = $(this).attr('data-chimeiB');
		var mapNumber = $(this).attr('data-Number');
		$('#prgname').val('W241');
		$('#param1').val(order_no);
		$('#param2').val(InOutKbn);
		$('#param3').val(chimeiA);
		$('#param4').val(chimeiB);
		$('#param5').val(mizusakiCd);
		$('#param6').val(harborInExist);
		$('#param7').val(harborOutExist);
		$('#param8').val(harborInTug);
		$('#param9').val(harborInBarse);
		$('#param10').val(harborOutTug);
		$('#param11').val(harborOutBarse);
		$('#param12').val(signalFlag[0]);
		$('#param13').val(signalFlag[1]);
		$('#param14').val(harborInTugNoList);
		$('#param15').val(harborOutTugNoList);
		$('#param16').val(createType);
		$('#param17').val(mapNumber);
		$('#menuForm').submit();
	});
	
	$('#btnEXCEL').on('click',function(){
		callOutputPIC('E');
	});
	
	$('#btnPDF').on('click',function(){
		callOutputPIC('P');
	});
	
	$('#btnClose').on('click',function(){
		var setParam = {
			'appname':'NAIS3WEB',
			'prgname':'W243',
			'ssid': $('#ssid').val(),
			'orderNo' : order_no,
			'arguments':'ssid,orderNo'
		};
		$.ajax({
			async : false,
			url: ajaxURL,
			type:'POST',
			data : setParam,
			dataType:'xml',
			scriptCharset: 'utf-8',
			success : function(xml){
				var retKbn = $(xml).find('ret').text();
				var retMsg = $(xml).find('message').text(); 
				alert(retMsg);
				if(retKbn === 'I001'){
					window.close();
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown){
					cmAjaxError(XMLHttpRequest, textStatus, errorThrown);
			}
		});
	});
	
	harborInTug = $('#hdnHarborInTug').val();
	harborOutTug = $('#hdnHarborOutTug').val();
	harborInBarse = $('#hdnHarborInBarse').val();
	harborOutBarse = $('#hdnHarborOutBarse').val();
	if(harborInTug !== ''){$('*[name="rdoHarvorInTug"][value="'+harborInTug+'"]').prop('checked',true);}
	if(harborOutTug !== ''){$('*[name="rdoHarvorOutTug"][value="'+harborOutTug+'"]').prop('checked',true);}
	if(harborInBarse !== ''){$('*[name="rdoHarvorInBarse"][value="'+harborInBarse+'"]').prop('checked',true);}
	if(harborOutBarse !== ''){$('*[name="rdoHarvorOutBarse"][value="'+harborOutBarse+'"]').prop('checked',true);}

	harborInTugSelect = $('#hdnHarborInTugSelect').val();
	harborInBarseSelect = $('#hdnHarborInBarseSelect').val();
	harborOutTugSelect = $('#hdnHarborOutTugSelect').val();
	harborOutBarseSelect = $('#hdnHarborOutBarseSelect').val();

});

function callOutputPIC(syoriKbn){
	var checkInp = checkInput();
	var checkMsg = '';
	
	if(checkInp[2] === 1 && harborOutTugSelect==="True"){
		checkMsg = checkMsg + '出港タグ配置図を選択してください。<br>';
	}
	if(checkInp[7].length !== 0){
		if(checkInp[7][0]){
			checkMsg = checkMsg + '出港タグリストの最小値は1です。<br>';
		}
		if(checkInp[7][1]){
			checkMsg = checkMsg + '出港タグリストの最大値は'+$('#hdnHarborOutTugListCnt').val()+'です。<br>';
		}
		if(checkInp[7][2]){
			checkMsg = checkMsg + '出港タグリストは重複がないように入力してください。<br>';
		}
	}
	if(checkInp[3] === 1 && harborOutBarseSelect==="True"){
		checkMsg = checkMsg + '出港バース海図を選択してください。<br>';
	}
	if(checkInp[0] === 1 && harborInTugSelect==="True"){
		checkMsg = checkMsg + '入港タグ配置図を選択してください。<br>';
	}
	if(checkInp[6].length !== 0){
		if(checkInp[6][0]){
			checkMsg = checkMsg + '入港タグリストの最小値は1です。<br>';
		}
		if(checkInp[6][1]){
			checkMsg = checkMsg + '入港タグリストの最大値は'+$('#hdnHarborInTugListCnt').val()+'です。<br>';
		}
		if(checkInp[6][2]){
			checkMsg = checkMsg + '入港タグリストは重複がないように入力してください。<br>';
		}
	}
	if(checkInp[1] === 1 && harborInBarseSelect==="True"){
		checkMsg = checkMsg + '入港バース海図を選択してください。<br>';
	}
	if(checkInp[4] === 1){
		checkMsg = checkMsg + '航路信号旗の選択は1種類までです。<br>';
	}
	if(checkInp[5] === 1){
		checkMsg = checkMsg + '地名信号旗の選択は1種類までです。<br>';
	}	
	
	if(checkMsg !== ''){
		cmAlert("エラー",checkMsg);
	}else {
		var ssid = $('#ssid').val();
		/*
		var printUrl = naisserver+'?APPNAME='+nasiappname+'&PRGNAME=W250&ARGUMENTS=-A'+ssid+',-A'+syoriKbn+',-A,-A0'+',-A'+order_no+',-A'+harborInExist+',-A'+harborOutExist+',-A'+harborInTug+',-A'+harborInBarse+',-A'+harborOutTug+',-A'+harborOutBarse+',-A'+signalFlag[0]+',-A'+signalFlag[1]+',-A'+harborInTugNoList+',-A'+harborOutTugNoList;
		window.open(printUrl,"稼動記録印刷",'toolbar=0,directories=0,status=1,resizable=1,scrollbars=0,menubar=0,left=100,top=60,width=800,height=550,');
		*/
		window.open("",'稼動記録印刷','toolbar=0,directories=0,status=1,resizable=1,scrollbars=0,menubar=0,left=100,top=60,width=800,height=550');
		$('#menuForm').attr('target','稼動記録印刷');
		$('#prgname').val('W250');
		$('#param1').val(syoriKbn);
		$('#param2').val('');
		$('#param3').val('0');
		$('#param4').val(order_no);
		$('#param5').val(harborInExist);
		$('#param6').val(harborOutExist);
		$('#param7').val(harborInTug);
		$('#param8').val(harborInBarse);
		$('#param9').val(harborOutTug);
		$('#param10').val(harborOutBarse);
		$('#param11').val(signalFlag[0]);
		$('#param12').val(signalFlag[1]);
		$('#param13').val(harborInTugNoList);
		$('#param14').val(harborOutTugNoList);
		$('#param15').val('');
		$('#param16').val('');
		$('#param17').val('');
		$('#param18').val('');
		$('#param19').val('');
		$('#param20').val('');
		$('#menuForm').submit();
		$('#menuForm').attr('target','_self');
	}
}

function checkInput(){
	var retCheck = [];
	
	if($('#divharborIn').get(0)){
		harborInExist = 1;		
		harborInTug = $('input[name="rdoHarvorInTug"]:checked').val();
		if(!harborInTug){
			retCheck[0] = 1;
		}else {
			retCheck[0] = 0;
		}
		
		if($('#tblHarborInTugList').get(0)){
			retCheck[6] = checkTugNoList('In');
		}else {
			harborInTugNoList = '';
			retCheck[6] = [];
		}
		
		harborInBarse = $('input[name="rdoHarvorInBarse"]:checked').val();
		if(!harborInBarse){
			retCheck[1] = 1;
		}else {
			retCheck[1] = 0;
		}
	}else {
		harborInExist = 0;
		harborInTug = '';
		harborInTugNoList = '';
		retCheck[0] = 0;
		harborInBarse = '';
		retCheck[1] = 0;
		retCheck[6] = [];
	}

	if($('#divharborOut').get(0)){
		harborOutExist = 1;
		harborOutTug = $('input[name="rdoHarvorOutTug"]:checked').val();
		if(!harborOutTug){
			retCheck[2] = 1;
		}else {
			retCheck[2] = 0;
		}
		
		if($('#tblHarborOutTugList').get(0)){
			retCheck[7] = checkTugNoList('Out');
		}else {
			harborOutTugNoList = '';
			retCheck[7] = [];
		}
		
		harborOutBarse = $('input[name="rdoHarvorOutBarse"]:checked').val();
		if(!harborOutBarse){
			retCheck[3] = 1;
		}else {
			retCheck[3] = 0;
		}
	}else {
		harborOutExist = 0;
		harborOutTug = '';
		harborOutTugNoList = '';
		retCheck[2] = 0;
		harborOutBarse = '';
		retCheck[3] = 0;
		retCheck[7] = [];
	}
	
	signalFlag[0] = '';
	signalFlag[1] = '';
	var signalKoroCnt = 0;
	$('.chbSignalFlag_koro').each(function(){
		if($(this).prop('checked')){
			signalFlag[0] = $(this).val();
			signalKoroCnt = signalKoroCnt + 1;
		}
	});
	if(signalKoroCnt > 1){
		retCheck[4] = 1;
	}else {
		retCheck[4] = 0;
	}
	
	var signalTimeiCnt = 0;
	$('.chbSignalFlag_timei').each(function(){
		if($(this).prop('checked')){
			signalFlag[1] = $(this).val();
			signalTimeiCnt = signalTimeiCnt + 1;
		}
	});
	if(signalTimeiCnt > 1){
		retCheck[5] = 1;
	}else {
		retCheck[5] = 0;
	}
	return retCheck;
}

function checkTugNoList(InOutKbn){
	var checkErr = [];
	var checkInputClass = '';
	var checkMaxVal = 0;
	var checkHarborTagList = '';
	if(InOutKbn === 'In'){
		checkInputClass = '.inpHarborInTugNo';
		checkMaxVal = parseInt($('#hdnHarborInTugListCnt').val());
	}else if(InOutKbn === 'Out'){
		checkInputClass = '.inpHarborOutTugNo';
		checkMaxVal = parseInt($('#hdnHarborOutTugListCnt').val());
	}
	var checkInputValArr = [];
	var checkInputCnt = 0;
	$(checkInputClass).each(function(){
		checkInputValArr[checkInputCnt] = parseInt($(this).val());
		checkInputCnt = checkInputCnt + 1;
		checkHarborTagList = checkHarborTagList + $(this).val();
	});
	var minVal = Math.min.apply(null,checkInputValArr);
	var maxVal = Math.max.apply(null,checkInputValArr);	
	var chohukuArr = checkInputValArr.filter(function(x,i,self){
		return self.indexOf(x) === i && i !== self.lastIndexOf(x);
	});
	
	if(minVal < 1){
		checkErr[0] = 1;
	}
	if(maxVal !== checkMaxVal){
		checkErr[1] = 1;
	}
	if(chohukuArr.length !== 0){
		checkErr[2] = 1;
	}
	
	if(InOutKbn === 'In'){
		harborInTugNoList = checkHarborTagList;
	}else if(InOutKbn === 'Out'){
		harborOutTugNoList = checkHarborTagList;
	}
	
	return checkErr;
}

function NPHelp(){
	var hdnWebDocAlias = $('#hdnWebDocAlias').val();
	window.open(hdnWebDocAlias+"help/nphelp_search001.htm");
}