function check_necessary( doc )
{
  var field = new Array();
  var number_error = new Array();
  var mailaddr_error = new Array();
  var elements = doc.getElementsByTagName( 'input' );
  for( i = 0; i < elements.length; ++i )
  {
    var elem = elements[i];
    var text = elem.getAttribute( 'gt:necessary' );
    if( ! text )
      continue;
    text = '(' + text +')';
    var attr = eval( text );
    var str = elem.value;
    str = str.trim();
    
    if( attr && attr.necessary == 1 && attr.type == 'file' )
    {
      var f_name = elem.name;
      var del_field = doc.getElementById( 'file_delete['+f_name+']' )
      if( del_field )
      {
        if( del_field.checked )
        {
          if( str.length == 0 )
          {
            field[field.length] = attr.name;
          }
        }
      }
      else
      {
        if( str.length == 0 )
        {
          field[field.length] = attr.name;
        }
      }
    }
    else if( attr && attr.necessary == 1 )
    {
      if( str.length == 0 )
      {
        field[field.length] = attr.name;
      }
      if( attr.type == 'number' )
      {
        if( isNaN( str ) )
          number_error[number_error.length] = attr.name;
      }
      else if( attr.type == 'email' )
      {
        if( ! check_mailaddress( elem.id ) )
          mailaddr_error[mailaddr_error.length] = attr.name;
      }
    }
    else if( attr && attr.necessary == 0 && attr.type == 'number' )
    {
      if( isNaN( str ) )
        number_error[number_error.length] = attr.name;
    }
  }

  var fields = __check_field( doc.getElementsByTagName( 'textarea' ) );
  if( fields.length > 0 )
    field = field.concat( fields );
  fields = __check_field( doc.getElementsByTagName( 'file' ) );
  if( fields.length > 0 )
    field = field.concat( fields );

  msg = ''
  if( field.length > 0 )
  {
    msg += "以下の必須項目が入力されていません。\r\n";
    for( i = 0; i < field.length ; ++ i )
    {
      msg += '　『' + field[i] + "』\r\n"; 
    }
    
  }
  if( number_error.length > 0 )
  {
    msg += "以下の項目に数値以外が入力されています。\r\n";
    for( i = 0; i < number_error.length ; ++ i )
    {
      msg += '　『' + number_error[i] + "』\r\n"; 
    }
  }
  if( mailaddr_error.length > 0 )
  {
    msg += "以下の項目のメールアドレスが不正です。\r\n";
    for( i = 0; i < mailaddr_error.length ; ++ i )
    {
      msg += '　『' + mailaddr_error[i] + "』\r\n"; 
    }
  }
  
  if( msg.length > 0 )
  {
    alert( msg );
    return false;
  }
  return true;
}

function check_mailaddress( id )
{
  var address = document.getElementById( id ).value;
  if( ! address ) return false;
  
  address = address.trim();
  
  if( address.match(/^[\w!#\$%&\'\*\+\/=\?\^_`{\|}~-][\w!#\$%&\'\*\+\/=\?\^_`{\|}~\.-]*@[\w\.-]+\.\w{2,}$/ ) )
  {
    return true;
  }
  
  return false;
}

function __check_field( elements )
{
  var fields = new Array();
  for( i = 0; i < elements.length; ++i )
  {
    var elem = elements[i];
    var text = elem.getAttribute( 'gt:necessary' );
    if( ! text )
      continue;
    text = '(' + text +')';
    var attr = eval( text );
    if( attr && attr.necessary == 1 )
    {
      var str = elem.value;
      str = str.trim();
      if( str.length == 0 )
      {
        fields[fields.length] = attr.name;
      }
    }
  }
  return fields;
}

function onScreenHelp( strID, x, y )
{
  if( document.all )
  {
    document.getElementById( strID ).style.left = event.x + document.body.scrollLeft + 15;
    document.getElementById( strID ).style.top  = event.y + document.body.scrollTop  + 0;
  }
  else
  {
    document.getElementById( strID ).style.left = x + 15;
    document.getElementById( strID ).style.top  = y + 0;
  }
  document.getElementById( strID ).style.display = '';
}

function offScreenHelp( strID )
{
  document.getElementById( strID ).style.display = 'none';
  strID = '';
}

String.prototype.trim = function()
{
  return this.replace(/^(\s|\r|\n)+|(\s|\r|\n)+$/g, '');
}

String.prototype.str_head = function( len, word )
{
  if( ! word )
    word = '...';

  if( this.length > len )
  {
    return this.substr( 0, len - word.length ) + word;
  }
  
  return this;
}

function Rect( x, y, width, height )
{
  this.x = x;
  this.y = y;
  this.width = width;
  this.height = height;
  
}

Rect.prototype.getElementRect = function( target )
{
  if( target.display != 'none' )
  {
    var top = target.offsetTop;
    var left = target.offsetLeft;
    var obj = target.offsetParent;
    for( ;; )
    {
      if( ! obj )
        break;

      top += obj.offsetTop;
      left += obj.offsetLeft;

      obj = obj.offsetParent;
    }

    this.x = left;
    this.y = top;
    this.width = target.offsetWidth;
    this.height = target.offsetHeight;

    return true;
  }

  return false;
}

Rect.prototype.PtInRect = function(point)
{
  if( this.x > point.x || (this.x+this.width ) < point.x )
  	return false;
  if( this.y > point.y || (this.y+this.height ) < point.y )
  	return false;

  return true;
}

Rect.prototype.toString = function()
{
  return 'X:' + this.x + ' Y:' + this.y + ' Width:' + this.width + ' Height:' + this.height;
}

function Point( x, y )
{
  this.x = x;
  this.y = y;
}

Point.prototype.toString = function()
{
  return 'X:' + this.x + ' Y:' + this.y;
}

function change_order_top( id )
{
  var selected_options = new Array();
  var unselected_options = new Array();
  
  var target = document.getElementById( id );
  for( i = 0 ; i < target.options.length; i ++ )
  {
    if( target.options[i].value == '' )
      continue;
    if( target.options[i].selected )
    {
      selected_options[selected_options.length] = target.options[i];
    }
    else
    {
      unselected_options[unselected_options.length] = target.options[i];
    }
  }
  
  for( i = target.options.length - 1 ; i >= 0 ; i -- )
  {
    target.options[i] = null;
  }
  
  for( i = 0 ; i < selected_options.length ; i++ )
  {
    target.options[target.options.length] = selected_options[i];
  }
  for( i = 0 ; i < unselected_options.length ; i++ )
  {
    target.options[target.options.length] = unselected_options[i];
  }
  
  target.options[target.options.length] = new Option( '                    ', '' );
}

function change_order_bottom( id )
{
  var selected_options = new Array();
  var unselected_options = new Array();
  
  var target = document.getElementById( id );
  for( i = 0 ; i < target.options.length; i ++ )
  {
    if( target.options[i].value == '' )
      continue;

    if( target.options[i].selected )
    {
      selected_options[selected_options.length] = target.options[i];
    }
    else
    {
      unselected_options[unselected_options.length] = target.options[i];
    }
  }
  
  for( i = target.options.length - 1 ; i >= 0 ; i -- )
  {
    target.options[i] = null;
  }
  
  for( i = 0 ; i < unselected_options.length ; i++ )
  {
    target.options[target.options.length] = unselected_options[i];
  }
  for( i = 0 ; i < selected_options.length ; i++ )
  {
    target.options[target.options.length] = selected_options[i];
  }
  
  target.options[target.options.length] = new Option( '                    ', '' );
}

function change_otpion( opts, idx1, idx2 )
{
  var text = opts[idx1].text;
  var value = opts[idx1].value;
  var selected = opts[idx1].selected;
  
  opts[idx1].text = opts[idx2].text;
  opts[idx1].value = opts[idx2].value;
  opts[idx1].selected = opts[idx2].selected;
  opts[idx2].text = text;
  opts[idx2].value = value;
  opts[idx2].selected = selected;
}

function change_order_up( id )
{
  var target = document.getElementById( id );
  for( i = 0 ; i < target.options.length  ; i ++ )
  {
    if( target.options[i].value == '' )
      continue;
    
    if( i != 0 && target.options[i].selected )
    {
      change_otpion( target.options, i -1, i );
    }
  }
}

function change_order_down( id )
{
  var target = document.getElementById( id );
  for( i = target.options.length - 2 ; i >= 0 ; i -- )
  {
    if( target.options[i].value == '' )
      continue;
    
    if( i < target.options.length - 2 && target.options[i].selected )
    {
      change_otpion( target.options, i, i+1 );
    }
  }
}

function is_alive_window( winVar )
{
  if( !! winVar )
  {
    if( document.all )
    {
      if( typeof( winVar.document ) != 'object' )
      {
        return false;
      }
    }
    else
    {
      if ( winVar.closed )
      {
          return false;
      }
    }
  }
  else
  {
    return false;
  }

  return true;
}
