
var isMSIE = /*@cc_on!@*/false;						// IE判定

/*--------------------------------------------------------------------
/ 共通系
/-------------------------------------------------------------------*/

var tempParam;
var tempValidateparam;
var tempCallDomPhpPath;
var tempCallBackFunction;

/***********************************************************
* dom_runAsynchronous
*****************************************[FutureSoftware]**/
function dom_runAsynchronous(callDomPhpPath,callBackFunction,param)
{
	if (param == null)
	{
		tempParam				= null;
	}
	else
	{
		tempParam				= param;
	}

	var makeParam = "";

	if (param == null)
	{
		makeParam  = autoGetParam();
	}
	else
	{
		makeParam  = param;
	}
	
	var ajaxrequest = new Ajax.Request('./dom/' + callDomPhpPath,
									{
										method:"post",
										parameters:makeParam,
										onComplete:callBackFunction,
										asynchronous:true
									});
}

/***********************************************************
* dom_getResult
*****************************************[FutureSoftware]**/
function dom_getResult(callDomPhpPath,callBackFunction,param,validateparam)
{

	tempCallDomPhpPath		= callDomPhpPath;
	tempCallBackFunction	= callBackFunction;

	if (param == null)
	{
		tempParam				= null;
	}
	else
	{
		tempParam				= param;
	}

	if (validateparam == null)
	{
		tempValidateparam		= null;
	}
	else
	{
		tempValidateparam		= validateparam;
	}

	var makeParam = "";

	if (param == null && validateparam == null)
	{
		makeParam  = autoGetParam();
	}
	else
	{
		makeParam  = param;
		makeParam += validateparam;
	}

	validateSubmitParam(makeParam,dom_getResult_main);
}


/***********************************************************
* dom_getResult_main
*****************************************[FutureSoftware]**/
function dom_getResult_main()
{
	var makeParam = "";

	if (tempParam == null)
	{
		makeParam = autoGetParam();
	}
	else
	{
		makeParam = tempParam;
	}

	var ajaxrequest = new Ajax.Request('./dom/' + tempCallDomPhpPath,
									{
										method:"post",
										parameters:makeParam,
										onComplete:dom_getResult_result,
										asynchronous:false
									});
}

/***********************************************************
* dom_getResult_result
*****************************************[FutureSoftware]**/
function dom_getResult_result(request)
{
	var resultXmlObj = request.responseXML;
	// alert(request.responseText);

	var proc_resultObj	= resultXmlObj.getElementsByTagName("proc_result").item(0);

	var proc_result		= getTextContent(proc_resultObj.getElementsByTagName("result"));
	var proc_message	= getTextContent(proc_resultObj.getElementsByTagName("message"));

	if (proc_result == "false")
	{
		// DOM処理でエラーが発生した場合
		// myAlert(proc_message.replace(/\n/g,"<br>"));
		alert(proc_message);
	}
	else
	{
		tempCallBackFunction(request);
	}
}

/***********************************************************
* autoGetParam
*****************************************[FutureSoftware]**/
function autoGetParam()
{
	// フォーム内の値でPOSTパラメータを自動生成する(prototype.jsを使用)
	var param = Form.serialize("inputform");

	// フォーム内の値の型一覧POSTパラメータを自動生成する
	var type_param = makeInputTypeParameter("inputform");

	// パラメータの連結処理
	if (param.length != 0)
	{
		type_param = "&" + type_param;
	}
	param = param + type_param;

	return param;
}

/***********************************************************
* makeInputTypeParameter
* ----------------------------------------------------------
* 指定したIDを持つForm内の入力項目の全ての型をパラメータとし
* て生成します。
*
* [引数]
*  対象FormのID
* 
* [戻り値]
*  パラメータ
* 
*****************************************[FutureSoftware]**/
function makeInputTypeParameter(formid)
{
	// 戻り値を格納する領域を定義
	var ret = "";

	// id属性からformを取得する
	var formobj =  document.getElementById(formid);

	// formが取得できたかチェックする
	if (formobj == undefined )
	{
		// formが取得できなかった場合は現状の戻り値(つまり空)を返す
		return ret;
	}

	// 同一の名称リストを返却しないように、それを格納しておく連想配列を作成
	var ass_names = new Object();

	// formが取得できた場合、その中の要素を抽出する
	for (var cnt = 0 ; cnt < formobj.length ; cnt++ )
	{
		// 最初に要素の型と名前を取得
		var type = formobj[cnt].type;
		var name = formobj[cnt].name;

		// 処理対象で絞る
		if (type == "text" || type == "password" || type == "checkbox" || type == "hidden"|| type == "radio" || type == "select-one" || type == "select-multiple" || type == "textarea")
		{
			// 既に存在している名称かどうかの判断
			if (ass_names[name] == undefined)
			{
				// 存在していない名称の場合は配列に登録
				ass_names[name] = name;

				// 名前の生成(名前のルールは元々の名前+"__type"である
				name = name.replace("[]","");
				name = name + "__type";

				// 既になんらかの戻り値が設定されているかをチェック
				if (ret.length > 0 )
				{
					// 結合子を付与
					ret += "&";
				}

				// 名前=値の形でパラメータを生成していく
				ret += name + "=" + type;
			}
		}
	}

	// 作成したパラメータの返却
	return ret;
}

/***********************************************************
* validateSubmit
* ----------------------------------------------------------
* モーダルウインドウオープン時のコールバック関数です。
* openDivWindow関数からコールされるので、この関数は直接呼び
* 出さないでください。
* 
* [引数]
*  なし
* 
* [戻り値]
*  なし
* 
*****************************************[FutureSoftware]**/
function validateSubmit(formid,callbackFunction)
{
	// グローバル変数に引数を保存しておく
	g_Formid = formid;
	g_CallbackFunction = callbackFunction;

	// フォーム内の値でPOSTパラメータを自動生成する(prototype.jsを使用)
	var param = Form.serialize(formid);

	// フォーム内の値の型一覧POSTパラメータを自動生成する
	var type_param = makeInputTypeParameter(formid);

	// パラメータの連結処理
	if (param.length != 0)
	{
		type_param = "&" + type_param;
	}
	param = param + type_param;

	// 作成したパラメータでエラーチェック処理PHPをコールする
	var validate = new Ajax.Request(
				'./futuresoftware_validate.php',
				{
					method:"post",
					parameters:param,
					onComplete:validateSubmitProc,
					asynchronous:false
				});
}

/***********************************************************
* validateSubmitParam
* ----------------------------------------------------------
* 入力値チェックサブミットです。
* 独自でパラメータを作った場合はこれをコールします。
* 
* [引数]
*  なし
* 
* [戻り値]
*  なし
* 
*****************************************[FutureSoftware]**/
function validateSubmitParam(param,callbackFunction)
{
	// グローバル変数に引数を保存しておく
	g_Formid = null;
	g_CallbackFunction = callbackFunction;

	// 作成したパラメータでエラーチェック処理PHPをコールする
	var validate = new Ajax.Request(
				'./futuresoftware_validate.php',
				{
					method:"post",
					parameters:param,
					onComplete:validateSubmitProc,
					asynchronous:false
				});
}

/***********************************************************
* validateSubmitProc(内部関数)
* ----------------------------------------------------------
* モーダルウインドウオープン時のコールバック関数です。
* openDivWindow関数からコールされるので、この関数は直接呼び
* 出さないでください。
* 
* [引数]
*  なし
* 
* [戻り値]
*  なし
* 
*****************************************[FutureSoftware]**/
function validateSubmitProc(request)
{
	// 結果XMLを取得する
	var resultXmlObj = request.responseXML;

	// 変数生成
	var allResult = true;
	var message = "";

	for (var cnt = 0 ; cnt < resultXmlObj.getElementsByTagName("item").length ; cnt++ )
	{
		var itemObj = resultXmlObj.getElementsByTagName("item").item(cnt);

		var name_value			= getTextContent(itemObj.getElementsByTagName("name"));
		var value_value			= getTextContent(itemObj.getElementsByTagName("value"));
		var validateresult_value	= getTextContent(itemObj.getElementsByTagName("validateresult"));
		var message_value		= getTextContent(itemObj.getElementsByTagName("message"));
		var type_value			= getTextContent(itemObj.getElementsByTagName("type"));
		var func_value			= getTextContent(itemObj.getElementsByTagName("func"));

		if (validateresult_value == "false")
		{
			// 入力値チェックでエラーの場合
			allResult = false;
			message += message_value + "\r\n";
		}
		else
		{
			// 正常の場合
		}
	}

	// 最後の実行
	if (allResult)
	{
		if (g_CallbackFunction != null ){g_CallbackFunction();}
		else{submit();}
	}
	else
	{
		// myAlert(message.replace(/\n/g,"<br>"));
		alert(message);
	}
}

/***********************************************************
* getWidth
* ----------------------------------------------------------
* 現在のウインドウの横幅を求めます。
* 
* [引数]
*  なし
* 
* [戻り値]
*  ウインドウの横幅
* 
*****************************************[FutureSoftware]**/
function getWidth() 
{
     var ua = navigator.userAgent;							// ユーザーエージェント
     var nWidth, nHeight;									// サイズ
     var nHit = ua.indexOf("MSIE");							// 合致した部分の先頭文字の添え字
     var bIE = (nHit >=  0);								// IE かどうか
     var bVer6 = (bIE && ua.substr(nHit+5, 1) == "6");		// バージョンが 6 かどうか

     var bStd = (document.compatMode && document.compatMode=="CSS1Compat");

     // 標準モードかどうか
     if (bIE) {
          if (bVer6 && bStd) {
               nWidth = document.documentElement.clientWidth;
               nHeight = document.documentElement.clientHeight;
          } else {
               nWidth = document.body.clientWidth;
               nHeight = document.body.clientHeight;
          }
     } else {
          nWidth = window.innerWidth;
          nHeight = window.innerHeight;
     }

     return nWidth;
}

/***********************************************************
* getHeight
* ----------------------------------------------------------
* 現在のウインドウの高さを求めます。
* 
* [引数]
*  なし
* 
* [戻り値]
*  ウインドウの高さ
* 
*****************************************[FutureSoftware]**/
function getHeight() 
{
     var ua = navigator.userAgent;							// ユーザーエージェント
     var nWidth, nHeight;									// サイズ
     var nHit = ua.indexOf("MSIE");							// 合致した部分の先頭文字の添え字
     var bIE = (nHit >=  0);								// IE かどうか
     var bVer6 = (bIE && ua.substr(nHit+5, 1) == "6");		// バージョンが 6 かどうか

     var bStd = (document.compatMode && document.compatMode=="CSS1Compat");

     // 標準モードかどうか
     if (bIE) {
          if (bVer6 && bStd) {
               nWidth = document.documentElement.clientWidth;
               nHeight = document.documentElement.clientHeight;
          } else {
               nWidth = document.body.clientWidth;
               nHeight = document.body.clientHeight;
          }
     } else {
          nWidth = window.innerWidth;
          nHeight = window.innerHeight;
     }

     return nHeight;
}

/***********************************************************
* getDivWidth
* ----------------------------------------------------------
* DIVオブジェクトの幅を求めます。
* 
* [引数]
*  DIVオブジェクト
* 
* [戻り値]
*  DIVオブジェクトの高さ
* 
*****************************************[FutureSoftware]**/
function getDivWidth (div)
{
  return document.layers?
         div.clip.width:(div.offsetWidth||div.style.pixelWidth||0);
}

/***********************************************************
* getDivHeight
* ----------------------------------------------------------
* DIVオブジェクトの高さを求めます。
* 
* [引数]
*  DIVオブジェクト
* 
* [戻り値]
*  DIVオブジェクトの高さ
* 
*****************************************[FutureSoftware]**/
function getDivHeight(div)
{
  return document.layers?
         div.clip.height:(div.offsetHeight||div.style.pixelHeight||0);
}

/***********************************************************
* getScrollX
* ----------------------------------------------------------
* X軸のスクロール量を求めます
* 
* [引数]
*  なし
* 
* [戻り値]
*  ウインドウの高さ
* 
*****************************************[FutureSoftware]**/
function getScrollX()
{ 
	var scrollX = document.documentElement.scrollLeft || document.body.scrollLeft; 

	return scroll;
} 

/***********************************************************
* getScrollY
* ----------------------------------------------------------
* Y軸のスクロール量を求めます
* 
* [引数]
*  なし
* 
* [戻り値]
*  ウインドウの高さ
* 
*****************************************[FutureSoftware]**/
function getScrollY()
{
	var scrollY = document.documentElement.scrollTop || document.body.scrollTop; 

	return scrollY;
}

function replaceReturnToBR(str)
{
	str		= replaceAll(str,"\r\n","\n");
	str		= replaceAll(str,"\r","\n");
	str		= replaceAll(str,"\n","<br>");

	return str;
}

function replaceSpaceToNBSP(str)
{
	str		= replaceAll(str," ","&nbsp;");

	return str;
}


function replaceAll(expression, org, dest)
{
    return expression.split(org).join(dest);
}

/*------------------------------------------------------------------------------
* 動的HTML生成用共通関数
* ------------------------------------------------------------------------------
* 動的にHTMLオブジェクトを生成する際に使用する共通関数を定義する
* 
* 
-------------------------------------------------------------[FutureSoftware]-*/
/***********************************************************
* setStyle
* ----------------------------------------------------------
* 
* 
* 
* 
* 
*
* [引数]
*  obj:
*  style:
* 
* [戻り値]
*  なし
* 
*****************************************[FutureSoftware]**/
function setStyle(obj,style)
{
		if (isMSIE)
		{
			obj.style.cssText = style;
		}
		else
		{
			obj.setAttribute("style",style);
		}
}

/***********************************************************
* setJavascriptFunction
* ----------------------------------------------------------
* 
* 
* 
* 
* 
*
* [引数]
*  obj:
*  ontype:
*  func:
* 
* [戻り値]
*  なし
* 
*****************************************[FutureSoftware]**/
function setJavascriptFunction(obj,ontype,func)
{
		if (isMSIE)
		{
			obj.setAttribute(ontype,new Function(func));
		}
		else
		{
			obj.setAttribute(ontype,func);
		}
}

function removeJavascriptFunction(obj,ontype)
{
		if (isMSIE)
		{
			obj.setAttribute(ontype,new Function(""));
		}
		else
		{
			obj.removeAttribute(ontype);
		}
}

/*------------------------------------------------------------------------------
* XML関連
* ------------------------------------------------------------------------------
* 
* 
* 
-------------------------------------------------------------[FutureSoftware]-*/
/***********************************************************
* getTextContent
* ----------------------------------------------------------
* XML内のタグに囲まれた値を取得します。
* 
* [引数]
*  タグ名
* 
* [戻り値]
*  タグに囲まれた値
* 
*****************************************[FutureSoftware]**/
function getTextContent(node)
{
	if(window.ActiveXObject)
	{
		try
		{
			//MSXML2以降用
			return node.item(0).firstChild.nodeValue;
		}
		catch (e)
		{
			try
			{
				//旧MSXML用
				return node.item(0).firstChild.nodeValue;
			}
			catch (e2)
			{
				return "";
			}
		}
	}
	else if(window.XMLHttpRequest)
	{
		//Win ie以外のXMLHttpRequestオブジェクト実装ブラウザ用
		return node.item(0).textContent;
	}
	else
	{
		return "";
	}

	return (node.firstChild.textContent ? node.item(0).textContent : node.firstChild.nodeValue);
}

/***********************************************************
* getExp
* ----------------------------------------------------------
* 引数で渡したファイルの拡張子を返す
*
* [引数]
*  画像ソースファイルパス
* 
* [戻り値]
*  拡張子
* 
*****************************************[FutureSoftware]**/
function getExp(src)
{
	// 最後のピリオドを検索する
	var lastindex = src.lastIndexOf(".");

	// もしピリオドが見つからなかったら・・・
	if (lastindex == -1){return "";}

	return(src.substring(lastindex + 1 ,src.length));
}

/***********************************************************
* getDir
* ----------------------------------------------------------
* 引数で渡したファイルが格納されているディレクトリを返す
*
* [引数]
*  画像ソースファイルパス
* 
* [戻り値]
*  ディレクトリパス
* 
*****************************************[FutureSoftware]**/
function getDir(src)
{
	var lastindex_yen = src.lastIndexOf("\"");
	var lastindex_slu = src.lastIndexOf("/");

	if (lastindex_yen == -1 && lastindex_slu == -1){return "";}

	var lastindex = lastindex_yen;
	if (lastindex < lastindex_slu)
	{
		lastindex = lastindex_slu;
	}

	return(src.substring(0,lastindex + 1));
}

/***********************************************************
* onButtonAuto
* ----------------------------------------------------------
* マウスオーバーで画像を変化させたいところでこれをコールする
*
* [引数]
*  画像のオブジェクト
* 
* [戻り値]
*  なし
* 
*****************************************[FutureSoftware]**/
function onButtonAuto(obj)
{
	// まずは拡張子を取得しておく
	var exp = getExp(obj.src);
	var dir = getDir(obj.src);
	var mode = true;

	if (obj.src.indexOf("_off." + exp) == -1){mode = true;}
	if (obj.src.indexOf("_on." + exp) == -1) {mode = false;}

	// 次にidからファイル名のベースとなる名称を取得する
	var filenamebase = "";
	if (mode)
	{
		filenamebase = obj.src.substring(obj.src.lastIndexOf("/",obj.src.length) + 1).replace("_on."+exp,"");
	}
	else
	{
		filenamebase = obj.src.substring(obj.src.lastIndexOf("/",obj.src.length) + 1).replace("_off."+exp,"");
	}

	// ファイル名のベースと拡張子から予想できるファイル名を生成する
	var imgfilename = "";
	if (mode)
	{
		imgfilename = dir + filenamebase + "_off" + "." + exp;
	}
	else
	{
		imgfilename = dir + filenamebase + "_on" + "." + exp;
	}

	obj.src = imgfilename;

	if (mode)
	{
		document.body.style.cursor = "default";
	}
	else
	{
		document.body.style.cursor = "pointer";
	}
}

function imageFitting(imgObj,width,height,fix_width,fix_height)
{
	var fix_rate		= fix_width / fix_height;
	var rate			= width     / height;

	if (fix_rate >= rate)
	{
		imgObj.width	= width * fix_height / height;
		imgObj.height	= fix_height;
	}
	else
	{
		imgObj.width	= fix_width;
		imgObj.height	= height * fix_width / width;
	}
}

function getMyApp(appName) {
    if (navigator.appName.indexOf ("Microsoft") !=-1) {
        return window[appName];
    } else {
        return document[appName];
    }
}

function isBackgroundColor(obj,targetcolor)
{
	// IEか否かを判別する
	if(isMSIE)
	{
		// IEの場合
		if (obj.style.background == targetcolor)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		// FIrefox Safariの場合

		// まずは、styleに定義された色情報を小文字変換しながら取得する
		var background = obj.style.background.toLowerCase();
		var color = obj.style.color.toLowerCase();

		// その結果から色情報の位置を取得する
		var background_idx = background.indexOf(FIREFOX_COLOR_INFO);
		var color_idx = color.indexOf(FIREFOX_COLOR_INFO);

		// 取得できなかった場合はエラーだが、選択されていないとして処理する
		if(background_idx == -1 || color_idx == -1){return false;}

		// 色情報の出現以降に出てくる括弧閉じの位置を検索し、その中に挟まれた情報を色情報として取得する
		var background_rgb = background.substring(background_idx + FIREFOX_COLOR_INFO.length,background.indexOf(")",background_idx));
		var color_rgb = color.substring(color_idx + FIREFOX_COLOR_INFO.length,color.indexOf(")",color_idx));

		// 色情報はカンマで区切られているので、分断する
		var background_rgb_split = background_rgb.split(",");
		var color_rgb_split		 = color_rgb.split(",");

		// 分断した結果を16進数に変換し色情報として取得する
		var background_color	= "#"
								 + parseInt(background_rgb_split[0]).toString(16)
								 + parseInt(background_rgb_split[1]).toString(16)
								 + parseInt(background_rgb_split[2]).toString(16);
		var color_color			= "#"
								 + parseInt(color_rgb_split[0]).toString(16)
								 + parseInt(color_rgb_split[1]).toString(16)
								 + parseInt(color_rgb_split[2]).toString(16);

		// 色情報の比較により、選択値か否かをチェックする
		if (background_color == targetcolor)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}

