// Copyright 2003-2004, Cross Language Inc., All Rights Reserved.
//
// Project: webtranser
// File:    $Id: login.js,v 1.9 2004/04/08 12:01:38 brodie Exp $
// Author:  Brodie Thiesfield
// Summary: Javascript code library for use by the login page
//
// Note: the md5.js javascript file must be included too.


// ----------------------------------------------------------------------------
// initPage()
//  Initialize the user interface of this page.
//
// Parameters:
//  none
//
// Return:
//  none
//
function initPage()
{
  // set the focus to the appropriate field
  var form = document.formAuth;
  if ( form.ctw_user.value == "" )
  {
    form.ctw_user.focus();
  }
  else
  {
    form.ctw_pass.focus();
  }
}
  
// ----------------------------------------------------------------------------
// submitPage()
//  Implement the CHAP login security. We calculate the final hash of the 
//  password using the challenge string. The username and hashed password are 
//  then submitted via a redirection. 
//
// Parameters:
//  a_strSubmitPage   string    url to submit this form to
//
// Return:
//  none
//
function submitPage( a_strSubmitPage )
{
  var form = document.formAuth;
  
  // calculate our password hash
  var strUser      = encodeURIComponentNew( form.ctw_user.value );
  var strPass      = form.ctw_pass.value;
  var strPassHash  = hex_md5(strPass);
  var strChallenge = form.ctw_challenge.value;
  var strFinalHash = hex_md5( strChallenge + strPassHash );
  var strRemember  = form.ctw_remember.checked ? '1' : '0';
// alert( 'challenge: '+strChallenge);
// alert( 'strFinalHash: '+strFinalHash);
  // generate our new url
  var strUrl = a_strSubmitPage+"?ctw_user="+strUser+"&ctw_pass="+strFinalHash+"&ctw_remember="+strRemember;
  
  // submit our form via this redirect
  location.href = strUrl;
  
  // don't allow this function to be run again
  form.onsubmit = null;

  // abort normal form submission
  return false;
}

// ----------------------------------------------------------------------------
// convertToUtf8()
//  Convert a string from unicode to a UTF-8 representation.
//
// Parameters:
//  strResult   string    string to convert to UTF-8
//
// Return:
//  string      Input string converted to UTF-8
//
function convertToUtf8( a_strSource ) 
{
    var strResult = "";
    var c, s;
    var i = 0;

    while( i < a_strSource.length ) 
    {
        c = a_strSource.charCodeAt(i++);
        
        // handle UTF-16 surrogates
        if ( c >= 0xDC00 && c < 0xE000 ) 
            continue;
        if ( c >= 0xD800 && c < 0xDC00 ) 
        {
            if ( i >= a_strSource.length ) 
                continue;
            s = a_strSource.charCodeAt( i++ );
            if ( s < 0xDC00 || c >= 0xDE00 ) 
                continue;
            c = ( (c - 0xD800) << 10 ) + ( s - 0xDC00 ) + 0x10000;
        }
        
        // output value
        if ( c < 0x80 ) 
            strResult += String.fromCharCode( c );
        else if ( c < 0x800 ) 
            strResult += String.fromCharCode( 0xC0 + (c >> 6), 0x80 + (c & 0x3F) );
        else if ( c < 0x10000 ) 
            strResult += String.fromCharCode( 0xE0 + (c >> 12), 0x80 + (c >> 6 & 0x3F), 0x80 + (c & 0x3F) );
        else 
            strResult += String.fromCharCode( 0xF0 + (c >> 18), 0x80 + (c >> 12 & 0x3F), 0x80 + (c >> 6 & 0x3F), 0x80 + (c & 0x3F));
    }
    
    return strResult;
}

// ----------------------------------------------------------------------------
// convertToHex()
//  Convert an integer in the range 0x00 to 0xFF to it's hex representation.
//  Note that values outside of this range will have the upper bytes discarded.
//
// Parameters:
//  a_nChar     integer     Character to convert to hex
//
// Return:
//  string      Hex representation of character
//
function convertToHex( a_nChar ) 
{
    var strHexChars = "0123456789ABCDEF";
    return strHexChars.charAt((a_nChar >> 4) & 0xF) + strHexChars.charAt(a_nChar & 0xF);
}

// ----------------------------------------------------------------------------
// encodeURIComponentNew()
//  Convert a value to an URI encoded string. All unicode characters are 
//  converted to their UTF-8 representation. If encodeURIComponent is available
//  natively in the browser than that is used, otherwise a javascript version
//  of the function is used.
//
// Parameters:
//  a_strSource     string      Source string to encode
//
// Return:
//  string      Encoded version of the string
//
function encodeURIComponentNew( a_strSource )
{
    if ( typeof encodeURIComponent == "function" )
    {
        // Use JavaScript built-in function
        // IE 5.5+ and Netscape 6+ and Mozilla
        return encodeURIComponent( a_strSource );
    }
    else
    {
        // Need to mimic the JavaScript version
        // Netscape 4 and IE 4 and IE 5.0
        var strValidURIChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
        var strResult = "";
        
        var strSourceUtf8 = convertToUtf8( a_strSource );
        var c;
        for ( var i = 0; i < strSourceUtf8.length; i++ ) 
        {
            c = strSourceUtf8.charAt(i);
            if ( strValidURIChars.indexOf(c) == -1 )
                strResult += "%" + convertToHex(c);
            else
                strResult += c;
        }
        
        return strResult;
    }
}

function submitForm()
{
  var tgtWinName;
  var oForm;

  if( w_url == null ){
    w_url = window.open("", "bb_url", "directories=no,location=no,menubar=no,"
                                      +"resizable=yes,scrollbars=yes,status=no,toolbar=yes");
  }else{
    if( w_url.closed ){
    w_url = window.open("", "bb_url", "directories=no,location=no,menubar=no,"
                                      +"resizable=yes,scrollbars=yes,status=no,toolbar=yes");
    }else{
      w_url.name = "bb_url";
    }
    tgtWinName = "bb_url";
  }
  // 一番手前にウインドウを表示
  w_url.focus();

  // set Form property
  oForm = document.getElementById('formAction_keyword');
  oForm.target = "bb_url";

  // submit()の為に強制的に文字コードを変更
  document.charset='UTF-8'; 

}



function keyCheck(){
 var icount = document.formAction_keyword.keywords.value.length;
 var i = 0;
 var j = 0;
 var stemp = document.formAction_keyword.keywords.value;
 for(;i <icount;){
  if(stemp.substring(i,i+1) == " "||stemp.substr(i,i+1) == "　"){
    j++
  }
  i ++;
 }
 if(j == icount){
    alert("キーワードを入力してください。");
    document.formAction_keyword.keywords.focus();
    return false;
 }
 submitForm();
  return true;
}
