// File Name : format.js
// File Type : Java script
// Function  : Change/validate format of user input utility functions
// Date      : 13.Sept.2001
// Author    : Katherine Lee


/* 
 * Convert user input in key field to UPPERCASE
 *
 * Function name        j_chg_to_upper
 * @param               po_key            (Key field object)
 */
function j_chg_to_upper(po_key) {
  po_key.value = po_key.value.toUpperCase();
}

/* 
 * Convert user input in key field to Lowercase
 *
 * Function name        j_chg_to_lower
 * @param               po_key            (Key field object)
 */
function j_chg_to_lower(po_key) {
  po_key.value = po_key.value.toLowerCase();
}

/* 
 * Alltrim user input
 *
 * Function name        j_trim_fld
 * @param               po_key            (Key field object)
 */
function j_trim_fld(po_key) {
  po_key.value = j_trim(po_key.value);
}

/* 
 * Trim spaces in front of and at the end of a string 
 *
 * Function name        j_trim
 * @param               ps_key            
 */
function j_trim(ps_key) {

  while (ps_key.substring(0,1) == ' ') {
    ps_key = ps_key.substring(1);
  }

  var ji_endspace = ps_key.length;

  while (ps_key.substring(ji_endspace-1,ji_endspace) == ' ') {	
    ps_key      = ps_key.substring(0,ji_endspace-1);
    ji_endspace = ji_endspace-1;    
  }

  return ps_key;
}

/*
 *
 * Function name        j_validate_email
 *
 * @param               ps_email    Email address
 *
 * @return              valid or not            
 */
function j_validate_email(ps_email) {

  var js_reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
  var js_reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
  if (!js_reg1.test(ps_email) && js_reg2.test(ps_email)) { // if syntax is valid
    return true;
  }
  return false;
}


/*
 * validate login id
 *
 * Function name     j_validate_login_id 
 * @param            ps_string: login id    
 * @return           valid or not
 *
 */
function j_validate_login_id(ps_string) {
  if (ps_string.length < 6 || ps_string.length > 20) {
    return false;
  } else {
    return j_is_char_num(ps_string);
  }
}


/*
 * validate password  
 *
 * Function name     j_validate_pwd
 * @param            ps_string: password 
 * @return           valid or not
 *
 */
function j_validate_pwd(ps_string) {
  if (ps_string.length < 6 || ps_string.length > 20) {
    return false;
  } else {
    return j_is_char_num(ps_string);
  }
}


/*
 * validate the string contains a-z/A-Z/0-9 only 
 *
 * Function name     j_is_char_num 
 * @param            ps_string: any string 
 * @return           valid or not
 *
 */
function j_is_char_num(ps_string){
  for (var i = 0; i < ps_string.length; i++){
    var ch = ps_string.substring(i, i + 1);
    if (ch != '-' && (ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch) && (ch < "0" ||"9" < ch)){
      return false;
    }
  }
  return true;
}

/*
 * convert '+' to '%2B' in a string 
 *
 * Function name     j_conv_plus(ps_string)
 * @param            ps_string:  any string
 * @return           js_tmp:     converted string 
 *
 */
function j_conv_plus(ps_string) {
  js_in  = "+" ; 
  js_out = "%2B" ; 
  js_tmp = "" + ps_string; 

  while (js_tmp.indexOf(js_in)>-1) {
    ji_pos = js_tmp.indexOf(js_in);
    js_tmp = "" + (js_tmp.substring(0, ji_pos) + js_out + 
             js_tmp.substring((ji_pos + js_in.length), js_tmp.length));
  }
  return js_tmp;
}



