/**
*	file:
*			tools.js
*	summary
*			汎用のJavaScript
*	@author
*			takeshi kyoda
*	@todo
*			2006.02.20	作成
*			2006.11.19	submit_check確認関数追加
*			2006.12.12	json_parse関数追加
*/

/**
*	ページ読込時に変数、vmsg が設定されていたら表示する。
*/
var vmsg;
var	jurl;

window.onload = function()
{
	if(vmsg != null){
		if(vmsg.length > 0){
			alert(vmsg);
		}
	}
	if(jurl != null){
		if(jurl.length > 0){
			location.href = jurl;
			return false;
		}
	}
}
/**
*	非表示の入力項目を作成する。
*/
function makeHidden(f, name, val){

	var find=false;

	for(var i=0; i<f.elements.length; i++){
		if(f.elements[i].name == name){
			find = true;
			break;
		}
	}
	
	if(find == false){	
		var ele = document.createElement("input");
	
		ele.type 	= "hidden";
		ele.value 	= val;
		ele.id 		= name;
		ele.name	= name;

		f.appendChild(ele);
	}else{
		f.elements[name].value = val;
	}
}
/**
*	リスト値を作成する。
*/
function setList(list, str, sel){

	var option = list.options;

	var oList = str.split("\n");
	var	tmp;

	list.length = oList.length;
	for(var i = 0; i<oList.length; i++){
		tmp = oList[i].split(",");

		option[i].value = tmp[0];
		option[i].text 	= tmp[1];

		if(sel != null){
			if(tmp[0] == sel){
				list.selectedIndex = i;
			}
		}
	}
}
/**
*	確認メッセージプロトタイプ
*/
function cMsg(){
}
new cMsg();
/**
*	登録用メッセージ
*/
cMsg.prototype.AddMsg = function(btn,msg)
{
	if(submit_check(btn.form) == true){
		if(msg == null){
			msg = '登録しますか？';
		}
		if(confirm(msg) == true){
			makeHidden(btn.form, 'func', '_add');
			btn.form.submit();
		}
	}
	return false;
}
/**
*	更新用メッセージ
*/
cMsg.prototype.UpMsg = function(btn,msg)
{
	if(msg == null){
		msg = '更新しますか？';
	}	
	if(confirm(msg) == true){
		makeHidden(btn.form, 'func', '_update');
		btn.form.submit();
	}
	return false;
}
/**
*	削除用のメッセージ
*/
cMsg.prototype.DelMsg = function(btn,msg,val)
{
	if(msg == null){
		msg = '削除しますか？';
	}
	if(confirm(msg) == true){
		if(val == null){
			makeHidden(btn.form, 'func', '_del');
		}else{
			makeHidden(btn.form, 'func', val);
		}
		btn.form.submit();
	}
	return false;
}
/**
*	リセットのメッセージ
*/
cMsg.prototype.ResetMsg = function(btn,msg)
{
	if(msg == null){
		msg = 'リセットしますか？';
	}
	if(confirm(msg) == true){
		btn.form.reset();
	}
	return false;
}
/**
*	フォームを送信する。	
*/
cMsg.prototype.fSubmit = function(btn,val,msg){

	var	ret = true;

	if(submit_check(btn.form) == true){
	
		if(val == null){
			val = '_edit';
		}
		if(msg != null){
			if(msg.length > 0){
				ret = confirm(msg);
			}
		}
		if(ret == true){
			makeHidden(btn.form, 'func', val);
			btn.form.submit();
			return true;
		}
	}
	return false;
}
//	定義
var cMsg = new cMsg();



/**
*	使用不可にする
*
*
*/
function dis(ctl,flag){
	ctl.disabled = flag;
}

function getRadio(con)
{
	for(i=0;i<con.length;i++){
    	if(con[i].checked == true){
			return con[i].value;
		}
	}
	return false;
}
/**
*	クッキーの設定
*/
function setCookie(cName,cValue, eDay){
	xDay = new Date;						//
//	xDay.setHours(xDay.getHours() + 1); 	//	一時間後に削除
	if(eDay != null){
	   	xDay.setTime(xDay.getTime()+(eDay*24*60*60*1000))
		
   		expDay = xDay.toGMTString(); 			//	GMT形式の文字列に変換

		document.cookie = cName + "=" + escape(cValue) + ";expires=" + expDay;
	}else{
		document.cookie = cName + "=" + escape(cValue) + ";";
	}
}
/**
*	クッキーの削除
*/
function deleteCookie(cName){
	document.cookie = cName + "=;expires=Thu,01-Jan-70 00:00:01 GMT";
	return true;
}
/**
*	クッキー取得
*/
function getCookie(cName){
	cName += "=";
	cCookie = document.cookie + ";";
	start = cCookie.indexOf(cName);
	
	if(start != -1){
		end = cCookie.indexOf(";", start);
		return unescape(cCookie.substring(start + cName.length, end));
	}
	return false;
}
/**
*	XMLHttpRequest オブジェクトを作成する。
*/
function createXMLHttpRequest(cbFunc,url)
{
	var XMLhttpObject = null;
	
	try{
		XMLhttpObject = new XMLHttpRequest();
		if(XMLhttpObject) XMLhttpObject.onload = cbFunc;
	}catch(e){
		try{
			XMLhttpObject = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				XMLhttpObject = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){
				return null;
			}
		}
		if(XMLhttpObject) XMLhttpObject.onreadystatechange = cbFunc;
	}

	if(url != null){
		XMLhttpObject.open("GET",url,true);
		XMLhttpObject.send(null);
	}
	
	return XMLhttpObject;
}
/**
*	表示・非表示を切り替える
*/
function show(name, is_show){
	obj = document.getElementById(name);
	
	stat = obj.style.display;
	
	if(is_show == null){
		if(stat == "none"){
			obj.style.display = "";
		}else{
			obj.style.display = "none";
		}
	}else{
		if(is_show == true){
			obj.style.display = "";
		}else{
			obj.style.display = "none";
		}
	}
}
/**
*
*/
function designmode(src){
	
	obj= document.forms[0].elements[src];
	
	if(obj.style.display != "none"){
		
		var iframe = null;

		if(document.all){
			iframe = document.all(src+"_iframe");
			doc = iframe.contentWindow.document;
			ico = document.all(src+"_mode");
		}else if(document.getElementById){
			iframe = document.getElementById(src+"_iframe");
			doc = iframe.contentWindow.document;
			ico = document.getElementById(src+"_mode");
		}
		
		ico.src = "img/design.gif";
		
		width = obj.style.width;
		height = obj.style.height;
		
		val = obj.value;
		if(!val.match(/\S/)) val = '';
		
		val = val.replace(/\r\n/g,"<br>");
		val = val.replace(/\n/g,"<br>");
		
		if(document.all){
			doc.body.contentEditable = true;
			doc.body.innerHTML = val;
			obj.style.display= "none";
			iframe.style.display = "block";
		}else if(document.getElementById){
			doc.designMode = "off";
			doc.body.innerHTML = val;
			obj.style.display= "none";
			iframe.style.display = "block";
			doc.designMode = "on";
		}
		
	}else{
		if(document.all){
			iframe = document.all(src+"_iframe");
			ico = document.all(src+"_mode");
		}else if(document.getElementById){
			iframe = document.getElementById(src+"_iframe");
			ico = document.getElementById(src+"_mode");
		}
		
		ico.src = "img/code.gif";
		
		doc = iframe.contentWindow.document;
		val = doc.body.innerHTML;
		if(val.match(/^<br>\s+$/)) val = '';
		
		val = val.replace(/<br>/gi,"\n");
		
		obj.value = val;

		iframe.style.display = "none";
		obj.style.display='block';
		
	}
}
/**
*	JSON生成
*/
function json_parse(v){
	if(v.length != 0){
		return eval('(' + v + ')');
	}
}
/**
*	フォーム送信時の入力確認をする確認
*
*/
function submit_check(fm){

	/** 暫定処置 */
	return true;

	if(!window['validate']){
		return true;
	}
	if(window['console']){
		console.log(fm.name);
	}
	var fname = fm.name;
	
	alert('submit_check() ' + fm.name);
	var	msg = null;
//	var v 	= validate[ fm._form.value ];	//	指定フォームに対する入力確認情報設定
	var v 	= validate[ fname ];	//	指定フォームに対する入力確認情報設定
	
	for(var i=0; i<v.length; i++){
		
		if(v[i].empty == false){
//			alert(v[i].name + ":" + fm[v[i].name].type);

			switch(fm[v[i].name].type){
				case 'text':
				case 'password':
				case 'hidden':
				
					if(fm[v[i].name].value.length == 0){
						alert(v[i].fname + 'を入力してください');
						return false;
					}
					break;
				case 'checkbox':
				case 'radio':
					if(fm[v[i].name].checked != true){
						alert(v[i].fname + 'を選択してください');
						return false;
					}
			}
		}
		if(v[i].type != null){
			var val = fm[v[i].name].value;
			
			if(val.length != 0){
				switch(v[i].type){
					case 'eng':
						ret = val.match(/^[a-zA-Z]+$/);
						break;
					case 'num':
						ret = val.match(/^[0-9]+$/);
						break;
					case 'eng_num':
						ret = val.match(/^[0-9a-zA-Z]+$/);
						break;
					case 'han':
						ret = val.match(/^[a-z0-9\s\.\/\[\]\(\)\{\}\|\^\$\-\+\*\\\"\'#$%&~@`:<>,!?_=;]+$/i);
						break;
					case 'date':		//	日付
						ret = val.match(/^[0-9]{4}[-\/\.][0-9]{2}[-\/\.][0-9]{2}$/);
						break;
					case 'time':		//	時間
						ret = val.match(/^[0-9]{2}\:[0-9]{2}\:[0-9]{2}$/);
						break;
					case 'date_time':	//	日時
						ret = val.match(/^[0-9]{4}[-\/\.][0-9]{2}[-\/\.][0-9]{2} [0-9]{2}\:[0-9]{2}\:[0-9]{2}$/);
						break;
					case 'zip':			//	郵便番号	999-9999
						ret = val.match(/^[0-9]{3}\-[0-9]{4}$/);
						break;
					case 'tel':			//	電話番号	
						ret = val.match(/^[0-9]{2,3}\-[0-9]{2,4}\-[0-9]{4}$/);
						break;
					case 'email':		//	メールアドレス
						ret = val.match(/^[^@]+@([^.]+\..+$)/);
						break;
					case 'email_mobile':
						ret = val.match(/^[^@]+@([^.]+\..+$)/);
						if(ret != null){
							ret = RegExp.$1.match(/(docomo\.co\.jp$)|(ezweb\.ne\.jp$)|(vodafone\.ne\.jp$)|(softbank\.ne\.jp$)/);
							if(ret == null){
								alert('携帯のメールアドレスを入力してください');
								return false;
							}
						}
						break;
					case 'email_nomobile':
						ret = val.match(/^[^@]+@([^.]+\..+$)/);
						if(ret != null){
							ret = RegExp.$1.match(/(docomo\.co\.jp$)|(ezweb\.ne\.jp$)|(vodafone\.ne\.jp$)|(softbank\.ne\.jp$)/);
							if(ret != null){
								alert('携帯のメールアドレスは使用できません');
								return false;
							}
						}
						break;
					default:			//	独自の正規表現を指定
						ret = val.match(v[i].type);
						break;
				}	
				if(ret == null){
					if(msg == null){
						alert(v[i].fname + 'の入力書式が正しくありません!');
						return false;
					}
				}
			}
		}
	}
	return true;
}
