/**
 * プレフィックスを除去
 */
function excludePrefix(val, prefix) {
	
	if (val == null || val == "" || prefix == null || prefix == "") return val;

	
	if (!startsWith(val, prefix)) {
		return val;
	}
	
	var idx = prefix.length;
	
	return val.substr(idx, val.length - idx);
}


/**
 * 指定文字列から開始される場合にtrue
 * @param $val
 * @param $test
 */
function startsWith(val, test) {
	if (val == null || val == "") return false;
	return val.indexOf(test) == 0;
}

/**
 * 本日の日付を返す
 * @return
 */
function getJaToday() {
	var d = new Date();
	var m = d.getMonth() + 1;
	return d.getFullYear() + "年" + m + "月" + d.getDate() + "日";
}


/**
 * PHP配列のnameを考慮して要素を取得
 * @param name
 * @return
 */
function byNameIfArray(name) {
	
	var ret = byName(name);
	
	if (ret == null) {
		return byName(name + "[]");
	}
	return ret;
}

/**
 * 指定クラスを持つテーブルのnodeが該当するtrの背景色を変更
 * @param tableClass
 * @return
 */
function highlightTr(node, tableClass, color) {
	
	var tr = null;
	
	while (true) {
		var tagName = null;
		try {
			tagName = node.tagName.toLowerCase();
		} catch (e) {
			return;
		}
		
		if (tagName == "tr") {
			tr = node;

		} else if (tagName == "table") {
			if ($(node).hasClass(tableClass)) {
				$("td", tr).css("backgroundColor", color);
				return;
			}

		}
		
		try {
			node = node.parentNode;
			if (node) {
			} else {
				break;
			}
		
		} catch (e) {
			break;
		}
		
	}
	
	
}

/**
 * 指定名の場所へスクロール
 * @param name
 * @return
 */
function scrollById(id) {
	
	try {
		$.scrollTo($("#" + id).position().top - 130, 800);
	} catch (e) {}
}


/**
 * 指定名の場所へスクロール
 * @param name
 * @return
 */
function scrollByName(name1, name2) {
	
	try {
		var elm = $(byNameIfArray(name1));
		if (elm.css("display") == "none") {
			elm = elm.next();
		}
		$.scrollTo(elm.position().top - 130, 800);
	} catch (e) {
		
		try {
			var elm = $(byNameIfArray(name2));
			if (elm.css("display") == "none") {
				elm = elm.next();
			}
			$.scrollTo(elm.position().top - 130, 800);
		} catch (e) {}
	}
}

// 全てreplace
function replaceAll(value, find, replaceValue) {
	
	while (value.indexOf(find) != -1) {
		value = value.replace(find, replaceValue);
	}
	return value;
}

// 文字充填(左)
function leftPad(value, len, char) {
	
	value = value + "";
	
	while (value.length < len) {
		value = char + value;
	}
	
	return value;
}

// 文字充填(右)
function rightPad(value, len, char) {
	
	value = value + "";
	
	while (value.length < len) {
		value = value + char;
	}
	
	return value;
}

// document.getElementById()の短縮形
function byId(id) {
	return document.getElementById(id);
}

// document.getElementsByName()[0]の短縮形
function byName(name) {
	var elms = document.getElementsByName(name);
	if (elms == null || elms.length == 0) return null;
	return elms[0];
}

function byNames(name) {
	var elms = document.getElementsByName(name);
	if (elms == null || elms.length == 0) return null;
	return elms;
}

// optionsの再構成
function replaceOptions(select, options) {
	
	while (select.options.length != 0) {
		select.options[0] = null;
	}
	
	if (options == null) return;
	
	for (var i = 0; i < options.length; i++) {
		select.options[i] = options[i];
	}
}

// null, undeinfedなどを空文字に変換する
function clean(val) {
	
	if (val == null || val == undefined || val == "null" || val == "undefined") {
		return "";
	}
	
	return val;
}



// 数値に3桁カンマを挿入する
function formatNumber(str) {
	if (str == null) return "";
	var num = new String(str).replace(/,/g, "");
	while(num != (num = num.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
	return num;
}
	
// 日付のフォーマットを行う
function formatDate(millis) {
	if (millis == null) return "";
	
	var date = new Date(millis);
	
	var m = date.getMonth() + 1;
	if (m < 10) m = "0" + m;
	
	var d = date.getDate();
	if (d < 10) d = "0" + d;
	
	var h = date.getHours();
	if (h < 10) h = "0" + h;
	
	var mi = date.getMinutes();
	if (mi < 10) mi = "0" + mi;
	
	var s = date.getSeconds();
	if (s < 10) s = "0" + s;
	
	return date.getFullYear() + "/" + m + "/" + d + " " + h + ":" + mi + ":" + s;
	
}
//optionsの再構成
function replaceOptions(select, options) {
	
	while (select.options.length != 0) {
		select.options[0] = null;
	}
	
	if (options == null) return;
	
	for (var i = 0; i < options.length; i++) {
		select.options[i] = options[i];
	}
}




