// JavaScript Document
$(document).ready(function(){
	$('#divAjax').remove();
	var divAjax =
		'<div style="text-align:center;z-index:10000;" class="Shadow" id="divAjax">'+
			'<div  style="width:48px;height:48px;position:absolute;top:50%;margin-top:-24px;left:50%;margin-left:-24px;">'+
				'<img src="../../webnais/images/loading.gif" width="48" height="48">'+
			'</div>'+
		'</div>';
	$(divAjax).appendTo('body');
	$('#divAjax').hide();
	
	getID();
	
	$('#txtSUSER').focus();
	
	$('#btnClose').on('click',function(){
		window.open("about:blank","_self").close();
	});
	
	$('#btnLogin').on('click',function(){
		if($('#txtSUSER').val() !== "" && $('#txtPass').val() !== ""){
			if($('#chbMemory').prop('checked')){
				setCookie('naisID',$('#txtSUSER').val());
			}else {
				setCookie('naisID','');
			}
			var checkStr = checkProhibition($('#txtSUSER').val());
			if(checkStr !== ""){
				alert(checkStr);
			}else {
				var loginParam = {
					'appname':nasiappname,
					'prgname':'W011',
					'loginID' : $('#txtSUSER').val(),
					'loginPass' : $('#txtPass').val(),
					'userKbn' :'0',
					'arguments': "loginID,loginPass,userKbn"
				};
				
				$.ajax({
					async : true,
					url: loginURL,
					data : loginParam,
					dataType : 'xml',
					scriptCharset : 'utf-8',
					type : 'post',
					success : function(xml){
						var kekka = $(xml).find('kbn').text();
						var msg = $(xml).find('message').text();
						var ssid = $(xml).find('ssid').text();
						if(kekka !== 'I001'){
							alert(msg);
							$('#txtSUSER').focus();
						}else {
							$('#hdnSSID').val(ssid);
							$('#loginForm').submit();
						}
					}
				});
			}
		}else {
			alert('ユーザＩＤ、パスワードを入力してください。');
		}
		
	});
});

function setCookie(key,val){
	life = new Date();
	life.setTime(life.getTime()+24*60*60*1000*30);
	document.cookie = key+"="+escape(val)+"; expires="+life.toGMTString()+"; ";
}

function getCookie(key)	{
	var strCookie = document.cookie;
	var strData = strCookie.split("; ");

	for(var i=0; i<strData.length; i++){
		var cookieValue = strData[i].split("=");
		if(key == cookieValue[0]){
			if (!cookieValue[1]){
				// undefined,null,""（空文字）の場合
				return null;
			}
			return unescape(cookieValue[1]);
		}
	}
}

function getID(){
	var loginID = getCookie("naisID");
	if(loginID != null){
		$('#txtSUSER').val(loginID);
		$('#txtPass').val('');
		$('#chbMemory').prop('checked',true);
	}else {
		$('#txtSUSER').val('');
		$('#txtPass').val('');
		$('#chbMemory').prop('checked',false);
	}
}