// JavaScript Document
$(document.body).addClass('smoothness');

$(function(){
	$('#com_dialog').dialog({
		modal: true,
		autoOpen: false,
		width: 400,
		buttons: {
			"Ok": function() { 
				$(this).dialog("close"); 
			}
		}
	});
	$('#com_network_dialog').dialog({
		modal: true,
		autoOpen: false,
		width: 200,
		buttons: {}
	});

	$('#com_logout_dialog').dialog({
		modal: true,
		autoOpen: false,
		width: 200,
		buttons: {
			"CANCEL": function() { 
				$(this).dialog("close"); 
			},
			"Ok": function() { 
				$(this).dialog("close");
				location.href="http://"+document.domain+"/new/index.php";
			}

		}
	});
	$('#com_login_dialog').dialog({
		title: "ログイン",
		modal: true,
		autoOpen: false,
		width: 300,
		buttons: {
			"CANCEL": function() { 
				$(this).dialog("close"); 
			},
			"Ok": function() { 
				com_login_ajax();
			}

		}
	});

});

/*
 * 日付チェック
 *
 * zero_flag: trueの場合0はOK
 */
function com_date_check( input_data, message, zero_flag ){
	if( input_data.value.length <= 0 ){
		if( zero_flag == true ){
			return true;
		}else{
			$('#com_dialog').html(message+"が入力されていません");
			$('#com_dialog').dialog('open');
			input_data.focus();
			return false;
		}
	}
	if( input_data.value.match( /^(\d{4})\/(\d{2})\/(\d{2})$/ ) ){
		return true;
	}else{
		$('#com_dialog').html(message+"が正しくありません");
		$('#com_dialog').dialog('open');
		input_data.focus();
		return false;
	}
}

var com_login_after;
function com_login_chk( a_tag ){
	if( $('#com_login_dialog').html().length <= 2 ){
		return true;
	}
	com_login_after = a_tag.href;
	$('#com_login_dialog').dialog( "open" );
	return false;
}

function com_login_ajax(){
	// チェック
	if( document.fm_login.user_id.value.length <= 0 ){
		$('#error_msg').html("IDが入力されていません");
		document.fm_login.user_id.focus();
		return;
	}
	if( document.fm_login.passwd.value.length <= 0 ){
		$('#error_msg').html("PASSWORDが入力されていません");
		document.fm_login.passwd.focus();
		return;
	}
	
	$.ajax({
		type: "POST",
		url: "login.php",
		data: $('#fm_login').serialize(),
		success: function(msg){
			$('#com_network_dialog').dialog('close');
			if( msg == "NG" ){
				$('#error_msg').html("ログイン失敗");
			}else{
				location.href=com_login_after;
			}
		}
	});
}

