//==========================================================
// 文字列が数値表現か調べる
//----------------------------------------------------------
// 引数
//	strValue:	検査する文字列
//----------------------------------------------------------
// 戻り値
//	文字列が数値表現である場合は true を返します。それ以外の場合は false 
//	を返します。
//==========================================================
function IsNumeric(strValue)
{
	var strValidChars = "0123456789.-";
	var strChar;
	var bResult = true;
	if(strValue.length == 0) return false;
	for(var i = 0; i < strValue.length && bResult == true; i++){
		strChar = strValue.charAt(i);
		if(strValidChars.indexOf(strChar) == -1){
			bResult = false;
     	}
	}
	return bResult;
}
//==========================================================
// HTML要素を取得
//----------------------------------------------------------
// 引数
//	strId:	タグ ID
//----------------------------------------------------------
// 戻り値
//	HTML要素が見つかった場合はその要素を返します。
//	見つからない場合は undefined を返します。
//==========================================================
function GetDom(strId)
{
	var dom = null;
	try{
		dom = document.getElementById(strId);
		if((dom != undefined) && (dom != null)){
			return dom;
		}
	}catch(e){
		return undefined;
	}
	return undefined;
}
//==========================================================
// HTML要素の値を取得
//----------------------------------------------------------
// 引数
//	strId:				タグ ID
//	strDefaultValue:	デフォルト値
//----------------------------------------------------------
// 戻り値
//	HTML要素が見つかった場合はその値を返します。値が null である場合は
//	デフォルト値を返します。
//==========================================================
function GetDomValue(strId, strDefaultValue)
{
	var strValue = null; 
	try{
		var dom = document.getElementById(strId);
		if((dom != undefined) && (dom != null)){
			strValue = dom.value;		
		}
		if(strValue == null){
			strValue = strDefaultValue;
		}
		if(strValue == ""){
			strValue = strDefaultValue;
		}
	}catch(e){
		alert(e);
	}
	return strValue;
}
//==========================================================
// HTML要素の値を設定
//----------------------------------------------------------
// 引数
//	strId:				タグ ID
//	strValue:			設定値
//	strErrorMessage:	エラーメッセージ格納バッファ
//----------------------------------------------------------
// 戻り値
//	処理が成功した場合は true を返します。それ以外の場合は false を返します。
//==========================================================
function SetDomValue(strId, strValue, strErrorMessage)
{
	try{
		var dom = document.getElementById(strId);
		if((dom != undefined) && (dom != null)){
			dom.value = strValue;			
		}
	}catch(e){
		strErrorMessage = e;
		return false;			
	}
	return true;
}
//==========================================================
// HTML要素の innerHTMLを取得
//----------------------------------------------------------
// 引数
//	strTagId:		タグ ID
//	strDefaultHtml:	デフォルト値
//----------------------------------------------------------
// 戻り値
//	HTML要素が見つかった場合はその値を返します。値が null である場合は
//	デフォルト値を返します。
//==========================================================
function GetDomInnerHtml(strId, strDefaultHtml)
{
	var strInnerHtml = null; 
	try{
		var dom = document.getElementById(strId);
		if((dom != undefined) && (dom != null)){
			strInnerHtml = dom.innerHTML;		
		}
		if(strInnerHtml == null){
			strInnerHtml = strDefaultHtml;
		}
		if(strInnerHtml == ""){
			strInnerHtlm = strDefaultHtml;
		}
	}catch(e){
		alert(e);		
	}
	return strInnerHtml;
}
//==========================================================
// HTML要素の innerHTMLを設定
//----------------------------------------------------------
// 引数
//	strTagId:			タグ ID
//	strInnerHtml:		HTMLコード
//	strErrorMessage:	エラーメッセージ格納バッファ
//----------------------------------------------------------
// 戻り値
//	処理が成功した場合は true を返します。それ以外の場合は false を返します。
//==========================================================
function SetDomInnerHtml(strId, strInnerHtml, strErrorMessage)
{
	try{
		var dom = document.getElementById(strId);						// HTML 要素を取得
		if((dom != undefined) && (dom != null)){						// 要素が有効なら･･･
			var strElementType = dom.nodeName;							// 要素の種類を取得
			var bCapsule = false;										// カプセル化フラグを初期化
			if(strElementType == "TABLE"){ 								// 要素がテーブルなら･･･
				bCapsule = true;										// カプセル化フラグをオン
				strElementType = "div";									// 要素の種類を変更
				strInnerHtml = "<table>" + strInnerHtml + "</table>";	// HTML コードをカプセル化
			}
			if(strElementType == "TFOOT"){ 								// 要素が TFOOT なら･･･
				bCapsule = true;										// カプセル化フラグをオン
				strElementType = "div";									// 要素の種類を変更
				strInnerHtml = "<tfoot>" + strInnerHtml + "</tfoot>";	// HTML コードをカプセル化
			}
			if(strElementType == "THEAD"){ 								// 要素が THEAD なら･･･
				bCapsule = true;										// カプセル化フラグをオン
				strElementType = "div";									// 要素の種類を変更
				strInnerHtml = "<thead>" + strInnerHtml + "</thead>";	// HTML コードをカプセル化
			}
			var domTmp = document.createElement(strElementType);		// テンポラリ要素を構成
			domTmp.innerHTML = strInnerHtml;							// テンポラリ要素に innerHTML を格納
			var aryNewChildren = null;									// テンポラリ要素の子ノードを取得
			if(bCapsule){												// カプセル化されていたら･･･
				aryNewChildren = domTmp.firstChild.childNodes;			// テンポラリ要素の孫ノードを取得
			}else{														// カプセル化されていなければ･･･
				aryNewChildren = domTmp.childNodes;						// テンポラリ要素の子ノードを取得
			}
			var aryOldChildren = dom.childNodes;						// 現在の要素の子ノードを取得
			for(var i = 0; i < aryOldChildren.length; i++){				// ▽ループ開始
				dom.removeChild(aryOldChildren.item(i));				// 現在の要素の子ノードを削除
			}															// △ループ終了
			for(var i = 0; i < aryOldChildren.length; i++){				// ▽ループ開始
				dom.removeChild(aryOldChildren.item(i));				// 現在の要素の子ノードを削除
			}															// △ループ終了
			for(var i = 0; i < aryNewChildren.length; i++){				// ▽ループ開始
				dom.appendChild(aryNewChildren.item(i));				// 現在の要素に子ノードを追加
			}															// △ループ終了
//			dom.innerHTML = strInnerHtml;
		}
	}catch(e){															// 例外を捕捉･･･
		alert(e);														// エラーメッセージを表示
		strErrorMessage = e;											// エラーメッセージを構成
		return false;													// 処理中断
	}
	return true;														// 返却値
}
//==========================================================
// 新規ウィンドウに指定ロケーションを展開
//----------------------------------------------------------
// 引数
//	strForm:			カレントドキュメントのフォーム名
//	strAction:			送信先 URL
//	strResultWindow:	送信結果出力ウィンドウ
//	cxWin:				送信結果出力ウィンドウの幅
//	cyWin:				送信結果出力ウィンドウの高さ
//==========================================================
function SendFormToWindow(strForm, strAction, strResultWindow, cxWin, cyWin)
{
	var strOption = "";	// ウィンドウオプション
	if(cxWin != undefined && cxWin != ""){
		strOption += 'width=' + cxWin;
		if(cyWin != undefined && cyWin != ""){
			strOption += ',height=' + cyWin;
		}
	}else{
		if(cyWin != undefined && cyWin != ""){
			strOption += 'height=' + cyWin;
		}
	}
	var winNew = window.open(null, strResultWindow, strOption);
	winNew.onLoad = SendForm(strForm, strAction, strResultWindow);
}
//==========================================================
// フォーム送信
//----------------------------------------------------------
// 引数
//	strForm:	フォーム名
//	strAction:	フォームアクション
//	strTarget:	ターゲットウィンドウ
//==========================================================
function SendForm(strForm, strAction, strTarget)
{
	try{													// 例外処理開始
		if(strAction != null){								// 送信先 URL が指定済みなら･･･
			document.forms[strForm].action = strAction;		// 送信先 URL を設定
		}
		if(strTarget != null){								// ターゲットウィンドウが指定済みなら･･･
			document.forms[strForm].target = strTarget;		// ターゲットウィンドウを設定
		}
		document.forms[strForm].submit();					// 送信
	}catch(e){												// 例外を捕捉･･･
		alert(e);											// エラーメッセージを表示
	}
}
//==========================================================
// ログアウト
//==========================================================
function Logout()
{
	document.frmLogout.action = "v2_logout.php";		// 送信先を指定
	document.frmLogout.submit();						// 送信
}
//==========================================================
// 商品リストを表示
//----------------------------------------------------------
// 引数
//	nShouhinType:	商品タイプ
//	strFilterType:	フィルタタイプ(category/maker/shop)
//	strFilterValue:	フィルタ値
//	nSortOrder:		ソート順(0:売値昇順/1:売値降順/2:新着順)
//==========================================================
function ShowShouhinList(nShouhinType, strFilterType, strFilterValue, nSortOrder)
{
	try{																				// 例外処理開始
		var strErrorMessage = "";														// エラーメッセージ格納バッファを初期化
		var nEnable = parseInt(GetDomValue("nEnable", "0"));							// 有効化フラグを取得
		if(nEnable == 0){																// 有効でないなら･･･
			return;																		// 処理中断
		}
		if((strFilterType != null) || (nShouhinType != null)){							// フィルタタイプ/商品タイプが指定済みなら･･･
			//----------------------------------------------------------
			// フォームパラメータを初期化
			//----------------------------------------------------------
			if(!SetDomValue("strCategory", "", strErrorMessage)){						// カテゴリを初期化･･･
				alert(strErrorMessage);													// エラーメッセージを表示
				return;																	// 処理中断
			}	
			if(!SetDomValue("strMakerMei", "", strErrorMessage)){						// メーカ名を初期化･･･
				alert(strErrorMessage);													// エラーメッセージを表示
				return;																	// 処理中断
			}	
			if(!SetDomValue("nShopId", "", strErrorMessage)){							// ショップ ID を初期化･･･
				alert(strErrorMessage);													// エラーメッセージを表示
				return;																	// 処理中断
			}	
			if(!SetDomValue("strKeyword", "", strErrorMessage)){						// キーワードを初期化･･･
				alert(strErrorMessage);													// エラーメッセージを表示
				return;																	// 処理中断
			}	
			if(!SetDomValue("strSubKeyword", "", strErrorMessage)){						// サブキーワードを初期化･･･
				alert(strErrorMessage);													// エラーメッセージを表示
				return;																	// 処理中断
			}	
			if(!SetDomValue("nShouhinType", "", strErrorMessage)){						// 商品タイプを初期化･･･
				alert(strErrorMessage);													// エラーメッセージを表示
				return;																	// 処理中断
			}	
			//----------------------------------------------------------
			// 商品タイプ
			//----------------------------------------------------------
			if(nShouhinType != null){													// 商品タイプが指定済みなら･･･
				if(!SetDomValue("nShouhinType", nShouhinType, strErrorMessage)){		// 商品タイプを設定･･･
					alert(strErrorMessage);												// エラーメッセージを表示
					return;																// 処理中断
				}
			}
			//----------------------------------------------------------
			// フォームパラメータを設定
			//----------------------------------------------------------
			switch(strFilterType){														// 条件分岐: フィルタタイプ
			case "strCategory": 														// フィルタタイプ: カテゴリ
				if(!SetDomValue("strCategory", strFilterValue, strErrorMessage)){		// カテゴリを設定･･･
					alert(strErrorMessage);												// エラーメッセージを表示
					return;																// 処理中断
				}	
				break;																	// 分岐中断		
			case "strMakerMei": 														// フィルタタイプ: メーカ名
				if(!SetDomValue("strMakerMei", strFilterValue, strErrorMessage)){		// メーカ名を設定･･･
					alert(strErrorMessage);												// エラーメッセージを表示
					return;																// 処理中断
				}	
				break;																	// 分岐中断		
			case "nShopId": 															// フィルタタイプ: ショップ ID
				if(!SetDomValue("nShopId", strFilterValue, strErrorMessage)){			// ショップ ID を設定･･･
					alert(strErrorMessage);												// エラーメッセージを表示
					return;																// 処理中断
				}	
				break;																	// 分岐中断		
			}
		}
		if(nSortOrder != null){															// ソート順が指定済みなら･･･
			if(!SetDomValue("nSortOrder", nSortOrder, strErrorMessage)){				// ソート順を設定･･･
				alert(strErrorMessage);													// エラーメッセージを表示
				return;																	// 処理中断
			}	
			var csvSortConditions = "";													// ソート条件を初期化
			switch(nSortOrder){															// 条件分岐: ソート順
			case 0:	csvSortConditions = "v2_Shouhin.nUrine ASC";	break;				// ソート順:売値昇順･･･
			case 1:	csvSortConditions = "v2_Shouhin.nUrine DESC";	break;				// ソート順:売値降順･･･
			case 2:	csvSortConditions = 
					"v2_Shouhin.nShouhinId DESC," + 
					"v2_ZaikoList.nZaikoListId ASC";	break;							// ソート順:新着順･･･
			}		
			if(!SetDomValue("csvSortConditions", csvSortConditions, strErrorMessage)){	// ソート条件を設定･･･
				alert(strErrorMessage);													// エラーメッセージを表示
				return;																	// 処理中断
			}	
		}
		document.frmSideMenu.submit();													// 送信
	}catch(e){																			// 例外を捕捉･･･
		alert(e);
	}
}
//==========================================================
// 商品詳細ページを表示
//----------------------------------------------------------
// 引数
//	strForm:	フォーム名
//==========================================================
function lnkShowShouhinShousai_OnClick(nShouhinId, nZaikoListId)
{
	try{																	// 例外処理開始
		document.frmShowShouhinShousai.nShouhinId.value = nShouhinId;		// 商品 ID
		document.frmShowShouhinShousai.nZaikoListId.value = nZaikoListId;	// 在庫リスト ID
		document.frmShowShouhinShousai.submit();							// 送信
		/*
		for(var i = 0; i < document.forms.length; i++){
			if(document.forms[i].name == strForm){
				document.forms[i].submit();	// 送信
			}
		}
		*/
//		document.forms[strForm].submit();	// 送信
	}catch(e){								// 例外を捕捉･･･
		alert(e);							// エラーメッセージを表示
	}
}
//==========================================================
// ページ移動
//----------------------------------------------------------
// 引数
//	strForm:	フォーム名
//	strAction:	アクション
//	nPageNo:	ページ番号
//==========================================================
function ChangePage(strForm, strAction, nPageNo){
	try{															// 例外処理開始
		var strErrorMessage = "";									// エラーメッセージ格納バッファを初期化
		if(!SetDomValue("nPageNo", nPageNo, strErrorMessage)){		// ページ番号を設定･･･
			alert(strErrorMessage);									// エラーメッセージを表示
			return;													// 処理中断			
		}
		SendForm(strForm, strAction, null);
//		document.frmHookup.action = "v2_registration_com_lis.php";	// 送信先 URL を設定
//		document.frmHookup.submit();								// 送信
	}catch(e){														// 例外を捕捉･･･
		alert(e);													// エラーメッセージを表示
	}
}
//==========================================================
// キーワード検索
//==========================================================
function SearchKeyword()
{
	try{																// 例外処理開始
		var strKeyword = document.frmKeyword.txtKeyword.value;			// キーワードを取得
		document.frmSideMenu.strKeyword.value = strKeyword;				// キーワードを設定
		document.frmSideMenu.strCategory.value = "";					// カテゴリを初期化
		document.frmSideMenu.strMakerMei.value = "";					// メーカ名を初期化
		document.frmSideMenu.nShopId.value = "";						// ショップ IDを初期化
		document.frmSideMenu.strSubKeyword.value = "";					// サブキーワードを初期化
		document.frmSideMenu.nPageNo.value = "1";						// ページ番号
		document.frmSideMenu.submit();									// 送信
	}catch(e){															// 例外を捕捉･･･
		alert(e);														// エラーメッセージを表示
	}
}
//==========================================================
// サブキーワード検索
//==========================================================
function SearchSubKeyword()
{
	try{																			// 例外処理開始
		var strSearchWaribikiRitsu = "";											// 抽出割引率を初期化
		if(document.frmSubKeyword.cmbSearchWaribikiRitsu != undefined){				// アウトレット商品一覧なら･･･
			strSearchWaribikiRitsu = 
				document.frmSubKeyword.cmbSearchWaribikiRitsu.value;				// 抽出割引率を取得
		}
		var strSubKeyword = document.frmSubKeyword.txtSubKeyword.value;				// サブキーワードを取得
		document.frmSideMenu.strSearchWaribikiRitsu.value = strSearchWaribikiRitsu;	// 抽出割引率を設定
		document.frmSideMenu.strSubKeyword.value = strSubKeyword;					// サブキーワードを設定
		document.frmSideMenu.nPageNo.value = "1";									// ページ番号
		document.frmSideMenu.submit();												// 送信
	}catch(e){																		// 例外を捕捉･･･
		alert(e);																	// エラーメッセージを表示
	}
}
//==========================================================
// メールアドレスの書式を確認
//----------------------------------------------------------
// 引数
//	strMailAddress:	メールアドレス
//----------------------------------------------------------
// 戻り値
//	メールアドレスの書式が正しい場合は true を返します、それ以外の場合は false
//	を返します。
//==========================================================
function IsValidMailAddress(strMailAddress)
{
	if(!strMailAddress.match(/^[A-Za-z0-9]+[\w-]+@[\w\.-]+\.\w{2,}$/)){
		return false;
	}	
	return true;
}
//==========================================================
// 半角英数のみの書式を確認
//----------------------------------------------------------
// 引数
//	strText:	検査するテキスト
//----------------------------------------------------------
// 戻り値
//	テキストが半角英数のみの場合は true を返します、それ以外の場合は false
//	を返します。
//==========================================================
function IsAlphNum(strText)
{
	if(!strText.match(/^[A-Za-z0-9]/)){
		return false;
	}	
	return true;
}

//**********************************************************
// イベントハンドラ
//**********************************************************

//==========================================================
// コンボボックスが選択されたときの処理
//----------------------------------------------------------
// 引数
//	csvZaikoListId:			在庫リスト ID
//	csvNittokouColorCode:	日本塗料工業会カラーコード
//	csvHtmlColorCode:		HTML カラーコード
//	csvZaikoSuu:			在庫数
//==========================================================
function OnOrderColorChanged(csvZaikoListId, csvNittokouColorCode, csvHtmlColorCode, csvZaikoSuu)
{
	try{																		// 例外処理開始
		var domComboBox = GetDom("strOrderColor");								// 御注文色コンボボックスを取得
		if(domComboBox == undefined){											// 御注文色コンボボックスが未定義なら･･･
			return;																// 処理中断			
		}
		var nOrderColor = domComboBox.selectedIndex;							// 選択済みの色調を取得
		var aryZaikoListId = csvZaikoListId.split(",");							// 在庫リスト ID を配列に格納
		var aryNittokouColorCode = csvNittokouColorCode.split(",");				// 日本塗料工業会カラーコードを配列に格納
		var aryHtmlColorCode = csvHtmlColorCode.split(",");						// HTML カラーコードを配列に格納
		var aryZaikoSuu = csvZaikoSuu.split(",");								// 在庫数を配列に格納
		//----------------------------------------------------------
		var domZaikoListId = GetDom("nZaikoListId");							// 在庫リスト ID 格納タグを取得
		var domNittokouColorCode = GetDom("spnNittokouColorCode");				// 日本塗料工業会カラーコード出力タグを取得
		var domColorSample = GetDom("strColorSample");							// カラーサンプル出力タグを取得
		var domZaikoSuu = GetDom("spnZaikoSuu");								// 在庫数出力タグを取得
		//----------------------------------------------------------
		if(domZaikoListId != undefined){										// 在庫リスト ID 格納タグが定義済みなら･･･
			domZaikoListId.value = aryZaikoListId[nOrderColor];					// 在庫リスト IDを出力
		}
		if(domNittokouColorCode != undefined){									// 日本塗料工業会カラーコード出力タグが定義済みなら･･･
			domNittokouColorCode.innerHTML = aryNittokouColorCode[nOrderColor];	// 日本塗料工業会カラーコードを出力
		}
		if(domColorSample != undefined){										// カラーサンプル出力タグが定義済みなら･･･
			domColorSample.style.background = aryHtmlColorCode[nOrderColor];	// カラーサンプルを出力
		}
		if(domZaikoSuu != undefined){											// 在庫数出力タグが定義済みなら･･･
			domZaikoSuu.innerHTML = aryZaikoSuu[nOrderColor];					// 在庫数を出力
		}
	}catch(e){																	// 例外を捕捉･･･
		alert(e);																// エラーメッセージを表示
	}
}

