// 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 picOk;

$(document).ready(function(){
	window.name = "W130";
	
	ssid = $('#ssid').val();
	order_no  = $('#searchOrderNo').val();
	paraBar = $('#hdnBar').val();
	paraDocBar = $('#hdnDocBar').val();
	
	picOk = $('#hdnPicOk').val();

	$('#btnGotoShio').click(function(){
		gotoTyoRyu();
	});
	
	$('#btnPrintShien').click(function(){
		SienJohoPrt();
	});
	
	$('#btnPrintVouchar').click(function(){
		VoucharPrt();
	});
	
	$('#btnDocumentOpen').click(function(){
		documentOpen();
	});
	
	$('#btnPrintPIC').click(function(){
		PicPrt();
	});
	
	$('#btnPrintPICWord').click(function(){
		PicDload();
	});
	
	$('#btnCreatePIC').click(function(){
		if (picOk === "True" ){
			PicCreate();
		}else {
			var errerHTML = '<span>PICが登録されていません。</span>';
					$('#dialogErrorContents').empty();
					$('#dialogErrorContents').append(errerHTML);
					$('#dialogError').dialog('open');
		};
	});
	
	$('#btnPrev').click(function(){
		gotoIchiran();
	});
	
	$('#dialogDoc').dialog({
		autoOpen: false,
		title: "オーダー関連文書一覧",
		closeOnEscape: false,
		width: $('body').width() * 0.82,
		height: $('body').height() * 0.4,
		modal: true,
		open: function() {
			setTimeout(function(){
				$('.ui-dialog-buttonset button:eq(0)').focus();
			},0);
		},
		close: function() {
			$('body').removeClass('noscroll');
		},
		buttons: {
			'全て選択': function(){
				for( var i = 0; i < createData[0].line.length; i++ ){
					for( var j = 0; j < createData[0].line[i].detailLine.length; j++ ){
						if( $('#chkDoc' + String(i) + String(j) ).size() !== 0 && $('#divData'+ String(i) + String(j) ).css('display') !== 'none' ){
							$('#chkDoc' + String(i) + String(j) ).prop("checked",true);
						}
					}
				}
			},
			'選択を全て解除': function(){
				for( var i = 0; i < createData[0].line.length; i++ ){
					for( var j = 0; j < createData[0].line[i].detailLine.length; j++ ){
						if( $('#chkDoc' + String(i) + String(j) ).size() !== 0 && $('#divData'+ String(i) + String(j) ).css('display') !== 'none' ){
							$('#chkDoc' + String(i) + String(j) ).prop("checked",false);
						}
					}
				}
			},
			'選択を非表示とする': function(){
				$('#divAjax').show();
				setTimeout(function(){
					if( createRegistList('chkDoc') ){
						var retXml = updateDocAttr('7', registList);
						if( $(retXml).find('ret').text() === 'I001' ){
							var retXml2 = updateDocAttr('6', releaseList);
							if( $(retXml2).find('ret').text() === 'I001' ){
								createDocDialog(gOrderNo);
							}
						}
					}else{
						alert('更新対象の文書が存在しません。');
					}
					$('#divAjax').hide();
				},0);
			},
			'一時的に非表示の文書も表示': function(){
				for( var i = 0; i < createData[0].line.length; i++ ){
					$('#line' + String(i) ).show();

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

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

					if( $('#arrow' + String(i) ).size() !== 0 ){
						$('#arrow' + String(i) ).show();
					}
				}
			},
	        '閉じる': function(){
				$(this).dialog("close");
			}
    	}
	});

	if($('#returnPG').val() == 'W400'){
		$('#btnMenu0').hide();
	}
	
	getSearch();
});


function getSearch(){
	var setParam = {
		'appname':'NAIS3WEB',
		'prgname':'W132',
		'ssid': $('#ssid').val(),
		'orderNo' : $('#searchOrderNo').val(),
		'arguments':'ssid,orderNo'
	};
	getSearchCore(setParam);
}

function getSearchCore(searchParam){
	$.ajax({
		async : false,
		url: ajaxURL,
		data : searchParam,
		dataType:'html',
		scriptCharset: 'utf-8',
		success : function(html){			
			var getHtml = $($.parseHTML(html));
			var sessionError = getHtml.filter('#sessionError').val();
			if(sessionError === '0'){
				$('#divSearchContents').empty();
				$('#divSearchContents').append(getHtml.filter('#divSearchRes')[0].innerHTML);
			}else {
				cm_sessionError();
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown){
				cmAjaxError(XMLHttpRequest, textStatus, errorThrown);
		}
	});
}

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 html;
	var docXml = getOrderDoc(orderNo);
	switch( $(docXml).find('ret').text() ){
		case 'I001':
			var lineTag = [];
			lineTag[0] = 'line';
			
			var subLineTag = [];
			subLineTag[0] = 'detailLine';

			createData = xmlToMultiStageArray(docXml,'data',lineTag,subLineTag);
			break;
		case 'E001':
			break;
		default:
			alert($(docXml).find('message').text());
			break;
	}

	$('#divDoc').empty();
	html = '';

	for( var i = 0; i < createData[0].line.length; i++ ){
		var lineData = createData[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 === 2 ){
				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 + 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 + '</div>';
	}

	$('#divDoc').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');
		var documentUrl;
		if( docKind === '' || docNo === '' ){
			/*
			documentUrl = naisserver+'?APPNAME='+nasiappname+'&PRGNAME=W231&ARGUMENTS=-A'+nasiappname+',-A'+ssid+',-A'+order_no + ",-A" + lineNo + ",-A" + dataTitle;
	        window.open(documentUrl,"",'toolbar='+paraDocBar+',directories=0,status=1,resizable=1,scrollbars=1,menubar=0,left=100,top=60,width=800,height=500,');
			*/
			newWindowOpen("ファイル",'toolbar='+paraDocBar+',directories=0,status=1,resizable=1,scrollbars=1,menubar=0,left=100,top=60,width=800,height=500','W231',nasiappname,ssid,order_no,lineNo,dataTitle,'','','','','');
		}else{
			/*
			documentUrl = naisserver+'?APPNAME='+nasiappname+'&PRGNAME=D220&ARGUMENTS=-A'+nasiappname+',-A'+ssid + ",-A" + docKind + ",-A" + docNo;
	        window.open(documentUrl,"",'toolbar=0,directories=0,status=1,resizable=1,scrollbars=1,menubar=0,left=100,top=60,width=800,height=500,');
			*/
			newWindowOpen("ファイル",'toolbar=0,directories=0,status=1,resizable=1,scrollbars=1,menubar=0,left=100,top=60,width=800,height=500','D220',nasiappname,ssid,docKind,docNo,'','','','','','');
		}
		return false;
	});

	$('.chkDoc').unbind().click(function(){
		var docKind = $(this).attr('data-kind');
		var docNo = $(this).attr('data-no');
		for( var i = 0; i < createData[0].line.length; i++ ){
			for( var j = 0; j < createData[0].line[i].detailLine.length; j++ ){
				if( $('#chkDoc' + String(i) + String(j) ).size() !== 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;
	});

	$('#dialogDoc').dialog({
		position: {
			my: "top", 
			at: "top", 
			of: 'body',
			collision: "fit"
		}
	});
	$('#dialogDoc').dialog('open');
	$('#dialogDoc').height($('body').height() * 0.3);
}

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 = ajaxFunction(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 = ajaxFunction(param,'0');
	if( $(xmlData).find('message').text() !== '' ){
		alert($(xmlData).find('message').text());
	}
	return xmlData;
}
//支援情報印刷
function SienJohoPrt(){	
	/*
	var printUrl = naisserver+'?APPNAME='+nasiappname+'&PRGNAME=W150&ARGUMENTS=-A'+nasiappname+',-A'+ssid+',-A,-A,-A,-A,-A'+order_no;
	window.open(printUrl,"支援情報印刷",'toolbar=0,directories=0,status=1,resizable=1,scrollbars=0,menubar=0,left=100,top=60,width=800,height=550,');
	*/
	newWindowOpen("支援情報印刷",'toolbar=0,directories=0,status=1,resizable=1,scrollbars=0,menubar=0,left=100,top=60,width=800,height=550','W150',nasiappname,ssid,'','','','',order_no,'','','');
}
//Vouchar印刷
function VoucharPrt(){	
	/*
	var printUrl = naisserver+'?APPNAME='+nasiappname+'&PRGNAME=W260&ARGUMENTS=-A'+nasiappname+',-A'+ssid+',-A,-A,-A,-A,-A'+order_no;
	window.open(printUrl,"Vouchar印刷",'toolbar=0,directories=0,status=1,resizable=1,scrollbars=0,menubar=0,left=100,top=60,width=800,height=550,');
	*/
	newWindowOpen("Vouchar印刷",'toolbar=0,directories=0,status=1,resizable=1,scrollbars=0,menubar=0,left=100,top=60,width=800,height=550','W260',nasiappname,ssid,'','','','',order_no,'','','');
}
//PIC印刷
function PicPrt(){
	/*
	var printUrl = naisserver+'?APPNAME='+nasiappname+'&PRGNAME=W210&ARGUMENTS=-A'+nasiappname+',-A'+ssid+',-A'+order_no;
	window.open(printUrl,"PIC印刷選択",'toolbar=0,directories=0,status=1,resizable=1,scrollbars=0,menubar=0,left=100,top=60,width=800,height=550,');
	*/
	newWindowOpen("PIC印刷選択",'toolbar=0,directories=0,status=1,resizable=1,scrollbars=0,menubar=0,left=100,top=60,width=800,height=550','W210',nasiappname,ssid,order_no,'','','','','','','');
}
//PICダウンロード
function PicDload(){
	/*
	var printUrl = naisserver+'?APPNAME='+nasiappname+'&PRGNAME=W220&ARGUMENTS=-A'+nasiappname+',-A'+ssid+',-A'+order_no;
	window.open(printUrl,"PICダウンロード選択",'toolbar='+paraBar+',directories=0,status=1,resizable=1,scrollbars=0,menubar='+paraBar+',left=100,top=60,width=800,height=450,');
	*/
	newWindowOpen("PICダウンロード選択",'toolbar='+paraBar+',directories=0,status=1,resizable=1,scrollbars=0,menubar='+paraBar+',left=100,top=60,width=800,height=450','W220',nasiappname,ssid,order_no,'','','','','','','');
}

function PicCreate(){
	/*
	var printUrl = naisserver+'?APPNAME='+nasiappname+'&PRGNAME=W240&ARGUMENTS=-A'+ssid+',-A'+order_no;
	window.open(printUrl,"P.I.C.作成");
	*/
	newWindowOpen("P.I.C.作成",'','W240',ssid,order_no,'','','','','','','','');
}
//添付書類
function AttachedPapers(){
	/*
	var paperURL = naisserver+'?APPNAME='+nasiappname+'&PRGNAME=W230&ARGUMENTS=-A'+nasiappname+',-A'+ssid+',-A'+order_no;
	window.open(paperURL,"添付書類選択",'toolbar='+paraDocBar+',directories=0,status=1,resizable=1,scrollbars=1,menubar=0,left=100,top=60,width=800,height=500,');
	*/
	newWindowOpen("添付書類選択",'toolbar='+paraDocBar+',directories=0,status=1,resizable=1,scrollbars=1,menubar=0,left=100,top=60,width=800,height=500','W230',nasiappname,ssid,order_no,'','','','','','','');
}
//一覧に戻る
function gotoIchiran(){
	var maeFunk1     = $('#hdnMaeFunk').val();
	var maeKyoten    = $('#hdnMaeKyoten').val();	
	var maeDateym  = $('#hdnMaeDateYM').val();
	var maeDated   = $('#hdnMaeDateD').val();
	var kadoFunk = $('#hdnKadoFunk').val();
	var kadoHaijo = $('#hdnKadoHaijo').val();
	var kadoRigyo = $('#hdnKadoRigyo').val();
	var kadoShikaku = $('#hdnKadoShikaku').val();
	var maeMinato    = $('#hdnMaeMinatoCd').val();
	var returnPG = $('#returnPG').val();
	$('#prgname').val(returnPG);
	switch(returnPG){
		case 'W120':
			$('#param1').val(maeFunk1);
			$('#param2').val(maeKyoten);
			$('#param3').val(maeDateym);
			$('#param4').val(maeDated);
			$('#param5').val(order_no);
			$('#param6').val('W110');
			$('#param7').val(kadoFunk);
			$('#param8').val(kadoHaijo);
			$('#param9').val(kadoRigyo);
			$('#param10').val(kadoShikaku);
			$('#param11').val(maeMinato);
			//break;
			$('#menuForm').submit();
			
		case 'W160':
			$('#param1').val(maeFunk1);
			$('#param2').val(maeKyoten);
			$('#param3').val(maeDateym);
			$('#param4').val(maeDated);
			$('#param5').val(order_no);
			//break;
			$('#menuForm').submit();

		case 'W400':
			window.close();
		}
	//$('#menuForm').submit();
}
//潮流・潮汐計算を表示
function gotoTyoRyu(){
	var maeFunk1     = $('#hdnMaeFunk').val();
	var maeKyoten    = $('#hdnMaeKyoten').val();	
	var maeDateym  = $('#hdnMaeDateYM').val();
	var maeDated   = $('#hdnMaeDateD').val();
	var kadoFunk = $('#hdnKadoFunk').val();
	var kadoHaijo = $('#hdnKadoHaijo').val();
	var kadoRigyo = $('#hdnKadoRigyo').val();
	var kadoShikaku = $('#hdnKadoShikaku').val();
	var maeMinato    = $('#hdnMaeMinatoCd').val();
	var returnPG = $('#returnPG').val();
	$('#prgname').val('D300');
	$('#param1').val(order_no);
	$('#param2').val(returnPG);
	$('#param3').val('0');
	$('#param4').val(maeFunk1);
	$('#param5').val(maeKyoten);
	$('#param6').val(maeDateym);
	$('#param7').val(maeDated);
	$('#param8').val(kadoFunk);
	$('#param9').val(kadoHaijo);
	$('#param10').val(kadoRigyo);
	$('#param11').val(kadoShikaku);
	$('#param12').val(maeMinato);
	$('#menuForm').submit();
}
		
		
function documentOpen(){
	/*
	var documentUrl = naisserver+'?APPNAME='+nasiappname+'&PRGNAME=W133&ARGUMENTS=-A'+nasiappname+',-A'+ssid+',-A'+order_no;
    window.open(documentUrl,"関連文書表示",'toolbar=0,directories=0,status=1,resizable=1,scrollbars=1,menubar=0,left=100,top=60,width=900,height=500,');
	*/
	newWindowOpen("関連文書表示",'toolbar=0,directories=0,status=1,resizable=1,scrollbars=1,menubar=0,left=100,top=60,width=900,height=500','W133',nasiappname,ssid,order_no,'','','','','','','');
}

function showRelatedInfo(){
	var openWidth = $('body').width() * 0.78
	var openHeight = $('body').height() * 0.78
	newWindowOpen("関連情報",'toolbar=0,directories=0,status=1,resizable=1,scrollbars=1,menubar=0,left=100,top=60,width='+openWidth+',height='+openHeight+'','W134',ssid,order_no,$('#hdnJousenCdA').val(),$('#hdnJousenCdB').val(),$('#hdnGesenCdA').val(),$('#hdnGesenCdB').val(),'','','','');
}

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

function newWindowOpen(windowNM,windowOption,callPG,opt1,opt2,opt3,opt4,opt5,opt6,opt7,opt8,opt9,opt10){
	window.open("",windowNM,windowOption);
	$('#windowForm').attr('target',windowNM);
	$('#Wprgname').val(callPG);
	$('#w_param1').val(opt1);
	$('#w_param2').val(opt2);
	$('#w_param3').val(opt3);
	$('#w_param4').val(opt4);
	$('#w_param5').val(opt5);
	$('#w_param6').val(opt6);
	$('#w_param7').val(opt7);
	$('#w_param8').val(opt8);
	$('#w_param9').val(opt9);
	$('#w_param10').val(opt10);
	$('#windowForm').submit();
	$('#windowForm').attr('target','_self');
}
