﻿/*
 * input check functions
 */

NowDate  = new Date();
NowYear  = NowDate.getFullYear();
NowMonth = NowDate.getMonth() +1;
NowDay   = NowDate.getDate();

/*
 * arg : str  string
 *       from numeric
 *       to   numeric
 */
function IsStrLenBetween( str, from, to ){
  if( str.length < from || to < str.length ){
    return false;
  }
  return true;
}

/*
 * arg : str  string
 *       from numeric
 *       to   numeric
 */
function IsStrByteBetween( str, from, to ){
  var bytesize = getStrByteSize( str );
  if( bytesize < from || to < bytesize ){
    return false;
  }
  return true;
}

/*
 * arg : str string
 * return : true all is Ascii char
 */
function IsAsciiAll( str ){
  var i = 0;

  if( str.length <= 0 ){
    return false;
  }

  for( i = 0; i < str.length; i++){
    if( str.charCodeAt(i) > 127){
      return false;
    }
  }
  return true;
}

/*
 * arg : num string
 * return : true all is Ascii number 
 */
function IsAsciiNum( str ){
  var i = 0;
  var code = 0;

  if( str.length <= 0 ){
    return false;
  }

  for( i = 0; i < str.length; i++){
    code = str.charCodeAt(i);
    if( code < 48 || code > 57 ){
      return false;
    }
  }
  return true;
}

/*
 * arg : str string
 * return : true all is Ascii alphabet
 */
function IsAsciiAlph( str ){
  var i = 0;
  var code = 0;

  if( str.length <= 0 ){
    return false;
  }

  for( i = 0; i < str.length; i++){
    code = str.charCodeAt(i);
    if( ( code < 65 || code > 90  ) ||
        ( code < 97 || code > 122 )
    ){
      return false;
    }
  }
  return true;
}

/*
 * arg : str string
 * return : true all is Ascii number or alphabet
 */
function IsAsciiNumAlph( str ){
  var i = 0;
  var code = 0;

  if( str.length <= 0 ){
    return false;
  }

  for( i = 0; i < str.length; i++){
    code = str.charCodeAt(i);
    if( ( code < 48 || code > 57  ) &&
        ( code < 65 || code > 90  ) &&
        ( code < 97 || code > 122 )
    ){
      return false;
    }
  }
  return true;
}

/*
 * arg : str  string
 * return : true all is space
 */
function IsAllSpace( str ){
  var i = 0;
  var flag = true;

  for( i = 0; i < str.length; i++){
    if( ( str.charAt( i ) != " " ) &&
         ( str.charAt( i ) != "　" )
    ){
      flag = false;
      break;
    }
  }
  return flag;
}

/*
 * arg : year  string
 *       month string
 *       day   string
 * return : true The right date 'y-m-d'
 */
function IsDate( year, month, day){
  var i_year  = 0;
  var i_month = 0;
  var i_day   = 0;

  if( year == "" ||
       month == "" ||
       day == "" ){
    return false;
  }

  i_year  = parseInt( year  );
  i_month = parseInt( month );
  i_day   = parseInt( day   );

  if( i_year < 1 ){
    return false;
  }

  if( i_month < 1 || i_month > 12 ){
    return false;
  }

  if( getLastDayOfMonth( i_year, i_month ) < i_day){
    return false;
  }
  return true;
}

/*
 * arg : add string
 * return : true The right mail address
 */
function IsMailAddress( add ){
  if( add.match(/^[a-zA-Z\d][\w\d\-\.]*@([0-9A-Za-z\-]+\.)+[A-Za-z]+$/) == null ){
    return false;
  }
  return true;
}

/*
 * arg : str string
 * return : byte size of str
 */
function getStrByteSize( str ){
  var i = 0;
  var count = 0;

  for( i = 0; i < str.length; i++){
    if( str.charCodeAt( i ) > 127){
      count += 2;
    } else {
      count++;
    }
  }
  
  return count;
}

/*
 * arg : year  numeric
 *       month numeric
 *
 * retuen : the last day of month
 */
function getLastDayOfMonth( year, month ){
  switch( month ){
    case 1 :
    case 3 :
    case 5 :
    case 7 :
    case 8 :
    case 10:
    case 12: return 31;
    case 4 :
    case 6 :
    case 9 :
    case 11: return 30;
    case 2 : 
      if( ( ( year % 4  ) ==  0 ) &&
          ( 
            ( ( year % 100) !=  0 ) ||  
            ( ( year % 400) ==  0 )
          )
      ){ 
        return 29;
      } else {
        return 28;
      }
  }
}

/*
 * arg : year  string
 *       month string
 *       day   string
 *       hour  string
 * return : true The right date 'y-m-d-h'
 */
function IsDateHour( year, month, day, hour){
  var i_year  = 0;
  var i_month = 0;
  var i_day   = 0;
  var i_hour  = 0;

  if( year == "" || month == "" || day == "" || hour == ""){
    return false;
  }

  i_year  = parseInt( year  );
  i_month = parseInt( month );
  i_day   = parseInt( day   );
  i_hour  = parseInt( hour  );

  if( i_year < 1 ){
    return false;
  }

  if( i_month < 1 || i_month > 12 ){
    return false;
  }

  if( getLastDayOfMonth( i_year, i_month ) < i_day){
    return false;
  }

  if( i_hour < 0 || i_hour > 23 ){
    return false;
  }
  return true;
}

