/**
 * 任意のフォームを利用して、任意のActionへsubmitするメソッド、複数ボタン時使用
 * @param frm フォーム番号
 * @param url submit先のURL
 */
function submitAction(frm,url) { 
	document.forms[frm].action=url;
	document.forms[frm].submit();
}

/**
 * 任意のフォームのオブジェクトに値をセットする
 * @param frm フォーム番号
 * @param obj セットしたいオブジェクト名
 * @param arg セットしたい値
*/
function setParam(frm,obj,arg) { 
    var submitForm="document.forms[" + frm +"]";
    var chObject="document.forms[" + frm +"]."+ obj;
	eval(chObject).value=arg;
}

/**
 * 任意のフォームのオブジェクトに値をセットし、サブミットまで行う
 * @param frm フォーム番号
 * @param obj セットしたいオブジェクト名
 * @param arg セットしたい値
 * @param url 飛ばしたいURL
*/
function setParamSubmit(frm,obj,arg,url) {
    var submitForm="document.forms[" + frm +"]";
    var chObject="document.forms[" + frm +"]."+ obj;
	eval(chObject).value=arg;
	eval(submitForm).action=url;
	document.forms[frm].submit();
}
/**
 * 項目の横移動
 * @param   selectobj1  移動元selectオブジェクト
 * @param   selectobj2  移動先selectオブジェクト
 * @param   all_flag    全て移動=true、選択項目のみ移動=false
 */
function moveModule(selectobj1, selectobj2, all_flag) {
    // 移動元の1番目の選択項目インデックス
    from_index = selectobj1.selectedIndex;
    // 移動元の最後の選択項目インデックス
    last_selected_index = 0;
    // 移動元の項目を選択しているか調べる
    if(all_flag || (from_index!=-1 && selectobj1.options[from_index].value>"")) {
        // 移動元の項目数回ループする
        for(i=0 ; i<selectobj1.length ; i++) {
            // optionオブジェクトが選択状態かどうか調べる
            if(all_flag || selectobj1.options[i].selected==true) {
                // all_flagがtrueか、選択状態ならば移動処理を実行する
                oText = selectobj1.options[i].text;
                oValue = selectobj1.options[i].value;
                selectobj1.options[i] = null;
                // 移動先の項目数
                to_length = selectobj2.length;
                selectobj2.options[to_length] = new Option(oText, oValue, false, true);
                i--;    // 移動したらインデックスをデクリメント
                last_selected_index = i + 1;
            }
        }
        // 移動元のオブジェクトにフォーカス移動する
        selectobj1.focus();
        // 移動元の最後の選択項目インデックスが項目数以上ならば項目数-1にする
        if(last_selected_index >= selectobj1.length) {
            last_selected_index = selectobj1.length - 1;
        }
        // 移動元リストの項目数が0でなければ、最終選択項目をselect状態にする
        if(selectobj1.length != 0) {
            selectobj1.options[last_selected_index].selected = true;
        }
        // 移動先の選択状態を全て解除する
        for(i=0 ; i<selectobj2.length ; i++) {
            selectobj2.options[i].selected = false;
        }
    } else {
        alert("項目を選択してください");
    }
}


/**
 * Submit時の処理
 * @param   selectobj   selectオブジェクト
 * @param   hiddenobj   hiddenオブジェクト
 */
function submitMultiSelect(selectobj, hiddenobj) {
    selectAll(selectobj, true);
    //bindSelectItems(selectobj,hiddenobj);
}

/**
 * SELECTオブジェクトの選択肢を全て同じ選択状態にする
 * @param   selectobj   selectオブジェクト
 * @param   status      true･･･選択状態にする、false･･･非選択状態にする
 */
function selectAll(selectobj, status) {
    for(var i=0 ; i<selectobj.length ; i++) {
        selectobj.options[i].selected = status;
    }
}

/**
 * 選択された項目の値をカンマ区切りの文字列にする
 * @param   selectobj   selectオブジェクト
 * @param   hiddenobj   hiddenオブジェクト
 */
function bindSelectItems(selectobj, hiddenobj) {
    var bindStr = "";
    for(var i=0 ; i<selectobj.length ; i++) {
        //alert(selectobj.options[i].value);
        if (i == 0) {
            bindStr = selectobj.options[i].value;
        } else {
            bindStr = bindStr + "," + selectobj.options[i].value;
        }
    }
    // hiddenobjが存在していれば、カンマ区切り文字列をhiddenオブジェクトに代入する
    if (hiddenobj) {
        hiddenobj.value = bindStr;
    }
}
/**
 * Submit時の処理
 * @param   selectobj   selectオブジェクト
 */
function checkSubmit(selectobj) {
	// 選択商材をselect状態にする
	//alert(selectobj.options[0].value);
	submitMultiSelect(selectobj);
	return true;
}

/**
 *　チェックされたラジオボタンのvalueを返す
 * @param frm フォーム番号
 * @param obj 値を取得したいラジオボタンオブジェクト名
 */
function checkRadio(frm,obj,cnt){
	n = 0;
	for (i=0; i<cnt; i++)
	{
		if (cnt==1) {
    		flag = document.forms[frm][obj].checked;
		} else {
    		flag = document.forms[frm][obj][i].checked;
			if (flag == true) n = i;
		}
	}
	if (cnt==1) {
		return (document.forms[frm][obj].value);
	} else if (cnt>1) {
		return (document.forms[frm][obj][n].value);
	} else if (cnt<1) {
		return "";
	}
}


/**
 * 新しくウィンドウを開く
 * @param url  対象URL
 * @param page 対象ウィンドウ名
 */
function openWindow(url, page){
	var config;
	
	config = "directories=no";
	config = config + ",location=no";
	config = config + ",menubar=no";
	config = config + ",titlebar=no";
	config = config + ",toolbar=no";
	config = config + ",status=no";
	config = config + ",scrollbars=yes";
	config = config + ",resizable=no";
	config = config + ",width=795";
	config = config + ",height=500";
	w = window.open(url, page, config);
	w.focus(); 
	//<A>?^?O??href???????L?q??????false?????????\???????????R?????g?A?E?g
	//?g?p???F<a href="javascript:openWindow(url, page);">
	//return false;
}

function getAffiliateSettingValue(name) {
    var object= "document.forms[0]." + name;
    var val = eval(object).value;
    return val;
}

function getCategoryValue() {
    var val = "";
    var categoryList = new Array();
    for(var i = 0; i < document.forms[0].ct.length; i++){
        if(document.forms[0].ct[i].checked == true) {
            categoryList[categoryList.length] = document.forms[0].ct[i].value;
        }
    }
    for(var j = 0; j < categoryList.length; j++) {
        val += categoryList[j];
        if(j+1 < categoryList.length) {
            val += ",";
        }
    }
    return val;
}

function encodeAffiliateSettingKeyword(name) {
    var keyword = getAffiliateSettingValue(name);
    var result = "";
    for (i = 0; i < keyword.length; i++) {
        var ch = keyword.charAt(i);
        if(ch == "&") {
            result += "%26";
        } else if(ch == "?") {
            result += "%3F";
        } else if(ch == "=") {
            result += "%3D";
        } else {
            result += ch;
        }
    }
    return result;
}

/**
 * Ajax用のXmlHttp
 */
function createXmlHttp() {
	if (window.ActiveXObject) {
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				return new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e2) {
				return null;
			}
		}
	} else {
		if (window.XMLHttpRequest) {
			return new XMLHttpRequest();
			
		}
	}
}
/**
 * Ajax用のHttpRquest
 * @param url      リクエストURL
 * @param params   パラメータ
 * @param callback コールバック
 * @param method   GET/POST
 */
var httpRequest;
function senRequest(url, params, callback, method) {
	httpRequest = createXmlHttp();
	var httpMethod = method ? method : 'GET';
	if (httpMethod != 'GET' && httpMethod != 'POST') {
		httpMethod = 'GET';
	}
	
	var httpParams = (params == null || params == '') ? null : params;
	var httpUrl = url;
	if (httpMethod == 'GET' && httpParams != null) {
		httpUrl = httpUrl + "?" + httpParams;
	}
	
	httpRequest.open(httpMethod,httpUrl,true);
	httpRequest.setRequestHeader(
		'Content-Type', 'application/x-www-form-urlencoded');

	httpRequest.onreadystatechange = callback;
	httpRequest.send(httpMethod == 'POST' ? httpParams : null);
}

/**
 * 配列の値をランダムソートして返す
 * @param targetArr ランダムソートする配列
 * @return retArr ランダムソートした配列
 */
function getRandomSort(targetArr) {
	var tempArr = new Array();
	var no;
	var put;
	while (true) {
		no = Math.floor(Math.random() * targetArr.length);

		if (tempArr.length == 0) {
			tempArr.push(no);

		} else {
			for (i=0; i<tempArr.length; i++) {
				if (tempArr[i] == no) {
					put = "true";
					break;
				}
			}
			if (put != "true") {
				tempArr.push(no);
			}
			put = "";
		}
		
		if (tempArr.length == targetArr.length) {
			break;
		}
	}
	
	var retArr = new Array();
	for (j=0; j<tempArr.length; j++) {
		var num = tempArr[j];
		retArr.push(targetArr[num]);
	}
	
	return retArr;
}
