/*  login.js
 *  20070523 yamazaki
 *
/*--------------------------------------------------------------------------*/

var ACTION_URL_ORG = '';
function doGet(command) {
    ACTION_URL_ORG = document.forms[0].action;
    var url = document.forms[0].action;

    // command
    if (typeof command != 'undefined' && command == null) {
       url = url.concat('?command=');
    } else {
       url = url.concat('?command=' + command);
    }

    // nextCommand
    if (document.forms[0].nextCommand != null && typeof document.forms[0].nextCommand.value != 'undefined') {
       url = url.concat('&nextCommand=' + document.forms[0].nextCommand.value);
    }

    // url
    if (document.forms[0].url != null && typeof document.forms[0].url.value != 'undefined') {
       url = url.concat('&url=' + document.forms[0].url.value);
    }

    // login_url
    if (document.forms[0].login_url != null && typeof document.forms[0].login_url.value != 'undefined') {
       url = url.concat('&login_url=' + document.forms[0].login_url.value);
    }

    // request_url
    if (document.forms[0].request_url != null && typeof document.forms[0].request_url.value != 'undefined') {
       url = url.concat('&request_url=' + document.forms[0].request_url.value);
    }

    // temppass
    if (document.forms[0].temppass != null && typeof document.forms[0].temppass.value != 'undefined') {
       url = url.concat('&temppass=' + document.forms[0].temppass.value);
    }

    // tempcode
    if (document.forms[0].tempcode != null && typeof document.forms[0].tempcode.value != 'undefined') {
       url = url.concat('&tempcode=' + document.forms[0].tempcode.value);
    }

    // custCode
    if (document.forms[0].custCode != null && typeof document.forms[0].custCode.value != 'undefined') {
       url = url.concat('&custCode=' + document.forms[0].custCode.value);
    }

    // regist_cust_code
    if (document.forms[0].regist_cust_code != null && typeof document.forms[0].regist_cust_code.value != 'undefined') {
       url = url.concat('&regist_cust_code=' + document.forms[0].regist_cust_code.value);
    }

    // telno
    if (document.forms[0].telno != null && typeof document.forms[0].telno.value != 'undefined') {
       url = url.concat('&telno=' + document.forms[0].telno.value);
    }

    // mail
    if (document.forms[0].mail != null && typeof document.forms[0].mail.value != 'undefined') {
       url = url.concat('&mail=' + document.forms[0].mail.value);
    }

    // login
    if (document.forms[0].login != null && typeof document.forms[0].login.value != 'undefined') {
       url = url.concat('&login=' + document.forms[0].login.value);
    }

    // regist
    if (document.forms[0].regist != null && typeof document.forms[0].regist.value != 'undefined') {
       url = url.concat('&regist=' + document.forms[0].regist.value);
    }

    // temp
    if (document.forms[0].temp != null && typeof document.forms[0].temp.value != 'undefined') {
       url = url.concat('&temp=' + document.forms[0].temp.value);
    }

    // next
    if (document.forms[0].next != null && typeof document.forms[0].next.value != 'undefined') {
       url = url.concat('&next=' + document.forms[0].next.value);
    }

    // cont
    if (document.forms[0].cont != null && typeof document.forms[0].cont.value != 'undefined') {
       url = url.concat('&cont=' + document.forms[0].cont.value);
    }

    // tab
    if (document.forms[0].tab != null && typeof document.forms[0].tab.value != 'undefined') {
       url = url.concat('&tab=' + document.forms[0].tab.value);
    }

    // failed
    if (document.forms[0].failed != null && typeof document.forms[0].failed.value != 'undefined') {
       url = url.concat('&failed=' + document.forms[0].failed.value);
    }

    if (command == 'logout') {
//alert('logout');
//document.forms[0].method = 'get';
//xDomainDoGet(document.forms[0].action, command);
      document.forms[0].action = url;
      xDomainLogoutMain(0);
    } else if (command == 'xDomainLogin') {
//alert('xDomainLogin');
//xDomainDoGet(document.forms[0].action, command);
      xDomainLoginMain(0);
    } else {
//alert('etc');
      document.forms[0].action = url;
//document.forms[0].method = 'get';
      document.forms[0].submit();
    }
}

/*
 * xDomainLoginMain
 */
var xDomainLoginMainCnt = 0;
function xDomainLoginMain(flag) {
  if (flag == 0) {
    // Init 
    xDomainLoginMainCnt = 0;
    myInterval = setInterval('xDomainLoginMain(1)', 0.05*1000);
  } else if (flag == 1) {
    if (xDomainLoginMainCnt == 4) {
      // Run
      document.forms[0].action = ACTION_URL_ORG;
      document.forms[0].submit();
      // Stop
      clearInterval(myInterval);
      return;
    }
    xDomainLoginMainCnt++;
  }
}

/*
 * xDomainLogoutMain
 */
var xDomainLogoutMainCnt = 0;
function xDomainLogoutMain(flag) {
  if (flag == 0) {
    // Init 
    xDomainLogoutMainCnt = 0;
    myInterval = setInterval('xDomainLogoutMain(1)', 0.05*1000);
  } else if (flag == 1) {
    if (xDomainLogoutMainCnt == 4) {
      // Run
      document.forms[0].submit();
      // Stop
      clearInterval(myInterval);
      return;
    }
    xDomainLogoutMainCnt++;
  }
}

/*
 * 弥生ストア別ドメインログイン|ログアウト対応
 */
function xDomainDoGet(action, command) {
  var hostName = document.location.host;
  var hostName2 = '';
  if (hostName == 'www.yayoi-kk.co.jp') hostName2 = 'yayoi-store.jp';
  if (hostName == 'www-test.yayoi-kk.co.jp') hostName2 = 'www-test.yayoi-store.jp';
  if (hostName == 'www-test2.yayoi-kk.co.jp') hostName2 = 'www-test2.yayoi-store.jp';
  if (hostName == 'yayoi-store.jp') hostName2 = 'www.yayoi-kk.co.jp';
  if (hostName == 'www-test.yayoi-store.jp') hostName2 = 'www-test.yayoi-kk.co.jp';
  if (hostName == 'www-test2.yayoi-store.jp') hostName2 = 'www-test2.yayoi-kk.co.jp';

  var scriptA = document.createElement('script');
  scriptA.charset = 'utf-8';
  scriptA.type = 'text/javascript';
  if (command == 'xDomainLogin'
  &&  typeof document.getElementById('regist_cust_code').value != 'undefined'
  &&  typeof document.getElementById('password').value != 'undefined'
  ) {
    scriptA.src = 'https://' + hostName2 + '/icare/login/login?command=xDomainLogin&accountNum=' + document.getElementById('regist_cust_code').value + '&password=' + document.getElementById('password').value + '&' + getTime();
    document.lastChild.appendChild(scriptA);
  } else if (command == 'logout') {
    scriptA.src = 'http://' + hostName2 + '/icare/login/login?command=logout';
    document.lastChild.appendChild(scriptA);
  }
//alert(scriptA.src);
}

/*
 * Time
 */
function getTime() {
  var dd = new Date();
  var year = dd.getYear();
  if (year < 2000) year += 1900;
  var mon = dd.getMonth() + 1;
  var date = dd.getDate();
  var hours = dd.getHours();
  var minutes = dd.getMinutes();
  var seconds = dd.getSeconds();
  return year.toString() + mon.toString() + date.toString() + hours.toString() + minutes.toString() + seconds.toString();
}

