function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}


function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}


function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



// show summary
function showsum(elem, visible, sum_id) {
  var target = elem;
  if (typeof(elem) == 'object') {
    target = elem.target || elem.srcElement;
  }
  var helper = new divHelper(target);
  var d = document.getElementById(sum_id);
  if (d.innerHTML == ""){
    return;
  }
  if (visible == 1) {
    if (d) {
      if (d.style.display == 'none') {
        helper.show(d.innerHTML);
      }
    }
  } else {
    helper.hide();
    if (d) {
      d.style.display = 'none';
    }
  }
}


// page moving 
function gotop() {
  location.href='http://'+location.host;
}


function goregist() {
  location.href='/user/regist/?ref='+escape(location.href);
}



// url utility
function getq(key) {
  var r = '';
  var re = new RegExp('\\?.*'+key+'=([0-9a-z_\\-%]+)', 'gi');
  var m = re.exec(location.href);
  if (m != undefined) {
    if (m.length > 0) {
      r = m[1];
    }
  }
  return r;
}


// パネルのon/off
function togglePanel(id) {
  var d = document.getElementById(id);
  if (d) {
    if (d.style.display=='') {
      d.style.display='none';
    } else {
      d.style.display='';
    }
  }
}


// hmltエンコード
function htmlencode(str) {
  var encstr = str;
  encstr = encstr.replace("<", "&lt;");
  encstr = encstr.replace(">", "&gt;");
  return encstr;
}


function MenuSwapImage(folder, item) {
  var swapimgfolder = folder;
  var swapitem = item;
  function changeimg(swapimg) {
    var img = document.getElementById(swapitem.imgid)
    var fname = swapimgfolder + swapimg + "." + swapitem.ext;
    img.src = fname;
  };
  MenuSwapImage.prototype.onmouseout=function(){
    changeimg(swapitem.baseimg);
  };
  MenuSwapImage.prototype.onmouseover=function(){
    changeimg(swapitem.swapimg);
  };
}


function connectMenuItems(folder, item_list) {
  var current_item;
  for(var i=0;i<item_list.length;i++) {
    var swapitem = item_list[i];
    var si = new MenuSwapImage(folder, swapitem);
    var elem = document.getElementById(swapitem.elemid);
    if (elem != undefined) {
      var re = new RegExp('^'+elem.href, 'i');
      var m = re.exec(location.href);
      if (m != null) { 
        current_item = swapitem;
      }
      elem.onmouseout = si.onmouseout;
      elem.onmouseover = si.onmouseover;
    }
  }
  if (current_item != undefined) {
    var current_elem = document.getElementById(current_item.elemid);
    current_elem.onmouseout = function(){};
    current_elem.onmouseover = function(){};
    var img = document.getElementById(current_item.imgid)
    if (img != undefined) {
      var fname = folder + current_item.swapimg + "." + current_item.ext;
      img.src = fname;
    }
  }
}


function urlEncode(text){
  var encmsg1 = encodeURI(text);
  var encmsg2;
  var re1 = /\+/gi; var re2 = /\=/gi; var re3 = /\&/gi;
  encmsg2 = encmsg1.replace(re1, "%2B"); //+
  encmsg1 = encmsg2.replace(re2, "%3D"); //=
  encmsg2 = encmsg1.replace(re3, "%26"); //&
  return encmsg2;
}


// HTMLの挿入, docは省略化
function setHtml(id, text, doc){
  var d = document;
  if (arguments.length == 3){
    d = doc;
  }
  var elm = d.getElementById(id);
  if (elm){
    elm.innerHTML = text;
  }
}


// リクエスト for ajax
function requestFile(method, fileName, data, async)
{
  var req = createHttpRequest();
  req.open(method ,fileName ,async);
  req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  req.onreadystatechange = function()
  { 
    if (req.readyState==4)
    { 
      onReqLoaded(req);
    }
  }
  req.send(data);
}


// XMLHttpRequestオブジェクト生成  for ajax
function createHttpRequest(){
  if(window.ActiveXObject){
    try {
      return new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        return new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
        return null;
      }
    }
  } else if(window.XMLHttpRequest){
    return new XMLHttpRequest();
  } else {
    return null;
  }
}


function showConf(msg)
{
  if(confirm(msg)){return true;}else{return false;}
}


function getSelVal(oElem)
{
  var idx = oElem.selectedIndex;
  return oElem.options[idx].value;
}


// デフォルトの前処理
function init() {}


function changeMsgList(oSelect, thread_id)
{
  var idx = oSelect.selectedIndex;
  var ltype = oSelect.options[idx].value;
  location.href='/forum/message.aspx?thid=' + thread_id + '&ltype=' + ltype
}

function getaddr(enddom, startdom, user, islink)
{
  var email = user + '@' + startdom + enddom;
  var link = email;
  if (islink)
    link = '<a href="mailto:'+email+'">'+email+'</a>';
    
  document.write(link);
}


// サブウインドウを表示
function openSubWin(url, target, width, height, topOffset, noRetObj)
{
  winTop = window.screenTop;
  winLeft = window.screenLeft;
  winWidth = window.document.body.clientWidth;
  winHeigth = window.document.body.clientHeight;
  var docLeft = winLeft + winWidth - width;
  var docTop = winTop + topOffset;
  argstr = 'width=' + width + ',height=' + height + ',' +
      'left=' + docLeft + ',top=' + docTop + ',screenX=' + docLeft + ',screenY=' + docTop + ',' +
      'scrollbars=1,resizable=1,status=0,location=0';
  var subwin = window.open(url,target,argstr);
  subwin.focus();
  if (noRetObj){
    return;
  } else {
    return subwin;
  }
}


// おすすめ行追加
function addRowRecom(doc, inputBaseName, cellDataList, headerExists)
{
  var rowCountId = inputBaseName.replace(/:/g,'_');
  var tableId = rowCountId+"_table";
  var inputName = inputBaseName+"_table";
  var oTable = doc.getElementById(tableId);
  var oRowCount = doc.getElementById(rowCountId);
  var rowLen = oRowCount.value;
  if (headerExists)
  {
    rowLen--;
  }
  rowLen++;
  cellDataList[0] = '<input type="hidden" size="4" name="' + createRowControlName(inputName, rowLen, 0) + '" value="' + cellDataList[0] + '" />'+cellDataList[0];
  cellDataList[2] = '<textarea rows="3" cols="60" name="' + createRowControlName(inputName, rowLen, 2) + '"></textarea>';
  cellDataList[3] = '<input type="text" size="2" name="' + createRowControlName(inputName, rowLen, 3) + '" value="' + cellDataList[3] + '" />';
  cellDataList[4] = createDelRowButton();
  addRowCount(oTable, oRowCount, cellDataList, headerExists);
}


// 投票行追加
function addRowVote(doc, tableId, rowCountId, inputName, cellDataList, headerExists, optionList)
{
  var oTable = doc.getElementById(tableId);
  var oRowCount = doc.getElementById(rowCountId);
  var rowLen = oRowCount.value;
  if (headerExists)
  {
    rowLen--;
  }
  rowLen++;
  cellDataList[0] = '<input type="hidden" size="4" name="' + createRowControlName(inputName, rowLen, 0) + '" value="' + cellDataList[0] + '" />' + cellDataList[0];
  cellDataList[1] = '<input type="text" size="30" name="' + createRowControlName(inputName, rowLen, 1) + '" value="' + cellDataList[1] + '" />';
  addRowCount(oTable, oRowCount, cellDataList, headerExists);
}


// カテゴリ行追加
function addRowCategory(doc, inputBaseName, cellDataList, headerExists)
{
  var rowCountId = inputBaseName.replace(/:/g,'_');
  var tableId = rowCountId+"_table";
  var inputName = inputBaseName+"_table";
  var oTable = doc.getElementById(tableId);
  var oRowCount = doc.getElementById(rowCountId);
  var rowLen = oRowCount.value;
  if (headerExists)
  {
    rowLen--;
  }
  rowLen++;
  cellDataList[0] = '<input type="text" size="4" name="' + createRowControlName(inputName, rowLen, 0) + '" value="' + cellDataList[0] + '" />';
  cellDataList[1] = '<input type="text" size="30" name="' + createRowControlName(inputName, rowLen, 1) + '" value="' + cellDataList[1] + '" />';
  cellDataList[2] = '<input type="text" size="4" name="' + createRowControlName(inputName, rowLen, 2) + '" value="' + cellDataList[2] + '" />';
  cellDataList[3] = createDelRowButton();
  addRowCount(oTable, oRowCount, cellDataList, headerExists);
}


// 著者行追加
function addRowAuthor(doc, inputBaseName, cellDataList, headerExists)
{
  var rowCountId = inputBaseName.replace(/:/g,'_');
  var tableId = rowCountId+"_table";
  var inputName = inputBaseName+"_table";
  var oTable = doc.getElementById(tableId);
  var oRowCount = doc.getElementById(rowCountId);
  var rowLen = oRowCount.value;
  if (headerExists)
  {
    rowLen--;
  }
  rowLen++;
  cellDataList[0] = '<input type="text" size="4" name="' + createRowControlName(inputName, rowLen, 0) + '" value="' + cellDataList[0] + '" />';
  cellDataList[1] = '<input type="text" size="30" name="' + createRowControlName(inputName, rowLen, 1) + '" value="' + cellDataList[1] + '" />';
  cellDataList[2] = createSelectControl(createRowControlName(inputName, rowLen, 2), cellDataList[2], "");
  cellDataList[3] = createDelRowButton();
  addRowCount(oTable, oRowCount, cellDataList, headerExists);
}


// add related article row
function addRowArticle(doc, inputBaseName, cellDataList, headerExists)
{
  var rowCountId = inputBaseName.replace(/:/g,'_');
  var tableId = rowCountId+"_table";
  var inputName = inputBaseName+"_table";
  var oTable = doc.getElementById(tableId);
  var oRowCount = doc.getElementById(rowCountId);
  var rowLen = oRowCount.value;
  if (headerExists)
  {
    rowLen--;
  }
  rowLen++;
  cellDataList[0] = '<input type="text" size="4" name="' + createRowControlName(inputName, rowLen, 0) + '" value="' + cellDataList[0] + '" />';
  cellDataList[1] = '<input type="text" size="30" name="' + createRowControlName(inputName, rowLen, 1) + '" value="' + cellDataList[1] + '" />';
  cellDataList[2] = createDelRowButton();
  addRowCount(oTable, oRowCount, cellDataList, headerExists);
}


// 行コントロールの名前を作成
function createRowControlName(inputName, rowLen, cellIndex)
{
  return inputName + ':_ctl' + rowLen + ':_ctl' + cellIndex + ':row';
}

// 行の追加
function addRowCount(oTable, oRowCount, cellDataList, headerExists)
{
  var oRow = oTable.insertRow(oTable.rows.length);
  var oCell;
  for(i = 0 ; i < cellDataList.length; i++)
  {
    oCell = oRow.insertCell(i);
    oCell.innerHTML = cellDataList[i];
  }
  var rowLen = oRowCount.value;
  rowLen++
  oRowCount.value = rowLen;

}


//クリックした行を削除する
function deleteRow(element) {
  var e;
  if (window.event == undefined) {
    e = arguments.callee.caller.arguments[0]
  } else {
    e = window.event;
  }
  var elem = e.target || e.srcElement; // input
  var tr = elem.parentNode.parentNode;
  var tbl = tr.parentNode.parentNode;
  tbl.deleteRow(tr.rowIndex);
}


// delete row button
function createDelRowButton() {
  var btn = '<input type="button" value="削除" onclick="deleteRow();">';
  return btn;
}


// Option要素配列からSelectメニューの作成
function createSelectControl(controlName, optionList, scriptText)
{
  var onChangeScript = '';
  if (scriptText != ""){
    onChangeScript = 'onchange="'+scriptText+'"';
  }
  var oSelHtml = '<select name="'+controlName+'"'+onChangeScript+'>';
  for(i = 0 ; i < optionList.length; i++)
  {
    var optionItem = optionList[i].split("=");
    oSelHtml += '<option value="'+optionItem[0]+'">'+optionItem[1]+'</option>';
  }
  oSelHtml += '</select>';
  return oSelHtml;
}


// for debug
function dumpobj(obj) {
  for(var prop in obj) {
    try {
      window.document.writeln(prop+' = '+obj[prop]+'<br>');
    } catch(e) {
    }
  }
}


function gologin() {
  location.href='https://sems.shoeisha.com/users/login/careerzine?ref='+escape(location.href)
}


function goprint() {
  var mode = getq('mode');
  var u = location.pathname;
  if (mode != 'print') {
    u = location.pathname + '?mode=print';
  }
  location.href = u;
}


function ov_click_count(v1, v2, v3) {
  var s=s_gi(s_account);
  s.linkTrackVars='products,eVar19,eVar20,events';
  s.linkTrackEvents='event4';
  s.events='event4';
  s.products = 'ov;' + v1;
  s.eVar19= v2;
  s.eVar20= v3;
  s.tl(this,'o','Overture Click');
}


// ---- SiteCatalyt Click Events ----//

// <a href="#" 
//    ref="sc" 
//    scCategory="AreaTest" 
//    scVars="14" 
//    scVals="Data" 
//    scEvents="5"  
//    scProducts="%TITLE%" 
//    scLabel="test">Test</a>
// 
//    
//    If you'd like to omit value of attributes 
//    in scProducts,scLabel,scCategory and scVals , 
//    you can set the following value to the attributes.
//    
//    %TITLE% = this anchor text
//    %LOC% = location.href(page url)
//    %HREF% = this anchor href
//    

$(function(){
  $("a").click(function () {
    var ref = $(this).attr("ref");
    if (typeof(ref) == 'undefined' || ref != 'sc') return;
    
    // event
    var events = $(this).attr("scEvents");
    if(typeof(events) == 'undefined'){
        events = '';
    } else {
      events = events.split(',');
    }

    // category
    var category = $(this).attr("scCategory");
    if(typeof(category) == 'undefined'){
        category = '';
    } 
    category = rep_val(category, $(this));
    
    // products
    var products = $(this).attr("scProducts");
    if(typeof(products) == 'undefined'){
        products = $(this).text();
    }
    products = rep_val(products, $(this));

    // eVar
    var eVars = $(this).attr("scVars");
    if(typeof(eVars) == 'undefined'){
        eVars = '';
    } else {
      eVars = eVars.split(',');
    }
    
    // eVal
    var eVals = $(this).attr("scVals");
    if(typeof(eVals) == 'undefined'){
        eVals = '';
    } else {
      eVals = eVals.split(',');
    }
    
    // LABEL
    var linkname = $(this).attr("scLabel");
    if(typeof(linkname) == 'undefined'){
        linkname = $(this).text();
    }
    linkname = rep_val(linkname, $(this));
    
    var s=s_gi(s_account);

    // Set linkTrackVars
    var linkTrackVars = '';
    for ( var i=0; i < eVars.length  ; i++  ){
      linkTrackVars +=  "eVar" + eVars[i];
      if (i != eVars.length -1 ){
         linkTrackVars += ",";
      }
    }
    if (products){
       if (linkTrackVars){
          linkTrackVars += ",products";
       } else {
          linkTrackVars += "products";
       }
    }
    if (events.length > 0){
       if (linkTrackVars){
          linkTrackVars += ",events";
       } else {
          linkTrackVars += "events";
       }
    }
    s.linkTrackVars = linkTrackVars  ;
    
    // Set linkTrackEvents
    var linkTrackEvents = '';
    for ( var i=0; i < events.length  ; i++  ){
      linkTrackEvents +=  "event" + events[i];
      if (i != eVars.length -1 ){
         linkTrackVars += ",";
      }
    }
    s.linkTrackEvents = linkTrackEvents;
    // Set events
    s.events = linkTrackEvents;
    // Set products
    s.products = category + ";" + products;
    
    // Set eVar99
    if ( eVars && eVals ){
       for ( var i=0; i < eVars.length  ; i++  ){
            eVals[i] = rep_val(eVals[i], $(this));
            eval( "s.eVar" + eVars[i] + "='" + eVals[i] + "'");
       }
       
    }
    
    s.tl(this,'o',linkname);
  });
  
  function rep_val(val, elem) {
    if (val) {
      val = val.replace('%TITLE%', elem.text(), 'g');
      val = val.replace('%HREF%', elem.attr('href'), 'g');
      val = val.replace('%LOC%', location.href, 'g');
    }
    return val;
  }
});


function sc_checkout(products){
  var s=s_gi(s_account);
  s.linkTrackVars='products,events';
  s.linkTrackEvents='scCheckout'; 
  s.events='scCheckout';
  s.products = products;
  s.tl(this,'o','Checkout Click');
}


function sc_purchase(products){
  var s=s_gi(s_account);
  s.linkTrackVars='products,events';
  s.linkTrackEvents='purchase'; 
  s.events='purchase';
  s.products = products;
  s.tl(this,'o','Purchase Click');
}

