﻿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.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.elail);
    return false;
  }
  if(myForm.maker.value.match(/^\s*$/)){
    alertAndFocus("It is not inputted to 'Maker'", myForm.maker);
    return false;
  }
  if(myForm.car_name.value.match(/^\s*$/)){
    alertAndFocus("It is not inputted to 'Model'", myForm.car_name);
    return false;
  }
  if(myForm.gread.value.match(/^\s*$/)){
    alertAndFocus("It is not inputted to 'Glade'", myForm.gread);
    return false;
  }
  if(myForm.car_year1.value == 0){
    alertAndFocus("It is not inputted to 'Year'", myForm.car_year1);
    return false;
  }
  if(myForm.car_year2.value == 0){
    alertAndFocus("It is not inputted to 'Year'", myForm.car_year2);
    return false;
  }
  if(myForm.color1.value == 0){
    alertAndFocus("It is not inputted to 'Color'", myForm.color1);
    return false;
  }
  if(myForm.color2.value == 0){
    alertAndFocus("It is not inputted to 'Color'", myForm.color2);
    return false;
  }
  if(myForm.kyori.value == 0){
    alertAndFocus("It is not inputted to 'Mileage'", myForm.kyori);
    return false;
  }
  if(myForm.price.value == 0){
    alertAndFocus("It is not inputted to 'Budget'", myForm.price);
    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.keitai.value, "0123456789-")){
    alertAndFocus("The letter which cannot be used for 'Evening Phone' is included", myForm.keitai);
    return false;
  }
  if(! isLegalChar(myForm.fax.value, "0123456789-")){
    alertAndFocus("The letter which cannot be used for 'FAX Number' is included", myForm.fax);
    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;
}
