﻿var dbselectOutID = "";
var funcDelCheck = DefCallBack;
var funcCloseCallBack = DefCallBack;

function DefCallBack(){
	return true;
}

function jSelectOpen(dbName, tblName, grpNo, outElmID, title, mode, callbackDel)
{
	dbselectOutID = outElmID;
	dbName 	= dbName ||"";
	tblName	= tblName || "";
	grpNo 	= grpNo || 0;
	mode 		= mode || 0;
	title 	= title || 'データ選択';
	funcDelCheck = callbackDel || DefCallBack;
	var src	= "jselect.php?DB=" + dbName + "&TBL=" + tblName + "&GRP=" + grpNo + "&MOD=" + mode;
	w = (mode ==1) ? 260 : 390;
//	$jBox( {id:'dbselect', width:w, height:310, title:title, ifsrc:src} );
	$jBox( {id:'dbselect', width:w, height:310, title:title, url:src} );
}

function jSelectClose(strSelect, valSelect)
{
	if (strSelect && dbselectOutID)	{
		var elm = $("#"+dbselectOutID);

		if ( elm.is(":input") ) {
			elm.val(strSelect);
		} else {
			var html = strSelect + "<span style=\"display: none;\" id=\"" + dbselectOutID + "_val\">" + valSelect + "</span>";
			elm.html(html);
		}
	}
	$jBox('dbselect').close();
}


function GetSelectVal()
{
	var val = $("#jselect-select option:selected").val();
	if ( !val ) {
		alert("データを選択して下さい !! ");
		return false;
	}
	return val;
}

function OnOk()
{
	var data = $("#jselect-select option:selected").text();
	var val = $("#jselect-select option:selected").val();
	if ( !val ) {
		self.alert("データを選択して下さい !! ");
		return false;
	}

	jSelectClose(data, val);
	return false;
}

function OnNew()
{
	$("#jselect-select").css("visibility", "hidden");
	$jBox( {id:'dbselect-edit', width:260, height:150, title:"データ追加", url:"jselect.php?ED=1", modal:true, hideclose:true, init: function(){$("#jselect-edit-form").submit(OnOkEdit);} } );
	return false;
}

function OnEdit()
{
	var val = GetSelectVal();
	if ( val ) {
		$("#jselect-select").css("visibility", "hidden");
		$jBox( {id:'dbselect-edit', width:260, height:150, title:"データ編集", url:"jselect.php?ED=2&ID="+val, modal:true, hideclose:true, init: function(){$("#jselect-edit-form").submit(OnOkEdit);} });
	}
	return false;
}

function OnUp()
{
	var val = GetSelectVal();
	if ( val ) {
		var param = {"UD":"1", "ID":val};
		$.get('jselect.php', param, OnLoadCallBack);
	}
	return false;
}

function OnDown()
{
	var val = GetSelectVal();
	if ( val ) {
		var param = {"UD":"2", "ID":val};
		$.get('jselect.php', param, OnLoadCallBack);
	}
	return false;
}

function OnDel()
{
	var val = GetSelectVal();
	if ( val ) {
		if ( !funcDelCheck() )
			return false;
		if ( confirm('選択項目を削除してもよろしいですか？\n削除すると元には戻せません。') )	{
			var param = {"DEL":"1", "ID":val};
			$.get('jselect.php', param, OnLoadCallBack);
		}
	}
	return false;
}

function OnLoadCallBack(data, textStatus)
{
	if (textStatus != "success")
	{
		alert("送信エラーが発生しました!!");
		return false;
	}
	var pos = data.indexOf("}",0) + 1;
	var json = data.substr(0, pos);
	var text = data.substr(pos);
	var ret = eval("("+json+")");
	
	if (ret['error'] != 0) {
		alert(text);
	} else {
		$("#jselect-select").html(text);
	}
}

function OnOkEdit()
{
	var url = $("#jselect-edit-form").attr("action");
	var text = $("#jselect-editdata").val();
	var post = {"DATA":text};
	$jBox( {id:'dbselect-edit', width:260, height:70, title:'メッセージ', msg:'データ送信中・・・・・'} );
	$.post(url, post, function(data, textStatus){
		if (textStatus != "success")
		{
			$jBox( {id:'dbselect-edit', width:260, height:70, title:'メッセージ', msg:'送信エラーが発生しました!!'} );
			return false;
		}
		var pos = data.indexOf("}",0) + 1;
		var json = data.substr(0, pos);
		var text = data.substr(pos);
		var ret = eval("("+json+")");
		
		if (ret['error'] != 0) {
			$jBox( {id:'dbselect-edit', width:260, height:70, title:'メッセージ', msg:text} );
		} else {
			$("#jselect-select").css("visibility", "visible");
			$("#jselect-select").html(text);
			$jBox("dbselect-edit").close();
		}
	});
}

function OnCancelEdit()
{
	$("#jselect-select").css("visibility", "visible");
	$jBox("dbselect-edit").close();
	return false;
}


