var divname1 = "enquiry-background"; // 背景
var divname2 = "enquiry-form";       // 入力フォーム
var divname3 = "enquiry-confirm";    // 確認画面
var divname4 = "enquiry-thankyou";   // 完了画面

var obj1  = null;
var obj2  = null;
var obj3  = null;
var obj4  = null;

// 現在のウィンドウサイズ
var nWinWidth;
var nWinHeight;

// コンテンツサイズ
var nContentsWidth = 1000;
var nContentsHeight = 910;

// 問合せフォームの背景サイズ
var nCoveredWidth;
var nCoveredHeight;

// 試験会場情報
var atcid = 0;
var atcname = '';

window.onload = function(){
  nWinWidth = getBrowserWidth();
  nWinHeight = getBrowserHeight();

  nCoveredWidth = getCoveredWidth();
  nCoveredHeight = getCoveredHeight();
}

window.onresize = function(){
  nWinWidth = getBrowserWidth();
  nWinHeight = getBrowserHeight();

  nCoveredWidth = getCoveredWidth();
  nCoveredHeight = getCoveredHeight();
  if ( obj1 )
  {
    obj1.style.width = nCoveredWidth;
    obj1.style.height = nCoveredHeight;
  }
}

function div_flip(id,name)
{
  obj1  = document.getElementById(divname1);
  obj2  = document.getElementById(divname2);
  obj3  = document.getElementById(divname3);
  obj4  = document.getElementById(divname4);

  atcid = id;
  atcname = name;

  var strformhtml = createForm(atcid,atcname);

  obj2.innerHTML = strformhtml;
  vflg9 = obj1.style.display;
  if (vflg9 == 'none')
  {
    obj1.style.width = nCoveredWidth;
    obj1.style.height = nCoveredHeight;

    obj1.style.display = 'block';
    obj2.style.display = 'block';
  }else{
    close();
  }

  return false;
}

function createForm(atcid,atcname)
{
  var html = new Array();

  html[html.length] = '<form name="form1" method="get" action="#" onsubmit="javascript:return check()">';
  html[html.length] = '<table border="0" cellpadding="5px" cellspacing="0" width="480px">';

  html[html.length] = '<tr><td>';
  html[html.length] = '<div style="width:480px; background-color:#FFFFFF; border:1px solid silver;">';
  html[html.length] = '<table>';
  html[html.length] = '<tr><td>【個人情報の利用目的】<br></td></tr>';
  html[html.length] = '<tr><td align="right">株式会社オデッセイコミュニケーションズ<br>個人情報保護管理者<br></td></tr>';
  html[html.length] = '<tr><td>';
  html[html.length] = '入力いただいた個人情報は、お客様からの問合せに関する返信・連絡のみに利用します。<br>';
  html[html.length] = 'また、問合せへの対応や資料送付を目的に、' + atcname + 'に提供します。<br>';
  html[html.length] = '個人情報に関する基本方針やお問合せ窓口は、弊社の<a href="http://www.odyssey-com.co.jp/legal_policy.html" target="_blank">プライバシーポリシー</a>をご覧ください。<br>';
  html[html.length] = '※当フォームから送信された場合は、上記利用目的に同意されたものとみなします。';
  html[html.length] = '</td></tr>';
  html[html.length] = '</table>';
  html[html.length] = '</div>';
  html[html.length] = '</td></tr>';

  html[html.length] = '<tr><td>&nbsp;</td></tr>';
  html[html.length] = '<tr><td style="font-size:12pt;">' + atcname + 'への問合せ</td></tr>';
  
  html[html.length] = '<tr><td height="30px" style="padding:5px;">▼&nbsp;質問と連絡先を入力してください&nbsp;&nbsp;<font style="color:#FF6600; font-size:8pt;">◎必須</font></td></tr>';

  html[html.length] = '<tr><td>';
  html[html.length] = '<table width="480px" border="0" bgcolor="gray" cellpadding="5px" cellspacing="1">';
  
  html[html.length] = '<tr><td style="background-color:#CCCCFF; padding:5px; vertical-align:middle;">質問：&nbsp;<font style="color:#FF6600; font-size:8pt;">◎</font></td>';
  html[html.length] = '<td style="background-color:#FFFFFF; padding:5px;">';
  html[html.length] = '<input type=checkbox name="qlist1[]" value="1" id ="q1">&nbsp;<label for="q1">受験について</label>&nbsp;<br>';
  html[html.length] = '<input type=checkbox name="qlist1[]" value="2" id ="q2">&nbsp;<label for="q2">講座について</label>&nbsp;<br>';
  html[html.length] = '<input type=checkbox name="qlist1[]" value="3" id ="q3">&nbsp;<label for="q3">その他</label>';
  html[html.length] = '</td></tr>';

  html[html.length] = '<tr><td style="background-color:#CCCCFF; padding:5px; vertical-align:middle;">質問の詳細：&nbsp;<font style="color:#FF6600; font-size:8pt;">◎</font></td><td style="background-color:#FFFFFF; padding:5px;"><textarea name="qdetail" style="width:350px; height:120px;" cols="65" rows="5"></textarea></tr>';
  html[html.length] = '<tr><td style="background-color:#CCCCFF; padding:5px;">氏名：&nbsp;<font style="color:#FF6600; font-size:8pt;">◎</font></td><td style="background-color:#FFFFFF; padding:5px;">姓：&nbsp;<input type="text" name="lastname" size="25">&nbsp;&nbsp;&nbsp;名：&nbsp;<input type="text" name="firstname" size="25"></td></tr>';
  
  html[html.length] = '<tr><td style="background-color:#CCCCFF; padding:5px; vertical-align:middle;">メールアドレス：&nbsp;<font style="color:#FF6600; font-size:8pt;">◎</font></td>';
  html[html.length] = '<td style="background-color:#FFFFFF; padding:5px;"><input type="text" name="email" size="50"><br><input type="text" name="email2" size="50" style="margin: 5px 0px 0px;">&nbsp;（確認用）</td></tr>';

  html[html.length] = '<tr><td style="background-color:#CCCCFF; padding:5px;">電話番号：</td><td style="background-color:#FFFFFF; padding:5px;"><input type="text" name="tel" size="50"></td></tr>';

  html[html.length] = '</table>';
  html[html.length] = '</td></tr>';

  html[html.length] = '<tr><td height="80px" align="center" style="vertical-align: middle;"><input type="submit" value="  確認画面へ  "></td></tr>';
  html[html.length] = '<tr><td align="right"><a href="javascript:close();">閉じる</a></td></tr>';

  html[html.length] = '</table>';
  
  html[html.length] = '</form>';

  return html.join('');
}

function confirm()
{
  var tmphtml   = '';
  var strqlist1 = '';
  var html   = new Array();

  for( i=0; i<document.form1.elements["qlist1[]"].length; i++)
  {
    if(document.form1.elements["qlist1[]"][i].checked)
    {
      if ( strqlist1 == '' )
      {
        strqlist1 += ((document.form1.elements["qlist1[]"][i].value == '1')? '受験について': ((document.form1.elements["qlist1[]"][i].value == '2')? '講座について': ((document.form1.elements["qlist1[]"][i].value == '3')? 'その他': '')));
      }
      else
      {
        strqlist1 += '<br>' + ((document.form1.elements["qlist1[]"][i].value == '1')? '受験について': ((document.form1.elements["qlist1[]"][i].value == '2')? '講座について': ((document.form1.elements["qlist1[]"][i].value == '3')? 'その他': '')));
      }

      if ( tmphtml == '' )
      {
        tmphtml += document.form1.elements["qlist1[]"][i].value;
      }
      else
      {
        tmphtml += ',' + document.form1.elements["qlist1[]"][i].value;
      }
    }
  }

  html[html.length] = '<table border="0" cellpadding="5px" cellspacing="0" width="480px">';

  html[html.length] = '<tr><td style="font-size:12pt;">' + atcname + 'への問合せ</td></tr>';
  html[html.length] = '<tr><td height="30px" style="padding:5px;">▼&nbsp;入力内容を確認してください&nbsp;&nbsp;</td></tr>';

  html[html.length] = '<tr><td>';
  html[html.length] = '<table border="0" bgcolor="gray" cellpadding="5px" cellspacing="1">';

  html[html.length] = '<tr><td width="115px" style="background-color:#CCCCFF; padding:5px; vertical-align:middle;" nowrap>質問：</td><td width="365px" style="background-color:#FFFFFF; padding:5px;">' + strqlist1 + '</td></tr>';
  html[html.length] = '<tr><td width="115px" style="background-color:#CCCCFF; padding:5px; vertical-align:middle;" nowrap>質問の詳細：</td><td style="background-color:#FFFFFF; padding:5px;"><div style="height:350px; overflow:auto;">' + newline_escape( html_escape( document.form1.qdetail.value ) ) + '</div></td></tr>';
  html[html.length] = '<tr><td width="115px" style="background-color:#CCCCFF; padding:5px;" nowrap>氏名：</td><td style="background-color:#FFFFFF; padding:5px;">' + html_escape( document.form1.lastname.value ) + '&nbsp;' + html_escape( document.form1.firstname.value ) + '</td></tr>';
  html[html.length] = '<tr><td width="115px" style="background-color:#CCCCFF; padding:5px;" nowrap>メールアドレス：</td><td style="background-color:#FFFFFF; padding:5px;">' + document.form1.email.value + '</td></tr>';
  html[html.length] = '<tr><td width="115px" style="background-color:#CCCCFF; padding:5px;" nowrap>電話番号：</td><td style="background-color:#FFFFFF; padding:5px;">' + document.form1.tel.value + '</td></tr>';

  html[html.length] = '</table>';
  html[html.length] = '</td></tr>';
  
  html[html.length] = '<tr>';
  html[html.length] = '<td height="80px" align="center" style="vertical-align: middle;">';
  html[html.length] = '<button onclick="javascript:return connect();">&nbsp;&nbsp;送信&nbsp;&nbsp;</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  html[html.length] = '<button onclick="javascript:return back();">&nbsp;&nbsp;修正&nbsp;&nbsp;</button>';
  html[html.length] = '</td>';
  html[html.length] = '</tr>';
  
  html[html.length] = '<tr><td>&nbsp;</td></tr>';
  
  html[html.length] = '</table>';

  obj2.style.display = 'none';

  obj3.innerHTML = '';
  obj3.innerHTML = html.join('');
  obj3.style.display = 'block';
}

function back()
{
  obj3.style.display = 'none';
  obj2.style.display = 'block';
}

function close()
{
  obj2.innerHTML = '';
  obj3.innerHTML = '';
  obj4.innerHTML = '';

  obj2.style.display = 'none';
  obj3.style.display = 'none';
  obj4.style.display = 'none';
  obj1.style.display = 'none';
}

function check()
{
  var flg = false;
  for( i=0; i<document.form1.elements["qlist1[]"].length; i++)
  {
    if(document.form1.elements["qlist1[]"][i].checked)
    {
      flg = true;
      break;
    }
  }
  if (!flg)
  {
    alert("質問を選択してください。");
    return false;
  }

  if (document.form1.qdetail.value == '')
  {
    alert("質問の詳細を入力してください。");
    document.form1.qdetail.focus();
    return false;
  }
  else
  {
    var str = document.form1.qdetail.value;
    if (str.length > 750)
    {
      alert("質問の詳細は、750文字以内で入力してください。");
      document.form1.qdetail.focus();
      return false;
    }
  }

  if (document.form1.lastname.value == '' || document.form1.firstname.value == '')
  {
    alert("姓名を入力してください。");
    document.form1.lastname.focus();
    return false;
  }
  else
  {
    document.form1.lastname.value.replace(/ /g,"");
    document.form1.firstname.value.replace(/ /g,"");
  }

  if (document.form1.email.value == '')
  {
    alert("メールアドレスを入力してください。");
    document.form1.email.focus();
    return false;
  }
  else
  {
    if(document.form1.email.value.match(/^[a-zA-Z0-9_\.\-]+?@[A-Za-z0-9_\.\-]+$/))
    {
      document.form1.email.value.replace(/ /g,"");
    }else{
      alert("メールアドレスの指定に誤りがあります。入力し直してください。");
      document.form1.email.focus();
      return false;
    }
  }

  if (document.form1.email2.value == '')
  {
    alert("メールアドレスをもう一度入力してください。");
    document.form1.email2.focus();
    return false;
  }
  else
  {
    if(document.form1.email2.value.match(/^[a-zA-Z0-9_\.\-]+?@[A-Za-z0-9_\.\-]+$/))
    {
      document.form1.email2.value.replace(/ /g,"");
    }else{
      alert("メールアドレス（確認）の指定に誤りがあります。入力し直してください。");
      document.form1.email2.focus();
      return false;
    }
  }

  if (document.form1.email.value != document.form1.email2.value)
  {
    alert("メールアドレスが一致しません。");
    document.form1.email2.focus();
    return false;
  }

  if (document.form1.tel.value != '')
  {
    if (document.form1.tel.value.match(/^[0-9]+\-[0-9]+\-[0-9]+$/))
    {
      document.form1.tel.value.replace(/ /g,"");
    }
    else
    {
      alert("電話番号の指定に誤りがあります。数字（及び -(ハイフン)）で入力し直してください。");
      document.form1.tel.focus();
      return false;
    }
  }

  confirm();
  
  return false;
}

function connect()
{
  obj3.style.display = 'none';
  
  // 送信
  var strqlist1 = '';
  for( i=0; i<document.form1.elements["qlist1[]"].length; i++)
  {
    if(document.form1.elements["qlist1[]"][i].checked)
    {
      if (i==0)
      {
        strqlist1 += "qlist1[]=" + encodeURI(document.form1.elements["qlist1[]"][i].value);
      }
      else
      {
        strqlist1 += "&qlist1[]=" + encodeURI(document.form1.elements["qlist1[]"][i].value);
      }

    }
  }

  strurl = "https://apply.odyssey-com.co.jp/enquiry.php?" + strqlist1 + "&qdetail=" + encodeURI(document.form1.qdetail.value) + "&email=" + encodeURI(document.form1.email.value) + "&tel=" + encodeURI(document.form1.tel.value) + "&lastname=" + encodeURI(document.form1.lastname.value) + "&firstname=" + encodeURI(document.form1.firstname.value) + "&atcid=" + encodeURI(atcid);

  obj4.innerHTML = '手続き中です。しばらくお待ちください。';
  obj4.style.display = 'block';

  try{
    var originalScriptNode = document.getElementById('enquiry-import');
    var newScriptNode = document.createElement('script');
    newScriptNode.src = strurl + '&callback=callbackfunc';
    newScriptNode.charset = 'utf-8';
    newScriptNode.id = 'enquiry-import';
    originalScriptNode.parentNode.replaceChild(newScriptNode, originalScriptNode);
  }catch(e){
    var html = new Array();

    html[html.length] = '<table border="0" cellpadding="5px" cellspacing="0" width="480px">';
    html[html.length] = '<tr><td style="padding:5px;">ただ今、サーバが大変混み合っています。<br>しばらく時間をおいて再度お試しください。</td></tr>';
    html[html.length] = '<tr><td height="100px" align="center" style="vertical-align: middle;">';
    html[html.length] = '<a href="javascript:close();">閉じる</a>';
    html[html.length] = '</td></tr>';
    html[html.length] = '</table>';
    obj4.innerHTML = html.join('');
    obj4.style.display = 'block';
  }
}

function callbackfunc(msg1,msg2)
{
  var html = new Array();
  
  if ( msg1 != '' )
  {
    html[html.length] = '<table border="0" cellpadding="5px" cellspacing="0" width="480px">';
    html[html.length] = '<tr><td style="padding:5px;">';
    html[html.length] = msg1;
    html[html.length] = '</td></tr>';
    html[html.length] = '<tr><td height="100px" align="center" style="vertical-align: middle;">';
    html[html.length] = '<a href="javascript:back();">戻る</a>';
    html[html.length] = '</td></tr>';
    html[html.length] = '<tr><td align="right">';
    html[html.length] = '<a href="javascript:close();">閉じる</a>';
    html[html.length] = '</td></tr>';
    html[html.length] = '</table>';
    obj4.innerHTML = html.join('');
    return false;
  }

  if ( msg2 != '' )
  {
    html[html.length] = '<table border="0" cellpadding="5px" cellspacing="0" width="480px">';
    html[html.length] = '<tr><td style="padding:5px;">';
    html[html.length] = msg2;
    html[html.length] = '</td></tr>';
    html[html.length] = '<tr><td height="100px" align="center" style="vertical-align: middle;">';
    html[html.length] = '<a href="javascript:close();">閉じる</a>';
    html[html.length] = '</td></tr>';
    html[html.length] = '</table>';
    obj4.innerHTML = html.join('');
    return false;
  }
  else
  {
    html[html.length] = '<table border="0" cellpadding="5px" cellspacing="0" width="480px">';
    html[html.length] = '<tr><td align="center" style="padding:5px;">';
    html[html.length] = 'お問合せが完了しました。';
    html[html.length] = '</td></tr>';
    
    html[html.length] = '<tr><td height="30px" style="padding:5px;"></td></tr>';
    
    html[html.length] = '<tr><td align="center" style="padding:5px;">';
    html[html.length] = atcname;
    html[html.length] = 'からのご連絡をお待ちください。';
    html[html.length] = '</td></tr>';
    
    html[html.length] = '<tr><td height="70px" style="padding:5px;"></td></tr>';
    
    html[html.length] = '<tr><td align="right" style="padding:5px;">';
    html[html.length] = '<font style="font-size:10pt;"><a href="http://www.odyssey-com.co.jp/" target="_blank">株式会社オデッセイコミュニケーションズ</a></font><br>';
    html[html.length] = '東京都千代田区丸の内3-3-1&nbsp;新東京ビル<br>';
    html[html.length] = 'TEL：03-5293-1881（平日10:00-18:00）<br>';
    html[html.length] = 'EMail：mail@odyssey-com.co.jp<br>';
    html[html.length] = 'URL：http://www.odyssey-com.co.jp/<br>';
    html[html.length] = '</td></tr>';
    
    html[html.length] = '<tr><td height="50px" style="padding:5px;"></td></tr>';
    
    html[html.length] = '<tr><td align="right" style="vertical-align: middle;">';
    html[html.length] = '<a href="javascript:close();">閉じる</a>';
    html[html.length] = '</td></tr>';

    html[html.length] = '</table>';
    obj4.innerHTML = html.join('');
    return true;
  }
}

function getBrowserWidth()
{
  if ( window.innerWidth ) {
      return window.innerWidth;
  }
  else if ( document.documentElement && document.documentElement.clientWidth != 0 ) {
      return document.documentElement.clientWidth;
  }
  else if ( document.body ) {
      return document.body.clientWidth;
  }
  return 0;
}

function getBrowserHeight()
{
  if ( window.innerHeight ) {
      return window.innerHeight;
  }
  else if ( document.documentElement && document.documentElement.clientHeight != 0 ) {
      return document.documentElement.clientHeight;
  }
  else if ( document.body ) {
      return document.body.clientHeight;
  }
  return 0;
}

function getCoveredWidth()
{
  if (nWinWidth > nContentsWidth)
  {
    return nWinWidth;
  }
  else
  {
    return nContentsWidth;
  }
}

function getCoveredHeight()
{
  if (nWinHeight > nContentsHeight)
  {
    return nWinHeight;
  }
  else
  {
    return nContentsHeight;
  }
}

function html_escape(str)
{
  str = str.replace(/&/g, "&amp;");
  str = str.replace(/"/g, "&quot;");
  str = str.replace(/>/g, "&gt;");
  str = str.replace(/</g, "&lt;");
  return str;
}

function newline_escape(str)
{
  str = str.replace(/\r\n/g, "<br>");
  str = str.replace(/(\n|\r)/g, "<br>");
  return str;
}

