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

var palettePatern = ['black','red','blue','green','purple'];

$(document).ready(function(){
	ajaxLoadingCreate();
	
	window.name = "W020";
	$('#dialogVoteReminder').hide();

/*
	if($('#passKbn').val() === 'T'){
		$('#divReport').show();
		$('#divDocFavorite').show();
		createDocTable('DocSummary','発刊文書のお知らせ(未確認のお知らせは赤字)');
		createDocTable('DocFavorite','よく見る発刊文書');
	}else {
		$('#divReport').hide();
		$('#divDocFavorite').hide();
		$('#btnViewChangeContact').hide();
		$('#divRenraku').css('height','calc(75% - 10px)');
	}
*/

	$('#divReport').hide();
	$('#divDocFavorite').hide();
	$('#btnViewChangeContact').hide();
	$('#divRenraku').css('height','calc(75% - 10px)');
	
	$('#dialogContact').dialog({
		autoOpen: false,
		title: "過去の連絡事項",
		closeOnEscape: false,
		width: $('body').width() * 0.78,
		modal: true,
		open: function() {
			setTimeout(function(){
				$('.ui-dialog-buttonset button:eq(0)').focus();
			},0);
		},
		buttons: {
	        '閉じる': function(){
				$(this).dialog("close");
			}
    	}
	});

	$('#btnPastContact').click(function(){
		$('#dialogContact').dialog('open');
	});
	$('#btnViewChangeContact').click(function(){
		if( !largeContact ){
			$('#divRenraku').css('height','75%');
/*
			if($('#passKbn').val() === 'T'){
				$('#divReport').hide();
				$('#divDocFavorite').hide();
			}
*/
			$('#btnViewChangeContact').text('表示領域を元に戻す');
			largeContact = true;
		}else{
			$('#divRenraku').css('height','25%');
/*
			if($('#passKbn').val() === 'T'){
				$('#divReport').show();
				$('#divDocFavorite').show();
			}
*/
			$('#btnViewChangeContact').text('表示領域を拡大する');
			largeContact = false;
		}
	});

	$('#dialogPastDoc').dialog({
		autoOpen: false,
		title: "過去の発刊文書",
		closeOnEscape: false,
		width: $('body').width() * 0.78,
		modal: true,
		open: function() {
			setTimeout(function(){
				$('.ui-dialog-buttonset button:eq(1)').focus();
			},0);
		},
		buttons: {
	        '閉じる': function(){
				$(this).dialog("close");
			}
    	}
	});
	
	$('#dialogDocDetail').dialog({
		autoOpen: false,
		title: "発刊文書の詳細",
		closeOnEscape: false,
		width: $('body').width() * 0.78,
		height: $('body').height() * 0.5,
		modal: true,
		open: function() {
			setTimeout(function(){
				$('.ui-dialog-buttonset button:eq(2)').focus();
			},0);
		},
		buttons: {
	        '閉じる': function(){
				$(this).dialog("close");
			}
    	}
	});

	$('#dialogVoteReminder').dialog({
		autoOpen: false,
		title: "電子投票",
		closeOnEscape: false,
		width: 400,
		modal: true,
		open: function() {
		},
		buttons: {
	        '閉じる': function(){
				$(this).dialog("close");
			}
    	}
	});

	$('.summaryBtn').click(function(){
		var btnId = $(this).attr('id');
		$('#divAjax').show();
		setTimeout(function(){
			switch( btnId ){
				case 'btnAllSelectSummary' :
					for( var i = 0; i < docData[0].line.length; i++ ){
						$('#chkDocSummary' + String(i) ).prop("checked",true);
					}
					break;
				case 'btnAllCancelSummary' :
					for( var j = 0; j < docData[0].line.length; j++ ){
						$('#chkDocSummary' + String(j) ).prop("checked",false);
					}
					break;
				case 'btnMoveFavorite' :
					if( createRegistList('chkDocSummary') ){
						var retXml = updateDocAttr('1','',registList);
						if( $(retXml).find('ret').text() === 'I001' ){
							createDocTable('DocSummary','発刊文書のお知らせ(未確認のお知らせは赤字)');
							createDocTable('DocFavorite','よく見る発刊文書');
						}
					}else{
						alert('1つ以上の文書を選択して下さい。');
					}
					break;
				case 'btnDelSummary' :
					if( createRegistList('chkDocSummary') ){
						var retXml = updateDocAttr('4','',registList);
						if( $(retXml).find('ret').text() === 'I001' ){
							createDocTable('DocSummary','発刊文書のお知らせ(未確認のお知らせは赤字)');
							createDocTable('DocFavorite','よく見る発刊文書');
						}
					}else{
						alert('1つ以上の文書を選択して下さい。');
					}
					break;
				case 'btnPastSummary' :
					createDocTable('DocPast','過去の発刊文書一覧');
					$('#dialogPastDoc').dialog('open');
					break;
				case 'btnViewChangeSummary' :
					if( !largeSummary ){
						$('#divRenraku').hide();
						$('#divReport').css('height','75%');
						$('#divDocFavorite').hide();
						$('#btnViewChangeSummary').text('表示領域を元に戻す');
						largeSummary = true;
					}else{
						$('#divRenraku').show();
						$('#divReport').css('height','25%');
						$('#divDocFavorite').show();
						$('#btnViewChangeSummary').text('表示領域を拡大する');
						largeSummary = false;
					}
					break;
			}
			$('#divAjax').hide();
		},0);
	});

	$('.pastBtn').click(function(){
		var btnId = $(this).attr('id');
		$('#divAjax').show();
		setTimeout(function(){
			switch( btnId ){
				case 'btnAllSelectPast' :
					for( var i = 0; i < docPastData[0].line.length; i++ ){
						$('#chkDocPast' + String(i) ).prop("checked",true);
					}
					break;
				case 'btnAllCancelPast' :
					for( var j = 0; j < docPastData[0].line.length; j++ ){
						$('#chkDocPast' + String(j) ).prop("checked",false);
					}
					break;
				case 'btnMoveFavoritePast' :
					if( createRegistList('chkDocPast') ){
						var retXml = updateDocAttr('1','',registList);
						if( $(retXml).find('ret').text() === 'I001' ){
							createDocTable('DocSummary','発刊文書のお知らせ(未確認のお知らせは赤字)');
							createDocTable('DocFavorite','よく見る発刊文書');
						}
					}else{
						alert('1つ以上の文書を選択して下さい。');
					}
					break;
				case 'btnMoveSummaryPast' :
					if( createRegistList('chkDocPast') ){
						var retXml = updateDocAttr('3','',registList);
						if( $(retXml).find('ret').text() === 'I001' ){
							createDocTable('DocSummary','発刊文書のお知らせ(未確認のお知らせは赤字)');
							createDocTable('DocFavorite','よく見る発刊文書');
						}
					}else{
						alert('1つ以上の文書を選択して下さい。');
					}
					break;
			}
			$('#divAjax').hide();
		},0);
	});

	$('.favoriteBtn').click(function(){
		var btnId = $(this).attr('id');
		$('#divAjax').show();
		setTimeout(function(){
			switch( btnId ){
				case 'btnAllSelectFavorite' :
					for( var i = 0; i < docFavoriteData[0].line.length; i++ ){
						$('#chkDocFavorite' + String(i) ).prop("checked",true);
					}
					break;
				case 'btnAllCancelFavorite' :
					for( var j = 0; j < docFavoriteData[0].line.length; j++ ){
						$('#chkDocFavorite' + String(j) ).prop("checked",false);
					}
					break;
				case 'btnDelFavorite' :
					if( createRegistList('chkDocFavorite') ){
						var retXml = updateDocAttr('2','',registList);
						if( $(retXml).find('ret').text() === 'I001' ){
							createDocTable('DocSummary','発刊文書のお知らせ(未確認のお知らせは赤字)');
							createDocTable('DocFavorite','よく見る発刊文書');
						}
					}else{
						alert('1つ以上の文書を選択して下さい。');
					}
					break;
				case 'btnPalletFavorite':
					if( createRegistList('chkDocFavorite') ){
						var retXml = updateDocAttr('5',registColor,registList);
						if( $(retXml).find('ret').text() === 'I001' ){
							createDocTable('DocFavorite','よく見る発刊文書');
						}
					}else{
						alert('1つ以上の文書を選択して下さい。');
					}
					break;
				case 'btnViewChangeFavorite' :
					if( !largeFavorite ){
						$('#divRenraku').hide();
						$('#divReport').hide();
						$('#divDocFavorite').css('height','75%');
						$('#btnViewChangeFavorite').text('表示領域を元に戻す');
						largeFavorite = true;
					}else{
						$('#divRenraku').show();
						$('#divReport').show();
						$('#divDocFavorite').css('height','25%');
						$('#btnViewChangeFavorite').text('表示領域を拡大する');
						largeFavorite = false;
					}
					break;
			}
			$('#divAjax').hide();
		},0);
	});
	
	$('#lnkTaxi').on('click',function(){
		var setURL = topURL + '/webnais/pilot/taxi_tensou.html' + '?ssid=' + $('#ssid').val();
		window.open(setURL,'_blank');
	});
	
	$('#lnkMyPage').on('click',function(){
		$('#prgname').val('W940');
		$('#menuForm').submit();
	});
	
	$('#lnkGetPAss').on('click',function(){
		var param = {
			'appname':'NAIS3WEB',
			'prgname':'W930',
			'ssid': $('#ssid').val(),
			'arguments':'ssid'
		};
		var xmlData = cmGetXmlData(param);
		if( $(xmlData).find('ret').text() === 'I001' ){
//			alert('受け付けました。');
			alert($(xmlData).find('message').text());
		}
	});
	
	ajaxLoadingDelete();

	if($('#msgKbn').val() === '1'){
		$('#dialogVoteReminder').dialog('open');
	}

});

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

	if( registColor !== ''){
		registColor = registColor.substring( 1 );
	}
	
	return retCheck;
}

function createAllRegistList(registClass){
	var retCheck = false;
	registList = '';
	registColor = '';
	$('.' + registClass).map(function(){
		retCheck = true;
		registList = registList + '@' + $(this).attr('data-kind') + ':' + $(this).attr('data-no');
		registColor = registColor + '@' + $(this).attr('data-color');
	});
	
	if( registList !== '' ){
		registList = registList.substring( 1 );
	}

	if( registColor !== ''){
		registColor = registColor.substring( 1 );
	}
	
	return retCheck;
}

function createDetailTable(dataNo,createData){
	var lineData = createData[0].line[dataNo];

	$('#tbHeadDocDetail').empty();
	$('#tbBodyDocDetail').empty();
	var html = '';
	html = html + '<tr bgcolor="<!$MG_doc_bgcolor_title>">';
	html = html + '<td nowrap><font color="<!$MG_doc_font_color_title>"><b>[詳細]' + lineData.title  + '</b></font></td>';
    html = html + '</tr>';
	$('#tbHeadDocDetail').append(html);

	html = '';
	for( var i = 0; i < lineData.detailLine.length; i++ ){
		var detailData = lineData.detailLine[i];
		html = html + '<tr >';
		html = html + '<td nowrap>';
		html = html + '<font color="' + lineData.color + '">' + detailData.detailTitle + '</font>';
		html = html + '</td>';
		html = html + '</tr>';
	}

	$('#tbBodyDocDetail').append(html);
}

function createDocTable(kind){
	var searchKbn = '';
	switch( kind ){
		case 'DocSummary':
			searchKbn = '1';
			break;
		case 'DocFavorite':
			searchKbn = '2';
			break;
		case 'DocPast':
			searchKbn = '3';
			break;
	}

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

			var createData = xmlToMultiStageArray(docXml,'data',lineTag,subLineTag);

			switch( kind ){
				case 'DocSummary':
					docData = createData;
					tableID = 'tblReportList';
					break;
				case 'DocFavorite':
					docFavoriteData = createData;
					tableID = 'tblDocFavoriteList';
					break;
				case 'DocPast':
					docPastData = createData;
					tableID = 'tblPastDocList';
					break;
			}
			break;
		case 'S001':
			createData = [];
			break;
		case 'E001':
			ExecPgm('W040');
			break;
	}

	if(tableID !== ""){
		$('#' + tableID ).empty();
		var html = '';
		if( createData.length <= 0 ){
		}else{
			for( var i = 0; i < createData[0].line.length; i++ ){
				var lineData = createData[0].line[i];
				html = html + '<tr style="height: 24px;background-color:'+lineData.backColor+'">';
				html = html + '<td align="center" width="2%" nowrap>';
				html = html + '<input type="checkbox" class="chk' + kind + '" id="chk'  + kind + String(i) + '" data-color="' + lineData.color + '" data-kind="' + lineData.docKindCd + '" data-no="' + lineData.docNo + '">';
				html = html + '</td>';
				html = html + '<td align="center" width="8%" nowrap>';
				html = html + '<span id="dateFont' + kind + String(i) + '" style="color:' + lineData.color + '">' + lineData.publishDate + '</span>';
				html = html + '</td>';
				html = html + '<td align="center" width="7%" nowrap>';
				if( kind !== 'DocFavorite' ){
					html = html + '&nbsp;';
				}else{
					html = html + '<a href="#" class="palette" id="palette' + kind + String(i) + '" data-line="' + String(i) + '" data-color="' + lineData.color + '" ><img src="images/palette.png" alt="パレット" width="20" height="20" border="0" align="right"></a>';
				}
				html = html + '</td>';
				html = html + '<td>';
				html = html + '<span id="titleFont' + kind + String(i) + '"  style="color:' + lineData.color + '">' + lineData.title + '</span>';
				html = html + '</td>';
				html = html + '<td align="center" width="7%" nowrap>';
				if( lineData.detailFlg === '1' ){
					html = html + '<a href="#" class="detailHref" id="detail' + kind + String(i) + '" data-kind="' + kind + '" data-no="' + String(i) + '" data-title="' + lineData.title + '">詳細</a>';
				}else{
					html = html + '&nbsp;';
				}
				html = html + '</td>';
				html = html + '<td width="12%" nowrap>';
				html = html + '<span color="' + lineData.color + '">';
				if( lineData.tempFlg === '1' ){
					html = html + '<a href="#" class="hrefFile" id="href' + kind + String(i) + '" data-kind="' + lineData.docKindCd + '" data-no="' + lineData.docNo + '"><img src="images/attachment.png" alt="添付資料" width="69" height="20" border="0" align="middle"></a>';
				}else{
					html = html + '&nbsp;';
				}
				html = html + '</span>';
				html = html + '</td>';
				html = html + '</tr>';
			}
		}
		$('#' + tableID ).append(html);

		$('.detailHref').on(
			'click',
			function(){
				switch( $(this).attr('data-kind') ){
					case 'DocSummary':
						createDetailTable($(this).attr('data-no'),docData);
						break;
					case 'DocFavorite':
						createDetailTable($(this).attr('data-no'),docFavoriteData);
						break;
				}
				$('#dialogDocDetail').dialog('open');
			}
		);

		$('.hrefFile').on(
			'click',
			function(){
				var docKind = $(this).attr('data-kind');
				var docNo = $(this).attr('data-no');
				/*
				window.open("/Magic33Scripts/MGrqispi.dll?APPNAME=NAIS3WEB&PRGNAME=D220&ARGUMENTS=-ANAIS3WEB,-A" + $('#ssid').val() + ",-A" + docKind + ",-A" + docNo,
							   "",'toolbar=0,directories=0,status=1,resizable=1,scrollbars=1,menubar=0,left=100,top=60,width=800,height=500,');
							   */
				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('ANAIS3WEB');
				$('#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');
			}
		);

		$('.palette').on(
			'click',
			function(){
				var nowColor = $(this).attr('data-color');
				var newColor = 'black';
				for( var i = 0; i < palettePatern.length; i++ ){
					if( palettePatern[i] === nowColor ){
						if( i === palettePatern.length - 1 ){
							newColor = palettePatern[0];
						}else{
							newColor = palettePatern[i+1];
						}
						break;
					}
				}
				$(this).attr('data-color',newColor);

				var lineNo = $(this).attr('data-line');
				$('#chkDocFavorite' + lineNo).attr('data-color',newColor);
				$('#dateFontDocFavorite' + lineNo).css('color',newColor);
				$('#titleFontDocFavorite' + lineNo).css('color',newColor);
			}
		);
	}
}

function updateDocAttr(kbn,color,list){
	var param = {
		'appname':'NAIS3WEB',
		'prgname':'D210',
		'ssid': $('#ssid').val(),
		'updateKbn': kbn,
		'color': color,
		'list': list,
		'arguments':'ssid,updateKbn,color,list'
	};
	var xmlData = cmGetXmlData(param);
	if( $(xmlData).find('message').text() !== '' ){
		alert($(xmlData).find('message').text());
	}
	return xmlData;
}

function getPublishDoc(kbn){
	var param = {
		'appname':'NAIS3WEB',
		'prgname':'D200',
		'ssid': $('#ssid').val(),
		'searchKbn': kbn,
		'arguments':'ssid,searchKbn'
	};
	var xmlData;
	$.ajax({
		async : false,
		url: ajaxURL,
		data : param,
		dataType:'xml',
		scriptCharset: 'utf-8',
		success : function(xml){
			$('#ssid').val($(xml).find('ssid').text());	
			xmlData = xml;
		},
		error : function(XMLHttpRequest, textStatus, errorThrown){
				cmAjaxError(XMLHttpRequest, textStatus, errorThrown);
		}
	});
	//var xmlData = cmGetXmlData(param);
	return xmlData;
}

function MenuSelect(form,MenuIdx)
	{

	window.name = "";
	createAllRegistList('chkDocFavorite')
	var retXml = updateDocAttr('5',registColor,registList);

	if (MenuIdx == 1)
        {
		ExecPgm("W110");
    	}
	if (MenuIdx == 2)
        {
        ExecPgm("W120");
    	}
	if (MenuIdx == 3)
        {
        ExecPgm("W160");
    	}
	if (MenuIdx == 4)
        {
        ExecPgm("W190");
    	}
	if (MenuIdx == 5)
        {
        ExecPgm("W180");
    	}
	if (MenuIdx == 6)
        {
        //ExecPgm("D260");
		ExecPgm("D270");
    	}
	if (MenuIdx == 7)
        {
        ExecPgm("W095");
    	}
	if (MenuIdx == 8)
        {
        ExecPgm("D300");
    	}
	
	if (MenuIdx == 9)
        {
        NPClose();
    	}
    }
function ExecPgm(pgmId)
	    {
			$('#prgname').val(pgmId);
			//$('#menuForm').submit();
		// Input image タグの場合、submit の記述は不要
        }
function DispStatus(form)
        {
        status = ("");
        }
function NPClose(){
	top.location.replace("<!$MG_web_doc_alias>npclose.html");
}
function NPHelp(){
	var hdnWebDocAlias = $('#hdnWebDocAlias').val();
	window.open(hdnWebDocAlias+"help/nphelp_menu001.htm");
}
//支援情報
function SienJoho(order_no){
	/*
	document.W081.elements[1].value = "W082";
	document.W081.elements[2].value = "-A<!$MG_appname>,-A<!$MG_user>,-A"+ order_no ;
	document.W081.submit();
	*/
	$('#prgname').val('W082');
	$('#param1').val('W120');
	$('#param2').val(order_no);
	$('#menuForm').submit();
}
function ColChange(para){
	para.style.color = "#800080";
}
//投票
function callVote(voteNo){
	$('#prgname').val('W600');
	$('#param1').val(voteNo);
	$('#menuForm').submit();
}
//休暇抽選
function callHoliday(nendo){
	$('#prgname').val('W900');
	$('#param1').val(nendo);
	$('#menuForm').submit();
}

//Ridoc文書表示
function getDocument(docKind,docNo){

	var ssid = $('#ssid').val();
	cmWindowOpen("ファイル",'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,'','','','','','');

}
