﻿function formCheck(){
  if(nullCheck() == false){ return false; }
  if(formatCheck() == false){ return false; }
  document.form1.submit();
}

function nullCheck(){
  var myForm = document.form1;

  if(myForm.name.value.match(/^\s*$/)){
    alertAndFocus("It is not inputted to 'Name'", myForm.name);
    return false;
  }
  if(myForm.post.value.match(/^\s*$/)){
    alertAndFocus("It is not inputted to 'Postal code'", myForm.post);
    return false;
  }
  if(myForm.prefecture.value == 0){
    alertAndFocus("It is not inputted to 'Prefecture'", myForm.prefecture);
    return false;
  }
  if(myForm.prefecture.value.match(/^\s*$/)){
    alertAndFocus("It is not inputted to 'Prefecture'", myForm.prefecture);
    return false;
  }
  if(myForm.address1.value.match(/^\s*$/)){
    alertAndFocus("It is not inputted to 'Other Address'", myForm.address1);
    return false;
  }
  if(myForm.tel.value.match(/^\s*$/)){
    alertAndFocus("It is not inputted to 'Daytime Phone'", myForm.tel);
    return false;
  }
  if(myForm.email.value.match(/^\s*$/)){
    alertAndFocus("It is not inputted to 'E-mail Address'", myForm.email);
    return false;
  }
  if( ! myForm.email.value.match(/^[^@]+@[^@]+(\.[^@]+)+$/) ){
    alertAndFocus("It is not inputted to 'E-mail Address'", myForm.email);
    return false;
  }
  if(myForm.inquiry.value.match(/^\s*$/)){
    alertAndFocus("It is not inputted to 'Comment'", myForm.inquiry);
    return false;
  }

}

function formatCheck(){
  var myForm = document.form1;

  if(! isLegalChar(myForm.post.value, "0123456789-")){
    alertAndFocus("The letter which cannot be used for 'Postal code' is included", myForm.post);
    return false;
  }
  if(! isLegalChar(myForm.tel.value, "0123456789-")){
    alertAndFocus("The letter which cannot be used for 'Daytime Phone' is included", myForm.tel);
    return false;
  }
  if(! isLegalChar(myForm.email.value, "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~")){
    alertAndFocus("The letter which cannot be used for 'E-mail Address' is included", myForm.email);
    return false;
  }
  if(myForm.email.value.indexOf("@") == -1){
    alertAndFocus("The format of the 'E-mail Address' is wrong", myForm.email);
    return false;
  }

}

function alertAndFocus(alrtMsg, obj){
  alert(alrtMsg);
  obj.focus();
}

function alertAndJump(alrtMsg, dst){
  alert(alrtMsg);
  location.hash = dst;
}

function radioCheck(radioObj){
  var isChecked = 0;
  var index = 0;
  var i;
  for(i = 0; i < radioObj.length; i++){
    if(radioObj[i].checked){
      isChecked++;
      index = i;
      break;
    }
  }
  if(i == radioObj.length){
    return false;
  }
  else{
    return radioObj[index];
  }
}

function isLegalChar(target, canUse){
  for(i = 0; i < target.length; i++){
    if(canUse.indexOf(target.charAt(i)) == -1){
      return false;
    }
  }
  return true;
}
