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

$(document).ready(function(){
	window.name = "W900";
	
	if($('#hdnFinFlg').val() === '0'){
		$('.divBlock.clickEnable').on('click',function(){
			updateCheckBlock($(this).attr('data-blobkNo'));
		});
	}
	
	$('#btnGotoHolidayKakutei').click(function(){
		HolidayKakutei();
	});
	
	$('#btnGotoHolidayKakunin').click(function(){
		HolidayCheck();
	});
	
	callCheckBlock();
	
});

function callCheckBlock(){
	$('*').removeClass('cliclBlock');
	$('.divCheckBlock').each(function(){
		$(this).empty();
	});
	
	var setParam = {
		'appname':'NAIS3WEB',
		'prgname':'W901',
		'ssid': $('#ssid').val(),
		'nendo': $('#hdnNendo').val(),
		'chusenCnt' : $('#hdnChusenCnt').val(),
		'arguments':'ssid,nendo,chusenCnt'
	};
	$.ajax({
		async : false,
		url: ajaxURL,
		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();
			$(xml).find('line').each(function(){
				var blockNo = $(this).find('blockNo').text();
				var blockCapa = Number($(this).find('capacity').text());
				var blockChkCnt = Number($(this).find('checkCnt').text());
				
				var createChk = blockCapa;
				var checkClass = '';
				if(blockCapa === blockChkCnt){
					checkClass = 'checkLimit';
				}else if(blockCapa < blockChkCnt){
					checkClass = 'checkOver';
					createChk = blockChkCnt;
				}else {
					checkClass = 'checkSafe';
				}
				for(var i=0; i<blockChkCnt; i++){
					var checkPoint = '<span class="checkPoint '+checkClass+'">&nbsp;</span>';
					$('#divCheckBlock_'+blockNo).append(checkPoint);
				}
				for(var j=blockChkCnt; j<blockCapa; j++){
					var checkPointBlank = '<span class="checkPoint">&nbsp;</span>';
					$('#divCheckBlock_'+blockNo).append(checkPointBlank);
				}
				$('#divCheckBlock_'+blockNo).append('<div style="clear: both;"></div>');
			});
			
			$('#divBlock_'+$('#hdnSelectBlockNo').val()).addClass('cliclBlock');
			
			$('.spnMitohyo').text('未投票：'+$(xml).find('mitohyo').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 updateCheckBlock(blockNo){
	$('#hdnSelectBlockNo').val(blockNo);
	var setParam = {
		'appname':'NAIS3WEB',
		'prgname':'W902',
		'ssid': $('#ssid').val(),
		'nendo': $('#hdnNendo').val(),
		'chusenCnt' : $('#hdnChusenCnt').val(),
		'blockNo' : blockNo,
		'arguments':'ssid,nendo,chusenCnt,blockNo'
	};

	var kbn,msg;
	$.ajax({
		async : false,
		url: ajaxURL,
		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();
			callCheckBlock();
		},
		error : function(XMLHttpRequest, textStatus, errorThrown){
				cmAjaxError(XMLHttpRequest, textStatus, errorThrown);
		}
	});

	if(kbn === 'I001'){
		cmAlert("確認",msg,kbn);
	} else {
		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");
}

//確定者確認に移る
function HolidayKakutei(){
	var nendo = $('#hdnNendo').val();
	var chusenCnt = $('#hdnChusenCnt').val();
	$('#prgname').val('W920');
	$('#param1').val(nendo);
	$('#param2').val(chusenCnt);
	$('#param3').val('');
	$('#param4').val('');
	$('#param5').val('');
	$('#param6').val('');
	$('#param7').val('');
	$('#param8').val('');
	$('#param9').val('');
	$('#param10').val('');
	$('#param11').val('');
	$('#param12').val('');
	$('#menuForm').submit();
}

//休暇枠確認に移る
function HolidayCheck(){
	var nendo = $('#hdnNendo').val();
	var chusenCnt = $('#hdnChusenCnt').val();
	$('#prgname').val('W910');
	$('#param1').val(nendo);
	$('#param2').val(chusenCnt);
	$('#param3').val('');
	$('#param4').val('');
	$('#param5').val('');
	$('#param6').val('');
	$('#param7').val('');
	$('#param8').val('');
	$('#param9').val('');
	$('#param10').val('');
	$('#param11').val('');
	$('#param12').val('');
	$('#menuForm').submit();
}