/// JavaScript

///common ///////////////////////////////////////////////////////////////
function numberFormat(num){
 return num.toString().replace(/([\d]+?)(?:(\d{3}\.\d+)|(?=(?:\d{3})+(?:\.\d+)?$))/g, "$1,$2");
}
function cmd(action, param){
  if(param != ''){
     param = '&' + param;
  }
  script = action + '.php?' + param + "&rd=" + Math.random();
  requestFile( '' , 'GET', script , true );
}

function cmd_ajax(action, param){
  if(param != ''){
     param = '&' + param;
  }
  script = action + '_ajax.php?' + param + "&rd=" + Math.random();
  requestFile( '' , 'GET', script , true );
}


///ajax ///////////////////////////////////////////////////////////////////
var DEBUG=0;
var httpoj; //[1]

//XMLHttpRequest
function createHttpRequest(){
  //Win ie
  if(window.ActiveXObject){
      try {
          //MSXML2
          return new ActiveXObject('Msxml2.XMLHTTP') //[1]'
      } catch (e) {
          try {
              //old MSXML
              return new ActiveXObject('Microsoft.XMLHTTP') //[1]'
          } catch (e2) {
              return null
          }
       }
  } else if(window.XMLHttpRequest){
      //is not IE XMLHttpRequest
      return new XMLHttpRequest() //[1]'
  } else {
      return null
  }
}

function requestFile( data , method , fileName , async ){
  if(DEBUG==1){
	  //debug DIV
	  var command_div = document.getElementById('command');
	  if(command_div){
	    var s = '';
	    if(method == 'POST'){
	      s = '?';
	    }
	    command_div.innerHTML      = fileName + 's' + data;
	  }
	}

  //XMLHttpRequest
  httpoj = createHttpRequest(); //[1]

  if(!httpoj){
    alert('このブラウザでは、ご利用できません。');
    return false;
  }

  //open
  httpoj.open( method , fileName , async ) //[2]
  if(method == 'POST') {
    httpoj.setRequestHeader('Content-Type' , 'application/x-www-form-urlencoded');
  }
  //handler
  httpoj.onreadystatechange = function()  //[4]
  { 
    //readyState=4
    if (httpoj.readyState==4 && httpoj.status==200)  //[5]
    { 
      //callback
      on_loaded(httpoj);
    }
  }
  //send
  httpoj.send( data ); //[3]
}

//callback
function on_loaded(oj){
      res  = oj.responseText; //[6]
      if(DEBUG==1){
				alert(res);
      }
      //run
      eval(res);
}

//form
function parseForm(form_name){
  var fme = document.forms[form_name].elements;
  var query = '';
  for(i=0; i < fme.length; i++){
    var tagName = fme[i].tagName;
    var type = fme[i].type;

    if(tagName == 'INPUT'){
      if (type == 'text' || type == 'hidden') {
        query = '&' + query + fme[i].name + '=' + encodeURIComponent(fme[i].value);
      }
      //radio
      else if(type == 'radio' || type == 'checkbox'){
        if(fme[i].getAttribute('checked')) {
          query = query + '&' + fme[i].name + '=' + encodeURIComponent(fme[i].value);
        }
      }
    }
    else if(tagName == 'SELECT'){
      var index = fme[i].selectedIndex;
      var str = fme[i].options[index].value;
      query = query + '&' + fme[i].name + '=' + encodeURIComponent(str);
    }
  }
  return query;
}

//POST
function requestPOST(form_name, script){
  var query = parseForm(form_name);
  requestFile('rd=' + Math.random() + query , 'POST', script , true );
}

//GET
function requestGET(form_name, script){
  var query = parseForm(form_name);
  requestFile( '' , 'GET', script + '?rd=' + Math.random() + query , true );
}





function on_zipcode(id,ret1,ret2,ret3){
  var zipcode = document.getElementById(id).value;
  if(zipcode != ''){
     var param = 'zp=' + encodeURIComponent(zipcode) +
             '&sel=' + encodeURIComponent(ret1) +
             '&ret2=' + encodeURIComponent(ret2) +
             '&ret3=' + encodeURIComponent(ret3);
     cmd_ajax('getaddress', param);
  }
}

function select_box(id, val){
	var slc = document.getElementById(id);
	for(var i=0; i < slc.options.length; i++) {
		//option要素の値を確認
		if(slc.options[i].value == val){
			slc.options[i].selected = true;
		}
		else {
			slc.options[i].selected = false;
		}
	}
}
