/**
 * JavaScript
 *
 *
 * @date	2006/01/15
 * @author K
 *
 */

/**
 * Global Values
 */

/* Browser */
var isMSIE = (navigator.userAgent.indexOf('MSIE')>=0);
var isMac 	= (navigator.userAgent.indexOf('Mac')>= 0) ;
var isMacIE = (isMac)&&(navigator.appName=="Microsoft Internet Explorer");
var isOpera = (navigator.userAgent.indexOf('Opera')>=0);

// 子window管理用配列
var winlist = new Array() ;

/*
function get_response_text ( text ) {
    if ( isSafari ) {
        var esc = escape( text );
        if ( esc.indexOf("%u") < 0 && esc.indexOf("%") > -1 ) {
            text = decodeURIComponent( esc );
        }
    }
    return text;
}
*/
/**
 * Form element List Up
 *
 * @param form_id	form ID
 */
/*
function getFormParams(form_id) {

	var params = "";
	try {
		if (form_id != null && form_id != "") {
			var frm = document.getElementById(form_id);

			if (frm != null) {
				for (var i = 0; i < frm.elements.length; i++) {
					if (frm.elements[i].name != null && frm.elements[i].name != ""
						&& frm.elements[i].type != "button"
						&& frm.elements[i].type != "image"
						&& frm.elements[i].type != "submit") {

						params += getFormElement(frm.elements[i]);
					}
				}
				frm = null;
			}
		}
	}
	catch (e) {

	}

	return params;
}
*/

/**
 *
 * Element List Up
 *
 */
/*
function getFormElement(el) {
	var params = "";

	if (el.type == "checkbox" || el.type == "radio") {
		if (el.checked) {
			params += "&" + el.name + "=" + el.value;
		}
	}
	else if (el.type == "select") {
		params += "&" + el.name + "=" + el.options[el.selectedIndex].value;
	}
	else if (el.type == "file") {

	}
	else {
		if (el.name != "module" && el.name != "action") {
			params += "&" + el.name + "=" + encodeURIComponent(el.value);
		}
	}

	return params;
}

*/
// ===============================================================

var window_lock = 0;
/**
 * Window Open
 */
function open_window(url, target, width, height) {

	var param = "";
	param  =  "width=" + width;
	param += ",height=" + height;
	param += ",resizable=yes";
	param += ",scrollbars=yes";
	param += ",status=yes";
	param += ",location=no";
	param += ",menubar=no";
	param += ",toolbar=no";

	if (window_lock == 1) {
		// 早押し防止
		return;
	}
	window_lock = 1;

	// open
	try {
		if (target == null || target == "_blank") {
			window.open(url, target, param) ;
			window_lock = 0;
			return;
		}
	}
	catch (e) {
	}

	if (isOpera){
		// Operaの場合はとりあえず、エラーになったら、Windowを開く
		try {
			winlist[target].focus() ;
		}
		catch (e) {
			winlist[target] = window.open(url, target, param) ;
			winlist[target].focus();
		}

		window_lock = 0;
		return ;
	}


	var rtn = windows_is_closed(winlist[target]);
	if (rtn) {
		try {
			winlist[target] = window.open(url, target, param);
			winlist[target].focus();
		}
		catch (ex) {
		}
	}
	else {
		try {
			winlist[target].focus() ;
		}
		catch (e) {
			winlist[target] = window.open(url, target, param) ;
			winlist[target].focus();
		}
	}
	window_lock = 0;
}


/*
 * windowのopen状態チェック
 * windowが開いていなければ,trueが返る
 */
function windows_is_closed(obj) {
	return ((obj==null)||(typeof obj!='object')||(obj.closed) );
}

/**
 * Windowを閉じる
 *
 */
function close_windows() {

	for (field in winlist) {

		if (isOpera) {
			try {
				winlist[field].close() ;
			}
			catch (e) {

			}
			winlist[field] = null ;
			continue;
		}
		else {
			if (windows_is_closed(winlist[field])) {
				winlist[field] = null;
				continue ;
			}
			winlist[field].close() ;
			winlist[field] = null ;
		}
	}
	winlist = null;
	winlist = new Array() ;
}


/**
 * 操作の確認
 */
function submit_confirm(type) {

	var msg = "";

	if (type == null || type == "undefined") {
		return true;
	}
	else if (type == "update") {
		msg = "更新してもよろしいですか？";
	}
	else if (type == "delete") {
		msg = "削除してもよろしいですか？";
	}
	else if (type == "close") {
		msg = "ウィンドウを閉じてもよろしいですか？";
	}
	else if (type == "logout") {
		msg = "ログアウトしてもよろしいですか？";
	}
	else if (type == "cancel") {
		msg = "キャンセルしてもよろしいですか？";
	}
	else if (type == "submit") {
		msg = "決定してもよろしいですか？";
	}
    else if (type == "add") {
        msg = "追加してもよろしいですか？";
    }
	else if (type == "mail") {
		msg = "メールを送信してもよろしいですか？";
	}
	else if (type == "settle") {
		msg = "入金済みにしてもよろしいですか？";
	}
	else if (type == "reset") {
		msg = "取り消してもよろしいですか？";
	}
	else {
		msg = "処理をしてもよろしいですか？";
	}

	if  (!window.confirm("【確認】\n" + msg)) {
		return false;
	}

	return true;
}

// ------------------------------------------------------------
/**
 * フォーム送信
 * 動的な要素の値を指定
 */
function submit_form(url, target_name, form_id, params) {

    // 処理分岐ためのフラグ
    if (params != null) {
        try {
            for (key in params) {
                var el = top.document.getElementById(key);
                if (el != null) {
                    el.value = params[key];
                }
            }
        }
        catch (e) {

        }
    }

    try {
        var frm = document.getElementById(form_id);
        frm.method = "post";
        // IE でactionを指定するとエラー
//      frm.action = param;
        frm.target = target_name;
        frm.submit();
   }
    catch (e) {
//      alert("失敗");
    }
}

/**
 * 送信(FormのPOST)
 */
function submit_form2(url, target, form_id, type, params) {

    if (submit_confirm(type) == true) {
        submit_form(url, target, form_id, params);
    }
}

/**
 * ウィンドウを開いてsubmit
 */
function submit_form3(url, target, form_id, type, width, height, params) {

    if (submit_confirm(type) == true) {

        open_window("/dms/admin/blank.html", target, width, height);

        submit_form(url, target, form_id, params);
    }
}

// ===========================================================

/**
 *
 */
function close_window() {
	if (submit_confirm("close")) {
		window.close();
	}
}

/* ========================================================== */
/**
 * データを編集する
 *
 */
function edit_form_data(params) {

    try {
        if (params != null) {
            for (key in params) {
                var el = top.document.getElementById(key);
                if (el != null) {
                    el.value = params[key];
                }
            }
        }
    }
    catch (e) {

    }
}
function disable_form(key, value, reverse) {

    try {
        var el = top.document.getElementById(key);

        if (reverse) {
            el.disabled = !(el.disabled);
        }
        else {
            el.disabled = value;
        }
    }
    catch (e) {

    }
}


function popup_thumbnail(){
	win = new Window({className: "mac_os_x", title: "Sample", width:200, height:150, destroyOnClose: true, recenterAuto:false});
	win.getContent().update("<h1>Hello world !!</h1>");
	win.showCenter(); 
}
/**
 * 画像ポップアップテスト
 * @return
 */
function win1()
{
	//open_window('www.google.co.jp','_blank',100,100);
	//return;
	
  var win = new Window({className: "alphacube", title: "Sample", width:250, height:150, top:0, left: 1, parent:$('container')}); 
  win.getContent().innerHTML = "<h1>Constraint inside  a div !!</h1>constraint: {left:10, right:20}"; 

  win.setDestroyOnClose(); 
  win.show();
  win.setConstraint(true, {left:10, right:20})
  win.toFront();
}

