/*********************************************
 * ファイル名 : function.js
 * 概要       : 共通関数
 * 履歴       : 2007/02 S.Ito
 *********************************************/
 

var browserType;

//ブラウザをチェック
if(navigator.appName.indexOf("Microsoft") > -1){ 
  browserType = "ms";
} else {
  browserType = "";
}

var clicked = false;

// 登録時の二重クリック防止用*
function checkClick( nextform ){
    if ( clicked ) return;
    clicked = true;
    document.getElementById(nextform).submit();
}

// 説明用サブウィンドウ
function openMemos( url ){
    window.open(url, "subwindow", "directories=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");
}

// 説明用サブウィンドウ（小）
function openMemosS( url ){
    window.open(url, "subwindow", "width=330, height=260, directories=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no");
}

function convertToDateStr( date_obj ){
  var date_strings;
  date_strings = date_obj.getFullYear() + "/" + (date_obj.getMonth()+1) + "/" + date_obj.getDate();
  if(date_strings.substr(7,1) != "/") date_strings = date_strings.substr(0,5) + "0" + date_strings.substr(5,4);
  if(date_strings.length < 10) date_strings = date_strings.substr(0,8) + "0" + date_strings.substr(8,1);
  return date_strings;
}

function convertToDateObj( date_strings ){
  var date_obj;
  tmp_string_array = date_strings.split("/");
  date_obj = new Date( tmp_string_array[0], tmp_string_array[1], tmp_string_array[3] );
  return date_obj;
}

function setnl2br( obj, frameObj, data_strings ){
  tmp_string_array = data_strings.split("\n");
  
  for( t = 0; t < tmp_string_array.length; t++ ){
    if( t != 0 ) obj.appendChild(frameObj.createElement("br"));
    obj.appendChild(frameObj.createTextNode(tmp_string_array[t]));
  }
  return obj;
}

function clearNode( id ){
    obj = document.getElementById( id );
    nodeCount = obj.childNodes.length;
    for(t = 0; t < nodeCount; t++){
        obj.removeChild(obj.firstChild);
    }
}

function clearNodeObj( obj ){
    nodeCount = obj.childNodes.length;
    for(t = 0; t < nodeCount; t++){
        obj.removeChild(obj.firstChild);
    }
    return obj;
}


function clearTextNodeObj( obj ){
    nodeCount = obj.childNodes.length;
    s = 0;
    for(t = 0; t < nodeCount; t++){
      if( obj.childNodes[s].nodeType == 3 ){
        obj.removeChild(obj.childNodes[s]);
      } else s++;
    }
    return obj;
}

function clearTextNodeCal( obj ){
    nodeCount = obj.childNodes.length;
    s = 0;
    for(t = 0; t < nodeCount; t++){
        obj.removeChild(obj.childNodes[s]);
    }
}

function modifyTextNode( id, newtext ){

    obj = document.getElementById( id );
    nodeCount = obj.childNodes.length;
    s = 0;
    for(t = 0; t < nodeCount; t++){
      if( obj.childNodes[s].nodeType == 3 ){
        obj.removeChild(obj.childNodes[s]);
      } else s++;
    }
    obj.appendChild(document.createTextNode( newtext ) );
}

function modifyTextNodeObj( obj, newtext, frameObj){

    if( frameObj == "" ) frameObj = document;
    nodeCount = obj.childNodes.length;
    s = 0;
    for(t = 0; t < nodeCount; t++){
      if( obj.childNodes[s].nodeType == 3 ){
        obj.removeChild(obj.childNodes[s]);
      } else s++;
    }
    obj.appendChild( frameObj.createTextNode( newtext ) );
    return obj;
}

function copyChildNodes( obj, newTag ){
    var newObj;
    
    newObj = document.createElement( newTag );
    nodeCount = obj.childNodes.length;
    for(t = 0; t < nodeCount; t++){
      newObj.appendChild(obj.childNodes[0]);
    }
    return newObj;
}

function modifyStyleClass( id, className ){

    obj = document.getElementById( id );
    if( browserType == "ms" ) {
      obj.setAttribute('className', className);
    } else {
      obj.setAttribute('class', className);
    }
}

function modifyStyleClassObj( obj, className ){
    if( browserType == "ms" ) {
      obj.setAttribute('className', className);
    } else {
      obj.setAttribute('class', className);
    }
    return obj;
}

function getInputBox( input_name , frameObj) {

    if( frameObj == "" ) frameObj = document;
    if( browserType == "ms" ) {
      newInput = frameObj.createElement('<input name="' + input_name + '">');
    } else {
      newInput = frameObj.createElement('input');
      newInput.setAttribute("name", input_name);
    }
    return newInput;
}

function getTextArea( input_name , frameObj) {

    if( frameObj == "" ) frameObj = document;
    if( browserType == "ms" ) {
      newInput = frameObj.createElement('<textarea name="' + input_name + '">');
    } else {
      newInput = frameObj.createElement('textarea');
      newInput.setAttribute("name", input_name);
    }
    return newInput;
}

function getNumSelectBox( input_name, min_number ,max_number, frameObj ) {

    if( frameObj == "" ) frameObj = document;
    if( min_number == "" ) min_number = 0;
    if( browserType == "ms" ) {
      newSelect = frameObj.createElement('<select name="' + input_name + '">');
    } else {
      newSelect = frameObj.createElement('select');
      newSelect.setAttribute("name", input_name);
    }
    
    if( max_number > 10 ) max_number = 10;
    for( t = min_number; t <=  max_number; t++ ){
      newOption = frameObj.createElement('option');
      newOption.value = t;
      newOption.appendChild( frameObj.createTextNode( t ) );
      newSelect.appendChild( newOption );
    }

    return newSelect;
}

function setOnClick( obj, funcName) {
    if( browserType == "ms" ){
      obj.setAttribute('onclick', new Function( funcName ) );
    } else {
      obj.setAttribute('onclick', funcName );
    }
    return obj;
}

function setOnChange( obj, funcName) {
    if( browserType == "ms" ){
      obj.setAttribute('onchange', new Function( funcName ) );
    } else {
      obj.setAttribute('onchange', funcName );
    }
    return obj;
}


function makePriceFormat( price_int ){

  if( price_int >= 1000 ) {
    var tmp_str;
    var price_str;
    var tmp_position;
    
    price_str = "";
    
    tmp_str = "p" + price_int;
    tmp_position = (tmp_str.length) % 3 + 3
    if( tmp_position == 5 ) tmp_position = 2;
    
    price_str = tmp_str.substring(1,tmp_position);
    price_str += "," + tmp_str.substring(tmp_position,tmp_position + 3 );
    tmp_position += 3;
    while( tmp_position < tmp_str.length ){
      price_str += "," + tmp_str.substring(tmp_position,tmp_position + 3 );
      tmp_position += 3
    }
    
  } else {
    price_str = price_int;
  }
  return price_str;
}
