/** フォーム用  */ 

// 数字文字の整形
//   val_len('567',5)  >> out:00567
function val_len(str,len) {
    var strw = '0000000000'+str;
    return strw.substr(strw.length-len,len);
} 

// 数字のチェック
function val_chk(val) {
    //if(val.match(/[^0-9]+/)){
    if(val.match(/^[0-9]+$/)){
       return true;
    }
    return false;
}

// 半角文字のチェック
function asc_chk(str) {
    if(str.match(/^[a-zA-Z0-9]+$/)){
       return true;
    }
    return false;
}

// input Boxの入力文字数チェック
function getInputLengthX(str,lmax,flag){
    var len=str.length;
    var cnt=x=y=0;
    for(i=0; ;cnt++){
        x=str.indexOf("\n",y);
         if(x==-1) break;
         y=x+2;
    }
    kekka = len-(cnt*2);
    if (kekka==0){
       document.getElementById('str_length').innerHTML = '入力文字数が表示されます';
    }else if (kekka<=lmax){
       document.getElementById('str_length').innerHTML = '入力文字数 = '+""+kekka;
    }else{
       document.getElementById('str_length').innerHTML = '<font color="red">入力文字数オーバーです</font>';
       if (flag=='on')
          window.alert('入力文字数オーバーです');
    }
}

// ブリンク
function blink(sel) {
  if (sel==false)
     return;

  if (!document.all) { return; }
  for (i = 0; i < document.all.length; i++) {
    obj = document.all(i);
    if (obj.className == "blink") {
      if (obj.style.visibility == "visible") {
        obj.style.visibility = "hidden";
      } else {
        obj.style.visibility = "visible";
      }
    }
  }
  setTimeout("blink()", 800);
}

// 汎用数字チェック
function js_val_chk(sel){
	//if (sel=='') sel=this;
	var val = document.getElementById(sel).value;
	//window.alert('数値チェック');
	if (val==''){
		return;
	}
	if (!val_chk(val)){
		document.getElementById(sel).value = '';
		window.alert('数値ではありません');
		return;
	}
}

// カンマ編集・除去
// var str_value = '-123456.78';
function addComma(str_value){
    var i;
    for(i = 0; i < str_value.length/3; i++){
        str_value = str_value.replace(/^([+-]?\d+)(\d\d\d)/,"$1,$2");
    }
    return str_value;
}

function removeComma(str_value) {
    return str_value.split(",").join("")
}

// 簡易sleep関数
//  tt:ウェイト時間 (msec)
function sleep_sub(tt) {
	var st1 = new Date();
	while(0==0){
		nt1 = new Date();
		// -- chk_time = parseInt((nt1.getTime() - st1.getTime()) / 1000);
		chk_time = parseInt(nt1.getTime() - st1.getTime());
		if (chk_time>tt){
			break;
		}
	}
}

// 郵便番号から住所取得
function js_postadrs_get(post,adrs,pref){
	var rdata= new Array();
	var pdata= '';

	//window.alert("該当の住所はありませんでした。");

	var post_s = document.getElementById(post).value;
	var adrs_s = document.getElementById(adrs).value;
	if (post_s==''||adrs_s!='')			// 既に住所が入っている場合はパス
		return true;

	pdata = post_s;     // +'\t';
	pdata= base64encode(pdata);
	sock_get_subB(1,pdata,rdata);
	if (rdata[0]=='')
		return false;

	var ary = rdata[0].split("\t");
	//window.alert("postadrs rdata="+rdata[0]+" ary="+ary[2]);
	if (ary[0]==''){
		window.alert("該当の住所はありませんでした。");
		return false;
	}

	document.getElementById(adrs).value = ary[2];			// 住所
	if (pref!=''){							// 都道府県 ary[1]=文字 ary[3]=番号
		_change_dropdownlist( pref, ary[3] );
	}

	return true;
}

// コンボボックスに任意の番号をselectedする
function _change_dropdownlist( id_name, value ){
	var element = document.getElementById(id_name);
	if (! element){ alert( '<select id="' + id_name +'"> is not find.'); return; }
	
	var flag = false;
	for (var i=0; i<element.options.length; i++) {
		var option = element.options[i];
		if (option.value == value){
			if (flag){
				option.selected = false;
			}
			else{
				option.selected = true; flag = true;
			}
		}
		else{ option.selected = false; }
	}
	if(! flag){ alert( 'DATA [' + value + '] is not find in <select id="'+id_name+'"> '); }
}

//日付のチェック (閏年含む)
//in  year,month,day
//out 戻り true:月末日正常  28/29/30 etc:正しい月末日
function checkDate(year, month, day){
   year  = eval(year);
   month = eval(month);
   day   = eval(day);
   //alert(year+'-'+month+'-'+day);
   if(month==2){
     if( ( (year % 4 == 0) && (year % 100 != 0) ) || (year % 400 == 0) ){
       if(day > 29){
         return 29;
       }
     } else {
       if(day > 28){
         return 28;
       }
     }
   } else {
     switch(month){
       case 4:
       case 6:
       case 9:
       case 11:
         if(day > 30){
           return 30;
         }
         break;
       default:
         break;
     }    
   }
   return true;
}

function wopen_wind(url,wcont,param){
	if (wcont=='')
		wcont="left=0,top=0,width=800,height=600,location=no,menubar=no,scrollbars=no,status=no,toolbar=no";
	if (param!='')
		url = url+'?'+utf8to16(base64decode(param));

	win1 = window.open(url,"FormShow",wcont);

	//if (title!='')
	//	win1.document.title = title;

	//win1.document.write('bbbbbbbbbbb');

/*
	if (param!='')
		win1.document.write(param);
	window.alert("url="+url);
*/
}

/** 確認ダイアログの返り値によりフォームを送信  */ 
function SaveSubmitChk () { 
    /* 確認ダイアログ表示 */ 
    /* [OK]ボタンでTRUE、[キャンセル]ボタンでFALSEを返す */ 
    send_flg = confirm ( "保存してもよろしいですか？" ); 
    return send_flg; 
} 

function SubmitChk () { 
    /* 確認ダイアログ表示 */ 
    /* [OK]ボタンでTRUE、[キャンセル]ボタンでFALSEを返す */ 
    send_flg = confirm ( "送信してもよろしいですか？" ); 
    return send_flg; 
} 

function ExecSubmitChk () { 
    /* 確認ダイアログ表示 */ 
    /* [OK]ボタンでTRUE、[キャンセル]ボタンでFALSEを返す */ 
    send_flg = confirm ( "実行してもよろしいですか？" ); 
    return send_flg; 
} 

function DeleteSubmitChk () { 
    /* 確認ダイアログ表示 */ 
    /* [OK]ボタンでTRUE、[キャンセル]ボタンでFALSEを返す */ 
    send_flg = confirm ( "削除してもよろしいですか？" ); 
    return send_flg; 
} 

/* 任意のinput枠にフォーカスを当てる (loc:0-nn)*/
function infocus(loc){
   var inp = document.getElementsByTagName("input");
   inp[loc].focus();
}

/* TextAreaに文字を追加する (既存文字に加える) （※リソースの問題は要注意）*/
function set_tarea_add(parts1,str1){
   //document.getElementById(parts1).value += str1;			// 一辺に行うとリソースを食うので分散
   var w = document.getElementById(parts1).value + str1;	// 食う率は減るがやはりある程度食う
   document.getElementById(parts1).value = w; 

   //document.getElementById("post_body").value += str1;
}

/* TextAreaに文字を追加する (新規に文字追加) */
function set_tarea_new(parts1,str1){
   //alert("文字列:" + str1 + "を設定");
   document.getElementById(parts1).value = str1; 
   //document.getElementById("post_body").value = str1;
}


/* TEXTARE/TEXTから文字をコピーする（クリップボード）
<a href="javascript:copyText(\'tbody\')">文字列をコピー(1)</a>;
<button onClick="copyText(\'tbody\')">文字列をコピー(2)</button>;
*/
function copyText(part1) {
	var text = document.getElementById(part1).value;
	clipboardData.setData("Text", text);
/*
	alert("文字列:" + text + "をコピー");
*/
}

