<?php
/**
 * popup関係の関数
 * $Id: do_select_popup.php
 * @author zhouz
 * @access public
 * @package manager.public_html.dynamicscripts
 */
// クラス・設定読み込み
require_once("../manager_include.inc");
?>
// <!--
/**
 * doHelpPopup();
 * @return
 */
function doHelpPopup(html_data , event) {
	var event = event || window.event;

	var width_min = 320;
	var width_max = 320;
	var height_min = 320;
	var height_max = 320;
	var win_x = document.body.clientWidth;
	var win_y = document.body.clientHeight;
	var mouse_x = event.pageX;
	var mouse_y = event.pageY;
	
	var selectdialogouterframe = document.getElementById('selectdialogouterframe');
	if (mouse_x <= (win_x - 340) && mouse_y <= (win_y - 325)) {
		selectdialogouterframe.style.top = (mouse_y + 5) + "px";
		selectdialogouterframe.style.left = (mouse_x + 20) + "px";
	} else if (mouse_x <= (win_x - 340) && mouse_y > (win_y - 325)) {
		selectdialogouterframe.style.top = (mouse_y - 325) + "px";
		selectdialogouterframe.style.left = (mouse_x + 20) + "px";
	} else if (mouse_x > (win_x - 340) && mouse_y <= (win_y - 325)) {
		selectdialogouterframe.style.top = (mouse_y + 5) + "px";
		selectdialogouterframe.style.left = (mouse_x - 325) + "px";
	} else {
		selectdialogouterframe.style.top = (mouse_y - 325) + "px";
		selectdialogouterframe.style.left = (mouse_x - 325) + "px";
	}
	
	selectdialogouterframe.style.zIndex = "3";
	selectdialogouterframe.style.visibility = "visible";
	selectdialogouterframe.style.position = "absolute";
	selectdialogouterframe.style.margin = "0px";
	
	doPopup(html_data,width_min, width_max, height_min, height_max);

	$("#selectdialogouterframe").mouseout(function(){
		$("body").click(function(){
			closeSelectPopup();
		});
	});
}

/**
 * doSelectPopup();
 * @return
 */
function doSelectPopup(html_data) {
	var width_min = <?=POPUP_WINDOW_INFO_MIN_WIDTH ?>;
	var width_max = <?=POPUP_WINDOW_INFO_MAX_WIDTH ?>;
	var height_min = <?=POPUP_WINDOW_INFO_MIN_HEIGHT ?>;
	var height_max = <?=POPUP_WINDOW_INFO_MAX_HEIGHT ?>;

	var selectpopup = document.getElementById('selectpopup');
	selectpopup.className = "showdialog";

	doPopup(html_data,width_min, width_max, height_min, height_max);
}

/**
 * setCheckboxValue
 * @param viewTarget
 * @param valueTarget
 * @param requestUrl
 */
function setCheckboxValue(viewTarget, valueTarget, requestUrl, formName) {
	var arr = new Array();
	var checkbox = document.getElementsByName("id_array[]");
	for(var i = 0;i < checkbox.length; i++){
		if(checkbox[i].checked) {
			arr.push(checkbox[i].value);
		}
	}

	var id_array = arr.toString();

	if (valueTarget == "category_name") {
		if (id_array.length > 0) {
			requestUrl = requestUrl + "&category_name=" + id_array;
			$.getJSON(requestUrl, function(json){
				var view = new Array();
				for (var i=0; i < json.length; i++) {
					view[i] = json[i]["title"];
				}
				document.getElementsByName(viewTarget)[0].value = view.join(",");
			})
		} else {
			selectClear(formName, 'category_title', 'category_name');
		}
	} else {
		document.getElementsByName(viewTarget)[0].value = id_array;
	}
	document.getElementsByName(valueTarget)[0].value = id_array;
	// セットしたら閉める
	closeSelectPopup();
}

/**
 * 選んだ候補をセットします。
 * @param formName
 * @param viewTarget
 * @param valueTarget
 * @param viewValue
 * @param valueValue
 */
function setSelectValue(obj, formName, viewTarget, valueTarget, viewValue, valueValue) {

	// TODO 存在チェック
	var targetForm = document.getElementsByName(formName)[0];
	//alert("DEBUG:" + " " + viewTarget + " " + valueTarget + " " + viewValue + " " + valueValue);

	// 複数
//	if (targetForm.elements[viewTarget].value != "") {
//		targetForm.elements[viewTarget].value += "," + viewValue;
//	} else {
//		targetForm.elements[viewTarget].value = viewValue;
//	}
//	if (targetForm.elements[valueTarget].value != "") {
//		targetForm.elements[valueTarget].value += "," + valueValue;
//	} else {
//		targetForm.elements[valueTarget].value = valueValue;
//	}
	
	// 一つだけ
	targetForm.elements[viewTarget].value = viewValue;
	targetForm.elements[valueTarget].value = valueValue;
	
	
	obj.disabled = true;
	
	// セットしたら閉める
	closeSelectPopup();
}

/**
 * ポップアップを閉めます。
 * @return
 */
function closeSelectPopup() {

	var selectpopup = document.getElementById('selectpopup');
	selectpopup.className = "hiddendialog";

	var innerframe = document.getElementById('innerframe');
	innerframe.innerHTML = "";
	
	var selectdialogouterframe = document.getElementById('selectdialogouterframe');
	selectdialogouterframe.style.zIndex = "0";
	selectdialogouterframe.style.visibility = "hidden";
	selectdialogouterframe.style.position = "relative";
	selectdialogouterframe.style.marginTop = "40px";
	selectdialogouterframe.style.marginLeft = "auto";
	selectdialogouterframe.style.marginRight = "auto";
	
	$("body").unbind("click");
}

/**
 * 選択クリア。
 * @return
 */
function selectClear(formName,name1,name2) {
	var targetForm = document.getElementsByName(formName)[0];
	targetForm.elements[name1].value = "";
	if (name2 != "") {
		targetForm.elements[name2].value = "";
	}
}

/**
 * 選択クリア。
 * @return
 */
function in_array(val, arr) {
	for (key in arr) {
		if (arr[key] == val) {
			return true;
		}
	}
	return false;
}



// -->