// JavaScript Document

function allCheck(check, name)
{
	if(check.checked==true){
		chk = true;
	}else{
		chk = false;
	}

　　for(i = 0; i < document.form.elements.length; i++){
　　　　if(document.form.elements[i].type == "checkbox" && document.form.elements[i].name == name){
			document.form.elements[i].checked = chk;
　　　　}
　　}
}

function deleteMultiRecord(type, name, ret, mes)
{
	list = "";
　　for(i = 0; i < document.form.elements.length; i++){
　　　　if(document.form.elements[i].type == "checkbox" && document.form.elements[i].name == name && document.form.elements[i].checked == true){
			if(list != "") list += "/";
　　　　　　list += document.form.elements[i].value;
　　　　}
　　}

	if(!type || !list){
		alert("削除対象が選択されていません。");
		return;
	}

	if(!mes) mes = 'これらのデータを削除しますか？';

	if(window.confirm(mes)){
		if(ret){
			location.href = "proc.php?run=multiDelete&link=ret&type=" + type + "&list=" + list + "&ret=" + encodeURIComponent(ret);
		}else{
			location.href = "proc.php?run=multiDelete&link=ref&type=" + type + "&list=" + list;
		}
	}
}

function deleteRecord(type, id, ret, mes)
{
	if(!type || !id){
		alert("削除対象が選択されていません。");
		return;
	}
	
	if(!mes) mes = 'このデータを削除しますか？';

	if(window.confirm(mes)){
		if(ret){
			location.href = "proc.php?run=delete&link=ret&type=" + type + "&id=" + id + "&ret=" + encodeURIComponent(ret);
		}else{
			location.href = "proc.php?run=delete&link=ref&type=" + type + "&id=" + id;
		}
	}
}

function editRecord(type, id, name, val, ret, mes, comp)
{
	if(!type || !id || !name){
		alert("変更対象が選択されていません。");
		return;
	}

	param = "";

	if(!mes) mes = '値を入力してください。';
	if(comp) param = '&comp=true';

	jPrompt(mes, val, '　',function(input){
		if(input)
		{
			if(ret){
				location.href = "proc.php?run=edit&link=ret&type=" + type + "&id=" + id + param + "&name=" + name + "&val=" + encodeURIComponent(input) + "&ret=" + encodeURIComponent(ret);
			}else{
				location.href = "proc.php?run=edit&link=ref&type=" + type + "&id=" + id + param + "&name=" + name + "&val=" + encodeURIComponent(input);
			}
		}
	});
}

function filedelete(type, id, name, ret, mes)
{
	if(!type || !id || !name){
		alert("削除対象が選択されていません。");
		return;
	}

	if(!mes) mes = 'このファイルを削除しますか？';

	if(window.confirm(mes)){
		if(ret){
			location.href = "proc.php?run=filedelete&link=ret&type=" + type + "&id=" + id + "&name=" + name + "&ret=" + encodeURIComponent(ret);
		}else{
			location.href = "proc.php?run=filedelete&link=ref&type=" + type + "&id=" + id + "&name=" + name;
		}
	}
}

function getRequest(){
	if(location.search.length > 1) {
		var get = new Object();
		var ret = location.search.substr(1).split("&");
		for(var i = 0; i < ret.length; i++) {
			var r = ret[i].split("=");
			get[r[0]] = r[1];
		}
		return get;
	}else{
		return false;
	}
}

var child_class_name;

function changeParentList(form, parent_key, child_type, child_key, sort_key, sort_val, class_name)
{
	child_class_name = class_name;
	var val = form.options[form.options.selectedIndex].value;
	$.getJSON('proc.php?run=jsonChildList&id=' + val + '&parent_key=' + parent_key + '&child_type=' + child_type + '&child_key=' + child_key + '&sort_key=' + sort_key + '&sort_val=' + sort_val, null, setChildList);
}

function setChildList(data)
{
	var select  = $('#' + child_class_name);
	var current = select.val();
	
	select.clearOptions();
	for(var key in data){
		select.addOption(data[key], key);
	}
}
