var MasterControl = Class.create();

function onLoadMasterControl() {
	ctrl = new MasterControl();
}

MasterControl.prototype = {

	initialize: function() {
		this.ajaxwin = null;
		this.scrX = 0;
		this.scrY = 0;
	},

	center_ajaxwin: function () {
		if (this.ajaxwin != null && this.ajaxwin.style.display=="block") {
			this.ajaxwin.moveTo('middle', 'middle');
			window.scroll(this.scrX,this.scrY);
		} else {
			this.scrX = document.documentElement.scrollLeft || document.body.scrollLeft;
			this.scrY = document.documentElement.scrollTop || document.body.scrollTop;;
		}
 	},

	//Ajax画面呼出
	popup_open: function (motion) {
		try {
			var obj = document.getElementById("main_form");
			var openurl = obj.action+"?command="+obj.command.value;
			if (motion != null) {
				openurl += "&motion="+motion;
			}
			this.popup_open_direct(openurl);
		} catch (e) {
			alert("popup_open:"+e);
		}
		return false;
	},

	popup_open_ex: function (motion,key) {
		try {
			var obj = document.getElementById("main_form");
			var openurl = obj.action+"?command="+obj.command.value;
			if (motion != null) {
				openurl += "&motion="+motion;
			}
			if (key != null) {
				openurl += "&"+key+"="+document.getElementById(key).value;
			}
			this.popup_open_direct(openurl);
		} catch (e) {
			alert("popup_open:"+e);
		}
		return false;
	},

	popup_open_direct: function (openurl) {
		try {
			this.ajaxwin=dhtmlwindow.open("ajaxbox","iframe",openurl,"大あたり.jp","width=640px,height=480px,left=200px,top=120px,resize=1,scrolling=1")
			this.ajaxwin.moveTo('middle', 'middle');
		} catch (e) {
			alert("popup_open:"+e);
		}
		return false;
	},

	popup_open_image: function (label,code,image) {
		try {
			this.ajaxwin=dhtmlwindow.open("ajaxbox","iframe","./entry?command=image_popup&code="+code+"&image="+image,label,"width=740px,height=570px,left=200px,top=120px,resize=0,scrolling=0")
			this.ajaxwin.moveTo('middle', 'middle');
		} catch (e) {
			alert("popup_open:"+e);
		}
		return false;
	},

	popup_viwer: function (openurl) {
		try {
			this.ajaxwin=dhtmlwindow.open("ajaxbox","iframe",openurl,"大あたり.jp","width=820px,height=500px,left=200px,top=120px,resize=1,scrolling=1")
			this.ajaxwin.moveTo('middle', 'middle');
		} catch (e) {
			alert("popup_open:"+e);
		}
		return false;
	},

	//Ajax画面を閉じる
	popup_close: function() {
		this.ajaxwin.hide();
	},

	//Ajax画面を閉じる
	parent_popup_close: function() {
		parent.ctrl.ajaxwin.hide();
	},

	//TOPに戻る
	back: function() {
		setValueName("command","");
		setValueName("motion","");
		document.getElementById("main_form").submit();
	},

	//コンテンツTOPに戻る
	back_cont: function() {
		setValueName("motion","back_cont");
		document.getElementById("main_form").submit();
	},

	//検索
	search: function() {
		setValueName("motion","search");
		document.getElementById("main_form").submit();
	},

	//CSVダウンロード
	csv: function() {
		setValueName("motion","csv");
		document.getElementById("main_form").submit();
	},

	//ページ切り替え
	page: function(page) {
		setValueName("motion","");
		setValueName("page",page);
		document.getElementById("main_form").submit();
	},

	//検索
	search: function() {
		setValueName("motion","");
		document.getElementById("main_form").submit();
	},

	//トグルボタン
	toggle: function(key1,value1,key2,value2) {
		setValueName("motion","toggle");
		setValueName(key1,value1);
		setValueName(key2,value2);
		document.getElementById("main_form").submit();
	},

	//ソート
	sort: function(sortKey) {
		var orgSortKey = getValueName("sort_key");
		var orgSortAscending = getValueName("sort_ascending");
		if (orgSortKey == sortKey) {
			if (orgSortAscending=="-1") {
				setValueName("sort_ascending","1");
			} else {
				setValueName("sort_ascending","-1");
			}
		} else {
			setValueName("sort_ascending","1");
		}
		setValueName("motion","sort");
		setValueName("sort_key",sortKey);
		document.getElementById("main_form").submit();
	},

	//閲覧画面呼出
	view: function(key,value) {
		setValueName("motion","view");
		setValueName(key,value);
		document.getElementById("main_form").submit();
	},

	//新規登録画面呼出
	add: function() {
		setValueName("motion","add");
		document.getElementById("main_form").submit();
	},

	//新規登録確認
	add_confirm: function() {
		setValueName("motion","addconfirm");
		document.getElementById("main_form").submit();
	},

	//新規登録完了
	add_end: function() {
		setValueName("motion","addend");
		document.getElementById("main_form").submit();
	},

	//新規登録に戻る
	add_back: function() {
		setValueName("motion","addback");
		document.getElementById("main_form").submit();
	},

	//修正画面呼出
	mod: function(key,value) {
		setValueName("motion","mod");
		setValueName(key,value);
		document.getElementById("main_form").submit();
	},

	//修正確認
	mod_confirm: function() {
		setValueName("motion","modconfirm");
		document.getElementById("main_form").submit();
	},

	//修正完了
	mod_end: function() {
		setValueName("motion","modend");
		document.getElementById("main_form").submit();
	},

	//修正に戻る
	mod_back: function() {
		setValueName("motion","modback");
		document.getElementById("main_form").submit();
	},

	//修正
	mod_once: function(key,value) {
		setValueName("motion","modonce");
		setValueName(key,value);
		document.getElementById("main_form").submit();
	},

	//削除画面呼出
	del: function(key,value) {
		setValueName("motion","del");
		setValueName(key,value);
		document.getElementById("main_form").submit();
	},

	//削除確認
	del_confirm: function() {
		setValueName("motion","delconfirm");
		document.getElementById("main_form").submit();
	},

	//削除完了
	del_end: function() {
		if (!confirm("削除しますよろしいですか?")){
			return;
		}
		setValueName("motion","delend");
		document.getElementById("main_form").submit();
	},

	//削除
	del_once: function(key,value) {
		if (!confirm("削除しますよろしいですか?")){
			return;
		}
		setValueName("motion","delonce");
		setValueName(key,value);
		document.getElementById("main_form").submit();
	},

	//画面遷移
	send: function(command,action,key,value) {
		setValueName("command",command);
		setValueName("action",action);
		setValueName("motion","");
		setValueName(key,value);
		document.getElementById("main_form").submit();
	},

	//拡張１
	extension1: function(key,value) {
		setValueName("motion","extension1");
		setValueName(key,value);
		document.getElementById("main_form").submit();
	},

	//拡張２
	extension2: function(key,value) {
		setValueName("motion","extension2");
		setValueName(key,value);
		document.getElementById("main_form").submit();
	},

	//拡張３
	extension3: function(key,value) {
		setValueName("motion","extension3");
		setValueName(key,value);
		document.getElementById("main_form").submit();
	},

	//拡張４
	extension4: function(key,value) {
		setValueName("motion","extension4");
		setValueName(key,value);
		document.getElementById("main_form").submit();
	},

	//表示・非表示
	showHide: function(id,showhide) {
		try {
			document.getElementById(id).style.display=showhide;
		} catch (e) {}
	},

	//代理
	agent:function(acode) {
		window.open('admin?acode='+acode, 'agent', 'menubar=no, toolbar=no, scrollbars=yes');
	}
};

