/**
 * JavaScript
 */

/**
 * 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);
var isSafari = (navigator.userAgent.indexOf('Safari') >= 0);
var isFirefox = (navigator.userAgent.indexOf('Firefox') >= 0);
var isiPad = (navigator.platform == "iPad");

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

// ベースURL
var lw_base_url = "/";

if (typeof console == "undefined") {
	// wrapper
	console = new Object();
	console.log = function() {}
}

var lw_log = function(msg) {
	console.log(msg);
}


// ===============================================================
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, "");
}

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

var window_lock = 0;

function  open_window(url, target, width, height) {
	return openWindow(url, target, width, height);
}

/**
 * Window Open
 */
function openWindow(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=yes";

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

	// IEでtargetに - ハイフン が含まれるとエラーになるので アンスコに置換
	if (target != null){
		target = target.replace("-", "_");
	}

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

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

		window_lock = 0;
		return ;
	}

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


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

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

	for (var field in windowList) {

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

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

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

	var msg = "";

	if (type == null || type == "undefined") {
		return true;
	}

	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 == "reset") {
		msg = "取り消してもよろしいですか？";
	}
	else {
		msg = type;
	}

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

	return true;
}

// ------------------------------------------------------------
/**
 * フォーム送信
 * 動的な要素の値を指定
 * urlはIEとの互換性のため、かざりです。正しくはFormのactionにてurlを指定してください。
 */
function submitForm(url, target_name, form_id, params, method) {

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

        }
    }

    try {
		if (form_id.charAt(0) == '#') {
			form_id = form_id.substr(1);
		}

        var frm = document.getElementById(form_id);

		if (method) {
	        frm.method = method;
		}
		else {
	        frm.method = "post";
		}
        // IE でactionを指定するとエラー
        if (!isMSIE) {
            frm.action = url;
        }
        frm.target = target_name;

        frm.submit();
    }
    catch (e) {
//      alert("失敗");
    }
}

/**
 * 送信(FormのPOST)
 * urlはIEとの互換性のため、かざりです。正しくはFormのactionにてurlを指定してください。
 */
function confirmForm(url, target, form_id, type, params) {

    if (submitConfirm(type) == true) {
        submitForm(url, target, form_id, params);
    }
}

/**
 * ウィンドウを開いてsubmit
 * urlはIEとの互換性のため、かざりです。正しくはFormのactionにてurlを指定してください。
 */
function submitNewWindow(url, target, form_id, type, width, height, params) {

    if (submitConfirm(type) == true) {
        openWindow('about:blank', target, width, height);
        submitForm(url, target, form_id, params);
    }
}

/**
 * iframeの中のformをsubmit;
 *
 * @param frame_id
 * @param form_id
 */
function submitIframe(frame_id, form_id) {

	$('#' + frame_id + ':first').contents().find('#' + form_id).submit();
}

 /**
  * iframeの中の要素に値を設定
  *
  * @param src_id
  * @param dest_id
  * @return
  */
 function setIframeElValue(frame_id, src_id, dest_id) {

	 $('#' + frame_id + ':first').contents().find('#'+dest_id).val( $('#'+src_id).val() );
 }

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

/**
 *
 */
function closeWindow() {
	if (submitConfirm("close")) {
		window.close();
	}
}


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

var LwForm = {
	/**
	 * データを編集する
	 *
	 * @param params
	 */
	setValues: function (params) {

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

		}
	},
	/**
	 * フォームを無効にする。
	 */
	disable: function (key, value, reverse) {

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

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

		}
	},
	getParams: function (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 += LwForm.getElement(frm.elements[i]);
						}
					}
					frm = null;
				}
			}
		}
		catch (e) {
		}

		return params;
	},
	getElement: function (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;
	},

	/**
	 * 透過。
	 * @param {Object} el
	 * @param {Object} value
	 */
	 setOpacity: function (el, value) {
		try {
			el.style.filter="alpha(opacity:" + value + ")";
			el.style.KHTMLOpacity= value;
			el.style.MozOpacity= value;
			el.style.opacity= value;
		}
		catch (ex) {}
	}

};

/**
 * フォーム送信
 * 動的な要素の値を指定
 */
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);
        
        return true;
    }
    else {
    	return false;
    }
}

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

    if (submit_confirm(type) == true) {

        open_window(url, target, width, height);

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

/**
 * 操作の確認
 */
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;
}

