// JavaScript Document
var docData = [];
var docPastData = [];
var docFavoriteData = [];
var largeSummary = false;
var largeFavorite = false;
var largeContact = false;
var voteArr = {};
var tempSave = 0;
var retCheck = true;
var retVote = true;

$(document).ready(function(){
	window.name = "W600";
	$("#divVoteKakunin").hide();	
	$("#dialogTempSave").hide();	
	$("#dialogRetTopPage").hide();	
	$('#dialogNoVoting').hide();

	$('.selectBtn').click(function(){
		var lineNo = $(this).attr("data-btn_lineNo");
		getSelectPilot(lineNo);
	});

	$('.clearPilotBtn').click(function(){
		var lineNo = $(this).attr("data-btn_lineNo");
		$('input[data-lineNo="'+lineNo+'"].selectedPilotCd').val(0);
		$('input[data-lineNo="'+lineNo+'"].selectedPilotName').val("");
	});


	$('#btnVoteKakunin').click(function(){
		retCheck = false;
		retVote = false;
		
		checkVote(0);
		if(!retCheck){
			comfirmVote(0);
		}
	});

	$('#btnTempSave').click(function(){
		retCheck = false;
		retVote = false;
		
		checkVote(1);
		if(!retCheck){
			comfirmVote(1);
			if(!retVote){
			$('#dialogTempSave').dialog('open');
			}
		}
	});

	$('#btnRetInput').click(function(){
		$("#divVoteKakunin").hide();
		$("#divMain").show();
	});

	$('#btnUpdateVote').click(function(){
		if($('#loginPilotCd').val() === "000" ){
			$('#dialogNoVoting').dialog('open');
		}else{
			updateVote();
			if(!retVote){
				$('#dialogRetTopPage').dialog('open');
			}
		}
	});


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



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

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

	$('#dialogSelectPilot').dialog({
		autoOpen: false,
		title: "水先人選択",
		closeOnEscape: false,
		width: 400,
		height: 600,
		modal: true,
		open: function() {
		},
		buttons: {
	        '閉じる': function(){
				$(this).dialog("close");
			}
    	}
	});

	$('input[type="radio"].selectedIninCd').change(function(){
		var ininCd = $(this).val();
		if (ininCd==="99"){
			$('#selectedPilotCd').val(0);
			$('#selectedPilotName').val("");
			$('#selectedPilotName').prop('disabled', false);
			$('#selectBtn').prop('disabled', false);
			$('#clearPilotBtn').prop('disabled', false);
		}else{
			$('#selectedPilotCd').val(0);
			$('#selectedPilotName').val("");
			$('#selectedPilotName').prop('disabled', true);
			$('#selectBtn').prop('disabled', true);
			$('#clearPilotBtn').prop('disabled', true);
		}
	});

/*	$('input[type="checkbox"]').change(function(){
		var thisID = $(this).attr('id');
		if($(this).prop('checked')){
			$('label[for="'+thisID+'"]').css('color','#F00');
		}else {
			$('label[for="'+thisID+'"]').css('color','#000');
		}
	});*/
	

	$('.hrefFile').on(
		'click',
		function(){
			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('W601');
			$('#w_param1').val('ANAIS3WEB');
			$('#w_param2').val($('#ssid').val());
			$('#w_param3').val($('#voteNo').val());
			$('#w_param4').val($(this).attr('data-fileNo'));
			$('#w_param5').val('');
			$('#w_param6').val('');
			$('#w_param7').val('');
			$('#w_param8').val('');
			$('#w_param9').val('');
			$('#w_param10').val('');
			$('#windowForm').submit();
			$('#windowForm').attr('target','_self');
		}
	);

})

function getSelectPilot(lineNo,sortKbn){
	var setParam = {
		'appname':'NAIS3WEB',
		'prgname':'W605',
		'ssid': $('#ssid').val(),
		'voteNo': $('#voteNo').val(),
		'lineNo': lineNo,
		'ininCd': $('input:radio[name="ininCd"]:checked').val(),
		'sortKbn': sortKbn,
		'pilotCls': $('input:radio[name="pilotCls"]:checked').val(),
		'arguments':'ssid,voteNo,lineNo,ininCd,sortKbn,pilotCls'
	}
	$.ajax({
		async : false,
		url: ajaxURL,
		data : setParam,
		type : 'POST',
		dataType:'html',
		scriptCharset: 'utf-8',
		success : function(html){
			var getHtml = $($.parseHTML(html));
			var sessionError = getHtml.filter('#sessionError').val();
			if(sessionError === '0'){
				$('#divSelectPilot').empty();
				$('#divSelectPilot').append(getHtml.filter('#divSelectPilotRes')[0].innerHTML);

				$('.choicePilotBtn').click(function(){
					var pilotCd = $(this).attr("data-pilotCd");
					var pilotName = $(this).attr("data-pilotName");
					$('input[data-lineNo="'+lineNo+'"].selectedPilotCd').val(pilotCd)
					$('input[data-lineNo="'+lineNo+'"].selectedPilotName').val(pilotName)
					$('#dialogSelectPilot').dialog("close");
				});

				$('.sortPilotBtn').click(function(){
					var lineNo = $(this).attr("data-btn_lineNo");
					var sortKbn = $(this).attr("data-sortKbn");
					$('#dialogSelectPilot').dialog("close");
					getSelectPilot(lineNo,sortKbn);
				});


				$('#dialogSelectPilot').dialog('open');
			}else {
				cm_sessionError();
			}
			
		},
		error : function(XMLHttpRequest, textStatus, errorThrown){
			cmAjaxError(XMLHttpRequest, textStatus, errorThrown);
		}
	});
}

function checkVote(tempSave){
	voteArr = [];
	var voteNo ="";
	var counter=0;
	var addArr = [];
	$('div.divVote').each(function(){
		voteNo = $(this).attr('data-voteNo');
		var voteTitle = $(this).attr('data-voteTitle');
		var lineNo = $(this).attr('data-vote_lineNo');
		var voteCnt = parseInt($(this).attr('data-vote_cnt'));
		var voteType =$(this).attr('data-voteType');
		var blankVote =$(this).attr('data-blankVote');
		var pilotCd =""; 
		var selectNo= 0;
		var checkCnt = 0;
		var errorFlg =0;
		var vote_mizusakiCd = "";

		if(voteType==="1" || voteType==="2"){
			selectNo = $('input:radio[name="senkyoLine'+lineNo+'"]:checked').val();
			if(blankVote === "0" && tempSave===0){
				if(selectNo===0 || selectNo==null ){
					if(voteType==="1"){
						var errerHTML = '<span>'+voteTitle+'</span><br>' +
									'<span>'+'選択肢のいずれかを選択してください。'+ '</span>';
					}else{
						var errerHTML = '<span>'+voteTitle+'</span><br>' +
									'<span>'+'選択肢のいずれかを選択してください。'+ '</span>';
					}
					errorFlg=1;
				}
			}
		}

		if(voteType==="3"){
			vote_mizusakiCd=$('input[data-lineNo="'+lineNo+'"].selectedPilotCd').val();
			if(blankVote==="0" && tempSave===0){
				if (vote_mizusakiCd==="" || vote_mizusakiCd==="0" || vote_mizusakiCd==="000"){
					var errerHTML = '<span>'+voteTitle+'</span><br>' +
								'<span>'+'水先人を選択してください。'+ '</span>';
						errorFlg=1;
				}
			}
		}

		if(voteType==="4" || voteType==="5" || voteType==="6" || voteType==="7" || voteType==="8" || voteType==="9"){
			$('input[type="checkbox"][data-voteNo="'+voteNo+'"][data-vote_lineNo="'+lineNo+'"]').each(function(){
				if($(this).prop('checked')){
					checkCnt = checkCnt + 1;
					vote_mizusakiCd = vote_mizusakiCd + $(this).attr("data-mizusakiCd") + '@';
				}
			});
			
			if(voteCnt !== checkCnt && blankVote==="0" && tempSave===0){
				var errerHTML = '<span>'+voteTitle+'</span><br>' +
								'<span>'+'候補者は'+voteCnt+'人選択してください。'+ '</span>';
				errorFlg=1;
			}else {
				vote_mizusakiCd = vote_mizusakiCd.slice(0,-1);
			}
		}

		if(errorFlg===1){
				$('#dialogErrorContents').empty();
				$('#dialogErrorContents').append(errerHTML);
				$('#dialogError').dialog('open');
				retCheck = true;
				return false;
		}

		addArr = [];
		addArr['voteNo'] = voteNo;
		addArr['lineNo'] = lineNo;
		addArr['selectNo'] = selectNo;
		addArr['mizusakiCd'] = vote_mizusakiCd;
		voteArr.push(addArr);
        counter=counter+1;
	});
 
   /* 未使用のパラメータエリアを初期化するに*/
    while(counter<20){
		addArr = [];
		addArr['voteNo'] = voteNo;
		addArr['lineNo'] = counter+1;
		addArr['selectNo'] = 0;
		addArr['mizusakiCd'] = "";
		voteArr.push(addArr);
        counter=counter+1;
    }
}

function comfirmVote(tempSave){

		var setParam = {
			'appname':'NAIS3WEB',
			'prgname':'W610',
			'ssid': $('#ssid').val(),
			'voteNo': $('#voteNo').val(),
			'tempSave': tempSave,
			'ininSaki' : $('input:radio[name="ininCd"]:checked').val(),
			'ininPilot': $('input[data-lineNo="0"].selectedPilotCd').val(),
			'lineDat1': voteArr[0]['lineNo'] + '@' + voteArr[0]['selectNo'] + '@' + voteArr[0]['mizusakiCd'] ,
			'lineMsg1': $('#voteMsg01').val(),
			'lineDat2': voteArr[1]['lineNo'] + '@' + voteArr[1]['selectNo'] + '@' + voteArr[1]['mizusakiCd'] ,
			'lineMsg2': $('#voteMsg02').val(),
			'lineDat3': voteArr[2]['lineNo'] + '@' + voteArr[2]['selectNo'] + '@' + voteArr[2]['mizusakiCd'],
			'lineMsg3': $('#voteMsg03').val(),
			'lineDat4': voteArr[3]['lineNo'] + '@' + voteArr[3]['selectNo'] + '@' + voteArr[3]['mizusakiCd'],
			'lineMsg4': $('#voteMsg04').val(),
			'lineDat5': voteArr[4]['lineNo'] + '@' + voteArr[4]['selectNo'] + '@' + voteArr[4]['mizusakiCd'],
			'lineMsg5': $('#voteMsg05').val(),
			'lineDat6': voteArr[5]['lineNo'] + '@' + voteArr[5]['selectNo'] + '@' + voteArr[5]['mizusakiCd'] ,
			'lineMsg6': $('#voteMsg06').val(),
			'lineDat7': voteArr[6]['lineNo'] + '@' + voteArr[6]['selectNo'] + '@' + voteArr[6]['mizusakiCd'] ,
			'lineMsg7': $('#voteMsg07').val(),
			'lineDat8': voteArr[7]['lineNo'] + '@' + voteArr[7]['selectNo'] + '@' + voteArr[7]['mizusakiCd'],
			'lineMsg8': $('#voteMsg08').val(),
			'lineDat9': voteArr[8]['lineNo'] + '@' + voteArr[8]['selectNo'] + '@' + voteArr[8]['mizusakiCd'],
			'lineMsg9': $('#voteMsg09').val(),
			'lineDat10': voteArr[9]['lineNo'] + '@' + voteArr[9]['selectNo'] + '@' + voteArr[9]['mizusakiCd'],
			'lineMsg10': $('#voteMsg10').val(),
			'lineDat11': voteArr[10]['lineNo'] + '@' + voteArr[10]['selectNo'] + '@' + voteArr[10]['mizusakiCd'] ,
			'lineMsg11': $('#voteMsg11').val(),
			'lineDat12': voteArr[11]['lineNo'] + '@' + voteArr[11]['selectNo'] + '@' + voteArr[11]['mizusakiCd'] ,
			'lineMsg12': $('#voteMsg12').val(),
			'lineDat13': voteArr[12]['lineNo'] + '@' + voteArr[12]['selectNo'] + '@' + voteArr[12]['mizusakiCd'],
			'lineMsg13': $('#voteMsg13').val(),
			'lineDat14': voteArr[13]['lineNo'] + '@' + voteArr[13]['selectNo'] + '@' + voteArr[13]['mizusakiCd'],
			'lineMsg14': $('#voteMsg14').val(),
			'lineDat15': voteArr[14]['lineNo'] + '@' + voteArr[14]['selectNo'] + '@' + voteArr[14]['mizusakiCd'],
			'lineMsg15': $('#voteMsg15').val(),
			'lineDat16': voteArr[15]['lineNo'] + '@' + voteArr[15]['selectNo'] + '@' + voteArr[15]['mizusakiCd'] ,
			'lineMsg16': $('#voteMsg16').val(),
			'lineDat17': voteArr[16]['lineNo'] + '@' + voteArr[16]['selectNo'] + '@' + voteArr[16]['mizusakiCd'] ,
			'lineMsg17': $('#voteMsg17').val(),
			'lineDat18': voteArr[17]['lineNo'] + '@' + voteArr[17]['selectNo'] + '@' + voteArr[17]['mizusakiCd'],
			'lineMsg18': $('#voteMsg18').val(),
			'lineDat19': voteArr[18]['lineNo'] + '@' + voteArr[18]['selectNo'] + '@' + voteArr[18]['mizusakiCd'],
			'lineMsg19': $('#voteMsg19').val(),
			'lineDat20': voteArr[19]['lineNo'] + '@' + voteArr[19]['selectNo'] + '@' + voteArr[19]['mizusakiCd'],
			'lineMsg20': $('#voteMsg20').val(),
			'arguments':'ssid,voteNo,tempSave,ininSaki,ininPilot,lineDat1,lineMsg1,lineDat2,lineMsg2,lineDat3,lineMsg3,lineDat4,lineMsg4,lineDat5,lineMsg5,lineDat6,lineMsg6,lineDat7,lineMsg7,lineDat8,lineMsg8,lineDat9,lineMsg9,lineDat10,lineMsg10,lineDat11,lineMsg11,lineDat12,lineMsg12,lineDat13,lineMsg13,lineDat14,lineMsg14,lineDat15,lineMsg15,lineDat16,lineMsg16,lineDat17,lineMsg17,lineDat18,lineMsg18,lineDat19,lineMsg19,lineDat20,lineMsg20'
		};

	if (tempSave === 0){
		/* 投票確認画面を表示 */
		$.ajax({
			async : false,
			url: ajaxURL,
			type : 'POST',
			data : setParam,
			dataType:'html',
			scriptCharset: 'utf-8',
			success : function(html){
				var getHtml = $($.parseHTML(html));
				var sessionError = getHtml.filter('#sessionError').val();
				if(sessionError === '0'){
					$('#divVoteKakuninSub').empty();
					$('#divVoteKakuninSub').append(getHtml.filter('#divVoteKakuninSubRes')[0].innerHTML);
					$("#divMain").hide();
					$("#divVoteKakunin").show();
				}else {
					cm_sessionError();
				}
				
			},
			error : function(XMLHttpRequest, textStatus, errorThrown){
				cmAjaxError(XMLHttpRequest, textStatus, errorThrown);
			}
		});
	}else{
		/* 一時保存 */
		var kbn,msg;
		$.ajax({
			async : false,
			url: ajaxURL,
			type : 'POST',
			data : setParam,
			dataType:'xml',
			scriptCharset: 'utf-8',
			success : function(xml){
				$('#ssid').val($(xml).find('ssid').text());
				kbn = $(xml).find('ret').text();
				msg = $(xml).find('message').text();
			},
			error : function(XMLHttpRequest, textStatus, errorThrown){
					cmAjaxError(XMLHttpRequest, textStatus, errorThrown);
			}
		});
		
		if(kbn !== 'I001'){
			var errerHTML = '<span>'+msg + '</span>';
			$('#dialogErrorContents').empty();
			$('#dialogErrorContents').append(errerHTML);
			$('#dialogError').dialog('open');
			retVote = true;
			return false;
		}
	}

}

function updateVote(voteNo,lineNo,mizusakiStr){

		var setParam = {
			'appname':'NAIS3WEB',
			'prgname':'W620',
			'ssid': $('#ssid').val(),
			'voteNo': $('#voteNo').val(),
			'arguments':'ssid,voteNo'
		};
		
		var kbn,msg;
		$.ajax({
			async : false,
			url: ajaxURL,
			type : 'POST',
			data : setParam,
			dataType:'xml',
			scriptCharset: 'utf-8',
			success : function(xml){
				$('#ssid').val($(xml).find('ssid').text());
				kbn = $(xml).find('ret').text();
				msg = $(xml).find('message').text();
			},
			error : function(XMLHttpRequest, textStatus, errorThrown){
					cmAjaxError(XMLHttpRequest, textStatus, errorThrown);
			}
		});
		
		if(kbn !== 'I001'){
			var errerHTML = '<span>'+msg + '</span>';
			$('#dialogErrorContents').empty();
			$('#dialogErrorContents').append(errerHTML);
			$('#dialogError').dialog('open');
			retVote = true;
			return false;
		}
}

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