/*---------------------------------------------------------------------------*/
/*                                                                           */
/*    DSL javascript common library                                          */
/*                                                                           */
/*---------------------------------------------------------------------------*/
/*   Digital Sheep Learning, Inc All Rights Reserved.                        */
/*---------------------------------------------------------------------------*/

/*********************************************************/
/*  ユーティリティ 
/*********************************************************/
/* オンマウスで画像を変えるリンクを作成する */
function imgChgLink(href, img_src, idx, img_w, img_h){
	var img_name = "/" + img_src.substring(0, img_src.lastIndexOf("."));
	img_name = img_name.substring(img_name.lastIndexOf("/")+1, img_name.length) + idx;
	var img_ext = img_src.substring(img_src.lastIndexOf(".")+1, img_src.length);
	var doc = '<a href="'+href+'" onmouseover="imgChg(\''+img_name+'\',1);" onmouseout="imgChg(\''+img_name+'\',0);">'
			+ '<img id="'+img_name+'" src="'+img_src+'" width="'+img_w+'" height="'+img_h+'" border="0">'
			+ '</a>';
	return doc;
}

/* オンマウスで画像を変える */
function imgChg(id, on_off){
	try {
		var img_src = document.getElementById(id).src;
		var img_name = img_src.substring(0, img_src.lastIndexOf("."));
		var img_ext = img_src.substring(img_src.lastIndexOf(".")+1, img_src.length);
		if (on_off == 1 && img_name.indexOf('-now',0) == -1 && img_name.indexOf('-up',0) == -1){
			img_src = img_name + "-up." + img_ext;
		} else if (on_off == 0 && img_name.indexOf('-now',0) == -1 && img_name.indexOf('-up',0) != -1){
		 	img_src = img_name.substring(0, img_name.length -3) + "." + img_ext;
		}
		document.getElementById(id).src = img_src;
	} catch (e){}
}

/* 画像を変える */
function imgNow(id){
	try {
		var img_src = document.getElementById(id).src;
		var img_name = img_src.substring(0, img_src.lastIndexOf("."));
		var img_ext = img_src.substring(img_src.lastIndexOf(".")+1, img_src.length);
		if (img_name.indexOf('-now',0) == -1){
			img_src = img_name + "-now." + img_ext;
		}
		document.getElementById(id).src = img_src;
	} catch (e){}
}

/* Flashコンテンツ */
function writeFlash(src, w, h, vars, bg){
	document.write(getFlash(src, w, h, vars, bg));
}

function getFlash(src, w, h, vars, bg){
	var swf_bgcolor		= (bg == undefined) ? "#FFFFFF" : bg;
	var swf_w			= w;
	var swf_h			= h;
	var swf_name = src;
	var flashvars = vars;
//	swf_name += "?" +new Date().getTime();
	var res = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="'+location.protocol+'//fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" id="SheepTown" width="'+swf_w+'" height="'+swf_h+'" align="middle" >\n';
	res += '<param name="allowScriptAccess" value="sameDomain" />\n';
	res += '<param name="movie" value="'+swf_name+'" />\n';
	res += '<param name="quality" value="high" />\n';
	res += '<param name="bgcolor" value="'+swf_bgcolor+'" />\n';
	res += '<param name="flashvars" value="'+flashvars+'"/>';
	res += '<embed src="'+swf_name+'" quality="high" bgcolor="'+swf_bgcolor+'" width="'+swf_w+'" height="'+swf_h+'" name="SheepTown" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="'+flashvars+'"/>\n';
	res += '</object>\n';
	return res;
}

/* ドキュメントへ書き込み */
function dw(input){
	document.write(input);
}

/*********************************************************/
/*  文字列操作系 
/*********************************************************/
// 前後の空白削除して返す
function trim(input){
	var strRet, strFinal;
	strRet = LTrim(input);
	strFinal = RTrim(strRet);
	return strFinal
}

// 数値へ変換して返す
function toInt(s, def){
	if (def == undefined) def = -1;
	if (isNaN(s)) return def;
	return Number(s);
}

// バイト数を返す
function getByte(str){
	if (str=="" || !str || str==null) return 0;
	str=trashGomi(str);
	var strS=str.replace(/[^0-9a-zｱ-ﾝ\!\"\#\$\%\&\'\(\)\-\=\^\~\\\|\@\`\[\{\;\+\:\*\]\}\,\<\.\>\/\?\_]/ig,"##");
	return strS.length;
}

// 長さを返す
function getLength(str){
	if (str=="" || !str || str==null) return 0;
	str=trashGomi(str);
	var strS=str.replace(/[^0-9a-zｱ-ﾝ\!\"\#\$\%\&\'\(\)\-\=\^\~\\\|\@\`\[\{\;\+\:\*\]\}\,\<\.\>\/\?\_]/ig,"#");
	return strS.length;
}

/*********************************************************/
/*  文字列チェック関係 
/*********************************************************/
// 入力値がメール形式か
function checkMail(str){
	var ck =/[!#-9A-~]+@+[a-z0-9]+.+[^.]$/i;
	return str.match(ck) && getByte(str)==str.length;
}

// 入力値が数値か
/*
function checkNumber(str){
	var ck =/[0-9]$/i;
	return str.match(ck);
}
*/

// 入力値が電話番号形式か
function checkTel(str){
	var ck =/[0-9]$/i;
	var len = (str.length >= 10 && str.length <= 11);
	return str.match(ck) && len;
}

/**
 * 文字列の入力値チェック
 * @param id htmlのid属性
 * @param type 0:文字列 1:英数字 2:カタカナ
 * @param min 最小の長さ
 * @param max 最大の長さ
 * @return true:文字列である false:文字列ではない
 */
function checkInput(id, type, min, max) {
	var input = document.getElementById(id);
	var res = false;

	if (input == undefined)
		return false;

	// 必須項目チェック	
	if ((input.value == "") && (min == undefined || min == 0))
		return true;
		
	// 文字列の種類チェック
	if (type == 0) {
		res = true; // 文字列チェック
	} else if (type == 1) {
		// コードチェック
		res = regularExpressionValidate(input.value, /[.0-9a-zA-Z@_-]*/);
	} else if (type == 2) {
		// カタカナチェック
		res = regularExpressionValidate(input.value, /[ァ-ヶー\s　]*/);
	} else if (type == 3) {
		// メールアドレスチェック
		res = regularExpressionValidate(input.value, /[!#-9A-~]+@[\.a-z0-9_-]+\.[a-zA-Z]+$/i);
	} else if (type == 4) {
		// 英数字チェック
		res = (getByte(input.value) == getLength(input.value));
	} else {
		res = true;
	}


	// 範囲チェック
	if (res == true && (min != undefined || max != undefined)) {
		res = lengthValidate(input.value, min, max);
	}

	return res;
}

/**
 * 数値(整数)の入力値チェック
 * @param htmlのid属性
 * @min 範囲の最小値
 * @max 範囲の最大値
 * @return true:数値である false:数値でない
 */
function checkNumber(id, min, max) {
	var input = document.getElementById(id);
	var res = false;
	
	if (input == undefined)
		return false;

	// 数値チェック
	res = regularExpressionValidate(input.value, /[0-9]*/i)

	// 範囲チェック
	if (res == true && (min != undefined || max != undefined)) {
		if (rangeValidate(input.value, min, max)) {
			res = true;
		} else {
			res = false;
		}
	}

	return res;
}

/**
 * 日付(yyyy/mm/dd)の入力値チェック
 * @param htmlのid属性
 * @return true:日付形式である false:日付形式でない
 */
function checkDate(id) {
	var input = document.getElementById(id);
	
	if (input == undefined)
		return false;

	// 日付の形式チェック
	if (! regularExpressionValidate(input.value, /^\d{4}\/\d{2}\/\d{2}$/))
		return false;

	chkdYear = input.value.substr(0, 4);
	chkdMonth = input.value.substr(5, 2) - 1; // javascriptで月は0..11
	chkdDay = input.value.substr(8, 2);

	if (0 <= chkdMonth && chkdMonth <= 11
	    && 1 <= chkdDay && chkdDay <= 31) {
		var chkdDate = new Date(chkdYear, chkdMonth, chkdDay);

		if (isNaN(chkdDate)) {
			return false;
		} else if (chkdDate.getFullYear() == chkdYear
				   && chkdDate.getMonth() == chkdMonth
				   && chkdDate.getDate() == chkdDay) {
			return true;
		}
	}
	return false;
}

/**
 * 拡張子チェック
 * @param id hemlのid属性
 * @param kindOfExt 拡張子の種類
 * @return true false
 */
function checkExtension(id, kindOfExt) {
	var input = document.getElementById(id);
	var res;
	
	if (input == undefined)
		return false;
		
	var re = new RegExp(".+\\." + kindOfExt + "$", "i");

	res = regularExpressionValidate(input.value, re);
	
	return res;
}

/**
 * 正規表現チェック
 * @param str チェックする文字列
 * @param pattern 文字列と一致させるパターン
 * @return true:文字列とパターンが一致
 *         false:文字列とパターンが不一致
 */
function regularExpressionValidate(str, pattern) {
	str = String(str);

	if (str.match(pattern) == str) {
		return true;
	} else {
		return false;
	}
}

/**
 * 文字列の長さチェック
 * @param input 入力値
 * @param min 最小の長さ
 * @param max 最大の長さ
 * @return true:範囲内である false:範囲内でない
 */
function lengthValidate(input, min, max) {
	if (input == undefined)
		return false;
		
	var text = String(input);
	
	if (min == undefined) min = 0;
	if (max == undefined) max = text.length;
		
	if (min <= text.length && text.length <= max) {
		return true;
	} else {
		return false;
	}
}


/**
 * 範囲チェック(数値)
 * @param input 入力値
 * @param min 最小値
 * @param max 最大値
 * @return true:範囲内である false:範囲内でない
 */
function rangeValidate(input, min, max) {

	if (input == undefined)
		return false;
	var num = Number(input);
	
	if (min == undefined) min = num;
	if (max == undefined) max = num;

	if (min <= num && num <= max) {
		return true;
	} else {
		return false;
	}
}

/*********************************************************/
/*  form操作
/*********************************************************/
// チェックのついたチェックボックスの値をseparatorで結合
function getCheckboxValue(name, separator) {
	var checkBoxList = document.getElementsByName(name);
	var value = "";
	
	if (checkBoxList == null)
		return;
	
	for (var i = 0; i < checkBoxList.length; i++) {
		if (checkBoxList[i].checked) {
			value += checkBoxList[i].value;
			value += separator;
		}
	}
	return value;
}

// values[]と一致する値を持つチェックボックスにチェックを入れる
function setCheckboxValue(name, values) {
	var checkBoxList = document.getElementsByName(name);
	var index = 0;

	if (checkBoxList == null)
		return;

	if (values != null && values.length != 0) {
		for (var i = 0; i < checkBoxList.length; i++) {
			if (checkBoxList[i].value == values[index]) {
				checkBoxList[i].checked = "checked";
				index++;
			}
		}
	}
}

/**
 *  リストボックス内のアイテムをsrcListBox→dstListBoxにコピー
 * @param srcId 移動元リストボックスのid
 * @param dstId 移動先リストボックスのid
 */
function copyItems(srcId, dstId) {
	var srcListBox = document.getElementById(srcId);
	var dstListBox = document.getElementById(dstId);

	for (i = 0; i < srcListBox.length; i++) {
		var existOption = true;
		if (srcListBox[i].selected == true) {
			
			for (j = 0; j < dstListBox.length; j++) {
				if (srcListBox[i].value == dstListBox[j].value) {
					existOption = false;
					break;
				}
			}
			if (existOption == true) {
				var o = new Option();
				o.text = srcListBox[i].text;
				o.value = srcListBox[i].value;
				dstListBox.options[dstListBox.length] = o;
			}
		}
	}
	return;
}

/**
 *  リストボックス内のアイテムをsrcListBox→dstListBoxに移動
 * @param srcId 移動元リストボックスのid
 * @param dstId 移動先リストボックスのid
 */
function moveItems(srcId, dstId) {
	var srcListBox = document.getElementById(srcId);
	var dstListBox = document.getElementById(dstId);

	for (var i = 0; i < srcListBox.length; i++) {
		if (srcListBox[i].selected == true) {
			var o = new Option();
			o.text = srcListBox[i].text;
			o.value = srcListBox[i].value;
			dstListBox.options[dstListBox.length] = o;
		}
	}
	removeItem(srcId);
	return;
}

/**
 *  リストボックス内のアイテムを削除
 * @param id リストボックスのid
 */
function removeItem(id) {
	var listBox = document.getElementById(id);
	
	if (listBox == null)
		return;
	
	while (listBox.selectedIndex >= 0) {
		listBox.remove(listBox.selectedIndex);
	}
	
	return;
}

/**
 *  リストボックス内のアイテムを全削除
 * @param id リストボックスのid
 */
function removeAllItem(id) {
	var listBox = document.getElementById(id);
	
	if (listBox == null)
		return;
	
	while (listBox.length > 0) {
		listBox.remove(0);
	}
	
	return;
}

/**
 *  リストボックス内のアイテムを追加
 * @param id リストボックスのid
 */
function addItem(id, text, value) {
	var listBox = document.getElementById(id);
	
	if (listBox == null)
		return;
	
	var o = new Option();
	o.text = text;
	o.value = value;
	listBox.options[listBox.length] = o;
	
	return;
}

// リストボックス内のオプションの値をseparatorで結合
function getListBoxValue(id, separator) {
	var listBox = document.getElementById(id);
	var value = "";
	
	if (listBox == null)
		return;
	
	for (var i = 0; i < listBox.length; i++) {
	    value += listBox[i].value;
	    value += separator;
	}
	return value;
}

function getListBoxSelectedValues(id) {
	var listBox = document.getElementById(id);
	var value = "";
	
	if (listBox == null)
		return;
	
	var res = new Array();
	for (var i = 0; i < listBox.length; i++) {
		if (listBox[i].selected){
			var obj = new Object();
			obj.value = listBox[i].value;
			obj.text = listBox[i].text;
		    res.push(obj);
		}
	}
	return res;
}
/*********************************************************/
/* display 操作
/*********************************************************/
function switch_display(id){
	var obj = document.getElementById(id);
	if (obj == undefined) return;
	var disp = obj.style.display;
	obj.style.display = (disp == "none") ? "block":"none";
}

/*********************************************************/
/*  cookie 操作
/*********************************************************/
// 取得する
function getCookie(key) {
    var tmp1 = " " + document.cookie + ";";
    xx1 = xx2 = 0;
    var len = tmp1.length;
    while (xx1 < len) {
        xx2 = tmp1.indexOf(";", xx1);
        var tmp2 = tmp1.substring(xx1 + 1, xx2);
        xx3 = tmp2.indexOf("=");
        if (tmp2.substring(0, xx3) == key) {
            return(unescape(tmp2.substring(xx3 + 1, xx2 - xx1 - 1)));
        }
        xx1 = xx2 + 1;
    }
    return("");
}
// 設定する
function setCookie(key, val) {
    tmp = key + "=" + escape(val) + "; ";
    // tmp += "path=" + location.pathname + "; ";
    tmp += "path=/";
    document.cookie = tmp;
}

/*********************************************************/
/*  ブラウザ判別
/*********************************************************/
// Macintosh            ->  MacOS
// Windows95/98/NT/2000/XP  -> Windows
// UNIX                  -> UNIX
function getOSType()
{
    var uAgent  = navigator.userAgent.toUpperCase();
    if (uAgent.indexOf("MAC") >= 0) return "MacOS";
    if (uAgent.indexOf("WIN") >= 0) return "Windows";
    if (uAgent.indexOf("X11") >= 0) return "UNIX";
    return "";
}
// Netscape Navigator ->  Netscape
// Internet Explorer  ->Explorer
// Safari  -> Safari
// Opera  -> Opera
function getBrowserName()
{
    var aName  = navigator.appName.toUpperCase();
    var uName = navigator.userAgent.toUpperCase();
    if (uName.indexOf("SAFARI") >= 0)  return "Safari";
    if (uName.indexOf("OPERA") >= 0)  return "Opera";
    if (aName.indexOf("NETSCAPE") >= 0)  return "Netscape";
    if (aName.indexOf("MICROSOFT") >= 0) return "Explorer";
    return "";
}
function getBrowserVersion()
{
	var browser = getBrowserName();
	var version = 0;
	var s = 0;
	var e = 0;
	var appVer  = navigator.appVersion;
	var uName  = navigator.userAgent.toUpperCase();
	if (browser == "Safari")
	{
		version = eval(appVer.substring(0,3)) - 4;
	}
	if (browser == "Opera")
	{
		s = uName.indexOf("OPERA",0) + 6;
		e = uName.indexOf(" ",s);
		version = eval(uName.substring(s,e));
	}
	if (browser == "Netscape")
	{
		s = appVer.indexOf(" ",0);
		version = eval(appVer.substring(0,s));
		if (version >= 5) version++;
	}
	if (browser == "Explorer")
	{
		appVer  = navigator.userAgent;
		s = appVer.indexOf("MSIE ",0) + 5;
		e = appVer.indexOf(";",s);
		version = eval(appVer.substring(s,e));
	}
	return version;
}

function isIE5up() {
	var browser = getBrowserName();
	var ver = getBrowserVersion();
	if ( browser == "Explorer" && ver >= 5) {
		return true;
	}
	return false;
}

function isSafari() {
	var browser = getBrowserName();
	if ( browser == "Safari" ) {
		return true;
	}
	return false;
}

function isMozilla() {
	var browser = getBrowserName();
	if ( browser == "Netscape" ) {
		return true;
	}
	return false;
}

/*********************************************************/
/*  private
/*********************************************************/
function trashGomi(s){
	s=unescape(escape(s).split("%00")[0]);
	return s;
}

function RTrim(strTemp){
	var nLoop = 0;
	var strReturn = strTemp;
	while (nLoop < strTemp.length)	{
		if (strReturn.substring(strReturn.length - 1, strReturn.length) == " "){
			strReturn = strTemp.substring(0, strTemp.length - (nLoop + 1));
		} else {
			break;
		}
		nLoop++;
	}
	return strReturn;
}

function LTrim(strTemp){
	var nLoop = 0;
	var strReturn = strTemp;
	while (nLoop < strTemp.length) {
		if (strReturn.substring(0, 1) == " ") {
			strReturn = strTemp.substring(nLoop + 1, strTemp.length);
		} else {
			break;
		}
		nLoop++;
	}
	return strReturn;
}

