var createDocData = [];
var createBerthData = [];
var gOrderNo = '';
var btnControlFlg = false;

$(document).ready(function(){

	$('.btnControl').button();

	$('.btnControl').click(function(){
		if( !btnControlFlg ){
			btnControlFlg = true;
			switch($(this).attr('id')){
				case 'btnAllSelect':
					for( var i = 0; i < createDocData[0].line.length; i++ ){
						for( var j = 0; j < createDocData[0].line[i].detailLine.length; j++ ){
							if( $('#chkDoc' + String(i) + String(j) ).length != 0 && $('#divData'+ String(i) + String(j) ).css('display') != 'none' ){
								$('#chkDoc' + String(i) + String(j) ).prop("checked",true);
							}
						}
					}
					btnControlFlg = false;
					break;
				case 'btnAllCancel':
					for( var i = 0; i < createDocData[0].line.length; i++ ){
						for( var j = 0; j < createDocData[0].line[i].detailLine.length; j++ ){
							if( $('#chkDoc' + String(i) + String(j) ).length != 0 && $('#divData'+ String(i) + String(j) ).css('display') != 'none' ){
								$('#chkDoc' + String(i) + String(j) ).prop("checked",false);
							}
						}
					}
					btnControlFlg = false;
					break;
				case 'btnHideSelect':
					$('#divAjax').show();
					setTimeout(function(){
						if( createRegistList('chkDoc') ){
							var retXml = updateDocAttr('7', registList);
							if( $(retXml).find('ret').text() == 'I001' ){
								var retXml = updateDocAttr('6', releaseList);
								if( $(retXml).find('ret').text() == 'I001' ){
									createDocDialog(gOrderNo);
								}
							}
						}else{
							alert('更新対象の文書が存在しません。');
						}
						btnControlFlg = false;
						$('#divAjax').hide();
					},0);
					break;
				case 'btnShowAll':
					for( var i = 0; i < createDocData[0].line.length; i++ ){
						$('#line' + String(i) ).show();

						for( var j = 0; j < createDocData[0].line[i].detailLine.length; j++ ){
							$('#divData'+ String(i) + String(j) ).show();
						}

						if( $('#divHideData' + String(i) ).length != 0 ){
							$('#divHideData' + String(i) ).hide();
						}

						if( $('#arrow' + String(i) ).length != 0 ){
							$('#arrow' + String(i) ).show();
						}
					}
					btnControlFlg = false;
					break;
				case 'btnClose':
					window.close()
					break;
			}
		}
	});

	gOrderNo = $('#hidOrderNo').val();
	$('#divAjax').show();
	setTimeout(function(){
		createDocDialog(gOrderNo);
		createBerthLink('Jousen',$('#hidJousenCdA').val(),$('#hidJousenCdB').val());
		createBerthLink('Gesen',$('#hidGesenCdA').val(),$('#hidGesenCdB').val());

		$('#divAjax').hide();
	},0);

});

var registList = '';
var releaseList = '';
function createRegistList(registClass){
	var retCheck = false;
	registList = '';
	releaseList = '';
	$('.' + registClass).map(function(){
		if( $(this).prop("checked") ){
			if( registList.indexOf( $(this).attr('data-kind') + ':' + $(this).attr('data-no') ) == -1 ){
				retCheck = true;
				registList = registList + '@' + $(this).attr('data-kind') + ':' + $(this).attr('data-no');
			}
		}else{
			if( releaseList.indexOf( $(this).attr('data-kind') + ':' + $(this).attr('data-no') ) == -1 ){
				retCheck = true;
				releaseList = releaseList + '@' + $(this).attr('data-kind') + ':' + $(this).attr('data-no');
			}
		}
	});
	
	if( registList != '' ){
		registList = registList.substring( 1 );
	}

	if( releaseList != '' ){
		releaseList = releaseList.substring( 1 );
	}
	return retCheck;
};
/*
function displayDoc(orderNo){
	gOrderNo = orderNo;
	$('#divAjax').show();
		setTimeout(function(){
		createDocDialog(orderNo);
		$('#divAjax').hide();
	},0);
}
*/
function createDocDialog(orderNo){

	var docXml = getOrderDoc(orderNo);
	switch( $(docXml).find('ret').text() ){
		case 'I001':
			var lineTag = [];
			lineTag[0] = 'line';
			
			var subLineTag = [];
			subLineTag[0] = 'detailLine';

			createDocData = xmlToMultiStageArray(docXml,'data',lineTag,subLineTag)
			break;
		case 'E001':
			window.opener.$('#prgname').val('W040');
			window.opener.$('#menuForm').submit();
			window.close();
			break;
		default:
			//alert($(docXml).find('message').text());
			displayError($(docXml).find('message').text());
			return;
			break;
	}

	$('#divDocTemp').empty();
	$('#divDocJousen').empty();
	$('#divDocTyukei').empty();
	$('#divDocGesen').empty();
	html = '';

	for( var i = 0; i < createDocData[0].line.length; i++ ){
		html = '';
		var lineData = createDocData[0].line[i];
		var dataLineNum = 0;
		var dataLineNumWithOutHide = 0;
		var hideFlg = false;
		var dHtml = '';
		var dispData = 'display:none;';
		var displayNoneForHideData = '';
		var displayNoneForArrow = 'display:none;';
		for( var q = 0; q < lineData.detailLine.length; q++ ){
			var detailData = lineData.detailLine[q];
			if( detailData.hideFlg == '0' ){
				dataLineNumWithOutHide++;
			}
		}

		if( dataLineNumWithOutHide > 2 ){
			displayNoneForHideData = 'display:none;';
			displayNoneForArrow = '';
		}

		for( var j = 0; j < lineData.detailLine.length; j++ ){
			var detailData = lineData.detailLine[j];
			var displayNone = '';
			var dispChecked = '';

			if( detailData.hideFlg == '1' ){
				displayNone = 'display:none;';
				dispChecked = 'checked="checked"';
			}else{
				dispData = '';
				dataLineNumWithOutHide++;
			}

			if( dataLineNum == 10 ){
				dHtml = dHtml + '<div id="divHideData' + String(i) +'" style="' + displayNoneForHideData +'">';
				hideFlg = true;
			}

			dHtml = dHtml + '<div id="divData' + String(i) + String(j) + '" style="' + displayNone + '">';
			if( detailData.docKindCd != '' || detailData.docNo != '' ){
				dHtml = dHtml + '<input type="checkbox" class="chkDoc" id="chkDoc' + String(i) + String(j) + '" data-kind="' + detailData.docKindCd + '" data-no="' + detailData.docNo + '"' + dispChecked + '>';
			}
			if( detailData.tempFlg == '1' ){
				dHtml = dHtml + '<a href="#" class="hrefFile" data-title="' + detailData.title + '" data-line="' + detailData.lineNo + '" data-kind="' + detailData.docKindCd + '" data-no="' + detailData.docNo + '">' + detailData.title + '</a>'
			}else{
				dHtml = dHtml + detailData.title;
			}
			dHtml = dHtml + '</div>';
			dataLineNum++;
		}
		html = html + '<div id="line' + String(i) + '" style="' + dispData + '">';
		html = html + '<div class="titlebar">';
		html = html + lineData.kindName;
		html = html + '</div>';
		html = html + '<div style="">';
		html = html + dHtml;
		if( hideFlg ){
			html = html + '</div>';
			html = html + '<a href="#" class="arrowData" id="arrow' + String(i) +'" style="float:right;text-decoration: none;' + displayNoneForArrow + '" data-num="' + String(i) + '"><span id="spnArrow' + String(i) +'">続きを見る</span><img src="images/arrowBottom.gif" height="16" width="16" id="imgArrow' + String(i) +'" style="margin-top:2px;border:none;" ></a>';
			html = html + '<br>';
		}
		html = html + '<br>';
		html = html + '<br>';
		html = html + '<br>';
		html = html + '</div>';
		html = html + '</div>';

		if( lineData.kindName.indexOf('添付資料') !== -1){
			$('#divDocTemp').append(html);
		}else{
			if( lineData.kindName.indexOf('乗船') !== -1){
				$('#divDocJousen').append(html);
			}else{
				if( lineData.kindName.indexOf('中継') !== -1){
					$('#divDocTyukei').append(html);
				}else{
					$('#divDocGesen').append(html);
				}
			}
		}

	}

	$('.hrefFile').unbind().click(function(){
		var docKind = $(this).attr('data-kind');
		var docNo = $(this).attr('data-no');
		var lineNo = $(this).attr('data-line');
		var dataTitle = $(this).attr('data-title');
		window.open("","ファイルウィンドウ",'toolbar=0,directories=0,status=1,resizable=1,scrollbars=1,menubar=0,left=100,top=60,width=800,height=500,');
		$('#fmWindow').attr('target','ファイルウィンドウ');
		if( docKind == '' || docNo == '' ){
			$('#prgname').val('W231');
			$('#srchOption1').val(nasiappname);
			$('#srchOption2').val($('#ssid').val());
			$('#srchOption3').val(orderNo);
			$('#srchOption4').val(lineNo);
			$('#srchOption5').val(dataTitle);
			$('#srchOption6').val('');
			$('#srchOption7').val('');
			$('#srchOption8').val('');
			$('#srchOption9').val('');
			$('#srchOption10').val('');
			$('#fmWindow').submit();
		}else{
			$('#prgname').val('D220');
			$('#srchOption1').val(nasiappname);
			$('#srchOption2').val($('#ssid').val());
			$('#srchOption3').val(docKind);
			$('#srchOption4').val(docNo);
			$('#srchOption5').val('');
			$('#srchOption5').val('');
			$('#srchOption6').val('');
			$('#srchOption7').val('');
			$('#srchOption8').val('');
			$('#srchOption9').val('');
			$('#srchOption10').val('');
			$('#fmWindow').submit();
		}
		return false;
	});

	$('.chkDoc').unbind().click(function(){
		var docKind = $(this).attr('data-kind');
		var docNo = $(this).attr('data-no');
		for( var i = 0; i < createDocData[0].line.length; i++ ){
			for( var j = 0; j < createDocData[0].line[i].detailLine.length; j++ ){
				if( $('#chkDoc' + String(i) + String(j) ).length != 0 && 
					$('#chkDoc' + String(i) + String(j) ).attr('data-kind') == docKind &&
					$('#chkDoc' + String(i) + String(j) ).attr('data-no') == docNo &&
					$('#divData'+ String(i) + String(j) ).css('display') != 'none' 
				){
					$('#chkDoc' + String(i) + String(j) ).prop("checked",$(this).prop("checked"));
				}
			}
		}
	});

	$('.arrowData').unbind().click(function(){
		var dataNum = $(this).attr("data-num");
		slideData(dataNum);
		return false;
	});
}

function slideData(dataNum){
	if($('#divHideData'+ dataNum ).css("display") == "none"){
		$('#spnArrow' + dataNum).text('隠す');
		$('#imgArrow' + dataNum).attr('src','images/arrowRight.gif');
		$('#divHideData'+ dataNum ).slideDown();
	} else {
		$('#spnArrow' + dataNum).text('続きを見る');
		$('#imgArrow' + dataNum).attr('src','images/arrowBottom.gif');
		$('#divHideData'+ dataNum ).slideUp();
	}
}

function getOrderDoc(orderNo){
	var param = {
		'appname':'NAIS3WEB',
		'prgname':'D250',
		'ssid': $('#ssid').val(),
		'orderNo': orderNo,
		'arguments':'ssid,orderNo'
	};

	var xmlData = cmGetXmlData(param,'0');
	return xmlData;
}

function updateDocAttr(kbn,list){
	var param = {
		'appname':'NAIS3WEB',
		'prgname':'D210',
		'ssid': $('#ssid').val(),
		'updateKbn': kbn,
		'color': '',
		'list': list,
		'arguments':'ssid,updateKbn,color,list'
	};

	var xmlData = cmGetXmlData(param,'0');
	if( $(xmlData).find('message').text() != '' ){
		alert($(xmlData).find('message').text());
	}
	return xmlData;
}

function displayError(errorMessage){
	$('#divBtn').hide();
	$('#divDoc').empty();
	var html = '';
	html = html + '<br><br><br>';
	html = html + '<table align="center" border="0" cellspacing="0" cellpadding="2">';
	html = html + '<tr><td>&nbsp;</td></tr>';
	html = html + '<tr><td>&nbsp;</td></tr>';
	html = html + '<tr><td>&nbsp;</td></tr>';
	html = html + '<tr><td>&nbsp;</td></tr>';
	html = html + '<tr><td>&nbsp;</td></tr>';
	html = html + '<tr>';
	html = html + '<td align="center" nowrap>' + errorMessage + '</td>';
	html = html + '</tr>';
	html = html + '<tr><td>&nbsp;</td></tr>';
	html = html + '<tr><td>&nbsp;</td></tr>';
	html = html + '<tr>';
	html = html + '<td align="center"nowrap><input TYPE="button" VALUE="&nbsp;閉じる&nbsp;" onClick="window.close()"></A></td>';
	html = html + '</tr>';
	html = html + '</table>';
	$('#divDoc').append(html);
}

function createBerthLink(kbn,cdA, cdB){
	var docXml = getBerthDoc(cdA,cdB);
	switch( $(docXml).find('ret').text() ){
		case 'I001':
			var lineTag = [];
			lineTag[0] = 'line';
			
			var subLineTag = [];
			subLineTag[0] = 'detailLine';

			createBerthData = xmlToMultiStageArray(docXml,'data',lineTag,subLineTag)
			break;
		case 'S001':
			createBerthData = [];
			break;
		case 'E001':
			$('#prgname').val('W040');
			$('#menuForm').submit();
			break;
	}

	cmSetXML(docXml,'dialogBerthRes' + kbn);

	switch( $('#suiroKbn' + kbn).val() ){
		case '1':
		case '2':
			$('#tdSuiroData0' + kbn).hide();
			$('#tdSuiroData1' + kbn).show();
			$('#tdSuiroData2' + kbn).show();
			$('#tdSuiroTitle0' + kbn).hide();
			$('#tdSuiroTitle1' + kbn).show();
			$('#tdSuiroTitle2' + kbn).show();
			break;
		default:
			$('#tdSuiroData0' + kbn).show();
			$('#tdSuiroData1' + kbn).hide();
			$('#tdSuiroData2' + kbn).hide();
			$('#tdSuiroTitle0' + kbn).show();
			$('#tdSuiroTitle1' + kbn).hide();
			$('#tdSuiroTitle2' + kbn).hide();
			break;
	}

	var titleHosoku = ''
	if(kbn == 'Jousen'){
		titleHosoku = ' [乗船地]'
	}else{
		titleHosoku = ' [下船地]'
	}

	$('#divBerthDetail').empty();
	var html = '';
	for( var i = 0; i < createBerthData[0].line.length; i++ ){
		var lineData = createBerthData[0].line[i];
		html = html + '<div class="titlebar">';
		html = html + lineData.kindName + titleHosoku;
		html = html + '</div>';
		var dataLineNum = 0;
		var hideFlg = false;
		for( var j = 0; j < lineData.detailLine.length; j++ ){
			var detailData = lineData.detailLine[j];

			if( dataLineNum === 10 ){
				html = html + '<div id="divHideData' + kbn + String(i) +'" style="display:none">';
				hideFlg = true;
			}

			if( detailData.title !== '' ){
				html = html + '<div>';
				dataLineNum++;
			}
			html = html + detailData.title + '&nbsp;<a href="#" class="hrefFile' + kbn + '" data-kind="' + detailData.docKindCd + '" data-no="' + detailData.docNo + '">' + detailData.link + '</a>';
			if( j < lineData.detailLine.length - 1 ){
				if( lineData.detailLine[j+1].title !== '' ){
					html = html + '</div>';
				}
			}else{
				html = html + '</div>';
			}
		}
		if( hideFlg ){
			html = html + '</div>';
			html = html + '<a href="#" class="arrowData' + kbn + '" style="float:right;text-decoration: none;" data-num="' + String(i) + '"><span id="spnArrow' + kbn + String(i) +'">続きを見る</span><img src="images/arrowBottom.gif" height="16" width="16" id="imgArrow' + kbn + String(i) +'" style="margin-top:2px;border:none;" ></a>';
		}
		html = html + '<br>';
		html = html + '<br>';
		html = html + '<br>';
	}

	$('#divBerthDetail' + kbn).append(html);

	$('.hrefFile' + kbn).unbind().click(function(){
		var docKind = $(this).attr('data-kind');
		var docNo = $(this).attr('data-no');
		window.open("","ファイル",'toolbar=0,directories=0,status=1,resizable=1,scrollbars=1,menubar=0,left=100,top=60,width=800,height=500');
		$('#windowForm').attr('target','ファイル');
		$('#Wprgname').val('D220');
		$('#w_param1').val('NAIS3WEB');
		$('#w_param2').val($('#ssid').val());
		$('#w_param3').val(docKind);
		$('#w_param4').val(docNo);
		$('#w_param5').val('');
		$('#w_param6').val('');
		$('#w_param7').val('');
		$('#w_param8').val('');
		$('#w_param9').val('');
		$('#w_param10').val('');
		$('#windowForm').submit();
		$('#windowForm').attr('target','_self');
		return false;
	});

	$('.arrowData' + kbn).unbind().click(function(){
		var dataNum = $(this).attr("data-num");
		slideDataForBerth(kbn,dataNum);
		return false;
	});
}

function slideDataForBerth(kbn,dataNum){
	if($('#divHideData'+ kbn + dataNum ).css("display") === "none"){
		$('#spnArrow' + kbn + dataNum).text('隠す');
		$('#imgArrow' + kbn + dataNum).attr('src','images/arrowRight.gif');
		$('#divHideData'+ kbn + dataNum ).slideDown();
	} else {
		$('#spnArrow' + kbn + dataNum).text('続きを見る');
		$('#imgArrow' + kbn + dataNum).attr('src','images/arrowBottom.gif');
		$('#divHideData' + kbn + dataNum ).slideUp();
	}
}

function getBerthDoc(cdA,cdB){
	var param = {
		'appname':'NAIS3WEB',
		'prgname':'D230',
		'ssid': $('#ssid').val(),
		'cdA': cdA,
		'cdB': cdB,
		'arguments':'ssid,cdA,cdB'
	};

	var xmlData = cmGetXmlData(param,'0');
	return xmlData;
}
