/* javascript 共通関数 */

/* IE で高速化(http://d.hatena.ne.jp/amachang/20071010/1192012056) */
/*@cc_on _d=document;eval('var document=_d')@*/

/* select 文をクリックしてページジャンプで飛ぶ */
function pushNewDisplay(frm) {
  frm.newdisplay.value= "表示" ;
  frm.submit();
  return true;
};
function pushNewDisplay2(frm, url) {
  frm.action = url;
  frm.newdisplay.value= "表示" ;
  frm.submit();
  return true;
};

/* select 文をクリックしてページジャンプで飛ぶ */
function pushSelChange(frm) {
  frm.submit();
  return true;
};

/* Submit して別のページに飛ぶ */
function onFormSubmitURL(frm, url) {
  frm.action = url;
  return true;
};

/* ソート Submit して別のページに飛ぶ */
function onFormSubmitSort(tmp) {
  document.frmkokyaku.sort.value = tmp;
  document.frmkokyaku.submit();
  return true;
};
/* ソート Submit して別のページに飛ぶ */
function formSubmitSort(frm, tmp) {
  frm.sort.value = tmp;
  frm.submit();
  return true;
};

/* 郵便番号抽出 */
function winOpen(opt){
  url = "/php/get_zipcode.php?txtZipcode=" + opt.value;
  win=window.open(url , "Title" , "width=1,height=1,menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=no");
};

/* 新しいウィンドウを開く */
function openNewWindow(url){
  win=window.open(url , "Title" , "");
};

/* ヘルプウィンドウを開く */
function openHelpWindow(url) {
	if (url=="") { return; }
	win=window.open(url,"help","top=80,left=80,width=512,height=600,menubar=no,toolbar=no,scrollbars=yes,resizable=yes");
	if (!win.opener) win.opener=self;
	if (window.focus) win.focus();
};

/* グラフウィンドウを開く */
function openGraphWindow(url) {
	if (url=="") { return; }
	win=window.open(url,"graph","top=80,left=80,width=660,height=530,menubar=no,toolbar=no,scrollbars=yes,resizable=yes");
	if (!win.opener) win.opener=self;
	if (window.focus) win.focus();
};

/* ウィザードウィンドウを開く */
function openWizWindow(url) {
	if (url=="") { return; }
	win=window.open(url,"wizard","top=80,left=80,width=512,height=600,menubar=no,toolbar=no,scrollbars=yes,resizable=yes");
	if (!win.opener) win.opener=self;
	if (window.focus) win.focus();
};

/* メインウィンドウを変更する */
function changeMainWindow(url) {
	if (url=="") { return; }
	win=window.opener.location.href = url;
};

/* フラッシュを介して，クリップボードにコピー */
function copyClipboard(obj, msg){
  var ua = navigator.userAgent.toLowerCase(); 
  var is_pc_ie  = ( (ua.indexOf('msie') != -1 ) && ( ua.indexOf('win') != -1 ) && ( ua.indexOf('opera') == -1 ) && ( ua.indexOf('webtv') == -1 ) );
  if (is_pc_ie) {
    copytext = obj.createTextRange();
    copytext.execCommand("Copy");
  } else {
    var divcontainer = "flash_setvariables";
    if(!document.getElementById(divcontainer)){
      var divholder = document.createElement("div");
      divholder.id = divcontainer;
      document.body.appendChild(divholder);
    };
    document.getElementById(divcontainer).innerHTML = "";
    var swf = "<embed src='copyClipboard.swf' FlashVars='copytext="+encodeURI(obj.value)+"' width='0' height='0' type='application/x-shockwave-flash'></embed>";
    document.getElementById(divcontainer).innerHTML = swf;
  };
  alert(msg + 'をクリップボードにコピーしました．');
};

/* フラッシュを介して，クリップボードにコピー(警告なし) buttonで呼び出す */
function copyClipboardNoAlert(obj){
  var ua = navigator.userAgent.toLowerCase(); 
  var is_pc_ie  = ( (ua.indexOf('msie') != -1 ) && ( ua.indexOf('win') != -1 ) && ( ua.indexOf('opera') == -1 ) && ( ua.indexOf('webtv') == -1 ) );
  if (is_pc_ie) {
    copytext = obj.createTextRange();
    copytext.execCommand("Copy");
  } else {
    var divcontainer = "flash_setvariables";
    if(!document.getElementById(divcontainer)){
      var divholder = document.createElement("div");
      divholder.id = divcontainer;
      document.body.appendChild(divholder);
    };
    document.getElementById(divcontainer).innerHTML = "";
    var swf = "<embed src='copyClipboard.swf' FlashVars='copytext="+encodeURI(obj.value)+"' width='0' height='0' type='application/x-shockwave-flash'></embed>";
    document.getElementById(divcontainer).innerHTML = swf;
  };
};

function checkEnquete(frm, lang, nopref) {
  if (!lang) {
    lang = '';
  }

  var Namae = frm.txtNamae.value;

  if (Namae != '') {
    Namae = Namae.split('　');
    Namae = Namae.join(' ');

    arrNamae = Namae.split(' ');
    if (arrNamae.length == 1) {
      if (lang == 'english') {
        alert("Please insert a space between your first name and family name.");
      } else if (lang == 'german') {
        alert("Bitte setzen Sie ein Zwischenraumzeichen zwischen Ihren Vornamen und dem Nachnamen.");
      } else if (lang == 'chinese') {
        alert("请在您的名字的姓和名中间留空格");
      } else {
        alert("姓と名の間に１つだけスペースをいれてください．(例：山田 太郎)");
      }
      return false;
    }
    frm.txtNamae.value = Namae;
  }

  if (lang == '') {
    if (frm.txtJuusho && (frm.txtJuusho.value != '')) {
      var stMatch = new RegExp("^(中国|中華|北海道|東京都|(京都|大阪)府|(青森|秋田|岩手|宮城|山形|福島|神奈川|千葉|埼玉|栃木|群馬|茨城|新潟|富山|石川|福井|山梨|長野|岐阜|静岡|愛知|三重|滋賀|兵庫|奈良|和歌山|鳥取|島根|岡山|広島|山口|徳島|香川|愛媛|高知|福岡|佐賀|長崎|大分|熊本|宮崎|鹿児島|沖縄)県)");
      if (!nopref && !frm.txtJuusho.value.match(stMatch)) {
        alert("都道府県は必ずいれてください．");
        return false;
      }
      if (!AlphaNumCheck(frm.txtJuusho, "住所", true)) return false;
      if (checkZipcode(frm.txtJuusho, frm.txtZipcode, 0) == false) {
        if (!confirm("住所の番地が記入されていませんがよろしいですか？")) {
          return false;
        }
      }
    }

    if (frm.txtChouiki && (frm.txtChouiki != '')) {
      // 英数全角を半角に変換
      if (!AlphaNumCheck(frm.txtChouiki, "住所", true)) return false;

      // 住所が郵便番号からの自動代入で入れられた文字列のままである場合, 差し戻す
      if (checkZipcode(frm.txtChouiki, frm.txtZipcode, 1) == false) {
        alert("住所を番地まで入力してください．");
        return false;
      }
    }

    if (!checkTEL(frm.txtTEL, "電話番号")) return false;
    if (!checkTEL(frm.txtFAX, "FAX番号")) return false;
  }

  return true;
}

function checkZipcode(oJuusho, oZipcode, mode)
{
  if ((oJuusho == null) || (oZipcode == null)) {
    return true;
  }

  var val = oZipcode.value;
  var zip = '';

  //
  // get zipcode value
  //
  if (!val) {
    return true;
  }

  for (var i = 0; i < val.length; i++) {
    var c = val.charCodeAt(i);
    if (( c >= 48 ) && ( c <= 57 )) {
      zip += val.charAt(i);
    } else if (( c >= 65296 ) && ( c <= 65305 )) {
      // Zenkaku number
      zip += String.fromCharCode(c - 65248);
    }
  }
  if (zip.length < 7) {
    return true;
  }
  var url = location.protocol + "//" + location.host + "/php/getZipcode.php";
  var req = sendRequest(function(){}, '&zipcode=' + zip, 'GET', url, false);
  if (!req) return true;
  if (!req.responseText) return true;
  var data = eval('(' + req.responseText + ')');
  if (data["Jigyosyo"] == 1) {
    return true;
  }

  if (mode == 0) {
    var juusho = data["Ken"] + data["Shi"] + data["Chouiki"];
    if (oJuusho.value != juusho) {
      return true;
    }
    return false;
  } else {
    if (oJuusho.value != data["Chouiki"]) {
      return true;
    }
    return false;
  }
}

/* 電話番号チェック */
function checkTEL(obj, Msg) {
  if (obj == undefined) {
    return true;
  }
  //全角数字配列
  var char1 = new Array("１","２","３","４","５","６","７","８","９","０","−","－","ー","―","‐","＋");
  //半角数字配列
  var char2 = new Array(1,2,3,4,5,6,7,8,9,0,'-','-','-','-','-','+');  
  while(obj.value.match(/[０-９−]/)){     	//入力データに全角数字がある場合
    for(count = 0; count < char1.length; count++){
      //入力データを全角数字から半角数字に置換する
      obj.value = obj.value.replace(char1[count], char2[count]);
    }
  }
  for (var i = 0; i < obj.value.length; i++) {
    if ("0123456789-+".indexOf(obj.value.charAt(i)) < 0) {
      alert(Msg +  "を正しく入力してください。") ;
      return false ;
    }
  }
  if (obj.value == "") {
    return true;
  } else if (obj.value.match(/-/)) {
    return true;
  } else {
    alert(Msg + "は - で区切ってください。") ;
    return false ;
  }
}

/* キーワード検索へ移動 */
function searchKeyword() {
	var url = "/php/search-keyword.php"
		+ "?referrer=" + escape(window.document.referrer) ;
	location.href = url;
	return ;
};

/* アクセスされた検索単語を抽出 */
function setSWord() {
  var url = location.protocol + "//" + location.host + "/php/getSearchWord.php";
  var req = sendRequest(function(){}, '&ref=' + escape(window.document.referrer), 'GET', url, false);
  if (!req) return true;
  if (!req.responseText) return true;
  var data = eval('(' + req.responseText + ')');
  if (document.getElementById('skeyword')) {
    document.getElementById('skeyword').value = data["SearchWord"];
    document.getElementById('skeyword').style.background = "#FDD";
  }
}

